commit a346d668797d9eff20015d3af627edbc390ff121 Author: Tom-on64 Date: Thu Dec 21 22:13:56 2023 +0100 Initial Commit diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..43b5eaa --- /dev/null +++ b/build.sh @@ -0,0 +1,13 @@ +# Bootloader +nasm -fbin src/boot.asm -o build/boot.bin +nasm -fbin src/filetable.asm -o build/filetable.bin +cat build/main.bin build/filetable.bin > build/bootloader.bin + +# Kernel +nasm -fbin src/kernel.asm -o build/kernel.bin + +# Final binary +cat build/bootloader.bin build/kernel.bin > build/os.bin + +# Run QEMU +qemu-system-x86_64 -drive format=raw,file="./build/os.bin",index=0,media=disk -m 256M diff --git a/build/boot.bin b/build/boot.bin new file mode 100644 index 0000000..05aa27c Binary files /dev/null and b/build/boot.bin differ diff --git a/build/bootloader.bin b/build/bootloader.bin new file mode 100644 index 0000000..84b1ac7 Binary files /dev/null and b/build/bootloader.bin differ diff --git a/build/filetable.bin b/build/filetable.bin new file mode 100644 index 0000000..1688bea Binary files /dev/null and b/build/filetable.bin differ diff --git a/build/kernel.bin b/build/kernel.bin new file mode 100644 index 0000000..a9c02b9 Binary files /dev/null and b/build/kernel.bin differ diff --git a/build/main.bin b/build/main.bin new file mode 100644 index 0000000..2985e5a Binary files /dev/null and b/build/main.bin differ diff --git a/build/os.bin b/build/os.bin new file mode 100644 index 0000000..9c1f7d4 Binary files /dev/null and b/build/os.bin differ diff --git a/clean.sh b/clean.sh new file mode 100644 index 0000000..8ea9fbd --- /dev/null +++ b/clean.sh @@ -0,0 +1 @@ +rm -rf build/* \ No newline at end of file diff --git a/src/boot.asm b/src/boot.asm new file mode 100644 index 0000000..5943c7f --- /dev/null +++ b/src/boot.asm @@ -0,0 +1,60 @@ +[org 0x7c00] +[bits 16] +[global start] + +start: + ; Setup + xor ax, ax + mov es, ax + mov ds, ax + mov bp, 0x9000 + mov sp, bp + + ; Read filetable from disk + mov bx, FILETAB_LOC ; Filetable location in memory + mov es, bx + xor bx, bx + + mov ah, 2 ; Read from disk + mov al, 1 ; Read 1 sector + mov ch, 0 ; Cylinder + mov dh, 0 ; Head + mov cl, 2 ; Sector + int 0x13 ; INT 0x13 - Disk operations + + jc readFail ; CF will be set if the read fails + + ; Read filetable from disk + mov bx, KERNEL_LOC ; Filetable location in memory + mov es, bx + xor bx, bx + + mov ah, 2 ; Read from disk + mov al, 2 ; Read 2 sectors + mov ch, 0 ; Cylinder + mov dh, 0 ; Head + mov cl, 3 ; Sector + int 0x13 ; INT 0x13 - Disk operations + + jc readFail ; CF will be set if the read fails + + ; Setup segmentation + mov ax, KERNEL_LOC + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + + ; Far jump to the kernel + jmp KERNEL_LOC:0x0 + +readFail: + cli + hlt + +FILETAB_LOC equ 0x1000 +KERNEL_LOC equ 0x2000 + +times 510-($-$$) db 0 +dw 0xaa55 diff --git a/src/disk.asm b/src/disk.asm new file mode 100644 index 0000000..e69de29 diff --git a/src/filetable.asm b/src/filetable.asm new file mode 100644 index 0000000..9c2b7e9 --- /dev/null +++ b/src/filetable.asm @@ -0,0 +1,8 @@ +; Simple Filetable +db "{" ; Start +db "kernel.bin:03," +db "calc:05," +db "coolThing:07" +db "}" ; End + +times 512-($-$$) db 0 diff --git a/src/kernel.asm b/src/kernel.asm new file mode 100644 index 0000000..0447668 --- /dev/null +++ b/src/kernel.asm @@ -0,0 +1,185 @@ +[bits 16] + +%define ENDL 0x0a, 0x0d + +start: + call resetTextMode + + ; Print start message + mov si, startMsg + call print + +; Runs prompt +input: + mov si, prompt + call print + mov di, userInput + mov byte [inputLen], 0 +.loop: + xor ax, ax ; Clear ax + int 0x16 ; Get a character + mov ah, 0x0e ; Echo back input + mov bh, 0 + int 0x10 + + cmp al, 0x0d ; Compare with CR + je .enter + + mov [di], al ; Add the input to userInput + inc di + jmp .loop +.enter: + mov al, 0x0a ; Print LF + int 0x10 + mov byte [di], 0; NULL terminate userInput (Replaces CR) + + mov al, [userInput] + cmp al, 'H' + je help + cmp al, 'F' + je fileBrowser + cmp al, 'G' + je graphicsTest + cmp al, 'R' + je reboot +; +; Load Program - Takes userInput and tries to run that file +; +loadProgram: + mov ax, userInput + ; Get filetable into [es:di] + mov bx, FILETAB_LOC + mov es, bx + xor bx, bx + mov di, bx + + repne scasb ; Scan for string at ax, in string at di + je .found + + mov si, notFoundMsg + call print + + jmp input +.found: + + jmp input + +; +; Help - Prints the help message (helpMsg) +; +help: + mov si, helpMsg + call print + jmp input + +; +; File Browser +; +fileBrowser: + mov si, browserMsg + call print + +; Prints the filetable +printFiletable: + mov bx, FILETAB_LOC + mov es, bx + xor bx, bx + xor cx, cx +.filetableLoop: + inc bx + mov al, [es:bx] ; Get the filetable character + cmp al, '}' ; Check for filetable end + je .done + cmp al, ':' ; Check for sector number + je .sectorLoop + cmp al, ',' ; Check for another entry + je .entryLoop + inc cx + int 0x10 + jmp .filetableLoop +.sectorLoop: + cmp cx, 20 + je .filetableLoop + mov al, ' ' + int 0x10 + inc cx + jmp .sectorLoop +.entryLoop: + xor cx, cx + mov si, newline + call print + jmp .filetableLoop +.done: + mov si, newline + call print + jmp input + +; +; Graphics Mode Test +; +graphicsTest: + call resetGraphicsMode + + ; Test square + mov ah, 0x0c ; Write gfx pixel + mov al, 0x02 ; Green + mov bh, 0 ; Page number + + mov cx, 100 ; Column number + mov dx, 100 ; Row number + int 0x10 +.colLoop: + inc cx + int 0x10 + cmp cx, 150 + jne .colLoop + + inc dx + int 0x10 + mov cx, 99 + cmp dx, 150 + jne .colLoop +.done: + mov si, startMsg + call print + + xor ax, ax + int 0x16 + + call resetTextMode + jmp input + +; +; Reboots the OS +; +reboot: jmp 0xffff:0x0000 + +; ---------- +; Includes +; ---------- +%include "./src/print.asm" +%include "./src/screen.asm" + +; -------- +; Data +; -------- +startMsg: db "kern.", ENDL, ENDL, 0 ; The bootup message +prompt: db "> ", 0 ; Prompt for input +newline: db ENDL, 0 ; CRLF +notFoundMsg: db "File not found!", ENDL, 0 + +browserMsg: db " Filename Sector ", ENDL, \ + "---------- --------", ENDL, 0 + +helpMsg: db "Available Commands:", ENDL, \ + "F - List all files", ENDL, \ + "G - Graphics mode test", ENDL, \ + "H - Help", ENDL, \ + "R - Reboot", ENDL, 0 + +userInput: db 0 +inputLen: db 0 + +FILETAB_LOC equ 0x1000 + +times 2*512-($-$$) db 0 diff --git a/src/print.asm b/src/print.asm new file mode 100644 index 0000000..b399030 --- /dev/null +++ b/src/print.asm @@ -0,0 +1,49 @@ +; +; Prints a string +; si - Pointer to NULL terminated string +; +print: + pusha + mov ah, 0x0e + mov bl, 0x0f + mov bh, 0 +.loop: + lodsb ; Load [si] into al; Increment si + or al, al + jz .done + int 0x10 + jmp .loop +.done: + popa + ret + +; +; Prints a hex value +; dx - Value to print in Hex +; +printHex: + pusha + xor cx, cx ; Init counter +.loop: + cmp cx, 4 ; Check for loop end + je .done + mov ax, dx ; Copy dx to ax + and ax, 0x000f ; Get last nibble (4 bits) of ax + add ax, '0' ; Convert number to an ASCII number (0-9) + cmp ax, '9' ; Check if number was less than 9, meaning we can use it + jle .addChar + add ax, 0x07 ; Convert to letter (A-F) +.addChar: + mov bx, hexString + 5 + sub bx, cx ; Get position of current hex character + mov [bx], al + ror dx, 4 ; Rotate bits of dx (0x1a2b -> 0xb1a2) + inc cx ; Increment the counter + jmp .loop +.done: + mov si, hexString + call print + popa + ret + +hexString: db "0x0000", 0 diff --git a/src/screen.asm b/src/screen.asm new file mode 100644 index 0000000..14f2c9d --- /dev/null +++ b/src/screen.asm @@ -0,0 +1,24 @@ +resetTextMode: + pusha + + mov ah, 0 ; Set mode + mov al, 0x03 ; 25x80, Text mode + int 0x10 + + mov ah, 0x0b ; Change color/palette + mov bh, 0x00 ; Change BG color + mov bl, 0x01 ; Blue BG + int 0x10 + + popa + ret + +resetGraphicsMode: + pusha + + mov ah, 0 ; Set mode + mov al, 0x13 ; 320x200, 256 color graphics mode + int 0x10 + + popa + ret