-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbgpu.go
463 lines (399 loc) · 12.5 KB
/
gbgpu.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
package gpu
import (
"log"
"github.com/jmontupet/gbcore/pkg/coreio"
"github.com/jmontupet/gbcore/internal/pkg/ioports"
"github.com/jmontupet/gbcore/internal/pkg/mmu/memorymap"
)
const (
nbFrameLines = 144
nbFrameRow = 160
oamLength = 80 // 77-83 (80)
drawLength = 172 // 169-175 (172)
hBlankLength = 204 // 201-207 (204)
vBlankLength = 4560 // 4560
// Each Line
oamEnd = oamLength
drawEnd = oamEnd + drawLength
hBlanckEnd = drawEnd + hBlankLength
// Each Fram
frameEnd = nbFrameLines * hBlanckEnd
vBlankEnd = frameEnd + vBlankLength
)
type gpuMode uint8
const frameBufferSize = nbFrameRow * nbFrameLines
const (
ModeHBlank = iota
ModeVBlank
ModeOAM
ModeVRAM
)
type GPU struct {
// Color ?
cgb bool
// Palettes Manager
palettesManager *palettesManager
// Internal VRAM
_vram gbVRAM
// OAM Memory
_oam oam
// Store & Send rendered pixels
renderer coreio.FrameDrawer
frameBuffer *coreio.FrameBuffer
frameColors *coreio.FrameColors
// frameBufferSprite coreio.FrameBuffer
// frameBufferBg coreio.FrameBuffer
// Cycles counters
frameCycles int
lineCycles int
// STAT
statInterruptOnCoincidence *ioports.BitPtr // Bit 6 - LYC=LY Coincidence Interrupt (1=Enable) (Read/Write)
statInterruptOnOAM *ioports.BitPtr // Bit 5 - Mode 2 OAM Interrupt (1=Enable) (Read/Write)
statInterruptOnVBlank *ioports.BitPtr // Bit 4 - Mode 1 V-Blank Interrupt (1=Enable) (Read/Write)
statInterruptOnHBlank *ioports.BitPtr // Bit 3 - Mode 0 H-Blank Interrupt (1=Enable) (Read/Write)
coincidenceFlag *ioports.BitPtr // Bit 2 - Coincidence Flag (0:LYC<>LY, 1:LYC=LY) (Read Only)
mode *ioports.MaskedPtr // Bit 1-0 - Mode Flag (Mode 0-3, see below)
// 0: During H-Blank
// 1: During V-Blank
// 2: During Searching OAM-RAM
// 3: During Transfering Data to LCD Driver
// Display registers
ly *ioports.Ptr // LY - LCDC Y-Coordinate
lyc *ioports.Ptr // LYC - LY Compare
// Background position
scy *ioports.Ptr
scx *ioports.Ptr
// Colors palettes
bgPalette *ioports.Ptr // BGP - BG Palette Data
spritePalette0 *ioports.Ptr // OBP0 - Object Palette 0 Data
spritePalette1 *ioports.Ptr // OBP1 - Object Palette 1 Data
// Window position
wy *ioports.Ptr // WY - Window Y Position
wx *ioports.Ptr // WX - Window X Position minus 7
// LCDC Values
displayEnable *ioports.BitPtr // Bit 7 - LCD Display Enable (0=Off, 1=On)
winTileMap *ioports.BitPtr // Bit 6 - Window Tile Map Display Select (0=9800-9BFF, 1=9C00-9FFF)
winEnable *ioports.BitPtr // Bit 5 - Window Display Enable (0=Off, 1=On)
tileData *ioports.BitPtr // Bit 4 - BG & Window Tile Data Select (0=8800-97FF, 1=8000-8FFF)
bgTileMap *ioports.BitPtr // Bit 3 - BG Tile Map Display Select (0=9800-9BFF, 1=9C00-9FFF)
spriteSize *ioports.BitPtr // Bit 2 - OBJ (Sprite) Size (0=8x8, 1=8x16)
spriteEnable *ioports.BitPtr // Bit 1 - OBJ (Sprite) Display Enable (0=Off, 1=On)
bgEnable *ioports.BitPtr // Bit 0 - BG Display (0=Off, 1=On)
// Interrupts
vBlankInterrupt *ioports.BitPtr
statInterrupt *ioports.BitPtr
}
func (gpu *GPU) getMode() gpuMode { return gpuMode(gpu.mode.Get()) }
func (gpu *GPU) setMode(mode gpuMode) { gpu.mode.Set(uint8(mode)) }
func (gpu *GPU) setMonoColorPalette(offset int, palette uint8) {
for i, c := range [...]uint8{
0: palette & 0x03,
1: palette >> 2 & 0x03,
2: palette >> 4 & 0x03,
3: palette >> 6 & 0x03,
} {
var intencity uint8 = 0
switch c {
case 0:
intencity = 0xED
case 1:
intencity = 0x99
case 2:
intencity = 0x66
case 3:
intencity = 0x21
default:
log.Fatal("COLOR NOT DEFINED")
}
gpu.frameColors[(offset+i)*3] = intencity
gpu.frameColors[(offset+i)*3+1] = intencity
gpu.frameColors[(offset+i)*3+2] = intencity
}
}
func (gpu *GPU) FlushFrameBuffer() {
// Prepare colors palette
if gpu.cgb {
gpu.palettesManager.setPalettes(gpu.frameColors)
} else {
gpu.setMonoColorPalette(0b100000, gpu.bgPalette.Get())
gpu.setMonoColorPalette(0b000000, gpu.spritePalette0.Get())
gpu.setMonoColorPalette(0b000100, gpu.spritePalette1.Get())
}
// for i := 0; i < frameBufferSize; i++ {
// if gpu.frameBufferSprite[i] != 255 {
// gpu.frameBuffer[i] = gpu.frameBufferSprite[i]
// } else {
// gpu.frameBuffer[i] = gpu.frameBufferBg[i]
// }
// gpu.frameBufferSprite[i] = 255
// gpu.frameBufferBg[i] = 0
// }
gpu.frameBuffer, gpu.frameColors =
gpu.renderer.SwapFrameBuffer(gpu.frameBuffer, gpu.frameColors)
}
func (gpu *GPU) Tick(cycles uint8) uint8 {
gpu.frameCycles += int(cycles)
gpu.lineCycles += int(cycles)
mode := gpu.getMode()
if gpu.frameCycles > frameEnd { // IN V-BLANK
if mode != ModeVBlank {
// Last Line
gpu.drawLine(gpu.ly.Get())
// VBlank interrupt
gpu.vBlankInterrupt.Set(true)
if gpu.statInterruptOnVBlank.Get() {
gpu.statInterrupt.Set(true)
}
gpu.FlushFrameBuffer()
gpu.setMode(ModeVBlank)
}
if gpu.frameCycles >= vBlankEnd { // END : Reset cycles
// gpu.frameCycles - vBlankEnd should not exceed 80 (OAM END)
gpu.lineCycles = gpu.frameCycles - vBlankEnd
gpu.frameCycles = gpu.frameCycles - vBlankEnd
gpu.ly.Set(0)
gpu.setMode(ModeOAM)
}
} else { // IN FRAME
switch {
case gpu.lineCycles < oamEnd:
if mode != ModeOAM {
gpu.setMode(ModeOAM)
if gpu.statInterruptOnOAM.Get() {
gpu.statInterrupt.Set(true)
}
}
break
case gpu.lineCycles < drawEnd:
if mode != ModeVRAM {
gpu.setMode(ModeVRAM)
gpu.drawLine(gpu.ly.Get())
}
break
case gpu.lineCycles < hBlanckEnd:
if mode != ModeHBlank {
gpu.setMode(ModeHBlank)
if gpu.statInterruptOnHBlank.Get() {
gpu.statInterrupt.Set(true)
}
}
break
}
}
// ALLWAYS UPDATE LY IN FRAME AND V-BLANK
if gpu.lineCycles >= hBlanckEnd {
gpu.ly.Set(gpu.ly.Get() + 1)
gpu.lineCycles = gpu.lineCycles - hBlanckEnd
}
// Compare ly - lyc
if gpu.ly.Get() == gpu.lyc.Get() {
gpu.coincidenceFlag.Set(true)
if gpu.statInterruptOnCoincidence.Get() {
gpu.statInterrupt.Set(true)
}
} else {
gpu.coincidenceFlag.Set(false)
}
return gpu.ly.Get()
}
func (gpu *GPU) drawLine(screenLine uint8) {
if screenLine >= nbFrameLines { // if memory is corrupted...
return
}
if gpu.bgEnable.Get() {
gpu.drawBgLine(screenLine)
}
if gpu.winEnable.Get() {
gpu.drawWindowLine(screenLine)
}
if gpu.spriteEnable.Get() {
gpu.drawSpriteLine(screenLine)
}
}
func (gpu *GPU) drawWindowLine(screenLine uint8) {
bgScrollY := screenLine + gpu.wy.Get()
bgScrollX := gpu.wx.Get() - 7
if screenLine < gpu.wy.Get() {
return
}
firstTileOffset := bgScrollX & 7
var winMap uint8 = 0
if gpu.winTileMap.Get() {
winMap = 1
}
tileDataLine := bgScrollY & 7
drawPointer := uint32(screenLine) * nbFrameRow
tileDataTable := gpu.tileData.Get()
nbPixelsDraw := 0
var nbPixelsToDraw uint8 = 8
drawloop:
for i := uint8(0); ; i++ { // Open range : break with label "drawloop"
tileMapInfo := gpu._vram.GetTileInfo(winMap, (bgScrollX>>3+i)&0x1F, bgScrollY>>3)
palettePrefix := 0b100000 | (tileMapInfo.palette)<<2
gpu._vram.GetTileData(tileDataTable, tileMapInfo.tileID, tileMapInfo.bank).
appendPixelsLine(gpu.frameBuffer[drawPointer:drawPointer], tileDataLine, firstTileOffset, nbPixelsToDraw, palettePrefix, tileMapInfo.hFlip == 1, tileMapInfo.vFlip == 1)
drawPointer += (8 - uint32(firstTileOffset))
nbPixelsDraw += (8 - int(firstTileOffset))
if diff := nbFrameRow - nbPixelsDraw; diff <= 0 { // END of the line : 160 pixels draw
break drawloop
} else if diff < 8 {
nbPixelsToDraw = uint8(diff)
}
firstTileOffset = 0
}
}
func (gpu *GPU) drawSpriteLine(screenLine uint8) {
// println("DRAW sprites")
var maxHeight uint8 = 8
mode8x16 := gpu.spriteSize.Get()
if mode8x16 {
maxHeight = 16
}
for i := 0; i < 40; i++ { // 40 sprites * 4 bytes
sprite := gpu._oam.GetSprite(uint8(i))
if sprite.Y == 0 || sprite.Y >= 160 { // Offscreen
continue
}
topLeftX := int(sprite.X) - 8
topLeftY := int(sprite.Y) - 16
if topLeftY > int(screenLine) || topLeftY+int(maxHeight) <= int(screenLine) { // Not on the line
continue
}
offscreenX := sprite.X == 0 || sprite.X >= 168 // Offscreen but affects the priority
if !offscreenX {
spriteLine := int(screenLine) - topLeftY
for j, p := range sprite.GetPixels(&gpu._vram, uint8(spriteLine), 0, mode8x16) {
pixelX := topLeftX + j
if pixelX >= 160 {
break
}
if pixelX < 0 {
continue
}
// Sprite data 00 is transparent
if p == 0 {
continue
}
var colorPrefix uint8 = 0b000000
if gpu.cgb {
colorPrefix |= sprite.ColorPalette << 2
} else {
if sprite.PaletteNumber == 1 {
colorPrefix = 0b000100
}
}
// BG color 0 is always behind OBJ
if gpu.frameBuffer[int(screenLine)*160+pixelX]&0x03 == 0 {
gpu.frameBuffer[int(screenLine)*160+pixelX] = colorPrefix + p
}
// OBJ-to-BG Priority (0=OBJ Above BG, 1=OBJ Behind BG color 1-3)
if !sprite.ObjToBgPriority {
gpu.frameBuffer[int(screenLine)*160+pixelX] = colorPrefix + p
}
}
}
}
}
func (gpu *GPU) drawBgLine(screenLine uint8) {
bgScrollY := screenLine + gpu.scy.Get()
bgScrollX := gpu.scx.Get()
firstTileOffset := bgScrollX & 7
var bgMap uint8 = 0
if gpu.bgTileMap.Get() {
bgMap = 1
}
tileDataLine := bgScrollY & 7 // modulus 8
drawPointer := uint32(screenLine) * nbFrameRow
tileDataTable := gpu.tileData.Get()
nbPixelsDraw := 0
nbPixelsToDraw := uint8(8)
// var buff [8]uint8
drawloop:
for i := uint8(0); ; i++ { // Open range : break with label "drawloop"
tileMapInfo := gpu._vram.GetTileInfo(bgMap, (bgScrollX>>3+i)&0x1F, bgScrollY>>3)
palettePrefix := 0b100000 | (tileMapInfo.palette)<<2
gpu._vram.GetTileData(tileDataTable, tileMapInfo.tileID, tileMapInfo.bank).
appendPixelsLine(gpu.frameBuffer[drawPointer:drawPointer], tileDataLine, firstTileOffset, nbPixelsToDraw, palettePrefix, tileMapInfo.hFlip == 1, tileMapInfo.vFlip == 1)
drawPointer += (8 - uint32(firstTileOffset))
nbPixelsDraw += (8 - int(firstTileOffset))
if diff := nbFrameRow - nbPixelsDraw; diff <= 0 { // END of the line : 160 pixels draw
break drawloop
} else if diff < 8 {
nbPixelsToDraw = uint8(diff)
}
firstTileOffset = 0
}
}
// Proxy for _vram access & OAM
func (gpu *GPU) Read(addr uint16) uint8 {
switch {
case
addr == 0xFF68,
addr == 0xFF69,
addr == 0xFF6A,
addr == 0xFF6B:
return gpu.palettesManager.Read(addr)
case addr == 0xFF46, addr >= memorymap.OAMStart && addr <= memorymap.OAMEnd:
return gpu._oam.Read(addr)
case addr >= memorymap.VRamStart && addr <= memorymap.VRamEnd:
return gpu._vram.Read(addr)
default:
log.Fatalf("GPU MEMORY UNREACHABLE : 0x%04X", addr)
return 0x00
}
}
func (gpu *GPU) Write(addr uint16, value uint8) {
switch {
case
addr == 0xFF68,
addr == 0xFF69,
addr == 0xFF6A,
addr == 0xFF6B:
gpu.palettesManager.Write(addr, value)
case addr == 0xFF46:
gpu._oam.Write(addr, value)
case addr >= memorymap.OAMStart && addr <= memorymap.OAMEnd:
gpu._oam.internalWrite(addr, value)
case addr >= memorymap.VRamStart && addr <= memorymap.VRamEnd:
gpu._vram.Write(addr, value)
default:
log.Fatalf("GPU MEMORY UNREACHABLE : 0x%04X", addr)
}
}
func NewGBGPU(io *ioports.IOPorts, renderer coreio.FrameDrawer, cgb bool) *GPU {
gpu := &GPU{
cgb: cgb,
_vram: newGBVRAM(io),
_oam: newOAM(),
palettesManager: newPalettesManager(cgb),
renderer: renderer,
frameBuffer: new(coreio.FrameBuffer),
frameColors: new(coreio.FrameColors),
statInterruptOnCoincidence: io.NewBit6Ptr(0xFF41),
statInterruptOnOAM: io.NewBit5Ptr(0xFF41),
statInterruptOnVBlank: io.NewBit4Ptr(0xFF41),
statInterruptOnHBlank: io.NewBit3Ptr(0xFF41),
coincidenceFlag: io.NewBit2Ptr(0xFF41),
mode: io.NewMaskedPtr(0xFF41, 0x03),
ly: io.NewPtr(0xFF44),
lyc: io.NewPtr(0xFF45),
wy: io.NewPtr(0xFF4A),
wx: io.NewPtr(0xFF4B),
scy: io.NewPtr(0xFF42),
scx: io.NewPtr(0xFF43),
statInterrupt: io.NewBit1Ptr(0xFF0F),
vBlankInterrupt: io.NewBit0Ptr(0xFF0F),
displayEnable: io.NewBit7Ptr(0xFF40),
winTileMap: io.NewBit6Ptr(0xFF40),
winEnable: io.NewBit5Ptr(0xFF40),
tileData: io.NewBit4Ptr(0xFF40),
bgTileMap: io.NewBit3Ptr(0xFF40),
spriteSize: io.NewBit2Ptr(0xFF40),
spriteEnable: io.NewBit1Ptr(0xFF40),
bgEnable: io.NewBit0Ptr(0xFF40),
bgPalette: io.NewPtr(0xFF47), // BGP - BG Palette Data
spritePalette0: io.NewPtr(0xFF48), // OBP0 - Object Palette 0 Data
spritePalette1: io.NewPtr(0xFF49), // OBP1 - Object Palette 1 Data
}
return gpu
}