-
Notifications
You must be signed in to change notification settings - Fork 0
/
vdp.go
655 lines (555 loc) · 13.7 KB
/
vdp.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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
package segmago
type tvType int
const (
tvNTSC tvType = 0
tvPAL = 1
)
type vdp struct {
framebuffer [256 * 240 * 4]byte
OnSecondControlByte bool
AddrReg uint16
CodeReg byte
TVType tvType
ModeHeight uint16
VRAM [16 * 1024]byte
TMS9918NameTableAddr uint16
SMSNameTableAddr uint16
SMSNameTableMaskBit uint16
TMS9918SpriteAttrTableAddr uint16
SMSSpriteAttrTableAddr uint16
SMSSpriteAttrTableMaskBit uint16
TMS9918SpriteTileTableAddr uint16
SMSSpriteTileTableAddr uint16
SMSSpriteTileTableMaskBit uint16
TMS9918ColortableAddr uint16
TMS9918TileAddr uint16
ScrollX uint16
ScrollY uint16
SMSBackdropCplane byte
SpriteList [9]sprite // extra is used for overflow check
NumSprites byte
ColorRAM [64]byte
GGColorLatch byte
BufferReg byte
FrameInterruptPending bool
SpriteOverflow bool
SpriteCollision bool
DisableVertScrollForRightSide bool
DisableHorizScrollForTop bool
MaskColumn0WithOverscanCol bool
LineInterruptEnable bool
ShiftSpritesLeft bool
RegM4 bool
RegM2 bool
TurnOffSync bool
DisplayEnable bool
FrameInterruptEnable bool
RegM1 bool
RegM3 bool
LargeSprites bool
StretchedSprites bool
LineInterruptPending bool
LineInterruptCounter byte
LineInterruptCounterSetReg byte
VCounter byte
VCounterFixupsThisFrame byte
HCounter byte
ScreenX uint16
ScreenY uint16
FlipRequested bool
IsGameGear bool
CPUClock byte
}
func (v *vdp) writeDataPort(val byte, isGameGear bool) {
switch v.CodeReg {
case 0, 1, 2:
v.VRAM[v.AddrReg] = val
case 3:
if isGameGear {
v.IsGameGear = true
if v.AddrReg&1 == 0 {
v.GGColorLatch = val
} else {
v.ColorRAM[(v.AddrReg-1)&63] = v.GGColorLatch
v.ColorRAM[v.AddrReg&63] = val
}
} else {
v.ColorRAM[v.AddrReg&31] = val
}
}
v.AddrReg = (v.AddrReg + 1) & 0x3fff
v.BufferReg = val
v.OnSecondControlByte = false
}
func (v *vdp) readDataPort() byte {
val := v.BufferReg
v.BufferReg = v.VRAM[v.AddrReg]
v.AddrReg = (v.AddrReg + 1) & 0x3fff
v.OnSecondControlByte = false
return val
}
func (v *vdp) incVCounter() {
switch {
case v.ModeHeight == 192 && v.TVType == tvNTSC:
if v.VCounterFixupsThisFrame == 0 && v.VCounter == 0xda {
v.VCounterFixupsThisFrame++
v.VCounter = 0xd5
} else {
v.VCounter++
}
case v.ModeHeight == 224 && v.TVType == tvNTSC:
if v.VCounterFixupsThisFrame == 0 && v.VCounter == 0xea {
v.VCounterFixupsThisFrame++
v.VCounter = 0xe5
} else {
v.VCounter++
}
case v.ModeHeight == 192 && v.TVType == tvPAL:
if v.VCounterFixupsThisFrame == 0 && v.VCounter == 0xf2 {
v.VCounterFixupsThisFrame++
v.VCounter = 0xba
} else {
v.VCounter++
}
case v.ModeHeight == 224 && v.TVType == tvPAL:
if v.VCounterFixupsThisFrame == 0 && v.VCounter == 0xff {
v.VCounterFixupsThisFrame++
v.VCounter = 0
} else if v.VCounterFixupsThisFrame == 1 && v.VCounter == 0x02 {
v.VCounterFixupsThisFrame++
v.VCounter = 0xca
} else {
v.VCounter++
}
case v.ModeHeight == 240 && v.TVType == tvPAL:
if v.VCounterFixupsThisFrame == 0 && v.VCounter == 0xff {
v.VCounterFixupsThisFrame++
v.VCounter = 0
} else if v.VCounterFixupsThisFrame == 1 && v.VCounter == 0x0a {
v.VCounterFixupsThisFrame++
v.VCounter = 0xd2
} else {
v.VCounter++
}
default:
errOut("incVCounter(): ModeHeight not implemented", v.ModeHeight)
}
}
func (v *vdp) onePastLastVCounter() bool {
// TODO: fixups for PAL
switch {
case v.ModeHeight == 192 && v.TVType == tvNTSC:
return v.VCounterFixupsThisFrame == 1 && v.VCounter == 0x00
case v.ModeHeight == 224 && v.TVType == tvNTSC:
return v.VCounterFixupsThisFrame == 1 && v.VCounter == 0x00
case v.ModeHeight == 192 && v.TVType == tvPAL:
return v.VCounterFixupsThisFrame == 1 && v.VCounter == 0x00
case v.ModeHeight == 224 && v.TVType == tvPAL:
return v.VCounterFixupsThisFrame == 2 && v.VCounter == 0x00
case v.ModeHeight == 240 && v.TVType == tvPAL:
return v.VCounterFixupsThisFrame == 2 && v.VCounter == 0x00
default:
errOut("atLastVCounter(): ModeHeight not implemented", v.ModeHeight)
return false
}
}
func (v *vdp) readVCounter() byte {
return v.VCounter
}
func (v *vdp) readHCounter() byte {
return v.HCounter
}
func (v *vdp) updateHCounter() {
v.HCounter = byte(v.ScreenX >> 1)
}
func (v *vdp) drawColor(x, y uint16, r, g, b byte) {
base := int(y)*256*4 + int(x)*4
v.framebuffer[base+0] = r
v.framebuffer[base+1] = g
v.framebuffer[base+2] = b
v.framebuffer[base+3] = 0xff
}
type nameTableEntry struct {
patternNum uint16
hFlip bool
vFlip bool
wantsPriority bool
paletteSel byte
}
func (v *vdp) getNameTableEntry(tileX, tileY uint16) nameTableEntry {
baseAddr := v.SMSNameTableAddr
if v.ModeHeight != 192 {
baseAddr &^= 0x0800
baseAddr |= 0x0700
}
addr := baseAddr + ((tileY << 6) | tileX<<1)
rawEntry := uint16(v.VRAM[addr]) | uint16(v.VRAM[addr+1])<<8
return nameTableEntry{
patternNum: rawEntry & 0x1ff,
hFlip: rawEntry>>9&1 > 0,
vFlip: rawEntry>>10&1 > 0,
wantsPriority: rawEntry&0x1000 > 0,
paletteSel: byte((rawEntry >> 11) & 1),
}
}
func (v *vdp) getPatternCplanes(patternNum, x, y uint16) byte {
pattern := v.VRAM[patternNum*32:]
line := pattern[4*y:]
sel := 7 - x
bit0 := line[0] >> sel & 1
bit1 := line[1] >> sel & 1
bit2 := line[2] >> sel & 1
bit3 := line[3] >> sel & 1
palIdx := bit0 | bit1<<1 | bit2<<2 | bit3<<3
return palIdx
}
func (v *vdp) getRGB(vdpCol byte) (byte, byte, byte) {
r := (vdpCol & 3) << 6
g := (vdpCol >> 2 & 3) << 6
b := (vdpCol >> 4 & 3) << 6
return r, g, b
}
func (v *vdp) ggGetRGB(ggCol uint16) (byte, byte, byte) {
r := byte(ggCol&0x0f) << 4
g := byte(ggCol & 0xf0)
b := byte((ggCol >> 4) & 0xf0)
return r, g, b
}
type sprite struct {
X, Y uint16
PatternNum uint16
}
func (v *vdp) getSpriteHeight() uint16 {
spriteHeight := uint16(8)
if v.LargeSprites {
spriteHeight *= 2
}
if v.StretchedSprites {
spriteHeight *= 2
}
return spriteHeight
}
func (v *vdp) parseSpritesForLine(y uint16) {
base := v.SMSSpriteAttrTableAddr
slist := v.SpriteList[:0]
spriteHeight := v.getSpriteHeight()
v.NumSprites = 0
for i := uint16(0); i < 64; i++ {
spriteY := uint16(v.VRAM[base+i]) + 1
if spriteY == 0xd1 && v.ModeHeight == 192 {
break
}
if y >= spriteY && y < spriteY+spriteHeight {
spriteX := uint16(v.VRAM[base+0x80+i*2])
patternNum := uint16(v.VRAM[base+0x80+i*2+1])
slist = append(slist, sprite{
X: spriteX,
Y: spriteY,
PatternNum: patternNum,
})
v.NumSprites++
}
if v.NumSprites == 9 {
v.SpriteOverflow = true
v.NumSprites = 8
break
}
}
}
func (v *vdp) getSpriteCplanes(sp sprite, x, y uint16) byte {
tileBase := v.SMSSpriteTileTableAddr
patternNum := sp.PatternNum
if tileBase > 0 {
patternNum += 0x100
}
if v.StretchedSprites {
x /= 2
y /= 2
}
if v.LargeSprites {
if y >= 8 {
patternNum |= 1
y -= 8
} else {
patternNum &^= 1
}
}
palIdx := v.getPatternCplanes(patternNum, x, y)
return palIdx
}
func (v *vdp) renderScanline(y uint16) {
scrollY := v.ScrollY
scrollX := v.ScrollX
if v.DisableHorizScrollForTop && y < 16 {
scrollX = 0
}
tileY := y / 8
scrolledTileY := (y + scrollY) / 8
if v.ModeHeight == 192 {
for scrolledTileY >= 28 {
scrolledTileY -= 28
}
} else {
scrolledTileY &= 31
}
bgPriority := [256]bool{}
bgCplanes := [256]byte{}
bgPalettes := [256]byte{}
for i := uint16(0); i < 32; i++ {
tileX := (i - scrollX/8) & 31
var effectiveTileY uint16
if v.DisableVertScrollForRightSide && i >= 24 {
effectiveTileY = tileY
} else {
effectiveTileY = scrolledTileY
}
entry := v.getNameTableEntry(tileX, effectiveTileY)
bgY := (y + scrollY) & 7
if entry.vFlip {
bgY = 7 - bgY
}
bgX := 0
bgXDir := 1
if entry.hFlip {
bgX = 7
bgXDir = -1
}
pX := i*8 + scrollX&7
for j := uint16(0); j < 8; j++ {
if pX >= 256 {
bgCplanes[pX-256] = v.SMSBackdropCplane
bgPalettes[pX-256] = 1
} else {
bgCplanes[pX] = v.getPatternCplanes(entry.patternNum, uint16(bgX), bgY)
bgPriority[pX] = bgCplanes[pX] != 0 && entry.wantsPriority
bgPalettes[pX] = entry.paletteSel
}
bgX += bgXDir
pX++
}
}
numSprites := v.NumSprites
spriteList := [9]sprite{}
copy(spriteList[:], v.SpriteList[:])
if y < v.ModeHeight-1 {
v.parseSpritesForLine(y + 1)
} else {
v.NumSprites = 0
}
spriteHeight := v.getSpriteHeight()
cPlanes := [256]byte{}
palettes := [256]byte{}
if v.DisplayEnable {
for x := uint16(0); x < 256; x++ {
spriteCplanes := byte(0)
for i := byte(0); i < numSprites; i++ {
spriteX := spriteList[i].X
if v.ShiftSpritesLeft {
spriteX -= 8
}
if x >= spriteX && x < spriteX+spriteHeight {
sprite := spriteList[i]
colX, colY := x-sprite.X, y-sprite.Y
cPlanes := v.getSpriteCplanes(sprite, colX, colY)
if spriteCplanes == 0 {
spriteCplanes = cPlanes
} else if cPlanes != 0 {
v.SpriteCollision = true
break
}
}
}
if spriteCplanes != 0 && !bgPriority[x] {
cPlanes[x] = spriteCplanes
palettes[x] = 1
} else {
cPlanes[x] = bgCplanes[x]
palettes[x] = bgPalettes[x]
}
}
if v.MaskColumn0WithOverscanCol {
for j := uint16(0); j < 8; j++ {
cPlanes[j] = v.SMSBackdropCplane
palettes[j] = 1
}
}
if v.IsGameGear {
if y >= 3*8 && y < 3*8+18*8 {
for x := uint16(6 * 8); x < 256-6*8; x++ {
pal := v.ColorRAM[32*palettes[x]:]
color := uint16(pal[cPlanes[x]*2])
color |= uint16(pal[cPlanes[x]*2+1]) << 8
r, g, b := v.ggGetRGB(color)
v.drawColor(x, y, r, g, b)
}
}
} else {
for x := uint16(0); x < 256; x++ {
pal := v.ColorRAM[16*palettes[x]:]
color := pal[cPlanes[x]]
r, g, b := v.getRGB(color)
v.drawColor(x, y, r, g, b)
}
}
} else {
for x := uint16(0); x < 256; x++ {
v.drawColor(x, y, 0, 0, 0)
}
}
}
func (v *vdp) init(tvType tvType) {
v.TVType = tvType
v.ModeHeight = 192
v.RegM2 = true
v.RegM4 = true
v.LineInterruptEnable = true
v.FrameInterruptEnable = true
}
func (v *vdp) runCycle() {
v.CPUClock ^= 1
if v.CPUClock == 1 {
v.ScreenX++
} else {
v.ScreenX += 2
if v.ScreenX == 342 {
if v.ScreenY < v.ModeHeight {
v.renderScanline(v.ScreenY)
}
v.ScreenX = 0
v.ScreenY++
v.incVCounter()
if v.ScreenY <= v.ModeHeight {
v.LineInterruptCounter--
if v.LineInterruptCounter == 0xff {
v.LineInterruptCounter = v.LineInterruptCounterSetReg
v.LineInterruptPending = true
}
} else {
v.LineInterruptCounter = v.LineInterruptCounterSetReg
}
if v.ScreenY == v.ModeHeight+1 {
v.FrameInterruptPending = true
}
if v.onePastLastVCounter() {
v.ScreenY = 0
v.VCounter = 0
v.VCounterFixupsThisFrame = 0
v.FlipRequested = true
}
}
}
}
func (v *vdp) updateMode() {
assert(v.RegM4, "Non mode4-based modes are not implemented")
m3, m2, m1 := v.RegM3, v.RegM2, v.RegM1
if !m3 && !m2 && !m1 {
v.ModeHeight = 192 // normal Mode 4
} else if !m3 && m2 && m1 {
v.ModeHeight = 224
} else if !m3 && m2 && !m1 {
v.ModeHeight = 192 // normal Mode 4
} else if m3 && !m2 && !m1 {
v.ModeHeight = 192 // normal Mode 4
} else if m3 && m2 && !m1 {
v.ModeHeight = 240 // TODO: set PAL?
} else if m3 && m2 && m1 {
v.ModeHeight = 192 // normal Mode 4
} else {
errOut("Unimplemented mode4 variant!", v.RegM3, v.RegM2, v.RegM1)
}
}
func (v *vdp) setReg(regNum byte, val byte) {
if regNum >= 11 {
//fmt.Println("big reg num", regNum)
return
}
switch regNum {
case 0:
boolsFromByte(val,
&v.DisableVertScrollForRightSide,
&v.DisableHorizScrollForTop,
&v.MaskColumn0WithOverscanCol,
&v.LineInterruptEnable,
&v.ShiftSpritesLeft,
&v.RegM4,
&v.RegM2,
&v.TurnOffSync)
v.updateMode()
case 1:
boolsFromByte(val,
nil,
&v.DisplayEnable,
&v.FrameInterruptEnable,
&v.RegM1,
&v.RegM3,
nil,
&v.LargeSprites,
&v.StretchedSprites)
v.updateMode()
case 2:
v.TMS9918NameTableAddr = uint16(val & 0x0f)
v.SMSNameTableAddr = uint16((val>>1)&0x07) << 11
v.SMSNameTableMaskBit = uint16(val & 1) // TODO use?
case 3:
// FIXME: noting here in case I want to implement sg-1000 mode
// that i'm unsure on the masking/shifting in this and the other
// tms99918-only tables.
v.TMS9918ColortableAddr = uint16(val)
case 4:
// TODO: bottom 3 bits should be 1 in sms mode. enforce this?
v.TMS9918TileAddr = uint16(val & 0x3f)
case 5:
v.TMS9918SpriteAttrTableAddr = uint16(val & 0x7f)
v.SMSSpriteAttrTableAddr = uint16((val>>1)&0x3f) << 8
v.SMSSpriteAttrTableMaskBit = uint16(val & 1)
case 6:
v.TMS9918SpriteTileTableAddr = uint16(val & 0x07)
v.SMSSpriteTileTableAddr = uint16((val>>2)&1) << 13
v.SMSSpriteTileTableMaskBit = uint16(val & 1)
case 7:
v.SMSBackdropCplane = val & 0x0f
case 8:
v.ScrollX = uint16(val)
case 9:
v.ScrollY = uint16(val)
case 10:
v.LineInterruptCounterSetReg = val
}
}
func (v *vdp) writeControlPort(val byte) {
if !v.OnSecondControlByte {
v.AddrReg &^= 0x00ff
v.AddrReg |= uint16(val)
v.OnSecondControlByte = true
} else {
v.AddrReg &^= 0xff00
v.AddrReg |= uint16(val&0x3f) << 8
v.CodeReg = val >> 6
v.OnSecondControlByte = false
switch v.CodeReg {
case 0:
v.BufferReg = v.VRAM[v.AddrReg]
v.AddrReg++
case 2:
v.setReg(byte(v.AddrReg>>8&0x0f), byte(v.AddrReg))
}
}
}
func (v *vdp) readControlPort() byte {
val := byteFromBools(
v.FrameInterruptPending,
v.SpriteOverflow,
v.SpriteCollision,
false,
false,
false,
false,
false,
)
v.OnSecondControlByte = false
v.LineInterruptPending = false
v.FrameInterruptPending = false
v.SpriteOverflow = false
v.SpriteCollision = false
return val
}