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.
|
|
|
|
;
|
2019-06-05 01:53:02 +10:00
|
|
|
; This module exposes a seek/tell/getc/putc API that is then re-routed to
|
|
|
|
; 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-06-05 01:53:02 +10:00
|
|
|
; Random access drivers are expected to supply two routines: GetC and PutC.
|
2019-05-13 01:45:08 +10:00
|
|
|
;
|
|
|
|
; GetC:
|
2019-06-05 01:53:02 +10:00
|
|
|
; Reads one character 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
|
|
|
;
|
|
|
|
; PutC:
|
2019-06-05 01:53:02 +10:00
|
|
|
; Writes character 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
|
|
|
;
|
|
|
|
; 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_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-06-05 01:53:02 +10:00
|
|
|
.equ BLOCKDEV_SIZE 8
|
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
|
2019-06-05 01:53:02 +10:00
|
|
|
; memory with pointers to GetC, PutC, and a 32-bit counter, in that order.
|
2019-06-04 23:56:36 +10:00
|
|
|
.equ BLOCKDEV_SEL BLOCKDEV_RAMSTART
|
2019-06-05 01:53:02 +10:00
|
|
|
.equ BLOCKDEV_RAMEND BLOCKDEV_SEL+BLOCKDEV_SIZE
|
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).
|
2019-06-04 23:56:36 +10:00
|
|
|
; For example, for a "regular" blkSel, you will want to set DE to BLOCKDEV_SEL.
|
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-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
|
|
|
|
ret
|
|
|
|
|
|
|
|
; Setup blkdev handle in (DE) using routines at (HL).
|
|
|
|
blkSet:
|
|
|
|
push af
|
|
|
|
push de
|
|
|
|
push hl
|
|
|
|
|
2019-06-05 01:53:02 +10:00
|
|
|
; Write GETC
|
|
|
|
push hl ; <|
|
|
|
|
call intoHL ; |
|
|
|
|
call writeHLinDE ; |
|
|
|
|
inc de ; |
|
|
|
|
inc de ; |
|
|
|
|
pop hl ; <|
|
2019-04-24 05:03:23 +10:00
|
|
|
inc hl
|
|
|
|
inc hl
|
2019-06-05 01:53:02 +10:00
|
|
|
; Write PUTC
|
2019-04-24 05:03:23 +10:00
|
|
|
call intoHL
|
2019-05-11 08:20:43 +10:00
|
|
|
call writeHLinDE
|
2019-06-05 01:53:02 +10:00
|
|
|
inc de
|
|
|
|
inc de
|
|
|
|
; Initialize pos
|
|
|
|
xor a
|
|
|
|
ld (de), a
|
|
|
|
inc de
|
|
|
|
ld (de), a
|
|
|
|
inc de
|
|
|
|
ld (de), a
|
|
|
|
inc de
|
|
|
|
ld (de), a
|
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-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-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-06-05 01:53:02 +10:00
|
|
|
ld ix, BLOCKDEV_SEL
|
|
|
|
_blkGetC:
|
|
|
|
push hl
|
|
|
|
push de
|
|
|
|
call _blkTell
|
|
|
|
call callIXI
|
|
|
|
pop de
|
|
|
|
pop hl
|
|
|
|
jr _blkInc ; advance and return
|
|
|
|
|
|
|
|
; Writes character in A in current position in the selected device. Sets Z
|
|
|
|
; according to whether the operation was successful.
|
|
|
|
blkPutC:
|
|
|
|
ld ix, BLOCKDEV_SEL
|
|
|
|
_blkPutC:
|
|
|
|
push ix
|
|
|
|
push hl
|
|
|
|
push de
|
|
|
|
call _blkTell
|
|
|
|
inc ix ; make IX point to PutC
|
|
|
|
inc ix
|
|
|
|
call callIXI
|
|
|
|
pop de
|
|
|
|
pop hl
|
|
|
|
pop ix
|
|
|
|
jr _blkInc ; advance and return
|
2019-04-16 11:56:15 +10:00
|
|
|
|
2019-04-23 07:15:59 +10:00
|
|
|
; Reads B chars from blkGetC and copy them in (HL).
|
|
|
|
; Sets Z if successful, unset Z if there was an error.
|
|
|
|
blkRead:
|
2019-06-05 01:53:02 +10:00
|
|
|
ld ix, BLOCKDEV_SEL
|
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-06-05 01:53:02 +10:00
|
|
|
call _blkGetC
|
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-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-06-05 01:53:02 +10:00
|
|
|
ld ix, BLOCKDEV_SEL
|
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-06-05 01:53:02 +10:00
|
|
|
call _blkPutC
|
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
|
|
|
|
; 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-06-05 01:53:02 +10:00
|
|
|
ld ix, BLOCKDEV_SEL
|
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-06-05 01:53:02 +10:00
|
|
|
ld ix, BLOCKDEV_SEL
|
|
|
|
_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-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:
|