-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
background.asm
173 lines (136 loc) · 2.46 KB
/
background.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
; Load background from RAM into PPU
UpdateBackground:
bit bgUpdateFlags ; do not set this in NMI
bpl ubPPUOn
; TODO: use a constant for this
lda #PPU_OFF
sta $2001
ubPPUOn:
bit $2002 ; reset high/low latch
; if %0100 0000, don't read from queue
bit bgUpdateFlags
bvs ubLoop
lda #$00
sta bgPointer
lda #$04
sta bgPointer+1
ubLoop:
lda #0
sta bgUpdateFlags
ldy #$00 ; byte in the packet
; Packet length
lda (bgPointer), y
sta bgLength
bne ub_ok
; TODO: use a constant for this
lda #PPU_ON
sta $2001
rts
ub_ok:
iny
lda (bgPointer), y
sta bgFlags
bit bgFlags
bvs ubSkipAddr
; PPU Address
iny
lda (bgPointer), y
sta $2006
iny
lda (bgPointer), y
sta $2006
ubSkipAddr:
bit bgFlags
bmi ubRunLength
ubDataLoop:
iny
lda (bgPointer), y
sta $2007
dec bgLength
bne ubDataLoop
jmp ubNextPacket
ubRunLength:
iny
lda (bgPointer), y
ubRunLengthLoop:
sta $2007
dec bgLength
bne ubRunLengthLoop
ubNextPacket:
; Update pointer to next packet
tya
clc
adc #$01
adc bgPointer
sta bgPointer
lda bgPointer+1
adc #0
sta bgPointer+1
jmp ubLoop
; Load data into RAM
LoadBackgroundData:
; queue address is not reset here
lda bgWrites
beq lbLoop
; If something was loaded into the queue previously,
; remove it's terminator byte
dec bgQueue
lbLoop:
; bgPointer needs to be set before calling this
ldy #0
; Packet length
lda (bgPointer), y
sta bgLength
sta (bgQueue), y
bne lb_ok
; Set the bgWrites flag
inc bgWrites
rts
lb_ok:
iny
lda (bgPointer), y
sta bgFlags
sta (bgQueue), y
bit bgFlags
bvs lbSkipAddr
; PPU Address
iny
lda (bgPointer), y
sta (bgQueue), y
iny
lda (bgPointer), y
sta (bgQueue), y
lbSkipAddr:
bit bgFlags
bmi lbRunLength
lbDataLoop:
iny
lda (bgPointer), y
sta (bgQueue), y
dec bgLength
bne lbDataLoop
jmp lbNextPacket
lbRunLength:
iny
lda (bgPointer), y
sta (bgQueue), y
lbNextPacket:
; Update pointer and queue to next packet
tya
clc
adc #1
adc bgPointer
sta bgPointer
lda bgPointer+1
adc #0
sta bgPointer+1
; update queue pointer
tya
clc
adc #1
adc bgQueue
sta bgQueue
lda bgQueue+1
adc #0
sta bgQueue+1
jmp lbLoop