-
Notifications
You must be signed in to change notification settings - Fork 1
/
video.cpp
363 lines (315 loc) · 12.4 KB
/
video.cpp
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
#include <cstdlib>
#include <assert.h>
#include "video.h"
#include "mem.h"
#include "graphics_display.h"
// the implementation of sprites is poor.
// pick a value for this that is between 0 and 256 and not equal to one of the colors
#define TRANSPARENT_SPRITE 37
#define BRIGHTEST_COLOR 255
VideoState globalVideoState;
// colors on screen (white, light gray, dark gray, black)
// the first one must be "BRIGHTEST_COLOR" - other code depends on this!
static const u8 colors[4] = {BRIGHTEST_COLOR, 180, 80, 0};
void initVideo() {
globalVideoState.mode = 0;
globalVideoState.modeClock = 0;
globalVideoState.line = 0;
globalVideoState.frameBuffer = (u8*)malloc(160*144); // <- todo change for STM32
for(u32 i = 0; i < 160*144; i++) {
globalVideoState.frameBuffer[i] = 255;
}
}
void dumpVideo() {
for(u16 i = 0x8000; i < 0x87ff; i++) {
if((i%8) == 1) {
printf("@ 0x%04x: ", i);
}
printf("0x%02x ", readByte(i));
if(!(i%8)) printf("\n");
}
printf("\n");
assert(false);
}
// read a pixel out of a tile and apply the given palette
u8 readTile(u16 tileAddr, u8 x, u8 y, u8 palette) {
assert(x <= 8);
assert(y <= 8);
x = (7 - x);
u16 loAddr = tileAddr + (y*(u16)2);
u16 hiAddr = loAddr + (u16)1;
u8 lo = readByte(loAddr);
u8 hi = readByte(hiAddr);
u8 loV = (lo >> x) & (u8)1;
u8 hiV = (hi >> x) & (u8)1;
//u8 result = loV * 120 + hiV * 60;
u8 colorIdx = loV + 2 * hiV;
u8 colorID = (palette >> (2 * colorIdx)) & 3;
return colors[colorID];
}
// read a pixel out of a tile and apply the given palette
// returns TRANSPARENT_SPRITE if the sprite should be transparent
u8 readSpriteTile(u16 tileAddr, u8 x, u8 y, u8 palette) {
//tileAddr = 0x8180;
assert(x <= 8);
assert(y <= 8);
x = (7 - x);
u16 loAddr = tileAddr + (y*(u16)2);
u16 hiAddr = loAddr + (u16)1;
u8 lo = readByte(loAddr);
u8 hi = readByte(hiAddr);
u8 loV = (lo >> x) & (u8)1;
u8 hiV = (hi >> x) & (u8)1;
u8 colorIdx = loV + 2 * hiV;
if(colorIdx == 0) {
return TRANSPARENT_SPRITE;
}
u8 colorID = (palette >> (2 * colorIdx)) & 3;
return colors[colorID];
}
// compute the address of the tile from the tile's index
// this is confusing because depending on the tileData selected,
// the tileIdx is either signed or unsigned
u16 computeTileAddr(u8 tileIdx, bool tileData) {
if(tileData) {
return 0x8000 + 16 * tileIdx;
} else {
if(tileIdx <= 127) {
return 0x9000 + 16 * tileIdx;
} else {
return 0x8000 + 16 * (tileIdx);
}
}
}
u8 readTilePtr(u8* tileAddr, u8 x, u8 y, u8 palette) {
assert(x <= 8);
assert(y <= 8);
x = (7 - x);
u8* loAddr = tileAddr + (y*(u16)2);
u8* hiAddr = loAddr + (u16)1;
u8 lo = *(loAddr);
u8 hi = *(hiAddr);
u8 loV = (lo >> x) & (u8)1;
u8 hiV = (hi >> x) & (u8)1;
//u8 result = loV * 120 + hiV * 60;
u8 colorIdx = loV + 2 * hiV;
u8 colorID = (palette >> (2 * colorIdx)) & 3;
return colors[colorID];
}
u8 readSpriteTileAddr(u8* tileAddr, u8 x, u8 y, u8 palette) {
//tileAddr = 0x8180;
assert(x <= 8);
assert(y <= 8);
x = (7 - x);
u8* loAddr = tileAddr + (y*(u16)2);
u8* hiAddr = loAddr + (u16)1;
u8 lo = *(loAddr);
u8 hi = *(hiAddr);
u8 loV = (lo >> x) & (u8)1;
u8 hiV = (hi >> x) & (u8)1;
u8 colorIdx = loV + 2 * hiV;
if(colorIdx == 0) {
return TRANSPARENT_SPRITE;
}
u8 colorID = (palette >> (2 * colorIdx)) & 3;
return colors[colorID];
}
u8* computeTileAddrPtr(u8 tileIdx, bool tileData) {
if(tileData) {
return globalMemState.vram + 16 * tileIdx;
} else {
if(tileIdx <= 127) {
return globalMemState.vram + 0x1000 + 16 * tileIdx;
} else {
return globalMemState.vram + 16 * (tileIdx);
}
}
}
static constexpr u32 H_RES = 160;
static constexpr u32 Y0 = 0;
static constexpr u32 X0 = 0;
inline u32 xy2px(u8 x, u8 y) {
return H_RES*(y+Y0) + x + X0;
}
// main function to render a line of the display.
// this implementation is missing a number of things, including (but not limited to)
// -- proper position of the WINDOW
// -- 16x8 sprites
// -- sprite sorting
// -- 10 sprite limit
void renderLine() {
u8 lcdc = globalMemState.ioRegs[IO_LCDC]; // lcd control register
bool lcdOn = (lcdc >> 7) & (u8)1; // lcd display on?
bool windowTileMap = (lcdc >> 6) & (u8)1; // select tilemap source for window
bool windowEnable = (lcdc >> 5) & (u8)1; // draw window?
bool tileData = (lcdc >> 4) & (u8)1; // select tile data source
bool bgTileMap = (lcdc >> 3) & (u8)1; // select tilemap source for background
bool objSize = (lcdc >> 2) & (u8)1; // pick sprite size (nyi)
bool objEnable = (lcdc >> 1) & (u8)1; // enable sprite renderer
bool bgWinEnable = (lcdc >> 0) & (u8)1; // enable background and window renderer?
u16 windowMapAddr = (u16)(windowTileMap ? 0x9c00 : 0x9800);
u16 bgTileMapAddr = (u16)(bgTileMap ? 0x9c00 : 0x9800);
// background renderer
if(lcdOn && bgWinEnable) {
// render background onto framebuffer
u8 pal = globalMemState.ioRegs[IO_BGP]; // color palette
u16 tileMapRowAddr = (u16)(bgTileMapAddr + 32*((((u16)globalVideoState.line +
globalMemState.ioRegs[IO_SCROLLY]) & (u16)255) >> 3)); // address of the row of the tilemap
u8 tileMapColIdx = globalMemState.ioRegs[IO_SCROLLX] >> 3; // column index of the tilemap
u8 yPixOffset = ((u8)globalVideoState.line + globalMemState.ioRegs[IO_SCROLLY]) & (u8)7; // y-pixel of tile
u8 xPixOffset = globalMemState.ioRegs[IO_SCROLLX] & (u8)7; // x-pixel of tile
//u8* linePtr = globalVideoState.frameBuffer + 160 * globalVideoState.line; // frame buffer pointer
u8 tileIdx = readByte(tileMapRowAddr + tileMapColIdx); // tile index
// loop over pixels in the line
for(u8 px = 0; px < 160; px++) {
globalVideoState.frameBuffer[xy2px(px,globalVideoState.line)] =
readTilePtr(computeTileAddrPtr(tileIdx, tileData), xPixOffset, yPixOffset, pal);
xPixOffset++; // increment tile pixel
//linePtr++; // increment frame buffer pixel
if(xPixOffset == 8) { // if we have overflowed the tile
xPixOffset = 0; // go to the beginning
tileMapColIdx = (tileMapColIdx + 1) & 31; // of the next tile (allow wraparound)
tileIdx = readByte(tileMapRowAddr + tileMapColIdx); // and look up the tile index in the tile map
}
}
}
// window renderer
if(windowEnable) {
u8 pal = globalMemState.ioRegs[IO_BGP]; // palette
u8 wx = globalMemState.ioRegs[IO_WINX]; // location of the window (nyi)
u8 wy = globalMemState.ioRegs[IO_WINY]; // location of the window (nyi)
if(wx > 166 || wy > 143) {
// if the window is out of this range, it is disabled too.
} else {
u16 tileMapRowAddr = windowMapAddr + 32*((((u16)globalVideoState.line)) >> 3); // address of the row of the tilemap
u8 tileMapColIdx = 0; // column index of the tilemap
u8 yPixOffset = ((u8)globalVideoState.line) & (u8)7; // y-pixel of tile
u8 xPixOffset = 0; // x-pixel of tile
//u8* linePtr = globalVideoState.frameBuffer + 160 * globalVideoState.line; // frame buffer pointer
u8 tileIdx = readByte(tileMapRowAddr + tileMapColIdx); // tile index
// loop over pixels in the line
for(u8 px = 0; px < 160; px++) {
globalVideoState.frameBuffer[xy2px(px,globalVideoState.line)] =
readTilePtr(computeTileAddrPtr(tileIdx, tileData), xPixOffset, yPixOffset, pal);
//*linePtr = readTile(computeTileAddr(tileIdx, tileData), xPixOffset, yPixOffset, pal); // set the frame buffer
xPixOffset++; // increment tile pixel
//linePtr++; // increment frame buffer pixel
if(xPixOffset == 8) { // if we have overflowed the tile
xPixOffset = 0; // go to the beginning
tileMapColIdx = (tileMapColIdx + 1) & 31; // of the next tile (allow wraparound, but it shouldn't happen?)
tileIdx = readByte(tileMapRowAddr + tileMapColIdx); // and look up the tile index in the tile map
}
}
}
}
// sprite renderer
if(objEnable) {
for(u16 spriteID = 0; spriteID < 40; spriteID++) {
u16 oamPtr = 0xfe00 + 4 * spriteID; // sprite information table
u8 spriteY = readByte(oamPtr); // y-coordinate of sprite
u8 spriteX = readByte(oamPtr + 1); // x-coordinate of sprite
u8 patternIdx = readByte(oamPtr + 2); // sprite pattern
u8 flags = readByte(oamPtr + 3); // flag bits
bool pri = (flags >> 7) & (u8)1; // priority (transparency stuff)
bool yFlip = (flags >> 6) & (u8)1; // flip around y?
bool xFlip = (flags >> 5) & (u8)1; // flip around x?
bool palID = (flags >> 4) & (u8)1; // palette ID (OBP0/OBP2)
u8 pal = palID ? globalMemState.ioRegs[IO_OBP1] : globalMemState.ioRegs[IO_OBP0];
if(spriteX | spriteY) {
// the sprite coordinates have an offset
u8 spriteStartY = spriteY - 16;
u8 spriteLastY = spriteStartY + 8; // todo 16 row sprites
// reject based on y if the sprite won't be visible in the current line
if(globalVideoState.line < spriteStartY || globalVideoState.line >= spriteLastY) {
continue;
}
// get y px relative to the sprite pattern
u8 tileY = globalVideoState.line - spriteStartY;
if(yFlip) {
tileY = 7 - tileY;
}
assert(tileY < 8);
// loop over the 8 pixels that the sprite is on:
for(u8 tileX = 0; tileX < 8; tileX++) {
u8 xPos = spriteX - 8 + tileX; // position on the screen
if(xPos >= 160) continue; // reject if we go off the end, don't wrap around
u32 fbIdx = xy2px(xPos, globalVideoState.line);
// current color at the screen
u8 old = globalVideoState.frameBuffer[fbIdx];
// get the pixel from the sprite pattern data
u8 tileLookupX = tileX;
if(xFlip) {
tileLookupX = 7 - tileX;
}
u8 tileValue = readSpriteTileAddr(globalMemState.vram + patternIdx * 16, tileLookupX, tileY, pal);
// don't draw transparent
if(tileValue == TRANSPARENT_SPRITE) continue; // (transparent sprites)
// not sure this is 100% right...
if(!pri) {
globalVideoState.frameBuffer[fbIdx] = tileValue;
} else {
if(old == BRIGHTEST_COLOR) {
globalVideoState.frameBuffer[fbIdx] = tileValue;
}
}
}
}
}
}
}
// step the video by a number of clock cycles
void stepVideo(u32 cycles) {
globalVideoState.modeClock += cycles;
switch(globalVideoState.mode) {
case 2: // OAM read, scanning
if(globalVideoState.modeClock >= 80) {
globalVideoState.modeClock = 0;
globalVideoState.mode = 3; // VRAM read, scanning
}
break;
case 3: // VRAM read, scanning
if(globalVideoState.modeClock >= 172) {
globalVideoState.modeClock = 0;
globalVideoState.mode = 0; // hblank
renderLine(); // draw line into framebuffer
}
break;
case 0: // hblank
if(globalVideoState.modeClock >= 204) {
globalVideoState.modeClock = 0;
globalVideoState.line++;
if(globalVideoState.line == 143) {
globalVideoState.mode = 1; // vblank
globalMemState.ioRegs[IO_IF] |= 0x1; // set interrupt for vblank
if(!keyboard.turbo) // if we are in "turbo" mode, don't update graphics
updateGraphics(); // display framebuffer on screen
} else {
globalVideoState.mode = 2; // oam
}
}
break;
case 1: // vblank
if(globalVideoState.modeClock >= 456) {
globalVideoState.modeClock = 0;
globalVideoState.line++;
if(globalVideoState.line > 153) {
globalVideoState.mode = 2;
globalVideoState.line = 0;
}
}
break;
default:
assert(false);
}
globalMemState.ioRegs[IO_LY] = (u8)globalVideoState.line; // update current line
// this is likely somewhat wrong.
u8 stat = globalMemState.ioRegs[IO_STAT]; // update STAT register (this is likely the source of issue on bubble bobble)
stat &= ~7; // clear mode, coincidence
stat += globalVideoState.mode; // set current mode
if(globalMemState.ioRegs[IO_LYC] == globalMemState.ioRegs[IO_LY]) { // check coincidence
stat += 4;
if((stat >> 6) & 1) {
globalMemState.ioRegs[IO_IF] |= 2; // stat interrupt
}
}
}