-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathunpack.s
More file actions
258 lines (215 loc) · 4.32 KB
/
Copy pathunpack.s
File metadata and controls
258 lines (215 loc) · 4.32 KB
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
; Packed source data adress
#define ptr_source tmp0
; Destination adress where we depack
#define ptr_destination tmp1
; Point on the end of the depacked stuff
#define ptr_destination_end tmp2
; Temporary used to hold a pointer on depacked stuff
#define ptr_source_back tmp3
; Temporary
#define offset tmp4
#define mask_value reg0
#define nb_src reg1
#define nb_dst reg2
UnpackError
rts
UnpackLetters .byt "LZ77"
; file_unpack_raw(void *ptr_dst,void *ptr_src,uint decompressed_size)
_file_unpack_raw
.(
; Destination adress
ldy #0
lda (sp),y
sta ptr_destination
iny
lda (sp),y
sta ptr_destination+1
; Source adress
ldy #2
lda (sp),y
sta ptr_source
iny
lda (sp),y
sta ptr_source+1
; Get the unpacked size, and add it to the destination
; adress in order to get the end adress.
ldy #4
clc
lda ptr_destination
adc (sp),y
sta ptr_destination_end+0
iny
lda ptr_destination+1
adc (sp),y
sta ptr_destination_end+1
jmp start_unpack
.)
; void file_unpack(void *ptr_dst,void *ptr_src)
_file_unpack
.(
ldy #0
lda (sp),y
sta ptr_destination
iny
lda (sp),y
sta ptr_destination+1
; Source adress
ldy #2
lda (sp),y
sta ptr_source
iny
lda (sp),y
sta ptr_source+1
; Test if it's LZ77
ldy #3
.(
test_lz77
lda (ptr_source),y
cmp UnpackLetters,y
bne UnpackError
dey
bpl test_lz77
.)
; Get the unpacked size, and add it to the destination
; adress in order to get the end adress.
ldy #4
clc
lda ptr_destination
adc (ptr_source),y
sta ptr_destination_end+0
iny
lda ptr_destination+1
adc (ptr_source),y
sta ptr_destination_end+1
; Move the source pointer ahead to point on packed data (+0)
clc
lda ptr_source
adc #8
sta ptr_source
lda ptr_source+1
adc #0
sta ptr_source+1
+start_unpack
; Initialise variables
; We try to keep "y" null during all the code,
; so the block copy routine has to be sure that
; y is null on exit
ldy #0
lda #1
sta mask_value
unpack_loop
; Handle bit mask
lsr mask_value
bne end_reload_mask
; Read from source stream
lda (ptr_source),y
.(
; Move stream pointer (one byte)
inc ptr_source
bne skip
inc ptr_source+1
skip
.)
ror
sta mask_value
end_reload_mask
bcc back_copy
write_byte
; Copy one byte from the source stream
lda (ptr_source),y
sta (ptr_destination),y
.(
; Move stream pointer (one byte)
inc ptr_source
bne skip
inc ptr_source+1
skip
.)
lda #1
sta nb_dst
_UnpackEndLoop
;// We increase the current destination pointer,
;// by a given value, white checking if we reach
;// the end of the buffer.
clc
lda ptr_destination
adc nb_dst
sta ptr_destination
.(
bcc skip
inc ptr_destination+1
skip
.)
cmp ptr_destination_end
lda ptr_destination+1
sbc ptr_destination_end+1
bcc unpack_loop
rts
back_copy
;BreakPoint jmp BreakPoint
; Copy a number of bytes from the already unpacked stream
; Here we know that y is null. So no need for clearing it.
; Just be sure it's still null at the end.
; At this point, the source pointer points to a two byte
; value that actually contains a 4 bits counter, and a
; 12 bit offset to point back into the depacked stream.
; The counter is in the 4 high order bits.
;clc <== No need, since we access this routie from a BCC
lda (ptr_source),y
adc #1
sta offset
iny
lda (ptr_source),y
tax
and #$0f
adc #0
sta offset+1
txa
lsr
lsr
lsr
lsr
clc
adc #3
sta nb_dst
sec
lda ptr_destination
sbc offset
sta ptr_source_back
lda ptr_destination+1
sbc offset+1
sta ptr_source_back+1
; Beware, in that loop, the direction is important
; since RLE like depacking is done by recopying the
; very same byte just copied... Do not make it a
; reverse loop to achieve some speed gain...
; Y was equal to 1 after the offset computation,
; a simple decrement is ok to make it null again.
dey
.(
copy_loop
lda (ptr_source_back),y ; Read from already unpacked stream
sta (ptr_destination),y ; Write to destination buffer
iny
cpy nb_dst
bne copy_loop
.)
ldy #0
;// C=1 here
lda ptr_source
adc #1
sta ptr_source
bcc _UnpackEndLoop
inc ptr_source+1
bne _UnpackEndLoop
rts
.)
; Taille actuelle du code 279 octets
; 0x08d7 - 0x07e8 => 239 octets
; 0x08c8 - 0x07e5 => 227 octets
; 0x08d5 - 0x0800 => 213 octets
; 0x08c9 - 0x0800 => 201 octets
; 0x08c5 - 0x0800 => 197 octets
; 0x08c3 - 0x0800 => 195 octets
; 0x08c0 - 0x0800 => 192 octets
; => 146 octets