From 43e8caefdf80c6f7945c09a42ba09f70d907e68e Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 11 Aug 2021 19:36:51 +0200 Subject: [PATCH 1/7] Added VGA print --- src/kernel.c | 7 +++---- src/lib/vga.c | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 src/lib/vga.c diff --git a/src/kernel.c b/src/kernel.c index c5bc72d..4076de7 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1,7 +1,6 @@ -// VGA base address: 0xb8000 -// Charpos = 0xb8000 + 2(row*80 + col) +#include "lib/vga.c" void main() { - char* video_mem = (char*)0xb8000; // VGA Video Memory location - *video_mem = 'X'; + // Do kernel stuff + println("HEJ", 3); } diff --git a/src/lib/vga.c b/src/lib/vga.c new file mode 100644 index 0000000..61e6275 --- /dev/null +++ b/src/lib/vga.c @@ -0,0 +1,18 @@ +// VGA Graphics Library + +// VGA base address: 0xb8000 +// Charpos = 0xb8000 + 2(row*80 + col) + +#define VIDEO_MEM (char*)0xb8000 +static char* cursor_pos = VIDEO_MEM; +static int cursor_y = 0; + +void set_cursor_pos(int row, int col) { cursor_pos = (char*)(VIDEO_MEM + 2*(row*80 + col)); } + +void println(char* str, int str_len) { + for( int i = 0; i < str_len; i++ ) { + set_cursor_pos(i, cursor_y); // set cursor pos + *cursor_pos = str[i]; // Write char to video memory + } + cursor_y++; // Increment to next y-pos (newline) +} From f769e62887f91f35b4768c9d17a33f470bbf74ec Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 11 Aug 2021 19:40:00 +0200 Subject: [PATCH 2/7] Refactor --- src/lib/vga.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lib/vga.c b/src/lib/vga.c index 61e6275..33de16a 100644 --- a/src/lib/vga.c +++ b/src/lib/vga.c @@ -7,12 +7,16 @@ static char* cursor_pos = VIDEO_MEM; static int cursor_y = 0; -void set_cursor_pos(int row, int col) { cursor_pos = (char*)(VIDEO_MEM + 2*(row*80 + col)); } +void set_cursor_pos(unsigned int row, unsigned int col) { cursor_pos = (char*)(VIDEO_MEM + 2*(row*80 + col)); } -void println(char* str, int str_len) { - for( int i = 0; i < str_len; i++ ) { +void print(char* str, unsigned int str_len) { + for( unsigned int i = 0; i < str_len; i++ ) { set_cursor_pos(i, cursor_y); // set cursor pos *cursor_pos = str[i]; // Write char to video memory } +} + +void println(char* str, unsigned int str_len) { + print(str, str_len); cursor_y++; // Increment to next y-pos (newline) } From 2ca8de3148bbb37dff220473cc423a56b38c76ef Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 11 Aug 2021 19:47:32 +0200 Subject: [PATCH 3/7] Borked printing functions --- src/kernel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel.c b/src/kernel.c index 4076de7..b4a3ac0 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -2,5 +2,5 @@ void main() { // Do kernel stuff - println("HEJ", 3); + println("C-Printing test!", 16); } From 8e0260b8a09d1cad0f1e6511d8ebfc756096a064 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 11 Aug 2021 20:27:44 +0200 Subject: [PATCH 4/7] Refactor --- src/lib/vga.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/lib/vga.c b/src/lib/vga.c index 33de16a..ae2407a 100644 --- a/src/lib/vga.c +++ b/src/lib/vga.c @@ -4,15 +4,13 @@ // Charpos = 0xb8000 + 2(row*80 + col) #define VIDEO_MEM (char*)0xb8000 -static char* cursor_pos = VIDEO_MEM; static int cursor_y = 0; -void set_cursor_pos(unsigned int row, unsigned int col) { cursor_pos = (char*)(VIDEO_MEM + 2*(row*80 + col)); } +char* get_cursor_pos(unsigned int row, unsigned int col) { return (char*)(VIDEO_MEM + 2*(row*80 + col)); } void print(char* str, unsigned int str_len) { for( unsigned int i = 0; i < str_len; i++ ) { - set_cursor_pos(i, cursor_y); // set cursor pos - *cursor_pos = str[i]; // Write char to video memory + *get_cursor_pos(i, cursor_y) = str[i]; // write to video memory } } From a5ba69a1d37f5d2cb6c720254df25a51c1cd08f5 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 11 Aug 2021 21:13:18 +0200 Subject: [PATCH 5/7] Stuff --- src/kernel.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/kernel.c b/src/kernel.c index b4a3ac0..4fbba34 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1,6 +1,8 @@ -#include "lib/vga.c" +//#include "lib/vga.c" void main() { // Do kernel stuff - println("C-Printing test!", 16); + char* vidmem = (char*)(0xb8000); + *vidmem = 'X'; + //println("C-Printing test!", 16); } From 4b028524f01ba53f39e13805c782956d5f9e60c1 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 11 Aug 2021 23:18:43 +0200 Subject: [PATCH 6/7] Fixed kernel compiling & linking problems --- compile.sh | 12 ++++++------ src/bootloader.asm | 2 +- src/kernel.c | 6 ++---- src/lib/vga.c | 4 ++-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/compile.sh b/compile.sh index 57f142e..a1a90f3 100755 --- a/compile.sh +++ b/compile.sh @@ -6,11 +6,11 @@ fail="[FAIL]" srcDir=src binDir=bin -echo -n "Compiling bootloader... " -nasm -f bin $srcDir/bootloader.asm -i $srcDir -o $binDir/bootloader.bin && echo $done +echo "Compiling bootloader... " +nasm -f bin $srcDir/bootloader.asm -i $srcDir -o $binDir/bootloader.bin -echo -n "Compiling kernel... " -gcc -ffreestanding -c $srcDir/kernel.c -o $binDir/kernel.o && ld -o $binDir/kernel.bin -Ttext 0x1000 $binDir/kernel.o --oformat binary 2>/dev/null && echo $done +echo "Compiling kernel... " +gcc -fno-pie -m32 -Os -ffreestanding -c $srcDir/kernel.c -o $binDir/kernel.o && gcc -o $binDir/kernel.bin $binDir/kernel.o -Wl,--oformat=binary -ffreestanding -nostdlib -shared -Ttext 0x1000 -m32 -echo -n "Creating OS image... " -cat $binDir/bootloader.bin $binDir/kernel.bin > $binDir/os-image && echo $done +echo "Creating OS image... " +cat $binDir/bootloader.bin $binDir/kernel.bin > $binDir/os-image diff --git a/src/bootloader.asm b/src/bootloader.asm index b477677..c568288 100644 --- a/src/bootloader.asm +++ b/src/bootloader.asm @@ -38,7 +38,7 @@ BEGIN_PM: load_kernel: mov bx, KERNEL_OFFSET ; Load kernel at the kernel offset - mov dh, 15 ; Read 15 sectors + mov dh, 7 ; Read 15 sectors mov dl, [BOOT_DRIVE] ; Drive index call disk_read ; Load the kernel diff --git a/src/kernel.c b/src/kernel.c index 4fbba34..b4a3ac0 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1,8 +1,6 @@ -//#include "lib/vga.c" +#include "lib/vga.c" void main() { // Do kernel stuff - char* vidmem = (char*)(0xb8000); - *vidmem = 'X'; - //println("C-Printing test!", 16); + println("C-Printing test!", 16); } diff --git a/src/lib/vga.c b/src/lib/vga.c index ae2407a..7a65b68 100644 --- a/src/lib/vga.c +++ b/src/lib/vga.c @@ -10,11 +10,11 @@ char* get_cursor_pos(unsigned int row, unsigned int col) { return (char*)(VIDEO_ void print(char* str, unsigned int str_len) { for( unsigned int i = 0; i < str_len; i++ ) { - *get_cursor_pos(i, cursor_y) = str[i]; // write to video memory + *get_cursor_pos(cursor_y, i) = 'X'; // write to video memory } } void println(char* str, unsigned int str_len) { print(str, str_len); - cursor_y++; // Increment to next y-pos (newline) + cursor_y++; // Increment to next y-pos (newline) } From dbd87a765704aa257fc330adee6f3ac18bc60681 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Thu, 12 Aug 2021 00:09:19 +0200 Subject: [PATCH 7/7] Code refactor --- src/kernel.c | 6 +++++- src/lib/vga.c | 10 ++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/kernel.c b/src/kernel.c index b4a3ac0..8629a26 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -2,5 +2,9 @@ void main() { // Do kernel stuff - println("C-Printing test!", 16); + char* vidmem = (char*)0xb8000; + *vidmem = 'X'; + for( int i = 0; i < 16; i++ ) { + println("C-Printing test!", 16); + } } diff --git a/src/lib/vga.c b/src/lib/vga.c index 7a65b68..d951031 100644 --- a/src/lib/vga.c +++ b/src/lib/vga.c @@ -4,14 +4,16 @@ // Charpos = 0xb8000 + 2(row*80 + col) #define VIDEO_MEM (char*)0xb8000 +#define GET_INDEX(s, c) (int)(c-s) static int cursor_y = 0; -char* get_cursor_pos(unsigned int row, unsigned int col) { return (char*)(VIDEO_MEM + 2*(row*80 + col)); } +char* get_cursor_pos(unsigned int col, unsigned int row) { + return (char*)(VIDEO_MEM + 2*((row*80) + col)); +} void print(char* str, unsigned int str_len) { - for( unsigned int i = 0; i < str_len; i++ ) { - *get_cursor_pos(cursor_y, i) = 'X'; // write to video memory - } + for( char* c = str; *c != '\0'; c++ ) + *get_cursor_pos( GET_INDEX(str, c), cursor_y ) = 'Y'; } void println(char* str, unsigned int str_len) {