From 7c901071a1168d41905025177f1a7d5f6b873f7f Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 23 Aug 2021 21:18:15 +0200 Subject: [PATCH 1/8] Refactor --- drivers/vga.c | 20 +++++++++++++------- kernel/kernel.c | 2 ++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/drivers/vga.c b/drivers/vga.c index 3ea9cf6..c2e1dbe 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -5,8 +5,11 @@ // Charpos = 0xb8000 + 2(row*80 + col) // Memory -#define VIDEO_MEM (char*)0xb8000 -#define VIDEO_MEM_MAX (char*)0xb8fa0 +#define VGA_ADDRESS (char*)0xb8000 +#define VGA_ADDRESS_MAX (char*)0xb8fa0 + +#define MAX_ROWS 25 +#define MAX_COLS 80 // Global static unsigned int cursor_row = 0; @@ -16,12 +19,15 @@ static unsigned int cursor_col = 0; VGA & Memory Functions */ char* get_vga_charpos_pointer(unsigned int col, unsigned int row) { - return (char*)(VIDEO_MEM + 2*((row*80) + col)); + return (char*)(VGA_ADDRESS + 2*((row*80) + col)); } void writechar(char c, unsigned int col, unsigned int row, int attribute_byte) { + if( !attribute_byte ) + attribute_byte = 0x0f; + char* mem = get_vga_charpos_pointer(col, row); - *mem = c; // Write the character + *mem = c; // Write the character *(mem+1) = attribute_byte; // Write the attribute_byte } @@ -30,9 +36,9 @@ void writechar(char c, unsigned int col, unsigned int row, int attribute_byte) { Graphics Functions */ void clear_screen() { - // Make all the characters spaces - for( char* c = VIDEO_MEM; c <= VIDEO_MEM_MAX; c += 2 ) - *c = 0x20; + for( int c = 0; c < MAX_COLS; c++ ) + for( int r = 0; r < MAX_ROWS; r++ ) + writechar(0x20, c, r, 0x0); } void disable_vga_cursor() { diff --git a/kernel/kernel.c b/kernel/kernel.c index 2d24fd9..2620165 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -1,3 +1,5 @@ +#define NULL ((void*)0x0) + #include "../drivers/vga.h" void main() { From c939c33b27ada55bf546641c76fe4fbd219c3c2b Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 23 Aug 2021 21:20:04 +0200 Subject: [PATCH 2/8] Minor style change --- drivers/vga.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/vga.c b/drivers/vga.c index c2e1dbe..5039b9c 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -38,7 +38,7 @@ void writechar(char c, unsigned int col, unsigned int row, int attribute_byte) { void clear_screen() { for( int c = 0; c < MAX_COLS; c++ ) for( int r = 0; r < MAX_ROWS; r++ ) - writechar(0x20, c, r, 0x0); + writechar(0x20, c, r, 0xf0); } void disable_vga_cursor() { From 21d8767ec82cd5e0ae59b95c3c5080ccf6705c3c Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 23 Aug 2021 21:20:17 +0200 Subject: [PATCH 3/8] Removed bloat --- kernel/kernel.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/kernel/kernel.c b/kernel/kernel.c index 2620165..2d24fd9 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -1,5 +1,3 @@ -#define NULL ((void*)0x0) - #include "../drivers/vga.h" void main() { From 28aeaec3965ff05705a0aa459898244fab120912 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 23 Aug 2021 21:36:20 +0200 Subject: [PATCH 4/8] Cleanup --- drivers/vga.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/drivers/vga.c b/drivers/vga.c index 5039b9c..150263a 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -1,9 +1,6 @@ // VGA Graphics Library #include "../kernel/io.h" -// VGA base address: 0xb8000 -// Charpos = 0xb8000 + 2(row*80 + col) - // Memory #define VGA_ADDRESS (char*)0xb8000 #define VGA_ADDRESS_MAX (char*)0xb8fa0 @@ -11,7 +8,6 @@ #define MAX_ROWS 25 #define MAX_COLS 80 -// Global static unsigned int cursor_row = 0; static unsigned int cursor_col = 0; @@ -32,6 +28,11 @@ void writechar(char c, unsigned int col, unsigned int row, int attribute_byte) { } +void set_cursor_pos(unsigned int x, unsigned int y) { + cursor_col = x; + cursor_row = y; +} + /* Graphics Functions */ @@ -50,11 +51,6 @@ void disable_vga_cursor() { /* General Printing Functions */ -void set_cursor_pos(unsigned int x, unsigned int y) { - cursor_col = x; - cursor_row = y; -} - void print(char* str, int attribute_byte) { for( char* c = str; *c != '\0'; c++ ) writechar(*c, (unsigned int)(c - str) + cursor_col, cursor_row, attribute_byte); From dcb763a334a1650371a801394b9d7aa0af3a112e Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 23 Aug 2021 21:39:27 +0200 Subject: [PATCH 5/8] Test code --- kernel/kernel.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/kernel.c b/kernel/kernel.c index 2d24fd9..db63a5e 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -6,4 +6,8 @@ void main() { set_cursor_pos(28, 2); print("eOS Version 0.1 2021", 0xf0); + + set_cursor_pos(0, 0); + for( int i = 0; i < 255; i++ ) + println("X", i); } From 4a56eb0156ff49738c991ffabe132dfb7985991a Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 23 Aug 2021 21:53:06 +0200 Subject: [PATCH 6/8] Cleanup --- drivers/vga.c | 6 +++--- kernel/kernel.c | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/vga.c b/drivers/vga.c index 150263a..8173d5a 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -28,9 +28,9 @@ void writechar(char c, unsigned int col, unsigned int row, int attribute_byte) { } -void set_cursor_pos(unsigned int x, unsigned int y) { - cursor_col = x; - cursor_row = y; +void set_cursor_pos(unsigned int col, unsigned int row) { + cursor_col = col; + cursor_row = row; } /* diff --git a/kernel/kernel.c b/kernel/kernel.c index db63a5e..290d9e5 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -7,7 +7,9 @@ void main() { set_cursor_pos(28, 2); print("eOS Version 0.1 2021", 0xf0); + /* set_cursor_pos(0, 0); for( int i = 0; i < 255; i++ ) println("X", i); + */ } From f5771c0dc9823c5505a9032f4b26e92e8abb17ce Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 23 Aug 2021 21:58:22 +0200 Subject: [PATCH 7/8] More cleanup --- drivers/vga.c | 25 ++++++++++++------------- drivers/vga.h | 3 +-- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/drivers/vga.c b/drivers/vga.c index 8173d5a..e215b7f 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -1,4 +1,5 @@ // VGA Graphics Library +#include "vga.h" #include "../kernel/io.h" // Memory @@ -11,10 +12,19 @@ static unsigned int cursor_row = 0; static unsigned int cursor_col = 0; +void vga_init() { + // Disable cursor + port_outb(0x0a, 0x3d4); + port_outb(0x20, 0x3d5); + + // Clear screen + clear_screen(); +} + /* VGA & Memory Functions */ -char* get_vga_charpos_pointer(unsigned int col, unsigned int row) { +char* get_memory_charpos(unsigned int col, unsigned int row) { return (char*)(VGA_ADDRESS + 2*((row*80) + col)); } @@ -22,7 +32,7 @@ void writechar(char c, unsigned int col, unsigned int row, int attribute_byte) { if( !attribute_byte ) attribute_byte = 0x0f; - char* mem = get_vga_charpos_pointer(col, row); + char* mem = get_memory_charpos(col, row); *mem = c; // Write the character *(mem+1) = attribute_byte; // Write the attribute_byte @@ -42,12 +52,6 @@ void clear_screen() { writechar(0x20, c, r, 0xf0); } -void disable_vga_cursor() { - port_outb(0x0a, 0x3d4); - port_outb(0x20, 0x3d5); -} - - /* General Printing Functions */ @@ -62,8 +66,3 @@ void println(char* str, int attribute_byte) { } -// VGA Initialization Function -void vga_init() { - disable_vga_cursor(); - clear_screen(); -} diff --git a/drivers/vga.h b/drivers/vga.h index cf7a721..983fb09 100644 --- a/drivers/vga.h +++ b/drivers/vga.h @@ -1,7 +1,6 @@ -char* get_vga_charpos_pointer(unsigned int col, unsigned int row); +char* get_memory_charpos(unsigned int col, unsigned int row); void writechar(char c, unsigned int col, unsigned int row, int colorcode); void clear_screen(); -void disable_vga_cursor(); void set_cursor_pos(); void print(); void println(); From beb014a6b4a3f7ffa6c9211af450ae5f01d9faef Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 23 Aug 2021 22:00:19 +0200 Subject: [PATCH 8/8] Fixed minor bug --- drivers/vga.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/vga.c b/drivers/vga.c index e215b7f..c53692b 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -14,8 +14,8 @@ static unsigned int cursor_col = 0; void vga_init() { // Disable cursor - port_outb(0x0a, 0x3d4); - port_outb(0x20, 0x3d5); + port_outb(0x3d4, 0x0a); + port_outb(0x3d5, 0x20); // Clear screen clear_screen(); @@ -43,6 +43,7 @@ void set_cursor_pos(unsigned int col, unsigned int row) { cursor_row = row; } + /* Graphics Functions */