; .-= Exomizer 2 Z80 decoder (STREAM EDITION!!) =-.

; .-= CREDITS =-.
; Compression algorithm by Magnus Lind
; Original code by Metalbrain 2007
; CPC adaptation by Tom&Jerry/GPA 2008
; Non-linear packed buffer support by Hicks/Vanity 2010
; Custom length decrunching by Hicks/Vanity 2010
; Interrupt compliancy by NoRecess/Condense 2010
; Minor bug-fixing/refactoring by NoRecess/Condense 2010
; This documentation by NoRecess/Condense 2010

; .-= VERSION DESCRIPTION =-.
; This version allows "on the fly" streaming from
; any Exomizer's compressed buffer. So you don't need anymore
; to uncompress the whole compressed buffer before using it.
; Also, you can "split" the compressed buffer at several
; memory location. By example, you could get the first part
; of compressed buffer starting at &2800, finishing at &2900,
; then gets the second (and last) part of compressed buffer
; starting at &4530 (bank &c4).

; .-= USAGE =-.
; First, create a simple table describing your compressed buffer
; Format is:
;  - 1 byte for the bank (&c0, &c4, &c5, &c6, &c7)
;  - 2 bytes for START memory location
;  - 2 bytes for END memory location
; .. then repeat this for all distinct memory locations
; WARNING! be sure to put your compressed data at right memory location
; So.. let's create a set of memory locations :
;  memLocations:
;   db &c0
;   dw &2800, &2900	
;   db &c0
;   dw &6800, &7b7f
; (here we have 2 distinct memory location, both are in bank &c0,
; the first one starting from &2800 and ending at &2900, 
; the second one starting from &6800 and ending at &7b7f)
; Now it's time to initialize Exomizer for first usage
; do this each time you need to specify a new buffer
; (or reset streaming from origin) :
;  ld a, &c0 ; default bank to set when you leave exoUnpack routine
;  ld hl, memLocations ; adress pointing to your memory locations table
;  call exoSetup
; So now your data is ready to be unpacked...
; the following will unpack the first 80 bytes from
; compressed buffer to screen!
;  ld de, &c000
;  ld bc, 80
;  call exoUnpack
; .. at exit, DE register contains next adress to write
; if you want to uncompress your buffer linearly, make sure to preserve
; DE register for next exoUnpack call!
; Then call again exoUnpack till Z flag is NON-ZERO !
; If Z flag is ZERO at exit, it means the end of compressed buffer
; has been reached!

	org &2800
incbin "data_split1.pck"

	org &6800
incbin "data_split2.pck"

	org &9000
memLocations:
	db &c0
	dw &2800, &2900	
	db &c0
	dw &6800, &7b7f

	org &a000	
	ld a, &c0
	ld hl, memLocations
	call exoSetup
	
	ld de, &c000
	
testLoop:
	;ld de, &c000
	ld bc, 80
	call exoUnpack
	jp nz, testLoop
	
	ret

; A = default bank at exit
; HL = memZones
exoSetup:
	ld ( defaultBankAtExit + 1 ), a
	ld a, ( hl )
	inc hl
	ld ( firstTimeBank + 1 ), a
	ld a, ( hl )
	inc hl
	ld d, ( hl )
	inc hl
	ld e, a
	ld ( memZoneStartPtr + 1 ), de
	ld ( memZonePtr + 1 ), hl
	xor a
	ld ( exoFirstTime + 1 ), a
	ld hl, deexo
	ld ( brkPnt+1 ), hl
	ret

; DE = destination
; BC = size of bytes to unpack
exoUnpack:		
	ld ( lngAdr ), bc
	ld ( register_de + 1 ), de
		
exoFirstTime:
	ld a, 0
	or a
	jp z, skipExoFirstTime

register_ix:	ld ix, 0
register_de:	ld de, 0
register_bc:	ld bc, 0
register_hl:	ld hl, 0

	ld iy, exo_mapbasebits
	jp endExoFirstTime
	
skipExoFirstTime:
	
firstTimeBank:
	ld a, 0
	call setBank
memZoneStartPtr:
	ld hl, 0
	
endExoFirstTime:
	ld a, 1
	ld ( exoFirstTime + 1 ), a
	
brkPnt:
	call deexo
	
	ld ( register_ix + 2 ), ix
	ld ( register_hl + 1 ), hl
	ld ( register_de + 1 ), de
	ld ( register_bc + 1 ), bc	
	ld ( brkPnt + 1 ), iy
	
defaultBankAtExit:
	ld a, 0
	call setBank

	ld a, 1
	or a
	ret	

endDep:
	pop hl

	ld a, ( defaultBankAtExit + 1 )
	call setBank

	xor a
	or a
	ret
	
deexo: 
	ld ixh, 128
	ld b, 52
  
	ld iy, exo_mapbasebits
	push de
  
exo_initbits: 
	ld a, b
	sub 4
	and 15
	jr nz,exo_node1
	ld de, 1
exo_node1:
	ld c,16
exo_get4bits: 
	call exo_getbit
	rl c
	jr nc, exo_get4bits
	ld ( iy ), c

	push hl
	inc c
	ld hl, 0
	scf
exo_setbit: 
	adc hl,hl
	dec c
	jr nz,exo_setbit
	ld ( iy + 52 ), e
	ld ( iy + 104 ), d
	add hl,de
	ex de,hl
	inc iy

	pop hl
	dec b
	jp nz, exo_initbits
	pop de

exo_literalcopy:
	ldi
	ld bc,( lngAdr )
	dec bc
	ld ( lngAdr ),bc
	ld a, b
	or c
	jr nz, exo_mainloop
	ld iy, exo_mainloop
	ret

exo_mainloop:
	call exo_getbit
	jr c, exo_literalcopy
	ld c, 255
exo_getindex: 
	inc c
	call exo_getbit
	jr nc, exo_getindex
	ld a, c
	cp 16
	jr z, endDep
	push de
	call exo_getpair
	ld ( exo_length + 1 ), bc
	ld de, 512+48
	dec bc
	ld a, b
	or c
	jr z, exo_goforit
	ld de, 1024+32
	dec bc
	ld a,b
	or c
	jr z, exo_goforit
	ld e, 16
exo_goforit: 
	call exo_getbits
	ld a, e
	add a, c
	ld c, a
	call exo_getpair
	pop de
	ld ( savUncr ), hl
	ld h, d
	ld l, e
	sbc hl,bc
exo_length:
	ld bc, 0
Label1:
	push hl
	ld hl,( lngAdr )
	or a
	sbc hl, bc
	jr z, Label2
	jr c, Label3
	ld ( lngAdr ), hl
	pop hl
	ldir
	ld hl,( savUncr )
	jr exo_mainloop
Label2:
	pop hl
	ldir
	ld hl,( savUncr )
	ld iy, exo_mainloop
    ret
Label3:
	add hl, bc
	ld a, c
	sub l
	ld c, a
	ld a, b
	sbc h
	ld b, a
	ld ( lngAdr ),bc
	ld b, h
	ld c, l
	pop hl
	ldir
	ld bc,( lngAdr )
	ld iy, Label1
	ret
exo_getpair:
	ld iyl, c
	ld d,( iy )
	call exo_getbits
	ld a, c
	add a,( iy + 52 )
	ld c, a
	ld a, b
	adc a,( iy + 104 )
	ld b, a
	ret
exo_getbits: 
	ld bc, 0
exo_gettingbits:
	dec d
	ret m
	call exo_getbit
	rl c
	rl b
	jr exo_gettingbits
exo_getbit: 
	ld a, ixh
	add a, a
	ld ixh, a
	ret nz
	
	push af
	push de	
	ex de, hl
	
memZonePtr:
	ld hl, 0
	
	ld a, ( hl )
	inc hl
	ld h, ( hl )
	ld l, a
	
	ld a, e
	cp l
	jr nz, skipNextZonePtr
	ld a, d
	cp h
	jr nz, skipNextZonePtr
	
	ld hl, ( memZonePtr + 1 )
	inc hl
	inc hl
	
	ld a, ( hl )
	inc hl
	call setBank
	
	ld a, ( hl )
	inc hl
	ld d, ( hl )
	inc hl
	ld e, a
	
	ld ( memZonePtr + 1 ), hl
		
skipNextZonePtr:
	ex de, hl
	pop de
	pop af

deb1:
	ld a,( hl )
	inc hl
	rla
	ld ixh, a
	ret
	
setBank:
	push bc
	ld bc, &7f00
	out (c), c
	out (c), a
	pop bc
	ret
	
lngAdr:
	dw 0
savUncr:
	dw 0
	
	align 256
exo_mapbasebits:
	ds 256
