-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu.cpp
More file actions
322 lines (254 loc) · 9.47 KB
/
Copy pathgpu.cpp
File metadata and controls
322 lines (254 loc) · 9.47 KB
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
#include "gpu.h"
#include <SDL2/SDL.h>
#include <iostream>
#include "cpu.h"
#include "joypad.h"
namespace GPU
{
uint32_t rendercycles = 0;
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
SDL_Texture* videoTexture = nullptr;
uint32_t* pixels = nullptr;
uint32_t scale = 2;
uint32_t palette[4] = { 0xFFEFCE, 0xDE944A, 0xAD2921, 0x311852 };
/* Line specific data for sprite drawing and priorities */
uint8_t bgpixels[160];
uint8_t spritey[144][160];
void init()
{
window = SDL_CreateWindow("GEM Gameboy Emulator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
160 * scale, 144 * scale, 0);
renderer = SDL_CreateRenderer(window, -1, 0);
// Scale info
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
SDL_RenderSetLogicalSize(renderer, 160, 144);
videoTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
160, 144);
pixels = new uint32_t[160 * 144];
}
inline uint32_t getPaletteIndex(uint16_t loc, uint8_t col)
{
return (CPU::RAM[loc] >> (col << 1)) & 0x03;
}
void drawSprites(uint8_t line)
{
if (line >= 144) return;
int objSize = (bool)(CPU::RAM[IO_LCDC] & 0x4);
objSize++;
for (int s = 0; s < 40; s++)
{
uint16_t spr_idx = s * 4 + OAM;
uint8_t y = CPU::RAM[spr_idx];
if (y >= 144 + 16) continue;
uint8_t x = CPU::RAM[spr_idx + 1] - 8;
uint8_t tile = CPU::RAM[spr_idx + 2];
if (objSize == 2) tile &= ~1;
uint8_t flags = CPU::RAM[spr_idx + 3];
bool priority = flags & 0x80;
bool flipv = flags & 0x40;
bool fliph = flags & 0x20;
bool pal = flags & 0x10;
if (flipv && objSize == 2) tile++;
if (objSize == 1) {
if (line < y - 16 || line >= y - 8) continue;
}
else if (objSize == 2) {
if (line < y - 16 || line >= y) continue;
if (line >= y - 8) {
if (flipv) tile--;
else tile++;
}
}
uint16_t datapos = (0x8000 + tile * 16);
uint32_t yline = y - line - 1;
uint32_t offs = (yline % 8);
if (!flipv) offs = 7 - offs;
offs <<= 1;
datapos += offs;
for (int i = 0; i < 8; i++)
{
uint32_t xp = i;
if (!fliph) xp = 7 - i;
uint8_t col = (((CPU::RAM[datapos + 1] >> (xp)) & 1) << 1) |
((CPU::RAM[datapos + 0] >> (xp)) & 1);
if (col != 0) {
uint8_t xx = x + i;
if (xx < 0 || xx >= 160 || (priority && bgpixels[xx] != 0)
|| spritey[line][xx] >= y) continue;
pixels[xx + line * 160] = palette[getPaletteIndex(IO_OBP0 + pal, col)];
spritey[line][xx] = y;
}
}
}
}
void renderLine(uint8_t line)
{
if (line >= 144) return;
uint16_t bgTilemap = (CPU::RAM[IO_LCDC] & 8) ? 0x9C00 : 0x9800;
uint16_t windowTilemap = (CPU::RAM[IO_LCDC] & 0x40) ? 0x9C00 : 0x9800;
bool showWindow = (CPU::RAM[IO_LCDC] & 0x20);
bool dataSigned = !(CPU::RAM[IO_LCDC] & 0x10);
uint8_t sx = CPU::RAM[IO_SCX];
uint8_t sy = CPU::RAM[IO_SCY];
uint8_t wx = CPU::RAM[IO_WX] - 7;
uint8_t wy = CPU::RAM[IO_WY];
for (int i = 0; i < 160; i++)
{
spritey[line][i] = 0;
uint16_t bgTilepos = (((i + sx) >> 3) & 31) + (((line + sy) >> 3) & 31) * 32;
uint16_t windowTilepos = (((i - wx) >> 3) & 31) + (((line - wy) >> 3) & 31) * 32;
uint8_t bgTile = CPU::RAM[bgTilemap + bgTilepos];
uint8_t windowTile = CPU::RAM[windowTilemap + windowTilepos];
uint16_t bgDatapos, windowDatapos;
if (dataSigned) {
bgDatapos = (0x9000 + ((int8_t)bgTile) * 16);
windowDatapos = (0x9000 + ((int8_t)windowTile) * 16);
}
else {
bgDatapos = (0x8000 + (bgTile * 16));
windowDatapos = (0x8000 + (windowTile * 16));
}
bgDatapos += ((line + sy) & 7) << 1;
windowDatapos += ((line + wy) & 7) << 1;
uint8_t txp = ((i + sx) & 7);
uint8_t wxp = ((i - wx) & 7);
uint8_t bgCol = (((CPU::RAM[bgDatapos + 1] >> (7 - txp)) & 1) << 1) |
((CPU::RAM[bgDatapos + 0] >> (7 - txp)) & 1);
uint8_t windowCol = (((CPU::RAM[windowDatapos + 1] >> (7 - wxp)) & 1) << 1) |
((CPU::RAM[windowDatapos + 0] >> (7 - wxp)) & 1);
uint32_t pos = i + line * 160;
bgpixels[i] = bgCol;
pixels[pos] = palette[getPaletteIndex(IO_BGP, bgCol)];
if (showWindow && line >= wy) {
bgpixels[i] = windowCol;
pixels[pos] = palette[getPaletteIndex(IO_BGP, windowCol)];
}
}
}
inline void checkCoincidence()
{
if (CPU::RAM[IO_LY] == CPU::RAM[IO_LYC])
{
CPU::RAM[IO_STAT] |= 0x04;
if (CPU::RAM[IO_STAT] & (1 << 6))
CPU::RAM[IO_IF] |= INTERRUPT_LCDC;
}
else
CPU::RAM[IO_STAT] &= ~0x04;
}
bool pendingVBlank = false;
void step()
{
bool LCDenabled = CPU::RAM[IO_LCDC] & (1 << 7);
uint8_t mode = CPU::RAM[IO_STAT] & 3;
switch(mode)
{
case 0x00: // HBlank
CPU::accessVRAM = true;
if (rendercycles >= 204)
{
rendercycles -= 204;
CPU::RAM[IO_LY]++;
checkCoincidence();
if (CPU::RAM[IO_LY] == 144)
{
// Mode -> 1
CPU::RAM[IO_STAT] &= ~0x03;
CPU::RAM[IO_STAT] |= 0x01;
//CPU::RAM[IO_IF] |= INTERRUPT_VBLANK;
pendingVBlank = true;
refresh();
if (LCDenabled)
{
if (CPU::tasplayer.isRunning())
{
CPU::tasplayer.step();
}
}
}
else
{
// Mode -> 2
CPU::RAM[IO_STAT] &= ~0x03;
CPU::RAM[IO_STAT] |= 0x02;
if (LCDenabled && (CPU::RAM[IO_STAT] & (1 << 5)))
CPU::RAM[IO_IF] |= INTERRUPT_LCDC;
}
if (LCDenabled)
renderLine(CPU::RAM[IO_LY] - 1);
if ((CPU::RAM[IO_LCDC] & 0x2))
drawSprites(CPU::RAM[IO_LY] - 1);
}
break;
case 0x01:
CPU::accessVRAM = true;
if (pendingVBlank && rendercycles >= 24)
{
if (LCDenabled)
{
CPU::RAM[IO_IF] |= INTERRUPT_VBLANK;
if (CPU::RAM[IO_STAT] & (1 << 4))
CPU::RAM[IO_IF] |= INTERRUPT_LCDC;
}
pendingVBlank = false;
}
if (rendercycles >= 456)
{
rendercycles -= 456;
if (CPU::RAM[IO_LY] == 153)
{
CPU::RAM[IO_LY] = 0;
// Mode -> 2
CPU::RAM[IO_STAT] &= ~0x03;
CPU::RAM[IO_STAT] |= 0x02;
if (LCDenabled && (CPU::RAM[IO_STAT] & (1 << 5)))
CPU::RAM[IO_IF] |= INTERRUPT_LCDC;
}
else CPU::RAM[IO_LY]++;
checkCoincidence();
}
break;
case 0x02:
if (rendercycles >= 80)
{
rendercycles -= 80;
// Mode -> 3
CPU::RAM[IO_STAT] &= ~0x03;
CPU::RAM[IO_STAT] |= 0x03;
}
case 0x03:
// CPU::accessVRAM = false;
if (rendercycles >= 172)
{
rendercycles -= 172;
// Mode -> 0
CPU::RAM[IO_STAT] &= ~0x03;
if (LCDenabled && (CPU::RAM[IO_STAT] & (1 << 3)))
CPU::RAM[IO_IF] |= INTERRUPT_LCDC;
}
}
if (!LCDenabled)
{
CPU::accessOAM = true;
CPU::accessVRAM = true;
}
}
void refresh()
{
SDL_UpdateTexture(videoTexture, nullptr, pixels, 160 * sizeof(uint32_t));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, videoTexture, nullptr, nullptr);
SDL_RenderPresent(renderer);
}
uint32_t getWindowID()
{
return SDL_GetWindowID(window);
}
void raise()
{
SDL_RaiseWindow(window);
CPU::debugger.loseFocus();
}
}