summaryrefslogtreecommitdiff
path: root/tools/rasm/decrunch/aplib_z80_todo.asm
blob: 6843a149c27e5c5cae740d944e0cf2cc5965d2fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
;Z80 Version by Dan Weiss
;Call depack.
;hl = source
;de = dest

ap_bits: .db 0
ap_byte: .db 0
lwm:	.db 0
r0:	.dw 0

ap_getbit:
	push bc
		ld bc,(ap_bits)
		rrc c
		jr nc,ap_getbit_continue
		ld b,(hl)
		inc hl
ap_getbit_continue:
		ld a,c
		and b
		ld (ap_bits),bc
	pop bc
	ret

ap_getbitbc: ;doubles BC and adds the read bit
	sla c
	rl b
	call ap_getbit
	ret z
	inc bc
	ret

ap_getgamma:
	ld bc,1
ap_getgammaloop:
	call ap_getbitbc
	call ap_getbit
	jr nz,ap_getgammaloop
	ret


depack:
	;hl = source
	;de = dest
	ldi
	xor a
	ld (lwm),a
	inc a
	ld (ap_bits),a
	
aploop:
	call ap_getbit
	jp z, apbranch1
	call ap_getbit
	jr z, apbranch2
	call ap_getbit
	jr z, apbranch3
	;LWM = 0
	xor a
	ld (lwm),a
	;get an offset
	ld bc,0
	call ap_getbitbc
	call ap_getbitbc
	call ap_getbitbc
	call ap_getbitbc
	ld a,b
	or c
	jr nz,apbranch4
	xor a  ;write a 0
	ld (de),a
	inc de
	jr aploop
apbranch4:
	ex de,hl ;write a previous bit (1-15 away from dest)
	push hl
		sbc hl,bc
		ld a,(hl)
	pop hl
	ld (hl),a
	inc hl
	ex de,hl
	jr aploop
apbranch3:
	;use 7 bit offset, length = 2 or 3
	;if a zero is encountered here, it's EOF
	ld c,(hl)
	inc hl
	rr c
	ret z
	ld b,2
	jr nc,ap_dont_inc_b
	inc b
ap_dont_inc_b:
	;LWM = 1
	ld a,1
	ld (lwm),a
	
	push hl
		ld a,b
		ld b,0
		;R0 = c
		ld (r0),bc
		ld h,d
		ld l,e
		or a
		sbc hl,bc
		ld c,a
		ldir
	pop hl
	jr aploop
apbranch2:
	;use a gamma code * 256 for offset, another gamma code for length
	call ap_getgamma
	dec bc
	dec bc
	ld a,(lwm)
	or a
	jr nz,ap_not_lwm
	;bc = 2?
	ld a,b
	or c
	jr nz,ap_not_zero_gamma
	;if gamma code is 2, use old r0 offset, and a new gamma code for length
	call ap_getgamma
	push hl
		ld h,d
		ld l,e
		push bc
			ld bc,(r0)
			sbc hl,bc
		pop bc
		ldir
	pop hl
	jr ap_finishup
	
ap_not_zero_gamma:
	dec bc
ap_not_lwm:
	;do I even need this code?
	;bc=bc*256+(hl), lazy 16bit way
	ld b,c
	ld c,(hl)
	inc hl
	ld (r0),bc
	push bc
		call ap_getgamma
		ex (sp),hl
		;bc = len, hl=offs
		push de
			ex de,hl
			;some comparison junk for some reason
			ld hl,31999
			or a
			sbc hl,de
			jr nc,skip1
			inc bc
skip1:
			ld hl,1279
			or a
			sbc hl,de
			jr nc,skip2
			inc bc
skip2:
			ld hl,127
			or a
			sbc hl,de
			jr c,skip3
			inc bc
			inc bc
skip3:
			;bc = len, de = offs, hl=junk
		pop hl
		push hl
			or a
			sbc hl,de
		pop de
		;hl=dest-offs, bc=len, de = dest
		ldir
	pop hl
ap_finishup:
	ld a,1
	ld (lwm),a
	jp aploop

apbranch1:
	ldi
	xor a
	ld (lwm),a
	jp aploop