From 27fc7bda593c8bd2c24a334a368772b5f15d4e9e Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Thu, 11 Dec 2025 16:34:51 +0800 Subject: feat:Cooperative Multitasking --- src/kernel/timer.cc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/kernel/timer.cc (limited to 'src/kernel/timer.cc') 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 -- cgit v1.2.3