From 33e4744d1c73c9f5512abb4010e4cc81763ab3a2 Mon Sep 17 00:00:00 2001 From: cheapie Date: Fri, 19 Jun 2026 22:38:28 -0500 Subject: Add machine timer interrupt --- assembly/timer-test/timer-test.S | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 assembly/timer-test/timer-test.S (limited to 'assembly/timer-test/timer-test.S') diff --git a/assembly/timer-test/timer-test.S b/assembly/timer-test/timer-test.S new file mode 100644 index 0000000..e114f3f --- /dev/null +++ b/assembly/timer-test/timer-test.S @@ -0,0 +1,36 @@ +la t0,handler +csrw mtvec,t0 # Set trap handler address and mode (0/direct) +li t0,0x400 +csrc mstatush,t0 # Enable trap handler +li t0,0x80 +csrw mie,t0 # Enable machine timer interrupt and disable others + +la t0,mmio +csrw 0x801,t0 # Set up MMIO base address + +lw t1,2(t0) # Read current time +addi t1,t1,5 +sw t1,10(t0) # Set mtimecmp to 5 seconds in the future + +csrsi mstatus,8 # Enable interrupts globally + +wfi # Wait for the interrupt to trigger + +li a7,10 # Exit program +ecall + +.balign 4 +handler: +li t1,-1 +sw t1,10(t0) # Set mtimecmp to the distant future +csrr a0,mcause +li a7,1 # Print integer +ecall + +li a0,'\n' +li a7,11 # Print character +ecall + +mret + +mmio: -- cgit v1.2.3