#
# Example demonstrating how to handle interupts generated by "Timer Tool" of RARS.
#

.eqv CUR 0xFFFF0018 # current time
.eqv NEW 0xFFFF0020 # time for new interrupt

.macro timer(%timeout)
    lw     t0, CUR
    addi   t0, t0, %timeout
    sw     t0, NEW, t1
.end_macro

.text
    j main
handler:
    timer  2000 # generate new timer interupt after 2000 ms.
    uret # return to uepc
main:
    la     t0, handler
    csrrw  zero, utvec, t0  # set utvec to the handlers address
    csrrsi zero, ustatus, 0x1 # set interrupt enable bit in ustatus
    csrrsi zero, uie, 0x10 # timer interrupts are enabled (UTIE bit)
    timer  2000 # generate new timer interupt after 2000 ms.
loop:
    wfi
    j loop
    nop
