2019-04-16 06:53:11 +10:00
|
|
|
; blockdev
|
|
|
|
;
|
|
|
|
; A block device is an abstraction over something we can read from, write to.
|
|
|
|
;
|
2019-10-31 07:59:35 +11:00
|
|
|
; A device that fits this abstraction puts the proper 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.
|
2019-04-16 06:53:11 +10:00
|
|
|
;
|
2019-10-31 07:59:35 +11:00
|
|
|
; This module exposes a seek/tell/getb/putb API that is then re-routed to
|
2019-06-05 01:53:02 +10:00
|
|
|
; underlying drivers. There will eventually be more than one driver type, but
|
|
|
|
; for now we sit on only one type of driver: random access driver.
|
2019-05-13 01:45:08 +10:00
|
|
|
;
|
2019-06-05 01:53:02 +10:00
|
|
|
; *** Random access drivers ***
|
2019-05-13 01:45:08 +10:00
|
|
|
;
|
2019-10-31 07:59:35 +11:00
|
|
|
; Random access drivers are expected to supply two routines: GetB and PutB.
|
2019-05-13 01:45:08 +10:00
|
|
|
;
|
2019-10-31 07:59:35 +11:00
|
|
|
; GetB:
|
|
|
|
; Reads one byte at address specified in DE/HL and returns its value in A.
|
2019-05-13 01:45:08 +10:00
|
|
|
; Sets Z according to whether read was successful: Set if successful, unset
|
|
|
|
; if not.
|
|
|
|
;
|
2019-06-05 01:53:02 +10:00
|
|
|
; Unsuccessful reads generally mean that requested addr is out of bounds (we
|
|
|
|
; reached EOF).
|
2019-05-13 01:45:08 +10:00
|
|
|
;
|
2019-10-31 07:59:35 +11:00
|
|
|
; PutB:
|
|
|
|
; Writes byte in A at address specified in DE/HL. Sets Z according to whether
|
|
|
|
; the operation was successful.
|
2019-05-13 01:45:08 +10:00
|
|
|
;
|
2019-06-05 01:53:02 +10:00
|
|
|
; Unsuccessful writes generally mean that we're out of bounds for writing.
|
2019-05-13 03:57:37 +10:00
|
|
|
;
|
2019-06-18 03:42:53 +10:00
|
|
|
; All routines are expected to preserve unused registers except IX which is
|
2019-10-31 07:59:35 +11:00
|
|
|
; explicitly protected during GetB/PutB calls. This makes quick "handle+jump"
|
2019-06-18 03:42:53 +10:00
|
|
|
; definitions possible.
|
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 ***
|
|
|
|
; *** VARIABLES ***
|
2019-04-23 07:15:59 +10:00
|
|
|
; Pointer to the selected block device. A block device is a 8 bytes block of
|
2019-10-31 07:59:35 +11:00
|
|
|
; memory with pointers to GetB, PutB, and a 32-bit counter, in that order.
|
2019-06-04 23:56:36 +10:00
|
|
|
.equ BLOCKDEV_SEL BLOCKDEV_RAMSTART
|
2019-10-31 07:59:35 +11:00
|
|
|
.equ BLOCKDEV_RAMEND @+BLOCKDEV_SIZE
|
2019-04-16 06:53:11 +10:00
|
|
|
|
|
|
|
; *** CODE ***
|
2019-11-16 07:37:49 +11:00
|
|
|
; Put the pointer to the "regular" blkdev selection in DE
|
|
|
|
blkSelPtr:
|
|
|
|
ld de, BLOCKDEV_SEL
|
|
|
|
|
2019-05-11 08:20:43 +10:00
|
|
|
; Select block index specified in A and place them in routine pointers at (DE).
|
2019-06-04 23:56:36 +10:00
|
|
|
; For example, for a "regular" blkSel, you will want to set DE to BLOCKDEV_SEL.
|
2019-11-16 07:37:49 +11:00
|
|
|
; Sets Z on success, reset on error.
|
|
|
|
; If A >= BLOCKDEV_COUNT, it's an error.
|
2019-04-17 00:17:25 +10:00
|
|
|
blkSel:
|
2019-11-16 07:37:49 +11:00
|
|
|
cp BLOCKDEV_COUNT
|
|
|
|
jp nc, unsetZ ; if selection >= device count, error
|
2019-04-17 00:17:25 +10:00
|
|
|
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-06-05 10:45:01 +10:00
|
|
|
jr z, .end ; index is zero? don't loop
|
2019-06-05 01:53:02 +10:00
|
|
|
push bc ; <|
|
|
|
|
ld b, a ; |
|
|
|
|
.loop: ; |
|
|
|
|
ld a, 4 ; |
|
|
|
|
call addHL ; |
|
|
|
|
djnz .loop ; |
|
|
|
|
pop bc ; <|
|
2019-06-05 10:45:01 +10:00
|
|
|
.end:
|
|
|
|
call blkSet
|
|
|
|
pop hl
|
|
|
|
pop de
|
|
|
|
pop af
|
2019-11-16 07:37:49 +11:00
|
|
|
cp a ; ensure Z
|
2019-06-05 10:45:01 +10:00
|
|
|
ret
|
|
|
|
|
|
|
|
; Setup blkdev handle in (DE) using routines at (HL).
|
|
|
|
blkSet:
|
|
|
|
push af
|
|
|
|
push de
|
|
|
|
push hl
|
2019-12-14 01:56:23 +11:00
|
|
|
push bc
|
2019-06-05 10:45:01 +10:00
|
|
|
|
2019-12-14 01:56:23 +11:00
|
|
|
ld bc, 4
|
|
|
|
ldir
|
2019-06-05 01:53:02 +10:00
|
|
|
; Initialize pos
|
2019-12-14 01:56:23 +11:00
|
|
|
ld b, 4
|
2019-06-05 01:53:02 +10:00
|
|
|
xor a
|
2019-12-14 01:56:23 +11:00
|
|
|
ex de, hl
|
|
|
|
call fill
|
2019-05-13 03:57:37 +10:00
|
|
|
|
2019-12-14 01:56:23 +11:00
|
|
|
pop bc
|
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-06-05 01:53:02 +10:00
|
|
|
_blkInc:
|
|
|
|
ret nz ; don't advance when in error condition
|
|
|
|
push af
|
|
|
|
push hl
|
|
|
|
ld a, BLOCKDEV_SEEK_FORWARD
|
|
|
|
ld hl, 1
|
|
|
|
call _blkSeek
|
|
|
|
pop hl
|
|
|
|
pop af
|
|
|
|
ret
|
|
|
|
|
2019-10-31 07:59:35 +11:00
|
|
|
; Reads one byte from selected device and returns its value in A.
|
2019-04-23 04:26:16 +10:00
|
|
|
; Sets Z according to whether read was successful: Set if successful, unset
|
|
|
|
; if not.
|
2019-10-31 07:59:35 +11:00
|
|
|
blkGetB:
|
2019-07-15 02:17:13 +10:00
|
|
|
push ix
|
2019-06-05 01:53:02 +10:00
|
|
|
ld ix, BLOCKDEV_SEL
|
2019-10-31 07:59:35 +11:00
|
|
|
call _blkGetB
|
2019-07-15 02:17:13 +10:00
|
|
|
pop ix
|
|
|
|
ret
|
2019-10-31 07:59:35 +11:00
|
|
|
_blkGetB:
|
2019-06-05 01:53:02 +10:00
|
|
|
push hl
|
|
|
|
push de
|
|
|
|
call _blkTell
|
|
|
|
call callIXI
|
|
|
|
pop de
|
|
|
|
pop hl
|
|
|
|
jr _blkInc ; advance and return
|
|
|
|
|
2019-10-31 07:59:35 +11:00
|
|
|
; Writes byte in A in current position in the selected device. Sets Z according
|
|
|
|
; to whether the operation was successful.
|
|
|
|
blkPutB:
|
2019-07-15 02:17:13 +10:00
|
|
|
push ix
|
2019-06-05 01:53:02 +10:00
|
|
|
ld ix, BLOCKDEV_SEL
|
2019-10-31 07:59:35 +11:00
|
|
|
call _blkPutB
|
2019-07-15 02:17:13 +10:00
|
|
|
pop ix
|
|
|
|
ret
|
2019-10-31 07:59:35 +11:00
|
|
|
_blkPutB:
|
2019-06-05 01:53:02 +10:00
|
|
|
push ix
|
|
|
|
push hl
|
|
|
|
push de
|
|
|
|
call _blkTell
|
2019-10-31 07:59:35 +11:00
|
|
|
inc ix ; make IX point to PutB
|
2019-06-05 01:53:02 +10:00
|
|
|
inc ix
|
|
|
|
call callIXI
|
|
|
|
pop de
|
|
|
|
pop hl
|
|
|
|
pop ix
|
|
|
|
jr _blkInc ; advance and return
|
2019-04-16 11:56:15 +10:00
|
|
|
|
2019-10-31 07:59:35 +11:00
|
|
|
; Reads B chars from blkGetB and copy them in (HL).
|
2019-04-23 07:15:59 +10:00
|
|
|
; Sets Z if successful, unset Z if there was an error.
|
|
|
|
blkRead:
|
2019-07-15 02:17:13 +10:00
|
|
|
push ix
|
2019-06-05 01:53:02 +10:00
|
|
|
ld ix, BLOCKDEV_SEL
|
2019-07-15 02:17:13 +10:00
|
|
|
call _blkRead
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-24 05:50:26 +10:00
|
|
|
_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-10-31 07:59:35 +11:00
|
|
|
call _blkGetB
|
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
|
|
|
|
|
2019-10-31 07:59:35 +11:00
|
|
|
; Writes B chars to blkPutB from (HL).
|
2019-04-24 02:04:09 +10:00
|
|
|
; Sets Z if successful, unset Z if there was an error.
|
|
|
|
blkWrite:
|
2019-07-15 02:17:13 +10:00
|
|
|
push ix
|
2019-06-05 01:53:02 +10:00
|
|
|
ld ix, BLOCKDEV_SEL
|
2019-07-15 02:17:13 +10:00
|
|
|
call _blkWrite
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-24 05:50:26 +10:00
|
|
|
_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-10-31 07:59:35 +11:00
|
|
|
call _blkPutB
|
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:
|
2019-05-29 05:56:39 +10:00
|
|
|
; 0 : Move exactly to X, X being the HL/DE argument.
|
|
|
|
; 1 : Move forward by X bytes, X being the HL argument (no DE)
|
|
|
|
; 2 : Move backwards by X bytes, X being the HL argument (no DE)
|
2019-04-23 12:28:35 +10:00
|
|
|
; 3 : Move to the end
|
|
|
|
; 4 : Move to the beginning
|
2019-05-29 05:56:39 +10:00
|
|
|
|
|
|
|
; Set position of selected device to the value specified in HL (low) and DE
|
|
|
|
; (high). DE is only used for mode 0.
|
2019-04-24 09:42:10 +10:00
|
|
|
;
|
|
|
|
; When seeking to an out-of-bounds position, the resulting position will be
|
2019-10-31 07:59:35 +11:00
|
|
|
; one position ahead of the last valid position. Therefore, GetB after a seek
|
2019-04-24 09:42:10 +10:00
|
|
|
; to end would always fail.
|
|
|
|
;
|
|
|
|
; If the device is "growable", it's possible that seeking to end when calling
|
2019-10-31 07:59:35 +11:00
|
|
|
; PutB doesn't necessarily result in a failure.
|
2019-04-16 11:56:15 +10:00
|
|
|
blkSeek:
|
2019-07-15 02:17:13 +10:00
|
|
|
push ix
|
2019-06-05 01:53:02 +10:00
|
|
|
ld ix, BLOCKDEV_SEL
|
2019-07-15 02:17:13 +10:00
|
|
|
call _blkSeek
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-04-24 05:50:26 +10:00
|
|
|
_blkSeek:
|
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
|
2019-06-05 01:53:02 +10:00
|
|
|
ld (ix+4), e
|
|
|
|
ld (ix+5), d
|
|
|
|
ld (ix+6), l
|
|
|
|
ld (ix+7), h
|
|
|
|
ret
|
2019-04-23 22:28:00 +10:00
|
|
|
.forward:
|
2019-06-05 01:53:02 +10:00
|
|
|
push bc ; <-|
|
|
|
|
push hl ; <||
|
|
|
|
ld l, (ix+6) ; || low byte
|
|
|
|
ld h, (ix+7) ; ||
|
|
|
|
pop bc ; <||
|
|
|
|
add hl, bc ; |
|
|
|
|
pop bc ; <-|
|
|
|
|
ld (ix+6), l
|
|
|
|
ld (ix+7), h
|
|
|
|
ret nc ; no carry? no need to adjust high byte
|
|
|
|
; carry, adjust high byte
|
|
|
|
inc (ix+4)
|
|
|
|
ret nz
|
|
|
|
inc (ix+5)
|
|
|
|
ret
|
2019-04-23 22:28:00 +10:00
|
|
|
.backward:
|
2019-06-05 01:53:02 +10:00
|
|
|
and a ; clear carry
|
|
|
|
push bc ; <-|
|
|
|
|
push hl ; <||
|
|
|
|
ld l, (ix+6) ; || low byte
|
|
|
|
ld h, (ix+7) ; ||
|
|
|
|
pop bc ; <||
|
|
|
|
sbc hl, bc ; |
|
|
|
|
pop bc ; <-|
|
|
|
|
ld (ix+6), l
|
|
|
|
ld (ix+7), h
|
|
|
|
ret nc ; no carry? no need to adjust high byte
|
|
|
|
ld a, 0xff
|
|
|
|
dec (ix+4)
|
|
|
|
cp (ix+4)
|
|
|
|
ret nz
|
|
|
|
; we decremented from 0
|
|
|
|
dec (ix+5)
|
|
|
|
ret
|
2019-04-23 22:28:00 +10:00
|
|
|
.beginning:
|
2019-06-05 01:53:02 +10:00
|
|
|
xor a
|
|
|
|
ld (ix+4), a
|
|
|
|
ld (ix+5), a
|
|
|
|
ld (ix+6), a
|
|
|
|
ld (ix+7), a
|
|
|
|
ret
|
2019-04-23 22:28:00 +10:00
|
|
|
.end:
|
2019-06-05 01:53:02 +10:00
|
|
|
ld a, 0xff
|
|
|
|
ld (ix+4), a
|
|
|
|
ld (ix+5), a
|
|
|
|
ld (ix+6), a
|
|
|
|
ld (ix+7), a
|
2019-05-29 05:56:39 +10:00
|
|
|
ret
|
2019-04-16 11:56:15 +10:00
|
|
|
|
2019-05-29 05:56:39 +10:00
|
|
|
; Returns the current position of the selected device in HL (low) and DE (high).
|
2019-04-23 07:15:59 +10:00
|
|
|
blkTell:
|
2019-07-15 02:17:13 +10:00
|
|
|
push ix
|
2019-06-05 01:53:02 +10:00
|
|
|
ld ix, BLOCKDEV_SEL
|
2019-07-15 02:17:13 +10:00
|
|
|
call _blkTell
|
|
|
|
pop ix
|
|
|
|
ret
|
2019-06-05 01:53:02 +10:00
|
|
|
_blkTell:
|
|
|
|
ld e, (ix+4)
|
|
|
|
ld d, (ix+5)
|
|
|
|
ld l, (ix+6)
|
|
|
|
ld h, (ix+7)
|
|
|
|
ret
|
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-10-31 07:59:35 +11:00
|
|
|
; has 2 word addresses: GetB and PutB. An entry could look like:
|
|
|
|
; .dw mmapGetB, mmapPutB
|
2019-04-17 00:17:25 +10:00
|
|
|
blkDevTbl:
|