-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll_multiple_screens.mfk
More file actions
430 lines (386 loc) · 11.9 KB
/
scroll_multiple_screens.mfk
File metadata and controls
430 lines (386 loc) · 11.9 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// compile with -t nes_small_v
import nes_joy
const word ppu_nametable_ram = $2000
const word ppu_pallete_ram = $3F00
bool scrolling_right
bool v_scrolling
byte v_scroll_dir
array oam_buffer [256] @$200 // sprite buffer
byte xscroll
byte yscroll
byte nametable
byte screencounter
byte border_draw_counter
void main() {
init_sprites()
load_palletes()
draw_screen_nametable1(screen1.addr)
xscroll = 0
nametable = 0
screencounter = 0
scrolling_right = true
border_draw_counter = 0
v_scrolling = false
v_scroll_dir = 0
yscroll = 0
ppu_set_scroll(0,0)
ppu_wait_vblank()
ppu_ctrl = %10010000 // enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
ppu_mask = %00011110 // enable sprites, enable background, no clipping on left side
while(true) {}
}
void nmi() {
//Push sprite information to the PPU through DMA transfer
ppu_oam_dma_write(oam_buffer.addr.hi)
if v_scrolling {
v_scroll()
ppu_set_scroll(xscroll,yscroll)
return
}
read_joy1()
if input_start != 0 {
scrolling_right = false
}
else {
scrolling_right = true
}
if input_dy > 0 && xscroll == 0 {
v_scroll_dir = 0
v_scrolling = true
}
if input_dy < 0 && xscroll == 0 {
v_scroll_dir = 1
v_scrolling = true
}
if xscroll & %00000111 == 0 {
// on an 8 pixel boundary, so
// load the appropriate tiles
border_draw_counter = 1
}
if border_draw_counter == 1 {
if scrolling_right {
load_new_right_column()
}
border_draw_counter += 1
}
else if border_draw_counter == 2 {
if not(scrolling_right) {
load_new_left_column()
}
border_draw_counter = 0
}
if scrolling_right {
xscroll += 1
}
else {
xscroll -= 1
}
if scrolling_right {
if xscroll == 0 {
nametable ^= %00000001
screencounter += 1
if screencounter >= 4 {
screencounter = 0
}
}
}
else {
if xscroll == $FF {
nametable ^= %00000001
if screencounter == 0 {
screencounter = 3
}
else {
screencounter -= 1
}
}
}
ppu_set_scroll(xscroll,0)
ppu_ctrl = %10010000 | nametable
ppu_mask = %00011110
}
void irq() {
}
void v_scroll() {
if v_scroll_dir == 0 {
yscroll -= 2
}
else {
yscroll += 2
}
if yscroll >= $F0 {
if v_scroll_dir == 0 {
yscroll = $EE
}
else {
yscroll = 0
}
}
if yscroll == 0 {
v_scrolling = false
}
}
void load_new_left_column() {
pointer screen
byte screencounter_next
byte i
byte xscroll_div_8
byte nametable_temp
byte alignment
xscroll_div_8 = xscroll >> 3
if xscroll_div_8 == 0 {
if screencounter == 0 {
screencounter_next = 3
}
else {
screencounter_next = screencounter - 1
}
nametable_temp = nametable ^ %00000001
alignment = $1F
}
else {
screencounter_next = screencounter
nametable_temp = nametable
alignment = xscroll_div_8 - 1
}
if screencounter_next == 0 {
screen = screen1.addr
}
if screencounter_next == 1 {
screen = screen2.addr
}
if screencounter_next == 2 {
screen = screen3.addr
}
if screencounter_next == 3 {
screen = screen4.addr
}
screen += alignment
//set ppu address increment to 32 so we can draw the left and right borders
//(allows us to draw to the nametable in vertical strips rather than horizontal)
ppu_ctrl = %10010100
read_ppu_status()
if nametable_temp == 0 {
ppu_set_addr(ppu_nametable_ram + alignment)
}
else {
ppu_set_addr(ppu_nametable_ram + $0400 + alignment)
}
for i,0,until,$1D {
ppu_write_data(screen[0])
screen += $20
}
}
void load_new_right_column() {
pointer screen
byte screencounter_next
byte i
byte xscroll_div_8
if screencounter == 3 {
screencounter_next = 0
}
else {
screencounter_next = screencounter + 1
}
if screencounter_next == 0 {
screen = screen1.addr
}
if screencounter_next == 1 {
screen = screen2.addr
}
if screencounter_next == 2 {
screen = screen3.addr
}
if screencounter_next == 3 {
screen = screen4.addr
}
xscroll_div_8 = xscroll >> 3
screen += xscroll_div_8 //properly align the screen pointer
//set ppu address increment to 32 so we can draw the left and right borders
//(allows us to draw to the nametable in vertical strips rather than horizontal)
ppu_ctrl = %10010100
read_ppu_status()
if nametable == 0 {
ppu_set_addr(ppu_nametable_ram + $0400 + xscroll_div_8)
}
else {
ppu_set_addr(ppu_nametable_ram + xscroll_div_8)
}
for i,0,until,$1D {
ppu_write_data(screen[0])
screen += $20
}
}
void init_sprites() {
byte i
for i,0,to,255 {
if (i & %00000011) == 0 {
//each sprite takes up 4 bytes, and we want to edit
//the y position of each sprite (0th byte)
//so we use the %00000011 mask to write every 4th byte (every 0th sprite byte)
oam_buffer[i] = $ef // move the sprite off screen
}
else {
oam_buffer[i] = 0
}
}
}
inline asm void ppu_wait_vblank() {
vblankwait:
BIT $2002
! BPL vblankwait
? RTS
}
inline void draw_screen_nametable1(pointer screen) {
word i
read_ppu_status()
ppu_set_addr(ppu_nametable_ram)
for i,0,until,$3C0 {
ppu_write_data(screen[i])
}
}
inline void draw_screen_nametable2(pointer screen) {
word i
read_ppu_status()
ppu_set_addr(ppu_nametable_ram+$0400)
for i,0,until,$3C0 {
ppu_write_data(screen[i])
}
}
inline void load_palletes() {
byte i
read_ppu_status() // read PPU status to reset the high/low latch
ppu_set_addr(ppu_pallete_ram) // point the PPU to palette ram
for i,0,until,$20 {
ppu_write_data(pallete[i])
}
}
const array screen1 = [
"11111111111111111111111111111111" ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
"This is random text I'm typing i" ascii
]
const array screen2 = [
"22222222222222222222222222222222" ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
"n order to test out the scrollin" ascii
]
const array screen3 = [
"33333333333333333333333333333333" ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
"g. Hold down the start button to" ascii
]
const array screen4 = [
"44444444444444444444444444444444" ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" " ascii,
" scroll backwards.End of screen." ascii
]
const array pallete = [
$22,$29,$1A,$0F, $22,$36,$17,$0F, $22,$30,$21,$0F, $22,$27,$17,$0F, //background palette
$22,$1C,$15,$14, $22,$02,$38,$3C, $22,$1C,$15,$14, $22,$02,$38,$3C //sprite palette
]
segment(chrrom) const array graphics @ $0000 = file("tiles.chr")