From d80b41ebda2c41d382e16e9b520606e45a8c95e6 Mon Sep 17 00:00:00 2001 From: Tom-on64 Date: Thu, 28 Dec 2023 21:09:59 +0100 Subject: [PATCH] Tried adding file saving/loading --- src/filetable.asm | 2 +- src/fs/edit.asm | 197 ++++++++++++++++++++++++++++++++++++++++------ src/kernel.asm | 10 ++- src/lib/disk.asm | 62 +++++++-------- 4 files changed, 213 insertions(+), 58 deletions(-) diff --git a/src/filetable.asm b/src/filetable.asm index b83d9a4..800cd3a 100644 --- a/src/filetable.asm +++ b/src/filetable.asm @@ -17,6 +17,6 @@ filetable: db 'filetable',0, 'txt',0,2,1 ; filetable.txt - start: 2, size: 1 db 'kernel',0,0,0,0, 'bin',0,3,4 ; kernel.bin - start: 3, size: 4 db 'calc',0,0,0,0,0,0, 'bin',0,7,1 ; calc.bin - start: 7, size: 1 - db 'edit',0,0,0,0,0,0, 'bin',0,8,3 ; edit.bin - start: 8, size: 2 + db 'edit',0,0,0,0,0,0, 'bin',0,8,4 ; edit.bin - start: 8, size: 2 times 512-($-$$) db 0 diff --git a/src/fs/edit.asm b/src/fs/edit.asm index 2d8244a..4badddb 100644 --- a/src/fs/edit.asm +++ b/src/fs/edit.asm @@ -1,11 +1,14 @@ [bits 16] -launch: +start: + mov byte [driveNum], dl + +hexEditor: pusha call clearText mov di, 0x0e60 ; Offset in vidmem (2nd to last line) - mov si, statusBarMsg + mov si, hexModeMsg call drawString mov di, 0x0f00 ; Last line mov si, controlsMsg @@ -16,10 +19,14 @@ launch: getHexCh: xor ax, ax int 0x16 - cmp al, '$' ; Check for end of input (Denoted by a "$") + cmp al, RUN_CODE je .runInput - cmp al, '!' ; End editor, Exit back to kernel + cmp al, QUIT_EDIT je return + cmp al, SAVE_FILE + je save + cmp al, OPEN_FILE + je open cmp al, 0x08 ; Backspace je .backspace @@ -57,7 +64,7 @@ getHexCh: je getHexCh ; If not, ignore mov byte [di], 0 dec di - mov si, backChars + mov si, hexBackspace call print jmp getHexCh .runInput: @@ -72,16 +79,7 @@ getHexCh: call print xor ax, ax int 0x16 - jmp launch - -; Expects al to contain an ASCII character, will set bx to it's hex equivalent -asciiToHex: - cmp al, '9' - jle .getHex - sub al, 7 -.getHex: - sub al, '0' - ret + jmp hexEditor return: mov cx, 256 @@ -104,24 +102,179 @@ return: mov gs, ax jmp 0x2000:0x0000 +; Saves a file +save: + mov dl, 9 + mov si, saveFileMsg + call getFilename + mov byte al, [filename] + cmp byte al, 0 + je getHexCh + + ; Call saveFile + mov byte dl, [driveNum] + push word filename + ; TODO: More filetypes other than .bin + push word binFile + push word 1 ; TODO: Dynamic filesize + push word 0x8000 ; Program location (this segment) + push word hexCode + call saveFile ; Returns exit code in al + add sp, 8 ; Stack bs + + cmp al, 0 ; Won't be 1 (File not found) so must be disk error + jne .error + + jmp getHexCh +.error: + mov si, diskErrMsg + mov di, 0x0f00 ; Last line + call drawString + jmp getHexCh + +; Opens a file +open: + mov dl, 11 + mov si, openFileMsg + call getFilename + mov byte al, [filename] + cmp byte al, 0 + je getHexCh + + ; Call loadFile + ; TODO: The same stuff as in save + mov byte dl, [driveNum] + push word filename + push word 0x8000 + push word hexCode + call loadFile + + add sp, 6 ; Stack bs + + cmp al, 1 + je .notFound + cmp al, 2 + je .diskErr + + jmp getHexCh +.notFound: + mov si, notFoundMsg + jmp .error +.diskErr: + mov si, diskErrMsg +.error: + mov di, 0x0f00 + call drawString + jmp getHexCh + +; +; Gets a filename (10 chars, If less, padded with Nulls) +; Outputs the filename in the filename buffer +; si - Prompt string (Null terminated) +; dl - String offset +; +getFilename: + pusha + + mov di, 0x0f00 ; Last line + call drawString ; si should contain the string + + mov ah, 2 ; Move cursor to input position + mov dh, 24 ; Last line + int 0x10 + + mov di, filename + xor cx, cx ; Entered filename length +.loop: + xor ax, ax + int 0x16 + + cmp al, 0xD ; User pressed enter + je .done + cmp al, 8 ; Backspace + je .backspace + cmp al, 27 ; Escape + je .escape + cmp cx, 10 ; Filename too long + je .loop + + mov ah, 0x0e + int 0x10 + mov [di], al + inc di + inc cx + jmp .loop +.backspace: + cmp cx, 0 + je .loop + dec cx + dec di + mov si, textBackspace + call print + jmp .loop +.escape: + xor cx, cx + mov si, filename +.done: + cmp cx, 10 + je .after + mov byte [di], 0 + inc cx + inc di +.after: + mov ah, 2 ; Move cursor + xor dx, dx ; 0,0 + int 0x10 + + popa + ret + +; Expects al to contain an ASCII character, will set bx to it's hex equivalent +asciiToHex: + cmp al, '9' + jle .getHex + sub al, 7 +.getHex: + sub al, '0' + ret + ;; Includes %include "./src/lib/print.asm" %include "./src/lib/disk.asm" ;; Variables -statusBarMsg: db " -- HEX -- ", 0 -controlsMsg: db "'$' to execute code; '!' to quit; 's' to save", 0 -fileChoiceMsg: db "'n' for new file; 'o' to open a file", 0 -chooseModeMsg: db "'x' for Hex mode; 't' for Text mode", 0 +hexModeMsg: db " -- HEX -- ", 0 +textModeMsg: db " -- Text -- ", 0 +controlsMsg: db "'$' Execute; ^Q Quit; ^S Save; ^O Open", 0 +saveFileMsg: db "Save as: ", 0 ; Make sure to erase the controls +openFileMsg: db "Open file: ", 0 runMsg: db ENDL, ENDL, 0 ranMsg: db ENDL, ENDL, " - Finished Execution - ", ENDL, 0 +notFoundMsg: db "File not found! ", 0 ; Erase the user input +diskErrMsg: db "Disk Error! ", 0 -backChars: db 8,8,8,' ',8,8,0 +; Filetypes +binFile: db "bin" +txtFile: db "txt" +hexBackspace: db 8,8,8,' ',8,8,0 +textBackspace: db 8,' ',8,0 + +driveNum: db 0 hexByte: db 0 +filenameLen: db 0 +filename: times 10 db 0 codeLen: db 0 -hexCode: times 256 db 0 ; Code buffer - ret + +;; Constants +RUN_CODE equ '$' +QUIT_EDIT equ 'q' +NEW_FILE equ 'n' +SAVE_FILE equ 's' +OPEN_FILE equ 'o' times 3*512-($-$$) db 0 + +hexCode: times 511 db 0 ; Code buffer + ret diff --git a/src/kernel.asm b/src/kernel.asm index 7c68e39..ad4daa6 100644 --- a/src/kernel.asm +++ b/src/kernel.asm @@ -2,7 +2,7 @@ start: ; Save the drive number from stack - mov byte [driveNumber], dl + mov byte [driveNum], dl mov bx, 0x2000 mov es, bx @@ -170,7 +170,7 @@ foundProgram: push ax ; Save file size mov ah, 0 ; Reset disk system - mov dl, byte [driveNumber] + mov dl, byte [driveNum] int 0x13 mov bx, PROGRAM_LOC @@ -178,7 +178,7 @@ foundProgram: xor bx, bx ; [es:bx] - Place to load out program mov ah, 2 ; Read sectors into memory - mov dl, byte [driveNumber] + mov dl, byte [driveNum] mov ch, 0 ; Cylinder mov dh, 0 ; Head int 0x13 @@ -200,6 +200,8 @@ foundProgram: repe cmpsb jne .printText ; TODO: Check for more file extensions + mov dl, [driveNum] + mov ax, PROGRAM_LOC mov ds, ax mov es, ax @@ -407,7 +409,7 @@ reboot: jmp 0xffff:0x0000 %include "./src/lib/screen.asm" ;; Variables -driveNumber: db 0 +driveNum: db 0 startMsg: db "kern.", ENDL, ENDL, 0 ; The bootup message prompt: db "> ", 0 ; Prompt for input diff --git a/src/lib/disk.asm b/src/lib/disk.asm index 59864a0..84ddb43 100644 --- a/src/lib/disk.asm +++ b/src/lib/disk.asm @@ -1,11 +1,10 @@ ; ; Loads a file from the filetable into a memory location -; dl - Disk number ; Params: +; dl - Disk number ; Filename (Null terminated) ; Destination location segment ; Destination location offset -; Disk number ; ; Returns: ; al - Exit code : 0 - Success; 1 - File not found; 2 - Disk error @@ -16,13 +15,13 @@ loadFile: mov bp, sp pusha - mov byte [driveNum], dl + mov byte [diskNum], dl mov bx, [bp+4] ; dest Offset mov fs, [bp+6] ; dest Segment mov si, [bp+8] ; Filename mov cx, 10 - mov di, filename + mov di, fileName .getFilename: lodsb ; mov byte al, [si]; inc si cmp al, 0 ; End of filename @@ -33,8 +32,8 @@ loadFile: mov ax, FILETAB_LOC mov es, ax xor di, di ; es:di - Filetable location - mov si, filename -.filenameCheck: + mov si, fileName +.fileNameCheck: mov al, [di] cmp al, 0 ; End of filetable je .notFound @@ -43,7 +42,7 @@ loadFile: je .beginCmp add di, 16 - jmp .filenameCheck + jmp .fileNameCheck .beginCmp: push di mov cl, 10 @@ -58,10 +57,10 @@ loadFile: inc si jmp .cmpLoop .redoCheck: - mov si, filename + mov si, fileName pop di inc di - jmp .filenameCheck + jmp .fileNameCheck .notFound: mov byte [exitCode], NOT_FOUND jmp .done @@ -75,7 +74,7 @@ loadFile: inc di ; Skip Number of entries xor ax, ax ; Reset disk - mov dl, [driveNum] + mov dl, [diskNum] int 0x13 push fs @@ -96,6 +95,7 @@ loadFile: popa mov sp, bp pop bp + mov byte al, [exitCode] ret ; @@ -117,11 +117,11 @@ saveFile: mov bp, sp pusha - mov byte [driveNum], dl - pop bx ; src Offset - pop fs ; src Segment - pop cx ; Filesize - pop ax ; Filetype + mov byte [diskNum], dl + mov bx, [bp+4] ; src Offset + mov fs, [bp+6] ; src Segment + mov cx, [bp+8] ; Filesize + mov ax, [bp+10] ; Filetype push bx @@ -131,17 +131,17 @@ saveFile: mov cx, 3 .filetype: lodsb - cmp al, 0 ; End of filename + cmp al, 0 ; End of fileName dec si ; Fill the rest with Null stosb loop .getFilename - pop si ; Filename + mov si, [bp+12] ; Filename mov cx, 10 - mov di, filename + mov di, fileName .getFilename: lodsb ; mov byte al, [si]; inc si - cmp al, 0 ; End of filename + cmp al, 0 ; End of fileName dec si ; Fill the rest with Null stosb loop .getFilename @@ -149,8 +149,8 @@ saveFile: mov ax, FILETAB_LOC mov es, ax xor di, di ; es:di - Filetable location - mov si, filename -.filenameCheck: + mov si, fileName +.fileNameCheck: mov byte al, [es:di+14] mov byte [availableSector], al mov byte al, [es:di+15] @@ -164,7 +164,7 @@ saveFile: je .beginCmp add di, 16 - jmp .filenameCheck + jmp .fileNameCheck .beginCmp: push di mov cl, 10 @@ -179,10 +179,10 @@ saveFile: inc si jmp .cmpLoop .redoCheck: - mov si, filename + mov si, fileName pop di add di, 16 - jmp .filenameCheck + jmp .fileNameCheck .found: ; File already exists (override) ; TODO: Ask user if they want to override file mov al, [es:di] @@ -194,7 +194,7 @@ saveFile: inc di ; Skip Number of entries xor ax, ax ; Reset disk - mov dl, [driveNum] + mov dl, [diskNum] int 0x13 ; TODO: Override current file @@ -215,8 +215,8 @@ saveFile: createFile: ; Appends a file to the filetable. mov cx, 10 - mov si, filename - rep movsb ; Append the filename + mov si, fileName + rep movsb ; Append the fileName mov cx, 3 mov si, filetype rep movsb ; Append the extension @@ -229,7 +229,7 @@ createFile: ; Appends a file to the filetable. ; Write changed filetable to disk ;! If filetable size changes, this will have to change (probably) xor bx, bx ; es:bx - Filetable location - mov dl, [driveNum] + mov dl, [diskNum] mov ah, 3 ; Write to disk mov al, 1 ; Write 1 sector mov ch, 0 ; Cylinder @@ -248,7 +248,7 @@ writeFileData: ; Writes file data to the disk pop bx ; es:bx - Source mov ah, 3 ; Write to disk mov byte al, [filesize] - mov dl, [driveNum] + mov dl, [diskNum] mov ch, 0 ; Cylinder mov dh, 0 ; Head mov byte cl, [availableSector] @@ -270,10 +270,10 @@ returnToCaller: availableSector: db 0 exitCode: db 0 -driveNum: db 0 +diskNum: db 0 filesize: db 0 filetype: times 3 db 0 -filename: times 10 db 0 +fileName: times 10 db 0 ;; Constants FILETAB_LOC equ 0x1000