BB2 C port
API reference: Blitz libraries in C and the shared code
Loading...
Searching...
No Matches
optidec.h
Go to the documentation of this file.
1/*
2 * optidec.h - the decruncher of StoneCracker 4.04 ("S404"), optidec.bin
3 * (third party code that the RWE sources stclib.bb and stcdecrunchlib.bb
4 * include with IncBin; its source is optidec.S v1.1 of StoneCracker 4.10,
5 * Aminet util/pack/Stc4103.lha), as assembler text for
6 * ristonecrackerlib.c and ristcdecrunchlib.c (each puts OPTIDEC_ASM in
7 * a BLITZ_ASM_BLOCK of its own).
8 *
9 * Assembler for speed (perf bstc: in C the decruncher took 1.8 times the
10 * original's cycles): the loop is optidec.S instruction by instruction,
11 * with the changes below; the unpacked data are the original's for every
12 * input it handles (see DIFF).
13 *
14 * sd_dec In: a0 the destination, a1 the S404 file (its header)
15 * Out: d0 = 1 and a0 = the destination for data unpacked,
16 * d0 = 0 for refused (corrupt) data
17 * Keeps a2-a6 (MOVEM.l on the stack), changes d1-d7/a0-a1.
18 *
19 * The registers are the original's: d0 = 1 (the 1 of its ADDX), d2 the
20 * far offset bits, d3 the count (its upper word is kept 0: MOVEQ #-1,d3
21 * became a word -1), d4 = $FFFF, d5 = 16, d6 the bit buffer (bits at the
22 * top of its low word), d7 the bits left in it, a1 the packed words (read
23 * backwards), a0 the output (written backwards), a2 a match, a3/a4 the
24 * main loop's return addresses of the bit reader (P_bits, back by JMP
25 * (a6)), a5 the loop's limit.
26 *
27 * Faster than the original: the 101xx match (4..7 bytes, the most
28 * frequent code in text) reads its 2 bits one at a time (P_m47, as the
29 * original reads the 11x match's bit) instead of through the bit reader:
30 * with d7 = 0..15, as in the main loop, the bits, d6's low word and d7
31 * come out the same (d6's upper word differs; every later use of it
32 * clears or shifts it out first).
33 * Fixed (#833): corrupt data no longer write below the destination; the
34 * original wrote a match or literal run that starts just above it past
35 * its start, into the memory below. The checks cost nothing per token
36 * on the main path: the loop's one test (CMP.l a0,a5) compares with
37 * dest + SD_LIMIT instead of dest, so every token but a long match of
38 * more than one length byte ends at or above dest, and such a match is
39 * checked (sm_more); the last bytes are unpacked by a second copy of the
40 * loop (labels st_*) that checks each literal run and match against the
41 * room left. Once per call: far offset bits (the efficiency) above 16
42 * and an odd packed length are refused. Corrupt data now give 0 and
43 * write only inside dest .. dest + unpacked length.
44 * Not checked (a known limit): reads. A stream that runs out reads
45 * words below the packed data (the header, then the memory below the
46 * file), and a match may read up to 544 + 65535 bytes above the output
47 * written so far, also above dest + unpacked length; such data unpack
48 * wrong, but nothing is written outside the destination.
49 * DIFF: the literal run reads its shift count from a table indexed by the
50 * bit count d7 (MOVE.b table(pc,d7.w)); d7 leaves 0..15 only with a
51 * first bit count (the trailer's third word) above 16, and then the
52 * original reads whatever lies around the table (its own code) - this
53 * build takes the index modulo 16. With d7 = 0..15 the result is the
54 * same. A file with a first bit count above 16 is unpacked by the tail
55 * loop alone, which takes the index modulo 16; the main loop needs no
56 * AND.
57 */
58#ifndef OPTIDEC_H
59#define OPTIDEC_H
60
61/* the main loop's limit, dest + SD_LIMIT: the longest output of one token
62 but a long match of more than one length byte (23 + 254 bytes; a
63 literal byte 1, the matches 2..3, 4..7, 8..22, a literal run 14..45),
64 less 1 */
65#define SD_LIMIT "276"
66
67/*
68 * SD_LOOP: the decruncher's loop (optidec.S from _lins on), twice: the
69 * main loop (P "sm") and the tail (P "st").
70 * exit the code at P_exit, where the loop ends (a0 <= a5)
71 * loop_a6 the length's return address into a6 (P_len5a)
72 * off5_a6 the offset's return address into a6 (P_off6)
73 * more where a long match's length byte 255 goes on (a label)
74 * more_code code behind the loop (the main loop: its continuation
75 * and its exit)
76 * chk_match after a match's offset, before its bytes (fixed #833)
77 * run_shift a literal run's shift count from P_tab into d1 (MOVE.b
78 * table(pc,d7.w),d1; the tail checks the run first)
79 */
80#define SD_LOOP(P, S, exit, loop_a6, off5_a6, more, more_code, chk_match, \
81 run_shift) \
82P "_m47:\n" /* 101xx: the 2 bits one by one */ \
83" dbf d7," P "_m47a\n" \
84" move.w -(a1),d6\n" \
85" moveq #15,d7\n" \
86P "_m47a:\n" \
87" add.w d6,d6\n" \
88" addx.w d3,d3\n" /* d3 = 2: 4 + bit */ \
89" dbf d7," P "_m47b\n" \
90" move.w -(a1),d6\n" \
91" moveq #15,d7\n" \
92P "_m47b:\n" \
93" add.w d6,d6\n" \
94" addx.w d3,d3\n" /* 8 + the 2 bits */ \
95" subq.w #6,d3\n" /* 2 + the 2 bits */ \
96" dbf d7," P "_off2\n" \
97" bra " P "_off2a\n" \
98P "_ins:\n" /* _lins: 8 bits, a literal byte */ \
99" subq.w #8,d7\n" \
100" bpl.s " P "_ins2\n" \
101" move.w d7,d1\n" /* across the next word */ \
102" addq.w #8,d7\n" \
103" lsl.l d7,d6\n" \
104" move.w -(a1),d6\n" \
105" neg.w d1\n" \
106" lsl.l d1,d6\n" \
107" addq.w #8,d7\n" \
108" swap d6\n" \
109" move.b d6,-(a0)\n" \
110" swap d6\n" \
111" cmp.l a0,a5\n" \
112" dbhs d7," P "_main\n" \
113" bra.s " P "_exma\n" \
114P "_ins2:\n" \
115" rol.w #8,d6\n" \
116" move.b d6,-(a0)\n" \
117P "_test1:\n" /* the loop's test */ \
118" cmp.l a0,a5\n" \
119" dbhs d7," P "_main\n" \
120P "_exma:\n" \
121" bhs" S " " P "_exit\n" \
122P "_main1:\n" \
123" move.w -(a1),d6\n" \
124" moveq #15,d7\n" \
125P "_main:\n" \
126" add.w d6,d6\n" \
127" bcc.s " P "_ins\n" \
128" dbf d7," P "_len1\n" \
129" move.w -(a1),d6\n" \
130" moveq #15,d7\n" \
131P "_len1:\n" \
132" add.w d6,d6\n" \
133" bcs.s " P "_len6\n" /* 11x: 2 or 3 bytes */ \
134" dbf d7," P "_len2\n" \
135" move.w -(a1),d6\n" \
136" moveq #15,d7\n" \
137P "_len2:\n" \
138" moveq #2,d1\n" \
139" moveq #2,d3\n" \
140" add.w d6,d6\n" \
141" bcs.s " P "_m47\n" /* 101xx: 4..7 bytes */ \
142" dbf d7," P "_len3\n" \
143" move.w -(a1),d6\n" \
144" moveq #15,d7\n" \
145P "_len3:\n" \
146" add.w d6,d6\n" \
147" bcc.s " P "_len4\n" /* 1000: a long match */ \
148" moveq #4,d1\n" /* 1001xxxx: 8..22 bytes */ \
149" moveq #6,d3\n" \
150" lea " P "_len3a(pc),a6\n" \
151" bra" S " " P "_bits\n" \
152P "_len3a:\n" \
153" add.w d1,d3\n" \
154" cmp.w #15,d1\n" \
155" blo.s " P "_off1\n" \
156" moveq #5,d1\n" /* 10011111xxxxx: 14..45 literals */\
157" moveq #14,d3\n" \
158" lea " P "_len3b(pc),a6\n" \
159" bra" S " " P "_bits\n" \
160P "_len4:\n" \
161" moveq #21,d3\n" \
162P "_loop:\n" /* 23 + n bytes, n in bytes */ \
163" moveq #8,d1\n" \
164P "_len5:\n" \
165loop_a6 \
166" bra" S " " P "_bits\n" \
167P "_len5a:\n" /* the long match: + 8 bits */ \
168" add.w d1,d3\n" \
169" not.b d1\n" \
170" dbeq d7," P "_off2\n" \
171" bne.s " P "_off2a\n" \
172" bra " more "\n" /* 255: one byte more */ \
173P "_off6:\n" /* the match: d3 + 2 bytes */ \
174" add.w d1,a2\n" \
175chk_match \
176" move.b (a2),-(a0)\n" \
177P "_copy:\n" \
178" move.b -(a2),-(a0)\n" \
179" dbf d3," P "_copy\n" \
180P "_test:\n" \
181" cmp.l a0,a5\n" \
182" dbhs d7," P "_main\n" \
183" blo" S " " P "_main1\n" \
184P "_exit:\n" \
185exit \
186P "_len6:\n" \
187" dbf d7," P "_len7\n" \
188" move.w -(a1),d6\n" \
189" moveq #15,d7\n" \
190P "_len7:\n" \
191" add.w d6,d6\n" \
192" addx.w d0,d3\n" \
193P "_off1:\n" \
194" dbf d7," P "_off2\n" \
195P "_off2a:\n" \
196" move.w -(a1),d6\n" \
197" moveq #15,d7\n" \
198P "_off2:\n" /* the offset */ \
199" add.w d6,d6\n" \
200" bcs.s " P "_off3\n" \
201" dbf d7," P "_off4\n" \
202" move.w -(a1),d6\n" \
203" moveq #15,d7\n" \
204P "_off4:\n" \
205" moveq #9,d1\n" /* 00: 9 bits + 32 */ \
206" lea 32(a0),a2\n" \
207" add.w d6,d6\n" \
208" bcc.s " P "_off5\n" \
209" moveq #5,d1\n" /* 01: 5 bits */ \
210" move.l a0,a2\n" \
211" bra.s " P "_off5\n" \
212P "_off3:\n" \
213" lea 544(a0),a2\n" /* 1: d2 bits + 544 */ \
214" move.w d2,d1\n" \
215P "_off5:\n" \
216off5_a6 \
217P "_bits:\n" /* d1 bits -> d1, back by a6 */ \
218" and.l d4,d6\n" \
219" sub.w d1,d7\n" \
220" bpl.s " P "_bits2\n" \
221" add.w d7,d1\n" \
222" lsl.l d1,d6\n" \
223" move.w d7,d1\n" \
224" move.w -(a1),d6\n" \
225" neg.w d1\n" \
226" add.w d5,d7\n" \
227P "_bits2:\n" \
228" lsl.l d1,d6\n" \
229" move.l d6,d1\n" \
230" swap d1\n" \
231" jmp (a6)\n" \
232P "_pins2:\n" /* d3 = 0: MOVEQ #-1,d3, the */ \
233" subq.w #1,d3\n" /* upper word stays 0 */ \
234" bra " P "_ins2\n" \
235P "_2ins2:\n" /* the literal run, 2 bytes a time */\
236" rol.w #8,d6\n" \
237" move.b d6,-(a0)\n" \
238P "_2ins1:\n" \
239" lsl.l d7,d6\n" \
240" move.w -(a1),d6\n" \
241" lsl.l d1,d6\n" \
242" swap d6\n" \
243" move.b d6,-(a0)\n" \
244" swap d6\n" \
245" subq.w #2,d3\n" \
246" bgt.s " P "_2ins2\n" \
247" beq.s " P "_pins2\n" \
248" addq.w #8,d7\n" \
249" bra" S " " P "_test\n" \
250P "_len3b:\n" /* the literal run: d3 bytes */ \
251" add.w d1,d3\n" \
252run_shift \
253" bpl.s " P "_2ins1\n" \
254" subq.w #8,d7\n" \
255" dbf d3," P "_2ins2\n" /* d3 = 14..45: always taken */ \
256P "_tab:\n" /* the shift count of the run's straddling byte (_lnewd1) */\
257" .byte 0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01\n" \
258" .byte 0x88,0x87,0x86,0x85,0x84,0x83,0x82,0x81\n" \
259more_code
260
261/* sd_dec (see the head of the file) */
262#define OPTIDEC_ASM \
263"sd_dec:\n" \
264" movem.l a2/a5-a6,-(sp)\n" /* a3-a4: for the main loop */ \
265" addq.w #8,a1\n" /* skip "S404" and the offset */ \
266" move.l a0,a5\n" \
267" add.l (a1)+,a0\n" /* + the unpacked length */ \
268" add.l (a1),a1\n" /* + the packed length */ \
269" moveq #1,d0\n" \
270" move.w a1,d1\n" /* fixed (#833): the trailer is */ \
271" and.w d0,d1\n" /* words (the file is even) */ \
272" bne sd_fail\n" \
273" movem.w (a1),d2/d6/d7\n" /* far bits, bit buffer, bits */ \
274" moveq #16,d5\n" \
275" cmp.w d5,d2\n" /* fixed (#833): far bits 0..16 */ \
276" bhi sd_fail\n" \
277" moveq #0,d4\n" \
278" not.w d4\n" \
279" move.l d4,d3\n" /* MOVEQ #-1,d3; the upper word 0 */\
280" lea " SD_LIMIT "(a5),a2\n" /* fixed (#833): the limit */ \
281" cmp.l a0,a2\n" \
282" bhs st_test1\n" /* all in the tail */ \
283" cmp.w d5,d7\n" /* DIFF: a first bit count above */ \
284" bhi st_test1\n" /* 16: the tail, P_tab index & 15 */\
285" move.l a2,a5\n" \
286" movem.l a3-a4,-(sp)\n" \
287" lea sm_off6(pc),a3\n" \
288" lea sm_len5a(pc),a4\n" \
289" bra sm_test1\n" \
290SD_LOOP("sm", ".s", \
291 " bra sm_exit2\n", \
292 " move.l a4,a6\n", \
293 " move.l a3,a6\n", \
294 "sm_more", \
295 /* the rest in the tail, the limit dest */ \
296 "sm_exit2:\n" \
297 " movem.l (sp)+,a3-a4\n" \
298 " lea -" SD_LIMIT "(a5),a5\n" \
299 " bra st_test1\n" \
300 /* a long match of more than one length byte (more than 277 */ \
301 /* bytes): its bytes are checked (fixed #833) */ \
302 "sm_more:\n" \
303 " moveq #8,d1\n" \
304 " lea sm_len5c(pc),a6\n" \
305 " bra sm_bits\n" \
306 "sm_len5c:\n" \
307 " add.w d1,d3\n" \
308 " not.b d1\n" \
309 " beq.s sm_more\n" \
310 /* dest + d3 + 2 <= a0, dest = a5 - SD_LIMIT */ \
311 " lea -" SD_LIMIT "+2(a5),a2\n" \
312 " add.l d3,a2\n" \
313 " cmp.l a0,a2\n" \
314 " bhi.s sm_fail\n" \
315 " dbf d7,sm_off2\n" \
316 " bra sm_off2a\n", \
317 "", \
318 " move.b sm_tab(pc,d7.w),d1\n") \
319"sm_fail:\n" \
320" movem.l (sp)+,a3-a4\n" \
321" bra sd_fail\n" \
322SD_LOOP("st", "", \
323 "sd_ok:\n" /* d0 = 1 */ \
324 " movem.l (sp)+,a2/a5-a6\n" \
325 " rts\n", \
326 " lea st_len5a(pc),a6\n", \
327 " lea st_off6(pc),a6\n", \
328 "st_loop", \
329 "", \
330 /* fixed (#833): a5 + d3 + 2 <= a0 */ \
331 " lea 2(a5,d3.l),a6\n" \
332 " cmp.l a0,a6\n" \
333 " bhi sd_fail\n", \
334 /* fixed (#833): a5 + d3 <= a0 (d3 = 14..45) */ \
335 " lea 0(a5,d3.l),a6\n" \
336 " cmp.l a0,a6\n" \
337 " bhi sd_fail\n" \
338 " moveq #15,d1\n" /* DIFF: the index & 15 */ \
339 " and.w d7,d1\n" \
340 " move.b st_tab(pc,d1.w),d1\n") \
341"sd_fail:\n" \
342" moveq #0,d0\n" \
343" movem.l (sp)+,a2/a5-a6\n" \
344" rts\n"
345
346#endif