From 3ab03703f40bac9f5ffe22e4f8d2451bd79b65f0 Mon Sep 17 00:00:00 2001 From: julianb0 Date: Sat, 13 Jul 2019 12:38:03 +0200 Subject: [PATCH] cmd --- as/k-as.py | 11 +++-- ka/command.k | 4 +- ka/crt/limits.k | 1 + ka/crt/sys.k | 47 +++++++++++++++++--- ka/sys/intr/trap0.k | 22 +++++++++- ka/usr/cmd/main.k | 104 ++++++++++++++++++++++++++++++++++++-------- vm/dv/DISKDEV | 2 +- 7 files changed, 159 insertions(+), 32 deletions(-) diff --git a/as/k-as.py b/as/k-as.py index 854d5c0..321773a 100755 --- a/as/k-as.py +++ b/as/k-as.py @@ -332,13 +332,18 @@ def parse_preproc(line): leave() sys.exit(1) - written = b_data.write(ord(c).to_bytes(1, byteorder='little', signed=False)) + b_data.write(ord(c).to_bytes(1, byteorder='little', signed=False)) real_len += 1 pdata += 1 + b_data.write(int(0).to_bytes(1, byteorder='little', signed=False)) + pdata += 1 + + l = real_len + 1 # s + null-term + # align - if (len(s) % 8) != 0: - for i in range(8 - len(s) % 8): + if (l % 8) != 0: + for i in range(8 - l % 8): written = b_data.write(int(0).to_bytes(1, byteorder='little', signed=False)) pdata += 1 diff --git a/ka/command.k b/ka/command.k index f67c10c..8116730 100644 --- a/ka/command.k +++ b/ka/command.k @@ -6,6 +6,8 @@ __cmdstart: include "crt/crt.k" +FILE_LOADP := 0x141000 ; 4KB above stack + start: mov rsp, 0x140000 xor rbp, rbp @@ -14,7 +16,7 @@ start: cld call main - mov rax, Sys.HLT + mov rax, Sys.EnterHaltMode trap 0 crash diff --git a/ka/crt/limits.k b/ka/crt/limits.k index c20c132..1ef0edb 100644 --- a/ka/crt/limits.k +++ b/ka/crt/limits.k @@ -22,3 +22,4 @@ QWORD_MAX := 0xFFFFFFFFFFFFFFFF STRLEN_MAX := 0xFFFFFFFF +FILE_MAXSZ := 0x8000 ; 512KB diff --git a/ka/crt/sys.k b/ka/crt/sys.k index e331de4..25d08ef 100644 --- a/ka/crt/sys.k +++ b/ka/crt/sys.k @@ -1,6 +1,20 @@ ; The OS/K Team licenses this file to you under the MIT license. ; See the LICENSE file in the project root for more information. +; +; EXIT syscall +; +; Return to COMMAND.COM +; +Sys.Exit := 0x00 + +; +; SHUTDOWN syscall +; +; End virtual machine +; +Sys.Shutdown := 0x01 + ; FIND syscalls ; ; Find file on disk @@ -18,19 +32,38 @@ Sys.FindFirst := 0x20 Sys.FindNext := 0x21 ; -; EXIT syscall +; OPEN/CREATE syscalls ; -; Return to COMMAND.COM +; IN +; ax0 = name string ; -Sys.Exit := 0x00 +; OUT +; rax = handle of file, or <0 if couldn't open +; +Sys.OpenFile := 0x30 +Sys.CreateFile := 0x31 ; -; SHUTDOWN syscall +; CLOSE syscall ; -; End virtual machine +; IN +; ax0 = file handle ; -Sys.Shutdown := 0x01 +Sys.CloseFile := 0x35 + +; +; READ syscall +; +; IN +; ax0 = file handle +; ax1 = buffer address +; ax2 = buffer size +; +; OUT +; rax = number of bytes read, <0 on error +; +Sys.ReadFile := 0x38 ; Halt mode -Sys.HLT := 0x99 +Sys.EnterHaltMode := 0x999 diff --git a/ka/sys/intr/trap0.k b/ka/sys/intr/trap0.k index f98dec5..cd8a751 100644 --- a/ka/sys/intr/trap0.k +++ b/ka/sys/intr/trap0.k @@ -14,11 +14,15 @@ trap0_handler: call RFS.LoadReg call RFS.LoadArgs - b.z rax, Sys.HLT, .handle_HLT b.z rax, Sys.Exit, .handle_Exit b.z rax, Sys.Shutdown, .handle_Shutdown b.z rax, Sys.FindNext, .handle_FindNext b.z rax, Sys.FindFirst, .handle_FindFirst + b.z rax, Sys.OpenFile, .handle_OpenFile + b.z rax, Sys.CloseFile, .handle_CloseFile + b.z rax, Sys.ReadFile, .handle_ReadFile + b.z rax, Sys.EnterHaltMode, .handle_HaltMode + mov rax, 1 << 63 .fini: jmp TrapHandlers.epilog @@ -105,13 +109,27 @@ trap0_handler: call DISK.FindNext jmp .fini +.handle_OpenFile: + add ax0, nx0 + call DISK.OpenFile + jmp .fini + +.handle_CloseFile: + call DISK.CloseFile + jmp .fini + +.handle_ReadFile: + add ax1, nx0 + call DISK.ReadFile + jmp .fini + ; ; Misc. ; .handle_Shutdown: stop -.handle_HLT: +.handle_HaltMode: hlt .HLT.loop: xpause diff --git a/ka/usr/cmd/main.k b/ka/usr/cmd/main.k index cd8fe82..b2b0fb2 100644 --- a/ka/usr/cmd/main.k +++ b/ka/usr/cmd/main.k @@ -25,6 +25,8 @@ main: ; iterator through argbuf xor rcx, rcx + mov q[argv1pos], 0 + .input_loop: pause pause @@ -163,23 +165,7 @@ main: call strcmp b.z rax, 0, .handle_VER - ; fallthrough - -.exec_prog: - - ; fallthrough - -.not_found: - mov rcx, STRLEN_MAX - mov rdx, argv0 - prns.rep.nz rdx - - mov rdx, .errmsg - prns.rep.nz rdx - - jmp .print_prompt - -.errmsg = " : command not found\n" + jmp .command_not_found ; ; call builtins @@ -201,7 +187,7 @@ main: mov ax0, .datefmt call printf - add rsp, 8*5 + add rsp, 5*8 jmp .print_prompt @@ -227,6 +213,31 @@ main: trap 0 .handle_PRINT: + mov rax, Sys.OpenFile + mov ax0, q[argv1pos] + b.z ax0, 0, .need_params + trap 0 + + b.l rax, 0, .file_not_found + + mov ax0, rax + mov ax1, FILE_LOADP + mov ax2, FILE_MAXSZ + + mov rax, Sys.ReadFile + trap 0 + + mov rcx, rax + + mov rax, Sys.CloseFile + trap 0 + + b.l rax, 0, .couldnt_read + b.z rax, 0, .empty_file + + mov rdx, FILE_LOADP + prns.rep rdx + jmp .print_prompt .handle_TIME: @@ -289,3 +300,60 @@ main: .helpmsg.time = " TIME Display current time of day\n" .helpmsg.ver = " VER Display current COMMAND.COM and DOS kernel versions\n" +.command_not_found: + mov rcx, STRLEN_MAX + mov rdx, argv0 + prns.rep.nz rdx + + mov rdx, .cnf_errmsg + prns.rep.nz rdx + + jmp .print_prompt + +.cnf_errmsg = ": command not found\n" + +.file_not_found: + mov ax0, .fnf_errmsg + push q[argv1pos] + push argv0 + call printf + add rsp, 16 + + jmp .print_prompt + +.fnf_errmsg = "%s: %s: file not found\n" + +.empty_file: + mov ax0, .ef_errmsg + push q[argv1pos] + push argv0 + call printf + add rsp, 16 + + jmp .print_prompt + +.ef_errmsg = "%s: %s: file is empty\n" + +.couldnt_read: + mov ax0, .cno_errmsg + push q[argv1pos] + push argv0 + call printf + add rsp, 16 + + jmp .print_prompt + +.cno_errmsg = "%s: %s: an error occured while reading file\n" + +.need_params: + mov rcx, STRLEN_MAX + mov rdx, argv0 + prns.rep.nz rdx + + mov rdx, .np_errmsg + prns.rep.nz rdx + + jmp .print_prompt + +.np_errmsg = ": need more parameters\n" + diff --git a/vm/dv/DISKDEV b/vm/dv/DISKDEV index df3b886..5cb415b 100644 --- a/vm/dv/DISKDEV +++ b/vm/dv/DISKDEV @@ -20,7 +20,7 @@ Disk device function slots: 18-21 - - - - (reserved) (reserved) - 25 p i - - open + 25 p - - - open - open file whose name is stored in string *ax0 - rax = -1 if couldn't open file, rax = file handle otherwise