-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle.s
More file actions
272 lines (223 loc) · 5.49 KB
/
Copy pathtoggle.s
File metadata and controls
272 lines (223 loc) · 5.49 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
;;;
;; Toggle between two nametables with smooth scrolling. You can press right/left
;; to move on that direction, or press Select as a toggle.
;;
;; The main take away is that the scroll is the last thing to be updated on
;; `nmi` code, and that the nametable on the PPU control register should be
;; update if the camera changed focus. In other words, if we gradually move into
;; the next nametable and the PPU scroll register wraps around; then the $00 on
;; that register doesn't mean to go over the same screen, but that it's $00 on
;; the other nametable. This is something that can be easily missed when
;; programming scrolling on the NES/Famicom for the first time.
;;
;; Besides initialization and setup functions like `setup_screens`, the most
;; relevant code is in `update` and `nmi`. Read the comments there carefully.
.segment "HEADER"
.byte 'N', 'E', 'S', $1A
.byte $02, $01
.byte $01, $00
.segment "VECTORS"
.addr nmi, reset, irq
.segment "CHARS"
.incbin "../assets/diskun.chr"
.segment "CODE"
.include "../shared/diskun.s"
.include "./include/apu.s"
.include "./include/ppu.s"
.include "./include/oam.s"
.include "./include/globals.s"
.include "../shared/clear.s"
;; Variables used on this example.
.scope Vars
;; Whether we are scrolling or not.
zp_scrolling = $A0
;; Right = 1; Left = 0.
zp_direction = $A1
;; Scroll value on the X axis.
zp_scroll = $A2
.endscope
;; Setup both nametables to contain the background indications that we want.
;; It's not much, just a square with a number to identify each screen.
.proc setup_screens
CLEAR_SCREENS $20, $24
WRITE_PPU_DATA $21AE, $31
WRITE_PPU_DATA $21AF, $31
WRITE_PPU_DATA $21CE, $31
WRITE_PPU_DATA $21CF, $31
WRITE_PPU_DATA $25AE, $32
WRITE_PPU_DATA $25AF, $32
WRITE_PPU_DATA $25CE, $32
WRITE_PPU_DATA $25CF, $32
rts
.endproc
.proc update
;; Are we already scrolling? If so just skip this.
lda Vars::zp_scrolling
bne @end
;; Read the joypad.
READ_JOYPAD1
;; Is the player pressing left? If so then the direction is to the left.
lda #Joypad::BUTTON_LEFT
and Joypad::zp_buttons1
beq @check_right
inc Vars::zp_scrolling
lda #0
sta Vars::zp_direction
rts
@check_right:
;; Is the player pressing right? If so then the direction is to the right.
lda #Joypad::BUTTON_RIGHT
and Joypad::zp_buttons1
beq @check_select
inc Vars::zp_scrolling
lda #1
sta Vars::zp_direction
rts
@check_select:
;; Is the player pressing Select? Then the direction depends on the current
;; nametable.
lda #Joypad::BUTTON_SELECT
and Joypad::zp_buttons1
beq @end
inc Vars::zp_scrolling
ldx #0
lda PPU::zp_control
and #%00000001
bne @left
inx
@left:
stx Vars::zp_direction
@end:
rts
.endproc
.proc main
lda #0
sta Globals::zp_flags
sta Vars::zp_scrolling
sta Vars::zp_direction
sta Vars::zp_scroll
jsr setup_screens
jsr Diskun::init_palettes
cli
lda #%10001000
sta PPU::zp_control
sta PPU::CONTROL
lda #%00011110
sta PPU::MASK
@main_game_loop:
jsr update
lda #%10000000
ora Globals::zp_flags
sta Globals::zp_flags
@wait_for_render:
bit Globals::zp_flags
bmi @wait_for_render
jmp @main_game_loop
.endproc
.proc nmi
bit Globals::zp_flags
bpl @next
pha
txa
pha
tya
pha
;; Are we scrolling at all? If not, then just go to `@set_scroll` which will
;; simply set the scroll to 0 again.
lda Vars::zp_scrolling
beq @set_scroll
;; We are scrolling... on which direction?
lda Vars::zp_direction
beq @left
;; If we are scrolling right, flip the nametable if we already reached zero.
;; Otherwise we can set the new scroll value.
inc Vars::zp_scroll
beq @flip_nametable
jmp @set_scroll
@left:
;; If we are scrolling left, and it's actually the first step, change the
;; nametable already. Otherwise set the new scroll value.
dec Vars::zp_scroll
lda #$FF
cmp Vars::zp_scroll
bne @set_scroll
@flip_nametable:
;; Reset the PPU address latch.
bit PPU::STATUS
;; Flip the current nametable.
lda PPU::zp_control
eor #%00000001
sta PPU::zp_control
sta PPU::CONTROL
@set_scroll:
;; Set whatever was computed as the new scroll. Note that `zp_scrolling` can
;; be set at the same value, as we only care whenever it reaches 0, when it
;; should stop scrolling.
lda Vars::zp_scroll
sta Vars::zp_scrolling
sta PPU::SCROLL
lda #$00
sta PPU::SCROLL
;; And the rest as usual.
lda #%01111111
and Globals::zp_flags
sta Globals::zp_flags
pla
tay
pla
tax
pla
@next:
rti
.endproc
.proc reset
sei
cld
ldx #$40
stx APU::FRAME_COUNTER
ldx #$FF
txs
inx
stx PPU::CONTROL
stx PPU::MASK
stx APU::DMC
bit PPU::STATUS
@vblankwait1:
bit PPU::STATUS
bpl @vblankwait1
ldx #0
lda #0
@ram_reset_loop:
sta $000, x
sta $100, x
sta $300, x
sta $400, x
sta $500, x
sta $600, x
sta $700, x
inx
bne @ram_reset_loop
lda #$EF
@sprite_reset_loop:
sta $200, x
inx
bne @sprite_reset_loop
OAM_WRITE_SPRITES
@vblankwait2:
bit PPU::STATUS
bpl @vblankwait2
lda #$3F
sta PPU::ADDRESS
lda #$00
sta PPU::ADDRESS
lda #$0F
ldx #$20
@palettes_reset_loop:
sta PPU::DATA
dex
bne @palettes_reset_loop
jmp main
.endproc
.proc irq
rti
.endproc