From 6c2829a0da87f2cacdfe76bd1ef190e1bff0d946 Mon Sep 17 00:00:00 2001 From: Riyan Dhiman Date: Fri, 26 Jul 2024 06:46:31 +0530 Subject: [PATCH] Time is working now --- .gitignore | 1 + kernel/arch/i386/make.config | 1 + kernel/arch/i386/time.c | 52 +++++++++++++++++++++++++++++++++ kernel/include/arch/i386/time.h | 16 ++++++---- kernel/kernel/io.c | 1 + kernel/kernel/kernel.c | 3 ++ 6 files changed, 68 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 8d4c8e1..9399ac3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ sysroot/* *.a iso*/* +.idea/* \ No newline at end of file diff --git a/kernel/arch/i386/make.config b/kernel/arch/i386/make.config index ada6ad5..602ebfa 100644 --- a/kernel/arch/i386/make.config +++ b/kernel/arch/i386/make.config @@ -8,5 +8,6 @@ $(ARCHDIR)/boot.o \ $(ARCHDIR)/tty.o \ ${ARCHDIR}/interrupts.o \ ${ARCHDIR}/x86.o \ +${ARCHDIR}/time.o \ ${ARCHDIR}/asm/interrupts.o \ diff --git a/kernel/arch/i386/time.c b/kernel/arch/i386/time.c index c8df5a2..a22ee40 100644 --- a/kernel/arch/i386/time.c +++ b/kernel/arch/i386/time.c @@ -1,2 +1,54 @@ #include +uint8_t read_cmos(uint8_t reg) +{ + outb(CMOS_ADDRESS, reg); + return inb(CMOS_DATA); +} + +void write_cmos(uint8_t reg, uint8_t value) +{ + outb(CMOS_ADDRESS, reg); + outb(CMOS_DATA, value); +} + + +uint8_t bcd_to_binary(uint8_t bcd) { + return ((bcd & 0xF0) >> 4) * 10 + (bcd & 0x0F); +} + +void convert_bcd_to_binary(struct kernel_time* rtc_datetime) { + rtc_datetime->second = bcd_to_binary(rtc_datetime->second); + rtc_datetime->minute = bcd_to_binary(rtc_datetime->minute); + rtc_datetime->hour = bcd_to_binary(rtc_datetime->hour); + rtc_datetime->day = bcd_to_binary(rtc_datetime->day); + rtc_datetime->month = bcd_to_binary(rtc_datetime->month); + rtc_datetime->year = bcd_to_binary(rtc_datetime->year); + rtc_datetime->century = bcd_to_binary(rtc_datetime->century); +} + +struct kernel_time read_rtc_hardware_time() +{ + struct kernel_time time; + + outb(CMOS_ADDRESS, 0x8A); + outb(CMOS_DATA, 0x20); + + time.second = read_cmos(0x00); + time.minute = read_cmos(0x02); + time.hour = read_cmos(0x04); + time.day = read_cmos(0x07); + time.month = read_cmos(0x08); + time.year = read_cmos(0x09); + time.century = read_cmos(0x32); + + outb(CMOS_ADDRESS, 0x8A); + outb(CMOS_DATA, 0x00); + return time; +} + +struct kernel_time rtc_time() { + struct kernel_time time = read_rtc_hardware_time(); + convert_bcd_to_binary(&time); + return time; +} \ No newline at end of file diff --git a/kernel/include/arch/i386/time.h b/kernel/include/arch/i386/time.h index 64f3118..c98e63e 100644 --- a/kernel/include/arch/i386/time.h +++ b/kernel/include/arch/i386/time.h @@ -1,8 +1,8 @@ #ifndef ARCH_I386_TIME_H #define ARCH_I386_TIME_H -##include "x86.h" - +#include +#include #define CMOS_ADDRESS 0x70 #define CMOS_DATA 0x71 @@ -16,10 +16,14 @@ struct kernel_time { uint8_t century; }; -void init_rtc(); -uint32_t read_rtc_time(); -void update_system_time(); -struct kernel_time convert_timestamp_to_datetime(uint32_t timestamp); +void init_rtc(void); +uint8_t read_cmos(uint8_t); +void write_cmos(uint8_t, uint8_t); +struct kernel_time read_rtc_hardware_time(void); +struct kernel_time rtc_time(void); +void convert_bcd_to_binary(struct kernel_time*); +void update_system_time(void); +struct kernel_time convert_timestamp_to_datetime(uint32_t); uint32_t convert_datetime_to_timestamp(struct kernel_time); void get_system_time(struct kernel_time *); int set_systme_time(struct kernel_time *); diff --git a/kernel/kernel/io.c b/kernel/kernel/io.c index c436a70..ce28205 100644 --- a/kernel/kernel/io.c +++ b/kernel/kernel/io.c @@ -1,4 +1,5 @@ #include + void outb(uint32_t ad, uint8_t v) { asm volatile("outb %%al, %%dx" :: "d" (ad), "a" (v)); diff --git a/kernel/kernel/kernel.c b/kernel/kernel/kernel.c index e82bec9..d66228a 100644 --- a/kernel/kernel/kernel.c +++ b/kernel/kernel/kernel.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -42,6 +43,8 @@ void kernel_main(void) { print_welcome_screen(); while(inb(0x60) & 0x80){}; terminal_initialize(); + struct kernel_time time = rtc_time(); + printf("current time: %d:%d:%d %d:%d:%d%d\n", time.hour, time.minute, time.second, time.day, time.month, time.century, time.year); printf("cheaky>"); while(1) { }