diff --git a/include/ports/serial.h b/include/ports/serial.h new file mode 100644 index 0000000..4a59557 --- /dev/null +++ b/include/ports/serial.h @@ -0,0 +1,24 @@ +#ifndef PORTS_SERIAL_H +#define PORTS_SERIAL_H + +#include + +#define PORT_COM1 0x3f8 +#define PORT_COM2 0x2f8 +#define PORT_COM3 0x3e8 +#define PORT_COM4 0x2e8 +#define PORT_COM5 0x5f8 +#define PORT_COM6 0x4f8 +#define PORT_COM7 0x5e8 +#define PORT_COM8 0x4e8 + +int setupSerial(uint32_t port); + +int serialTransmitEmpty(uint32_t port); +int serialRecieved(uint32_t port); + +void serialWrite(uint32_t port, char c); +void serialPrint(uint32_t port, char* s); +char serialRead(uint32_t port); + +#endif diff --git a/kern.iso b/kern.iso index 12bc63e..209d78c 100644 Binary files a/kern.iso and b/kern.iso differ diff --git a/libk/ports/serial.c b/libk/ports/serial.c new file mode 100644 index 0000000..ce1776e --- /dev/null +++ b/libk/ports/serial.c @@ -0,0 +1,46 @@ +#include +#include +#include + +int setupSerial(uint32_t port) { + outb(port + 1, 0x00); // Disable all interrupts + outb(port + 3, 0x80); // Enable DLAB (set baud rate divisor) + outb(port + 0, 0x03); // Set divisor to 3 (low byte) 38400 baud + outb(port + 1, 0x00); // (high byte) + outb(port + 3, 0x03); // 8 bits, no parity, one stop bit + outb(port + 2, 0xC7); // Enable FIFO, clear them, with 14 byte threshold + outb(port + 4, 0x0B); // IRQs enabled, RTS/DSR set + outb(port + 4, 0x1E); // Set in loopback mode, test the serial chip + outb(port + 0, 0xAE); // Test serial chip (send byte 0xAE and check if it returns) + + if (inb(port + 0) != 0xAE) return 1; + outb(port + 4, 0x0F); // non-loopback, IRQs enabled, OUT#1 and OUT#2 bits enabled + + return 0; +} + +int serialTransmitEmpty(uint32_t port) { + return inb(port + 5) & 0x20; +} + +int serialRecieved(uint32_t port) { + return inb(port + 5) & 0x01; +} + +void serialWrite(uint32_t port, char c) { + while (serialTransmitEmpty(port) == 0); + outb(port, c); +} + +void serialPrint(uint32_t port, char* s) { + while (*s != '\0') { + if (*s == '\n') serialWrite(port, '\r'); + serialWrite(port, *s++); + } +} + +char serialRead(uint32_t port) { + while (serialRecieved(port)); + return inb(port); +} + diff --git a/src/kernel.c b/src/kernel.c index 1f86fbf..e0e0a80 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -56,6 +57,11 @@ void main() { setupTimer(); setupRtc(); setupKeyboard(); + if (setupSerial(PORT_COM1) != 0) { printf("\e[1MFailed to initialize serial interface\e[8M\n"); } + else { + serialPrint(PORT_COM1, "\x1b[H\x1b[J"); + serialPrint(PORT_COM1, "kern. \x1b[36m(Serial console)\x1b[0m\n\n"); + } // Setup kernel malloc variables mallocNode_t* kernelMallocListHead = 0; diff --git a/todo.txt b/todo.txt index 87ef274..49637e8 100644 --- a/todo.txt +++ b/todo.txt @@ -15,12 +15,12 @@ Legend: - [ ] system root - [ ] arch/ - [ ] lib/ - - [ ] include/ + - [x] include/ - [ ] cross compiler setup - - [ ] libc - User space - - [ ] libk - Kernel libc + - [x] libc - User space + - [x] libk - Kernel libc - [ ] kernel - - [ ] use libk + - [x] use libk - [ ] makefile - [ ] probably a full rewrite - [ ] userspace