-
Notifications
You must be signed in to change notification settings - Fork 2
/
deexoopt_x3.asm
555 lines (476 loc) · 11.2 KB
/
deexoopt_x3.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
;Exomizer 3 Z80 decoder
;Copyright (C) 2008-2018 by Jaime Tejedor Gomez (Metalbrain)
;
;Optimized by Antonio Villena and Urusergi
;
;Fixed a bug, and optimized, and added support for all -P options by uniabis
;
;Compression algorithm by Magnus Lind
; exomizer raw -P15 -T0 (literals=1) (reuse=0)
; exomizer raw -P15 -T1 (literals=0) (reuse=0)
; exomizer raw -P47 -T0 (literals=1) (reuse=1)
; exomizer raw -P47 -T1 (literals=0) (reuse=1);
; This depacker is free software; you can redistribute it and/or
; modify it under the terms of the GNU Lesser General Public
; License as published by the Free Software Foundation; either
; version 2.1 of the License, or (at your option) any later version.
;
; This library is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; Lesser General Public License for more details.
;
; You should have received a copy of the GNU Lesser General Public
; License along with this library; if not, write to the Free Software
; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;
IFNDEF back
; without -b option
DEFINE back 0
; with -b option
;DEFINE back 1
ENDIF
IFNDEF literals
DEFINE literals 0
ENDIF
DEFINE PFLAG_BITS_ORDER_BE (1<<0)
DEFINE PFLAG_BITS_COPY_GT_7 (1<<1)
DEFINE PFLAG_IMPL_1LITERAL (1<<2)
DEFINE PFLAG_BITS_ALIGN_START (1<<3)
DEFINE PFLAG_4_OFFSET_TABLES (1<<4)
DEFINE PFLAG_REUSE_OFFSET (1<<5)
; -P0 (Exomizer2 raw compatible)
;DEFINE PFLAG_CODE 0
; -P7 (Exomizer3.0 raw default)
;DEFINE PFLAG_CODE (PFLAG_BITS_ORDER_BE | PFLAG_BITS_COPY_GT_7 | PFLAG_IMPL_1LITERAL)
; -P39 (Exomizer3.1 raw default)
;DEFINE PFLAG_CODE (PFLAG_BITS_ORDER_BE | PFLAG_BITS_COPY_GT_7 | PFLAG_IMPL_1LITERAL | PFLAG_REUSE_OFFSET)
IFNDEF PFLAG_CODE
IFNDEF reuse
; -P15
DEFINE PFLAG_CODE (PFLAG_BITS_ORDER_BE | PFLAG_BITS_COPY_GT_7 | PFLAG_IMPL_1LITERAL | PFLAG_BITS_ALIGN_START)
ELSEIF reuse==0
; -P15
DEFINE PFLAG_CODE (PFLAG_BITS_ORDER_BE | PFLAG_BITS_COPY_GT_7 | PFLAG_IMPL_1LITERAL | PFLAG_BITS_ALIGN_START)
ELSE
; -P47
DEFINE PFLAG_CODE (PFLAG_BITS_ORDER_BE | PFLAG_BITS_COPY_GT_7 | PFLAG_IMPL_1LITERAL | PFLAG_BITS_ALIGN_START | PFLAG_REUSE_OFFSET)
ENDIF
ENDIF
IF (PFLAG_CODE & PFLAG_4_OFFSET_TABLES)
DEFINE map_len (16*4+4)
ELSE
DEFINE map_len (16*3+4)
ENDIF
IF ((low mapbase)<256+(128-16)-128) && ((low mapbase)>(128-16)-map_len*2+127)
DEFINE map_ofs 0
ELSE
DEFINE map_ofs (128-16)
ENDIF
map_iyh equ (high (mapbase-map_ofs+128))
map_disp_bit equ ((low (mapbase-map_ofs+128))-128)
map_disp_lo equ (map_disp_bit+map_len*1)
map_disp_hi equ (map_disp_bit+map_len*2)
ld iy, map_iyh*256+map_ofs
cp a ;set ZF
ex af, af';'
ld bc, map_len * 256 + 16
IF (PFLAG_CODE & PFLAG_BITS_ALIGN_START)
scf
ELSE
or a ;reset CF
ENDIF
gb4 ld a, (hl)
IF back==1
dec hl
ELSE
inc hl
ENDIF
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
get4 adc a, a
ELSE
get4 rr a
ENDIF
get4l jr z, gb4
rl c
jr nc, get4
ex af, af';'
ld a, c
exx
ld hl, 1
jr nz, .skp1
ld d, h
ld e, l
ld c, 16
.skp1 ld (iy+map_disp_lo), e
ld (iy+map_disp_hi), d
IF (PFLAG_CODE & PFLAG_BITS_COPY_GT_7)
rrca
inc a
ld (iy+map_disp_bit), a
jr nc, get5
sub 128-8+1
setbit add hl, hl
get5 dec a
jr nz, setbit
ELSE
inc a
ld b, a
defb 40 ;40=028h;JR Z,nnnn
setbit add hl, hl
djnz setbit
cp 8+1
jr c, bit0
add 080h-8
bit0 ld (iy+map_disp_bit), a
ENDIF
add hl, de
ex de, hl
IFNDEF HD64180
inc iyl
ELSE
inc iy
ENDIF
dec c
ex af, af';'
exx
ld c, 16
IF (PFLAG_CODE & PFLAG_IMPL_1LITERAL)
or a ;reset CF
djnz get4
ELSE
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
add a, a
ELSE
srl a
ENDIF
djnz get4l
ENDIF
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
ld c, b ;BC=0
ENDIF
IFNDEF OPTIMIZE_JUMP
ELSEIF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
ELSE
ld ix, mloop
ENDIF
IF (PFLAG_CODE & PFLAG_IMPL_1LITERAL)
jr litcop
ELSE
jr mloopl
ENDIF
gbi ld a, (hl)
IF back==1
dec hl
ELSE
inc hl
ENDIF
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
adc a, a
ELSE
rra
ENDIF
IFNDEF OPTIMIZE_JUMP
jr gbic
ELSE
jp gbic
ENDIF
gbm ld a, (hl)
IF back==1
dec hl
ELSE
inc hl
ENDIF
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
adc a, a
ELSE
rra
ENDIF
jr nc, gbmc
litcop:
IF back==1
ldd
ELSE
ldi
ENDIF
mloop:
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
add a, a
ELSE
srl a
ENDIF
mloopl jr z, gbm
jr c, litcop
gbmc:
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
push bc
ENDIF
IFNDEF HD64180
ld c, (map_ofs-1) & 255
ELSE
ld bc, (map_ofs-1) & 255
ENDIF
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
getind add a, a
ELSE
getind srl a
ENDIF
jr z, gbi
gbic inc c
jr nc, getind
IF map_ofs==0
bit 4, c
IF literals==1
jr nz, litcat
ELSEIF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
jr nz, reuse_exit
ELSE
ret nz
ENDIF
ELSE
IF literals==1
jp m, litcat
ELSEIF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
jp m, reuse_exit
ELSE
ret m
ENDIF
ENDIF
ex de, hl
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
ex (sp), hl
ENDIF
push hl
IFNDEF HD64180
ld iyl, c
ld c, 0
or a
ELSE
ld iy, map_iyh*256
add iy, bc
ld c, b
ENDIF
ld b, (iy+map_disp_bit)
dec b
call nz, getbits
ld l, (iy+map_disp_lo)
ld h, (iy+map_disp_hi)
add hl, bc
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
ex af, af';'
pop bc
ld a, b
or c
jr z, checkreuse
ex af, af';'
readofs:
ENDIF
push hl
inc h
dec h
jr nz, fixhi
IF (PFLAG_CODE & PFLAG_4_OFFSET_TABLES)
ld bc, 2*256+(map_ofs+64)/4
dec l
jr z, goit
ld bc, 4*256+(map_ofs+48)/16
ELSE
ld bc, 2*256+(map_ofs+48)/4
ENDIF
dec l
jr z, goit
dec l
fixhi ld bc, 4*256+(map_ofs+32)/16
jr z, goit
dec c
goit call lee8
IFNDEF HD64180
ld iyl, c
ELSE
ld iy, map_iyh*256
add iy, bc
ENDIF
ld c, b
ld b, (iy+map_disp_bit)
dec b
call nz, getbits
ld l, (iy+map_disp_lo)
ld h, (iy+map_disp_hi)
add hl, bc
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
push hl
pop ix
useofs:
ENDIF
ex de, hl
pop bc
ex (sp), hl
IF back==1
ex de, hl
add hl,de
lddr
ELSE
push hl
sbc hl, de
pop de
ldir
ENDIF
pop hl
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
inc c; BC=1
ENDIF
IFNDEF OPTIMIZE_JUMP
jr mloop
ELSEIF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
jr mloop
ELSE
jp (ix)
ENDIF
IF literals==1
litcat:
IF map_ofs==0
bit 0, c
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
jr z, reuse_exit
ELSE
ret z
ENDIF
ELSE
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
jp pe, reuse_exit
ELSE
ret pe
ENDIF
ENDIF
IF (PFLAG_CODE & PFLAG_BITS_COPY_GT_7)
ld b, (hl)
IF back==1
dec hl
ELSE
inc hl
ENDIF
ld c, (hl)
IF back==1
dec hl
ELSE
inc hl
ENDIF
ELSE
ld bc, 8 * 256
or a
ex de, hl
call lee16
ex de, hl
ENDIF
IF back==1
lddr
ELSE
ldir
ENDIF
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
pop bc
dec bc
ENDIF
IFNDEF OPTIMIZE_JUMP
jr mloop
ELSEIF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
jp mloop
ELSE
jp (ix)
ENDIF
ENDIF
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
reuse_exit:
pop bc
ret
checkreuse:
ex af, af';'
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
add a, a
ELSE
srl a
ENDIF
jr nz, .skip
ld a, (de)
IF back==1
dec de
ELSE
inc de
ENDIF
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
adc a, a
ELSE
rra
ENDIF
.skip:
jr nc, readofs
push ix
ex (sp), hl
or a ;clear CF
jr useofs
ENDIF
getbits jp p, lee8
sla b
jr z, gby
srl b
defb 250 ;250=0FAh;JP M,nnnn
xopy ld a, (de)
IF back==1
dec de
ELSE
inc de
ENDIF
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
lee16 adc a, a
ELSE
lee16 rr a
ENDIF
jr z, xopy
rl c
djnz lee16
ld b, c
IF (PFLAG_CODE & PFLAG_BITS_COPY_GT_7)
gby ex de, hl
ld c, (hl)
IF back==1
dec hl
ELSE
inc hl
ENDIF
ex de, hl
ret
ELSE
gby ld c, 1
or a
defb 218 ;218=0DAh;JP C,nnnn
.f ld a, (de)
IF back==1
dec de
ELSE
inc de
ENDIF
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
.l adc a, a
ELSE
.l rr a
ENDIF
jr z, .f
rl c
jr nc, .l
ret
ENDIF
copy ld a, (de)
IF back==1
dec de
ELSE
inc de
ENDIF
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
lee8 adc a, a
ELSE
lee8 rr a
ENDIF
jr z, copy
rl c
djnz lee8
ret