2019-04-15 16:53:11 -04:00
|
|
|
; blockdev
|
|
|
|
;
|
|
|
|
; A block device is an abstraction over something we can read from, write to.
|
|
|
|
;
|
|
|
|
; A device that fits this abstraction puts the properly hook into itself, and
|
|
|
|
; then the glue code assigns a blockdev ID to that device. It then becomes easy
|
|
|
|
; to access arbitrary devices in a convenient manner.
|
|
|
|
;
|
|
|
|
; This part exposes a new "bsel" command to select the currently active block
|
|
|
|
; device.
|
|
|
|
|
|
|
|
; *** DEFINES ***
|
|
|
|
; BLOCKDEV_COUNT: The number of devices we manage.
|
|
|
|
|
|
|
|
; *** CONSTS ***
|
2019-04-15 20:38:25 -04:00
|
|
|
BLOCKDEV_ERR_OUT_OF_BOUNDS .equ 0x03
|
2019-04-16 08:36:26 -04:00
|
|
|
BLOCKDEV_ERR_UNSUPPORTED .equ 0x04
|
2019-04-15 20:38:25 -04:00
|
|
|
|
2019-04-23 09:37:22 -04:00
|
|
|
BLOCKDEV_SEEK_ABSOLUTE .equ 0
|
|
|
|
BLOCKDEV_SEEK_FORWARD .equ 1
|
|
|
|
BLOCKDEV_SEEK_BACKWARD .equ 2
|
|
|
|
BLOCKDEV_SEEK_BEGINNING .equ 3
|
|
|
|
BLOCKDEV_SEEK_END .equ 4
|
|
|
|
|
2019-04-15 16:53:11 -04:00
|
|
|
; *** VARIABLES ***
|
2019-04-22 17:15:59 -04:00
|
|
|
; Pointer to the selected block device. A block device is a 8 bytes block of
|
|
|
|
; memory with pointers to GetC, PutC, Seek and Tell routines, in that order.
|
|
|
|
; 0 means unsupported.
|
2019-04-16 10:17:25 -04:00
|
|
|
BLOCKDEV_SEL .equ BLOCKDEV_RAMSTART
|
2019-04-15 21:56:15 -04:00
|
|
|
BLOCKDEV_RAMEND .equ BLOCKDEV_SEL+2
|
2019-04-15 16:53:11 -04:00
|
|
|
|
|
|
|
; *** CODE ***
|
2019-04-16 10:17:25 -04:00
|
|
|
; Select block index specified in A
|
|
|
|
blkSel:
|
|
|
|
push af
|
|
|
|
push hl
|
|
|
|
ld hl, blkDevTbl
|
2019-04-15 16:53:11 -04:00
|
|
|
cp 0
|
2019-04-16 10:17:25 -04:00
|
|
|
jr z, .afterloop ; index is zero? don't loop
|
2019-04-15 16:53:11 -04:00
|
|
|
push bc
|
|
|
|
ld b, a
|
|
|
|
.loop:
|
2019-04-22 17:15:59 -04:00
|
|
|
ld a, 8
|
2019-04-16 10:17:25 -04:00
|
|
|
call addHL
|
2019-04-15 16:53:11 -04:00
|
|
|
djnz .loop
|
|
|
|
pop bc
|
2019-04-16 10:17:25 -04:00
|
|
|
.afterloop:
|
|
|
|
ld (BLOCKDEV_SEL), hl
|
2019-04-22 17:15:59 -04:00
|
|
|
pop hl
|
2019-04-16 10:17:25 -04:00
|
|
|
pop af
|
2019-04-15 16:53:11 -04:00
|
|
|
ret
|
|
|
|
|
2019-04-15 21:56:15 -04:00
|
|
|
; In those routines below, IY is destroyed (we don't push it to the stack). We
|
|
|
|
; seldom use it anyways...
|
|
|
|
|
2019-04-22 17:15:59 -04:00
|
|
|
; set IX to the address of the routine in BLOCKDEV_SEL with offset IYL.
|
|
|
|
_blkCallAddr:
|
2019-04-15 16:53:11 -04:00
|
|
|
push de
|
2019-04-15 21:56:15 -04:00
|
|
|
ld de, (BLOCKDEV_SEL)
|
|
|
|
; DE now points to the *address table*, not the routine addresses
|
|
|
|
; themselves. One layer of indirection left.
|
|
|
|
; slide by offset
|
|
|
|
push af
|
|
|
|
ld a, iyl
|
|
|
|
call addDE ; slide by offset
|
|
|
|
pop af
|
|
|
|
call intoDE
|
|
|
|
; Alright, now de points to what we want to call
|
2019-04-15 16:53:11 -04:00
|
|
|
ld ixh, d
|
|
|
|
ld ixl, e
|
|
|
|
pop de
|
2019-04-22 17:15:59 -04:00
|
|
|
ret
|
|
|
|
|
|
|
|
; call routine in BLOCKDEV_SEL with offset IYL.
|
|
|
|
_blkCall:
|
|
|
|
push ix
|
|
|
|
call _blkCallAddr
|
|
|
|
; Before we call... is IX zero? We don't want to call a zero.
|
2019-04-16 08:36:26 -04:00
|
|
|
push af
|
2019-04-22 17:15:59 -04:00
|
|
|
xor a
|
|
|
|
cp ixh
|
|
|
|
jr nz, .ok ; not zero, ok
|
|
|
|
cp ixl
|
|
|
|
jr z, .error ; zero, error
|
2019-04-16 08:36:26 -04:00
|
|
|
.ok:
|
|
|
|
pop af
|
2019-04-15 16:53:11 -04:00
|
|
|
call callIX
|
2019-04-16 08:36:26 -04:00
|
|
|
jr .end
|
|
|
|
.error:
|
|
|
|
pop af
|
|
|
|
ld a, BLOCKDEV_ERR_UNSUPPORTED
|
|
|
|
.end:
|
2019-04-15 16:53:11 -04:00
|
|
|
pop ix
|
|
|
|
ret
|
|
|
|
|
2019-04-22 14:26:16 -04:00
|
|
|
; Reads one character from selected device and returns its value in A.
|
|
|
|
; Sets Z according to whether read was successful: Set if successful, unset
|
|
|
|
; if not.
|
2019-04-15 21:56:15 -04:00
|
|
|
blkGetC:
|
|
|
|
ld iyl, 0
|
|
|
|
jr _blkCall
|
|
|
|
|
2019-04-22 14:26:16 -04:00
|
|
|
; Repeatedly call blkGetC until the call is a success.
|
|
|
|
blkGetCW:
|
2019-04-22 17:15:59 -04:00
|
|
|
ld iyl, 0
|
|
|
|
call _blkCallAddr
|
|
|
|
.loop:
|
|
|
|
call callIX
|
|
|
|
jr nz, .loop
|
|
|
|
ret
|
|
|
|
|
|
|
|
; Reads B chars from blkGetC and copy them in (HL).
|
|
|
|
; Sets Z if successful, unset Z if there was an error.
|
|
|
|
blkRead:
|
2019-04-22 22:28:35 -04:00
|
|
|
push hl
|
2019-04-22 17:15:59 -04:00
|
|
|
.loop:
|
2019-04-22 14:26:16 -04:00
|
|
|
call blkGetC
|
2019-04-22 22:28:35 -04:00
|
|
|
jr nz, .end ; Z already unset
|
2019-04-22 17:15:59 -04:00
|
|
|
ld (hl), a
|
|
|
|
inc hl
|
|
|
|
djnz .loop
|
|
|
|
cp a ; ensure Z
|
2019-04-22 22:28:35 -04:00
|
|
|
.end:
|
|
|
|
pop hl
|
2019-04-22 14:26:16 -04:00
|
|
|
ret
|
|
|
|
|
|
|
|
; Writes character in A in current position in the selected device. Sets Z
|
|
|
|
; according to whether the operation was successful.
|
2019-04-15 16:53:11 -04:00
|
|
|
blkPutC:
|
2019-04-15 21:56:15 -04:00
|
|
|
ld iyl, 2
|
|
|
|
jr _blkCall
|
|
|
|
|
2019-04-23 12:04:09 -04:00
|
|
|
; Writes B chars to blkPutC from (HL).
|
|
|
|
; Sets Z if successful, unset Z if there was an error.
|
|
|
|
blkWrite:
|
|
|
|
push hl
|
|
|
|
.loop:
|
|
|
|
ld a, (hl)
|
|
|
|
call blkPutC
|
|
|
|
jr nz, .end ; Z already unset
|
|
|
|
inc hl
|
|
|
|
djnz .loop
|
|
|
|
cp a ; ensure Z
|
|
|
|
.end:
|
|
|
|
pop hl
|
|
|
|
ret
|
|
|
|
|
2019-04-22 22:54:23 -04:00
|
|
|
; Seeks the block device in one of 5 modes, which is the A argument:
|
|
|
|
; 0 : Move exactly to X, X being the HL argument.
|
|
|
|
; 1 : Move forward by X bytes, X being the HL argument
|
|
|
|
; 2 : Move backwards by X bytes, X being the HL argument
|
2019-04-22 22:28:35 -04:00
|
|
|
; 3 : Move to the end
|
|
|
|
; 4 : Move to the beginning
|
2019-04-16 08:36:26 -04:00
|
|
|
; Set position of selected device to the value specified in HL
|
2019-04-15 21:56:15 -04:00
|
|
|
blkSeek:
|
2019-04-23 08:28:00 -04:00
|
|
|
push de
|
2019-04-23 09:37:22 -04:00
|
|
|
cp BLOCKDEV_SEEK_FORWARD
|
2019-04-23 08:28:00 -04:00
|
|
|
jr z, .forward
|
2019-04-23 09:37:22 -04:00
|
|
|
cp BLOCKDEV_SEEK_BACKWARD
|
2019-04-23 08:28:00 -04:00
|
|
|
jr z, .backward
|
2019-04-23 09:37:22 -04:00
|
|
|
cp BLOCKDEV_SEEK_BEGINNING
|
2019-04-23 08:28:00 -04:00
|
|
|
jr z, .beginning
|
2019-04-23 09:37:22 -04:00
|
|
|
cp BLOCKDEV_SEEK_END
|
2019-04-23 08:28:00 -04:00
|
|
|
jr z, .end
|
|
|
|
; all other modes are considered absolute
|
|
|
|
jr .seek ; for absolute mode, HL is already correct
|
|
|
|
.forward:
|
|
|
|
ex hl, de ; DE has our offset
|
|
|
|
call blkTell ; HL has our curpos
|
|
|
|
add hl, de
|
|
|
|
jr nc, .seek ; no carry? alright!
|
|
|
|
; we have carry? out of bounds, set to maximum
|
|
|
|
.backward:
|
|
|
|
; TODO - subtraction are more complicated...
|
|
|
|
jr .seek
|
|
|
|
.beginning:
|
|
|
|
ld hl, 0
|
|
|
|
jr .seek
|
|
|
|
.end:
|
|
|
|
ld hl, 0xffff
|
|
|
|
.seek:
|
|
|
|
pop de
|
2019-04-15 21:56:15 -04:00
|
|
|
ld iyl, 4
|
|
|
|
jr _blkCall
|
|
|
|
|
2019-04-22 17:15:59 -04:00
|
|
|
; Returns the current position of the selected device in HL.
|
|
|
|
blkTell:
|
|
|
|
ld iyl, 6
|
|
|
|
jr _blkCall
|
|
|
|
|
2019-04-16 10:17:25 -04:00
|
|
|
; This label is at the end of the file on purpose: the glue file should include
|
|
|
|
; a list of device routine table entries just after the include. Each line
|
2019-04-22 17:15:59 -04:00
|
|
|
; has 4 word addresses: GetC, PutC and Seek, Tell. An entry could look like:
|
|
|
|
; .dw mmapGetC, mmapPutC, mmapSeek, mmapTell
|
2019-04-16 10:17:25 -04:00
|
|
|
blkDevTbl:
|