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