-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace mtime counter with realtime timer
- Loading branch information
Showing
8 changed files
with
80 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ all: $(BIN) minimal.dtb | |
OBJS := \ | ||
riscv.o \ | ||
ram.o \ | ||
timer.o \ | ||
plic.o \ | ||
uart.o \ | ||
main.o \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <time.h> | ||
|
||
#include "riscv.h" | ||
|
||
#if defined(__APPLE__) | ||
#define HAVE_MACH_TIMER | ||
#include <mach/mach_time.h> | ||
#elif !defined(_WIN32) && !defined(_WIN64) | ||
#define HAVE_POSIX_TIMER | ||
#ifdef CLOCK_MONOTONIC | ||
#define CLOCKID CLOCK_MONOTONIC | ||
#else | ||
#define CLOCKID CLOCK_REALTIME | ||
#endif | ||
#endif | ||
|
||
void semu_timer_init(semu_timer_t *timer, uint64_t freq) | ||
{ | ||
timer->freq = freq; | ||
semu_timer_rebase(timer, 0); | ||
} | ||
|
||
uint64_t semu_timer_clocksource(uint64_t freq) | ||
{ | ||
#if defined(HAVE_POSIX_TIMER) | ||
struct timespec t; | ||
clock_gettime(CLOCKID, &t); | ||
return (t.tv_sec * freq) + (t.tv_nsec * freq / 1e9); | ||
#elif defined(HAVE_MACH_TIMER) | ||
static mach_timebase_info_data_t t; | ||
if (mach_clk.denom == 0) | ||
(void) mach_timebase_info(&t); | ||
return mach_absolute_time() * freq / t.denom * t.numer; | ||
#else | ||
return time(0) * freq; | ||
#endif | ||
} | ||
|
||
uint64_t semu_timer_gettime(semu_timer_t *timer) | ||
{ | ||
return semu_timer_clocksource(timer->freq) - timer->begin; | ||
} | ||
|
||
void semu_timer_rebase(semu_timer_t *timer, uint64_t time) | ||
{ | ||
timer->begin = semu_timer_clocksource(timer->freq) - time; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include <stdbool.h> | ||
#include <stdint.h> | ||
|
||
|
||
/* TIMER */ | ||
typedef struct { | ||
uint64_t begin; | ||
uint64_t freq; | ||
} semu_timer_t; | ||
|
||
void semu_timer_init(semu_timer_t *timer, uint64_t freq); | ||
uint64_t semu_timer_clocksource(uint64_t freq); | ||
uint64_t semu_timer_gettime(semu_timer_t *timer); | ||
void semu_timer_rebase(semu_timer_t *timer, uint64_t time); |