summaryrefslogtreecommitdiff
path: root/src/kernel/timer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/kernel/timer.cc')
-rw-r--r--src/kernel/timer.cc28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/kernel/timer.cc b/src/kernel/timer.cc
new file mode 100644
index 0000000..8cb3b0f
--- /dev/null
+++ b/src/kernel/timer.cc
@@ -0,0 +1,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));
+ }
+} \ No newline at end of file