Skip to content

Commit 4a9f8a1

Browse files
committed
Implement streamWriteBuffer
1 parent d66d223 commit 4a9f8a1

1 file changed

Lines changed: 101 additions & 1 deletion

File tree

src/00/filestreams.asm

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ getStreamBuffer:
449449
; Outputs:
450450
; Z: Set on success, reset on failure
451451
; A: Error code (on failure)
452-
; IX: File stream entry poitner (on success)
452+
; IX: File stream entry pointer (on success)
453453
getStreamEntry:
454454
push af
455455
push hl
@@ -1216,6 +1216,106 @@ streamReadWord:
12161216
inc sp \ inc sp
12171217
ret
12181218

1219+
;; streamWriteBuffer [Filestreams]
1220+
;; Writes a buffer of bytes to a file stream and advances the stream.
1221+
;; Inputs:
1222+
;; D: Stream ID
1223+
;; IX: Buffer address
1224+
;; BC: Length
1225+
;; Outputs:
1226+
;; Z: Set on success, reset on failure
1227+
;; A: Error code (on failure)
1228+
streamWriteBuffer:
1229+
push ix
1230+
push de
1231+
push hl
1232+
push af
1233+
push ix \ pop hl
1234+
call getStreamEntry
1235+
jr z, .continue
1236+
.errStreamNotFound:
1237+
pop af
1238+
pop hl
1239+
pop de
1240+
pop ix
1241+
or 1
1242+
ld a, errStreamNotFound
1243+
ret
1244+
.errNotWritable:
1245+
pop af
1246+
pop hl
1247+
pop de
1248+
pop ix
1249+
or 1
1250+
ld a, errReadOnly
1251+
ret
1252+
.continue:
1253+
bit 6, (ix + FILE_FLAGS)
1254+
jr z, .errNotWritable
1255+
.loop:
1256+
xor a
1257+
cp b \ jr nz, .write
1258+
cp c \ jr z, .done
1259+
.write:
1260+
; Note: 256 is represented with 0 in 8-bit registers here
1261+
ld a, (ix + FILE_STREAM)
1262+
neg ; Amount left in the buffer
1263+
or a
1264+
jr z, _
1265+
cp c
1266+
jr c, ++_
1267+
_: ld a, c
1268+
or a ; Handle A == 0 (i.e. 256)
1269+
jr nz, _
1270+
ld a, (ix + FILE_STREAM)
1271+
neg
1272+
_: ; A is the number of bytes to write this time around the loop
1273+
push bc
1274+
push af
1275+
push de
1276+
ld e, (ix + FILE_BUFFER)
1277+
ld d, (ix + FILE_BUFFER + 1)
1278+
ld b, 0
1279+
ld c, (ix + FILE_STREAM)
1280+
ex de, hl
1281+
add hl, bc
1282+
ex de, hl
1283+
ld c, a
1284+
or a \ jr nz, $+3 \ inc b ; 0 == 256
1285+
push bc
1286+
ldir
1287+
pop bc
1288+
res 0, (ix + FILE_WRITE_FLAGS) ; Mark as not flushed
1289+
ld a, (ix + FILE_STREAM)
1290+
add a, c
1291+
ld (ix + FILE_STREAM), a
1292+
or a ; cp 0
1293+
pop de
1294+
call z, advanceBlock
1295+
bit 5, (ix + FILE_FLAGS)
1296+
jr z, .done
1297+
ld a, (ix + FILE_WORKING_SIZE)
1298+
add c
1299+
ld (ix + FILE_WORKING_SIZE), a
1300+
jr nc, _ \ inc (ix + FILE_WORKING_SIZE + 1)
1301+
_: jr nc, _ \ inc (ix + FILE_WORKING_SIZE + 2)
1302+
_: pop af
1303+
pop bc
1304+
1305+
neg
1306+
add a, c
1307+
ld c, a
1308+
jr c, .loop
1309+
dec b
1310+
jr .loop
1311+
.done:
1312+
pop af
1313+
pop hl
1314+
pop de
1315+
pop ix
1316+
cp a
1317+
ret
1318+
12191319
;; streamReadBuffer [Filestreams]
12201320
;; Reads a number of bytes from a file stream and advances the stream.
12211321
;; Inputs:

0 commit comments

Comments
 (0)