diff --git a/.gitignore b/.gitignore index 7463555..ca2fca7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ isodir/ myos.iso *.o myos.bin +serial.log diff --git a/buildandrun.sh b/buildandrun.sh index 378a60b..0b2996c 100755 --- a/buildandrun.sh +++ b/buildandrun.sh @@ -2,4 +2,4 @@ make clean make ./builddisc.sh -qemu-system-i386 -cdrom myos.iso +qemu-system-i386 -cdrom myos.iso -serial file:serial.log diff --git a/kernel/arch/i386/io.nim b/kernel/arch/i386/io.nim index f72bc25..f5ba712 100644 --- a/kernel/arch/i386/io.nim +++ b/kernel/arch/i386/io.nim @@ -5,12 +5,14 @@ type {.push stackTrace:off.} proc outb*(data: IOPacket) = + let x = data.value + let y = data.port asm """ - outb `data.value` `data.port` + "outb %%al, %%dx" : :"a"(`x`), "d"(`y`) """ proc inb*(port: uint16): uint = asm """ - inb `port` `result` + "inb %%dx, %%al" :"=a"(`result`) :"d"(`port`) """ {.pop.} diff --git a/kernel/arch/i386/serial.nim b/kernel/arch/i386/serial.nim new file mode 100644 index 0000000..baa11d0 --- /dev/null +++ b/kernel/arch/i386/serial.nim @@ -0,0 +1,26 @@ +import io + +const portCOM1 = 0x3f8'u16 + +proc init*() = + io.outb((portCOM1 + 1, 0x00'u8)); # Disable all interrupts + io.outb((portCOM1 + 3, 0x80'u8)); # Enable DLAB (set baud rate divisor) + io.outb((portCOM1 + 0, 0x03'u8)); # Set divisor to 3 (lo byte) 38400 baud + io.outb((portCOM1 + 1, 0x00'u8)); # (hi byte) + io.outb((portCOM1 + 3, 0x03'u8)); # 8 bits, no parity, one stop bit + io.outb((portCOM1 + 2, 0xC7'u8)); # Enable FIFO, clear them, with 14-byte threshold + io.outb((portCOM1 + 4, 0x0B'u8)); # IRQs enabled, RTS/DSR set + +proc isTransitEmpty(): uint = + result = io.inb(portCOM1 + 5) and 0x20'u + +proc write*(input: char) = + while isTransitEmpty() == 0: + discard + io.outb((portCOM1, uint8(input))) + +proc write*(input: string) = + for i in 0 .. input.len - 1: + write(input[i]) + write('\L') + diff --git a/kernel/kernel.nim b/kernel/kernel.nim index 09a00fb..6e201b0 100644 --- a/kernel/kernel.nim +++ b/kernel/kernel.nim @@ -1,7 +1,10 @@ -import tty, gdt +import tty, gdt, irq, serial proc kernelMain() {.exportc: "kernel_main"}= + serial.init() + serial.write("Booting OS") gdt.gdtInstall() + serial.write("GDT installed") terminalInitialize() terminalWrite("Hello World!\L") terminalWrite("MAX_INT:") diff --git a/makefile b/makefile index 9416192..85af5cf 100644 --- a/makefile +++ b/makefile @@ -10,4 +10,4 @@ bootloader: nasm -felf32 kernel/arch/i386/boot.s -o boot.o clean: - rm -r myos.bin nimcache/ boot.o + rm -r myos.bin nimcache/ boot.o serial.log