2019-04-23 09:37:22 -04:00
|
|
|
; fs
|
|
|
|
;
|
|
|
|
; Collapse OS filesystem (CFS) is not made to be convenient, but to be simple.
|
|
|
|
; This is little more than "named storage blocks". Characteristics:
|
|
|
|
;
|
2019-10-30 16:59:35 -04:00
|
|
|
; * a filesystem sits upon a blockdev. It needs GetB, PutB, Seek.
|
2019-04-23 09:37:22 -04:00
|
|
|
; * No directory. Use filename prefix to group.
|
|
|
|
; * First block of each file has metadata. Others are raw data.
|
|
|
|
; * No FAT. Files are a chain of blocks of a predefined size. To enumerate
|
|
|
|
; files, you go through metadata blocks.
|
|
|
|
; * Fixed allocation. File size is determined at allocation time and cannot be
|
|
|
|
; grown, only shrunk.
|
|
|
|
; * New allocations try to find spots to fit in, but go at the end if no spot is
|
|
|
|
; large enough.
|
|
|
|
; * Block size is 0x100, max block count per file is 8bit, that means that max
|
|
|
|
; file size: 64k - metadata overhead.
|
|
|
|
;
|
|
|
|
; *** Selecting a "source" blockdev
|
|
|
|
;
|
|
|
|
; This unit exposes "fson" shell command to "mount" CFS upon the currently
|
|
|
|
; selected device, at the point where its seekptr currently sits. This checks
|
|
|
|
; if we have a valid first block and spits an error otherwise.
|
|
|
|
;
|
|
|
|
; "fson" takes an optional argument which is a number. If non-zero, we don't
|
|
|
|
; error out if there's no metadata: we create a new CFS fs with an empty block.
|
|
|
|
;
|
|
|
|
; The can only be one "mounted" fs at once. Selecting another blockdev through
|
2019-05-12 11:45:08 -04:00
|
|
|
; "bsel" doesn't affect the currently mounted fs, which can still be interacted
|
2019-04-23 09:37:22 -04:00
|
|
|
; with (which is important if we want to move data around).
|
|
|
|
;
|
|
|
|
; *** Block metadata
|
|
|
|
;
|
|
|
|
; At the beginning of the first block of each file, there is this data
|
|
|
|
; structure:
|
|
|
|
;
|
|
|
|
; 3b: Magic number "CFS"
|
|
|
|
; 1b: Allocated block count, including the first one. Except for the "ending"
|
|
|
|
; block, this is never zero.
|
|
|
|
; 2b: Size of file in bytes (actually written). Little endian.
|
|
|
|
; 26b: file name, null terminated. last byte must be null.
|
|
|
|
;
|
|
|
|
; That gives us 32 bytes of metadata for first first block, leaving a maximum
|
|
|
|
; file size of 0xffe0.
|
|
|
|
;
|
|
|
|
; *** Last block of the chain
|
|
|
|
;
|
|
|
|
; The last block of the chain is either a block that has no valid block next to
|
|
|
|
; it or a block that reports a 0 allocated block count.
|
|
|
|
;
|
2019-04-23 14:00:54 -04:00
|
|
|
; However, to simplify processing, whenever fsNext encounter a chain end of the
|
|
|
|
; first type (a valid block with > 0 allocated blocks), it places an empty block
|
|
|
|
; at the end of the chain. This makes the whole "end of chain" processing much
|
|
|
|
; easier: we assume that we always have a 0 block at the end.
|
|
|
|
;
|
2019-04-23 09:37:22 -04:00
|
|
|
; *** Deleted files
|
|
|
|
;
|
|
|
|
; When a file is deleted, its name is set to null. This indicates that the
|
|
|
|
; allocated space is up for grabs.
|
|
|
|
;
|
|
|
|
; *** File "handles"
|
|
|
|
;
|
|
|
|
; Programs will not typically open files themselves. How it works with CFS is
|
|
|
|
; that it exposes an API to plug target files in a blockdev ID. This all
|
|
|
|
; depends on how you glue parts together, but ideally, you'll have two
|
|
|
|
; fs-related blockdev IDs: one for reading, one for writing.
|
|
|
|
;
|
|
|
|
; Being plugged into the blockdev system, programs will access the files as they
|
|
|
|
; would with any other block device.
|
|
|
|
;
|
|
|
|
; *** Creating a new FS
|
|
|
|
;
|
|
|
|
; A valid Collapse OS filesystem is nothing more than the 3 bytes 'C', 'F', 'S'
|
|
|
|
; next to each other. Placing them at the right place is all you have to do to
|
|
|
|
; create your FS.
|
|
|
|
|
|
|
|
; *** DEFINES ***
|
|
|
|
; Number of handles we want to support
|
|
|
|
; FS_HANDLE_COUNT
|
2019-11-15 15:37:49 -05:00
|
|
|
;
|
2019-04-23 09:37:22 -04:00
|
|
|
; *** VARIABLES ***
|
2019-06-04 11:53:02 -04:00
|
|
|
; A copy of BLOCKDEV_SEL when the FS was mounted. 0 if no FS is mounted.
|
|
|
|
.equ FS_BLK FS_RAMSTART
|
2019-04-23 09:37:22 -04:00
|
|
|
; Offset at which our FS start on mounted device
|
2019-05-28 15:56:39 -04:00
|
|
|
; This pointer is 32 bits. 32 bits pointers are a bit awkward: first two bytes
|
|
|
|
; are high bytes *low byte first*, and then the low two bytes, same order.
|
|
|
|
; When loaded in HL/DE, the four bytes are loaded in this order: E, D, L, H
|
2019-10-30 16:59:35 -04:00
|
|
|
.equ FS_START @+BLOCKDEV_SIZE
|
2019-06-04 11:53:02 -04:00
|
|
|
; This variable below contain the metadata of the last block we moved
|
2019-04-23 10:54:56 -04:00
|
|
|
; to. We read this data in memory to avoid constant seek+read operations.
|
2019-10-30 16:59:35 -04:00
|
|
|
.equ FS_META @+4
|
|
|
|
.equ FS_HANDLES @+FS_METASIZE
|
|
|
|
.equ FS_RAMEND @+FS_HANDLE_COUNT*FS_HANDLE_SIZE
|
2019-04-23 09:37:22 -04:00
|
|
|
|
2019-04-23 12:04:09 -04:00
|
|
|
; *** DATA ***
|
|
|
|
P_FS_MAGIC:
|
|
|
|
.db "CFS", 0
|
|
|
|
|
2019-04-23 09:37:22 -04:00
|
|
|
; *** CODE ***
|
|
|
|
|
2019-04-23 13:29:16 -04:00
|
|
|
fsInit:
|
|
|
|
xor a
|
2019-06-04 11:53:02 -04:00
|
|
|
ld hl, FS_BLK
|
|
|
|
ld b, FS_RAMEND-FS_BLK
|
2019-04-23 13:29:16 -04:00
|
|
|
call fill
|
|
|
|
ret
|
|
|
|
|
2019-04-23 09:37:22 -04:00
|
|
|
; *** Navigation ***
|
|
|
|
|
2019-06-04 11:53:02 -04:00
|
|
|
; Seek to the beginning. Errors out if no FS is mounted.
|
2019-04-23 09:37:22 -04:00
|
|
|
; Sets Z if success, unset if error
|
|
|
|
fsBegin:
|
2019-06-04 11:53:02 -04:00
|
|
|
call fsIsOn
|
|
|
|
ret nz
|
2019-04-23 09:37:22 -04:00
|
|
|
push hl
|
2019-06-04 11:53:02 -04:00
|
|
|
push de
|
|
|
|
push af
|
|
|
|
ld de, (FS_START)
|
2019-05-28 15:56:39 -04:00
|
|
|
ld hl, (FS_START+2)
|
2019-06-04 11:53:02 -04:00
|
|
|
ld a, BLOCKDEV_SEEK_ABSOLUTE
|
|
|
|
call fsblkSeek
|
|
|
|
pop af
|
|
|
|
pop de
|
2019-04-23 09:37:22 -04:00
|
|
|
pop hl
|
2019-04-23 10:54:56 -04:00
|
|
|
call fsReadMeta
|
2019-05-28 15:56:39 -04:00
|
|
|
jp fsIsValid ; sets Z, returns
|
2019-04-23 09:37:22 -04:00
|
|
|
|
|
|
|
; Change current position to the next block with metadata. If it can't (if this
|
|
|
|
; is the last valid block), doesn't move.
|
|
|
|
; Sets Z according to whether we moved.
|
|
|
|
fsNext:
|
2019-04-23 12:04:09 -04:00
|
|
|
push bc
|
|
|
|
push hl
|
2019-04-23 13:29:16 -04:00
|
|
|
ld a, (FS_META+FS_META_ALLOC_OFFSET)
|
2019-06-04 11:53:02 -04:00
|
|
|
or a ; cp 0
|
2019-04-23 12:04:09 -04:00
|
|
|
jr z, .error ; if our block allocates 0 blocks, this is the
|
|
|
|
; end of the line.
|
|
|
|
ld b, a ; we will seek A times
|
2019-05-16 13:59:52 -04:00
|
|
|
.loop:
|
2019-04-23 12:04:09 -04:00
|
|
|
ld a, BLOCKDEV_SEEK_FORWARD
|
|
|
|
ld hl, FS_BLOCKSIZE
|
2019-04-23 15:50:26 -04:00
|
|
|
call fsblkSeek
|
2019-04-23 12:04:09 -04:00
|
|
|
djnz .loop
|
|
|
|
call fsReadMeta
|
2019-04-23 14:00:54 -04:00
|
|
|
jr nz, .createChainEnd
|
2019-04-23 12:04:09 -04:00
|
|
|
call fsIsValid
|
2019-04-23 14:00:54 -04:00
|
|
|
jr nz, .createChainEnd
|
2019-06-04 11:53:02 -04:00
|
|
|
; We're good! We have a valid FS block.
|
2019-04-23 12:04:09 -04:00
|
|
|
; Meta is already read. Nothing to do!
|
|
|
|
cp a ; ensure Z
|
|
|
|
jr .end
|
2019-04-23 14:00:54 -04:00
|
|
|
.createChainEnd:
|
|
|
|
; We are on an invalid block where a valid block should be. This is
|
|
|
|
; the end of the line, but we should mark it a bit more explicitly.
|
|
|
|
; Let's initialize an empty block
|
|
|
|
call fsInitMeta
|
|
|
|
call fsWriteMeta
|
|
|
|
; continue out to error condition: we're still at the end of the line.
|
2019-04-23 12:04:09 -04:00
|
|
|
.error:
|
2019-04-23 09:37:22 -04:00
|
|
|
call unsetZ
|
2019-04-23 12:04:09 -04:00
|
|
|
.end:
|
|
|
|
pop hl
|
|
|
|
pop bc
|
2019-04-23 09:37:22 -04:00
|
|
|
ret
|
|
|
|
|
2019-06-04 11:53:02 -04:00
|
|
|
; Reads metadata at current fsblk and place it in FS_META.
|
|
|
|
; Returns Z according to whether the operation succeeded.
|
2019-04-23 10:54:56 -04:00
|
|
|
fsReadMeta:
|
|
|
|
push bc
|
|
|
|
push hl
|
2019-04-23 19:42:10 -04:00
|
|
|
ld b, FS_METASIZE
|
2019-04-23 10:54:56 -04:00
|
|
|
ld hl, FS_META
|
2019-04-23 15:50:26 -04:00
|
|
|
call fsblkRead ; Sets Z
|
2019-04-23 10:54:56 -04:00
|
|
|
pop hl
|
|
|
|
pop bc
|
2019-06-04 11:53:02 -04:00
|
|
|
ret nz
|
|
|
|
; Only rewind on success
|
|
|
|
jr _fsRewindAfterMeta
|
2019-04-23 10:54:56 -04:00
|
|
|
|
2019-06-04 11:53:02 -04:00
|
|
|
; Writes metadata in FS_META at current fsblk.
|
2019-04-23 15:50:26 -04:00
|
|
|
; Returns Z according to whether the fsblkWrite operation succeeded.
|
2019-04-23 12:04:09 -04:00
|
|
|
fsWriteMeta:
|
|
|
|
push bc
|
|
|
|
push hl
|
2019-04-23 19:42:10 -04:00
|
|
|
ld b, FS_METASIZE
|
2019-04-23 12:04:09 -04:00
|
|
|
ld hl, FS_META
|
2019-04-23 15:50:26 -04:00
|
|
|
call fsblkWrite ; Sets Z
|
2019-04-23 12:04:09 -04:00
|
|
|
pop hl
|
|
|
|
pop bc
|
2019-06-04 11:53:02 -04:00
|
|
|
ret nz
|
|
|
|
; Only rewind on success
|
|
|
|
jr _fsRewindAfterMeta
|
|
|
|
|
|
|
|
_fsRewindAfterMeta:
|
|
|
|
; return back to before the read op
|
|
|
|
push af
|
|
|
|
push hl
|
|
|
|
ld a, BLOCKDEV_SEEK_BACKWARD
|
|
|
|
ld hl, FS_METASIZE
|
|
|
|
call fsblkSeek
|
|
|
|
pop hl
|
|
|
|
pop af
|
2019-04-23 12:04:09 -04:00
|
|
|
ret
|
|
|
|
|
|
|
|
; Initializes FS_META with "CFS" followed by zeroes
|
|
|
|
fsInitMeta:
|
|
|
|
push af
|
2019-04-23 14:00:54 -04:00
|
|
|
push bc
|
2019-04-23 12:04:09 -04:00
|
|
|
push de
|
|
|
|
push hl
|
|
|
|
ld hl, P_FS_MAGIC
|
|
|
|
ld de, FS_META
|
|
|
|
ld bc, 3
|
|
|
|
ldir
|
|
|
|
xor a
|
2019-05-12 14:31:35 -04:00
|
|
|
ld hl, FS_META+3
|
2019-04-23 19:42:10 -04:00
|
|
|
ld b, FS_METASIZE-3
|
2019-04-23 13:29:16 -04:00
|
|
|
call fill
|
2019-04-23 12:04:09 -04:00
|
|
|
pop hl
|
|
|
|
pop de
|
2019-04-23 14:00:54 -04:00
|
|
|
pop bc
|
2019-04-23 12:04:09 -04:00
|
|
|
pop af
|
|
|
|
ret
|
|
|
|
|
|
|
|
; Create a new file with A blocks allocated to it and with its new name at
|
|
|
|
; (HL).
|
2019-04-23 09:37:22 -04:00
|
|
|
; Before doing so, enumerate all blocks in search of a deleted file with
|
|
|
|
; allocated space big enough. If it does, it will either take the whole space
|
|
|
|
; if the allocated space asked is exactly the same, or of it isn't, split the
|
|
|
|
; free space in 2 and create a new deleted metadata block next to the newly
|
|
|
|
; created block.
|
2019-06-04 11:53:02 -04:00
|
|
|
; Places fsblk to the newly allocated block. You have to write the new
|
2019-04-23 09:37:22 -04:00
|
|
|
; filename yourself.
|
|
|
|
fsAlloc:
|
2019-04-23 12:04:09 -04:00
|
|
|
push bc
|
|
|
|
push de
|
2019-04-23 14:00:54 -04:00
|
|
|
ld c, a ; Let's store our A arg somewhere...
|
2019-04-23 12:04:09 -04:00
|
|
|
call fsBegin
|
2019-04-23 14:00:54 -04:00
|
|
|
jr nz, .end ; not a valid block? hum, something's wrong
|
|
|
|
; First step: find last block
|
|
|
|
push hl ; keep HL for later
|
2019-04-23 12:04:09 -04:00
|
|
|
.loop1:
|
|
|
|
call fsNext
|
2019-04-23 14:00:54 -04:00
|
|
|
jr nz, .found ; end of the line
|
|
|
|
call fsIsDeleted
|
|
|
|
jr nz, .loop1 ; not deleted? loop
|
|
|
|
; This is a deleted block. Maybe it fits...
|
|
|
|
ld a, (FS_META+FS_META_ALLOC_OFFSET)
|
|
|
|
cp c ; Same as asked size?
|
|
|
|
jr z, .found ; yes? great!
|
|
|
|
; TODO: handle case where C < A (block splitting)
|
|
|
|
jr .loop1
|
|
|
|
.found:
|
2019-04-23 12:04:09 -04:00
|
|
|
; We've reached last block. Two situations are possible at this point:
|
2019-04-23 14:00:54 -04:00
|
|
|
; 1 - the block is the "end of line" block
|
|
|
|
; 2 - the block is a deleted block that we we're re-using.
|
|
|
|
; In both case, the processing is the same: write new metadata.
|
2019-04-23 12:04:09 -04:00
|
|
|
; At this point, the blockdev is placed right where we want to allocate
|
|
|
|
; But first, let's prepare the FS_META we're going to write
|
|
|
|
call fsInitMeta
|
2019-04-23 14:00:54 -04:00
|
|
|
ld a, c ; C == the number of blocks user asked for
|
2019-04-23 13:29:16 -04:00
|
|
|
ld (FS_META+FS_META_ALLOC_OFFSET), a
|
2019-04-23 12:04:09 -04:00
|
|
|
pop hl ; now we want our HL arg
|
2019-06-07 14:59:53 -04:00
|
|
|
; TODO: stop after null char. we're filling meta with garbage here.
|
2019-04-23 13:29:16 -04:00
|
|
|
ld de, FS_META+FS_META_FNAME_OFFSET
|
2019-04-23 12:04:09 -04:00
|
|
|
ld bc, FS_MAX_NAME_SIZE
|
|
|
|
ldir
|
2019-06-04 11:53:02 -04:00
|
|
|
; Good, FS_META ready.
|
2019-04-23 12:04:09 -04:00
|
|
|
; Ok, now we can write our metadata
|
|
|
|
call fsWriteMeta
|
2019-04-23 14:00:54 -04:00
|
|
|
.end:
|
2019-04-23 12:04:09 -04:00
|
|
|
pop de
|
|
|
|
pop bc
|
2019-04-23 09:37:22 -04:00
|
|
|
ret
|
|
|
|
|
2019-06-04 11:53:02 -04:00
|
|
|
; Place fsblk to the filename with the name in (HL).
|
2019-05-12 15:38:58 -04:00
|
|
|
; Sets Z on success, unset when not found.
|
|
|
|
fsFindFN:
|
|
|
|
push de
|
|
|
|
call fsBegin
|
|
|
|
jr nz, .end ; nothing to find, Z is unset
|
|
|
|
ld a, FS_MAX_NAME_SIZE
|
|
|
|
.loop:
|
|
|
|
ld de, FS_META+FS_META_FNAME_OFFSET
|
|
|
|
call strncmp
|
|
|
|
jr z, .end ; Z is set
|
|
|
|
call fsNext
|
|
|
|
jr z, .loop
|
|
|
|
; End of the chain, not found
|
|
|
|
call unsetZ
|
|
|
|
.end:
|
|
|
|
pop de
|
|
|
|
ret
|
|
|
|
|
2019-04-23 09:37:22 -04:00
|
|
|
; *** Metadata ***
|
|
|
|
|
2019-04-23 10:54:56 -04:00
|
|
|
; Sets Z according to whether the current block in FS_META is valid.
|
2019-04-23 09:37:22 -04:00
|
|
|
; Don't call other FS routines without checking block validity first: other
|
|
|
|
; routines don't do checks.
|
|
|
|
fsIsValid:
|
|
|
|
push hl
|
2019-04-23 10:54:56 -04:00
|
|
|
push de
|
2019-04-23 09:37:22 -04:00
|
|
|
ld a, 3
|
2019-04-23 10:54:56 -04:00
|
|
|
ld hl, FS_META
|
2019-04-23 12:04:09 -04:00
|
|
|
ld de, P_FS_MAGIC
|
2019-04-23 09:37:22 -04:00
|
|
|
call strncmp
|
2019-04-23 10:54:56 -04:00
|
|
|
; The result of Z is our result.
|
|
|
|
pop de
|
2019-04-23 09:37:22 -04:00
|
|
|
pop hl
|
|
|
|
ret
|
|
|
|
|
2019-10-09 11:12:08 -04:00
|
|
|
; Returns whether current block is deleted in Z flag.
|
2019-04-23 13:29:16 -04:00
|
|
|
fsIsDeleted:
|
|
|
|
ld a, (FS_META+FS_META_FNAME_OFFSET)
|
|
|
|
cp 0 ; Z flag is our answer
|
2019-04-23 10:54:56 -04:00
|
|
|
ret
|
|
|
|
|
2019-04-23 15:50:26 -04:00
|
|
|
; *** blkdev methods ***
|
|
|
|
; When "mounting" a FS, we copy the current blkdev's routine privately so that
|
|
|
|
; we can still access the FS even if blkdev selection changes. These routines
|
|
|
|
; below mimic blkdev's methods, but for our private mount.
|
|
|
|
|
2019-10-30 16:59:35 -04:00
|
|
|
fsblkGetB:
|
2019-05-31 14:06:24 -04:00
|
|
|
push ix
|
2019-06-04 11:53:02 -04:00
|
|
|
ld ix, FS_BLK
|
2019-10-30 16:59:35 -04:00
|
|
|
call _blkGetB
|
2019-05-31 14:06:24 -04:00
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-23 19:42:10 -04:00
|
|
|
|
2019-04-23 15:50:26 -04:00
|
|
|
fsblkRead:
|
2019-05-31 14:06:24 -04:00
|
|
|
push ix
|
2019-06-04 11:53:02 -04:00
|
|
|
ld ix, FS_BLK
|
2019-05-31 14:06:24 -04:00
|
|
|
call _blkRead
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-23 15:50:26 -04:00
|
|
|
|
2019-10-30 16:59:35 -04:00
|
|
|
fsblkPutB:
|
2019-05-31 14:06:24 -04:00
|
|
|
push ix
|
2019-06-04 11:53:02 -04:00
|
|
|
ld ix, FS_BLK
|
2019-10-30 16:59:35 -04:00
|
|
|
call _blkPutB
|
2019-05-31 14:06:24 -04:00
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-23 19:42:10 -04:00
|
|
|
|
2019-04-23 15:50:26 -04:00
|
|
|
fsblkWrite:
|
2019-05-31 14:06:24 -04:00
|
|
|
push ix
|
2019-06-04 11:53:02 -04:00
|
|
|
ld ix, FS_BLK
|
2019-05-31 14:06:24 -04:00
|
|
|
call _blkWrite
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-23 15:50:26 -04:00
|
|
|
|
|
|
|
fsblkSeek:
|
2019-05-31 14:06:24 -04:00
|
|
|
push ix
|
2019-06-04 11:53:02 -04:00
|
|
|
ld ix, FS_BLK
|
2019-05-31 14:06:24 -04:00
|
|
|
call _blkSeek
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-23 15:50:26 -04:00
|
|
|
|
|
|
|
fsblkTell:
|
2019-05-31 14:06:24 -04:00
|
|
|
push ix
|
2019-06-04 11:53:02 -04:00
|
|
|
ld ix, FS_BLK
|
|
|
|
call _blkTell
|
2019-05-31 14:06:24 -04:00
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-23 15:50:26 -04:00
|
|
|
|
2019-04-23 09:37:22 -04:00
|
|
|
; *** Handling ***
|
|
|
|
|
2019-05-31 14:06:24 -04:00
|
|
|
; Open file at current position into handle at (IX)
|
2019-04-23 09:37:22 -04:00
|
|
|
fsOpen:
|
2019-04-23 19:42:10 -04:00
|
|
|
push hl
|
|
|
|
push af
|
2019-05-28 15:56:39 -04:00
|
|
|
; Starting pos
|
2019-06-04 11:53:02 -04:00
|
|
|
ld a, (FS_BLK+4)
|
2019-05-31 14:06:24 -04:00
|
|
|
ld (ix), a
|
2019-06-04 11:53:02 -04:00
|
|
|
ld a, (FS_BLK+5)
|
2019-05-31 14:06:24 -04:00
|
|
|
ld (ix+1), a
|
2019-06-04 11:53:02 -04:00
|
|
|
ld a, (FS_BLK+6)
|
2019-05-31 14:06:24 -04:00
|
|
|
ld (ix+2), a
|
2019-06-04 11:53:02 -04:00
|
|
|
ld a, (FS_BLK+7)
|
2019-05-31 14:06:24 -04:00
|
|
|
ld (ix+3), a
|
2019-05-31 11:12:29 -04:00
|
|
|
; file size
|
|
|
|
ld hl, (FS_META+FS_META_FSIZE_OFFSET)
|
2019-06-04 20:45:01 -04:00
|
|
|
ld (ix+4), l
|
|
|
|
ld (ix+5), h
|
2019-04-23 19:42:10 -04:00
|
|
|
pop af
|
|
|
|
pop hl
|
|
|
|
ret
|
|
|
|
|
2019-06-04 20:45:01 -04:00
|
|
|
; Place FS blockdev at proper position for file handle in (IX) at position HL.
|
2019-04-23 19:42:10 -04:00
|
|
|
fsPlaceH:
|
|
|
|
push af
|
2019-05-16 21:15:00 -04:00
|
|
|
push de
|
2019-06-04 20:45:01 -04:00
|
|
|
push hl
|
|
|
|
; Move fsdev to beginning of block
|
2019-05-28 15:56:39 -04:00
|
|
|
ld e, (ix)
|
|
|
|
ld d, (ix+1)
|
|
|
|
ld l, (ix+2)
|
|
|
|
ld h, (ix+3)
|
2019-04-23 19:42:10 -04:00
|
|
|
ld a, BLOCKDEV_SEEK_ABSOLUTE
|
|
|
|
call fsblkSeek
|
2019-06-04 20:45:01 -04:00
|
|
|
|
|
|
|
; skip metadata
|
|
|
|
ld a, BLOCKDEV_SEEK_FORWARD
|
|
|
|
ld hl, FS_METASIZE
|
|
|
|
call fsblkSeek
|
|
|
|
|
2019-04-23 19:42:10 -04:00
|
|
|
pop hl
|
2019-06-04 20:45:01 -04:00
|
|
|
pop de
|
2019-04-23 09:37:22 -04:00
|
|
|
|
2019-06-04 20:45:01 -04:00
|
|
|
; go to specified pos
|
|
|
|
ld a, BLOCKDEV_SEEK_FORWARD
|
|
|
|
call fsblkSeek
|
2019-05-12 15:38:58 -04:00
|
|
|
pop af
|
2019-04-23 09:37:22 -04:00
|
|
|
ret
|
|
|
|
|
2019-06-04 20:45:01 -04:00
|
|
|
; Sets Z according to whether HL is within bounds for file handle at (IX), that
|
|
|
|
; is, if it is smaller than file size.
|
|
|
|
fsWithinBounds:
|
2019-05-31 14:06:24 -04:00
|
|
|
push de
|
2019-05-31 11:12:29 -04:00
|
|
|
; file size
|
2019-06-04 20:45:01 -04:00
|
|
|
ld e, (ix+4)
|
|
|
|
ld d, (ix+5)
|
2019-05-31 11:12:29 -04:00
|
|
|
call cpHLDE
|
|
|
|
pop de
|
|
|
|
jr nc, .outOfBounds ; HL >= DE
|
|
|
|
cp a ; ensure Z
|
|
|
|
ret
|
|
|
|
.outOfBounds:
|
|
|
|
jp unsetZ ; returns
|
|
|
|
|
2019-07-21 11:39:00 -04:00
|
|
|
; Set size of file handle (IX) to value in HL.
|
|
|
|
; This writes directly in handle's metadata.
|
|
|
|
fsSetSize:
|
|
|
|
push hl ; --> lvl 1
|
2019-06-05 16:34:14 -04:00
|
|
|
ld hl, 0
|
|
|
|
call fsPlaceH ; fs blkdev is now at beginning of content
|
|
|
|
; we need the blkdev to be on filesize's offset
|
|
|
|
ld hl, FS_METASIZE-FS_META_FSIZE_OFFSET
|
|
|
|
ld a, BLOCKDEV_SEEK_BACKWARD
|
|
|
|
call fsblkSeek
|
2019-07-21 11:39:00 -04:00
|
|
|
pop hl ; <-- lvl 1
|
2019-06-05 16:34:14 -04:00
|
|
|
; blkdev is at the right spot, HL is back to its original value, let's
|
2019-07-21 11:39:00 -04:00
|
|
|
; write it both in the metadata block and in its file handle's cache.
|
|
|
|
push hl ; --> lvl 1
|
2019-06-05 16:34:14 -04:00
|
|
|
; now let's write our new filesize both in blkdev and in file handle's
|
|
|
|
; cache.
|
|
|
|
ld a, l
|
|
|
|
ld (ix+4), a
|
2019-10-30 16:59:35 -04:00
|
|
|
call fsblkPutB
|
2019-06-05 16:34:14 -04:00
|
|
|
ld a, h
|
|
|
|
ld (ix+5), a
|
2019-10-30 16:59:35 -04:00
|
|
|
call fsblkPutB
|
2019-07-21 11:39:00 -04:00
|
|
|
pop hl ; <-- lvl 1
|
2019-06-05 16:34:14 -04:00
|
|
|
xor a ; ensure Z
|
|
|
|
ret
|
|
|
|
|
2019-06-04 20:45:01 -04:00
|
|
|
; Read a byte in handle at (IX) at position HL and put it into A.
|
2019-04-23 09:37:22 -04:00
|
|
|
; Z is set on success, unset if handle is at the end of the file.
|
2019-10-30 16:59:35 -04:00
|
|
|
fsGetB:
|
2019-06-04 20:45:01 -04:00
|
|
|
call fsWithinBounds
|
2019-05-31 11:12:29 -04:00
|
|
|
jr z, .proceed
|
2019-10-30 16:59:35 -04:00
|
|
|
; We want to unset Z, but also return 0 to ensure that a GetB that
|
2019-05-31 11:12:29 -04:00
|
|
|
; doesn't check Z doesn't end up with false data.
|
|
|
|
xor a
|
|
|
|
jp unsetZ ; returns
|
|
|
|
.proceed:
|
2019-06-05 14:45:38 -04:00
|
|
|
push hl
|
2019-05-12 15:38:58 -04:00
|
|
|
call fsPlaceH
|
2019-10-30 16:59:35 -04:00
|
|
|
call fsblkGetB
|
2019-06-15 15:50:27 -04:00
|
|
|
cp a ; ensure Z
|
2019-06-05 14:45:38 -04:00
|
|
|
pop hl
|
|
|
|
ret
|
2019-04-23 09:37:22 -04:00
|
|
|
|
2019-07-21 11:39:00 -04:00
|
|
|
; Write byte A in handle (IX) at position HL.
|
2019-04-23 19:42:10 -04:00
|
|
|
; Z is set on success, unset if handle is at the end of the file.
|
2019-05-28 15:56:39 -04:00
|
|
|
; TODO: detect end of block alloc
|
2019-10-30 16:59:35 -04:00
|
|
|
fsPutB:
|
2019-06-05 14:45:38 -04:00
|
|
|
push hl
|
2019-04-23 19:42:10 -04:00
|
|
|
call fsPlaceH
|
2019-10-30 16:59:35 -04:00
|
|
|
call fsblkPutB
|
2019-06-05 14:45:38 -04:00
|
|
|
pop hl
|
2019-07-21 11:39:00 -04:00
|
|
|
; if HL is out of bounds, increase bounds
|
|
|
|
call fsWithinBounds
|
|
|
|
ret z
|
|
|
|
inc hl ; our filesize is now HL+1
|
|
|
|
jp fsSetSize
|
2019-04-23 09:37:22 -04:00
|
|
|
|
|
|
|
; 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.
|
2019-06-04 11:53:02 -04:00
|
|
|
; Upon mounting, copy currently selected device in FS_BLK.
|
2019-04-23 19:42:10 -04:00
|
|
|
fsOn:
|
2019-04-23 09:37:22 -04:00
|
|
|
push hl
|
2019-04-23 15:50:26 -04:00
|
|
|
push de
|
|
|
|
push bc
|
|
|
|
; We have to set blkdev routines early before knowing whether the
|
|
|
|
; mounting succeeds because methods like fsReadMeta uses fsblk* methods.
|
2019-06-04 09:56:36 -04:00
|
|
|
ld hl, BLOCKDEV_SEL
|
2019-06-04 11:53:02 -04:00
|
|
|
ld de, FS_BLK
|
|
|
|
ld bc, BLOCKDEV_SIZE
|
2019-04-23 15:50:26 -04:00
|
|
|
ldir ; copy!
|
|
|
|
call fsblkTell
|
2019-05-28 15:56:39 -04:00
|
|
|
ld (FS_START), de
|
|
|
|
ld (FS_START+2), hl
|
2019-04-23 10:54:56 -04:00
|
|
|
call fsReadMeta
|
|
|
|
jr nz, .error
|
2019-04-23 09:37:22 -04:00
|
|
|
call fsIsValid
|
|
|
|
jr nz, .error
|
|
|
|
; success
|
|
|
|
xor a
|
|
|
|
jr .end
|
|
|
|
.error:
|
2019-04-23 15:50:26 -04:00
|
|
|
; couldn't mount. Let's reset our variables.
|
|
|
|
xor a
|
2019-06-04 11:53:02 -04:00
|
|
|
ld b, FS_META-FS_BLK ; reset routine pointers and FS ptrs
|
|
|
|
ld hl, FS_BLK
|
2019-04-23 15:50:26 -04:00
|
|
|
call fill
|
|
|
|
|
2019-04-23 09:37:22 -04:00
|
|
|
ld a, FS_ERR_NO_FS
|
|
|
|
.end:
|
2019-04-23 15:50:26 -04:00
|
|
|
pop bc
|
|
|
|
pop de
|
2019-04-23 09:37:22 -04:00
|
|
|
pop hl
|
|
|
|
ret
|
2019-05-31 11:12:29 -04:00
|
|
|
|
|
|
|
; Sets Z according to whether we have a filesystem mounted.
|
|
|
|
fsIsOn:
|
2019-06-04 11:53:02 -04:00
|
|
|
; check whether (FS_BLK) is zero
|
2019-05-31 11:12:29 -04:00
|
|
|
push hl
|
|
|
|
push de
|
2019-06-04 11:53:02 -04:00
|
|
|
ld hl, (FS_BLK)
|
2019-05-31 11:12:29 -04:00
|
|
|
ld de, 0
|
|
|
|
call cpHLDE
|
|
|
|
jr nz, .mounted
|
|
|
|
; if equal, it means our FS is not mounted
|
|
|
|
call unsetZ
|
|
|
|
jr .end
|
|
|
|
.mounted:
|
|
|
|
cp a ; ensure Z
|
|
|
|
.end:
|
|
|
|
pop de
|
|
|
|
pop hl
|
|
|
|
ret
|
2019-11-15 15:37:49 -05:00
|
|
|
|
|
|
|
; Iterate over files in active file system and, for each file, call (IY) with
|
|
|
|
; the file's metadata currently placed. HL is set to FS_META.
|
|
|
|
; Sets Z on success, unset on error.
|
|
|
|
; There are no error condition happening midway. If you get an error, then (IY)
|
|
|
|
; was never called.
|
|
|
|
fsIter:
|
|
|
|
call fsIsOn
|
|
|
|
ret nz
|
|
|
|
call fsBegin
|
|
|
|
ret nz
|
|
|
|
.loop:
|
|
|
|
call fsIsDeleted
|
|
|
|
ld hl, FS_META
|
|
|
|
call nz, callIY
|
|
|
|
call fsNext
|
|
|
|
jr z, .loop ; Z set? fsNext was successful
|
2019-11-25 22:32:55 -05:00
|
|
|
cp a ; ensure Z
|
2019-11-15 15:37:49 -05:00
|
|
|
ret
|
|
|
|
|
|
|
|
; Delete currently active file
|
|
|
|
; Sets Z on success, unset on error.
|
|
|
|
fsDel:
|
|
|
|
call fsIsValid
|
|
|
|
ret nz
|
|
|
|
xor a
|
|
|
|
; Set filename to zero to flag it as deleted
|
|
|
|
ld (FS_META+FS_META_FNAME_OFFSET), a
|
|
|
|
jp fsWriteMeta
|
|
|
|
|
|
|
|
; Given a handle index in A, set DE to point to the proper handle.
|
|
|
|
fsHandle:
|
|
|
|
ld de, FS_HANDLES
|
|
|
|
or a ; cp 0
|
|
|
|
ret z ; DE already point to correct handle
|
|
|
|
push bc
|
|
|
|
ld b, a
|
|
|
|
.loop:
|
|
|
|
ld a, FS_HANDLE_SIZE
|
|
|
|
call addDE
|
|
|
|
djnz .loop
|
|
|
|
pop bc
|
|
|
|
ret
|