summaryrefslogtreecommitdiff
path: root/src/kernel/timer.cc
blob: 8cb3b0fe7a38979105ec102ba47f7355b998b59e (plain)
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
#include "common/types.h"
#include "kernel/timer.h"

#define CLINT_BASE 0x2000000L
#define CLINT_MTIMECMP(hartid) (CLINT_BASE + 0x4000 + 8 * (hartid))
#define CLINT_MTIME (CLINT_BASE + 0xBFF8)

#define INTERVAL 1000000

namespace Timer{
    uint64 get_time(){
        return *(volatile uint64*)CLINT_MTIME;
    }

    void set_next_trigger(){
        uint64 mtime = get_time();

        *(volatile uint64 *)CLINT_MTIMECMP(0) = mtime + INTERVAL;
    }

    void init(){
        set_next_trigger();
        uint64 sie;
        asm volatile("csrr %0, sie" : "=r"(sie));
        sie |= (1L << 5);
        asm volatile("csrw sie, %0" : : "r"(sie));
    }
}