-
Notifications
You must be signed in to change notification settings - Fork 76
/
timer.c
36 lines (28 loc) · 931 Bytes
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "timer.h"
#define interval 10000000 // cycles; about 1 second in qemu.
void timer_init()
{
// each CPU has a separate source of timer interrupts.
int id = r_mhartid();
// ask the CLINT for a timer interrupt.
*(reg_t*)CLINT_MTIMECMP(id) = *(reg_t*)CLINT_MTIME + interval;
// set the machine-mode trap handler.
w_mtvec((reg_t)sys_timer);
// enable machine-mode interrupts.
w_mstatus(r_mstatus() | MSTATUS_MIE);
// enable machine-mode timer interrupts.
w_mie(r_mie() | MIE_MTIE);
}
static int timer_count = 0;
reg_t timer_handler(reg_t epc, reg_t cause)
{
reg_t return_pc = epc;
// disable machine-mode timer interrupts.
w_mie(~((~r_mie()) | (1 << 7)));
lib_printf("timer_handler: %d\n", ++timer_count);
int id = r_mhartid();
*(reg_t *)CLINT_MTIMECMP(id) = *(reg_t *)CLINT_MTIME + interval;
// enable machine-mode timer interrupts.
w_mie(r_mie() | MIE_MTIE);
return return_pc;
}