From 3559d69b617aa68c5754981555f24e89080674f9 Mon Sep 17 00:00:00 2001 From: julianb0 Date: Mon, 5 Aug 2019 15:55:46 +0200 Subject: [PATCH] ka --- Makefile | 8 +++---- fs/empty.txt | 0 fs/{dir => test}/file | 0 ka/Makefile | 3 ++- ka/crt/fmt/printf.k | 1 + ka/crt/sys.k | 23 ++++++++++++++++++- ka/sys/drv/diskdev.k | 4 ++++ ka/sys/intr/trap0.k | 17 ++++++++++---- ka/usr/cmd/dir.k | 51 +++++++++++++++++++++++++++++------------- ka/usr/cmd/main.k | 52 ++++++++++++++++++++++++++++++++++++++----- vm/dv/diskdev.c | 38 ++++++++++++++++++++++++------- vm/pc/console.c | 4 ++-- 12 files changed, 159 insertions(+), 42 deletions(-) delete mode 100644 fs/empty.txt rename fs/{dir => test}/file (100%) diff --git a/Makefile b/Makefile index b6c1bf2..eff4fa9 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ # See the LICENSE file in the project root for more information. KODIR=fs +KSDIR=vm/ob KVVM=$(KODIR)/kvisc.exe @@ -16,12 +17,10 @@ kas: kpc as/regs.lst as/k-as.py dos: kas dosclean @cd ka && make --no-print-directory -s -#@as/k-as.py ka/dos.k 0x100000 $(AOUT) $(ASYM) - .PHONY: clean dosclean dosclean: - @rm -f $(KODIR)/*.com $(KODIR)/*.sym + @rm -f $(KODIR)/*.com $(KSDIR)/*.sym inclean: @rm -f vm/ob/arch_i.h vm/ob/instrs.lst vm/ob/in/*.o @@ -31,8 +30,7 @@ clean: dosclean @rm -f $(KVVM) as/instrs.lst test: dos - @$(KVVM) $(KODIR)/doskrnl.com $(KODIR)/doskrnl.sym - @rm -f $(AOUT) $(ASYM) + @$(KVVM) $(KODIR)/doskrnl.com $(KSDIR)/doskrnl.sym wc: clean @cat $(shell find -type f -name '*' ! -path './.git/*' | egrep '(ka|vm)/*') | wc -l diff --git a/fs/empty.txt b/fs/empty.txt deleted file mode 100644 index e69de29..0000000 diff --git a/fs/dir/file b/fs/test/file similarity index 100% rename from fs/dir/file rename to fs/test/file diff --git a/ka/Makefile b/ka/Makefile index 362cfca..4305ee5 100644 --- a/ka/Makefile +++ b/ka/Makefile @@ -4,12 +4,13 @@ all: dos KODIR=../fs +KSDIR=../vm/ob KSRC = $(shell ls *.k) KCOM = $(patsubst %.k,$(KODIR)/%.com,$(KSRC)) $(KODIR)/%.com: %.k - @../as/k-as.py $< 0x100000 $@ $(patsubst %.k,$(KODIR)/%.sym,$<) + @../as/k-as.py $< 0x100000 $@ $(patsubst %.k,$(KSDIR)/%.sym,$<) dos: $(KCOM) diff --git a/ka/crt/fmt/printf.k b/ka/crt/fmt/printf.k index d476ab6..9e76e01 100644 --- a/ka/crt/fmt/printf.k +++ b/ka/crt/fmt/printf.k @@ -30,6 +30,7 @@ nprintf: ; ; Print a string +; Guaranteed to only affect rcx and ax0 ; print: mov rcx, STRLEN_MAX diff --git a/ka/crt/sys.k b/ka/crt/sys.k index 614e000..0cfb4c6 100644 --- a/ka/crt/sys.k +++ b/ka/crt/sys.k @@ -32,7 +32,7 @@ Sys.FindFirst := 0x20 Sys.FindNext := 0x21 ; -; OPEN/CREATE syscalls +; OPEN syscall ; ; IN ; ax0 = name string @@ -41,8 +41,29 @@ Sys.FindNext := 0x21 ; rax = handle of file, or <0 if couldn't open ; Sys.OpenFile := 0x30 + +; +; CREATE syscall +; +; IN +; ax0 = name string +; +; OUT +; rax = 0 on success, or <0 if couldn't open +; Sys.CreateFile := 0x31 +; +; REMOVE syscall +; +; IN +; ax0 = name string +; +; OUT +; rax = 0 on success, or <0 if couldn't open +; +Sys.RemoveFile := 0x32 + ; ; CLOSE syscall ; diff --git a/ka/sys/drv/diskdev.k b/ka/sys/drv/diskdev.k index c6c6fb1..7deb013 100644 --- a/ka/sys/drv/diskdev.k +++ b/ka/sys/drv/diskdev.k @@ -25,6 +25,10 @@ DISK.CreateFile: iocall DISKDEV, 27 ret +DISK.RemoveFile: + iocall DISKDEV, 28 + ret + DISK.ReadFile: iocall DISKDEV, 32 ret diff --git a/ka/sys/intr/trap0.k b/ka/sys/intr/trap0.k index 4ff210d..ed8f393 100644 --- a/ka/sys/intr/trap0.k +++ b/ka/sys/intr/trap0.k @@ -14,14 +14,18 @@ trap0_handler: jmp.axz .handle_Shutdown b.z rax, Sys.Exit, .handle_Exit + b.z rax, Sys.ReadFile, .handle_ReadFile + b.z rax, Sys.OpenFile, .handle_OpenFile + b.z rax, Sys.CloseFile, .handle_CloseFile + b.z rax, Sys.CreateFile, .handle_CreateFile + b.z rax, Sys.RemoveFile, .handle_RemoveFile 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.CreateFile, .handle_CreateFile - 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 + mov rcx, 1 << 63 + mov rdx, 1 << 63 .fini.savecx: mov ax2, rcx @@ -108,6 +112,11 @@ trap0_handler: call DISK.CreateFile jmp .fini +.handle_RemoveFile: + inc ax0, r12 + call DISK.RemoveFile + jmp .fini + .handle_CloseFile: call DISK.CloseFile jmp .fini diff --git a/ka/usr/cmd/dir.k b/ka/usr/cmd/dir.k index 9d2e624..05dcae6 100644 --- a/ka/usr/cmd/dir.k +++ b/ka/usr/cmd/dir.k @@ -9,8 +9,12 @@ builtins.dir: push r12 push r13 + push r14 + push r15 - mov r12, zero # no. of files found + mov r12, zero # no. of files found + mov r13, zero # no. of directories found + mov r14, zero # total amount of bytes found call print, .dirmsg @@ -29,16 +33,17 @@ builtins.dir: trap 0 .list: - mov r13, rcx # file size - jmp.axz .end - ; found something - inc r12, 1 + mov r15, rcx # file size + inc r14, rcx ; directory? jmp.dxnz .is_dir + ; found a file + inc r12, 1 + ; separate extension from file name mov rcx, FNAME_MAX mov rsi, .buf @@ -89,10 +94,10 @@ builtins.dir: mov rcx, 3 prn.rep ' ' - shr rax, r13, 10 - and r13, r13, 1023 + shr rax, r15, 10 + and r15, r15, 1023 - push r13 + push r15 push rax call printf, .bytesstr inc rsp, 16 @@ -103,12 +108,24 @@ builtins.dir: jmp .next .end: + shr rax, r14, 10 + shr rdx, rax, 10 + and rax, rax, 1023 + and r14, r14, 1023 + + push r14 + push rax + push rdx + call printf, .endstr0 + inc rsp, 24 + + push r13 push r12 call printf, .endstr1 - inc rsp, 8 - - call print, .endstr2 + inc rsp, 16 + pop r15 + pop r14 pop r13 pop r12 @@ -117,24 +134,26 @@ builtins.dir: ; special case: direcory .is_dir: + inc r13, 1 + mov rcx, STRLEN_MAX mov rdx, .buf prns.rep.nz rdx sub rcx, STRLEN_MAX, rcx - b.ae rcx, N, .is_dir.print_ext sub rcx, N, rcx prn.rep ' ' -.is_dir.print_ext: call print, .dir_ext jmp .print_bytes .buf = [FNAME_MAX] .dir_ext = " " -.endstr1 = " %d file(s)\n" -.endstr2 = " 0 dir(s)\n" +.endstr0 = " total %dMB + %dKB + %dB\n" +.endstr1 = " found %d file(s), %d dir(s)\n" .dirmsg = "Directory of C:\\\n\n" -.bytesstr = "%d kilobytes + %d bytes" + +.bytesstr = "%d kilobytes + %d bytes" +# .bytesstr = "%dMB + %dKB + %dB" # too soon diff --git a/ka/usr/cmd/main.k b/ka/usr/cmd/main.k index d7f1a2d..cbcf8ab 100644 --- a/ka/usr/cmd/main.k +++ b/ka/usr/cmd/main.k @@ -146,6 +146,10 @@ main: call strcmp, argv0, .builtin_echo jmp.axz .handle_ECHO +.builtin_erase = "erase" + call strcmp, argv0, .builtin_erase + jmp.axz .handle_ERASE + .builtin_exit = "exit" call strcmp, argv0, .builtin_exit jmp.axz .handle_EXIT @@ -170,13 +174,17 @@ main: call strcmp, argv0, .builtin_prompt jmp.axz .handle_PROMPT +.builtin_remove = "remove" + call strcmp, argv0, .builtin_remove + jmp.axz .handle_REMOVE + .builtin_time = "time" call strcmp, argv0, .builtin_time jmp.axz .handle_TIME -.builtin_ver = "ver" - call strcmp, argv0, .builtin_ver - jmp.axz .handle_VER +.builtin_vers = "vers" + call strcmp, argv0, .builtin_vers + jmp.axz .handle_VERS jmp .command_not_found @@ -222,6 +230,16 @@ main: prn 10 jmp .print_prompt +.handle_ERASE: + mov rax, Sys.RemoveFile + mov ax0, q[argv1pos] + b.z ax0, zero, .need_params + trap 0 + + b.l rax, zero, .couldnt_remove + + jmp .handle_MAKE ; re-create it back + .handle_EXIT: mov rax, Sys.Shutdown trap 0 @@ -278,6 +296,16 @@ main: jmp .print_prompt +.handle_REMOVE: + mov rax, Sys.RemoveFile + mov ax0, q[argv1pos] + b.z ax0, zero, .need_params + trap 0 + + b.l rax, zero, .couldnt_remove + + jmp .print_prompt + .handle_TIME: call GetTimeUTC @@ -291,7 +319,7 @@ main: .timefmt = "%d:%d:%d\n" -.handle_VER: +.handle_VERS: call print, cmd.versionstr prn 10 @@ -304,12 +332,14 @@ main: call print, .helpmsg.dir call print, .helpmsg.dump call print, .helpmsg.echo + call print, .helpmsg.erase call print, .helpmsg.exit call print, .helpmsg.help call print, .helpmsg.halt call print, .helpmsg.make call print, .helpmsg.print call print, .helpmsg.prompt + call print, .helpmsg.remove call print, .helpmsg.time call print, .helpmsg.ver @@ -321,14 +351,16 @@ main: .helpmsg.dir = " DIR Print contents of current directory\n" .helpmsg.dump = " DUMP Toggles debug instruction dumping\n" .helpmsg.echo = " ECHO Write arguments to standard output\n" +.helpmsg.erase = " ERASE Empties a file, making it blank\n" .helpmsg.exit = " EXIT Initiate machine shutdown\n" .helpmsg.help = " HELP Display these messages\n" .helpmsg.halt = " HALT Put processor in halt mode\n" .helpmsg.make = " MAKE Create an empty file\n" .helpmsg.print = " PRINT Display contents of text file\n" .helpmsg.prompt = " PROMPT Change the command line prompt\n" +.helpmsg.remove = " REMOVE Deletes a file (permanently)\n" .helpmsg.time = " TIME Display current time of day\n" -.helpmsg.ver = " VER Display current COMMAND.COM version\n" +.helpmsg.ver = " VERS Display current COMMAND.COM version\n" .command_not_found: call print, argv0 @@ -367,6 +399,16 @@ main: .cno_errmsg = "%s: %s: an error occured while opening file\n" +.couldnt_remove: + push q[argv1pos] + push argv0 + call printf, .cne_errmsg + inc rsp, 16 + + jmp .print_prompt + +.cne_errmsg = "%s: %s: an error occured while removing file\n" + .couldnt_read: push q[argv1pos] push argv0 diff --git a/vm/dv/diskdev.c b/vm/dv/diskdev.c index d75bbcc..5fe7e44 100644 --- a/vm/dv/diskdev.c +++ b/vm/dv/diskdev.c @@ -112,6 +112,23 @@ long diskdev_open(dev_t *dev) return 0; } +long diskdev_close(dev_t *dev) +{ + GETDISK(); + + if (R(AX0) >= MAXOPEN) + return -1; + + if (disk->table[R(AX0)] <= 0) + return -1; + + close(disk->table[R(AX0)]); + + return 0; +} + +//----------------------------------------------------------------------------// + long diskdev_create(dev_t *dev) { int tmp; @@ -133,18 +150,22 @@ long diskdev_create(dev_t *dev) return 0; } -long diskdev_close(dev_t *dev) +long diskdev_remove(dev_t *dev) { - GETDISK(); + int tmp; + char buf[NAME_MAX+4] = { 'f', 's', '/', 0 }; - if (R(AX0) >= MAXOPEN) + readstr(R(AX0), NAME_MAX, buf+3); + + tmp = remove(buf); + + if (tmp < 0) + { + perror("diskdev: remove"); return -1; + } - if (disk->table[R(AX0)] <= 0) - return -1; - - close(disk->table[R(AX0)]); - + R(RAX) = 0; return 0; } @@ -201,6 +222,7 @@ long diskdev_poweron(dev_t *dev) dev->fslots[25] = diskdev_open; dev->fslots[26] = diskdev_close; dev->fslots[27] = diskdev_create; + dev->fslots[28] = diskdev_remove; dev->fslots[32] = diskdev_read; diff --git a/vm/pc/console.c b/vm/pc/console.c index 3419688..b227af2 100644 --- a/vm/pc/console.c +++ b/vm/pc/console.c @@ -13,8 +13,8 @@ # define _MULT 2 #endif -#define CONSOLE_WIDTH (80) -#define CONSOLE_HEIGHT (25) +#define CONSOLE_WIDTH (90) +#define CONSOLE_HEIGHT (30) #define CONSOLE_FONT_SIZE (16 * _MULT) #define SCREEN_WIDTH (9 * CONSOLE_WIDTH * _MULT) #define SCREEN_HEIGHT (16 * CONSOLE_HEIGHT * _MULT)