Skip to content

Commit

Permalink
Time is working now
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryand1234 committed Jul 26, 2024
1 parent 91ae8c6 commit 6c2829a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
sysroot/*
*.a
iso*/*
.idea/*
1 change: 1 addition & 0 deletions kernel/arch/i386/make.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ $(ARCHDIR)/boot.o \
$(ARCHDIR)/tty.o \
${ARCHDIR}/interrupts.o \
${ARCHDIR}/x86.o \
${ARCHDIR}/time.o \
${ARCHDIR}/asm/interrupts.o \

52 changes: 52 additions & 0 deletions kernel/arch/i386/time.c
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
#include<arch/i386/time.h>

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;
}
16 changes: 10 additions & 6 deletions kernel/include/arch/i386/time.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef ARCH_I386_TIME_H
#define ARCH_I386_TIME_H

##include "x86.h"

#include<kernel/io.h>
#include<stdint.h>
#define CMOS_ADDRESS 0x70
#define CMOS_DATA 0x71

Expand All @@ -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 *);
Expand Down
1 change: 1 addition & 0 deletions kernel/kernel/io.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include<kernel/io.h>

void outb(uint32_t ad, uint8_t v)
{
asm volatile("outb %%al, %%dx" :: "d" (ad), "a" (v));
Expand Down
3 changes: 3 additions & 0 deletions kernel/kernel/kernel.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include<stdio.h>
#include<kernel/tty.h>
#include<arch/i386/time.h>
#include<arch/i386/x86.h>
#include<arch/i386/interrupts.h>
#include<arch/devices/keyboard.h>
Expand Down Expand Up @@ -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) {
}
Expand Down

0 comments on commit 6c2829a

Please sign in to comment.