From 4bec0d073f8269f57dd9d2d20926dc3ebbf72598 Mon Sep 17 00:00:00 2001 From: Corwin Date: Mon, 14 Mar 2022 23:55:14 +0000 Subject: [PATCH] The poor profiler --- agb/examples/sprites.rs | 14 ++++++++++++++ agb/interrupt_handler.s | 12 ++++++++++++ agb/src/lib.rs | 8 ++++++++ agb/src/timer.rs | 5 +++++ 4 files changed, 39 insertions(+) diff --git a/agb/examples/sprites.rs b/agb/examples/sprites.rs index badda4a8..afb31424 100644 --- a/agb/examples/sprites.rs +++ b/agb/examples/sprites.rs @@ -5,6 +5,7 @@ extern crate alloc; use agb::display::object::{Graphics, ObjectController, Sprite, TagMap}; use alloc::vec::Vec; +use bare_metal::CriticalSection; const GRAPHICS: &Graphics = agb::include_aseprite!( "../examples/the-purple-night/gfx/objects.aseprite", @@ -100,6 +101,19 @@ fn all_tags(gfx: &ObjectController) { fn main(mut gba: agb::Gba) -> ! { let gfx = gba.display.object.get(); + let timers = gba.timers.timers(); + let mut my_timer = timers.timer0; + my_timer.set_interrupt(true); + my_timer.set_overflow_amount(10000); + my_timer.set_enabled(true); + + agb::add_interrupt_handler!( + agb::interrupt::Interrupt::Timer0, + |_key: &CriticalSection| { + agb::println!("{:#010x}", agb::get_program_counter_before_interrupt()); + } + ); + loop { all_tags(&gfx); all_sprites(&gfx); diff --git a/agb/interrupt_handler.s b/agb/interrupt_handler.s index a68814e8..8803ca4d 100644 --- a/agb/interrupt_handler.s +++ b/agb/interrupt_handler.s @@ -1,3 +1,4 @@ + @ An interrupt handler that simply acknowledges all interrupts .arm .global InterruptHandler @@ -14,6 +15,10 @@ InterruptHandler: ldrh r3, [r2, #2] @ load 16 bit interrupt request to r3 and r0, r1, r3 @ interrupts both enabled and requested + ldr r1, [sp, #20] + ldr r3, =agb_rs__program_counter + str r1, [r3] + @ change to system mode mrs r1, cpsr orr r1, r1, #0xD @@ -43,3 +48,10 @@ InterruptHandler: bx lr @ return to bios .pool + + +.section .iwram + .global agb_rs__program_counter + .balign 4 +agb_rs__program_counter: + .word 0 diff --git a/agb/src/lib.rs b/agb/src/lib.rs index 9d946b69..a4290c96 100644 --- a/agb/src/lib.rs +++ b/agb/src/lib.rs @@ -407,3 +407,11 @@ mod test { } } } + +#[inline(never)] +pub fn get_program_counter_before_interrupt() -> u32 { + extern "C" { + static mut agb_rs__program_counter: u32; + } + unsafe { agb_rs__program_counter } +} diff --git a/agb/src/timer.rs b/agb/src/timer.rs index f5ebcb7f..13ae810c 100644 --- a/agb/src/timer.rs +++ b/agb/src/timer.rs @@ -90,6 +90,11 @@ impl Timer { self.control_register().set_bits(bit, 1, 2); } + pub fn set_interrupt(&mut self, interrupt: bool) { + let bit = interrupt as u16; + self.control_register().set_bits(bit, 1, 6); + } + fn data_register(&self) -> MemoryMapped { timer_data(self.get_timer_number()) }