-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrle.s
66 lines (59 loc) · 1.47 KB
/
rle.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
; Konami RLE decompressor
;
; Encoding:
; $00 Unsupported (useless)
; <= $80 Repeat next byte n times
; $FF End
; > $80 Next (n-128) bytes are literal
addrLo := $0000
addrHi := addrLo+1
PPUDATA := $2007
; Decodes Konami RLE-encoded stream with address stored at $0000.
;
; Does not support 0-length runs; control byte $00 is not supported
rleDecodeToPpu:
; y is current input offset
; x is chunk length remaining
.export rleDecodeToPpu
ldy #$00
@processChunk:
lda (addrLo),y
cmp #$81
bmi @runLength
cmp #$FF
beq @ret
and #$7F
; literalLength
tax
@literalLoop:
iny
lda (addrLo),y
sta PPUDATA
dex
bne @literalLoop
beq @preventYOverflow
@runLength:
tax
iny
lda (addrLo),y
@runLengthLoop:
sta PPUDATA
dex
bne @runLengthLoop
@preventYOverflow:
; The largest input chunk size is literal with a length of 126, which
; is 127 bytes of input. We make sure adding 127 to y does not
; overflow. This allows us to ignore y overflow during loops.
iny
bpl @processChunk
; y is at risk of overflowing next chunk
tya
ldy #$00
clc
adc addrLo
sta addrLo
bcc @processChunk
inc addrHi
jmp @processChunk
@ret:
rts