1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-11-27 14:18:06 +11:00

fs: further adjust to previous blkdev refactoring

Previous refacoring broke all seek/tell within fs. fs handles now lost
the responsibility to keep track of current position. It's blkdev's job.
This commit is contained in:
Virgil Dupras 2019-06-04 20:45:01 -04:00
parent ae028e3a86
commit c18d42f08b
11 changed files with 101 additions and 112 deletions

View File

@ -31,11 +31,10 @@
; parseHex ; parseHex
; parseHexPair ; parseHexPair
; blkSel ; blkSel
; blkSet
; fsFindFN ; fsFindFN
; fsOpen ; fsOpen
; fsGetC ; fsGetC
; fsSeek
; fsTell
; cpHLDE ; cpHLDE
; parseArgs ; parseArgs
; _blkGetC ; _blkGetC

View File

@ -46,8 +46,10 @@
.equ IO_SAVED_POS IO_OUT_BLK+BLOCKDEV_SIZE .equ IO_SAVED_POS IO_OUT_BLK+BLOCKDEV_SIZE
; File handle for included source ; File handle for included source
.equ IO_INCLUDE_HDL IO_SAVED_POS+2 .equ IO_INCLUDE_HDL IO_SAVED_POS+2
; blkdev for include file
.equ IO_INCLUDE_BLK IO_INCLUDE_HDL+FS_HANDLE_SIZE
; see ioPutBack below ; see ioPutBack below
.equ IO_PUTBACK_BUF IO_INCLUDE_HDL+FS_HANDLE_SIZE .equ IO_PUTBACK_BUF IO_INCLUDE_BLK+BLOCKDEV_SIZE
.equ IO_IN_INCLUDE IO_PUTBACK_BUF+1 .equ IO_IN_INCLUDE IO_PUTBACK_BUF+1
.equ IO_PC IO_IN_INCLUDE+1 .equ IO_PC IO_IN_INCLUDE+1
; Current lineno in top-level file ; Current lineno in top-level file
@ -64,6 +66,9 @@ ioInit:
xor a xor a
ld (IO_PUTBACK_BUF), a ld (IO_PUTBACK_BUF), a
ld (IO_IN_INCLUDE), a ld (IO_IN_INCLUDE), a
ld de, IO_INCLUDE_BLK
ld hl, _ioIncBlk
call blkSet
jp ioResetCounters jp ioResetCounters
ioGetC: ioGetC:
@ -73,8 +78,8 @@ ioGetC:
call ioInInclude call ioInInclude
jr z, .normalmode jr z, .normalmode
; We're in "include mode", read from FS ; We're in "include mode", read from FS
ld ix, IO_INCLUDE_HDL ld ix, IO_INCLUDE_BLK
call fsGetC call _blkGetC
cp 0x0a ; newline cp 0x0a ; newline
jr nz, .notNewline jr nz, .notNewline
; We have newline. Increase lineno and return (the rest of the ; We have newline. Increase lineno and return (the rest of the
@ -182,8 +187,8 @@ _ioSeek:
jp _blkSeek jp _blkSeek
.include: .include:
; We're in "include mode", seek in FS ; We're in "include mode", seek in FS
ld ix, IO_INCLUDE_HDL ld ix, IO_INCLUDE_BLK
jp fsSeek ; returns jp _blkSeek ; returns
_ioTell: _ioTell:
call ioInInclude call ioInInclude
@ -193,8 +198,8 @@ _ioTell:
jp _blkTell jp _blkTell
.include: .include:
; We're in "include mode", tell from FS ; We're in "include mode", tell from FS
ld ix, IO_INCLUDE_HDL ld ix, IO_INCLUDE_BLK
jp fsTell ; returns jp _blkTell ; returns
; Sets Z according to whether we're inside an include ; Sets Z according to whether we're inside an include
ioInInclude: ioInInclude:
@ -213,6 +218,9 @@ ioOpenInclude:
ld (IO_IN_INCLUDE), a ld (IO_IN_INCLUDE), a
ld hl, 0 ld hl, 0
ld (IO_INC_LINENO), hl ld (IO_INC_LINENO), hl
xor a
ld ix, IO_INCLUDE_BLK
call _blkSeek
cp a ; ensure Z cp a ; ensure Z
ret ret
@ -229,3 +237,10 @@ ioLineNo:
pop af pop af
ret ret
_ioIncGetC:
ld ix, IO_INCLUDE_HDL
jp fsGetC
_ioIncBlk:
.dw _ioIncGetC, unsetZ

View File

@ -20,6 +20,7 @@ is configured to start at `0xe00`
> fopn 0 hello.asm ; open file in handle 0 > fopn 0 hello.asm ; open file in handle 0
> zasm 1 3 ; assemble opened file and spit result in mmap > zasm 1 3 ; assemble opened file and spit result in mmap
> bsel 3 ; select mmap > bsel 3 ; select mmap
> mptr e000 ; set memptr to mmap's beginning
> peek 5 > peek 5
210890CD3C ; looking good 210890CD3C ; looking good
> mptr 4200 ; hello.asm is configured to run from 0x4200 > mptr 4200 ; hello.asm is configured to run from 0x4200

View File

@ -58,7 +58,7 @@ blkSel:
ld hl, blkDevTbl ld hl, blkDevTbl
or a ; cp 0 or a ; cp 0
jr z, .afterloop ; index is zero? don't loop jr z, .end ; index is zero? don't loop
push bc ; <| push bc ; <|
ld b, a ; | ld b, a ; |
.loop: ; | .loop: ; |
@ -66,7 +66,19 @@ blkSel:
call addHL ; | call addHL ; |
djnz .loop ; | djnz .loop ; |
pop bc ; <| pop bc ; <|
.afterloop: .end:
call blkSet
pop hl
pop de
pop af
ret
; Setup blkdev handle in (DE) using routines at (HL).
blkSet:
push af
push de
push hl
; Write GETC ; Write GETC
push hl ; <| push hl ; <|
call intoHL ; | call intoHL ; |

View File

@ -86,9 +86,8 @@
.equ FS_META_FNAME_OFFSET 6 .equ FS_META_FNAME_OFFSET 6
; Size in bytes of a FS handle: ; Size in bytes of a FS handle:
; * 4 bytes for starting offset of the FS block ; * 4 bytes for starting offset of the FS block
; * 2 bytes for current position relative to block's position
; * 2 bytes for file size ; * 2 bytes for file size
.equ FS_HANDLE_SIZE 8 .equ FS_HANDLE_SIZE 6
.equ FS_ERR_NO_FS 0x5 .equ FS_ERR_NO_FS 0x5
.equ FS_ERR_NOT_FOUND 0x6 .equ FS_ERR_NOT_FOUND 0x6
@ -390,76 +389,63 @@ fsOpen:
ld (ix+2), a ld (ix+2), a
ld a, (FS_BLK+7) ld a, (FS_BLK+7)
ld (ix+3), a ld (ix+3), a
; Current pos
ld hl, FS_METASIZE
ld (ix+4), l
ld (ix+5), h
; file size ; file size
ld hl, (FS_META+FS_META_FSIZE_OFFSET) ld hl, (FS_META+FS_META_FSIZE_OFFSET)
ld (ix+6), l ld (ix+4), l
ld (ix+7), h ld (ix+5), h
pop af pop af
pop hl pop hl
ret ret
; Place FS blockdev at proper position for file handle in (IX). ; Place FS blockdev at proper position for file handle in (IX) at position HL.
fsPlaceH: fsPlaceH:
push af push af
push bc
push hl
push de push de
push hl
; Move fsdev to beginning of block
ld e, (ix) ld e, (ix)
ld d, (ix+1) ld d, (ix+1)
ld l, (ix+2) ld l, (ix+2)
ld h, (ix+3) ld h, (ix+3)
ld c, (ix+4)
ld b, (ix+5)
add hl, bc
jr nc, .nocarry
inc de
.nocarry:
ld a, BLOCKDEV_SEEK_ABSOLUTE ld a, BLOCKDEV_SEEK_ABSOLUTE
call fsblkSeek call fsblkSeek
pop de
; skip metadata
ld a, BLOCKDEV_SEEK_FORWARD
ld hl, FS_METASIZE
call fsblkSeek
pop hl pop hl
pop bc pop de
; go to specified pos
ld a, BLOCKDEV_SEEK_FORWARD
call fsblkSeek
pop af pop af
ret ret
; Advance file handle in (IX) by one byte ; Advance file handle in (IX) by one byte
fsAdvanceH: ; Sets Z according to whether HL is within bounds for file handle at (IX), that
push af ; is, if it is smaller than file size.
inc (ix+4) fsWithinBounds:
jr nz, .end
inc (ix+5)
.end:
pop af
ret
; Sets Z according to whether file handle at (IX) is within bounds, that is, if
; current position is smaller than file size.
fsHandleWithinBounds:
push hl
push de push de
; current pos in HL, adjusted to remove FS_METASIZE
call fsTell
; file size ; file size
ld e, (ix+6) ld e, (ix+4)
ld d, (ix+7) ld d, (ix+5)
call cpHLDE call cpHLDE
pop de pop de
pop hl
jr nc, .outOfBounds ; HL >= DE jr nc, .outOfBounds ; HL >= DE
cp a ; ensure Z cp a ; ensure Z
ret ret
.outOfBounds: .outOfBounds:
jp unsetZ ; returns jp unsetZ ; returns
; Read a byte in handle at (IX), put it into A and advance the handle's ; Read a byte in handle at (IX) at position HL and put it into A.
; position.
; Z is set on success, unset if handle is at the end of the file. ; Z is set on success, unset if handle is at the end of the file.
fsGetC: fsGetC:
call fsHandleWithinBounds ld a, h
ld a, l
call fsWithinBounds
jr z, .proceed jr z, .proceed
; We want to unset Z, but also return 0 to ensure that a GetC that ; We want to unset Z, but also return 0 to ensure that a GetC that
; doesn't check Z doesn't end up with false data. ; doesn't check Z doesn't end up with false data.
@ -467,35 +453,14 @@ fsGetC:
jp unsetZ ; returns jp unsetZ ; returns
.proceed: .proceed:
call fsPlaceH call fsPlaceH
call fsblkGetC jp fsblkGetC ; returns
ret nz ; error, don't advance
; increase current pos
jp fsAdvanceH ; returns
; Write byte A in handle (IX) and advance the handle's position. ; Write byte A in handle (IX) and advance the handle's position.
; Z is set on success, unset if handle is at the end of the file. ; Z is set on success, unset if handle is at the end of the file.
; TODO: detect end of block alloc ; TODO: detect end of block alloc
fsPutC: fsPutC:
call fsPlaceH call fsPlaceH
call fsblkPutC jp fsblkPutC ; returns
jp fsAdvanceH ; returns
; Sets position of handle (IX) to HL. This position does *not* include metadata.
; It is an offset that starts at actual data.
; Sets Z if offset is within bounds, unsets Z if it isn't.
fsSeek:
ld a, FS_METASIZE
call addHL
ld (ix+4), l
ld (ix+5), h
ret
; Returns current position of file handle at (IX) in HL.
fsTell:
ld l, (ix+4)
ld h, (ix+5)
ld a, FS_METASIZE
jp subHL ; returns
; Mount the fs subsystem upon the currently selected blockdev at current offset. ; Mount the fs subsystem upon the currently selected blockdev at current offset.
; Verify is block is valid and error out if its not, mounting nothing. ; Verify is block is valid and error out if its not, mounting nothing.

View File

@ -120,6 +120,8 @@ static void io_write(int unused, uint16_t addr, uint8_t val)
fsdev_ptr |= val; fsdev_ptr |= val;
fsdev_addr_lvl = 0; fsdev_addr_lvl = 0;
} }
} else {
fprintf(stderr, "Out of bounds I/O write: %d / %d (0x%x)\n", addr, val, val);
} }
} }

View File

@ -23,11 +23,10 @@
jp parseHex jp parseHex
jp parseHexPair jp parseHexPair
jp blkSel jp blkSel
jp blkSet
jp fsFindFN jp fsFindFN
jp fsOpen jp fsOpen
jp fsGetC jp fsGetC
jp fsSeek
jp fsTell
jp cpHLDE jp cpHLDE
jp parseArgs jp parseArgs
jp printstr jp printstr

View File

@ -16,15 +16,14 @@
.equ parseHex 0x1e .equ parseHex 0x1e
.equ parseHexPair 0x21 .equ parseHexPair 0x21
.equ blkSel 0x24 .equ blkSel 0x24
.equ fsFindFN 0x27 .equ blkSet 0x27
.equ fsOpen 0x2a .equ fsFindFN 0x2a
.equ fsGetC 0x2d .equ fsOpen 0x2d
.equ fsSeek 0x30 .equ fsGetC 0x30
.equ fsTell 0x33 .equ cpHLDE 0x33
.equ cpHLDE 0x36 .equ parseArgs 0x36
.equ parseArgs 0x39 .equ printstr 0x39
.equ printstr 0x3c .equ _blkGetC 0x3c
.equ _blkGetC 0x3f .equ _blkPutC 0x3f
.equ _blkPutC 0x42 .equ _blkSeek 0x42
.equ _blkSeek 0x45 .equ _blkTell 0x45
.equ _blkTell 0x48

View File

@ -21,11 +21,10 @@ jp findchar
jp parseHex jp parseHex
jp parseHexPair jp parseHexPair
jp blkSel jp blkSel
jp blkSet
jp fsFindFN jp fsFindFN
jp fsOpen jp fsOpen
jp fsGetC jp fsGetC
jp fsSeek
jp fsTell
jp cpHLDE jp cpHLDE
jp parseArgs jp parseArgs
jp _blkGetC jp _blkGetC

View File

@ -16,14 +16,13 @@
.equ parseHex 0x1e .equ parseHex 0x1e
.equ parseHexPair 0x21 .equ parseHexPair 0x21
.equ blkSel 0x24 .equ blkSel 0x24
.equ fsFindFN 0x27 .equ blkSet 0x27
.equ fsOpen 0x2a .equ fsFindFN 0x2a
.equ fsGetC 0x2d .equ fsOpen 0x2d
.equ fsSeek 0x30 .equ fsGetC 0x30
.equ fsTell 0x33 .equ cpHLDE 0x33
.equ cpHLDE 0x36 .equ parseArgs 0x36
.equ parseArgs 0x39 .equ _blkGetC 0x39
.equ _blkGetC 0x3c .equ _blkPutC 0x3c
.equ _blkPutC 0x3f .equ _blkSeek 0x3f
.equ _blkSeek 0x42 .equ _blkTell 0x42
.equ _blkTell 0x45

View File

@ -16,17 +16,16 @@
.equ parseHex 0x1e .equ parseHex 0x1e
.equ parseHexPair 0x21 .equ parseHexPair 0x21
.equ blkSel 0x24 .equ blkSel 0x24
.equ fsFindFN 0x27 .equ blkSet 0x27
.equ fsOpen 0x2a .equ fsFindFN 0x2a
.equ fsGetC 0x2d .equ fsOpen 0x2d
.equ fsSeek 0x30 .equ fsGetC 0x30
.equ fsTell 0x33 .equ cpHLDE 0x33
.equ cpHLDE 0x36 .equ parseArgs 0x36
.equ parseArgs 0x39 .equ _blkGetC 0x39
.equ _blkGetC 0x3c .equ _blkPutC 0x3c
.equ _blkPutC 0x3f .equ _blkSeek 0x3f
.equ _blkSeek 0x42 .equ _blkTell 0x42
.equ _blkTell 0x45
#include "err.h" #include "err.h"
#include "zasm/const.asm" #include "zasm/const.asm"