From 5c32c17474edb0e10c83eedd6f262f1f54abadb0 Mon Sep 17 00:00:00 2001 From: shockrahwow Date: Mon, 7 Oct 2019 18:51:29 -0700 Subject: [PATCH] FIRST (semi)WORKING PASS OF SERIAL DEBUGGING FINALLY!!!!! --- framebuffer.c | 8 +++---- loader.s | 4 ++-- ports.h | 2 +- ports.s | 23 +++++--------------- serial.c | 59 +++++++++++++++++++++++++++++++++++++-------------- serial.h | 7 ++++-- tests.c | 12 +++++++---- tests.h | 1 + 8 files changed, 69 insertions(+), 47 deletions(-) diff --git a/framebuffer.c b/framebuffer.c index 1ed12c3..ad8261a 100644 --- a/framebuffer.c +++ b/framebuffer.c @@ -39,9 +39,9 @@ void clear_fb(void) { // Takes the linear indexed position to move the cursos, if there is nothing at that position we should still have something there void fb_move_cursor(u16 position) { - serial_control(FB_CMD, FB_HIGH_CMD); - serial_control(FB_DATA, ((position >> 8) & 0x00ff) ); - serial_control(FB_CMD, FB_LOW_CMD); - serial_control(FB_DATA, position & 0x00ff); + serialport_write_byte(FB_CMD, FB_HIGH_CMD); + serialport_write_byte(FB_DATA, ((position >> 8) & 0x00ff) ); + serialport_write_byte(FB_CMD, FB_LOW_CMD); + serialport_write_byte(FB_DATA, position & 0x00ff); } diff --git a/loader.s b/loader.s index ea24c2d..7428f8f 100644 --- a/loader.s +++ b/loader.s @@ -9,7 +9,7 @@ CHECKSUM equ -MAGIC_NUMBER ; size in bytes of stack KERNEL_STACK_SIZE equ 4096 -extern test_write +extern test_dispatcher section .text ; align all instructions to 4 byte boundary by the x86 instruction set law @@ -20,7 +20,7 @@ align 4 dd CHECKSUM loader: - call test_write + call test_dispatcher .loop: jmp .loop diff --git a/ports.h b/ports.h index c0fb3da..a05d324 100644 --- a/ports.h +++ b/ports.h @@ -3,7 +3,7 @@ #ifndef INCLUDE_IO_H #define INCLUDE_IO_H -void serial_control(const u16, const u16); +void serialport_write_byte(const u16, const u16); u8 serial_read_buffer(const u16); #endif /* INCLUDE_IO_H */ diff --git a/ports.s b/ports.s index ed49e15..30b4313 100644 --- a/ports.s +++ b/ports.s @@ -1,34 +1,21 @@ ; Helpers for frame buffer which pretty much have to to be ; written in asm -global serial_control +global serialport_write_byte global serial_read_buffer -serial_control: - ;push ebp - ;mov ebp, esp - ;add esp, 0xc ; ebp + 2 local vars (32-bit kernel) - +; write a byte +;serialport_write_byte +serialport_write_byte: mov al, [esp+8] mov dx, [esp+4] out dx, al - - ;mov esp, ebp - ;pop ebp ret ; Read byte from serial port +;serialport_read_byte serial_read_buffer: - ;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 ret diff --git a/serial.c b/serial.c index b7cc353..2c611c7 100644 --- a/serial.c +++ b/serial.c @@ -17,41 +17,68 @@ void serial_set_baud_rate(const u16 com_port, const u16 divisor) { // send the higher 8 bits first // send the lower 8 bits next - serial_control(SERIAL_LINE_COMMAND_PORT(com_port), + serialport_write_byte(SERIAL_LINE_COMMAND_PORT(com_port), SERIAL_LINE_ENABLE); - serial_control(SERIAL_DATA_PORT(com_port), + serialport_write_byte(SERIAL_DATA_PORT(com_port), (divisor>>8) & 0x00ff); - serial_control(SERIAL_DATA_PORT(com_port), + serialport_write_byte(SERIAL_DATA_PORT(com_port), divisor & 0x00ff); } // Payload described above void serial_configure_line(const u16 line, const u8 payload) { - serial_control(SERIAL_LINE_COMMAND_PORT(line), payload); + serialport_write_byte(SERIAL_LINE_COMMAND_PORT(line), payload); } + 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; + // we get back 0x20 if it is empty + return serial_read_buffer(SERIAL_LINE_STATUS_PORT(com_port)) & + SERIAL_FIFO_EMPTY_CODE; } -u64 serial_write(const s8* buffer, const u64 size) { +u64 serial_write(const char* 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; + //serialport_write_byte(SERIAL_COM1_BASE + 1, 0x00); + serialport_write_byte(SERIAL_DATA_PORT_INT_EN(SERIAL_COM1_BASE), + 0x00); + + //serialport_write_byte(SERIAL_COM1_BASE + 3, 0x80); + serialport_write_byte(SERIAL_LINE_COMMAND_PORT(SERIAL_COM1_BASE), + SERIAL_LINE_ENABLE); + + //serialport_write_byte(SERIAL_COM1_BASE + 0, 0x03); + serialport_write_byte(SERIAL_COM1_BASE, + SERIAL_DEFAULT_LINE_CFG); + + //serialport_write_byte(SERIAL_COM1_BASE + 1, 0x00); + serialport_write_byte(SERIAL_DATA_PORT_INT_EN(SERIAL_COM1_BASE), + 0x00); + //serialport_write_byte(SERIAL_COM1_BASE + 3, 0x03); + serialport_write_byte(SERIAL_LINE_COMMAND_PORT(SERIAL_COM1_BASE), + SERIAL_DEFAULT_LINE_CFG); + + //serialport_write_byte(SERIAL_COM1_BASE + 2, 0xC7); + serialport_write_byte(SERIAL_FIFO_COMMAND_PORT(SERIAL_COM1_BASE), + SERIAL_DEFAULT_BUFFER_CFG); + + //serialport_write_byte(SERIAL_COM1_BASE + 4, 0x0B); + serialport_write_byte(SERIAL_MODEM_COMMAND_PORT(SERIAL_COM1_BASE), + 0x0B); + + for(idx = 0; idx < size; idx++) { + serialport_write_byte(SERIAL_COM1_BASE, buffer[idx]); + /* + if(serial_fifo_empty(SERIAL_COM1_BASE == 0x02)) { + serialport_write_byte(SERIAL_COM1_BASE, buffer[idx]); } + */ } return idx; } + diff --git a/serial.h b/serial.h index 7284dc4..bf8c2bf 100644 --- a/serial.h +++ b/serial.h @@ -6,6 +6,7 @@ #define SERIAL_COM1_BASE 0x03F8 #define SERIAL_DATA_PORT(base) (base) +#define SERIAL_DATA_PORT_INT_EN(base) (base+1) #define SERIAL_FIFO_COMMAND_PORT(base) (base+2) #define SERIAL_LINE_COMMAND_PORT(base) (base+3) #define SERIAL_MODEM_COMMAND_PORT(base) (base+4) @@ -15,14 +16,16 @@ // Default configurations for serial ports/lines/buffers etc. // Rational can be found here: https://littleosbook.github.io/#configuring-the-buffers -#define SERIAL_DEFAULT_LINE_CFG 0X03 +#define SERIAL_DEFAULT_LINE_CFG 0x03 #define SERIAL_DEFAULT_BUFFER_CFG 0xc7 #define SERIAL_DEFAULT_MODEM_CFG 0x03 +#define SERIAL_FIFO_EMPTY_CODE 0x20 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); +u64 serial_write(const char* buffer, const u64 size); + diff --git a/tests.c b/tests.c index 710a06b..9af850c 100644 --- a/tests.c +++ b/tests.c @@ -5,9 +5,13 @@ void test_write() { clear_fb(); - char* msg1 = "Imagine literally not \nwriting your own os"; - serial_write((signed char*)msg1, strlen(msg1)); - serial_write((signed char*)msg1, strlen(msg1)); - //write(msg1, strlen(msg1)); + char* msg1 = "Writing this to fbout\nOh yea and serial 0x3f8"; printf(msg1); + serial_write(msg1, strlen(msg1)); + char* msg2 = "\nserial write has finished"; + printf(msg2); +} + +void test_dispatcher() { + test_write(); } diff --git a/tests.h b/tests.h index b1ab1bc..a5389b5 100644 --- a/tests.h +++ b/tests.h @@ -2,3 +2,4 @@ #include "stlio.h" void test_write(); +void test_dispatcher();