-
Notifications
You must be signed in to change notification settings - Fork 0
/
metatile_test.mfk
476 lines (371 loc) · 17 KB
/
metatile_test.mfk
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// compile with -t nes_small_v
import nes_joy
const word ppu_nametable0 = $2000
const word ppu_nametable1 = $2400
const word ppu_nametable2 = $2800
const word ppu_nametable3 = $2C00
const word ppu_attrtable0 = $23C0
const word ppu_attrtable1 = $27C0
const word ppu_attrtable2 = $2BC0
const word ppu_attrtable3 = $2FC0
const word ppu_pallete_ram = $3F00
array oam_buffer [256] @$200 // sprite buffer
byte nametable
word current_column
pointer screen_pointer
void main() {
init_sprites()
load_palletes()
nametable = 0
current_column = 0
screen_pointer = screen1_metatiles.addr
//screen 1
draw_full_screen(0,0)
//screen 2
draw_full_screen(1,16)
//screen 1 attribute table
load_attr_column(0,0,0)
load_attr_column(1,0,1)
load_attr_column(2,0,2)
load_attr_column(3,0,3)
load_attr_column(4,0,4)
load_attr_column(5,0,5)
load_attr_column(6,0,6)
load_attr_column(7,0,7)
//screen 2 attribute table
load_attr_column(0,1,8)
load_attr_column(1,1,9)
load_attr_column(2,1,10)
load_attr_column(3,1,11)
load_attr_column(4,1,12)
load_attr_column(5,1,13)
load_attr_column(6,1,14)
load_attr_column(7,1,15)
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() {
ppu_oam_dma_write(oam_buffer.addr.hi)
ppu_set_scroll(0,0)
ppu_ctrl = %10010000
ppu_mask = %00011110
}
void irq() {
}
void load_attr_column(byte screen_column, byte nametable, byte attrs_column) {
byte i
pointer attrs_ptr
attrs_ptr = get_attrs_column(attrs_column)
//set ppu address increment to 1 so we can draw the left and right borders
//(allows us to draw to the nametable in vertical strips rather than horizontal)
ppu_ctrl = %00000000
read_ppu_status()
for i,0,until,$08 {
if nametable == 0 {
ppu_set_addr(ppu_attrtable0 + i*8 + screen_column)
}
else if nametable == 1 {
ppu_set_addr(ppu_attrtable1 + i*8 + screen_column)
}
else if nametable == 2 {
ppu_set_addr(ppu_attrtable2 + i*8 + screen_column)
}
else if nametable == 3 {
ppu_set_addr(ppu_attrtable3 + i*8 + screen_column)
}
ppu_write_data(attrs_ptr[i])
}
}
inline pointer get_attrs_column(byte metatiles_column) {
pointer attrs_ptr
attrs_ptr = metatiles_column
attrs_ptr *= $08
attrs_ptr += attrs
return attrs_ptr
}
void draw_full_screen(byte nametable, byte metatile_column) {
draw_column(0,nametable,metatile_column)
metatile_column += 1
draw_column(1,nametable,metatile_column)
metatile_column += 1
draw_column(2,nametable,metatile_column)
metatile_column += 1
draw_column(3,nametable,metatile_column)
metatile_column += 1
draw_column(4,nametable,metatile_column)
metatile_column += 1
draw_column(5,nametable,metatile_column)
metatile_column += 1
draw_column(6,nametable,metatile_column)
metatile_column += 1
draw_column(7,nametable,metatile_column)
metatile_column += 1
draw_column(8,nametable,metatile_column)
metatile_column += 1
draw_column(9,nametable,metatile_column)
metatile_column += 1
draw_column(10,nametable,metatile_column)
metatile_column += 1
draw_column(11,nametable,metatile_column)
metatile_column += 1
draw_column(12,nametable,metatile_column)
metatile_column += 1
draw_column(13,nametable,metatile_column)
metatile_column += 1
draw_column(14,nametable,metatile_column)
metatile_column += 1
draw_column(15,nametable,metatile_column)
}
void draw_column(byte screen_column, byte nametable, byte metatiles_column) {
byte i
pointer metatiles_ptr
screen_column *= 2
metatiles_ptr = get_metatile_column(metatiles_column)
//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 = %00000100
read_ppu_status()
if nametable == 0 {
ppu_set_addr(ppu_nametable0 + $80 + screen_column)
}
else if nametable == 1 {
ppu_set_addr(ppu_nametable1 + $80 + screen_column)
}
else if nametable == 2 {
ppu_set_addr(ppu_nametable2 + $80 + screen_column)
}
else if nametable == 3 {
ppu_set_addr(ppu_nametable3 + $80 + screen_column)
}
//skip to $02 because the metatile data is structured to ignore the
//first two metatiles, and end before $0F because that byte is a junk
//byte as well
for i,$02,until,$0F {
draw_metatile_first_column(metatiles_ptr[i])
}
if nametable == 0 {
ppu_set_addr(ppu_nametable0 + $80 + screen_column + 1)
}
else if nametable == 1 {
ppu_set_addr(ppu_nametable1 + $80 + screen_column + 1)
}
else if nametable == 2 {
ppu_set_addr(ppu_nametable2 + $80 + screen_column + 1)
}
else if nametable == 3 {
ppu_set_addr(ppu_nametable3 + $80 + screen_column + 1)
}
//skip to $02 because the metatile data is structured to ignore the
//first two metatiles, and end before $0F because that byte is a junk
//byte as well
for i,$02,until,$0F {
draw_metatile_second_column(metatiles_ptr[i])
}
}
inline pointer get_metatile_column(byte metatiles_column) {
pointer metatiles_ptr
metatiles_ptr = metatiles_column
metatiles_ptr *= $10
metatiles_ptr += screen1_metatiles
return metatiles_ptr
}
inline void draw_metatile_first_column(byte metatile) {
pointer metatile_ptr
metatile_ptr = metatiles
metatile_ptr += metatile*4
ppu_write_data(metatile_ptr[0])
ppu_write_data(metatile_ptr[1])
}
inline void draw_metatile_second_column(byte metatile) {
pointer metatile_ptr
metatile_ptr = metatiles
metatile_ptr += metatile*4
ppu_write_data(metatile_ptr[2])
ppu_write_data(metatile_ptr[3])
}
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
}
}
}
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])
}
}
inline asm void ppu_wait_vblank() {
vblankwait:
BIT $2002
! BPL vblankwait
? RTS
}
const array pallete = [
$0F,$17,$18,$29,$34,$38,$18,$2A,$38,$39,$3A,$3B,$3C,$3D,$3E,$0F, //nametable palettes
$3C,$10,$37,$0F,$07,$10,$2D,$0F,$0F,$00,$0F,$36,$31,$02,$38,$3C //sprite palettes
]
//define each metatile
const array metatiles = [
//Tile positions:
//top-left bot-left top-right bot-right
$24, $24, $24, $24, // $00, should always be
// all background
$30, $31, $30, $31, // $01, basic top ground tile
$31, $31, $31, $31, // $02, all ground tile
$24, $32, $24, $24, // $03, floor flower tile
$24, $24, $24, $33, // $04, floor grass tile
$24, $36, $24, $37, // $05, bushes tile A
$24, $30, $24, $24 // $06, single-tile platform
]
//contains 4 screens worth of metatiles
//each metatile column is 16 bytes long
//each metatile column contains 3 bytes of wasted space, two at the top, and one at end
//(this is a holdover from one of my older projects, you could rearange it so that all
//the unused bytes are contiguous, you'd just have to change the loops in the draw_column functions)
const array screen1_metatiles = [
//note: all columns will have 3 wasted bytes
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $03, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $04, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $06, $00, $00, $00, $00,
$00, $00, $00, $00, $05, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $01, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $01, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $01, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $01, $00, $00, $00,
$00, $00, $00, $00, $03, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $01, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $01, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $01, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $01, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $01, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $03, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $04, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $01, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $01, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $01, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $01, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $01, $00, $00, $00, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $01, $00, $00, $00, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $03, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $04, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF, // 15 meta tiles to fill a column plus one filler tile
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $01, $02, $FF // 15 meta tiles to fill a column plus one filler tile
]
const array attrs = file("attrib.bin")
segment(chrrom) const array graphics @ $0000 = file("nesthing.chr")