Skip to content

Commit

Permalink
Merge pull request #12 from Ryand1234/rtc_time
Browse files Browse the repository at this point in the history
Added rtc time module
  • Loading branch information
Ryand1234 authored Jul 26, 2024
2 parents 6e00698 + 8361e96 commit e8a58c7
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 18 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/*
5 changes: 4 additions & 1 deletion kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ LIBS:=$(LIBS) -nostdlib -lk -lgcc
ARCHDIR=arch/$(HOSTARCH)
MMUDIR=mmu
DEVICEDIR=arch/devices
KERNELDIR=kernel

include $(ARCHDIR)/make.config
include ${MMUDIR}/make.config
include ${DEVICEDIR}/make.config
include ${KERNELDIR}/make.config

CFLAGS:=$(CFLAGS) $(KERNEL_ARCH_CFLAGS)
CPPFLAGS:=$(CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS)
Expand All @@ -33,7 +35,7 @@ LIBS:=$(LIBS) $(KERNEL_ARCH_LIBS)

KERNEL_OBJS=\
$(KERNEL_ARCH_OBJS) \
kernel/kernel.o \
${KERNEL_KERNEL_OBJS} \
${MMU_OBJS} \
${KERNEL_DEVICE_OBJS}

Expand All @@ -45,6 +47,7 @@ $(ARCHDIR)/crtend.o \
$(ARCHDIR)/crtn.o \
${MMU_OBJS} \
${KERNEL_DEVICE_OBJS} \
${KERNEL_KERNEL_OBJS} \

LINK_LIST=\
$(LDFLAGS) \
Expand Down
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 \

59 changes: 59 additions & 0 deletions kernel/arch/i386/time.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#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;
}

void show_time() {
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);
}
12 changes: 0 additions & 12 deletions kernel/arch/i386/x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ void load_idt(struct idtr *idtr) {
asm volatile("lidt %0" : : "m" (*idtr));
}

void outb(uint32_t ad, uint8_t v)
{
asm volatile("outb %%al, %%dx" :: "d" (ad), "a" (v));
}

uint8_t inb(uint32_t ad)
{
uint8_t v;
asm volatile("inb %%dx, %%al" : "=a" (v) : "d" (ad));
return v;
}

void gdt_init(void) {

create_gdt_descriptor(0x0, 0x0, 0x0, 0x0, &kgdt[0]);
Expand Down
1 change: 1 addition & 0 deletions kernel/include/arch/devices/keyboard.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef ARCH_DEVICES_KEYBOARD_H
#define ARCH_DEVICES_KEYBOARD_H
#include<arch/i386/x86.h>
#include<kernel/io.h>

void keyboard_handler(struct regs *r);
void pause();
Expand Down
32 changes: 32 additions & 0 deletions kernel/include/arch/i386/time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef ARCH_I386_TIME_H
#define ARCH_I386_TIME_H

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

struct kernel_time {
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t day;
uint8_t month;
uint8_t year;
uint8_t century;
};

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 *);
void show_time(void);

#endif
6 changes: 1 addition & 5 deletions kernel/include/arch/i386/x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include<stddef.h>
#include<stdint.h>
#include<kernel/io.h>

struct gdtr {
uint16_t limit;
Expand Down Expand Up @@ -99,9 +100,4 @@ void init_pic(void);

void setup_isr(void);

void outb(uint32_t , uint8_t );

uint8_t inb(uint32_t );


#endif
8 changes: 8 additions & 0 deletions kernel/include/kernel/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef KERNEL_IO_H
#define KERNEL_IO_H
#include<stdint.h>
void outb(uint32_t , uint8_t );

uint8_t inb(uint32_t );

#endif
13 changes: 13 additions & 0 deletions kernel/kernel/io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include<kernel/io.h>

void outb(uint32_t ad, uint8_t v)
{
asm volatile("outb %%al, %%dx" :: "d" (ad), "a" (v));
}

uint8_t inb(uint32_t ad)
{
uint8_t v;
asm volatile("inb %%dx, %%al" : "=a" (v) : "d" (ad));
return v;
}
2 changes: 2 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,7 @@ void kernel_main(void) {
print_welcome_screen();
while(inb(0x60) & 0x80){};
terminal_initialize();
show_time();
printf("cheaky>");
while(1) {
}
Expand Down
9 changes: 9 additions & 0 deletions kernel/kernel/make.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
KERNEL_ARCH_CFLAGS=
KERNEL_ARCH_CPPFLAGS=
KERNEL_ARCH_LDFLAGS=
KERNEL_ARCH_LIBS=

KERNEL_KERNEL_OBJS=\
$(KERNELDIR)/kernel.o \
$(KERNELDIR)/io.o \

0 comments on commit e8a58c7

Please sign in to comment.