# HG changeset patch # User Laurens Holst # Date 1733588761 -3600 # Sat Dec 07 17:26:01 2024 +0100 # Node ID b334dc831dba08d42ba48ca49befde5eb81ce4ce # Parent dea6442dccd4c6513528111b0729a31a8187c1d7 gunzip/neonlib: Decouple reader from buffer and introduce supplier. diff --git a/.hgsubstate b/.hgsubstate --- a/.hgsubstate +++ b/.hgsubstate @@ -1,2 +1,2 @@ -8b931818a05f9ce3242a560ab962956232ad401f lib/gunzip -f5d4ac3a27c5e04578db4551e52ed6f50a111448 lib/neonlib +88fa75ec754f5af9c147fe9418524d071722045c lib/gunzip +a1204b887749a07200c9af22d4fbad5137923355 lib/neonlib diff --git a/src/COM.asm b/src/COM.asm --- a/src/COM.asm +++ b/src/COM.asm @@ -58,7 +58,7 @@ INCLUDE "deflate/HuffmanCodes.asm" INCLUDE "Reader.asm" INCLUDE "BitReader.asm" - INCLUDE "FileReader.asm" + INCLUDE "FileSupplier.asm" ENDS diff --git a/src/MappedGzipLoader.asm b/src/MappedGzipLoader.asm --- a/src/MappedGzipLoader.asm +++ b/src/MappedGzipLoader.asm @@ -7,12 +7,16 @@ 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 @@ 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 @@ 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 @@ ; 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 diff --git a/src/MappedReader.asm b/src/MappedReader.asm --- a/src/MappedReader.asm +++ b/src/MappedReader.asm @@ -7,7 +7,9 @@ super: Reader buffer: dw 0 - positionSegmentIndex: + bufferStart: + db 0 + segmentIndex: dw -1 _size: ENDM @@ -21,52 +23,47 @@ 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 @@ ; 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 @@ 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 @@ 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