From edf9e71e2a7b6b89775c29cf28c19c6b89992c25 Mon Sep 17 00:00:00 2001 From: aqua Date: Mon, 28 Mar 2022 20:03:38 +0300 Subject: Initial commit x86 kernel that prints a hello world message to com1 --- devices/uart_16550.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 devices/uart_16550.c (limited to 'devices/uart_16550.c') diff --git a/devices/uart_16550.c b/devices/uart_16550.c new file mode 100644 index 0000000..7cec968 --- /dev/null +++ b/devices/uart_16550.c @@ -0,0 +1,68 @@ +#include "uart_16550.h" +#include + +int +uart_init(enum UART 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 1; + } + + // 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); + return 0; +} + +int +uart_thre(enum UART port) +{ + return inb(port + LineStatus) & THRE; +} + +void +uart_write(enum UART port, char a) +{ + while (uart_thre(port) == 0) + ; + outb(a, port); + + if (a == '\n') { + while (uart_thre(port) == 0) + ; + outb('\r', port); + } +} + +int +uart_puts(enum UART port, const char *string, int length) +{ + int written = 0; + + if (length == -1) + while (*string != '\0') { + uart_write(port, *string); + ++string; + ++written; + } + + else + for (int i = 0; i < length; ++i) { + uart_write(port, string[i]); + ++written; + } + + return written; +} -- cgit v1.2.1