-
Notifications
You must be signed in to change notification settings - Fork 15
/
exomizer.s
353 lines (343 loc) · 9.13 KB
/
exomizer.s
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
tablBi = depackBuffer
tablLo = depackBuffer + 52
tablHi = depackBuffer + 104
FORWARD_DECRUNCHING = 1
LITERAL_SEQUENCES_NOT_USED = 1
MAX_SEQUENCE_LENGTH_256 = 1
; -------------------------------------------------------------------
; This source code is altered and is not the original version found on
; the Exomizer homepage. Forward decrunching modifications improved
; based on the version in Krill's loader.
; -------------------------------------------------------------------
;
; Copyright (c) 2002 - 2018 Magnus Lind.
;
; 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 distribution.
;
; 4. The names of this software and/or it's copyright holders may not be
; used to endorse or promote products derived from this software without
; specific prior written permission.
;
; -------------------------------------------------------------------
; no code below this comment has to be modified in order to generate
; a working decruncher of this source file.
; However, you may want to relocate the tables last in the file to a
; more suitable address.
; -------------------------------------------------------------------
; Depack from file stream packed with Exomizer 3 forward mode
;
; Parameters: A,X load address
; Returns: -
; Modifies: A,X,Y
Depack: sta zpDestLo
stx zpDestHi
DepackDestSet:
; -------------------------------------------------------------------
; jsr this label to decrunch, it will in turn init the tables and
; call the decruncher
; no constraints on register content, however the
; decimal flag has to be #0 (it almost always is, otherwise do a cld)
; init zeropage, x and y regs. (12 bytes)
;
jsr GetByte
sta zpBitBuf
ldy #0
; -------------------------------------------------------------------
; calculate tables (62 bytes) + get_bits macro
; x and y must be #0 when entering
;
clc
table_gen:
tax
tya
and #$0f
sta tablLo,y
beq shortcut ; start a new sequence
; -------------------------------------------------------------------
txa
adc tablLo - 1,y
sta tablLo,y
lda zpLenHi
adc tablHi - 1,y
shortcut:
sta tablHi,y
; -------------------------------------------------------------------
lda #$01
sta zpLenHi
lda #$78 ; %01111000
jsr get_bits
; -------------------------------------------------------------------
lsr
tax
beq rolled
php
rolle:
asl zpLenHi
sec
ror
dex
bne rolle
plp
rolled:
ror
sta tablBi,y
bmi no_fixup_lohi
lda zpLenHi
stx zpLenHi
skip1
no_fixup_lohi:
txa
; -------------------------------------------------------------------
iny
cpy #52
bne table_gen
; -------------------------------------------------------------------
; prepare for main decruncher
ldy zpDestLo
stx zpDestLo
stx zpBitsHi
; -------------------------------------------------------------------
; copy one literal byte to destination (11 bytes)
;
literal_start1:
if FORWARD_DECRUNCHING = 0
tya
bne no_hi_decr
dec zpDestHi
no_hi_decr:
dey
endif
jsr GetByte
sta (zpDestLo),y
if FORWARD_DECRUNCHING > 0
iny
bne no_hi_incr
inc zpDestHi
no_hi_incr:
endif
; -------------------------------------------------------------------
; fetch sequence length index (15 bytes)
; x must be #0 when entering and contains the length index + 1
; when exiting or 0 for literal byte
next_round:
dex
lda zpBitBuf
no_literal1:
asl
bne nofetch8
jsr GetByte
rol
nofetch8:
inx
bcc no_literal1
sta zpBitBuf
; -------------------------------------------------------------------
; check for literal byte (2 bytes)
;
beq literal_start1
; -------------------------------------------------------------------
; check for decrunch done and literal sequences (4 bytes)
;
cpx #$11
bcs exit_or_lit_seq
; -------------------------------------------------------------------
; calulate length of sequence (zp_len) (18(11) bytes) + get_bits macro
;
lda.wx tablBi - 1,x
jsr get_bits
adc tablLo - 1,x ; we have now calculated zpLenLo
sta zpLenLo
if MAX_SEQUENCE_LENGTH_256 = 0
lda zpBitsHi
adc tablHi - 1,x ; c = 0 after this.
sta zpLenHi
; -------------------------------------------------------------------
; here we decide what offset table to use (27(26) bytes) + get_bits_nc macro
; z-flag reflects zpLenHi here
;
ldx zpLenLo
else
tax
endif
lda #$e1
cpx #$03
bcs gbnc2_next
lda tablBit,x
gbnc2_next:
asl zpBitBuf
bne gbnc2_ok
tax
jsr GetByte
rol
sta zpBitBuf
txa
gbnc2_ok:
rol
bcs gbnc2_next
tax
; -------------------------------------------------------------------
; calulate absolute offset (zp_src) (21 bytes) + get_bits macro
;
if MAX_SEQUENCE_LENGTH_256 = 0
lda #0
sta zpBitsHi
endif
if FORWARD_DECRUNCHING = 0
lda tablBi,x
jsr get_bits
adc tablLo,x
sta zpSrcLo
lda zpBitsHi
adc tablHi,x
adc zpDestHi
sta zpSrcHi
else
lda tablBi,x
jsr get_bits
clc
adc tablLo,x
eor #$ff
sta zpSrcLo
lda zpBitsHi
adc tablHi,x
eor #$ff
adc zpDestHi
sta zpSrcHi
endif
; -------------------------------------------------------------------
; prepare for copy loop (2 bytes)
;
pre_copy:
ldx zpLenLo
; -------------------------------------------------------------------
; main copy loop (30 bytes)
;
copy_next:
if FORWARD_DECRUNCHING = 0
tya
bne copy_skip_hi
dec zpDestHi
dec zpSrcHi
copy_skip_hi:
dey
endif
if LITERAL_SEQUENCES_NOT_USED = 0
if FORWARD_DECRUNCHING > 0
bcc get_literal_byte
else
bcs get_literal_byte
endif
endif
lda (zpSrcLo),y
literal_byte_gotten:
sta (zpDestLo),y
if FORWARD_DECRUNCHING > 0
iny
bne copy_skip_hi
inc zpDestHi
inc zpSrcHi
copy_skip_hi:
endif
dex
bne copy_next
if MAX_SEQUENCE_LENGTH_256 = 0
lda zpLenHi
endif
begin_stx:
stx zpBitsHi
if (FORWARD_DECRUNCHING > 0 && MAX_SEQUENCE_LENGTH_256 = 0 && LITERAL_SEQUENCES_NOT_USED = 0)
bne no_next_round
jmp next_round
no_next_round:
else
beq next_round
endif
if MAX_SEQUENCE_LENGTH_256 = 0
copy_next_hi:
dec zpLenHi
jmp copy_next
endif
if LITERAL_SEQUENCES_NOT_USED = 0
get_literal_byte:
jsr GetByte
if FORWARD_DECRUNCHING > 0
bcc literal_byte_gotten
else
sec
bcs literal_byte_gotten
endif
endif
; -------------------------------------------------------------------
; exit or literal sequence handling (16(12) bytes)
;
if LITERAL_SEQUENCES_NOT_USED = 0
exit_or_lit_seq:
beq decr_exit
jsr GetByte
if MAX_SEQUENCE_LENGTH_256 = 0
sta zpLenHi
endif
jsr GetByte
tax
if FORWARD_DECRUNCHING > 0
clc
bcc copy_next
else
sec
bcs copy_next
endif
decr_exit:
endif
rts
; -------------------------------------------------------------------
; get bits from stream
;
get_bits:
adc #$80 ; needs c=0, affects v
asl
bpl gb_skip
gb_next:
asl zpBitBuf
bne gb_ok
pha
jsr GetByte
rol
sta zpBitBuf
pla
gb_ok:
rol
bmi gb_next
gb_skip:
bvc gb_get_done
sta zpBitsHi
jsr GetByte
sec
if LITERAL_SEQUENCES_NOT_USED > 0
exit_or_lit_seq:
endif
gb_get_done:
rts
; -------------------------------------------------------------------
; the static stable used for bits+offset for lengths 3, 1 and 2 (3 bytes)
; bits 4, 2, 4 and offsets 16, 48, 32
tablBit:
dc.b %11100001, %10001100, %11100010
; -------------------------------------------------------------------
; end of decruncher
; -------------------------------------------------------------------