gunzip/neonlib: Decouple reader from buffer and introduce supplier.
4 files changed, 73 insertions(+), 61 deletions(-)

M .hgsubstate
M src/COM.asm
M src/MappedGzipLoader.asm
M src/MappedReader.asm
M .hgsubstate +2 -2
@@ 1,2 1,2 @@ 
-8b931818a05f9ce3242a560ab962956232ad401f lib/gunzip
-f5d4ac3a27c5e04578db4551e52ed6f50a111448 lib/neonlib
+88fa75ec754f5af9c147fe9418524d071722045c lib/gunzip
+a1204b887749a07200c9af22d4fbad5137923355 lib/neonlib

          
M src/COM.asm +1 -1
@@ 58,7 58,7 @@ RAM: equ RAM_PAGE1
 	INCLUDE "deflate/HuffmanCodes.asm"
 	INCLUDE "Reader.asm"
 	INCLUDE "BitReader.asm"
-	INCLUDE "FileReader.asm"
+	INCLUDE "FileSupplier.asm"
 
 	ENDS
 

          
M src/MappedGzipLoader.asm +32 -12
@@ 7,12 7,16 @@ MappedGzipLoader_BUFFER_END: equ MappedG
 MappedGzipLoader_SEGMENT_SIZE: equ 4000H
 
 MappedGzipLoader: MACRO
+	fileHandle:
+		db 0FFH
 	buffer:
 		dw 0
 	decoders:
 		dw 0
-	fileReader:
-		FileReader
+	fileSupplier:
+		FileSupplier
+	bitReader:
+		BitReader
 	gzipArchive:
 		GzipArchive
 	_size:

          
@@ 35,13 39,22 @@ MappedGzipLoader_Construct:
 	pop ix
 	ld (ix + MappedGzipLoader.decoders + 0),e
 	ld (ix + MappedGzipLoader.decoders + 1),d
+	pop de
+	ld a,00000001B  ; read-only
+	call DOS_OpenFileHandle
+	call DOS_TerminateIfError
+	ld (ix + MappedGzipLoader.fileHandle),b
+	push ix
+	call MappedGzipLoader_GetFileSupplier
+	ld de,READBUFFER
+	ld hl,READBUFFER_SIZE
+	call FileSupplier_Construct
+	ex (sp),ix
 	pop hl
 	push ix
-	call MappedGzipLoader_GetFileReader
+	call MappedGzipLoader_GetBitReader
 	ex de,hl
-	ld hl,READBUFFER
-	ld bc,READBUFFER_SIZE
-	call FileReader_Construct
+	call BitReader_Construct
 	ex (sp),ix
 	pop hl
 	push ix

          
@@ 63,10 76,9 @@ MappedGzipLoader_Destruct:
 	ld bc,Decoders._size
 	call Heap_Free
 	pop ix
-	push ix
-	call MappedGzipLoader_GetFileReader
-	call FileReader_Destruct
-	pop ix
+	ld b,(ix + MappedGzipLoader.fileHandle)
+	call DOS_CloseFileHandle
+	call DOS_TerminateIfError
 	ret
 
 ; ix = this

          
@@ 82,8 94,16 @@ MappedGzipLoader_GetBuffer:
 ; ix = this
 ; ix <- file reader
 ; Modifies: de
-MappedGzipLoader_GetFileReader:
-	ld de,MappedGzipLoader.fileReader
+MappedGzipLoader_GetFileSupplier:
+	ld de,MappedGzipLoader.fileSupplier
+	add ix,de
+	ret
+
+; ix = this
+; ix <- bit reader
+; Modifies: de
+MappedGzipLoader_GetBitReader:
+	ld de,MappedGzipLoader.bitReader
 	add ix,de
 	ret
 

          
M src/MappedReader.asm +38 -46
@@ 7,7 7,9 @@ MappedReader: MACRO
 	super: Reader
 	buffer:
 		dw 0
-	positionSegmentIndex:
+	bufferStart:
+		db 0
+	segmentIndex:
 		dw -1
 	_size:
 	ENDM

          
@@ 21,52 23,47 @@ MappedReader_Construct:
 	and a
 	call nz,System_ThrowException
 	ld a,h
-	and 3FH
+	and (MappedReader_SEGMENT_SIZE >> 8) - 1
 	call nz,System_ThrowException
-	ld (ix + MappedReader.buffer),e
+	ld (ix + MappedReader.buffer + 0),e
 	ld (ix + MappedReader.buffer + 1),d
-	ld bc,MappedReader_SEGMENT_SIZE
-	ld de,MappedReader_Fill_IY
+	ld (ix + MappedReader.bufferStart),h
+	ld de,MappedReader_Supply_IY
 	jp Reader_Construct
 
 ; iy = this
-; de <- buffer
 ; ix <- buffer
+; Modifies: bc
 MappedReader_GetBuffer_IY:
-	ld e,(iy + MappedReader.buffer)
-	ld d,(iy + MappedReader.buffer + 1)
-	ld ixl,e
-	ld ixh,d
+	ld c,(iy + MappedReader.buffer + 0)
+	ld b,(iy + MappedReader.buffer + 1)
+	ld ixl,c
+	ld ixh,b
 	ret
 
 ; dehl = address
 ; iy = this
 MappedReader_SetPosition_IY:
 	push ix
-	ld c,e
-	ld b,d
 	call MappedReader_GetBuffer_IY
-	ld e,c
-	ld d,b
 	call MappedBuffer_IsValidPosition
 	call nc,MappedReader_ThrowAddressOutOfBounds
 	ld a,h
-	and 3FH
-	or 80H
-	ld (iy + MappedReader.super.bufferPosition),l
-	ld (iy + MappedReader.super.bufferPosition + 1),a
-	ld a,h
-	rla
+	and (MappedReader_SEGMENT_SIZE >> 8) - 1
+	add a,(iy + MappedReader.bufferStart)
+	ld (iy + MappedReader.super.address + 0),l
+	ld (iy + MappedReader.super.address + 1),a
+	rl h
 	rl e
 	rl d
 	call c,MappedReader_ThrowAddressOutOfBounds
-	rla
+	rl h
 	rl e
 	rl d
 	call c,MappedReader_ThrowAddressOutOfBounds
-	ld (iy + MappedReader.positionSegmentIndex),e
-	ld (iy + MappedReader.positionSegmentIndex + 1),d
-	ld h,(iy + MappedReader.super.bufferStart)
+	ld (iy + MappedReader.segmentIndex + 0),e
+	ld (iy + MappedReader.segmentIndex + 1),d
+	ld h,a
 	call MappedBuffer_SelectSegment
 	pop ix
 	ret

          
@@ 74,13 71,13 @@ MappedReader_SetPosition_IY:
 ; iy = this
 ; dehl <- address
 MappedReader_GetPosition_IY: PROC
-	ld l,(iy + MappedReader.super.bufferPosition)
-	ld h,(iy + MappedReader.super.bufferPosition + 1)
-	ld e,(iy + MappedReader.positionSegmentIndex)
-	ld d,(iy + MappedReader.positionSegmentIndex + 1)
+	ld l,(iy + MappedReader.super.address + 0)
+	ld h,(iy + MappedReader.super.address + 1)
+	ld e,(iy + MappedReader.segmentIndex + 0)
+	ld d,(iy + MappedReader.segmentIndex + 1)
 	ld a,h
 	cp (iy + MappedReader.super.bufferEnd)
-	call nc,FillPending
+	call nc,SupplyPending
 	rlc h
 	rlc h
 	srl d

          
@@ 90,9 87,9 @@ MappedReader_GetPosition_IY: PROC
 	rr e
 	rr h
 	ret
-FillPending:
+SupplyPending:
 	ld l,0
-	ld h,(iy + MappedReader.super.bufferStart)
+	ld h,(iy + MappedReader.bufferStart)
 	inc de
 	ret
 	ENDP

          
@@ 134,32 131,27 @@ Loop:
 	jr Loop
 	ENDP
 
-; bc = byte count
 ; de = buffer start
+; hl = buffer size
 ; iy = this
 ; Modifies: af, hl
-MappedReader_Fill_IY:
-	push bc
-	push de
+MappedReader_Supply_IY:
 	call MappedReader_SelectNextSegment_IY
-	pop de
-	pop bc
+	ld e,0
+	ld d,(iy + MappedReader.bufferStart)
+	ld hl,MappedReader_SEGMENT_SIZE
 	ret
 
 ; iy = this
 MappedReader_SelectNextSegment_IY:
-	ld e,(iy + MappedReader.positionSegmentIndex)
-	ld d,(iy + MappedReader.positionSegmentIndex + 1)
+	ld e,(iy + MappedReader.segmentIndex + 0)
+	ld d,(iy + MappedReader.segmentIndex + 1)
 	inc de
-	ld (iy + MappedReader.positionSegmentIndex),e
-	ld (iy + MappedReader.positionSegmentIndex + 1),d
+	ld (iy + MappedReader.segmentIndex + 0),e
+	ld (iy + MappedReader.segmentIndex + 1),d
 	push ix
-	ld c,e
-	ld b,d
 	call MappedReader_GetBuffer_IY
-	ld h,(iy + MappedReader.super.bufferStart)
-	ld e,c
-	ld d,b
+	ld h,(iy + MappedReader.bufferStart)
 	call MappedBuffer_SelectSegment
 	pop ix
 	ret