diff options
Diffstat (limited to 'kernel/drivers')
-rw-r--r-- | kernel/drivers/clock.c | 203 | ||||
-rw-r--r-- | kernel/drivers/tty.c | 27 | ||||
-rw-r--r-- | kernel/drivers/uart.c | 21 |
3 files changed, 135 insertions, 116 deletions
diff --git a/kernel/drivers/clock.c b/kernel/drivers/clock.c index 80a44df..dd229dc 100644 --- a/kernel/drivers/clock.c +++ b/kernel/drivers/clock.c @@ -4,16 +4,16 @@ #include <comus/drivers/clock.h> #define CMOS_WRITE_PORT 0x70 -#define CMOS_READ_PORT 0x71 +#define CMOS_READ_PORT 0x71 -#define CMOS_REG_SEC 0x00 -#define CMOS_REG_MIN 0x02 -#define CMOS_REG_HOUR 0x04 -#define CMOS_REG_WDAY 0x06 -#define CMOS_REG_MDAY 0x07 -#define CMOS_REG_MON 0x08 -#define CMOS_REG_YEAR 0x09 -#define CMOS_REG_CEN 0x32 +#define CMOS_REG_SEC 0x00 +#define CMOS_REG_MIN 0x02 +#define CMOS_REG_HOUR 0x04 +#define CMOS_REG_WDAY 0x06 +#define CMOS_REG_MDAY 0x07 +#define CMOS_REG_MON 0x08 +#define CMOS_REG_YEAR 0x09 +#define CMOS_REG_CEN 0x32 // Live buffers to work on data static time_t time; @@ -27,133 +27,140 @@ static time_t curr_localtime; static timezone_t curr_timezone = TZ_UTC; static timezone_t last_timezone = TZ_UTC; -static uint8_t cmos_read(uint8_t reg) { - uint8_t hex, ret; +static uint8_t cmos_read(uint8_t reg) +{ + uint8_t hex, ret; - outb(CMOS_WRITE_PORT, reg); - hex = inb(CMOS_READ_PORT); + outb(CMOS_WRITE_PORT, reg); + hex = inb(CMOS_READ_PORT); - ret = hex & 0x0F; - ret += (hex & 0xF0) / 16 * 10; + ret = hex & 0x0F; + ret += (hex & 0xF0) / 16 * 10; - return ret; + return ret; } -static int mday_offset[12] = { - 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 -}; +static int mday_offset[12] = { 0, 31, 59, 90, 120, 151, + 181, 212, 243, 273, 304, 334 }; -static int month_days[12] = { - 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 -}; +static int month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; -static void update_localtime(void) { +static void update_localtime(void) +{ + int change, max; - int change, max; + // set localtime + localtime = time; - // set localtime - localtime = time; + // if tz is UTC, we dont need to do anythin + if (last_timezone == TZ_UTC) + return; - // if tz is UTC, we dont need to do anythin - if (last_timezone == TZ_UTC) return; + localtime.hour += last_timezone; - localtime.hour += last_timezone; + // check if day rolled over + change = localtime.hour < 0 ? -1 : localtime.hour >= 24 ? 1 : 0; + if (!change) + return; - // check if day rolled over - change = localtime.hour < 0 ? -1 : localtime.hour >= 24 ? 1 : 0; - if (!change) return; + // roll over day + localtime.hour = (localtime.hour + 24) % 24; + localtime.wday = (localtime.wday + change + 7) % 7; + localtime.mday += change; + localtime.yday += change; - // roll over day - localtime.hour = (localtime.hour + 24) % 24; - localtime.wday = (localtime.wday + change + 7) % 7; - localtime.mday += change; - localtime.yday += change; + // check if month rolled over + max = month_days[localtime.mon]; + if (localtime.leap && localtime.mon == 1) + max++; + change = localtime.mday < 0 ? -1 : localtime.mday >= max ? 1 : 0; + if (!change) + return; - // check if month rolled over - max = month_days[localtime.mon]; - if (localtime.leap && localtime.mon == 1) max++; - change = localtime.mday < 0 ? -1 : localtime.mday >= max ? 1 : 0; - if (!change) return; + // roll over month + localtime.mon = (localtime.mon + change + 12) % 12; - // roll over month - localtime.mon = (localtime.mon + change + 12) % 12; + // check if year rolled over + max = localtime.leap ? 366 : 365; + change = localtime.yday < 0 ? -1 : localtime.yday >= max ? 1 : 0; + if (!change) + return; - // check if year rolled over - max = localtime.leap ? 366 : 365; - change = localtime.yday < 0 ? -1 : localtime.yday >= max ? 1 : 0; - if (!change) return; + // roll over year + localtime.yn += change; - // roll over year - localtime.yn += change; - - // check if cen rolled over - change = localtime.yn < 0 ? -1 : localtime.yn >= 100 ? 1 : 0; - if (!change) goto year; - - // roll over cen - localtime.cen += change; + // check if cen rolled over + change = localtime.yn < 0 ? -1 : localtime.yn >= 100 ? 1 : 0; + if (!change) + goto year; + // roll over cen + localtime.cen += change; year: - localtime.year = localtime.yn + localtime.cen * 100; - localtime.leap = localtime.year % 4 == 0 && localtime.year % 100 != 0; - - if (localtime.leap && localtime.yday == -1) - localtime.yday = 365; - else if (localtime.yday == -1) - localtime.yday = 364; - else - localtime.yday = 0; + localtime.year = localtime.yn + localtime.cen * 100; + localtime.leap = localtime.year % 4 == 0 && localtime.year % 100 != 0; - localtime.year -= 1900; + if (localtime.leap && localtime.yday == -1) + localtime.yday = 365; + else if (localtime.yday == -1) + localtime.yday = 364; + else + localtime.yday = 0; + localtime.year -= 1900; } -void clock_update(void) { - time.sec = cmos_read(CMOS_REG_SEC); - time.min = cmos_read(CMOS_REG_MIN); - time.hour = cmos_read(CMOS_REG_HOUR); - time.wday = cmos_read(CMOS_REG_WDAY) - 1; - time.mday = cmos_read(CMOS_REG_MDAY); - time.mon = cmos_read(CMOS_REG_MON) - 1; - time.yn = cmos_read(CMOS_REG_YEAR); - time.cen = 20; +void clock_update(void) +{ + time.sec = cmos_read(CMOS_REG_SEC); + time.min = cmos_read(CMOS_REG_MIN); + time.hour = cmos_read(CMOS_REG_HOUR); + time.wday = cmos_read(CMOS_REG_WDAY) - 1; + time.mday = cmos_read(CMOS_REG_MDAY); + time.mon = cmos_read(CMOS_REG_MON) - 1; + time.yn = cmos_read(CMOS_REG_YEAR); + time.cen = 20; - time.year = time.yn + time.cen * 100; + time.year = time.yn + time.cen * 100; - time.leap = time.year % 4 == 0 && time.year % 100 != 0; + time.leap = time.year % 4 == 0 && time.year % 100 != 0; - time.yday = mday_offset[time.mon] + time.mday; + time.yday = mday_offset[time.mon] + time.mday; - if (time.leap && time.mon > 2) - time.yday++; + if (time.leap && time.mon > 2) + time.yday++; - time.year -= 1900; + time.year -= 1900; - update_localtime(); + update_localtime(); - curr_time = time; - curr_localtime = localtime; + curr_time = time; + curr_localtime = localtime; } -void set_timezone(timezone_t tz) { - curr_timezone = tz; +void set_timezone(timezone_t tz) +{ + curr_timezone = tz; } -time_t get_utctime(void) { - return curr_time; +time_t get_utctime(void) +{ + return curr_time; } -time_t get_localtime(void) { - if (curr_timezone != last_timezone) { - last_timezone = curr_timezone; - update_localtime(); - curr_localtime = localtime; - } - return curr_localtime; +time_t get_localtime(void) +{ + if (curr_timezone != last_timezone) { + last_timezone = curr_timezone; + update_localtime(); + curr_localtime = localtime; + } + return curr_localtime; } -size_t get_systemtime(void) { - return 0; +size_t get_systemtime(void) +{ + return 0; } diff --git a/kernel/drivers/tty.c b/kernel/drivers/tty.c index 74715a2..88e161f 100644 --- a/kernel/drivers/tty.c +++ b/kernel/drivers/tty.c @@ -18,9 +18,10 @@ static uint32_t x, y; static uint8_t fg, bg; // blank color -const uint16_t blank = (uint16_t) 0 | 0 << 12 | 15 << 8; +const uint16_t blank = (uint16_t)0 | 0 << 12 | 15 << 8; -static void term_clear_line(int line) { +static void term_clear_line(int line) +{ if (line < 0 || line >= height) return; for (uint8_t x = 0; x < width; x++) { @@ -29,29 +30,33 @@ static void term_clear_line(int line) { } } -static void term_clear(void) { - for (uint8_t y = 0; y < height; y++) - term_clear_line(y); +static void term_clear(void) +{ + for (uint8_t y = 0; y < height; y++) + term_clear_line(y); } -static void term_scroll(int lines) { +static void term_scroll(int lines) +{ cli(); y -= lines; - if (!lines) return; + if (!lines) + return; if (lines >= height || lines <= -height) { term_clear(); } else if (lines > 0) { memmovev(buffer, buffer + lines * width, 2 * (height - lines) * width); term_clear_line(height - lines); } else { - memmovev(buffer + lines * width, buffer + lines, (height + lines) * width); + memmovev(buffer + lines * width, buffer + lines, + (height + lines) * width); } sti(); } void tty_init(void) { - buffer = mapaddr((void*)VGA_ADDR, width * height * sizeof(uint16_t)); + buffer = mapaddr((void *)VGA_ADDR, width * height * sizeof(uint16_t)); x = 0; y = 0; fg = 15; @@ -99,7 +104,7 @@ void tty_out(char c) // set cursor position on screen const uint16_t pos = y * width + x; outb(0x3D4, 0x0F); - outb(0x3D5, (uint8_t) (pos & 0xFF)); + outb(0x3D5, (uint8_t)(pos & 0xFF)); outb(0x3D4, 0x0E); - outb(0x3D5, (uint8_t) ((pos >> 8) & 0xFF)); + outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF)); } diff --git a/kernel/drivers/uart.c b/kernel/drivers/uart.c index c999e45..6dcfe63 100644 --- a/kernel/drivers/uart.c +++ b/kernel/drivers/uart.c @@ -3,12 +3,14 @@ #define PORT 0x3F8 -int uart_init(void) { +int uart_init(void) +{ outb(PORT + 1, 0x00); // disable interrupts outb(PORT + 3, 0x80); // enable DLAB (divisor latch access bit) outb(PORT + 0, 0x03); // (lo) Set baud divisor to 3 38400 baud outb(PORT + 1, 0x00); // (hi) - outb(PORT + 3, 0x03); // disable DLAB, set 8 bits per word, one stop bit, no parity + outb(PORT + 3, + 0x03); // disable DLAB, set 8 bits per word, one stop bit, no parity outb(PORT + 2, 0xC7); // enable and clear FIFOs, set to maximum threshold outb(PORT + 4, 0x0B); // ??? outb(PORT + 4, 0x1E); // set in loopback mode for test @@ -25,19 +27,24 @@ int uart_init(void) { return 0; } -uint8_t uart_in(void) { +uint8_t uart_in(void) +{ // wait for data to be available - while ((inb(PORT + 5) & 0x01) == 0); + while ((inb(PORT + 5) & 0x01) == 0) + ; return inb(PORT); } -void uart_out(uint8_t ch) { +void uart_out(uint8_t ch) +{ // wait for output to be free - while ((inb(PORT + 5) & 0x20) == 0); + while ((inb(PORT + 5) & 0x20) == 0) + ; outb(PORT, ch); } -void uart_out_str(const char *str) { +void uart_out_str(const char *str) +{ while (*str) uart_out(*str++); } |