diff options
author | Freya Murphy <freya@freyacat.org> | 2025-04-03 23:38:33 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2025-04-03 23:38:33 -0400 |
commit | 3c2a519ee9ab1ee5fcf043eb403242859b541d6b (patch) | |
tree | e5571b4732f1366e36c7da5ce08dcb4c18913f1e | |
parent | pci (diff) | |
download | comus-3c2a519ee9ab1ee5fcf043eb403242859b541d6b.tar.gz comus-3c2a519ee9ab1ee5fcf043eb403242859b541d6b.tar.bz2 comus-3c2a519ee9ab1ee5fcf043eb403242859b541d6b.zip |
serial and tty
-rw-r--r-- | build.zig | 6 | ||||
-rw-r--r-- | kernel/drivers/drivers.c | 4 | ||||
-rw-r--r-- | kernel/drivers/pci.c | 2 | ||||
-rw-r--r-- | kernel/drivers/tty.c | 105 | ||||
-rw-r--r-- | kernel/drivers/uart.c | 43 | ||||
-rw-r--r-- | kernel/include/comus/drivers/tty.h | 27 | ||||
-rw-r--r-- | kernel/include/comus/drivers/uart.h | 19 | ||||
-rw-r--r-- | kernel/io/io.c | 19 | ||||
-rw-r--r-- | kernel/lib/fputc.c | 11 | ||||
-rw-r--r-- | kernel/lib/panic.c (renamed from kernel/io/panic.c) | 0 |
10 files changed, 215 insertions, 21 deletions
@@ -32,8 +32,10 @@ const kernel_src = &[_][]const u8{ "kernel/cpu/pic.c", "kernel/drivers/drivers.c", "kernel/drivers/pci.c", - "kernel/io/io.c", - "kernel/io/panic.c", + "kernel/drivers/tty.c", + "kernel/drivers/uart.c", + "kernel/lib/fputc.c", + "kernel/lib/panic.c", "kernel/mboot/mboot.c", "kernel/mboot/mmap.c", "kernel/memory/memory.c", diff --git a/kernel/drivers/drivers.c b/kernel/drivers/drivers.c index 5b68248..4790c3a 100644 --- a/kernel/drivers/drivers.c +++ b/kernel/drivers/drivers.c @@ -1,7 +1,11 @@ #include <comus/drivers.h> +#include <comus/drivers/uart.h> +#include <comus/drivers/tty.h> #include <comus/drivers/pci.h> void drivers_init(void) { + uart_init(); + tty_init(); pci_init(); } diff --git a/kernel/drivers/pci.c b/kernel/drivers/pci.c index 83c47f1..5f17b3b 100644 --- a/kernel/drivers/pci.c +++ b/kernel/drivers/pci.c @@ -137,6 +137,8 @@ void pci_init(void) printf("PCI DEVICES\n"); for (size_t i = 0; i < pci_table_next; i++) { print_device(&pci_table[i]); + print_device(&pci_table[i]); + print_device(&pci_table[i]); } printf("\n"); } diff --git a/kernel/drivers/tty.c b/kernel/drivers/tty.c new file mode 100644 index 0000000..74715a2 --- /dev/null +++ b/kernel/drivers/tty.c @@ -0,0 +1,105 @@ +#include <lib.h> +#include <comus/drivers/tty.h> +#include <comus/asm.h> +#include <comus/memory.h> +#include <stdint.h> +#include <stdio.h> + +#define VGA_ADDR 0xB8000 + +static const uint8_t width = 80; +static const uint8_t height = 25; +static volatile uint16_t *buffer = NULL; + +// position +static uint32_t x, y; + +// color +static uint8_t fg, bg; + +// blank color +const uint16_t blank = (uint16_t) 0 | 0 << 12 | 15 << 8; + +static void term_clear_line(int line) { + if (line < 0 || line >= height) + return; + for (uint8_t x = 0; x < width; x++) { + const size_t index = line * width + x; + buffer[index] = blank; + } +} + +static void term_clear(void) { + for (uint8_t y = 0; y < height; y++) + term_clear_line(y); +} + +static void term_scroll(int lines) { + cli(); + y -= lines; + 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); + } + sti(); +} + +void tty_init(void) +{ + buffer = mapaddr((void*)VGA_ADDR, width * height * sizeof(uint16_t)); + x = 0; + y = 0; + fg = 15; + bg = 0; +} + +void tty_out(char c) +{ + if (buffer == NULL) + return; + + // handle special chars + switch (c) { + case '\n': + x = 0; + y++; + break; + case '\t': + x += 4; + break; + case '\v': + case '\f': + y++; + break; + case '\r': + x = 0; + break; + default: { + const size_t index = y * width + x; + buffer[index] = c | bg << 12 | fg << 8; + x++; + } + } + + // wrap cursor if needed + if (x >= width) { + x = 0; + y++; + } + if (y >= height) { + term_scroll(y - (height - 1)); + y = height - 1; + } + + // set cursor position on screen + const uint16_t pos = y * width + x; + outb(0x3D4, 0x0F); + outb(0x3D5, (uint8_t) (pos & 0xFF)); + outb(0x3D4, 0x0E); + outb(0x3D5, (uint8_t) ((pos >> 8) & 0xFF)); +} diff --git a/kernel/drivers/uart.c b/kernel/drivers/uart.c new file mode 100644 index 0000000..c999e45 --- /dev/null +++ b/kernel/drivers/uart.c @@ -0,0 +1,43 @@ +#include <comus/drivers/uart.h> +#include <comus/asm.h> + +#define PORT 0x3F8 + +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 + 2, 0xC7); // enable and clear FIFOs, set to maximum threshold + outb(PORT + 4, 0x0B); // ??? + outb(PORT + 4, 0x1E); // set in loopback mode for test + outb(PORT + 0, 0xAE); // test by sending 0xAE + + uint8_t response = inb(PORT + 0); + if (response != 0xAE) { + // TODO: panic here? + return -1; + } + + // disable loopback, enable IRQs + outb(PORT + 4, 0x0F); + return 0; +} + +uint8_t uart_in(void) { + // wait for data to be available + while ((inb(PORT + 5) & 0x01) == 0); + return inb(PORT); +} + +void uart_out(uint8_t ch) { + // wait for output to be free + while ((inb(PORT + 5) & 0x20) == 0); + outb(PORT, ch); +} + +void uart_out_str(const char *str) { + while (*str) + uart_out(*str++); +} diff --git a/kernel/include/comus/drivers/tty.h b/kernel/include/comus/drivers/tty.h new file mode 100644 index 0000000..c951c3e --- /dev/null +++ b/kernel/include/comus/drivers/tty.h @@ -0,0 +1,27 @@ +/** + * @file tty.h + * + * @author Freya Murphy <freya@freyacat.org> + * + * Terminal output + */ + +#ifndef TTY_H_ +#define TTY_H_ + +/** + * Initalize the terminal + */ +void tty_init(void); + +/** + * Output a character to the terminal + */ +void tty_out(char c); + +/** + * Output a string to the terminal + */ +void tty_out_str(char *str); + +#endif /* tty.h */ diff --git a/kernel/include/comus/drivers/uart.h b/kernel/include/comus/drivers/uart.h new file mode 100644 index 0000000..9a2d70a --- /dev/null +++ b/kernel/include/comus/drivers/uart.h @@ -0,0 +1,19 @@ +/** + * @file uart.h + * + * @author Freya Murphy <freya@freyacat.org> + * + * Serial interface + */ + +#ifndef UART_H_ +#define UART_H_ + +#include <stdint.h> + +int uart_init(void); +uint8_t uart_in(void); +void uart_out(uint8_t ch); +void uart_out_str(const char *str); + +#endif /* uart.h */ diff --git a/kernel/io/io.c b/kernel/io/io.c deleted file mode 100644 index 489148d..0000000 --- a/kernel/io/io.c +++ /dev/null @@ -1,19 +0,0 @@ -#include <lib.h> -#include <stdio.h> -#include <comus/asm.h> - -#define PORT 0x3F8 -static void serial_out(uint8_t ch) -{ - // wait for output to be free - while ((inb(PORT + 5) & 0x20) == 0) - ; - outb(PORT, ch); -} - -void fputc(FILE *stream, char c) -{ - (void)stream; - - serial_out(c); -} diff --git a/kernel/lib/fputc.c b/kernel/lib/fputc.c new file mode 100644 index 0000000..ef9b6f6 --- /dev/null +++ b/kernel/lib/fputc.c @@ -0,0 +1,11 @@ +#include <lib.h> +#include <comus/drivers/tty.h> +#include <comus/drivers/uart.h> + +void fputc(FILE *stream, char c) +{ + (void)stream; // TODO: manage stream + uart_out(c); + tty_out(c); +} + diff --git a/kernel/io/panic.c b/kernel/lib/panic.c index 403418f..403418f 100644 --- a/kernel/io/panic.c +++ b/kernel/lib/panic.c |