From c72ac8bf3b9eb7bbed11287ccb8833d056b11480 Mon Sep 17 00:00:00 2001 From: shockrah Date: Sun, 6 Oct 2019 14:17:58 -0700 Subject: [PATCH] support for newlines in write call but not printf :( --- bochsrc.conf | 2 +- ports.s | 33 +++++++++++---------------------- scripts/setup.sh | 2 +- serial.c | 21 ++++++++++++++++++++- serial.h | 6 ++++-- stlio.c | 39 ++++++++++++++++++++++++++++++++++++++- stlio.h | 2 ++ tests.c | 7 +++++-- 8 files changed, 82 insertions(+), 30 deletions(-) diff --git a/bochsrc.conf b/bochsrc.conf index 4b4b8f2..4607ea8 100644 --- a/bochsrc.conf +++ b/bochsrc.conf @@ -7,4 +7,4 @@ boot: cdrom log: bochslog.txt clock: sync=realtime, time0=local cpu: count=1, ips=1000000 -com1: enabled=1, mode=file, dev=com1.out +com1: enabled=1, mode=file, dev=COM1.out diff --git a/ports.s b/ports.s index d4c5a40..ed49e15 100644 --- a/ports.s +++ b/ports.s @@ -1,45 +1,34 @@ ; Helpers for frame buffer which pretty much have to to be ; written in asm - -; COM1 Base Port -%define SERIAL_COM1_BASE 0x3F8 - -%define SERIAL_DATA_PORT(base) (base) -%define SERIAL_FIFO_COMMAND_PORT(base) (base+2) -%define SERIAL_LINE_COMMAND_PORT(base) (base+3) -%define SERIAL_MODEM_COMMAND_PORT(base) (base+4) -%define SERIAL_LINE_STATUS_PORT(base) (base+5) - -%define SERIAL_DATA_ global serial_control global serial_read_buffer serial_control: - push ebp - mov ebp, esp - add esp, 0xc ; ebp + 2 local vars (32-bit kernel) + ;push ebp + ;mov ebp, esp + ;add esp, 0xc ; ebp + 2 local vars (32-bit kernel) mov al, [esp+8] mov dx, [esp+4] out dx, al - mov esp, ebp - pop ebp + ;mov esp, ebp + ;pop ebp ret ; Read byte from serial port serial_read_buffer: - push ebp - mov ebp, esp - push ebx + ;push ebp + ;mov ebp, esp + ;push ebx xor eax, eax mov dx, [esp + 4] ; grab the address of the targeted port in al, dx ; read byte from the port - mov esp, ebp - pop ebx - pop ebp + ;mov esp, ebp + ;pop ebx + ;pop ebp ret diff --git a/scripts/setup.sh b/scripts/setup.sh index d0906ca..14f8ee6 100644 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Setup script to grab any and all dependancies needed to build this on any of my machines bochs_=$(dpkg --list | grep bochs) if [ "$bochs_" == "" ] diff --git a/serial.c b/serial.c index 643efd6..b7cc353 100644 --- a/serial.c +++ b/serial.c @@ -12,7 +12,7 @@ * */ -void serial_set_buad_rate(const u16 com_port, const u16 divisor) { +void serial_set_baud_rate(const u16 com_port, const u16 divisor) { // Activate line - ready to send higher 8 bits of data // send the higher 8 bits first // send the lower 8 bits next @@ -36,3 +36,22 @@ u8 serial_fifo_empty(const u16 com_port) { // If the 5th bit is set then we know that the fifo queue is empty return serial_read_buffer(SERIAL_LINE_STATUS_PORT(com_port)) & 0x20; } + +u64 serial_write(const s8* buffer, const u64 size) { + /* + * Writes a given buffer to the com1 serial port for debugging in bochs + */ + u64 idx = 0; + serial_set_baud_rate(SERIAL_COM1_BASE, 1); // base baud rate + while(idx < size) { + if(serial_fifo_empty(SERIAL_COM1_BASE) == 0x20) { + serial_control(SERIAL_COM1_BASE, buffer[idx]); + idx++; + } + // otherwise we just wait until the queue is empty + else { + continue; + } + } + return idx; +} diff --git a/serial.h b/serial.h index ec386da..7284dc4 100644 --- a/serial.h +++ b/serial.h @@ -3,7 +3,7 @@ // Serial driver interface -#define SERIAL_COM1_BASE 0x3F8 +#define SERIAL_COM1_BASE 0x03F8 #define SERIAL_DATA_PORT(base) (base) #define SERIAL_FIFO_COMMAND_PORT(base) (base+2) @@ -19,8 +19,10 @@ #define SERIAL_DEFAULT_BUFFER_CFG 0xc7 #define SERIAL_DEFAULT_MODEM_CFG 0x03 -void serial_set_buad_rate(const u16, const u16); +void serial_set_baud_rate(const u16, const u16); void serial_configure_line(const u16, const u8); u8 serial_fifo_empty(const u16); + +u64 serial_write(const s8* buffer, const u64 size); diff --git a/stlio.c b/stlio.c index a50536d..faa32fe 100644 --- a/stlio.c +++ b/stlio.c @@ -1,15 +1,52 @@ #include "stlio.h" +static const char _line[52] = {" "}; // We are assuming null-terminated strings here u64 strlen(char* buffer) { char* c; for(c = buffer; *c != '\0'; c++); // LULW return (u64)(c - buffer); } +void putch(const char c) { + write_cell_fb(c, Green, White); +} + u64 write(const char* buffer, const u64 size) { u64 i; for(i = 0; i < size; i++) { - write_cell_fb(buffer[i], Green, White); + if(buffer[i] == '\n') { + write(_line, sizeof(_line)); + } + else { + write_cell_fb(buffer[i], Green, White); + } } return i; } + +u64 read(const u64 n) { + // read n bytes from keyboard + return n; +} + +void printf(const s8* fmt, ...) { + u64 i = 0; + char c = *fmt; + while(c != '\0') { + /* Check for some kind of contol character + * for now we just have \n and \t + */ + switch(c) { + case '\n': { + write(_line, sizeof(_line)); + } + case '%': { + continue; // eventually there will be more support but for now lmao nah + } + default: { + putch(fmt[i]); + i++; + } + } + } +} diff --git a/stlio.h b/stlio.h index 20d60ad..c4aa255 100644 --- a/stlio.h +++ b/stlio.h @@ -4,3 +4,5 @@ u64 strlen(char*); u64 write(const char*, const u64); +u64 read(const u64); +void printf(const s8*, ...); diff --git a/tests.c b/tests.c index 0d5faae..808a278 100644 --- a/tests.c +++ b/tests.c @@ -1,9 +1,12 @@ // Module for testing drivers and other things #include "stlio.h" #include "tests.h" +#include "serial.h" void test_write() { clear_fb(); - char* msg1 = "bigyeet"; - write(msg1, strlen(msg1)); + char* msg1 = "Imagine literally not \nwriting your own os"; + serial_write((signed char*)msg1, strlen(msg1)); + //write(msg1, strlen(msg1)); + printf((signed char*)msg1); }