wok view memtest/stuff/unlzsa2.S @ rev 25493

Add libgnt
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Nov 19 17:37:48 2022 +0000 (17 months ago)
parents 2ccf00d9c1cd
children 48dff2952881
line source
1 // Lzsa2Decode:
2 #ifndef FLAT32
3 // input ds:si=inStream, es:di=outStream
4 // output outStream[], ds:si, es:di
5 .code16
6 #define AX %ax
7 #define BX %bx
8 #define BP %bp
9 #define SI %si
10 #define DI %di
11 #else
12 // input esi=inStream, edi=outStream
13 // output outStream[], ds:esi, es:edi
14 .code32
15 #define AX %eax
16 #define BX %ebx
17 #define BP %ebp
18 #define SI %esi
19 #define DI %edi
20 #endif
22 MATCH_RUN_LEN = 7
23 LITERALS_RUN_LEN = 3
24 MIN_MATCH_SIZE = 2
25 MIN_LITERALS_SIZE = 0
27 #define PACKED_ONLY // assume no copy block, optional
28 //#define PARANOIA // cover rare cases, optional
30 lzsa2main:
31 #ifdef PARANOIA
32 cld
33 #endif
34 #ifndef RAW_FORMAT
35 # if defined(PARANOIA) && !defined(FLAT32) && !defined(FLAT16)
36 xorw %cx, %cx
37 call normalize
38 # endif
39 # ifndef NO_LZSA2_HEADER
40 lodsw
41 cmpw $0x9E7B, %ax // magic
42 jne lzsa2main
43 lodsb
44 testb $0x20, %al // lzsa2
45 je lzsa2main
46 # endif
47 lzsa2block: // uncompress chunk
48 # if !defined(FLAT32) && !defined(FLAT16)
49 xorw %cx, %cx
50 call normalize
51 # endif
52 lodsw // block size
53 xchgw %ax, %cx
54 lodsb
55 # ifndef PACKED_ONLY
56 orb %al, %al
57 jns lzsa2compressed
58 # if !defined(FLAT32) && !defined(FLAT16OUT)
59 movw %cx, %dx
60 movb $0, %cl
61 movb $0, %dh
62 copytail:
63 call lzsa1movStr
64 xchg %dx, %cx
65 incw %cx
66 loop copytail
67 # else
68 movsb // copy block
69 copylp:
70 movsb // copy block
71 loop copylp // handle 64K case
72 # endif
73 jmp lzsa2block
74 lzsa2compressed:
75 jne lzsa2chunk // 64Kb block
76 # endif
77 jcxz lzsa2quit // bail if we hit EOD
78 # if !defined(FLAT16)
79 movw %cx, %dx
80 xorw %cx, %cx
81 call normalize
82 # define NeedNormalize
83 addw %si, %dx
84 # else
85 movw %si, %dx
86 addw %cx, %dx
87 # endif
88 #else
89 # if !defined(FLAT16)
90 xorw %cx, %cx
91 call normalize
92 # define NeedNormalize
93 # endif
94 #endif
95 movb $0, %bh // no nibble stored
96 lzsa2chunk: // uncompress chunk
97 lodsb // get token XYZ|LL|MMM
98 movb %al, %bl // keep token in bl
99 movw $LITERALS_RUN_LEN+256*MIN_LITERALS_SIZE, %cx
100 shrb %cl, %al // shift literals length into place
101 call lzsa2len // %cl = LITERALS_RUN_LEN
102 #if defined(NeedNormalize) && defined(PARANOIA)
103 pushw %bp
104 call lzsa2movLit // copy %cx literals from %ds:%si to %es:%di
105 popw %bp
106 #else
107 rep movsb // copy %cx literals from %ds:%si to %es:%di
108 #endif
109 #ifndef RAW_FORMAT
110 maxsi:
111 cmpw %dx, %si
112 jae lzsa2block // bail if we hit EOD
113 #endif
114 #ifdef FLAT32
115 orl $-1, %eax // set offset bits 31-8 to 1
116 #else
117 movb $-1, %ah // set offset bits 15-8 to 1
118 #endif
119 // XYZ
120 testb $0xC0, %bl // check match offset mode in token (X bit)
121 bt $5, %bx // move bit 5 to carry
122 js rep_match_or_large_offset
123 jne offset_9_bit
124 // 00Z 5-bit offset: read a nibble for offset bits 1-4 and use the inverted bit Z of the token as bit 0 of the offset.
125 // set bits 5-15 of the offset to 1.
126 call getByteFromNibbleAndC
127 jmp get_match_length
128 offset_9_bit:
129 // 01Z 9-bit offset: read a byte for offset bits 0-7 and use the inverted bit Z for bit 8 of the offset.
130 // set bits 9-15 of the offset to 1.
131 sbbb %cl, %ah // clear bit 8 if Z bit is clear
132 jmp get_match_length_0
134 getNibble:
135 xorb $0xF0, %bh // toggle nibble stored flags
136 movb %bh, %al
137 jns gotnibble
138 lodsb
139 movb $0xF0, %bh
140 orb %al, %bh
141 shrb $4, %al
142 gotnibble:
143 lzsa2quit:
144 ret
146 rep_match_or_large_offset:
147 jpe rep_match_or_16_bit
148 //10Z 13-bit offset: read a nibble for offset bits 9-12 and use the inverted bit Z for bit 8 of the offset,
149 // then read a byte for offset bits 0-7. set bits 13-15 of the offset to 1.
150 // substract 512 from the offset to get the final value.
151 call getByteFromNibbleAndC
152 subb $2, %al // substract 512
153 jmp get_match_length_1
154 rep_match_or_16_bit:
155 jc repeat_match // rep-match
156 //110 16-bit offset: read a byte for offset bits 8-15, then another byte for offset bits 0-7.
157 lodsb // Get 2-byte match offset
158 get_match_length_1:
159 xchgb %al, %ah
160 get_match_length_0:
161 lodsb // load match offset bits 0-7
162 get_match_length:
163 xchgw %ax, %bp // bp: offset
164 repeat_match:
165 //111 repeat offset: reuse the offset value of the previous match command.
167 movb %bl, %al // %al: original token
168 movw $MATCH_RUN_LEN+256*MIN_MATCH_SIZE, %cx
169 call lzsa2len
170 #ifdef RAW_FORMAT
171 jz lzsa2quit // bail if we hit EOD
172 #endif
173 #if !defined(FLAT32) && !defined(FLAT16OUT)
174 pushw %ds
175 pushw %si
176 movw %di, %si
177 addw %bp, %si
178 movw %es, %ax
179 jc axok
180 subb $0x10, %ah
181 axok:
182 .macro norm reg
183 movw %si, \reg
184 andw $0xF, %si
185 shrw $4, \reg
186 addw \reg, %ax
187 movw %ax, %ds
188 lzsa2movLit:
189 movw %di, \reg
190 andw $0xF, %di
191 shrw $4, \reg
192 movw %es, %ax
193 addw \reg, %ax
194 movw %ax, %es
195 .endm
196 pushw %bp
197 # if defined(NeedNormalize) || defined(PARANOIA)
198 call lzsa2movStr // copy string
199 # else
200 norm %bp
201 rep movsb
202 # endif
203 popw %bp
204 popw %si
205 popw %ds
206 #else
207 xchg AX, SI // save %si
208 lea (BP,DI), SI
209 rep movsb %es:(SI), %es:(DI)
210 xchg AX, SI // restore %si
211 #endif
212 jmp lzsa2chunk
214 getByteFromNibbleAndC:
215 pushfw
216 call getNibble // get nibble for offset bits 0-3
217 popfw
218 rclb $1, %al
219 xorb $0xE1, %al // set offset bits 7-5 to 1
220 ret
222 lzsa2len: // get length in %cx
223 andb %cl, %al
224 cbw // clear %ah
225 cmpb %al, %cl
226 jne lzsa2minNumber // S=0-2, L=0-6
227 call getNibble
228 cmp $0xF, %al
229 jne lzsa2noExtraByte
230 addb %al, %ch
231 lodsb
232 lzsa2noExtraByte:
233 addb %cl, %ch
234 lzsa2minNumber:
235 addb %ch, %al
236 jnc lzsa2gotNumber // 0-255
237 #if 0
238 je lzsa2BigNumber
239 movb %al, %ah // S=256-767, L=256-1791
240 lodsb
241 .byte 0xB1 // mask lodsb with movb $0xAD, %cl
242 lzsa2BigNumber:
243 #endif
244 lodsw // 0-65535
245 lzsa2gotNumber:
246 xchgw %ax, %cx
247 ret
249 #if defined(NeedNormalize) || defined(PARANOIA)
250 # if defined(PARANOIA)
251 lzsa2movlp:
252 decw %ch
253 rep movsb
254 incw %ch
255 # endif
256 normalize:
257 movw %ds, %bp
258 lzsa2movStr:
259 norm %bp
260 # if defined(PARANOIA)
261 cmpb $0xFF, %ch // catch FFFX case
262 je lzsa2movlp
263 # endif
264 rep movsb
265 ret
266 #endif