From 4ee3940807c365b66c3d6655c777b7e454888838 Mon Sep 17 00:00:00 2001 From: Tom-on64 Date: Wed, 9 Oct 2024 16:50:56 +0200 Subject: [PATCH] Added ports/serial.h header for serial console stuff --- include/ports/serial.h | 24 +++++++++++++++++++++ kern.iso | Bin 1470464 -> 1470464 bytes libk/ports/serial.c | 46 +++++++++++++++++++++++++++++++++++++++++ src/kernel.c | 6 ++++++ todo.txt | 8 +++---- 5 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 include/ports/serial.h create mode 100644 libk/ports/serial.c 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 12bc63e2590c3a23128965845663a9dd54506678..209d78cacca2831f472dd1d427e082e2b71feb59 100644 GIT binary patch delta 1549 zcmYjReN2^A7=K>*zT6KApi1y{tz zkdEtPlaP<3#9H%Rp_S`JaYP^H#)gix?q-|khC|kDnHPNfo%3FiKYq`C&+qv?&wI|f z=iJ7iuQBNR^3fK}!M9zk)8B77p4M4u#}ZzMz1I*BG9@p#y3WK7u(t|F?c zX8BUcRmvMs(1<2~lK6coQR_Jvi1NrSYS*g|$V7a{M~`kP{0$4kawr;~3}bxn&dp?0 z_}KFp=oW)6qVB?cGDbGpZ(W_FkxlWbi5iJYIMG!l7xj^YztElPYIG2_b&9*D*_ydT z6!4br%JjWxS9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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