-
Notifications
You must be signed in to change notification settings - Fork 2
/
aplib247b.asm
195 lines (179 loc) · 5.93 KB
/
aplib247b.asm
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
191
192
193
194
195
; aPPack decompressor
; original source by dwedit
; very slightly adapted by utopian
; optimized by Metalbrain & Antonio Villena
;247b
;hl = source
;de = dest
depack ;di
;push iy
or a
ex af,af'
;call init
;pop iy
;ei
;ret
init ld a,128
apbranch1 ldi
aploop2 ld ixh,1
aploop add a,a
jr nz,apnogetbit1
ld a,(hl)
inc hl
rla
apnogetbit1 jr nc,apbranch1
add a,a
jr nz,apnogetbit2
ld a,(hl)
inc hl
rla
apnogetbit2 jr nc,apbranch2
add a,a
jr nz,apnogetbit3
ld a,(hl)
inc hl
rla
apnogetbit3 jr nc,apbranch3
ld bc,16 ;get an offset
apget4bits add a,a
jr nz,apnogetbit4
ld a,(hl)
inc hl
rla
apnogetbit4 rl c
jr nc,apget4bits
jr nz,apbranch4
ex de,hl
ld (hl),b ;write a 0
ex de,hl
inc de
jp aploop2
apbranch4 ex af,af'
ex de,hl ;write a previous byte (1-15 away from dest)
sbc hl,bc
ld a,(hl)
add hl,bc
ld (hl),a
ex af,af'
ex de,hl
inc de
jp aploop2
apbranch3 ld c,(hl) ;use 7 bit offset, length = 2 or 3
inc hl
ex af,af'
rr c
ret z ;if a zero is found here, it's EOF
ld a,2
ld b,0
adc a,b
push hl
ld iyh,b
ld iyl,c
ld h,d
ld l,e
sbc hl,bc
ld c,a
ex af,af'
ldir
pop hl
ld ixh,b
jp aploop
apbranch2 call ap_getgamma ;use a gamma code * 256 for offset, another gamma code for length
dec c
ex af,af'
ld a,c
sub ixh
jr z,ap_r0_gamma
dec a
;do I even need this code?
;bc=bc*256+(hl), lazy 16bit way
ld b,a
ld c,(hl)
inc hl
ld iyh,b
ld iyl,c
push bc
call ap_getgamma2
ex (sp),hl ;bc = len, hl=offs
push de
ex de,hl
ex af,af'
ld a,4
cp d
jr nc,apskip2
inc bc
or a
apskip2 ld hl,127
sbc hl,de
jr c,apskip3
inc bc
inc bc
apskip3 pop hl ;bc = len, de = offs, hl=junk
push hl
or a
sbc hl,de
ex af,af'
pop de ;hl=dest-offs, bc=len, de = dest
ldir
pop hl
ld ixh,b
jp aploop
ap_r0_gamma call ap_getgamma2 ;and a new gamma code for length
push hl
push de
ex de,hl
ld d,iyh
ld e,iyl
sbc hl,de
pop de ;hl=dest-offs, bc=len, de = dest
ldir
pop hl
ld ixh,b
jp aploop
ap5 ld a,(hl)
inc hl
rla
jp apnogetbit5
ap6 ld a, (hl)
inc hl
rla
jp apnogetbit6
ap7 ld a, (hl)
inc hl
rla
jp apnogetbit7
ap8 ld a, (hl)
inc hl
rla
jp apnogetbit8
ap9 ld a, (hl)
inc hl
rla
jp apnogetbit9
ap10 ld a, (hl)
inc hl
rla
ret nc
jp ap_getgammaloop
ap_getgamma2 ex af, af'
ap_getgamma ld bc, 1
add a, a
jr z, ap5
apnogetbit5 rl c
add a, a
jr z, ap6
apnogetbit6 ret nc
add a, a
jr z, ap7
apnogetbit7 rl c
add a, a
jr z, ap8
apnogetbit8 ret nc
ap_getgammaloop add a, a
jr z, ap9
apnogetbit9 rl c
rl b
add a, a
jr z, ap10
apnogetbit10 ret nc
jp ap_getgammaloop