diff options
Diffstat (limited to 'devices')
-rw-r--r-- | devices/BUILD.bazel | 22 | ||||
-rw-r--r-- | devices/Makefile | 11 | ||||
-rw-r--r-- | devices/i8042.c | 29 | ||||
-rw-r--r-- | devices/include/keyboard.h (renamed from devices/keyboard.h) | 0 | ||||
-rw-r--r-- | devices/include/mouse.h (renamed from devices/mouse.h) | 0 | ||||
-rw-r--r-- | devices/include/pic.h (renamed from devices/pic.h) | 0 | ||||
-rw-r--r-- | devices/include/ps2_controller.h (renamed from devices/ps2_controller.h) | 0 | ||||
-rw-r--r-- | devices/include/uart.h | 32 | ||||
-rw-r--r-- | devices/include/vga.h | 33 | ||||
-rw-r--r-- | devices/mouse.c | 26 | ||||
-rw-r--r-- | devices/pckbd.c | 16 | ||||
-rw-r--r-- | devices/pic_8259.c | 34 | ||||
-rw-r--r-- | devices/uart.hpp | 7 | ||||
-rw-r--r-- | devices/uart/sys_io.hh | 34 | ||||
-rw-r--r-- | devices/uart/uart_16550.c | 78 | ||||
-rw-r--r-- | devices/uart/uart_16550.h | 49 | ||||
-rw-r--r-- | devices/uart/unittest_uart_16550.cc | 116 | ||||
-rw-r--r-- | devices/uart_16550.cpp | 110 | ||||
-rw-r--r-- | devices/vga.c (renamed from devices/vga.cpp) | 88 | ||||
-rw-r--r-- | devices/vga.hpp | 51 |
20 files changed, 477 insertions, 259 deletions
diff --git a/devices/BUILD.bazel b/devices/BUILD.bazel new file mode 100644 index 0000000..0c58d48 --- /dev/null +++ b/devices/BUILD.bazel @@ -0,0 +1,22 @@ +cc_library( + name = "drivers", + srcs = [ + "i8042.c", + "mouse.c", + "pckbd.c", + "pic_8259.c", + "uart/uart_16550.c", + "uart/uart_16550.h", + "vga.c", + ], + hdrs = glob(["include/*.h"]), + defines = [ + "__ARCH__=i386", + ], + includes = ["include"], + visibility = ["//visibility:public"], + deps = [ + "//arch/i386:arch", + "//lib/libk:k", + ], +) diff --git a/devices/Makefile b/devices/Makefile deleted file mode 100644 index 3c61f6d..0000000 --- a/devices/Makefile +++ /dev/null @@ -1,11 +0,0 @@ -include ../Makefile.config - -INCLUDES := -I../${ARCH} -${ARCH}_CFLAGS += ${INCLUDES} -${ARCH}_CXXFLAGS += ${INCLUDES} - -TARGETLIB += devs -devs.SRCS = pic_8259.c uart_16550.cpp vga.cpp i8042.c pckbd.c mouse.c - -include ../rules.mk - diff --git a/devices/i8042.c b/devices/i8042.c index c612e6d..cfd96a8 100644 --- a/devices/i8042.c +++ b/devices/i8042.c @@ -8,8 +8,8 @@ #include <stdio.h> #include <sys/io.h> -// r status register -// w command register +/* r status register */ +/* w command register */ #define comm_port 0x64 #define comm_enable_first_ps2 0xae #define comm_enable_second_ps2 0xa8 @@ -19,16 +19,18 @@ #define data_port 0x60 #define data_enable_scanning 0xf4 -// TODO: All output to port 0x60 or 0x64 must be preceded by waiting for bit 1 (value=2) of port 0x64 to become clear. -// TODO: Similarly, bytes cannot be read from port 0x60 until bit 0 (value=1) of port 0x64 is set. +/* TODO: All output to port 0x60 or 0x64 must be preceded by waiting for bit 1 (value=2) of port 0x64 to become clear. + * TODO: Similarly, bytes cannot be read from port 0x60 until bit 0 (value=1) of port 0x64 is set. */ void ps2_ctrl_init() { - // eat all previous keystrikes + int i; + uint8_t test, conf; + + /* eat all previous keystrikes */ while (inb(comm_port) & 0x1) inb(data_port); - uint8_t test; outb(0xaa, comm_port); test = inb(data_port); printf("i8042: self test 0xaa:%x %s\n", test, test == 0x55 ? "ok" : "failed"); @@ -39,30 +41,30 @@ ps2_ctrl_init() test = inb(data_port); printf("i8042: port2 test 0xa9:%x %s\n", test, test == 0x00 ? "ok" : "failed"); - // printf("8042: init keyboard\n"); + /* printf("8042: init keyboard\n"); */ outb(comm_enable_first_ps2, comm_port); outb(comm_enable_second_ps2, comm_port); - // resets the cpu - // outb(0xfe, 0x64); + /* resets the cpu */ + /* outb(0xfe, 0x64); */ outb(0xf2, 0x60); printf("i8042: id port1: "); while ((inb(0x64) & 0x01) == 0) {} - for (int i = 0; i < 3; ++i) printf("%x ", inb(0x60)); + for (i = 0; i < 3; ++i) printf("%x ", inb(0x60)); printf("\n"); outb(0xd4, 0x64); outb(0xf2, 0x60); printf("i8042: id port2: "); while ((inb(0x64) & 0x01) == 0) {} - for (int i = 0; i < 3; ++i) printf("%x ", inb(0x60)); + for (i = 0; i < 3; ++i) printf("%x ", inb(0x60)); printf("\n"); outb(comm_read_ctrl_config, comm_port); - uint8_t conf = (inb(data_port) | 1) & ~0x10; - conf |= 0x22; // mouse + conf = (uint8_t)((inb(data_port) | 1) & ~0x10); + conf |= 0x22; /* mouse */ outb(comm_write_ctrl_config, comm_port); outb(conf, data_port); @@ -81,7 +83,6 @@ ps2_read_port1() unsigned char ps2_read_port2() { - outb(0xd4, 0x64); return inb(0x60); } diff --git a/devices/keyboard.h b/devices/include/keyboard.h index 5f4fcc2..5f4fcc2 100644 --- a/devices/keyboard.h +++ b/devices/include/keyboard.h diff --git a/devices/mouse.h b/devices/include/mouse.h index a34ecb4..a34ecb4 100644 --- a/devices/mouse.h +++ b/devices/include/mouse.h diff --git a/devices/pic.h b/devices/include/pic.h index c545c60..c545c60 100644 --- a/devices/pic.h +++ b/devices/include/pic.h diff --git a/devices/ps2_controller.h b/devices/include/ps2_controller.h index d2f7e80..d2f7e80 100644 --- a/devices/ps2_controller.h +++ b/devices/include/ps2_controller.h diff --git a/devices/include/uart.h b/devices/include/uart.h new file mode 100644 index 0000000..8b44519 --- /dev/null +++ b/devices/include/uart.h @@ -0,0 +1,32 @@ +#pragma once + +#ifdef __ARCH__ +#include <stdio.h> +#include <sys/io.h> + +#else +/* from stdio */ +typedef struct FILE { + int id; + void (*putc)(const struct FILE *, char); + int (*puts)(const struct FILE *, const char *, int); + void (*flush)(const struct FILE *); +} FILE; + +/* from sys/io */ +unsigned char inb(unsigned short); +void outb(unsigned char, unsigned short); + +enum UART { + COM1 = 0x3f8, + COM2 = 0x2f8, + COM3 = 0x3e8, + COM4 = 0x2e8, + COM5 = 0x5f8, + COM6 = 0x4f8, + COM7 = 0x5e8, + COM8 = 0x4e8, +}; +#endif + +FILE *uart_init(unsigned short port); diff --git a/devices/include/vga.h b/devices/include/vga.h new file mode 100644 index 0000000..df0d921 --- /dev/null +++ b/devices/include/vga.h @@ -0,0 +1,33 @@ +#pragma once + +#include <stdio.h> + +/** Hardware text mode color constants. */ +enum vga_color { + VGA_COLOR_BLACK = 0, + VGA_COLOR_BLUE = 1, + VGA_COLOR_GREEN = 2, + VGA_COLOR_CYAN = 3, + VGA_COLOR_RED = 4, + VGA_COLOR_MAGENTA = 5, + VGA_COLOR_BROWN = 6, + VGA_COLOR_LIGHT_GREY = 7, + VGA_COLOR_DARK_GREY = 8, + VGA_COLOR_LIGHT_BLUE = 9, + VGA_COLOR_LIGHT_GREEN = 10, + VGA_COLOR_LIGHT_CYAN = 11, + VGA_COLOR_LIGHT_RED = 12, + VGA_COLOR_LIGHT_MAGENTA = 13, + VGA_COLOR_LIGHT_BROWN = 14, + VGA_COLOR_WHITE = 15 +}; + +FILE *vga_init(void *addr); +void vga_clear(enum vga_color foreground, enum vga_color background); + +/* void vga_putc(char a); */ +/* void vga_puts(const char *string, int len); */ + +/* void vga_enable_cursor(unsigned char start, unsigned char end); */ +/* void vga_disable_cursor(); */ +void vga_update_cursor(void); diff --git a/devices/mouse.c b/devices/mouse.c index a950e8c..a03388a 100644 --- a/devices/mouse.c +++ b/devices/mouse.c @@ -7,29 +7,33 @@ void mouse_init() { - // Sending a command or data byte to the mouse (to port 0x60) must be preceded by sending a 0xD4 byte to port 0x64 - // (with appropriate waits on port 0x64, bit 1, before sending each output byte). Note: this 0xD4 byte does not - // generate any ACK, from either the keyboard or mouse. + int i; - // enable second ps/2 - // outb(0xa8, 0x64); + /* Sending a command or data byte to the mouse (to port 0x60) must be preceded by sending a 0xD4 byte to port 0x64 + * (with appropriate waits on port 0x64, bit 1, before sending each output byte). Note: this 0xD4 byte does not + * generate any ACK, from either the keyboard or mouse. */ - // outb(0x64, 0xd4); - // outb(0x60, 0xf4); - // printf("mouse_init: enable streaming(0xf4): %x\n", inb(0x60)); + /* enable second ps/2 */ + /* outb(0xa8, 0x64); */ + + /* outb(0x64, 0xd4); */ + /* outb(0x60, 0xf4); */ + /* printf("mouse_init: enable streaming(0xf4): %x\n", inb(0x60)); */ outb(0xd4, 0x64); - outb(0xeb, 0x60); // single packet + outb(0xeb, 0x60); /* single packet */ printf("mouse_init: single packet 0xeb\n"); while ((inb(0x64) & 0x01) == 0) {} - for (int i = 0; i < 10; ++i) printf("%x ", inb(0x60)); + for (i = 0; i < 10; ++i) printf("%x ", inb(0x60)); printf("\n"); } void mouse_packet() { + int i; + printf("mouse packet: "); - for (int i = 0; i < 4; ++i) printf("%x ", ps2_read_port2()); + for (i = 0; i < 4; ++i) printf("%x ", ps2_read_port2()); printf("\n"); } diff --git a/devices/pckbd.c b/devices/pckbd.c index d550f91..0e99fe6 100644 --- a/devices/pckbd.c +++ b/devices/pckbd.c @@ -26,22 +26,22 @@ ps2_keyboard_irq_handler() const uint8_t key = ps2_read_port1(); switch (key) { - case 0x2a: // left shift down - case 0x36: // right shift down + case 0x2a: /* left shift down */ + case 0x36: /* right shift down */ shift_case = 1; return; - case 0xaa: // left shift up - case 0xb6: // right shift up + case 0xaa: /* left shift up */ + case 0xb6: /* right shift up */ shift_case = 0; return; - case 0x5b: // left meta - case 0x5c: // right meta + case 0x5b: /* left meta */ + case 0x5c: /* right meta */ return; - case 0x58: // F12 - // vga_clear(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_LIGHT_GREY); + case 0x58: /* F12 */ + /* vga_clear(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_LIGHT_GREY); */ return; } diff --git a/devices/pic_8259.c b/devices/pic_8259.c index 75a2d0e..75aaf6d 100644 --- a/devices/pic_8259.c +++ b/devices/pic_8259.c @@ -6,11 +6,11 @@ #define PIC2 0xa0 #define DATA 1 -// initialization +/* initialization */ #define ICW1_INIT 0x10 -// TODO +/* TODO */ #define ICW1_ICW4 0x01 -// 8086/88 mode +/* 8086/88 mode */ #define ICW4_8086 0x01 void @@ -19,31 +19,39 @@ pic_init() outb(ICW1_INIT | ICW1_ICW4, PIC1); outb(ICW1_INIT | ICW1_ICW4, PIC2); - outb(0x20, PIC1 + DATA); // offset 0x20 - outb(0x28, PIC2 + DATA); // offset 0x28 + outb(0x20, PIC1 + DATA); /* offset 0x20 */ + outb(0x28, PIC2 + DATA); /* offset 0x28 */ - outb(0x04, PIC1 + DATA); // tell master pic there is a slave pic - outb(0x02, PIC2 + DATA); // tell slave pic its cascade identity + outb(0x04, PIC1 + DATA); /* tell master pic there is a slave pic */ + outb(0x02, PIC2 + DATA); /* tell slave pic its cascade identity */ outb(ICW4_8086, PIC1 + DATA); outb(ICW4_8086, PIC2 + DATA); - // PIC masks + /* PIC masks */ outb(0xff, PIC1 + DATA); outb(0xff, PIC2 + DATA); } +static unsigned char +irq_mask(unsigned char irq) +{ + unsigned char mask = (unsigned char)~(1u << irq); + return 0xff & mask; +} + void pic_enable() { unsigned char mask1 = 0xff; - mask1 &= ~(1 << 0); // irq0 timer - mask1 &= ~(1 << 1); // irq1 keyboard - mask1 &= ~(1 << 2); // irq1 cascade + unsigned char mask2 = 0xff; + + mask1 &= irq_mask(0); /* irq0 timer */ + mask1 &= irq_mask(1); /* irq1 keyboard */ + mask1 &= irq_mask(2); /* irq2 cascade */ outb(mask1, PIC1 + DATA); - unsigned char mask2 = 0xff; - mask2 &= ~(1 << 4); // irq12 mouse + mask2 &= irq_mask(12 - 8); /* irq12 mouse */ outb(mask2, PIC2 + DATA); enable_interrupts(); diff --git a/devices/uart.hpp b/devices/uart.hpp deleted file mode 100644 index c86557c..0000000 --- a/devices/uart.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include <stdio.h> -#include <sys/io.h> - -template <UART port> FILE *uart_init(); -template <> FILE *uart_init<COM1>(); diff --git a/devices/uart/sys_io.hh b/devices/uart/sys_io.hh new file mode 100644 index 0000000..142c9c1 --- /dev/null +++ b/devices/uart/sys_io.hh @@ -0,0 +1,34 @@ +#pragma once + +#include <gmock/gmock.h> + +class IPort { +public: + virtual unsigned char inb(unsigned short) const = 0; + virtual void outb(unsigned char, unsigned short) const = 0; +}; + +class MockPort : public IPort { +public: + MOCK_METHOD(unsigned char, inb, (unsigned short), (const, override)); + MOCK_METHOD(void, outb, (unsigned char, unsigned short), (const, override)); +}; + +static std::unique_ptr<MockPort> mockPort; + +// mock free functions +extern "C" { +unsigned char +inb(unsigned short v) +{ + EXPECT_TRUE(mockPort) << "MockPort was not set up by the test fixture"; + return mockPort->inb(v); +} + +void +outb(unsigned char p, unsigned short v) +{ + EXPECT_TRUE(mockPort) << "MockPort was not set up by the test fixture"; + return mockPort->outb(p, v); +} +} diff --git a/devices/uart/uart_16550.c b/devices/uart/uart_16550.c new file mode 100644 index 0000000..4697cf3 --- /dev/null +++ b/devices/uart/uart_16550.c @@ -0,0 +1,78 @@ +#include "uart_16550.h" +#include <stddef.h> + +int +uart_thre(unsigned short port) +{ + return inb(port + LineStatus) & THRE; +} + +void +uart_putc(const FILE *self, char a) +{ + const unsigned short port = (unsigned short)self->id; + + while (uart_thre(port) == 0) {} + outb((unsigned char)a, port); + + if (a == '\n') { + while (uart_thre(port) == 0) {} + outb('\r', port); + } +} + +int +uart_puts(const FILE *self, const char *string, int length) +{ + int i; + int written = 0; + + if (length == -1) + while (*string != '\0') { + uart_putc(self, *string); + ++string; + ++written; + } + + else + for (i = 0; i < length; ++i) { + uart_putc(self, string[i]); + ++written; + } + + return written; +} + +void +uart_flush(__attribute__((unused)) const FILE *self) +{ +} + +FILE uart_stream; + +FILE * +uart_init(unsigned short port) +{ + outb(0x00, port + 1); /* Disable all interrupts */ + outb(0x80, port + 3); /* Enable DLAB (set baud rate divisor) */ + outb(0x03, port + 0); /* Set divisor to 3 (lo byte) 38400 baud */ + outb(0x00, port + 1); /* (hi byte) */ + outb(0x03, port + 3); /* 8 bits, no parity, one stop bit */ + outb(0xc7, port + 2); /* Enable FIFO, clear them, with 14-byte threshold */ + outb(0x0b, port + 4); /* IRQs enabled, RTS/DSR set */ + outb(0x1e, port + 4); /* Set in loopback mode, test the serial chip */ + outb(0xae, port + 0); /* Test serial chip (send byte 0xAE and check if serial */ + /* returns same byte) */ + + /* Check if serial is faulty (i.e: not same byte as sent) */ + if (inb(port + 0) != 0xae) { return NULL; } + + /* If serial is not faulty set it in normal operation mode */ + /* (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) */ + outb(0x0f, port + 4); + uart_stream.id = port; + uart_stream.putc = &uart_putc; + uart_stream.puts = &uart_puts; + uart_stream.flush = &uart_flush; + return &uart_stream; +} diff --git a/devices/uart/uart_16550.h b/devices/uart/uart_16550.h new file mode 100644 index 0000000..bbeb9b7 --- /dev/null +++ b/devices/uart/uart_16550.h @@ -0,0 +1,49 @@ +#pragma once + +#include "uart.h" + +int uart_thre(unsigned short port); +void uart_putc(const FILE *self, char a); +int uart_puts(const FILE *self, const char *string, int length); +void uart_flush(__attribute__((unused)) const FILE *self); + +enum uart_16550_offset { + Data = 0, /* read from receive buffer / write to transmit buffer | BaudDiv_l */ + InterruptControl = 1, /* interrupt enable | BaudDiv_h */ + FifoControl = 2, /* interrupt ID and FIFO control */ + LineControl = 3, /* most significant bit is the DLAB */ + ModemControl = 4, + LineStatus = 5, + ModemStatus = 6, + Scratch = 7 +}; + +/* Line Control + * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | + * |dla| | parity | s | data | */ +enum LineControl { + d5bit = 0x00, /* 0000 0000 data bits */ + d6bit = 0x01, /* 0000 0001 */ + d7bit = 0x02, /* 0000 0010 */ + d8bit = 0x03, /* 0000 0011 */ + /* none = 0b00000000, // parity bits */ + odd = 0x08, /* 0000 1000 */ + even = 0x18, /* 0001 1000 */ + mark = 0x28, /* 0010 1000 */ + space = 0x38, /* 0011 1000 */ + /* s1bit = 0b00000000, // stop bits */ + s2bit = 0x04, /* 0000 0100 1.5 for 5bit data; 2 otherwise */ + dlab = 0x80 /* 1000 0000 divisor latch access bit */ +}; + +/* Line Status Register */ +enum LineStatus { + DR = (1 << 0), /* data ready: see if there is data to read */ + OE = (1 << 1), /* overrun error: see if there has been data lost */ + PE = (1 << 2), /* parity error: see if there was error in transmission */ + FE = (1 << 3), /* framing error: see if a stop bit was missing */ + BI = (1 << 4), /* break indicator: see if there is a break in data input */ + THRE = (1 << 5), /* transmitter holding register empty: see if transmission buffer is empty */ + TEMT = (1 << 6), /* transmitter empty: see if transmitter is not doing anything */ + ERRO = (1 << 7) /* impending error: see if there is an error with a word in the input buffer */ +}; diff --git a/devices/uart/unittest_uart_16550.cc b/devices/uart/unittest_uart_16550.cc new file mode 100644 index 0000000..f8124bb --- /dev/null +++ b/devices/uart/unittest_uart_16550.cc @@ -0,0 +1,116 @@ +#include "sys_io.hh" +#include <gtest/gtest.h> + +using ::testing::_; +using ::testing::MockFunction; +using ::testing::Return; + +namespace k { +extern "C" { +#include "uart.h" +#include "uart_16550.h" +} +} // namespace k + +class Uart16550 : public ::testing::Test { +protected: + void + SetUp() + { + ASSERT_FALSE(mockPort); + mockPort.reset(new MockPort); + } + void + TearDown() + { + ASSERT_TRUE(mockPort); + mockPort.reset(); + } +}; + +TEST_F(Uart16550, uart_thre) +{ + // set up expectations + EXPECT_CALL(*mockPort, inb(k::COM1 + k::LineStatus)).Times(1).WillOnce(Return(k::THRE)); + + const auto result = uart_thre(k::COM1); + EXPECT_TRUE(result); +} + +TEST_F(Uart16550, uart_putc) +{ + k::FILE f{k::COM1, nullptr, nullptr, nullptr}; + + // set up expectations + EXPECT_CALL(*mockPort, inb(k::COM1 + k::LineStatus)).Times(1).WillRepeatedly(Return(k::THRE)); + EXPECT_CALL(*mockPort, outb('a', k::COM1)).Times(1); + + uart_putc(&f, 'a'); +} + +TEST_F(Uart16550, uart_putc_newline) +{ + k::FILE f{k::COM1, nullptr, nullptr, nullptr}; + + // set up expectations + EXPECT_CALL(*mockPort, inb(k::COM1 + k::LineStatus)).Times(2).WillRepeatedly(Return(k::THRE)); + EXPECT_CALL(*mockPort, outb('\n', k::COM1)).Times(1); + EXPECT_CALL(*mockPort, outb('\r', k::COM1)).Times(1); + + uart_putc(&f, '\n'); +} + +TEST_F(Uart16550, uart_puts) +{ + k::FILE f{k::COM1, nullptr, nullptr, nullptr}; + const char *string{"This is a test string to write over uart"}; + const int length = (int)strlen(string); + + // set up expectations + EXPECT_CALL(*mockPort, inb(k::COM1 + k::LineStatus)).Times(length).WillRepeatedly(Return(k::THRE)); + EXPECT_CALL(*mockPort, outb(_, k::COM1)).Times(length); + + const auto result = uart_puts(&f, string, length); + ASSERT_EQ(result, length); +} + +TEST_F(Uart16550, uart_puts_WithUnknownLength) +{ + k::FILE f{k::COM1, nullptr, nullptr, nullptr}; + const char *string{"This is a test string to write over uart"}; + const int length = (int)strlen(string); + + // set up expectations + EXPECT_CALL(*mockPort, inb(k::COM1 + k::LineStatus)).Times(length).WillRepeatedly(Return(k::THRE)); + EXPECT_CALL(*mockPort, outb(_, k::COM1)).Times(length); + + const auto result = uart_puts(&f, string, -1); + ASSERT_EQ(result, length); +} + +TEST_F(Uart16550, uart_puts_WithPartialLength) +{ + k::FILE f{k::COM1, nullptr, nullptr, nullptr}; + const char *string{"This is a test string to write over uart"}; + const int length = (int)strlen(string); + const int partial = 10; + + ASSERT_LT(partial, length); + + // set up expectations + EXPECT_CALL(*mockPort, inb(k::COM1 + k::LineStatus)).Times(partial).WillRepeatedly(Return(k::THRE)); + EXPECT_CALL(*mockPort, outb(_, k::COM1)).Times(partial); + + const auto result = uart_puts(&f, string, partial); + ASSERT_EQ(result, partial); +} + +TEST_F(Uart16550, uart_flush) +{ + k::FILE f{k::COM1, nullptr, nullptr, nullptr}; + + // set up expectations + // no mock calls are expected + + uart_flush(&f); +} diff --git a/devices/uart_16550.cpp b/devices/uart_16550.cpp deleted file mode 100644 index 9620981..0000000 --- a/devices/uart_16550.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include "uart.hpp" -#include <sys/io.hpp> - -template <UART port> struct Uart16550 : public kIoDevice, private Port<unsigned short, port> { - using Base = Port<unsigned short, port>; - using Ports = UART; - using PortOffset = UARTPortOffset; - - // Line Control - // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | - // |dla| | parity | s | data | - enum LineControl : unsigned char { - d5bit = 0x00, // 0000 0000 data bits - d6bit = 0x01, // 0000 0001 - d7bit = 0x02, // 0000 0010 - d8bit = 0x03, // 0000 0011 - none = 0x00, // 0000 0000 parity bits - odd = 0x08, // 0000 1000 - even = 0x18, // 0001 1000 - mark = 0x28, // 0010 1000 - space = 0x38, // 0011 1000 - s1bit = 0x00, // 0000 0000 stop bits - s2bit = 0x04, // 0000 0100 1.5 for 5bit data; 2 otherwise - dlab = 0x80 // 1000 0000 divisor latch access bit - }; - - // Line Status Register - enum LineStatus : unsigned char { - DR = (1 << 0), // data ready: see if there is data to read - OE = (1 << 1), // overrun error: see if there has been data lost - PE = (1 << 2), // parity error: see if there was error in transmission - FE = (1 << 3), // framing error: see if a stop bit was missing - BI = (1 << 4), // break indicator: see if there is a break in data input - THRE = (1 << 5), // transmitter holding register empty: see if transmission buffer is empty - TEMT = (1 << 6), // transmitter empty: see if transmitter is not doing anything - ERRO = (1 << 7), // impending error: see if there is an error with a word in the input buffer - }; - - [[nodiscard]] static bool - test() - { - Base::out(0x00, 1); // Disable all interrupts - Base::out(0x80, 3); // Enable DLAB (set baud rate divisor) - Base::out(0x03, 0); // Set divisor to 3 (lo byte) 38400 baud - Base::out(0x00, 1); // (hi byte) - Base::out(0x03, 3); // 8 bits, no parity, one stop bit - Base::out(0xc7, 2); // Enable FIFO, clear them, with 14-byte threshold - Base::out(0x0b, 4); // IRQs enabled, RTS/DSR set - Base::out(0x1e, 4); // Set in loopback mode, test the serial chip - Base::out(0xae, 0); // Test serial chip (send byte 0xAE and check if serial - // returns same byte) - - // Check if serial is faulty (i.e: not same byte as sent) - if (Base::in() != 0xae) { return false; } - - // If serial is not faulty set it in normal operation mode - // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) - Base::out(0x0f, 4); - return true; - } - - [[nodiscard]] static bool - thre() - { - return Base::in(PortOffset::LineStatus)&THRE; - } - - void - putc(char a) override - { - while (!thre()) {} - Base::out(a); - if (a == '\n') putc('\r'); - } - - int - puts(const char *string, int length) override - { - int written = 0; - - if (length == -1) - while (*string != '\0') { - putc(*string); - ++string; - ++written; - } - - else { - for (int i = 0; i < length; ++i) putc(string[i]); - written += length; - } - - return written; - } - - void - flush() override - { - } -}; - -static_assert(sizeof(Uart16550<COM1>) == sizeof(void *)); - -template <> -FILE * -uart_init<COM1>() -{ - static Uart16550<COM1> device; - return &device; -} diff --git a/devices/vga.cpp b/devices/vga.c index 3f83a5f..51f860f 100644 --- a/devices/vga.cpp +++ b/devices/vga.c @@ -1,26 +1,31 @@ -#include "vga.hpp" +#include "vga.h" #include <stdint.h> #include <sys/io.h> -// FIXME user a Port #define cga_idx_port 0x3d4 #define cga_dat_port 0x3d5 -// FIXME make constexpr #define cursor_start 0x0a #define cursor_end 0x0b #define cursor_addr_h 0x0e #define cursor_addr_l 0x0f #define cursor_hide 0x20 -static_assert(sizeof(struct VGA::VGAEntry) == 2, "sizeof VGAEntry"); +struct __attribute__((packed)) VGAEntry { + char text; + unsigned foreground : 4; + unsigned background : 4; +}; +/* TODO _Static_assert(sizeof(struct VGAEntry) == 2, "sizeof VGAEntry"); */ -// FIXME make constexpr const int width = 80; const int height = 25; -// *** Cursor *** -// FIXME make VGA members +struct VGAEntry *buffer; +int col = 0; +int row = 0; + +/* *** Cursor *** */ void vga_enable_cursor(unsigned char start, unsigned char end) { @@ -39,21 +44,23 @@ vga_disable_cursor() } void -VGA::update_cursor() +vga_update_cursor(void) { - const uint16_t pos = row * width + col; + const uint16_t pos = (uint16_t)(row * width + col); outb(cursor_addr_l, cga_idx_port); - outb(pos & 0xff, cga_dat_port); + outb((unsigned char)pos & 0xff, cga_dat_port); outb(cursor_addr_h, cga_idx_port); - outb((pos >> 8) & 0xff, cga_dat_port); + outb((unsigned char)(pos >> 8) & 0xff, cga_dat_port); } -// *** Text Mode Output *** +/* *** Text Mode Output *** */ void -VGA::putc(char a) +vga_putc(__attribute__((unused)) const FILE *self, char a) { + int i, x, y; + switch (a) { case '\n': col = 0; @@ -77,62 +84,75 @@ VGA::putc(char a) } if (row == height) { - // scroll up - for (int y = 1; y < height; ++y) - for (int x = 0; x < width; ++x) { + /* scroll up */ + for (y = 1; y < height; ++y) + for (x = 0; x < width; ++x) { const int prev = (y - 1) * width + x; const int curr = y * width + x; buffer[prev] = buffer[curr]; } - // blank out last row - for (int i = (height - 1) * width; i < height * width; ++i) buffer[i].text = ' '; + /* blank out last row */ + for (i = (height - 1) * width; i < height * width; ++i) buffer[i].text = ' '; --row; } } int -VGA::puts(const char *string, int len) +vga_puts(const FILE *self, const char *string, int len) { + int i; + int written = 0; if (len == -1) while (*string != '\0') { - putc(*string); + vga_putc(self, *string); ++string; ++written; } else - for (int i = 0; i < len; ++i) { - putc(string[i]); + for (i = 0; i < len; ++i) { + vga_putc(self, string[i]); ++written; } return written; } -// *** Text Mode *** -VGA::VGA(void *addr) +void +vga_flush(__attribute__((unused)) const FILE *self) { - buffer = (struct VGAEntry *)addr; - vga_enable_cursor(14, 15); - clear(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_LIGHT_GREY); + vga_update_cursor(); } + +/* *** Text Mode *** */ +FILE vga_stream; + FILE * -vga_init(void *buffer) +vga_init(void *addr) { - static VGA device(buffer); - return &device; + buffer = (struct VGAEntry *)addr; + vga_enable_cursor(14, 15); + vga_clear(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_LIGHT_GREY); + + vga_stream.id = 0; + vga_stream.putc = &vga_putc; + vga_stream.puts = &vga_puts; + vga_stream.flush = &vga_flush; + return &vga_stream; } void -VGA::clear(enum vga_color foreground, enum vga_color background) +vga_clear(enum vga_color foreground, enum vga_color background) { - for (int y = 0; y < height; ++y) - for (int x = 0; x < width; ++x) { + int x, y; + + for (y = 0; y < height; ++y) + for (x = 0; x < width; ++x) { const int index = y * width + x; buffer[index].text = ' '; buffer[index].foreground = foreground; buffer[index].background = background; } col = row = 0; - update_cursor(); + vga_update_cursor(); } diff --git a/devices/vga.hpp b/devices/vga.hpp deleted file mode 100644 index 5287d73..0000000 --- a/devices/vga.hpp +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -#include <stdio.h> - -/** Hardware text mode color constants. */ -enum vga_color { - VGA_COLOR_BLACK = 0, - VGA_COLOR_BLUE = 1, - VGA_COLOR_GREEN = 2, - VGA_COLOR_CYAN = 3, - VGA_COLOR_RED = 4, - VGA_COLOR_MAGENTA = 5, - VGA_COLOR_BROWN = 6, - VGA_COLOR_LIGHT_GREY = 7, - VGA_COLOR_DARK_GREY = 8, - VGA_COLOR_LIGHT_BLUE = 9, - VGA_COLOR_LIGHT_GREEN = 10, - VGA_COLOR_LIGHT_CYAN = 11, - VGA_COLOR_LIGHT_RED = 12, - VGA_COLOR_LIGHT_MAGENTA = 13, - VGA_COLOR_LIGHT_BROWN = 14, - VGA_COLOR_WHITE = 15, -}; - -FILE *vga_init(void *buffer); - -struct VGA : public kIoDevice { - VGA(void *addr); - - void putc(char a) override; - int puts(const char *string, int length) override; - void - flush() override - { - update_cursor(); - } - - struct __attribute__((packed)) VGAEntry { - unsigned char text; - unsigned char foreground : 4; - unsigned char background : 4; - }; - -private: - void clear(enum vga_color foreground, enum vga_color background); - void update_cursor(); - - struct VGAEntry *buffer; - int col = 0; - int row = 0; -}; |