From a4534660fecb1b571c24ad82e67bc6c2fb814cb9 Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Sun, 24 Jan 2021 21:10:57 -0600 Subject: x86: reorganize the i386 directory Add structure to the internals of the x86 directory. Signed-off-by: Danny Holman --- arch/i386/kernel/pic.h | 16 ++++++++++++++++ arch/i386/kernel/serial.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 arch/i386/kernel/pic.h create mode 100644 arch/i386/kernel/serial.c (limited to 'arch/i386/kernel') diff --git a/arch/i386/kernel/pic.h b/arch/i386/kernel/pic.h new file mode 100644 index 0000000..da4a2ee --- /dev/null +++ b/arch/i386/kernel/pic.h @@ -0,0 +1,16 @@ +#ifndef PIC_H +#define PIC_H + +#include + +static inline void outb(uint16_t port, uint8_t value) { + asm volatile("outb %0, %1" : : "a"(value), "Nd"(port)); +} + +static inline uint8_t inb(uint16_t port) { + uint8_t ret; + asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port)); + return ret; +} + +#endif diff --git a/arch/i386/kernel/serial.c b/arch/i386/kernel/serial.c new file mode 100644 index 0000000..20bd8f9 --- /dev/null +++ b/arch/i386/kernel/serial.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include "pic.h" + +#define PORT 0x3f8 + +int serial_init(void) { + outb(PORT + 1, 0x00); + outb(PORT + 3, 0x80); + outb(PORT + 0, 0x03); + outb(PORT + 1, 0x00); + outb(PORT + 3, 0x03); + outb(PORT + 2, 0xC7); + outb(PORT + 4, 0x0B); + outb(PORT + 4, 0x1E); + outb(PORT + 0, 0xAE); + + if (inb(PORT + 0) != 0xAE) + return -1; + + outb(PORT + 4, 0x0F); + return 0; +} + +int serial_recieved(void) { + return inb(PORT + 5) & 1; +} + +char read_serial(void) { + while (serial_recieved() == 0); + return inb(PORT); +} + +int is_transmit_empty(void) { + return inb(PORT + 5) & 0x20; +} + +void write_serial(char a) { + while (is_transmit_empty() == 0); + outb(PORT, a); +} + +void serial_writestring(const char *str) { + for (size_t i = 0; i < strlen(str); i++) + write_serial(str[i]); +} -- cgit v1.2.3