-
Notifications
You must be signed in to change notification settings - Fork 0
/
wle.asm
196 lines (176 loc) · 2.93 KB
/
wle.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
; Walle Length Encoding decoder
z_wle_src = 0
z_wle_dst = 2
wle_hold = 4
decompress
; Decompress WLE data from z_wle_src to z_wle_dst
; then return the decompressed data length in xy
lda #0
sta wle_hold
; save this for later length calculation
lda z_wle_dst+1
pha
lda z_wle_dst
pha
_loop
ldy #0
lda (z_wle_src),y
iny
tax
and #$c0
beq _literal
cmp #$40
beq _repeat
cmp #$80
beq _increment
_copy
cpx #$ff
beq _done
lda z_wle_src+1
pha
lda z_wle_src
pha
; z_wle_src = z_wle_dst - *(z_wle_src) - 1
lda z_wle_dst
clc ; this is correct
sbc (z_wle_src),y
sta z_wle_src
lda z_wle_dst+1
sbc #0
sta z_wle_src+1
txa
and #$3f
tay
iny
jsr _memcpy_short
jsr _add_dst_y
pla
sta z_wle_src
pla
sta z_wle_src+1
ldy #2
jsr _add_src_y
jmp _loop
_literal
jsr _get_length_and_load_extra_flag
bne +
; short count
txa
pha
ldx #0
beq ++
+ ; long count
lda (z_wle_src),y
iny
pha
+ jsr _add_src_y
pla
tay
iny
bne +
inx
+ jsr _memcpy
; _memcpy doesn't add Y to to src and dst in the final stage
jsr _add_src_y
jsr _add_dst_y
jmp _loop
_repeat
jsr _repeat_increment_common
- sta (z_wle_dst),y
iny
dex
bne -
jsr _add_dst_y
jmp _loop
_increment
jsr _repeat_increment_common
- sta (z_wle_dst),y
clc
adc #1
iny
dex
bne -
sta wle_hold
jsr _add_dst_y
jmp _loop
_done
; length = (z_wle_dst) - (<TOS>)
pla
eor #$ff
sec
adc z_wle_dst
tay
pla
eor #$ff
adc z_wle_dst+1
tax
rts
_get_length_and_load_extra_flag
txa
and #$20
php
txa
and #$1f
tax
plp
rts
_repeat_increment_common
jsr _get_length_and_load_extra_flag
beq +
lda (z_wle_src),y
iny
sta wle_hold
+ jsr _add_src_y
lda wle_hold
inx
ldy #0
rts
_add_src_y
; z_wle_src += y
tya
clc
adc z_wle_src
sta z_wle_src
bcc +
inc z_wle_src+1
+ rts
_add_dst_y
; z_wle_dst += y
tya
clc
adc z_wle_dst
sta z_wle_dst
bcc +
inc z_wle_dst+1
+ rts
_memcpy
; Copy (z_wle_src) to (z_wle_dst) with length xy
txa
beq _memcpy_short
tya
pha
ldy #0
- .rept 2
lda (z_wle_src),y
sta (z_wle_dst),y
iny
.next
bne -
inc z_wle_src+1
inc z_wle_dst+1
dex
bne -
pla
tay
; fall through
_memcpy_short
; Copy (z_wle_src) to (z_wle_dst) with length y
tya
tax
ldy #0
- lda (z_wle_src),y
sta (z_wle_dst),y
iny
dex
bne -
rts