diff options
author | Benji Dial <benji@benjidial.net> | 2024-07-31 13:36:53 -0400 |
---|---|---|
committer | Benji Dial <benji@benjidial.net> | 2024-07-31 13:36:53 -0400 |
commit | b1cf9e5dfbc8967bd7cb2a22ec1e5e521f4e0e6e (patch) | |
tree | 00837891f9b9bf232e540a6f9b3e16f2438865c3 /euler | |
parent | 86b343f17175ef3e1fad2197636f75770466aa7c (diff) | |
download | hilbert-os-b1cf9e5dfbc8967bd7cb2a22ec1e5e521f4e0e6e.tar.gz |
add clock
Diffstat (limited to 'euler')
-rw-r--r-- | euler/include/ctime | 20 | ||||
-rw-r--r-- | euler/include/euler/syscall.hpp | 5 | ||||
-rw-r--r-- | euler/include/std/string.hpp | 10 | ||||
-rw-r--r-- | euler/makefile | 2 | ||||
-rw-r--r-- | euler/source/std/ctime.cpp | 58 | ||||
-rw-r--r-- | euler/source/syscall.cpp | 23 |
6 files changed, 117 insertions, 1 deletions
diff --git a/euler/include/ctime b/euler/include/ctime new file mode 100644 index 0000000..9cf1b1f --- /dev/null +++ b/euler/include/ctime @@ -0,0 +1,20 @@ +#pragma once + +#include <cstdint> + +typedef uint64_t time_t; + +struct tm { + int tm_sec; + int tm_min; + int tm_hour; + int tm_mday; + int tm_mon; + int tm_year; + int tm_wday; + int tm_yday; + int tm_isdst; +}; + +time_t time(time_t *arg); +tm *gmtime(const time_t *time); diff --git a/euler/include/euler/syscall.hpp b/euler/include/euler/syscall.hpp index 4a3daa4..043dcd4 100644 --- a/euler/include/euler/syscall.hpp +++ b/euler/include/euler/syscall.hpp @@ -159,6 +159,11 @@ namespace euler::syscall { void set_thread_name(const std::string &name); + void sleep(uint64_t mibiseconds); + + //out is mibiseconds since january 1st 2000 + uint64_t get_time(); + } #include <string> diff --git a/euler/include/std/string.hpp b/euler/include/std/string.hpp index 505ee69..c5a3d99 100644 --- a/euler/include/std/string.hpp +++ b/euler/include/std/string.hpp @@ -12,6 +12,8 @@ namespace std { std::vector<char> characters; public: + static const size_t npos = (size_t)-1; + constexpr string() : characters({'\0'}) {} constexpr string(const string &other) @@ -74,6 +76,14 @@ namespace std { return characters[pos]; } + constexpr string &erase(size_t index = 0, size_t count = npos) { + count = std::min(count, size() - index); + for (size_t i = index; i + count < size(); ++i) + characters[i] = characters[i + count]; + resize(size() - count); + return *this; + } + }; } diff --git a/euler/makefile b/euler/makefile index 62716f6..7574dea 100644 --- a/euler/makefile +++ b/euler/makefile @@ -1,6 +1,6 @@ LIBC_SOURCES = \ entry.cpp std/string.cpp std/cstring.cpp syscall.cpp std/cstdlib.cpp \ - heap.cpp syscall.asm std/cctype.cpp std/cstdio.cpp stream.cpp + heap.cpp syscall.asm std/cctype.cpp std/cstdio.cpp stream.cpp std/ctime.cpp clean: rm -rf build diff --git a/euler/source/std/ctime.cpp b/euler/source/std/ctime.cpp new file mode 100644 index 0000000..d83817a --- /dev/null +++ b/euler/source/std/ctime.cpp @@ -0,0 +1,58 @@ +#include <euler/syscall.hpp> +#include <ctime> + +time_t time(time_t *arg) { + time_t t = euler::syscall::get_time(); + if (arg) *arg = t; + return t; +} + +static tm static_tm; + +static int days_per_month[] = { + 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 +}; + +tm *gmtime(const time_t *time) { + + time_t t = *time / 1024; + + static_tm.tm_isdst = 0; + + static_tm.tm_sec = t % 60; t /= 60; + static_tm.tm_min = t % 60; t /= 60; + static_tm.tm_hour = t % 24; t /= 24; + static_tm.tm_wday = (t + 5) % 7 + 1; + static_tm.tm_year = (t / 1461) * 4 + 100; + + int days_into_quadyear = t % 1461; + + static_tm.tm_yday = 0; + static_tm.tm_mon = 0; + static_tm.tm_mday = 1; + + for (int i = 0; i < 48; ++i) { + if (days_into_quadyear >= days_per_month[i]) { + days_into_quadyear -= days_per_month[i]; + if (static_tm.tm_mon == 11) { + static_tm.tm_mon = 0; + static_tm.tm_yday = 0; + } + else { + ++static_tm.tm_mon; + static_tm.tm_yday += days_per_month[i]; + } + } + else { + static_tm.tm_yday += days_into_quadyear; + static_tm.tm_mday += days_into_quadyear; + break; + } + } + + return &static_tm; + +} diff --git a/euler/source/syscall.cpp b/euler/source/syscall.cpp index 0d30c4a..7637b10 100644 --- a/euler/source/syscall.cpp +++ b/euler/source/syscall.cpp @@ -407,4 +407,27 @@ namespace euler::syscall { } + void sleep(uint64_t mibiseconds) { + + uint64_t rax = 25; + uint64_t rdi = mibiseconds; + uint64_t rsi; + uint64_t rdx; + + __euler_do_syscall(rax, rdi, rsi, rdx); + + } + + uint64_t get_time() { + + uint64_t rax = 26; + uint64_t rdi; + uint64_t rsi; + uint64_t rdx; + + __euler_do_syscall(rax, rdi, rsi, rdx); + return rax; + + } + } |