2019-04-23 23:37:22 +10: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:
|
|
|
|
;
|
|
|
|
; * a filesystem sits upon a blockdev. It needs GetC, PutC, Seek.
|
|
|
|
; * 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-13 01:45:08 +10:00
|
|
|
; "bsel" doesn't affect the currently mounted fs, which can still be interacted
|
2019-04-23 23:37:22 +10: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-24 04:00:54 +10: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 23:37:22 +10: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
|
|
|
|
; *** CONSTS ***
|
2019-05-19 05:18:19 +10:00
|
|
|
.equ FS_MAX_NAME_SIZE 0x1a
|
|
|
|
.equ FS_BLOCKSIZE 0x100
|
|
|
|
.equ FS_METASIZE 0x20
|
2019-04-24 03:29:16 +10:00
|
|
|
|
2019-05-19 05:18:19 +10:00
|
|
|
.equ FS_META_ALLOC_OFFSET 3
|
|
|
|
.equ FS_META_FSIZE_OFFSET 4
|
|
|
|
.equ FS_META_FNAME_OFFSET 6
|
2019-04-23 23:37:22 +10:00
|
|
|
; Size in bytes of a FS handle:
|
2019-05-29 05:56:39 +10:00
|
|
|
; * 4 bytes for starting offset of the FS block
|
|
|
|
; * 2 bytes for current position relative to block's position
|
2019-06-01 01:12:29 +10:00
|
|
|
; * 2 bytes for file size
|
|
|
|
.equ FS_HANDLE_SIZE 8
|
2019-05-19 05:18:19 +10:00
|
|
|
.equ FS_ERR_NO_FS 0x5
|
|
|
|
.equ FS_ERR_NOT_FOUND 0x6
|
2019-04-23 23:37:22 +10:00
|
|
|
|
|
|
|
; *** VARIABLES ***
|
2019-04-24 05:50:26 +10:00
|
|
|
; A copy of BLOCKDEV routines when the FS was mounted. 0 if no FS is mounted.
|
2019-05-19 05:18:19 +10:00
|
|
|
.equ FS_GETC FS_RAMSTART
|
|
|
|
.equ FS_PUTC FS_GETC+2
|
|
|
|
.equ FS_SEEK FS_PUTC+2
|
|
|
|
.equ FS_TELL FS_SEEK+2
|
2019-04-23 23:37:22 +10:00
|
|
|
; Offset at which our FS start on mounted device
|
2019-05-29 05:56:39 +10: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-05-19 05:18:19 +10:00
|
|
|
.equ FS_START FS_TELL+2
|
2019-04-23 23:37:22 +10:00
|
|
|
; Offset at which we are currently pointing to with regards to our routines
|
|
|
|
; below, which all assume this offset as a context. This offset is not relative
|
2019-05-29 05:56:39 +10:00
|
|
|
; to FS_START. It can be used directly with fsblkSeek. 32 bits.
|
|
|
|
.equ FS_PTR FS_START+4
|
2019-04-24 00:54:56 +10:00
|
|
|
; This variable below contain the metadata of the last block FS_PTR was moved
|
|
|
|
; to. We read this data in memory to avoid constant seek+read operations.
|
2019-05-29 05:56:39 +10:00
|
|
|
.equ FS_META FS_PTR+4
|
2019-05-19 05:18:19 +10:00
|
|
|
.equ FS_HANDLES FS_META+FS_METASIZE
|
|
|
|
.equ FS_RAMEND FS_HANDLES+FS_HANDLE_COUNT*FS_HANDLE_SIZE
|
2019-04-23 23:37:22 +10:00
|
|
|
|
2019-04-24 02:04:09 +10:00
|
|
|
; *** DATA ***
|
|
|
|
P_FS_MAGIC:
|
|
|
|
.db "CFS", 0
|
|
|
|
|
2019-04-23 23:37:22 +10:00
|
|
|
; *** CODE ***
|
|
|
|
|
2019-04-24 03:29:16 +10:00
|
|
|
fsInit:
|
|
|
|
xor a
|
2019-04-24 05:50:26 +10:00
|
|
|
ld hl, FS_GETC
|
|
|
|
ld b, FS_RAMEND-FS_GETC
|
2019-04-24 03:29:16 +10:00
|
|
|
call fill
|
|
|
|
ret
|
|
|
|
|
2019-04-23 23:37:22 +10:00
|
|
|
; *** Navigation ***
|
|
|
|
|
|
|
|
; Resets FS_PTR to the beginning. Errors out if no FS is mounted.
|
|
|
|
; Sets Z if success, unset if error
|
|
|
|
fsBegin:
|
|
|
|
push hl
|
|
|
|
ld hl, (FS_START)
|
|
|
|
ld (FS_PTR), hl
|
2019-05-29 05:56:39 +10:00
|
|
|
ld hl, (FS_START+2)
|
|
|
|
ld (FS_PTR+2), hl
|
2019-04-23 23:37:22 +10:00
|
|
|
pop hl
|
2019-04-24 00:54:56 +10:00
|
|
|
call fsReadMeta
|
2019-05-29 05:56:39 +10:00
|
|
|
jp fsIsValid ; sets Z, returns
|
2019-04-23 23:37:22 +10: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-24 02:04:09 +10:00
|
|
|
push bc
|
|
|
|
push hl
|
2019-04-24 03:29:16 +10:00
|
|
|
ld a, (FS_META+FS_META_ALLOC_OFFSET)
|
2019-04-24 02:04:09 +10:00
|
|
|
cp 0
|
|
|
|
jr z, .error ; if our block allocates 0 blocks, this is the
|
|
|
|
; end of the line.
|
|
|
|
call fsPlace
|
|
|
|
ld b, a ; we will seek A times
|
2019-05-17 03:59:52 +10:00
|
|
|
.loop:
|
2019-04-24 02:04:09 +10:00
|
|
|
ld a, BLOCKDEV_SEEK_FORWARD
|
|
|
|
ld hl, FS_BLOCKSIZE
|
2019-04-24 05:50:26 +10:00
|
|
|
call fsblkSeek
|
2019-04-24 02:04:09 +10:00
|
|
|
djnz .loop
|
|
|
|
; Good, were here. We're going to read meta from our current position.
|
2019-05-29 05:56:39 +10:00
|
|
|
call fsblkTell ; --> HL, --> DE
|
|
|
|
ld (FS_PTR), de
|
|
|
|
ld (FS_PTR+2), hl
|
2019-04-24 02:04:09 +10:00
|
|
|
call fsReadMeta
|
2019-04-24 04:00:54 +10:00
|
|
|
jr nz, .createChainEnd
|
2019-04-24 02:04:09 +10:00
|
|
|
call fsIsValid
|
2019-04-24 04:00:54 +10:00
|
|
|
jr nz, .createChainEnd
|
2019-04-24 02:04:09 +10:00
|
|
|
; We're good! We have a valid FS block and FS_PTR is already updated.
|
|
|
|
; Meta is already read. Nothing to do!
|
|
|
|
cp a ; ensure Z
|
|
|
|
jr .end
|
2019-04-24 04:00:54 +10: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-24 02:04:09 +10:00
|
|
|
.error:
|
2019-04-23 23:37:22 +10:00
|
|
|
call unsetZ
|
2019-04-24 02:04:09 +10:00
|
|
|
.end:
|
|
|
|
pop hl
|
|
|
|
pop bc
|
2019-04-23 23:37:22 +10:00
|
|
|
ret
|
|
|
|
|
2019-04-24 00:54:56 +10:00
|
|
|
; Reads metadata at current FS_PTR and place it in FS_META.
|
2019-04-24 05:50:26 +10:00
|
|
|
; Returns Z according to whether the fsblkRead operation succeeded.
|
2019-04-24 00:54:56 +10:00
|
|
|
fsReadMeta:
|
|
|
|
call fsPlace
|
|
|
|
push bc
|
|
|
|
push hl
|
2019-04-24 09:42:10 +10:00
|
|
|
ld b, FS_METASIZE
|
2019-04-24 00:54:56 +10:00
|
|
|
ld hl, FS_META
|
2019-04-24 05:50:26 +10:00
|
|
|
call fsblkRead ; Sets Z
|
2019-04-24 00:54:56 +10:00
|
|
|
pop hl
|
|
|
|
pop bc
|
|
|
|
ret
|
|
|
|
|
2019-04-24 02:04:09 +10:00
|
|
|
; Writes metadata in FS_META at current FS_PTR.
|
2019-04-24 05:50:26 +10:00
|
|
|
; Returns Z according to whether the fsblkWrite operation succeeded.
|
2019-04-24 02:04:09 +10:00
|
|
|
fsWriteMeta:
|
|
|
|
call fsPlace
|
|
|
|
push bc
|
|
|
|
push hl
|
2019-04-24 09:42:10 +10:00
|
|
|
ld b, FS_METASIZE
|
2019-04-24 02:04:09 +10:00
|
|
|
ld hl, FS_META
|
2019-04-24 05:50:26 +10:00
|
|
|
call fsblkWrite ; Sets Z
|
2019-04-24 02:04:09 +10:00
|
|
|
pop hl
|
|
|
|
pop bc
|
|
|
|
ret
|
|
|
|
|
|
|
|
; Initializes FS_META with "CFS" followed by zeroes
|
|
|
|
fsInitMeta:
|
|
|
|
push af
|
2019-04-24 04:00:54 +10:00
|
|
|
push bc
|
2019-04-24 02:04:09 +10:00
|
|
|
push de
|
|
|
|
push hl
|
|
|
|
ld hl, P_FS_MAGIC
|
|
|
|
ld de, FS_META
|
|
|
|
ld bc, 3
|
|
|
|
ldir
|
|
|
|
xor a
|
2019-05-13 04:31:35 +10:00
|
|
|
ld hl, FS_META+3
|
2019-04-24 09:42:10 +10:00
|
|
|
ld b, FS_METASIZE-3
|
2019-04-24 03:29:16 +10:00
|
|
|
call fill
|
2019-04-24 02:04:09 +10:00
|
|
|
pop hl
|
|
|
|
pop de
|
2019-04-24 04:00:54 +10:00
|
|
|
pop bc
|
2019-04-24 02:04:09 +10:00
|
|
|
pop af
|
|
|
|
ret
|
|
|
|
|
2019-05-29 05:56:39 +10:00
|
|
|
; Make sure that our underlying blockdev is correctly placed.
|
2019-04-23 23:37:22 +10:00
|
|
|
fsPlace:
|
|
|
|
push af
|
|
|
|
push hl
|
2019-05-29 05:56:39 +10:00
|
|
|
push de
|
2019-04-23 23:37:22 +10:00
|
|
|
xor a
|
2019-05-29 05:56:39 +10:00
|
|
|
ld de, (FS_PTR)
|
|
|
|
ld hl, (FS_PTR+2)
|
2019-04-24 05:50:26 +10:00
|
|
|
call fsblkSeek
|
2019-05-29 05:56:39 +10:00
|
|
|
pop de
|
2019-04-23 23:37:22 +10:00
|
|
|
pop hl
|
|
|
|
pop af
|
|
|
|
ret
|
|
|
|
|
2019-04-24 02:04:09 +10:00
|
|
|
; Create a new file with A blocks allocated to it and with its new name at
|
|
|
|
; (HL).
|
2019-04-23 23:37:22 +10: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.
|
|
|
|
; Places FS_PTR to the newly allocated block. You have to write the new
|
|
|
|
; filename yourself.
|
|
|
|
fsAlloc:
|
2019-04-24 02:04:09 +10:00
|
|
|
push bc
|
|
|
|
push de
|
2019-04-24 04:00:54 +10:00
|
|
|
ld c, a ; Let's store our A arg somewhere...
|
2019-04-24 02:04:09 +10:00
|
|
|
call fsBegin
|
2019-04-24 04:00:54 +10: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-24 02:04:09 +10:00
|
|
|
.loop1:
|
|
|
|
call fsNext
|
2019-04-24 04:00:54 +10: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-24 02:04:09 +10:00
|
|
|
call fsPlace ; Make sure that our block device points to
|
|
|
|
; the beginning of our FS block
|
|
|
|
; We've reached last block. Two situations are possible at this point:
|
2019-04-24 04:00:54 +10: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-24 02:04:09 +10: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-24 04:00:54 +10:00
|
|
|
ld a, c ; C == the number of blocks user asked for
|
2019-04-24 03:29:16 +10:00
|
|
|
ld (FS_META+FS_META_ALLOC_OFFSET), a
|
2019-04-24 02:04:09 +10:00
|
|
|
pop hl ; now we want our HL arg
|
2019-04-24 03:29:16 +10:00
|
|
|
ld de, FS_META+FS_META_FNAME_OFFSET
|
2019-04-24 02:04:09 +10:00
|
|
|
ld bc, FS_MAX_NAME_SIZE
|
|
|
|
ldir
|
|
|
|
; Good, FS_META ready. Now, let's update FS_PTR because it hasn't been
|
|
|
|
; changed yet.
|
2019-04-24 05:50:26 +10:00
|
|
|
call fsblkTell
|
2019-05-29 05:56:39 +10:00
|
|
|
ld (FS_PTR), de
|
|
|
|
ld (FS_PTR+2), hl
|
2019-04-24 02:04:09 +10:00
|
|
|
; Ok, now we can write our metadata
|
|
|
|
call fsWriteMeta
|
2019-04-24 04:00:54 +10:00
|
|
|
.end:
|
2019-04-24 02:04:09 +10:00
|
|
|
pop de
|
|
|
|
pop bc
|
2019-04-23 23:37:22 +10:00
|
|
|
ret
|
|
|
|
|
2019-05-13 05:38:58 +10:00
|
|
|
; Place FS_PTR to the filename with the name in (HL).
|
|
|
|
; 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 23:37:22 +10:00
|
|
|
; *** Metadata ***
|
|
|
|
|
2019-04-24 00:54:56 +10:00
|
|
|
; Sets Z according to whether the current block in FS_META is valid.
|
2019-04-23 23:37:22 +10:00
|
|
|
; Don't call other FS routines without checking block validity first: other
|
|
|
|
; routines don't do checks.
|
|
|
|
fsIsValid:
|
|
|
|
push hl
|
2019-04-24 00:54:56 +10:00
|
|
|
push de
|
2019-04-23 23:37:22 +10:00
|
|
|
ld a, 3
|
2019-04-24 00:54:56 +10:00
|
|
|
ld hl, FS_META
|
2019-04-24 02:04:09 +10:00
|
|
|
ld de, P_FS_MAGIC
|
2019-04-23 23:37:22 +10:00
|
|
|
call strncmp
|
2019-04-24 00:54:56 +10:00
|
|
|
; The result of Z is our result.
|
|
|
|
pop de
|
2019-04-23 23:37:22 +10:00
|
|
|
pop hl
|
|
|
|
ret
|
|
|
|
|
2019-04-24 03:29:16 +10:00
|
|
|
; Returns wheter current block is deleted in Z flag.
|
|
|
|
fsIsDeleted:
|
|
|
|
ld a, (FS_META+FS_META_FNAME_OFFSET)
|
|
|
|
cp 0 ; Z flag is our answer
|
2019-04-24 00:54:56 +10:00
|
|
|
ret
|
|
|
|
|
2019-04-24 05:50:26 +10: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-04-24 09:42:10 +10:00
|
|
|
fsblkGetC:
|
2019-06-01 04:06:24 +10:00
|
|
|
push ix
|
2019-04-24 09:42:10 +10:00
|
|
|
ld ix, (FS_GETC)
|
2019-06-01 04:06:24 +10:00
|
|
|
call _blkCall
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-24 09:42:10 +10:00
|
|
|
|
2019-04-24 05:50:26 +10:00
|
|
|
fsblkRead:
|
2019-06-01 04:06:24 +10:00
|
|
|
push ix
|
2019-04-24 05:50:26 +10:00
|
|
|
ld ix, (FS_GETC)
|
2019-06-01 04:06:24 +10:00
|
|
|
call _blkRead
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-24 05:50:26 +10:00
|
|
|
|
2019-04-24 09:42:10 +10:00
|
|
|
fsblkPutC:
|
2019-06-01 04:06:24 +10:00
|
|
|
push ix
|
2019-04-24 09:42:10 +10:00
|
|
|
ld ix, (FS_PUTC)
|
2019-06-01 04:06:24 +10:00
|
|
|
call _blkCall
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-24 09:42:10 +10:00
|
|
|
|
2019-04-24 05:50:26 +10:00
|
|
|
fsblkWrite:
|
2019-06-01 04:06:24 +10:00
|
|
|
push ix
|
2019-04-24 05:50:26 +10:00
|
|
|
ld ix, (FS_PUTC)
|
2019-06-01 04:06:24 +10:00
|
|
|
call _blkWrite
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-24 05:50:26 +10:00
|
|
|
|
|
|
|
fsblkSeek:
|
2019-06-01 04:06:24 +10:00
|
|
|
push ix
|
|
|
|
push iy
|
2019-04-24 05:50:26 +10:00
|
|
|
ld ix, (FS_SEEK)
|
|
|
|
ld iy, (FS_TELL)
|
2019-06-01 04:06:24 +10:00
|
|
|
call _blkSeek
|
|
|
|
pop iy
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-24 05:50:26 +10:00
|
|
|
|
|
|
|
fsblkTell:
|
2019-06-01 04:06:24 +10:00
|
|
|
push ix
|
2019-05-29 05:56:39 +10:00
|
|
|
ld de, 0
|
2019-04-24 05:50:26 +10:00
|
|
|
ld ix, (FS_TELL)
|
2019-06-01 04:06:24 +10:00
|
|
|
call _blkCall
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-24 05:50:26 +10:00
|
|
|
|
2019-04-23 23:37:22 +10:00
|
|
|
; *** Handling ***
|
|
|
|
|
2019-06-01 04:06:24 +10:00
|
|
|
; Open file at current position into handle at (IX)
|
2019-04-23 23:37:22 +10:00
|
|
|
fsOpen:
|
2019-04-24 09:42:10 +10:00
|
|
|
push hl
|
|
|
|
push af
|
2019-05-29 05:56:39 +10:00
|
|
|
; Starting pos
|
2019-06-01 04:06:24 +10:00
|
|
|
ld a, (FS_PTR)
|
|
|
|
ld (ix), a
|
|
|
|
ld a, (FS_PTR+1)
|
|
|
|
ld (ix+1), a
|
|
|
|
ld a, (FS_PTR+2)
|
|
|
|
ld (ix+2), a
|
|
|
|
ld a, (FS_PTR+3)
|
|
|
|
ld (ix+3), a
|
2019-05-29 05:56:39 +10:00
|
|
|
; Current pos
|
|
|
|
ld hl, FS_METASIZE
|
2019-06-01 04:06:24 +10:00
|
|
|
ld (ix+4), l
|
|
|
|
ld (ix+5), h
|
2019-06-01 01:12:29 +10:00
|
|
|
; file size
|
|
|
|
ld hl, (FS_META+FS_META_FSIZE_OFFSET)
|
2019-06-01 04:06:24 +10:00
|
|
|
ld (ix+6), l
|
|
|
|
ld (ix+7), h
|
2019-04-24 09:42:10 +10:00
|
|
|
pop af
|
|
|
|
pop hl
|
|
|
|
ret
|
|
|
|
|
2019-06-01 04:06:24 +10:00
|
|
|
; Place FS blockdev at proper position for file handle in (IX).
|
2019-04-24 09:42:10 +10:00
|
|
|
fsPlaceH:
|
|
|
|
push af
|
2019-05-29 05:56:39 +10:00
|
|
|
push bc
|
2019-04-24 09:42:10 +10:00
|
|
|
push hl
|
2019-05-17 11:15:00 +10:00
|
|
|
push de
|
2019-05-29 05:56:39 +10:00
|
|
|
ld e, (ix)
|
|
|
|
ld d, (ix+1)
|
|
|
|
ld l, (ix+2)
|
|
|
|
ld h, (ix+3)
|
|
|
|
ld c, (ix+4)
|
|
|
|
ld b, (ix+5)
|
|
|
|
add hl, bc
|
|
|
|
jr nc, .nocarry
|
|
|
|
inc de
|
|
|
|
.nocarry:
|
2019-04-24 09:42:10 +10:00
|
|
|
ld a, BLOCKDEV_SEEK_ABSOLUTE
|
|
|
|
call fsblkSeek
|
2019-06-01 04:06:24 +10:00
|
|
|
pop de
|
2019-04-24 09:42:10 +10:00
|
|
|
pop hl
|
2019-05-29 05:56:39 +10:00
|
|
|
pop bc
|
2019-04-24 09:42:10 +10:00
|
|
|
pop af
|
2019-04-23 23:37:22 +10:00
|
|
|
ret
|
|
|
|
|
2019-05-13 05:38:58 +10:00
|
|
|
; Advance file handle in (IX) by one byte
|
2019-04-24 09:42:10 +10:00
|
|
|
fsAdvanceH:
|
2019-05-13 05:38:58 +10:00
|
|
|
push af
|
2019-05-29 05:56:39 +10:00
|
|
|
inc (ix+4)
|
2019-05-17 11:15:00 +10:00
|
|
|
jr nz, .end
|
2019-05-29 05:56:39 +10:00
|
|
|
inc (ix+5)
|
2019-05-13 05:38:58 +10:00
|
|
|
.end:
|
|
|
|
pop af
|
2019-04-23 23:37:22 +10:00
|
|
|
ret
|
|
|
|
|
2019-06-01 04:06:24 +10:00
|
|
|
; Sets Z according to whether file handle at (IX) is within bounds, that is, if
|
2019-06-01 01:12:29 +10:00
|
|
|
; current position is smaller than file size.
|
|
|
|
fsHandleWithinBounds:
|
|
|
|
push hl
|
2019-06-01 04:06:24 +10:00
|
|
|
push de
|
2019-06-01 01:12:29 +10:00
|
|
|
; current pos in HL, adjusted to remove FS_METASIZE
|
|
|
|
call fsTell
|
|
|
|
; file size
|
|
|
|
ld e, (ix+6)
|
|
|
|
ld d, (ix+7)
|
|
|
|
call cpHLDE
|
|
|
|
pop de
|
|
|
|
pop hl
|
|
|
|
jr nc, .outOfBounds ; HL >= DE
|
|
|
|
cp a ; ensure Z
|
|
|
|
ret
|
|
|
|
.outOfBounds:
|
|
|
|
jp unsetZ ; returns
|
|
|
|
|
2019-06-01 04:06:24 +10:00
|
|
|
; Read a byte in handle at (IX), put it into A and advance the handle's
|
2019-04-23 23:37:22 +10:00
|
|
|
; position.
|
|
|
|
; Z is set on success, unset if handle is at the end of the file.
|
2019-04-24 09:42:10 +10:00
|
|
|
fsGetC:
|
2019-06-01 01:12:29 +10:00
|
|
|
call fsHandleWithinBounds
|
|
|
|
jr z, .proceed
|
|
|
|
; 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.
|
|
|
|
xor a
|
|
|
|
jp unsetZ ; returns
|
|
|
|
.proceed:
|
2019-05-13 05:38:58 +10:00
|
|
|
call fsPlaceH
|
2019-04-24 09:42:10 +10:00
|
|
|
call fsblkGetC
|
2019-06-01 04:06:24 +10:00
|
|
|
ret nz ; error, don't advance
|
2019-04-24 09:42:10 +10:00
|
|
|
; increase current pos
|
2019-06-01 04:06:24 +10:00
|
|
|
jp fsAdvanceH ; returns
|
2019-04-23 23:37:22 +10:00
|
|
|
|
2019-06-01 04:06:24 +10:00
|
|
|
; Write byte A in handle (IX) and advance the handle's position.
|
2019-04-24 09:42:10 +10:00
|
|
|
; Z is set on success, unset if handle is at the end of the file.
|
2019-05-29 05:56:39 +10:00
|
|
|
; TODO: detect end of block alloc
|
2019-04-24 09:42:10 +10:00
|
|
|
fsPutC:
|
|
|
|
call fsPlaceH
|
|
|
|
call fsblkPutC
|
2019-06-01 04:06:24 +10:00
|
|
|
jp fsAdvanceH ; returns
|
2019-04-23 23:37:22 +10:00
|
|
|
|
2019-06-01 04:06:24 +10:00
|
|
|
; Sets position of handle (IX) to HL. This position does *not* include metadata.
|
2019-04-23 23:37:22 +10:00
|
|
|
; It is an offset that starts at actual data.
|
|
|
|
; Sets Z if offset is within bounds, unsets Z if it isn't.
|
|
|
|
fsSeek:
|
2019-05-29 05:56:39 +10:00
|
|
|
ld a, FS_METASIZE
|
|
|
|
call addHL
|
|
|
|
ld (ix+4), l
|
|
|
|
ld (ix+5), h
|
2019-04-24 09:42:10 +10:00
|
|
|
ret
|
|
|
|
|
2019-06-01 04:06:24 +10:00
|
|
|
; Returns current position of file handle at (IX) in HL.
|
2019-04-24 09:42:10 +10:00
|
|
|
fsTell:
|
2019-05-29 05:56:39 +10:00
|
|
|
ld l, (ix+4)
|
|
|
|
ld h, (ix+5)
|
|
|
|
ld a, FS_METASIZE
|
|
|
|
jp subHL ; returns
|
2019-04-23 23:37:22 +10: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-04-24 05:50:26 +10:00
|
|
|
; Upon mounting, copy currently selected device in FS_GETC/PUTC/SEEK/TELL.
|
2019-04-24 09:42:10 +10:00
|
|
|
fsOn:
|
2019-04-23 23:37:22 +10:00
|
|
|
push hl
|
2019-04-24 05:50:26 +10: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.
|
|
|
|
ld hl, BLOCKDEV_GETC
|
|
|
|
ld de, FS_GETC
|
|
|
|
ld bc, 8 ; we have 8 bytes to copy
|
|
|
|
ldir ; copy!
|
|
|
|
call fsblkTell
|
2019-05-29 05:56:39 +10:00
|
|
|
ld (FS_START), de
|
|
|
|
ld (FS_START+2), hl
|
|
|
|
ld (FS_PTR), de
|
|
|
|
ld (FS_PTR+2), hl
|
2019-04-24 00:54:56 +10:00
|
|
|
call fsReadMeta
|
|
|
|
jr nz, .error
|
2019-04-23 23:37:22 +10:00
|
|
|
call fsIsValid
|
|
|
|
jr nz, .error
|
|
|
|
; success
|
|
|
|
xor a
|
|
|
|
jr .end
|
|
|
|
.error:
|
2019-04-24 05:50:26 +10:00
|
|
|
; couldn't mount. Let's reset our variables.
|
|
|
|
xor a
|
2019-05-29 05:56:39 +10:00
|
|
|
ld b, FS_META-FS_GETC ; reset routine pointers and FS ptrs
|
2019-04-24 05:50:26 +10:00
|
|
|
ld hl, FS_GETC
|
|
|
|
call fill
|
|
|
|
|
2019-04-23 23:37:22 +10:00
|
|
|
ld a, FS_ERR_NO_FS
|
|
|
|
.end:
|
2019-04-24 05:50:26 +10:00
|
|
|
pop bc
|
|
|
|
pop de
|
2019-04-23 23:37:22 +10:00
|
|
|
pop hl
|
|
|
|
ret
|
2019-06-01 01:12:29 +10:00
|
|
|
|
|
|
|
; Sets Z according to whether we have a filesystem mounted.
|
|
|
|
fsIsOn:
|
|
|
|
; check whether (FS_GETC) is zero
|
|
|
|
push hl
|
|
|
|
push de
|
|
|
|
ld hl, (FS_GETC)
|
|
|
|
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
|