forked from fogleman/nes
/
ppu.go
641 lines (584 loc) · 13.9 KB
/
ppu.go
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
package nes
import "image"
type PPU struct {
Memory // memory interface
console *Console // reference to parent object
Cycle int // 0-340
ScanLine int // 0-261, 0-239=visible, 240=post, 241-260=vblank, 261=pre
Frame uint64 // frame counter
// storage variables
paletteData [32]byte
nameTableData [2048]byte
oamData [256]byte
front *image.RGBA
back *image.RGBA
// PPU registers
v uint16 // current vram address (15 bit)
t uint16 // temporary vram address (15 bit)
x byte // fine x scroll (3 bit)
w byte // write toggle (1 bit)
f byte // even/odd frame flag (1 bit)
register byte
// NMI flags
nmiOccurred bool
nmiOutput bool
nmiPrevious bool
nmiDelay byte
// background temporary variables
nameTableByte byte
attributeTableByte byte
lowTileByte byte
highTileByte byte
tileData uint64
// sprite temporary variables
spriteCount int
spritePatterns [8]uint32
spritePositions [8]byte
spritePriorities [8]byte
spriteIndexes [8]byte
// $2000 PPUCTRL
flagNameTable byte // 0: $2000; 1: $2400; 2: $2800; 3: $2C00
flagIncrement byte // 0: add 1; 1: add 32
flagSpriteTable byte // 0: $0000; 1: $1000; ignored in 8x16 mode
flagBackgroundTable byte // 0: $0000; 1: $1000
flagSpriteSize byte // 0: 8x8; 1: 8x16
flagMasterSlave byte // 0: read EXT; 1: write EXT
// $2001 PPUMASK
flagGrayscale byte // 0: color; 1: grayscale
flagShowLeftBackground byte // 0: hide; 1: show
flagShowLeftSprites byte // 0: hide; 1: show
flagShowBackground byte // 0: hide; 1: show
flagShowSprites byte // 0: hide; 1: show
flagRedTint byte // 0: normal; 1: emphasized
flagGreenTint byte // 0: normal; 1: emphasized
flagBlueTint byte // 0: normal; 1: emphasized
// $2002 PPUSTATUS
flagSpriteZeroHit byte
flagSpriteOverflow byte
// $2003 OAMADDR
oamAddress byte
// $2007 PPUDATA
bufferedData byte // for buffered reads
}
func NewPPU(console *Console) *PPU {
ppu := PPU{Memory: NewPPUMemory(console), console: console}
ppu.front = image.NewRGBA(image.Rect(0, 0, 256, 240))
ppu.back = image.NewRGBA(image.Rect(0, 0, 256, 240))
ppu.Reset()
return &ppu
}
func (ppu *PPU) Reset() {
ppu.Cycle = 340
ppu.ScanLine = 240
ppu.Frame = 0
ppu.writeControl(0)
ppu.writeMask(0)
ppu.writeOAMAddress(0)
}
func (ppu *PPU) readPalette(address uint16) byte {
if address >= 16 && address%4 == 0 {
address -= 16
}
return ppu.paletteData[address]
}
func (ppu *PPU) writePalette(address uint16, value byte) {
if address >= 16 && address%4 == 0 {
address -= 16
}
ppu.paletteData[address] = value
}
func (ppu *PPU) readRegister(address uint16) byte {
switch address {
case 0x2002:
return ppu.readStatus()
case 0x2004:
return ppu.readOAMData()
case 0x2007:
return ppu.readData()
}
return 0
}
func (ppu *PPU) writeRegister(address uint16, value byte) {
ppu.register = value
switch address {
case 0x2000:
ppu.writeControl(value)
case 0x2001:
ppu.writeMask(value)
case 0x2003:
ppu.writeOAMAddress(value)
case 0x2004:
ppu.writeOAMData(value)
case 0x2005:
ppu.writeScroll(value)
case 0x2006:
ppu.writeAddress(value)
case 0x2007:
ppu.writeData(value)
case 0x4014:
ppu.writeDMA(value)
}
}
// $2000: PPUCTRL
func (ppu *PPU) writeControl(value byte) {
ppu.flagNameTable = (value >> 0) & 3
ppu.flagIncrement = (value >> 2) & 1
ppu.flagSpriteTable = (value >> 3) & 1
ppu.flagBackgroundTable = (value >> 4) & 1
ppu.flagSpriteSize = (value >> 5) & 1
ppu.flagMasterSlave = (value >> 6) & 1
ppu.nmiOutput = (value>>7)&1 == 1
ppu.nmiChange()
// t: ....BA.. ........ = d: ......BA
ppu.t = (ppu.t & 0xF3FF) | ((uint16(value) & 0x03) << 10)
}
// $2001: PPUMASK
func (ppu *PPU) writeMask(value byte) {
ppu.flagGrayscale = (value >> 0) & 1
ppu.flagShowLeftBackground = (value >> 1) & 1
ppu.flagShowLeftSprites = (value >> 2) & 1
ppu.flagShowBackground = (value >> 3) & 1
ppu.flagShowSprites = (value >> 4) & 1
ppu.flagRedTint = (value >> 5) & 1
ppu.flagGreenTint = (value >> 6) & 1
ppu.flagBlueTint = (value >> 7) & 1
}
// $2002: PPUSTATUS
func (ppu *PPU) readStatus() byte {
result := ppu.register & 0x1F
result |= ppu.flagSpriteOverflow << 5
result |= ppu.flagSpriteZeroHit << 6
if ppu.nmiOccurred {
result |= 1 << 7
}
ppu.nmiOccurred = false
ppu.nmiChange()
// w: = 0
ppu.w = 0
return result
}
// $2003: OAMADDR
func (ppu *PPU) writeOAMAddress(value byte) {
ppu.oamAddress = value
}
// $2004: OAMDATA (read)
func (ppu *PPU) readOAMData() byte {
return ppu.oamData[ppu.oamAddress]
}
// $2004: OAMDATA (write)
func (ppu *PPU) writeOAMData(value byte) {
ppu.oamData[ppu.oamAddress] = value
ppu.oamAddress++
}
// $2005: PPUSCROLL
func (ppu *PPU) writeScroll(value byte) {
if ppu.w == 0 {
// t: ........ ...HGFED = d: HGFED...
// x: CBA = d: .....CBA
// w: = 1
ppu.t = (ppu.t & 0xFFE0) | (uint16(value) >> 3)
ppu.x = value & 0x07
ppu.w = 1
} else {
// t: .CBA..HG FED..... = d: HGFEDCBA
// w: = 0
ppu.t = (ppu.t & 0x8FFF) | ((uint16(value) & 0x07) << 12)
ppu.t = (ppu.t & 0xFC1F) | ((uint16(value) & 0xF8) << 2)
ppu.w = 0
}
}
// $2006: PPUADDR
func (ppu *PPU) writeAddress(value byte) {
if ppu.w == 0 {
// t: ..FEDCBA ........ = d: ..FEDCBA
// t: .X...... ........ = 0
// w: = 1
ppu.t = (ppu.t & 0x80FF) | ((uint16(value) & 0x3F) << 8)
ppu.w = 1
} else {
// t: ........ HGFEDCBA = d: HGFEDCBA
// v = t
// w: = 0
ppu.t = (ppu.t & 0xFF00) | uint16(value)
ppu.v = ppu.t
ppu.w = 0
}
}
// $2007: PPUDATA (read)
func (ppu *PPU) readData() byte {
value := ppu.Read(ppu.v)
// emulate buffered reads
if ppu.v%0x4000 < 0x3F00 {
buffered := ppu.bufferedData
ppu.bufferedData = value
value = buffered
} else {
ppu.bufferedData = ppu.Read(ppu.v - 0x1000)
}
// increment address
if ppu.flagIncrement == 0 {
ppu.v += 1
} else {
ppu.v += 32
}
return value
}
// $2007: PPUDATA (write)
func (ppu *PPU) writeData(value byte) {
ppu.Write(ppu.v, value)
if ppu.flagIncrement == 0 {
ppu.v += 1
} else {
ppu.v += 32
}
}
// $4014: OAMDMA
func (ppu *PPU) writeDMA(value byte) {
cpu := ppu.console.CPU
address := uint16(value) << 8
for i := 0; i < 256; i++ {
ppu.oamData[ppu.oamAddress] = cpu.Read(address)
ppu.oamAddress++
address++
}
cpu.stall += 513
if cpu.Cycles%2 == 1 {
cpu.stall++
}
}
// NTSC Timing Helper Functions
func (ppu *PPU) incrementX() {
// increment hori(v)
// if coarse X == 31
if ppu.v&0x001F == 31 {
// coarse X = 0
ppu.v &= 0xFFE0
// switch horizontal nametable
ppu.v ^= 0x0400
} else {
// increment coarse X
ppu.v++
}
}
func (ppu *PPU) incrementY() {
// increment vert(v)
// if fine Y < 7
if ppu.v&0x7000 != 0x7000 {
// increment fine Y
ppu.v += 0x1000
} else {
// fine Y = 0
ppu.v &= 0x8FFF
// let y = coarse Y
y := (ppu.v & 0x03E0) >> 5
if y == 29 {
// coarse Y = 0
y = 0
// switch vertical nametable
ppu.v ^= 0x0800
} else if y == 31 {
// coarse Y = 0, nametable not switched
y = 0
} else {
// increment coarse Y
y++
}
// put coarse Y back into v
ppu.v = (ppu.v & 0xFC1F) | (y << 5)
}
}
func (ppu *PPU) copyX() {
// hori(v) = hori(t)
// v: .....F.. ...EDCBA = t: .....F.. ...EDCBA
ppu.v = (ppu.v & 0xFBE0) | (ppu.t & 0x041F)
}
func (ppu *PPU) copyY() {
// vert(v) = vert(t)
// v: .IHGF.ED CBA..... = t: .IHGF.ED CBA.....
ppu.v = (ppu.v & 0x841F) | (ppu.t & 0x7BE0)
}
func (ppu *PPU) nmiChange() {
nmi := ppu.nmiOutput && ppu.nmiOccurred
if nmi && !ppu.nmiPrevious {
// TODO: this fixes some games but the delay shouldn't have to be so
// long, so the timings are off somewhere
ppu.nmiDelay = 15
}
ppu.nmiPrevious = nmi
}
func (ppu *PPU) setVerticalBlank() {
ppu.front, ppu.back = ppu.back, ppu.front
ppu.nmiOccurred = true
ppu.nmiChange()
}
func (ppu *PPU) clearVerticalBlank() {
ppu.nmiOccurred = false
ppu.nmiChange()
}
func (ppu *PPU) fetchNameTableByte() {
v := ppu.v
address := 0x2000 | (v & 0x0FFF)
ppu.nameTableByte = ppu.Read(address)
}
func (ppu *PPU) fetchAttributeTableByte() {
v := ppu.v
address := 0x23C0 | (v & 0x0C00) | ((v >> 4) & 0x38) | ((v >> 2) & 0x07)
shift := ((v >> 4) & 4) | (v & 2)
ppu.attributeTableByte = ((ppu.Read(address) >> shift) & 3) << 2
}
func (ppu *PPU) fetchLowTileByte() {
fineY := (ppu.v >> 12) & 7
table := ppu.flagBackgroundTable
tile := ppu.nameTableByte
address := 0x1000*uint16(table) + uint16(tile)*16 + fineY
ppu.lowTileByte = ppu.Read(address)
}
func (ppu *PPU) fetchHighTileByte() {
fineY := (ppu.v >> 12) & 7
table := ppu.flagBackgroundTable
tile := ppu.nameTableByte
address := 0x1000*uint16(table) + uint16(tile)*16 + fineY
ppu.highTileByte = ppu.Read(address + 8)
}
func (ppu *PPU) storeTileData() {
var data uint32
for i := 0; i < 8; i++ {
a := ppu.attributeTableByte
p1 := (ppu.lowTileByte & 0x80) >> 7
p2 := (ppu.highTileByte & 0x80) >> 6
ppu.lowTileByte <<= 1
ppu.highTileByte <<= 1
data <<= 4
data |= uint32(a | p1 | p2)
}
ppu.tileData |= uint64(data)
}
func (ppu *PPU) fetchTileData() uint32 {
return uint32(ppu.tileData >> 32)
}
func (ppu *PPU) backgroundPixel() byte {
if ppu.flagShowBackground == 0 {
return 0
}
data := ppu.fetchTileData() >> ((7 - ppu.x) * 4)
return byte(data & 0x0F)
}
func (ppu *PPU) spritePixel() (byte, byte) {
if ppu.flagShowSprites == 0 {
return 0, 0
}
for i := 0; i < ppu.spriteCount; i++ {
offset := (ppu.Cycle - 1) - int(ppu.spritePositions[i])
if offset < 0 || offset > 7 {
continue
}
offset = 7 - offset
color := byte((ppu.spritePatterns[i] >> byte(offset*4)) & 0x0F)
if color%4 == 0 {
continue
}
return byte(i), color
}
return 0, 0
}
func (ppu *PPU) renderPixel() {
x := ppu.Cycle - 1
y := ppu.ScanLine
background := ppu.backgroundPixel()
i, sprite := ppu.spritePixel()
if x < 8 && ppu.flagShowLeftBackground == 0 {
background = 0
}
if x < 8 && ppu.flagShowLeftSprites == 0 {
sprite = 0
}
b := background%4 != 0
s := sprite%4 != 0
var color byte
if !b && !s {
color = 0
} else if !b && s {
color = sprite | 0x10
} else if b && !s {
color = background
} else {
if ppu.spriteIndexes[i] == 0 && x < 255 {
ppu.flagSpriteZeroHit = 1
}
if ppu.spritePriorities[i] == 0 {
color = sprite | 0x10
} else {
color = background
}
}
c := Palette[ppu.readPalette(uint16(color))%64]
ppu.back.SetRGBA(x, y, c)
}
func (ppu *PPU) fetchSpritePattern(i, row int) uint32 {
tile := ppu.oamData[i*4+1]
attributes := ppu.oamData[i*4+2]
var address uint16
if ppu.flagSpriteSize == 0 {
if attributes&0x80 == 0x80 {
row = 7 - row
}
table := ppu.flagSpriteTable
address = 0x1000*uint16(table) + uint16(tile)*16 + uint16(row)
} else {
if attributes&0x80 == 0x80 {
row = 15 - row
}
table := tile & 1
tile &= 0xFE
if row > 7 {
tile++
row -= 8
}
address = 0x1000*uint16(table) + uint16(tile)*16 + uint16(row)
}
a := (attributes & 3) << 2
lowTileByte := ppu.Read(address)
highTileByte := ppu.Read(address + 8)
var data uint32
for i := 0; i < 8; i++ {
var p1, p2 byte
if attributes&0x40 == 0x40 {
p1 = (lowTileByte & 1) << 0
p2 = (highTileByte & 1) << 1
lowTileByte >>= 1
highTileByte >>= 1
} else {
p1 = (lowTileByte & 0x80) >> 7
p2 = (highTileByte & 0x80) >> 6
lowTileByte <<= 1
highTileByte <<= 1
}
data <<= 4
data |= uint32(a | p1 | p2)
}
return data
}
func (ppu *PPU) evaluateSprites() {
var h int
if ppu.flagSpriteSize == 0 {
h = 8
} else {
h = 16
}
count := 0
for i := 0; i < 64; i++ {
y := ppu.oamData[i*4+0]
a := ppu.oamData[i*4+2]
x := ppu.oamData[i*4+3]
row := ppu.ScanLine - int(y)
if row < 0 || row >= h {
continue
}
if count < 8 {
ppu.spritePatterns[count] = ppu.fetchSpritePattern(i, row)
ppu.spritePositions[count] = x
ppu.spritePriorities[count] = (a >> 5) & 1
ppu.spriteIndexes[count] = byte(i)
}
count++
}
if count > 8 {
count = 8
ppu.flagSpriteOverflow = 1
}
ppu.spriteCount = count
}
// tick updates Cycle, ScanLine and Frame counters
func (ppu *PPU) tick() {
if ppu.nmiDelay > 0 {
ppu.nmiDelay--
if ppu.nmiDelay == 0 && ppu.nmiOutput && ppu.nmiOccurred {
ppu.console.CPU.triggerNMI()
}
}
if ppu.flagShowBackground != 0 || ppu.flagShowSprites != 0 {
if ppu.f == 1 && ppu.ScanLine == 261 && ppu.Cycle == 339 {
ppu.Cycle = 0
ppu.ScanLine = 0
ppu.Frame++
ppu.f ^= 1
return
}
}
ppu.Cycle++
if ppu.Cycle > 340 {
ppu.Cycle = 0
ppu.ScanLine++
if ppu.ScanLine > 261 {
ppu.ScanLine = 0
ppu.Frame++
ppu.f ^= 1
}
}
}
// Step executes a single PPU cycle
func (ppu *PPU) Step() {
ppu.tick()
renderingEnabled := ppu.flagShowBackground != 0 || ppu.flagShowSprites != 0
preLine := ppu.ScanLine == 261
visibleLine := ppu.ScanLine < 240
// postLine := ppu.ScanLine == 240
renderLine := preLine || visibleLine
preFetchCycle := ppu.Cycle >= 321 && ppu.Cycle <= 336
visibleCycle := ppu.Cycle >= 1 && ppu.Cycle <= 256
fetchCycle := preFetchCycle || visibleCycle
// background logic
if renderingEnabled {
if visibleLine && visibleCycle {
ppu.renderPixel()
}
if renderLine && fetchCycle {
ppu.tileData <<= 4
switch ppu.Cycle % 8 {
case 1:
ppu.fetchNameTableByte()
case 3:
ppu.fetchAttributeTableByte()
case 5:
ppu.fetchLowTileByte()
case 7:
ppu.fetchHighTileByte()
case 0:
ppu.storeTileData()
}
}
if preLine && ppu.Cycle >= 280 && ppu.Cycle <= 304 {
ppu.copyY()
}
if renderLine {
if fetchCycle && ppu.Cycle%8 == 0 {
ppu.incrementX()
}
if ppu.Cycle == 256 {
ppu.incrementY()
}
if ppu.Cycle == 257 {
ppu.copyX()
}
}
}
// sprite logic
if renderingEnabled {
if ppu.Cycle == 257 {
if visibleLine {
ppu.evaluateSprites()
} else {
ppu.spriteCount = 0
}
}
}
// vblank logic
if ppu.ScanLine == 241 && ppu.Cycle == 1 {
ppu.setVerticalBlank()
}
if preLine && ppu.Cycle == 1 {
ppu.clearVerticalBlank()
ppu.flagSpriteZeroHit = 0
ppu.flagSpriteOverflow = 0
}
}