-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeexo3.asm
614 lines (454 loc) · 7.94 KB
/
deexo3.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
; Exomizer 3 depacker for Z80 sjasmplus
;
; license:zlib license
;
; Copyright (c) 2019-2022 uniabis
;
; This software is provided 'as-is', without any express or implied
; warranty. In no event will the authors be held liable for any damages
; arising from the use of this software.
;
; Permission is granted to anyone to use this software for any purpose,
; including commercial applications, and to alter it and redistribute it
; freely, subject to the following restrictions:
;
; 1. The origin of this software must not be misrepresented; you must not
; claim that you wrote the original software. If you use this software
; in a product, an acknowledgment in the product documentation would be
; appreciated but is not required.
;
; 2. Altered source versions must be plainly marked as such, and must not be
; misrepresented as being the original software.
;
; 3. This notice may not be removed or altered from any source
; distribution.
;
; inlining for depacking speed but size of depacker
DEFINE INLINE_GETBIT 0
DEFINE INLINE_GETBITS8 1
DEFINE INLINE_FILBIT 0
; -b option
IFNDEF BACKWARDS
;DEFINE BACKWARDS 1
DEFINE BACKWARDS 0
ENDIF
; -P option bits
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)
IFNDEF PFLAG_CODE
; -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)
ENDIF
opcode_add_hl EQU 41 ; 41=029h;ADD HL, HL
opcode_jp_z EQU 202 ; 202=0CAh;JP Z, nnnn
opcode_jp_c EQU 218 ; 218=0DAh;JP C, nnnn
IF (PFLAG_CODE & PFLAG_4_OFFSET_TABLES)
tbl_bytes EQU (16 + 16 + 16 + 16 + 4)
ELSE
tbl_bytes EQU (16 + 16 + 16 + 4)
ENDIF
tbl_shift EQU (opcode_add_hl - tbl_bytes - tbl_bytes)
tbl_ofs_bits EQU (tbl_shift)
tbl_ofs_lo EQU (tbl_shift + tbl_bytes)
tbl_ofs_hi EQU (tbl_shift + tbl_bytes + tbl_bytes)
tbl_size_all EQU (tbl_bytes + tbl_bytes + tbl_bytes)
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
MACRO m_getbit1
add a
ENDM
MACRO m_filbit1
ld a, (hl)
IF BACKWARDS==1
dec hl
ELSE
inc hl
ENDIF
rla
ENDM
ELSE
MACRO m_getbit1
srl a
ENDM
MACRO m_filbit1
ld a, (hl)
IF BACKWARDS==1
dec hl
ELSE
inc hl
ENDIF
rra
ENDM
ENDIF
MACRO m_getbit
;m_getbit macro
;[out]
; CF :bit data
;[affect]
; af, hl :bit buffer & pointer
IF (INLINE_GETBIT == 1)
m_getbit1
jr nz, .bufremain
m_filbit1
.bufremain:
ELSE
m_getbit1
call z, p_fillbitbuf
ENDIF
ENDM
MACRO m_getbits8i
ex af, af' ;'
.lp1:
m_getbit
rl c
djnz .lp1
ex af, af' ;'
ENDM
MACRO m_getbits8
;m_getbits8 macro
;[in]
; b :required bits length(0 < b <= 8)
; c :must be 0
;[out]
; b :always 0
; c :bits data
;[affect]
; af', hl :bit buffer & pointer
IF (INLINE_GETBITS8 == 1)
m_getbits8i
ELSE
call p_getbits8
ENDIF
ENDM
deexo3:
ld iy, exo_mapbasebits - tbl_shift
cp a ;set ZF, reset CF
ex af, af';'
ld bc, tbl_bytes * 256 + 16
IF (PFLAG_CODE & PFLAG_BITS_ALIGN_START)
scf
ELSE
;scf ;set CF
or a ;reset CF(bit data padding in first byte)
ENDIF
gb4:
ld a, (hl)
IF BACKWARDS==1
dec hl
ELSE
inc hl
ENDIF
init:
get4:
IF (PFLAG_CODE & PFLAG_BITS_ORDER_BE)
adc a, a
ELSE
rr a
ENDIF
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+tbl_ofs_bits), a
IF (PFLAG_CODE & PFLAG_BITS_COPY_GT_7)
rrca
jr nc, .skp2
xor 088h
.skp2:
ENDIF
inc a
ld b, a
ld (iy+tbl_ofs_lo), e
ld (iy+tbl_ofs_hi), d
setbit:
djnz setbit-1
add hl, de
ex de, hl
inc iy
dec c
ex af, af';'
exx
ld c, 16
or a ;reset CF
djnz init
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
ld c, b ;BC=0
ENDIF
IF (PFLAG_CODE & PFLAG_IMPL_1LITERAL)
IF (INLINE_FILBIT == 1)
jr literal_one
ENDIF
ELSEIF (INLINE_FILBIT == 1)
jr next
ELSE
defb opcode_jp_c
ENDIF
IF (INLINE_FILBIT == 1)
filbit:
m_filbit1
jr nc, start_copy
ENDIF
literal_one:
IF BACKWARDS==1
ldd
ELSE
ldi
ENDIF
next:
IF (INLINE_FILBIT == 1)
m_getbit1
jr z, filbit
jr c, literal_one
start_copy:
ELSE
m_getbit
jr c, literal_one
ENDIF
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
push bc
ENDIF
ld bc, 00ffh
.alpha:
inc c
m_getbit
jr nc, .alpha
ex af, af' ;'
ld a, c
sub 16
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
jr z, reuse_exit
ELSE
ret z
ENDIF
jr nc, literal
call p_readtable
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
exx
pop bc
ld a, b
or c
exx
push bc
jr nz, new_offset
ex af, af' ;'
m_getbit
jr c, reuse_offset_ix
ex af, af' ;'
new_offset:
ld a, b
or a
ELSE
push bc
ENDIF
jr nz, defaultofs
dec c
jr z, ofs1
dec c
jr z, ofs2
IF (PFLAG_CODE & PFLAG_4_OFFSET_TABLES)
dec c
jr z, ofs3
defaultofs:
ld c, 01h
defb opcode_jp_z
ofs3:
ELSE
defaultofs:
ld c, 01h
defb opcode_jp_z
ofs2:
ENDIF
ld c, 02h
ofsb4:
ld b, 04h
getofs:
m_getbits8
call p_readtable
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
push bc
pop ix
reuse_offset_bc:
ENDIF
ex (sp), hl
push hl
ld h, d
ld l, e
IF BACKWARDS==1
add hl, bc
pop bc
lddr
ELSE
;or a ;clear CF
sbc hl, bc
pop bc
ldir
ENDIF
pop hl
ex af, af' ;'
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
inc c ; BC=1
ENDIF
jp next
IF (PFLAG_CODE & PFLAG_4_OFFSET_TABLES)
ofs1:
ld bc, 0210h
jr getofs
ofs2:
ld c, 03h
jr ofsb4
ELSE
ofs1:
ld bc, 020Ch
jr getofs
ENDIF
literal:
IF (PFLAG_CODE & PFLAG_BITS_COPY_GT_7)
ld b, (hl)
IF BACKWARDS==1
dec hl
ELSE
inc hl
ENDIF
ld c, (hl)
IF BACKWARDS==1
dec hl
ELSE
inc hl
ENDIF
ELSE
ld c, b
ld b, 10h
call p_getbits16_b
ENDIF
IF BACKWARDS==1
lddr
ELSE
ldir
ENDIF
ex af, af' ;'
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
pop bc
dec bc
ENDIF
jp next
IF (PFLAG_CODE & PFLAG_REUSE_OFFSET)
reuse_exit
pop bc
ret
reuse_offset_ix:
ex af, af' ;'
push ix
pop bc
;or a ;clear CF
jp reuse_offset_bc
ENDIF
p_readtable:
;p_readtable procedure
;[in]
; b :must be 0
; c :tableindex (0 <= c < tbl_bytes)
;[out]
; bc :bits data
; ZF :set if high byte of output bits data equals zero
; CF :reset always
;[affect]
; af', hl :bit buffer & pointer
;[work]
; af, iy
ld iy, exo_mapbasebits - tbl_shift
add iy, bc
ld c, b
IF (PFLAG_CODE & PFLAG_BITS_COPY_GT_7)
ld b, (iy+tbl_ofs_bits)
srl b
jr z, .skp3
m_getbits8
.skp3:
jr nc, .skp4
ld b, c
ld c, (hl)
IF BACKWARDS==1
dec hl
ELSE
inc hl
ENDIF
.skp4:
ELSE
ld a, (iy+tbl_ofs_bits)
or a
call nz, p_getbits16_a
ENDIF
ld a, (iy+tbl_ofs_lo)
add c
ld c, a
ld a, (iy+tbl_ofs_hi)
adc b
ld b, a
ret
IF (PFLAG_CODE & PFLAG_BITS_COPY_GT_7)
ELSE
p_getbits16_a:
;p_getbits16_a procedure
;[in]
; a :length of bits to read(1-16)
; c :must be 0
;[out]
; bc :bits data
;[affect]
; af', hl :bit buffer & pointer
;[work]
; af
ld b, a
p_getbits16_b:
;p_getbits16_b procedure
;[in]
; b :length of bits to read(1-16)
; c :must be 0
;[out]
; bc :bits data
;[affect]
; af', hl :bit buffer & pointer
;[work]
; af
ld a, d
ld d, c
ex af, af' ;'
.lp3:
m_getbit
rl c
rl d
djnz .lp3
ex af, af' ;'
ld b, d
ld d, a
ret
ENDIF
IF (INLINE_GETBIT == 1)
ELSE
p_fillbitbuf:
m_filbit1
ret
ENDIF
IF (INLINE_GETBITS8 == 1)
ELSE
p_getbits8:
m_getbits8i
ret
ENDIF
exo_mapbasebits:
;DEFS tbl_size_all