apps/ed: print specified line

This commit is contained in:
Virgil Dupras 2019-07-13 14:01:20 -04:00
parent 237d04fefd
commit e0f2a71dfc
5 changed files with 111 additions and 17 deletions

73
apps/ed/buf.asm Normal file
View File

@ -0,0 +1,73 @@
; buf - manage line buffer
;
; Lines in edited file aren't loaded in memory, their offsets is referenced to
; in this buffer.
;
; *** Consts ***
;
; Maximum number of lines allowed in the buffer.
.equ BUF_MAXLINES 0x800
; *** Variables ***
; Number of lines currently in the buffer
.equ BUF_LINECNT BUF_RAMSTART
; List of words pointing to scratchpad offsets
.equ BUF_LINES BUF_LINECNT+2
.equ BUF_RAMEND BUF_LINES+BUF_MAXLINES*2
; *** Code ***
bufInit:
xor a
ld (BUF_LINECNT), a
ret
; Add a new line with offset HL to the buffer
bufAddLine:
push de
push hl
ld hl, BUF_LINES
ld de, (BUF_LINECNT)
add hl, de
add hl, de ; twice, because two bytes per line
; HL now points to the specified line offset in memory
pop de ; what used to be in HL ends up in DE
; line offset popped back in HL
ld (hl), e
inc hl
ld (hl), d
; increase line count
ld hl, (BUF_LINECNT)
inc hl
ld (BUF_LINECNT), hl
; our initial HL is in DE. Before we pop DE back, let's swap these
; two so that all registers are preserved.
ex de, hl
pop de
ret
; Read line number specified in HL and loads the I/O buffer with it.
; Like ioGetLine, sets HL to line buffer pointer.
; Sets Z on success, unset if out of bounds.
bufGetLine:
push de
ld de, (BUF_LINECNT)
call cpHLDE
jr nc, .outOfBounds ; HL > (BUF_LINECNT)
ex de, hl
ld hl, BUF_LINES
add hl, de
add hl, de ; twice, because two bytes per line
; HL now points to seek offset in memory
ld e, (hl)
inc hl
ld d, (hl)
; DE has seek offset
ex de, hl
; and now HL has it. We're ready to call ioGetLine!
pop de
cp a ; ensure Z
jp ioGetLine ; preserves AF
.outOfBounds:
pop de
jp unsetZ

View File

@ -7,5 +7,7 @@
#include "lib/parse.asm" #include "lib/parse.asm"
.equ IO_RAMSTART USER_RAMSTART .equ IO_RAMSTART USER_RAMSTART
#include "ed/io.asm" #include "ed/io.asm"
.equ BUF_RAMSTART IO_RAMEND
#include "ed/buf.asm"
#include "ed/main.asm" #include "ed/main.asm"

View File

@ -40,27 +40,38 @@
; when combined with commands (p, c, d, a, i). All numbers in ed are ; when combined with commands (p, c, d, a, i). All numbers in ed are
; represented in decimals. ; represented in decimals.
; ;
; Like in ed, line indexing is one-based. This is only in the interface,
; however. In the code, line indexes are zero-based.
;
; *** Requirements *** ; *** Requirements ***
; BLOCKDEV_SIZE ; BLOCKDEV_SIZE
; addHL
; blkGetC ; blkGetC
; blkSeek ; blkSeek
; blkTell
; cpHLDE
; intoHL
; printstr ; printstr
; printcrlf ; printcrlf
; stdioReadC
; stdioGetLine ; stdioGetLine
; stdioPutC
; stdioReadC
; unsetZ ; unsetZ
edMain: edMain:
; Dummy test. Read first line of file ; Fill line buffer
ld hl, 0 .loop:
call blkTell ; --> HL
call blkGetC
jr nz, edLoop
call bufAddLine
call ioGetLine call ioGetLine
call printstr jr .loop
call printcrlf
; Continue to loop ; Continue to loop
edLoop: edLoop:
ld hl, .prompt ld a, ':'
call printstr call stdioPutC
.inner: .inner:
call stdioReadC call stdioReadC
jr nz, .inner ; not done? loop jr nz, .inner ; not done? loop
@ -71,9 +82,6 @@ edLoop:
ret z ret z
jr edLoop jr edLoop
.prompt:
.db ":", 0
; Sets Z if we need to quit ; Sets Z if we need to quit
.processLine: .processLine:
ld a, (hl) ld a, (hl)
@ -81,17 +89,24 @@ edLoop:
ret z ret z
call parseDecimal call parseDecimal
jr z, .processNumber jr z, .processNumber
; nothing jr .error
jr .processEnd
.processNumber: .processNumber:
; number is in IX ; number is in IX
; Because we don't have a line buffer yet, let's simply print seek ; Because we don't have a line buffer yet, let's simply print seek
; offsets. ; offsets.
push ix \ pop hl push ix \ pop hl
call ioGetLine dec hl ; from 1-based to zero-based
call bufGetLine
jr nz, .error
call printstr call printstr
call printcrlf call printcrlf
; continue to end ; continue to end
.processEnd: .processEnd:
call printcrlf call printcrlf
jp unsetZ jp unsetZ
.error:
ld a, '?'
call stdioPutC
call printcrlf
jp unsetZ

View File

@ -35,10 +35,12 @@
jp _blkSeek jp _blkSeek
jp _blkTell jp _blkTell
jp printcrlf jp printcrlf
jp stdioPutC
jp stdioReadC jp stdioReadC
jp stdioGetLine jp stdioGetLine
jp blkGetC jp blkGetC
jp blkSeek jp blkSeek
jp blkTell
#include "core.asm" #include "core.asm"
#include "err.h" #include "err.h"

View File

@ -28,7 +28,9 @@
.equ _blkSeek 0x42 .equ _blkSeek 0x42
.equ _blkTell 0x45 .equ _blkTell 0x45
.equ printcrlf 0x48 .equ printcrlf 0x48
.equ stdioReadC 0x4b .equ stdioPutC 0x4b
.equ stdioGetLine 0x4e .equ stdioReadC 0x4e
.equ blkGetC 0x51 .equ stdioGetLine 0x51
.equ blkSeek 0x54 .equ blkGetC 0x54
.equ blkSeek 0x57
.equ blkTell 0x5a