src/gb/video.c (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "video.h"
7
8#include "core/sync.h"
9#include "core/thread.h"
10#include "gb/gb.h"
11#include "gb/io.h"
12#include "gb/serialize.h"
13
14#include "util/memory.h"
15
16static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model);
17static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer);
18static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
19static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value);
20static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax);
21static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
22static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
23static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
24
25static void _cleanOAM(struct GBVideo* video, int y);
26
27static struct GBVideoRenderer dummyRenderer = {
28 .init = GBVideoDummyRendererInit,
29 .deinit = GBVideoDummyRendererDeinit,
30 .writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
31 .writePalette = GBVideoDummyRendererWritePalette,
32 .drawRange = GBVideoDummyRendererDrawRange,
33 .finishScanline = GBVideoDummyRendererFinishScanline,
34 .finishFrame = GBVideoDummyRendererFinishFrame,
35 .getPixels = GBVideoDummyRendererGetPixels
36};
37
38void GBVideoInit(struct GBVideo* video) {
39 video->renderer = &dummyRenderer;
40 video->vram = 0;
41 video->frameskip = 0;
42}
43
44void GBVideoReset(struct GBVideo* video) {
45 video->ly = 0;
46 video->x = 0;
47 video->mode = 1;
48 video->stat = 1;
49
50 video->nextEvent = INT_MAX;
51 video->eventDiff = 0;
52
53 video->nextMode = INT_MAX;
54 video->dotCounter = INT_MIN;
55 video->nextFrame = INT_MAX;
56
57 video->frameCounter = 0;
58 video->frameskipCounter = 0;
59
60 if (video->vram) {
61 mappedMemoryFree(video->vram, GB_SIZE_VRAM);
62 }
63 video->vram = anonymousMemoryMap(GB_SIZE_VRAM);
64 GBVideoSwitchBank(video, 0);
65 video->renderer->vram = video->vram;
66 memset(&video->oam, 0, sizeof(video->oam));
67 video->renderer->oam = &video->oam;
68 memset(&video->palette, 0, sizeof(video->palette));
69
70 video->renderer->deinit(video->renderer);
71 video->renderer->init(video->renderer, video->p->model);
72}
73
74void GBVideoDeinit(struct GBVideo* video) {
75 GBVideoAssociateRenderer(video, &dummyRenderer);
76 mappedMemoryFree(video->vram, GB_SIZE_VRAM);
77}
78
79void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
80 video->renderer->deinit(video->renderer);
81 video->renderer = renderer;
82 renderer->vram = video->vram;
83 video->renderer->init(video->renderer, video->p->model);
84}
85
86int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles) {
87 video->eventDiff += cycles;
88 if (video->nextEvent != INT_MAX) {
89 video->nextEvent -= cycles;
90 }
91 if (video->nextEvent <= 0) {
92 if (video->nextEvent != INT_MAX) {
93 video->nextMode -= video->eventDiff;
94 video->nextFrame -= video->eventDiff;
95 }
96 video->nextEvent = INT_MAX;
97 GBVideoProcessDots(video);
98 if (video->nextMode <= 0) {
99 int lyc = video->p->memory.io[REG_LYC];
100 switch (video->mode) {
101 case 0:
102 if (video->frameskipCounter <= 0) {
103 video->renderer->finishScanline(video->renderer, video->ly);
104 }
105 ++video->ly;
106 video->p->memory.io[REG_LY] = video->ly;
107 video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
108 if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
109 video->nextMode = GB_VIDEO_MODE_2_LENGTH;
110 video->mode = 2;
111 if (!GBRegisterSTATIsHblankIRQ(video->stat) && GBRegisterSTATIsOAMIRQ(video->stat)) {
112 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
113 }
114 } else {
115 video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
116 video->mode = 1;
117 --video->frameskipCounter;
118 if (video->frameskipCounter < 0) {
119 mCoreSyncPostFrame(video->p->sync);
120 video->frameskipCounter = video->frameskip;
121 }
122 ++video->frameCounter;
123
124 if (video->nextFrame != 0) {
125 video->nextFrame = 0;
126 }
127
128 if (video->p->stream && video->p->stream->postVideoFrame) {
129 const color_t* pixels;
130 unsigned stride;
131 video->renderer->getPixels(video->renderer, &stride, (const void**) &pixels);
132 video->p->stream->postVideoFrame(video->p->stream, pixels, stride);
133 }
134
135 if (GBRegisterSTATIsVblankIRQ(video->stat) || GBRegisterSTATIsOAMIRQ(video->stat)) {
136 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
137 }
138 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
139
140 struct mCoreThread* thread = mCoreThreadGet();
141 mCoreThreadFrameEnded(thread);
142 }
143 if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) {
144 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
145 }
146 GBUpdateIRQs(video->p);
147 break;
148 case 1:
149 // TODO: One M-cycle delay
150 ++video->ly;
151 if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS + 1) {
152 video->ly = 0;
153 video->p->memory.io[REG_LY] = video->ly;
154 video->nextMode = GB_VIDEO_MODE_2_LENGTH;
155 video->mode = 2;
156 if (GBRegisterSTATIsOAMIRQ(video->stat)) {
157 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
158 GBUpdateIRQs(video->p);
159 }
160 video->renderer->finishFrame(video->renderer);
161 if (video->p->memory.mbcType == GB_MBC7 && video->p->memory.rotation && video->p->memory.rotation->sample) {
162 video->p->memory.rotation->sample(video->p->memory.rotation);
163 }
164 break;
165 } else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
166 video->p->memory.io[REG_LY] = 0;
167 video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH - 8;
168 } else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) {
169 video->p->memory.io[REG_LY] = video->ly;
170 video->nextMode = 8;
171 } else {
172 video->p->memory.io[REG_LY] = video->ly;
173 video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
174 }
175
176 video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->p->memory.io[REG_LY]);
177 if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->p->memory.io[REG_LY]) {
178 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
179 GBUpdateIRQs(video->p);
180 }
181 break;
182 case 2:
183 _cleanOAM(video, video->ly);
184 video->dotCounter = 0;
185 video->nextEvent = GB_VIDEO_HORIZONTAL_LENGTH;
186 video->x = 0;
187 video->nextMode = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 12;
188 video->mode = 3;
189 break;
190 case 3:
191 video->nextMode = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 12;
192 video->mode = 0;
193 if (GBRegisterSTATIsHblankIRQ(video->stat)) {
194 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
195 GBUpdateIRQs(video->p);
196 }
197 if (video->ly < GB_VIDEO_VERTICAL_PIXELS && video->p->memory.isHdma && video->p->memory.io[REG_HDMA5] != 0xFF) {
198 video->p->memory.hdmaRemaining = 0x10;
199 video->p->memory.hdmaNext = video->p->cpu->cycles;
200 }
201 break;
202 }
203 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
204 video->p->memory.io[REG_STAT] = video->stat;
205 }
206 if (video->nextFrame <= 0) {
207 if (video->p->cpu->executionState == LR35902_CORE_FETCH) {
208 GBFrameEnded(video->p);
209 video->nextFrame = GB_VIDEO_TOTAL_LENGTH;
210 struct mCoreThread* thread = mCoreThreadGet();
211 mCoreThreadFrameStarted(thread);
212 } else {
213 video->nextFrame = 4 - ((video->p->cpu->executionState + 1) & 3);
214 if (video->nextFrame < video->nextEvent) {
215 video->nextEvent = video->nextFrame;
216 }
217 }
218 }
219 if (video->nextMode < video->nextEvent) {
220 video->nextEvent = video->nextMode;
221 }
222 video->eventDiff = 0;
223 }
224 return video->nextEvent;
225}
226
227static void _cleanOAM(struct GBVideo* video, int y) {
228 // TODO: GBC differences
229 // TODO: Optimize
230 video->objMax = 0;
231 int spriteHeight = 8;
232 if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
233 spriteHeight = 16;
234 }
235 int o = 0;
236 int i;
237 for (i = 0; i < 40; ++i) {
238 uint8_t oy = video->oam.obj[i].y;
239 if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
240 continue;
241 }
242 // TODO: Sort
243 video->objThisLine[o] = video->oam.obj[i];
244 ++o;
245 if (o == 10) {
246 break;
247 }
248 }
249 video->objMax = o;
250}
251
252void GBVideoProcessDots(struct GBVideo* video) {
253 if (video->mode != 3 || video->dotCounter < 0) {
254 return;
255 }
256 int oldX = video->x;
257 video->x = video->dotCounter + video->eventDiff + (video->p->cpu->cycles >> video->p->doubleSpeed);
258 if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
259 video->x = GB_VIDEO_HORIZONTAL_PIXELS;
260 } else if (video->x < 0) {
261 mLOG(GB, FATAL, "Video dot clock went negative!");
262 video->x = oldX;
263 }
264 if (video->x == GB_VIDEO_HORIZONTAL_PIXELS) {
265 video->dotCounter = INT_MIN;
266 }
267 if (video->frameskipCounter <= 0) {
268 video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
269 }
270}
271
272void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
273 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
274 video->mode = 2;
275 video->nextMode = GB_VIDEO_MODE_2_LENGTH - 5; // TODO: Why is this fudge factor needed? Might be related to T-cycles for load/store differing
276 video->nextEvent = video->nextMode;
277 video->eventDiff = -video->p->cpu->cycles >> video->p->doubleSpeed;
278 video->ly = 0;
279 video->p->memory.io[REG_LY] = 0;
280 // TODO: Does this read as 0 for 4 T-cycles?
281 video->stat = GBRegisterSTATSetMode(video->stat, 2);
282 video->stat = GBRegisterSTATSetLYC(video->stat, video->ly == video->p->memory.io[REG_LYC]);
283 if (GBRegisterSTATIsLYCIRQ(video->stat) && video->ly == video->p->memory.io[REG_LYC]) {
284 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
285 GBUpdateIRQs(video->p);
286 }
287 video->p->memory.io[REG_STAT] = video->stat;
288
289 if (video->p->cpu->cycles + (video->nextEvent << video->p->doubleSpeed) < video->p->cpu->nextEvent) {
290 video->p->cpu->nextEvent = video->p->cpu->cycles + (video->nextEvent << video->p->doubleSpeed);
291 }
292 return;
293 }
294 if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
295 video->mode = 0;
296 video->nextMode = INT_MAX;
297 video->nextEvent = INT_MAX;
298 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
299 video->p->memory.io[REG_STAT] = video->stat;
300 video->ly = 0;
301 video->p->memory.io[REG_LY] = 0;
302 }
303}
304
305void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
306 video->stat = (video->stat & 0x7) | (value & 0x78);
307 if (video->p->model == GB_MODEL_DMG && video->mode == 1) {
308 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
309 GBUpdateIRQs(video->p);
310 }
311}
312
313void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
314 static const uint16_t dmgPalette[4] = { 0x7FFF, 0x56B5, 0x294A, 0x0000};
315 if (video->p->model < GB_MODEL_CGB) {
316 switch (address) {
317 case REG_BGP:
318 video->palette[0] = dmgPalette[value & 3];
319 video->palette[1] = dmgPalette[(value >> 2) & 3];
320 video->palette[2] = dmgPalette[(value >> 4) & 3];
321 video->palette[3] = dmgPalette[(value >> 6) & 3];
322 video->renderer->writePalette(video->renderer, 0, video->palette[0]);
323 video->renderer->writePalette(video->renderer, 1, video->palette[1]);
324 video->renderer->writePalette(video->renderer, 2, video->palette[2]);
325 video->renderer->writePalette(video->renderer, 3, video->palette[3]);
326 break;
327 case REG_OBP0:
328 video->palette[8 * 4 + 0] = dmgPalette[value & 3];
329 video->palette[8 * 4 + 1] = dmgPalette[(value >> 2) & 3];
330 video->palette[8 * 4 + 2] = dmgPalette[(value >> 4) & 3];
331 video->palette[8 * 4 + 3] = dmgPalette[(value >> 6) & 3];
332 video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
333 video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
334 video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
335 video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
336 break;
337 case REG_OBP1:
338 video->palette[9 * 4 + 0] = dmgPalette[value & 3];
339 video->palette[9 * 4 + 1] = dmgPalette[(value >> 2) & 3];
340 video->palette[9 * 4 + 2] = dmgPalette[(value >> 4) & 3];
341 video->palette[9 * 4 + 3] = dmgPalette[(value >> 6) & 3];
342 video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
343 video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
344 video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
345 video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
346 break;
347 }
348 } else {
349 switch (address) {
350 case REG_BCPD:
351 if (video->bcpIndex & 1) {
352 video->palette[video->bcpIndex >> 1] &= 0x00FF;
353 video->palette[video->bcpIndex >> 1] |= value << 8;
354 } else {
355 video->palette[video->bcpIndex >> 1] &= 0xFF00;
356 video->palette[video->bcpIndex >> 1] |= value;
357 }
358 video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
359 if (video->bcpIncrement) {
360 ++video->bcpIndex;
361 video->bcpIndex &= 0x3F;
362 video->p->memory.io[REG_BCPS] &= 0x80;
363 video->p->memory.io[REG_BCPS] |= video->bcpIndex;
364 }
365 video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
366 break;
367 case REG_OCPD:
368 if (video->ocpIndex & 1) {
369 video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
370 video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
371 } else {
372 video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
373 video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
374 }
375 video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
376 if (video->ocpIncrement) {
377 ++video->ocpIndex;
378 video->ocpIndex &= 0x3F;
379 video->p->memory.io[REG_OCPS] &= 0x80;
380 video->p->memory.io[REG_OCPS] |= video->ocpIndex;
381 }
382 video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
383 break;
384 }
385 }
386}
387
388void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
389 value &= 1;
390 video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
391 video->vramCurrentBank = value;
392}
393
394static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
395 UNUSED(renderer);
396 UNUSED(model);
397 // Nothing to do
398}
399
400static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
401 UNUSED(renderer);
402 // Nothing to do
403}
404
405static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
406 UNUSED(renderer);
407 UNUSED(address);
408 return value;
409}
410
411static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
412 UNUSED(renderer);
413 UNUSED(index);
414 UNUSED(value);
415}
416
417static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
418 UNUSED(renderer);
419 UNUSED(endX);
420 UNUSED(startX);
421 UNUSED(y);
422 UNUSED(obj);
423 UNUSED(oamMax);
424 // Nothing to do
425}
426
427static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
428 UNUSED(renderer);
429 UNUSED(y);
430 // Nothing to do
431}
432
433static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
434 UNUSED(renderer);
435 // Nothing to do
436}
437
438static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
439 UNUSED(renderer);
440 UNUSED(stride);
441 UNUSED(pixels);
442 // Nothing to do
443}
444
445void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
446 STORE_16LE(video->x, 0, &state->video.x);
447 STORE_16LE(video->ly, 0, &state->video.ly);
448 STORE_32LE(video->nextEvent, 0, &state->video.nextEvent);
449 STORE_32LE(video->eventDiff, 0, &state->video.eventDiff);
450 STORE_32LE(video->nextMode, 0, &state->video.nextMode);
451 STORE_32LE(video->dotCounter, 0, &state->video.dotCounter);
452 STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
453 state->video.vramCurrentBank = video->vramCurrentBank;
454
455 GBSerializedVideoFlags flags = 0;
456 flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
457 flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
458 state->video.flags = flags;
459 STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
460 STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
461
462 size_t i;
463 for (i = 0; i < 64; ++i) {
464 STORE_16LE(video->palette[i], i * 2, state->video.palette);
465 }
466
467 memcpy(state->vram, video->vram, GB_SIZE_VRAM);
468 memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
469}
470
471void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
472 LOAD_16LE(video->x, 0, &state->video.x);
473 LOAD_16LE(video->ly, 0, &state->video.ly);
474 LOAD_32LE(video->nextEvent, 0, &state->video.nextEvent);
475 LOAD_32LE(video->eventDiff, 0, &state->video.eventDiff);
476 LOAD_32LE(video->nextMode, 0, &state->video.nextMode);
477 LOAD_32LE(video->dotCounter, 0, &state->video.dotCounter);
478 LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
479 video->vramCurrentBank = state->video.vramCurrentBank;
480
481 GBSerializedVideoFlags flags = state->video.flags;
482 video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
483 video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
484 LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
485 video->bcpIndex &= 0x3F;
486 LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
487 video->ocpIndex &= 0x3F;
488
489 size_t i;
490 for (i = 0; i < 64; ++i) {
491 LOAD_16LE(video->palette[i], i * 2, state->video.palette);
492 video->renderer->writePalette(video->renderer, i, video->palette[i]);
493 }
494
495 memcpy(video->vram, state->vram, GB_SIZE_VRAM);
496 memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
497
498 _cleanOAM(video, video->ly);
499 GBVideoSwitchBank(video, video->vramCurrentBank);
500}