2019-04-16 06:53:11 +10: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.
|
2019-05-13 01:45:08 +10:00
|
|
|
;
|
|
|
|
; *** Blockdev routines ***
|
|
|
|
;
|
|
|
|
; There are 4 blockdev routines that can be defined by would-be block devices
|
|
|
|
; and they follow these specifications:
|
|
|
|
;
|
|
|
|
; GetC:
|
|
|
|
; 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.
|
|
|
|
;
|
|
|
|
; A successful GetC should advance the "pointer" of the device (if there is one)
|
|
|
|
; by one byte so that a subsequent GetC will read the next char. Unsuccessful
|
|
|
|
; reads generally mean that we reached EOF.
|
|
|
|
;
|
|
|
|
;
|
|
|
|
; PutC:
|
|
|
|
; Writes character in A in current position in the selected device. Sets Z
|
|
|
|
; according to whether the operation was successful.
|
|
|
|
;
|
|
|
|
; A successful PutC should advance the "pointer" of the device (if there is one)
|
|
|
|
; by one byte so that the next PutC places the next char next to this one.
|
|
|
|
; Unsuccessful writes generally mean that we reached EOF.
|
|
|
|
;
|
|
|
|
; Seek:
|
|
|
|
; Place device "pointer" at position dictated by HL.
|
|
|
|
;
|
|
|
|
; Tell:
|
|
|
|
; Return the position of the "pointer" in HL
|
2019-05-13 03:57:37 +10:00
|
|
|
;
|
|
|
|
; All routines are expected to preserve unused registers.
|
2019-05-13 01:45:08 +10:00
|
|
|
|
2019-04-16 06:53:11 +10:00
|
|
|
|
|
|
|
; *** DEFINES ***
|
|
|
|
; BLOCKDEV_COUNT: The number of devices we manage.
|
|
|
|
|
|
|
|
; *** CONSTS ***
|
2019-05-18 04:06:10 +10:00
|
|
|
.equ BLOCKDEV_ERR_OUT_OF_BOUNDS 0x03
|
|
|
|
.equ BLOCKDEV_ERR_UNSUPPORTED 0x04
|
2019-04-16 10:38:25 +10:00
|
|
|
|
2019-05-18 04:06:10 +10:00
|
|
|
.equ BLOCKDEV_SEEK_ABSOLUTE 0
|
|
|
|
.equ BLOCKDEV_SEEK_FORWARD 1
|
|
|
|
.equ BLOCKDEV_SEEK_BACKWARD 2
|
|
|
|
.equ BLOCKDEV_SEEK_BEGINNING 3
|
|
|
|
.equ BLOCKDEV_SEEK_END 4
|
2019-04-23 23:37:22 +10:00
|
|
|
|
2019-04-16 06:53:11 +10:00
|
|
|
; *** VARIABLES ***
|
2019-04-23 07:15:59 +10: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-05-18 04:06:10 +10:00
|
|
|
.equ BLOCKDEV_GETC BLOCKDEV_RAMSTART
|
|
|
|
.equ BLOCKDEV_PUTC BLOCKDEV_GETC+2
|
|
|
|
.equ BLOCKDEV_SEEK BLOCKDEV_PUTC+2
|
|
|
|
.equ BLOCKDEV_TELL BLOCKDEV_SEEK+2
|
|
|
|
.equ BLOCKDEV_RAMEND BLOCKDEV_TELL+2
|
2019-04-16 06:53:11 +10:00
|
|
|
|
|
|
|
; *** CODE ***
|
2019-05-11 08:20:43 +10:00
|
|
|
; Select block index specified in A and place them in routine pointers at (DE).
|
|
|
|
; For example, for a "regular" blkSel, you will want to set DE to BLOCKDEV_GETC.
|
2019-04-17 00:17:25 +10:00
|
|
|
blkSel:
|
|
|
|
push af
|
2019-05-11 08:20:43 +10:00
|
|
|
push de
|
2019-04-17 00:17:25 +10:00
|
|
|
push hl
|
2019-05-13 03:57:37 +10:00
|
|
|
|
2019-04-17 00:17:25 +10:00
|
|
|
ld hl, blkDevTbl
|
2019-05-13 03:57:37 +10:00
|
|
|
or a ; cp 0
|
2019-04-17 00:17:25 +10:00
|
|
|
jr z, .afterloop ; index is zero? don't loop
|
2019-04-16 06:53:11 +10:00
|
|
|
push bc
|
|
|
|
ld b, a
|
|
|
|
.loop:
|
2019-04-23 07:15:59 +10:00
|
|
|
ld a, 8
|
2019-04-17 00:17:25 +10:00
|
|
|
call addHL
|
2019-04-16 06:53:11 +10:00
|
|
|
djnz .loop
|
|
|
|
pop bc
|
2019-04-17 00:17:25 +10:00
|
|
|
.afterloop:
|
2019-04-24 05:03:23 +10:00
|
|
|
push hl
|
2019-05-13 03:57:37 +10:00
|
|
|
call intoHL
|
|
|
|
call writeHLinDE
|
|
|
|
inc de
|
|
|
|
inc de
|
2019-04-24 05:03:23 +10:00
|
|
|
pop hl
|
|
|
|
inc hl
|
|
|
|
inc hl
|
|
|
|
push hl
|
2019-05-13 03:57:37 +10:00
|
|
|
call intoHL
|
|
|
|
call writeHLinDE
|
|
|
|
inc de
|
|
|
|
inc de
|
2019-04-24 05:03:23 +10:00
|
|
|
pop hl
|
|
|
|
inc hl
|
|
|
|
inc hl
|
|
|
|
push hl
|
2019-05-13 03:57:37 +10:00
|
|
|
call intoHL
|
|
|
|
call writeHLinDE
|
|
|
|
inc de
|
|
|
|
inc de
|
2019-04-24 05:03:23 +10:00
|
|
|
pop hl
|
|
|
|
inc hl
|
|
|
|
inc hl
|
|
|
|
call intoHL
|
2019-05-11 08:20:43 +10:00
|
|
|
call writeHLinDE
|
2019-05-13 03:57:37 +10:00
|
|
|
|
2019-04-23 07:15:59 +10:00
|
|
|
pop hl
|
2019-05-11 08:20:43 +10:00
|
|
|
pop de
|
2019-04-17 00:17:25 +10:00
|
|
|
pop af
|
2019-04-16 06:53:11 +10:00
|
|
|
ret
|
|
|
|
|
2019-04-24 05:03:23 +10:00
|
|
|
; call IX unless it's zero
|
2019-04-23 07:15:59 +10:00
|
|
|
_blkCall:
|
|
|
|
; Before we call... is IX zero? We don't want to call a zero.
|
2019-04-16 22:36:26 +10:00
|
|
|
push af
|
2019-04-23 07:15:59 +10:00
|
|
|
xor a
|
2019-05-18 04:06:10 +10:00
|
|
|
push hl
|
|
|
|
push ix \ pop hl
|
|
|
|
cp h
|
2019-04-23 07:15:59 +10:00
|
|
|
jr nz, .ok ; not zero, ok
|
2019-05-18 04:06:10 +10:00
|
|
|
cp l
|
2019-04-23 07:15:59 +10:00
|
|
|
jr z, .error ; zero, error
|
2019-04-16 22:36:26 +10:00
|
|
|
.ok:
|
2019-05-18 04:06:10 +10:00
|
|
|
pop hl
|
2019-04-16 22:36:26 +10:00
|
|
|
pop af
|
2019-04-16 06:53:11 +10:00
|
|
|
call callIX
|
2019-04-24 05:03:23 +10:00
|
|
|
ret
|
2019-04-16 22:36:26 +10:00
|
|
|
.error:
|
2019-05-18 04:06:10 +10:00
|
|
|
pop hl
|
2019-04-16 22:36:26 +10:00
|
|
|
pop af
|
|
|
|
ld a, BLOCKDEV_ERR_UNSUPPORTED
|
2019-04-16 06:53:11 +10:00
|
|
|
ret
|
|
|
|
|
2019-04-23 04:26:16 +10: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-16 11:56:15 +10:00
|
|
|
blkGetC:
|
2019-04-24 05:03:23 +10:00
|
|
|
ld ix, (BLOCKDEV_GETC)
|
2019-04-16 11:56:15 +10:00
|
|
|
jr _blkCall
|
|
|
|
|
2019-04-23 04:26:16 +10:00
|
|
|
; Repeatedly call blkGetC until the call is a success.
|
|
|
|
blkGetCW:
|
2019-04-24 05:03:23 +10:00
|
|
|
ld ix, (BLOCKDEV_GETC)
|
2019-04-23 07:15:59 +10:00
|
|
|
.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-24 05:50:26 +10:00
|
|
|
ld ix, (BLOCKDEV_GETC)
|
|
|
|
_blkRead:
|
2019-04-23 12:28:35 +10:00
|
|
|
push hl
|
2019-04-24 09:42:10 +10:00
|
|
|
push bc
|
2019-04-23 07:15:59 +10:00
|
|
|
.loop:
|
2019-04-24 05:50:26 +10:00
|
|
|
call _blkCall
|
2019-04-23 12:28:35 +10:00
|
|
|
jr nz, .end ; Z already unset
|
2019-04-23 07:15:59 +10:00
|
|
|
ld (hl), a
|
|
|
|
inc hl
|
|
|
|
djnz .loop
|
|
|
|
cp a ; ensure Z
|
2019-04-23 12:28:35 +10:00
|
|
|
.end:
|
2019-04-24 09:42:10 +10:00
|
|
|
pop bc
|
2019-04-23 12:28:35 +10:00
|
|
|
pop hl
|
2019-04-23 04:26:16 +10:00
|
|
|
ret
|
|
|
|
|
|
|
|
; Writes character in A in current position in the selected device. Sets Z
|
|
|
|
; according to whether the operation was successful.
|
2019-04-16 06:53:11 +10:00
|
|
|
blkPutC:
|
2019-04-24 05:03:23 +10:00
|
|
|
ld ix, (BLOCKDEV_PUTC)
|
2019-04-16 11:56:15 +10:00
|
|
|
jr _blkCall
|
|
|
|
|
2019-04-24 02:04:09 +10:00
|
|
|
; Writes B chars to blkPutC from (HL).
|
|
|
|
; Sets Z if successful, unset Z if there was an error.
|
|
|
|
blkWrite:
|
2019-04-24 05:50:26 +10:00
|
|
|
ld ix, (BLOCKDEV_PUTC)
|
|
|
|
_blkWrite:
|
2019-04-24 02:04:09 +10:00
|
|
|
push hl
|
2019-04-24 09:42:10 +10:00
|
|
|
push bc
|
2019-04-24 02:04:09 +10:00
|
|
|
.loop:
|
|
|
|
ld a, (hl)
|
2019-04-24 05:50:26 +10:00
|
|
|
call _blkCall
|
2019-04-24 02:04:09 +10:00
|
|
|
jr nz, .end ; Z already unset
|
|
|
|
inc hl
|
|
|
|
djnz .loop
|
|
|
|
cp a ; ensure Z
|
|
|
|
.end:
|
2019-04-24 09:42:10 +10:00
|
|
|
pop bc
|
2019-04-24 02:04:09 +10:00
|
|
|
pop hl
|
|
|
|
ret
|
|
|
|
|
2019-04-23 12:54:23 +10: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-23 12:28:35 +10:00
|
|
|
; 3 : Move to the end
|
|
|
|
; 4 : Move to the beginning
|
2019-04-16 22:36:26 +10:00
|
|
|
; Set position of selected device to the value specified in HL
|
2019-04-24 09:42:10 +10:00
|
|
|
;
|
|
|
|
; When seeking to an out-of-bounds position, the resulting position will be
|
|
|
|
; one position ahead of the last valid position. Therefore, GetC after a seek
|
|
|
|
; to end would always fail.
|
|
|
|
;
|
|
|
|
; If the device is "growable", it's possible that seeking to end when calling
|
|
|
|
; PutC doesn't necessarily result in a failure.
|
2019-04-16 11:56:15 +10:00
|
|
|
blkSeek:
|
2019-04-24 05:50:26 +10:00
|
|
|
ld ix, (BLOCKDEV_SEEK)
|
|
|
|
ld iy, (BLOCKDEV_TELL)
|
|
|
|
_blkSeek:
|
2019-04-23 22:28:00 +10:00
|
|
|
push de
|
2019-04-23 23:37:22 +10:00
|
|
|
cp BLOCKDEV_SEEK_FORWARD
|
2019-04-23 22:28:00 +10:00
|
|
|
jr z, .forward
|
2019-04-23 23:37:22 +10:00
|
|
|
cp BLOCKDEV_SEEK_BACKWARD
|
2019-04-23 22:28:00 +10:00
|
|
|
jr z, .backward
|
2019-04-23 23:37:22 +10:00
|
|
|
cp BLOCKDEV_SEEK_BEGINNING
|
2019-04-23 22:28:00 +10:00
|
|
|
jr z, .beginning
|
2019-04-23 23:37:22 +10:00
|
|
|
cp BLOCKDEV_SEEK_END
|
2019-04-23 22:28:00 +10:00
|
|
|
jr z, .end
|
|
|
|
; all other modes are considered absolute
|
|
|
|
jr .seek ; for absolute mode, HL is already correct
|
|
|
|
.forward:
|
2019-05-18 04:06:10 +10:00
|
|
|
ex de, hl ; DE has our offset
|
2019-04-24 05:50:26 +10:00
|
|
|
; We want to be able to plug our own TELL function, which is why we
|
|
|
|
; don't call blkTell directly here.
|
|
|
|
; Calling TELL
|
|
|
|
call callIY ; HL has our curpos
|
2019-04-23 22:28:00 +10:00
|
|
|
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-05-13 03:57:37 +10:00
|
|
|
jp _blkCall
|
2019-04-16 11:56:15 +10:00
|
|
|
|
2019-04-23 07:15:59 +10:00
|
|
|
; Returns the current position of the selected device in HL.
|
|
|
|
blkTell:
|
2019-04-24 05:03:23 +10:00
|
|
|
ld ix, (BLOCKDEV_TELL)
|
2019-05-13 03:57:37 +10:00
|
|
|
jp _blkCall
|
2019-04-23 07:15:59 +10:00
|
|
|
|
2019-04-17 00:17:25 +10: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-23 07:15:59 +10:00
|
|
|
; has 4 word addresses: GetC, PutC and Seek, Tell. An entry could look like:
|
|
|
|
; .dw mmapGetC, mmapPutC, mmapSeek, mmapTell
|
2019-04-17 00:17:25 +10:00
|
|
|
blkDevTbl:
|