Printing stuff to screen

This commit is contained in:
Jarkko Toivanen 2023-06-13 09:45:04 +03:00
parent f30433dd27
commit aaffd2351f
Signed by: jt
GPG key ID: 9151B109B73ECAD5
5 changed files with 180 additions and 37 deletions

View file

@ -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

View file

@ -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...");
}

View file

@ -1,12 +1,6 @@
; Tutorial: A small kernel with Fasm & TCC
; By Tommy.
format elf
use32
format elf
use32
;
; Equates
;
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

97
vga.c Normal file
View file

@ -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;i++) {
*((unsigned short *) VGA_MEM_ADDR+i) = blank;
}
}
void scroll(void) {
unsigned short blank = vga_blank_entry();
int y;
int x;
*((unsigned short *) VGA_MEM_ADDR+cursor_loc) = blank;
for (y=0;y<VGA_HEIGHT;y++) {
for (x = 0;x<VGA_WIDTH;x++) {
*((unsigned short *) VGA_MEM_ADDR+y*VGA_WIDTH+x) = *((unsigned short *) VGA_MEM_ADDR+(y+1)*VGA_WIDTH+x);
}
}
for (x=0;x<VGA_WIDTH;x++) {
*((unsigned short *) VGA_MEM_ADDR+CURSOR_HOME+x) = blank;
}
cursor_loc = CURSOR_HOME;
draw_cursor();
}
void putchar(unsigned char chr) {
if (chr == '\n') {
scroll();
return;
}
*((unsigned char *) VGA_MEM_ADDR+cursor_loc * 2) = chr;
*((unsigned char *) VGA_MEM_ADDR+1+cursor_loc * 2) = vga_entry_color(fgcolor, bgcolor);
cursor_loc++;
if (cursor_loc >= 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;
}

20
vga.h Normal file
View file

@ -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);