From aaffd2351f95e576fae089ddafbb32a431a35ca3 Mon Sep 17 00:00:00 2001 From: Jarkko Toivanen Date: Tue, 13 Jun 2023 09:45:04 +0300 Subject: [PATCH] Printing stuff to screen --- Makefile | 9 +++-- kernel.c | 30 +++++++++++------ start32.asm | 61 ++++++++++++++++++++------------- vga.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ vga.h | 20 +++++++++++ 5 files changed, 180 insertions(+), 37 deletions(-) create mode 100644 vga.c create mode 100644 vga.h diff --git a/Makefile b/Makefile index 77b84cc..821bdab 100644 --- a/Makefile +++ b/Makefile @@ -5,9 +5,12 @@ clean: start32.o: start32.asm fasm start32.asm kernel.o: kernel.c - ../tinycc/tcc -m32 -c kernel.c -kernel-i386.elf: kernel.o start32.o - ../tinycc/tcc -m32 -nostdlib -Wl,-Ttext,0x100000 start32.o kernel.o -o kernel-i386.elf + tcc -m32 -c kernel.c +vga.o: vga.c + tcc -m32 -c vga.c + +kernel-i386.elf: kernel.o start32.o vga.o + tcc -m32 -nostdlib -Wl,-Ttext,0x100000 start32.o kernel.o vga.o -o kernel-i386.elf qemu: kernel-i386.elf qemu-system-i386 -kernel kernel-i386.elf diff --git a/kernel.c b/kernel.c index 7e87560..cce4e31 100644 --- a/kernel.c +++ b/kernel.c @@ -1,12 +1,22 @@ +#include "vga.h" + +// static inline void outb(unsigned int port, unsigned char val) { +// asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port) : "memory"); +// } + void kmain (void) { - *((unsigned char *) 0xB8000) = 'H'; - *((unsigned char *) 0xB8001) = 0x1F; - *((unsigned char *) 0xB8002) = 'E'; - *((unsigned char *) 0xB8003) = 0x1F; - *((unsigned char *) 0xB8004) = 'L'; - *((unsigned char *) 0xB8005) = 0x1F; - *((unsigned char *) 0xB8006) = 'L'; - *((unsigned char *) 0xB8007) = 0x1F; - *((unsigned char *) 0xB8008) = 'O'; - *((unsigned char *) 0xB8009) = 0x1F; + + // Cursor disabling + // TODO: outb function + //outb(0x3D4, 0x0A); + //outb(0x3D5, 0x20); + + //cls(); + vga_init(VGA_COLOR_GREY, VGA_COLOR_BLACK); + + vga_write_line("=== KoalemOS ==="); + vga_write("Testing color writing... "); + vga_write_color("Yes", VGA_COLOR_BLACK, VGA_COLOR_GREEN); + vga_write_color("No", VGA_COLOR_BLACK, VGA_COLOR_RED); + vga_write_line("Execution finished. Halting CPU..."); } diff --git a/start32.asm b/start32.asm index 257691d..69e4087 100644 --- a/start32.asm +++ b/start32.asm @@ -1,12 +1,6 @@ -; Tutorial: A small kernel with Fasm & TCC -; By Tommy. - - format elf - use32 - -; -; Equates -; + format elf + use32 + MULTIBOOT_PAGE_ALIGN equ (1 shl 0) MULTIBOOT_MEMORY_INFO equ (1 shl 1) MULTIBOOT_AOUT_KLUDGE equ (1 shl 16) @@ -14,30 +8,49 @@ MULTIBOOT_HEADER_MAGIC equ 0x1BADB002 MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN or MULTIBOOT_MEMORY_INFO MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) - -; -; Multiboot header -; .multiboot: align 4 dd MULTIBOOT_HEADER_MAGIC dd MULTIBOOT_HEADER_FLAGS dd MULTIBOOT_CHECKSUM - section '.bss' writable align 16 stack_bottom: rb 16384 stack_top: - section '.text' executable -; -; Kernel entry point. -; - public _start - extrn kmain +section '.text' executable + public _start + extrn kmain + +disable_cursor: + pushf + push eax + push edx + + mov dx, 0x3D4 + mov al, 0xA ; low cursor shape register + out dx, al + + inc dx + mov al, 0x20 ; bits 6-7 unused, bit 5 disables the cursor, bits 0-4 control the cursor shape + out dx, al + + pop edx + pop eax + popf + ret + _start: - ; Call the main kernel function. - call kmain - + ; Setup stack + ;mov esp, stack_top + + ; Call the main kernel function. + call disable_cursor + call kmain + + + @@: - jmp @b + cli + hlt + jmp @b diff --git a/vga.c b/vga.c new file mode 100644 index 0000000..187f56f --- /dev/null +++ b/vga.c @@ -0,0 +1,97 @@ +#include "vga.h" +#define VGA_WIDTH 80 +#define VGA_HEIGHT 25 +#define VGA_MEM_ADDR 0xb8000 +#define CURSOR_HOME (VGA_HEIGHT-1)*VGA_WIDTH +#define CURSOR_CHR 177; + +unsigned int cursor_loc = CURSOR_HOME; +unsigned char fgcolor; +unsigned char bgcolor; + +static unsigned char vga_entry_color(enum vga_color fg, enum vga_color bg) { + return fg | bg << 4; +} +unsigned short vga_blank_entry() { + return vga_entry_color(fgcolor, bgcolor) << 8; +} +void draw_cursor(void) { + *((unsigned char *)VGA_MEM_ADDR + cursor_loc * 2) = CURSOR_CHR; + *((unsigned char *)VGA_MEM_ADDR+1 + cursor_loc * 2) = vga_entry_color(fgcolor, bgcolor); +} + +void vga_set_color(enum vga_color fg, enum vga_color bg) { + fgcolor = fg; + bgcolor = bg; +} +void vga_init(enum vga_color fg, enum vga_color bg) { + vga_set_color(fg, bg); + cls(); +} +void cls(void) { + unsigned short blank = vga_blank_entry(); + int i; + for (i=0; i= VGA_HEIGHT*VGA_WIDTH) { + scroll(); + } + draw_cursor(); +} + +void vga_write(const char* text) { + int i = 0; + while(text[i]) { + putchar(text[i]); + i++; + } +} +void vga_write_color( const char* text, enum vga_color fg, enum vga_color bg) { + unsigned char prevfg = fgcolor; + unsigned char prevbg = bgcolor; + vga_set_color(fg, bg); + vga_write(text); + fgcolor = prevfg; + bgcolor = prevbg; +} +void vga_write_line(const char* text) { + if (cursor_loc != CURSOR_HOME) { + scroll(); + } + vga_write(text); + scroll(); +} +void vga_write_line_color(const char* text, enum vga_color fg, enum vga_color bg) { + unsigned char prevfg = fgcolor; + unsigned char prevbg = bgcolor; + vga_set_color(fg, bg); + vga_write_line(text); + fgcolor = prevfg; + bgcolor = prevbg; +} diff --git a/vga.h b/vga.h new file mode 100644 index 0000000..60c0666 --- /dev/null +++ b/vga.h @@ -0,0 +1,20 @@ + +enum vga_color { + VGA_COLOR_BLACK = 0, + VGA_COLOR_BLUE = 1, + VGA_COLOR_GREEN = 2, + VGA_COLOR_CYAN = 3, + VGA_COLOR_RED = 4, + VGA_COLOR_MAGENTA = 5, + VGA_COLOR_ORANGE = 6, + VGA_COLOR_GREY = 7, + VGA_COLOR_GRAY = 7, +}; + +void cls(void); +void vga_init(enum vga_color fg, enum vga_color bg); + +void vga_write(const char* text); +void vga_write_color( const char* text, enum vga_color fg, enum vga_color bg); +void vga_write_line(const char* text); +void vga_write_line_color( const char* text, enum vga_color fg, enum vga_color bg);