diff --git a/apps/ed/glue.asm b/apps/ed/glue.asm new file mode 100644 index 0000000..568d6cf --- /dev/null +++ b/apps/ed/glue.asm @@ -0,0 +1,7 @@ +#include "user.h" +.org USER_CODE + + jp edMain + +#include "ed/main.asm" + diff --git a/apps/ed/main.asm b/apps/ed/main.asm new file mode 100644 index 0000000..6a7761c --- /dev/null +++ b/apps/ed/main.asm @@ -0,0 +1,62 @@ +; ed - line editor +; +; A text editor modeled after UNIX's ed, but simpler. The goal is to stay tight +; on resources and to avoid having to implement screen management code (that is, +; develop the machinery to have ncurses-like apps in Collapse OS). +; +; ed has a mechanism to avoid having to move a lot of memory around at each +; edit. Each line is an element in an doubly-linked list and each element point +; to an offset in the "scratchpad". The scratchpad starts with the file +; contents and every time we change or add a line, that line goes to the end of +; the scratch pad and linked lists are reorganized whenever lines are changed. +; Contents itself is always appended to the scratchpad. +; +; That's on a resourceful UNIX system. +; +; That doubly linked list on the z80 would use 7 bytes per line (prev, next, +; offset, len), which is a bit much. Moreover, there's that whole "scratchpad +; being loaded in memory" thing that's a bit iffy. We sacrifice speed for +; memory usage. +; +; So here's what we do. First, we have two scratchpads. The first one is the +; file being read itself. The second one is is memory, for modifications we +; make to the file. When reading the file, we note the offset at which it ends. +; All offsets under this limit refer to the first scratchpad. Other offsets +; refer to the second. +; +; Then, our line list if just an array of 16-bit offsets. This means that we +; don't have an easy access to line length and we have to move a lot of memory +; around whenever we add or delete lines. Hopefully, "LDIR" will be our friend +; here... +; +; *** Requirements *** +; printstr +; printcrlf +; stdioReadC +; stdioGetLine + +edMain: +edLoop: + ld hl, .prompt + call printstr +.inner: + call stdioReadC + jr nz, .inner ; not done? loop + ; We're done. Process line. + call printcrlf + call stdioGetLine + call edProcessLine + ret z + jr edLoop + +.prompt: + .db ":", 0 + +; Sets Z if we need to quit +edProcessLine: + call printstr + ld a, (hl) + cp 'q' + ret z + call printcrlf + jp unsetZ diff --git a/tools/emul/.gitignore b/tools/emul/.gitignore index b7ac10d..cc529d7 100644 --- a/tools/emul/.gitignore +++ b/tools/emul/.gitignore @@ -3,4 +3,5 @@ /runbin/runbin /*/*-bin.h /cfsin/zasm +/cfsin/ed /cfsin/user.h diff --git a/tools/emul/Makefile b/tools/emul/Makefile index f758068..c59d361 100644 --- a/tools/emul/Makefile +++ b/tools/emul/Makefile @@ -4,7 +4,7 @@ KERNEL = ../../kernel APPS = ../../apps ZASMBIN = zasm/zasm ZASMSH = ../zasm.sh -SHELLAPPS = $(addprefix cfsin/, zasm) +SHELLAPPS = $(addprefix cfsin/, zasm ed) CFSIN_CONTENTS = $(SHELLAPPS) cfsin/user.h .PHONY: all diff --git a/tools/emul/shell/shell_.asm b/tools/emul/shell/shell_.asm index c18d3ec..d10785c 100644 --- a/tools/emul/shell/shell_.asm +++ b/tools/emul/shell/shell_.asm @@ -34,6 +34,9 @@ jp _blkPutC jp _blkSeek jp _blkTell + jp printcrlf + jp stdioReadC + jp stdioGetLine #include "core.asm" #include "err.h" diff --git a/tools/emul/shell/user.h b/tools/emul/shell/user.h index 5ce72b9..792cb9e 100644 --- a/tools/emul/shell/user.h +++ b/tools/emul/shell/user.h @@ -4,26 +4,29 @@ .equ BLOCKDEV_SIZE 8 ; *** JUMP TABLE *** -.equ strncmp 0x03 -.equ addDE 0x06 -.equ addHL 0x09 -.equ upcase 0x0c -.equ unsetZ 0x0f -.equ intoDE 0x12 -.equ intoHL 0x15 -.equ writeHLinDE 0x18 -.equ findchar 0x1b -.equ parseHex 0x1e -.equ parseHexPair 0x21 -.equ blkSel 0x24 -.equ blkSet 0x27 -.equ fsFindFN 0x2a -.equ fsOpen 0x2d -.equ fsGetC 0x30 -.equ cpHLDE 0x33 -.equ parseArgs 0x36 -.equ printstr 0x39 -.equ _blkGetC 0x3c -.equ _blkPutC 0x3f -.equ _blkSeek 0x42 -.equ _blkTell 0x45 +.equ strncmp 0x03 +.equ addDE 0x06 +.equ addHL 0x09 +.equ upcase 0x0c +.equ unsetZ 0x0f +.equ intoDE 0x12 +.equ intoHL 0x15 +.equ writeHLinDE 0x18 +.equ findchar 0x1b +.equ parseHex 0x1e +.equ parseHexPair 0x21 +.equ blkSel 0x24 +.equ blkSet 0x27 +.equ fsFindFN 0x2a +.equ fsOpen 0x2d +.equ fsGetC 0x30 +.equ cpHLDE 0x33 +.equ parseArgs 0x36 +.equ printstr 0x39 +.equ _blkGetC 0x3c +.equ _blkPutC 0x3f +.equ _blkSeek 0x42 +.equ _blkTell 0x45 +.equ printcrlf 0x48 +.equ stdioReadC 0x4b +.equ stdioGetLine 0x4e