Compare commits

...

2 Commits

Author SHA1 Message Date
ef75ad874d add: date cmd to shell 2024-09-18 21:45:18 +02:00
c7545d5fc6 add: rtc support 2024-09-18 21:45:10 +02:00
5 changed files with 117 additions and 0 deletions

32
headers/rtc.h Normal file
View File

@ -0,0 +1,32 @@
#pragma once
#include <stdint.h>
#define SECOND_REGISTER 0x00
#define MINUTE_REGISTER 0x02
#define HOUR_REGISTER 0x04
#define DAY_OF_THE_MONTH_REGISTER 0x07
#define MONTH_REGISTER 0x08
#define YEAR_REGISTER 0x09
#define CENTURY_REGISTER 0x32
#define REGISTER_A 0x0A
#define REGISTER_B 0x0B
enum {
CMOS_ADDRESS = 0x70,
CMOS_DATA = 0x71
};
struct rtc_date {
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t index_of_the_day;
uint8_t day;
uint8_t month;
uint32_t year;
};
struct rtc_date get_date(void);

View File

@ -53,6 +53,7 @@ typedef enum {
ECHO,
COLOR,
MERDELLA,
DATE,
ERROR
} CMD_TOK;
@ -60,3 +61,4 @@ void shell_init(void);
void reboot(void);
void halt(void);
void print_stack(void);
void date(void);

56
src/rtc/rtc.c Normal file
View File

@ -0,0 +1,56 @@
#include "sys/io.h"
#include "rtc.h"
#include "kprintf.h"
static uint16_t raw_read_register(uint16_t cmos_register)
{
//selecting the register
outb(CMOS_ADDRESS, cmos_register);
//read value of the selectect register
return inb(CMOS_DATA);
}
static uint8_t update_is_in_progress(void)
{
return raw_read_register(REGISTER_A) & 0x80;
}
static uint16_t read_register(uint16_t cmos_register)
{
while (update_is_in_progress())
kprintf(0, "%d\n", update_is_in_progress());
return raw_read_register(cmos_register);
}
uint8_t bcd_mode_to_bin(uint8_t value)
{
return (value & 0x0F) + ((value/ 16) * 10);
}
struct rtc_date get_date(void)
{
struct rtc_date rv;
uint8_t century;
rv.second = read_register(SECOND_REGISTER);
rv.minute = read_register(MINUTE_REGISTER);
rv.hour = read_register(HOUR_REGISTER);
rv.day = read_register(DAY_OF_THE_MONTH_REGISTER);
rv.month = read_register(MONTH_REGISTER);
rv.year += read_register(YEAR_REGISTER);
century = read_register(CENTURY_REGISTER);
if (!(read_register(REGISTER_B) & 0x04)) {
rv.second = bcd_mode_to_bin(rv.second);
rv.minute = bcd_mode_to_bin(rv.minute);
rv.hour = bcd_mode_to_bin(rv.hour);
rv.day = bcd_mode_to_bin(rv.day);
rv.month = bcd_mode_to_bin(rv.month);
rv.year = bcd_mode_to_bin(rv.year);
century = bcd_mode_to_bin(century);
}
rv.year += century * 100 - 108; // -108 = bozo offset
return rv;
}

22
src/shell/date.c Normal file
View File

@ -0,0 +1,22 @@
#include "kprintf.h"
#include "rtc.h"
void date()
{
static const char *months[12] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December" };
struct rtc_date date = get_date();
kprintf(0, "%s. %d %s. %d %d:%d:%d\n", "mer", date.day, months[date.month], date.year, date.hour, date.minute, date.second);
}

View File

@ -39,6 +39,8 @@ static CMD_TOK find_command(char *line)
command = COLOR;
else if (!strcmp(line, "merdella"))
command = MERDELLA;
else if (!strcmp(line, "date"))
command = DATE;
else
kprintf(0, "invalid command: %s\n", line);
if (uwu)
@ -120,6 +122,9 @@ void shell_init(void)
"by Targon (/)\n");
break;
}
case DATE:
date();
break;
case ERROR:
break;
default: