From 6f1b50a4cc6c232ee505a543f006abb1c6cd33cf Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Fri, 19 Feb 2021 23:41:56 -0500 Subject: rtc fixes, time command --- makefile | 13 ++++++---- src/kernel/cmos.c | 18 ++++++++++---- src/kernel/cmos.h | 1 + src/kernel/main.c | 1 + src/user/include/knob/time.h | 13 ++++++++++ src/user/knob/time.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ src/user/time/time.c | 24 +++++++++++++++++++ 7 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 src/user/include/knob/time.h create mode 100644 src/user/knob/time.c create mode 100644 src/user/time/time.c diff --git a/makefile b/makefile index 17dc0ec..611593a 100644 --- a/makefile +++ b/makefile @@ -37,7 +37,7 @@ out/fs/bin/%: obj/%.elf out/fs: out/fs/bin/init out/fs/bin/highway out/fs/bin/meminfo \ out/fs/bin/terminal out/fs/bin/hello out/fs/bin/mkpopup \ - out/fs/bin/dirlist out/fs/bin/ttt + out/fs/bin/dirlist out/fs/bin/ttt out/fs/bin/time touch out/fs cp -r fs-skel/* out/fs/ @@ -82,9 +82,10 @@ obj/c.rto: obj/runtimes/c/pcrt.ao obj/cpp.rto: #TODO -obj/knob.so: obj/knob/file.o obj/knob/format.o obj/knob/rand.o \ - obj/knob/heap.o obj/knob/ipc.o obj/knob/task.o \ - obj/knob/block.o obj/knob/key.o obj/knob/panic.o +obj/knob.so: obj/knob/file.o obj/knob/format.o obj/knob/rand.o \ + obj/knob/heap.o obj/knob/ipc.o obj/knob/task.o \ + obj/knob/block.o obj/knob/key.o obj/knob/panic.o \ + obj/knob/time.o ld ${partlink} $^ -o $@ obj/libterm.so: obj/libterm/terminal.o obj/libterm/termtask.o obj/libterm/readline.o @@ -126,4 +127,8 @@ obj/dirlist.elf: obj/dirlist/main.o obj/libterm.so obj/knob.so \ obj/ttt.elf : obj/ttt/main.o obj/popups.so obj/libfont.so \ obj/knob.so obj/c.rto + ld -T src/user/runtimes/c/elf.ld $^ -o $@ + +obj/time.elf: obj/time/time.o obj/libterm.so obj/knob.so \ + obj/c.rto ld -T src/user/runtimes/c/elf.ld $^ -o $@ \ No newline at end of file diff --git a/src/kernel/cmos.c b/src/kernel/cmos.c index 39f7c4f..1a95805 100644 --- a/src/kernel/cmos.c +++ b/src/kernel/cmos.c @@ -3,9 +3,22 @@ static inline uint8_t read_cmos(uint8_t reg) { outb(0x0070, reg); + for (uint32_t i = 0; i < 1000000; ++i) + ;//spin return inb(0x0071); } +static inline void write_cmos(uint8_t reg, uint8_t value) { + outb(0x0070, reg); + for (uint32_t i = 0; i < 1000000; ++i) + ;//spin + outb(0x0071, value); +} + +void cmos_init() { + write_cmos(0x0b, read_cmos(0x0b) | 0x06); +} + //The idea of reading until you get the same value twice, and checking the status register // is from the OSDev Wiki page at . struct rtc_time get_rtc_time() { @@ -21,11 +34,6 @@ get_sec: } ret.minutes = read_cmos(0x02); ret.hours = read_cmos(0x04); - if (ret.hours & 0x80) { - ret.hours -= 0x80 - 12; - if (ret.hours == 24) - ret.hours = 0; - } ret.day_of_week = read_cmos(0x06); ret.day_of_month = read_cmos(0x07); ret.month = read_cmos(0x08); diff --git a/src/kernel/cmos.h b/src/kernel/cmos.h index 53ec398..515de1d 100644 --- a/src/kernel/cmos.h +++ b/src/kernel/cmos.h @@ -14,5 +14,6 @@ struct rtc_time { }; struct rtc_time get_rtc_time(); +void cmos_init(); #endif \ No newline at end of file diff --git a/src/kernel/main.c b/src/kernel/main.c index 7e49049..36b5fc1 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -23,6 +23,7 @@ void _start_user_mode() __attribute__ ((noreturn)); __attribute__ ((noreturn)) void main() { + cmos_init(); init_pagemap(); init_paging(); init_tasks(); diff --git a/src/user/include/knob/time.h b/src/user/include/knob/time.h new file mode 100644 index 0000000..4b84697 --- /dev/null +++ b/src/user/include/knob/time.h @@ -0,0 +1,13 @@ +#include + +struct time { + uint16_t year;//ad + uint8_t month;//jan = 1 + uint8_t day; + uint8_t hour;//24-hour, midnight = 0 + uint8_t minute; + uint8_t second; + uint32_t timestamp; +}; + +struct time get_time(); \ No newline at end of file diff --git a/src/user/knob/time.c b/src/user/knob/time.c new file mode 100644 index 0000000..e3bbb08 --- /dev/null +++ b/src/user/knob/time.c @@ -0,0 +1,57 @@ +#include +#include +#include + +uint8_t days_in_months[] = { + 31, 0, 31, 30, + 31, 30, 31, 31, + 30, 31, 30, 31 +}; + +struct time get_time() { + struct time t; + + const uint32_t ts = _get_timestamp(); + t.timestamp = ts; + + const uint32_t time_part = ts % 86400; + t.hour = time_part / 3600; + t.minute = (time_part / 60) % 60; + t.second = time_part % 60; + + uint32_t days = ts / 86400; + + uint32_t year = 2000; + while (days >= 1461) { + year += 4; + days -= 1461; + } + + days_in_months[1] = 28; + if (days >= 1096) { + t.year = year += 3; + days -= 1096; + } + else if (days >= 731) { + t.year = year += 2; + days -= 731; + } + else if (days >= 366) { + t.year = ++year; + days -= 366; + } + else { + t.year = year; + days_in_months[1] = 29; + } + + uint8_t m = 0; + while (days >= days_in_months[m]) { + days -= days_in_months[m]; + ++m; + } + + t.month = m + 1; + t.day = days + 1; + return t; +} \ No newline at end of file diff --git a/src/user/time/time.c b/src/user/time/time.c new file mode 100644 index 0000000..49f6cf5 --- /dev/null +++ b/src/user/time/time.c @@ -0,0 +1,24 @@ +#include +#include + +static const char *const month_names[] = { + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December" +}; + +void main() { + const struct time t = get_time(); + term_addf("TS: 0x%8h\n", t.timestamp); + term_addf("Date: %s %u, %u\n", month_names[t.month - 1], t.day, t.year); + term_addf("Time: %u:%2u:%2u\n", t.hour, t.minute, t.second); +} \ No newline at end of file -- cgit v1.2.3