MappedLoader: Move gzip decompression from Application to MappedGzipLoader.

Also rename MappedBufferLoader to MappedFileLoader and make a few tweaks.
4 files changed, 174 insertions(+), 149 deletions(-)

M src/Application.asm
M src/COM.asm
M src/MappedBufferLoader.asm => src/MappedFileLoader.asm
M src/Mapped32KWriter.asm => src/MappedGzipLoader.asm
M src/Application.asm +13 -68
@@ 11,7 11,7 @@ Application: MACRO
 	mappedBufferFactory:
 		StaticFactory MappedBuffer_instance, MappedBuffer_Construct, MappedBuffer_Destruct
 	loader:
-		MappedBufferLoader
+		MappedFileLoader
 	reader:
 		MappedReader
 	drivers:

          
@@ 27,11 27,6 @@ Application: MACRO
 	_size:
 	ENDM
 
-FileReader_class: Class FileReader, FileReader_template, Heap_main
-FileReader_template: FileReader
-GzipArchive_class: Class GzipArchive, GzipArchive_template, Heap_main
-GzipArchive_template: GzipArchive
-
 ;
 Application_Main:
 	ld ix,Mapper_instance

          
@@ 212,12 207,12 @@ Application_LoadFile:
 	push ix
 	call Application_GetLoader
 	ld de,MappedBuffer_instance
-	call MappedBufferLoader_Construct
+	call MappedFileLoader_Construct
 	push ix
-	ld hl,MappedBufferLoader_Load
+	ld hl,MappedFileLoader_Load
 	call System_TryCall
 	pop ix
-	call MappedBufferLoader_Destruct
+	call MappedFileLoader_Destruct
 	pop ix
 	jp System_Rethrow
 

          
@@ 264,74 259,24 @@ Application_IsVGZ:
 	ret
 
 ; ix = this
-Application_InflateFile: PROC
+Application_InflateFile:
 	push ix
 	call Application_GetCLI
 	call CLI_GetFileInfoBlock
-	ld e,ixl
-	ld d,ixh
-	pop ix
-	ld hl,READBUFFER
-	ld bc,READBUFFER_SIZE
-	push ix
-	call FileReader_class.New
-	call FileReader_Construct
-	push ix
-	push ix
+	ex (sp),ix
+	pop hl
 	push ix
-	ld ix,Heap_main
-	ld bc,Decoders._size
-	call Heap_Allocate
-	ld c,e
-	ld b,d
-	pop ix
-	pop hl
-	push bc
-	ld de,Mapped32KWriter_BASE_ADDRESS
-	ld a,GZIP_CRC32 ? -1 : 0
-	call GzipArchive_class.New
-	call GzipArchive_Construct
+	call MappedGzipLoader_class.New
+	ld de,MappedBuffer_instance
+	call MappedGzipLoader_Construct
 	push ix
-	call Mapped32KWriter_class.New
-	ld de,MappedBuffer_instance
-	call Mapped32KWriter_Construct
-	ld e,ixl
-	ld d,ixh
-	ex (sp),ix
-	push ix
-	ld hl,InflateLoop
+	ld hl,MappedGzipLoader_Load
 	call System_TryCall
 	pop ix
-	ex (sp),ix
-	call Mapped32KWriter_Destruct
-	call Mapped32KWriter_class.Delete
-	pop ix
-	call GzipArchive_class.Delete
-	pop de
-	push ix
-	ld ix,Heap_main
-	ld bc,Decoders._size
-	call Heap_Free
-	pop ix
-	pop ix
-	call FileReader_Destruct
-	call FileReader_class.Delete
+	call MappedGzipLoader_Destruct
+	call MappedGzipLoader_class.Delete
 	pop ix
 	jp System_Rethrow
-; de = mapped writer
-; ix = gzip archive
-InflateLoop:
-	push de
-	call GzipArchive_Inflate
-	ex (sp),ix
-	push af
-	call Mapped32KWriter_CopyToMappedBuffer
-	pop af
-	ex (sp),ix
-	pop de
-	jr nz,InflateLoop
-	ret
-	ENDP
 
 ; ix = this
 Application_PrintLoading:

          
M src/COM.asm +2 -2
@@ 44,9 44,9 @@ RAM: equ RAM_PAGE1
 	INCLUDE "CLI.asm"
 	INCLUDE "StaticFactory.asm"
 	INCLUDE "MappedBuffer.asm"
-	INCLUDE "MappedBufferLoader.asm"
+	INCLUDE "MappedFileLoader.asm"
+	INCLUDE "MappedGzipLoader.asm"
 	INCLUDE "MappedReader.asm"
-	INCLUDE "Mapped32KWriter.asm"
 
 	; gunzip
 	INCLUDE "GzipArchive.asm"

          
M src/MappedBufferLoader.asm => src/MappedFileLoader.asm +30 -30
@@ 1,59 1,59 @@ 
 ;
-; Buffers an entire file in memory
+; Loads a file into a mapped buffer
 ;
-MappedBufferLoader_PAGE_START: equ 8000H
-MappedBufferLoader_PAGE_SIZE: equ 4000H
-MappedBufferLoader_PAGE_END: equ MappedBufferLoader_PAGE_START + MappedBufferLoader_PAGE_SIZE
+MappedFileLoader_BUFFER_START: equ 8000H
+MappedFileLoader_BUFFER_SIZE: equ 4000H
+MappedFileLoader_BUFFER_END: equ MappedFileLoader_BUFFER_START + MappedFileLoader_BUFFER_SIZE
 
-MappedBufferLoader: MACRO
+MappedFileLoader: MACRO
 	fileHandle:
-		db 0
+		db 0FFH
 	buffer:
 		dw 0
 	_size:
 	ENDM
 
+; de = mapped buffer
 ; hl = file path
-; de = mapped buffer
 ; ix = this
-MappedBufferLoader_Construct:
-	ld (ix + MappedBufferLoader.buffer),e
-	ld (ix + MappedBufferLoader.buffer + 1),d
+MappedFileLoader_Construct:
+	ld (ix + MappedFileLoader.buffer + 0),e
+	ld (ix + MappedFileLoader.buffer + 1),d
 	ex de,hl
 	ld a,00000001B  ; read-only
 	call DOS_OpenFileHandle
 	call DOS_TerminateIfError
-	ld (ix + MappedBufferLoader.fileHandle),b
+	ld (ix + MappedFileLoader.fileHandle),b
 	ret
 
 ; ix = this
-MappedBufferLoader_Destruct:
-	ld b,(ix + MappedBufferLoader.fileHandle)
+MappedFileLoader_Destruct:
+	ld b,(ix + MappedFileLoader.fileHandle)
 	call DOS_CloseFileHandle
 	jp DOS_TerminateIfError
 
 ; ix = this
 ; de <- buffer
 ; ix <- buffer
-MappedBufferLoader_GetBuffer:
-	ld e,(ix + MappedBufferLoader.buffer)
-	ld d,(ix + MappedBufferLoader.buffer + 1)
+MappedFileLoader_GetBuffer:
+	ld e,(ix + MappedFileLoader.buffer + 0)
+	ld d,(ix + MappedFileLoader.buffer + 1)
 	ld ixl,e
 	ld ixh,d
 	ret
 
 ; ix = this
-MappedBufferLoader_Load: PROC
-	ld de,MappedBufferLoader_PAGE_END
+MappedFileLoader_Load: PROC
+	ld de,MappedFileLoader_BUFFER_END
 Loop:
 	ld a,d
-	cp MappedBufferLoader_PAGE_END >> 8
+	cp MappedFileLoader_BUFFER_END >> 8
 	call nc,NextSegment
 	call Load
 	push af
 	push de
 	push ix
-	call MappedBufferLoader_GetBuffer
+	call MappedFileLoader_GetBuffer
 	call MappedBuffer_IncreaseSize
 	pop ix
 	pop de

          
@@ 64,26 64,26 @@ Loop:
 	jr Loop
 NextSegment:
 	push ix
-	call MappedBufferLoader_GetBuffer
+	call MappedFileLoader_GetBuffer
 	call MappedBuffer_AllocateAndAddSegment
-	ld h,MappedBufferLoader_PAGE_START >> 8
+	ld h,MappedFileLoader_BUFFER_START >> 8
 	call MappedBuffer_SelectSegment
 	pop ix
-	ld de,MappedBufferLoader_PAGE_START
+	ld de,MappedFileLoader_BUFFER_START
 	ret
 Load:
 	push de
 	ld h,d
 	call Memory_GetSlot
 	pop de
-	ld hl,MappedBufferLoader_PAGE_END
+	ld hl,MappedFileLoader_BUFFER_END
 	and a
 	sbc hl,de
 	ld b,a
 	ld a,(Mapper_instance.primaryMapperSlot)
 	cp b
-	jr z,MappedBufferLoader_LoadDirect
-	jr MappedBufferLoader_LoadIndirect
+	jr z,MappedFileLoader_LoadDirect
+	jr MappedFileLoader_LoadIndirect
 	ENDP
 
 ; de = destination

          
@@ 92,9 92,9 @@ Load:
 ; a <- error
 ; de <- updated destination
 ; hl <- actual byte count
-MappedBufferLoader_LoadDirect:
+MappedFileLoader_LoadDirect:
 	push de
-	ld b,(ix + MappedBufferLoader.fileHandle)
+	ld b,(ix + MappedFileLoader.fileHandle)
 	call DOS_ReadFromFileHandle
 	pop de
 	add hl,de

          
@@ 109,7 109,7 @@ MappedBufferLoader_LoadDirect:
 ; a <- error
 ; de <- updated destination
 ; hl <- actual byte count
-MappedBufferLoader_LoadIndirect: PROC
+MappedFileLoader_LoadIndirect: PROC
 	push de
 	ld de,READBUFFER_SIZE
 	and a

          
@@ 119,7 119,7 @@ MappedBufferLoader_LoadIndirect: PROC
 	ex de,hl
 NoClamp:
 	ld de,READBUFFER
-	ld b,(ix + MappedBufferLoader.fileHandle)
+	ld b,(ix + MappedFileLoader.fileHandle)
 	call DOS_ReadFromFileHandle
 	pop de
 	push af

          
M src/Mapped32KWriter.asm => src/MappedGzipLoader.asm +129 -49
@@ 1,39 1,116 @@ 
 ;
-; Mapped buffer writer for gzip inflate
+; Decompresses a Gzip file into a mapped buffer
 ;
-Mapped32KWriter_BASE_ADDRESS: equ 4000H
-Mapped32KWriter_SEGMENT_SIZE: equ 4000H
-Mapped32KWriter_BUFFER_SIZE: equ 8000H
+MappedGzipLoader_BUFFER_START: equ 4000H
+MappedGzipLoader_BUFFER_SIZE: equ 8000H
+MappedGzipLoader_BUFFER_END: equ MappedGzipLoader_BUFFER_START + MappedGzipLoader_BUFFER_SIZE
+MappedGzipLoader_SEGMENT_SIZE: equ 4000H
 
-Mapped32KWriter: MACRO
+MappedGzipLoader: MACRO
 	buffer:
 		dw 0
-	originalPage1: MapperSegment
-	originalPage2: MapperSegment
+	decoders:
+		dw 0
+	fileReader:
+		FileReader
+	gzipArchive:
+		GzipArchive
 	_size:
 	ENDM
 
-Mapped32KWriter_class: Class Mapped32KWriter, Mapped32KWriter_template, Heap_main
-Mapped32KWriter_template: Mapped32KWriter
+MappedGzipLoader_class: Class MappedGzipLoader, MappedGzipLoader_template, Heap_main
+MappedGzipLoader_template: MappedGzipLoader
 
 ; de = mapped buffer
+; hl = file path
 ; ix = this
-; ix <- this
-Mapped32KWriter_Construct:
-	ld (ix + Mapped32KWriter.buffer),e
-	ld (ix + Mapped32KWriter.buffer + 1),d
+MappedGzipLoader_Construct:
+	ld (ix + MappedGzipLoader.buffer + 0),e
+	ld (ix + MappedGzipLoader.buffer + 1),d
+	push hl
+	push ix
+	ld ix,Heap_main
+	ld bc,Decoders._size
+	call Heap_Allocate
+	pop ix
+	ld (ix + MappedGzipLoader.decoders + 0),e
+	ld (ix + MappedGzipLoader.decoders + 1),d
+	pop hl
+	push ix
+	call MappedGzipLoader_GetFileReader
+	ex de,hl
+	ld hl,READBUFFER
+	ld bc,READBUFFER_SIZE
+	call FileReader_Construct
+	ex (sp),ix
+	pop hl
+	push ix
+	ld c,(ix + MappedGzipLoader.decoders + 0)
+	ld b,(ix + MappedGzipLoader.decoders + 1)
+	call MappedGzipLoader_GetGzipArchive
+	ld de,MappedGzipLoader_BUFFER_START
+	ld a,GZIP_CRC32 ? -1 : 0
+	call GzipArchive_Construct
+	pop ix
+	ret
+
+; ix = this
+MappedGzipLoader_Destruct:
+	ld e,(ix + MappedGzipLoader.decoders + 0)
+	ld d,(ix + MappedGzipLoader.decoders + 1)
+	push ix
+	ld ix,Heap_main
+	ld bc,Decoders._size
+	call Heap_Free
+	pop ix
+	push ix
+	call MappedGzipLoader_GetFileReader
+	call FileReader_Destruct
+	pop ix
+	ret
+
+; ix = this
+; ix <- buffer
+; Modifies: de
+MappedGzipLoader_GetBuffer:
+	ld e,(ix + MappedGzipLoader.buffer + 0)
+	ld d,(ix + MappedGzipLoader.buffer + 1)
+	ld ixl,e
+	ld ixh,d
+	ret
+
+; ix = this
+; ix <- file reader
+; Modifies: de
+MappedGzipLoader_GetFileReader:
+	ld de,MappedGzipLoader.fileReader
+	add ix,de
+	ret
+
+; ix = this
+; ix <- gzip archive
+; Modifies: de
+MappedGzipLoader_GetGzipArchive:
+	ld de,MappedGzipLoader.gzipArchive
+	add ix,de
+	ret
+
+; ix = this
+MappedGzipLoader_Load:
 	ld h,40H
-	call Mapper_instance.GetPH
-	ld (ix + Mapped32KWriter.originalPage1.segment),a
 	call Memory_GetSlot
-	ld (ix + Mapped32KWriter.originalPage1.slot),a
-	ld h,80H
+	ld b,a
 	call Mapper_instance.GetPH
-	ld (ix + Mapped32KWriter.originalPage2.segment),a
+	ld c,a
+	push bc
+	ld h,80H
 	call Memory_GetSlot
-	ld (ix + Mapped32KWriter.originalPage2.slot),a
+	ld b,a
+	call Mapper_instance.GetPH
+	ld c,a
+	push bc
 	push ix
-	call Mapped32KWriter_GetBuffer
+	call MappedGzipLoader_GetBuffer
 	call MappedBuffer_AllocateAndAddSegment
 	ld h,40H
 	call MappedBuffer_SelectSegment

          
@@ 41,63 118,66 @@ Mapped32KWriter_Construct:
 	ld h,80H
 	call MappedBuffer_SelectSegment
 	pop ix
-	ret
+	ld hl,MappedGzipLoader_Inflate
+	call System_TryCall
+	pop bc
+	ld h,80H
+	ld a,c
+	call Mapper_instance.PutPH
+	ld a,b
+	call Memory_SetSlot
+	pop bc
+	ld h,40H
+	ld a,c
+	call Mapper_instance.PutPH
+	ld a,b
+	call Memory_SetSlot
+	jp System_Rethrow
 
 ; ix = this
-Mapped32KWriter_Destruct:
-	ld h,40H
-	ld a,(ix + Mapped32KWriter.originalPage1.segment)
-	call Mapper_instance.PutPH
-	ld a,(ix + Mapped32KWriter.originalPage1.slot)
-	call Memory_SetSlot
-	ld h,80H
-	ld a,(ix + Mapped32KWriter.originalPage2.segment)
-	call Mapper_instance.PutPH
-	ld a,(ix + Mapped32KWriter.originalPage2.slot)
-	jp Memory_SetSlot
-
-; ix = this
-; de <- buffer
-; ix <- buffer
-Mapped32KWriter_GetBuffer:
-	ld e,(ix + Mapped32KWriter.buffer)
-	ld d,(ix + Mapped32KWriter.buffer + 1)
-	ld ixl,e
-	ld ixh,d
+MappedGzipLoader_Inflate:
+	push ix
+	call MappedGzipLoader_GetGzipArchive
+	call GzipArchive_Inflate
+	pop ix
+	push af
+	call MappedGzipLoader_CopyToMappedBuffer
+	pop af
+	jr nz,MappedGzipLoader_Inflate
 	ret
 
 ; hl = byte count
 ; de = buffer start
 ; ix = this
-Mapped32KWriter_CopyToMappedBuffer: PROC
+MappedGzipLoader_CopyToMappedBuffer: PROC
 	push hl
 	push de
 	push ix
-	call Mapped32KWriter_GetBuffer
+	call MappedGzipLoader_GetBuffer
 	call MappedBuffer_IncreaseSize
 	pop ix
 	pop de
 	pop bc
-	ld hl,Mapped32KWriter_BASE_ADDRESS + Mapped32KWriter_BUFFER_SIZE
+	ld hl,MappedGzipLoader_BUFFER_END
 	and a
 	sbc hl,de
 	call c,System_ThrowException
 	sbc hl,bc
 	call c,System_ThrowException
 	ret nz
-	ld hl,Mapped32KWriter_BASE_ADDRESS
+	ld hl,MappedGzipLoader_BUFFER_START
 	sbc hl,de
 	call nz,System_ThrowException
 Loop:
-	ld hl,-Mapped32KWriter_SEGMENT_SIZE
+	ld hl,-MappedGzipLoader_SEGMENT_SIZE
 	add hl,bc
 	push hl
 	jr nc,NoCap
-	ld bc,Mapped32KWriter_SEGMENT_SIZE
+	ld bc,MappedGzipLoader_SEGMENT_SIZE
 NoCap:
 	push bc
 	push ix
-	call Mapped32KWriter_GetBuffer
+	call MappedGzipLoader_GetBuffer
 	call MappedBuffer_AllocateAndAddSegment
 	ld h,80H
 	call MappedBuffer_SelectSegment

          
@@ 107,7 187,7 @@ NoCap:
 	ld de,8000H
 	call System_FastLDIR
 	push ix
-	call Mapped32KWriter_GetBuffer
+	call MappedGzipLoader_GetBuffer
 	call MappedBuffer_GetSegmentCount
 	dec de
 	dec de