From dbdaa77fb5924b4b9e6b374a44ef76481a38d3d2 Mon Sep 17 00:00:00 2001 From: aqua Date: Tue, 8 Nov 2022 17:19:45 +0200 Subject: Add FILE struct --- devices/uart_16550.c | 78 +++++++++++++++++++++++++++++----------------------- devices/uart_16550.h | 8 ++++-- devices/vga.c | 78 +++++++++++++++++++++++++++++++++------------------- devices/vga.h | 8 ++++-- 4 files changed, 102 insertions(+), 70 deletions(-) (limited to 'devices') diff --git a/devices/uart_16550.c b/devices/uart_16550.c index 22f33ad..40ac594 100644 --- a/devices/uart_16550.c +++ b/devices/uart_16550.c @@ -1,4 +1,5 @@ #include "uart_16550.h" +#include #include enum uart_16550_offset { @@ -42,31 +43,6 @@ enum LineStatus { ERRO = (1 << 7), // impending error: see if there is an error with a word in the input buffer }; -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) { @@ -74,36 +50,68 @@ uart_thre(enum UART port) } void -uart_write(enum UART port, char a) +uart_write(const FILE *self, char a) { - while (uart_thre(port) == 0) - ; - outb(a, port); + while (uart_thre(self->id) == 0) {} + outb(a, self->id); if (a == '\n') { - while (uart_thre(port) == 0) - ; - outb('\r', port); + while (uart_thre(self->id) == 0) {} + outb('\r', self->id); } } int -uart_puts(enum UART port, const char *string, int length) +uart_puts(const FILE *self, const char *string, int length) { int written = 0; if (length == -1) while (*string != '\0') { - uart_write(port, *string); + uart_write(self, *string); ++string; ++written; } else for (int i = 0; i < length; ++i) { - uart_write(port, string[i]); + uart_write(self, string[i]); ++written; } return written; } + +void +uart_flush(const FILE *self) +{ +} + +FILE uart_stream; + +FILE * +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 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_write; + uart_stream.puts = &uart_puts; + uart_stream.flush = &uart_flush; + return &uart_stream; +} diff --git a/devices/uart_16550.h b/devices/uart_16550.h index bb219cc..5e5ee13 100644 --- a/devices/uart_16550.h +++ b/devices/uart_16550.h @@ -1,5 +1,7 @@ #pragma once +#include + enum UART { COM1 = 0x3f8, COM2 = 0x2f8, @@ -11,6 +13,6 @@ enum UART { COM8 = 0x4E8, }; -int uart_init(enum UART port); -void uart_write(enum UART port, char a); -int uart_puts(enum UART port, const char *string, int length); +FILE *uart_init(enum UART port); +// void uart_write(enum UART port, char a); +// int uart_puts(enum UART port, const char *string, int length); diff --git a/devices/vga.c b/devices/vga.c index 39370ba..12ac880 100644 --- a/devices/vga.c +++ b/devices/vga.c @@ -55,31 +55,9 @@ vga_update_cursor() outb((pos >> 8) & 0xff, cga_dat_port); } -// *** Text Mode *** -void -vga_init() -{ - buffer = (struct VGAEntry *)0xc03ff000; - vga_enable_cursor(14, 15); - vga_clear(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_LIGHT_GREY); -} - -void -vga_clear(enum vga_color foreground, enum vga_color background) -{ - for (int y = 0; y < height; ++y) - for (int 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; - vga_update_cursor(); -} - +// *** Text Mode Output *** void -vga_putc(char a) +vga_putc(const FILE *self, char a) { switch (a) { case '\n': @@ -117,16 +95,58 @@ vga_putc(char a) } } -void -vga_puts(const char *string, int len) +int +vga_puts(const FILE *self, const char *string, int len) { - + int written = 0; if (len == -1) while (*string != '\0') { - vga_putc(*string); + vga_putc(self, *string); ++string; + ++written; } else - for (int i = 0; i < len; ++i) { vga_putc(string[i]); } + for (int i = 0; i < len; ++i) { + vga_putc(self, string[i]); + ++written; + } + return written; +} + +void +vga_flush(const FILE *self) +{ + vga_update_cursor(); +} + +// *** Text Mode *** +FILE vga_stream; + +FILE * +vga_init() +{ + buffer = (struct VGAEntry *)0xc03ff000; + 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) +{ + for (int y = 0; y < height; ++y) + for (int 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; + vga_update_cursor(); } diff --git a/devices/vga.h b/devices/vga.h index daaa482..d9dfd44 100644 --- a/devices/vga.h +++ b/devices/vga.h @@ -1,5 +1,7 @@ #pragma once +#include + /** Hardware text mode color constants. */ enum vga_color { VGA_COLOR_BLACK = 0, @@ -20,11 +22,11 @@ enum vga_color { VGA_COLOR_WHITE = 15, }; -void vga_init(); +FILE *vga_init(); 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_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(); -- cgit v1.2.1