Simplify the basic example and make a timing example

This commit is contained in:
Gwilym Inzani 2023-07-19 12:31:59 +01:00
parent 6f8633861d
commit a9236531c7
2 changed files with 55 additions and 20 deletions

View file

@ -11,35 +11,15 @@ const DB_TOFFE: Track = import_xm!("examples/db_toffe.xm");
fn main(mut gba: Gba) -> ! { fn main(mut gba: Gba) -> ! {
let vblank_provider = agb::interrupt::VBlank::get(); let vblank_provider = agb::interrupt::VBlank::get();
let timer_controller = gba.timers.timers();
let mut timer = timer_controller.timer2;
let mut timer2 = timer_controller.timer3;
timer.set_enabled(true);
timer2.set_cascade(true).set_enabled(true);
let mut mixer = gba.mixer.mixer(Frequency::Hz18157); let mut mixer = gba.mixer.mixer(Frequency::Hz18157);
mixer.enable(); mixer.enable();
let mut tracker = Tracker::new(&DB_TOFFE); let mut tracker = Tracker::new(&DB_TOFFE);
loop { loop {
let before_mixing_cycles_high = timer2.value();
let before_mixing_cycles_low = timer.value();
tracker.step(&mut mixer); tracker.step(&mut mixer);
mixer.frame(); mixer.frame();
let after_mixing_cycles_low = timer.value();
let after_mixing_cycles_high = timer2.value();
vblank_provider.wait_for_vblank(); vblank_provider.wait_for_vblank();
let before_mixing_cycles =
((before_mixing_cycles_high as u32) << 16) + before_mixing_cycles_low as u32;
let after_mixing_cycles =
((after_mixing_cycles_high as u32) << 16) + after_mixing_cycles_low as u32;
let total_cycles = after_mixing_cycles.wrapping_sub(before_mixing_cycles);
agb::println!("{total_cycles} cycles");
} }
} }

View file

@ -0,0 +1,55 @@
#![no_std]
#![no_main]
use agb::sound::mixer::Frequency;
use agb::Gba;
use agb_tracker::{import_xm, Track, Tracker};
const DB_TOFFE: Track = import_xm!("examples/db_toffe.xm");
#[agb::entry]
fn main(mut gba: Gba) -> ! {
let vblank_provider = agb::interrupt::VBlank::get();
let timer_controller = gba.timers.timers();
let mut timer = timer_controller.timer2;
let mut timer2 = timer_controller.timer3;
timer.set_enabled(true);
timer2.set_cascade(true).set_enabled(true);
let mut mixer = gba.mixer.mixer(Frequency::Hz18157);
mixer.enable();
let mut tracker = Tracker::new(&DB_TOFFE);
loop {
let before_mixing_cycles_high = timer2.value();
let before_mixing_cycles_low = timer.value();
tracker.step(&mut mixer);
let after_step_cycles_high = timer2.value();
let after_step_cycles_low = timer.value();
mixer.frame();
let after_mixing_cycles_low = timer.value();
let after_mixing_cycles_high = timer2.value();
vblank_provider.wait_for_vblank();
let before_mixing_cycles =
((before_mixing_cycles_high as u32) << 16) + before_mixing_cycles_low as u32;
let after_mixing_cycles =
((after_mixing_cycles_high as u32) << 16) + after_mixing_cycles_low as u32;
let after_step_cycles =
((after_step_cycles_high as u32) << 16) + after_step_cycles_low as u32;
let step_cycles = after_step_cycles - before_mixing_cycles;
let mixing_cycles = after_mixing_cycles - before_mixing_cycles;
let total_cycles = after_mixing_cycles.wrapping_sub(before_mixing_cycles);
agb::println!(
"step = {step_cycles}, mixing = {mixing_cycles}, total = {total_cycles} cycles"
);
}
}