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 <mgba/internal/gb/video.h>
7
8#include <mgba/core/sync.h>
9#include <mgba/core/thread.h>
10#include <mgba/core/tile-cache.h>
11#include <mgba/internal/gb/gb.h>
12#include <mgba/internal/gb/io.h>
13#include <mgba/internal/gb/serialize.h>
14#include <mgba/internal/lr35902/lr35902.h>
15
16#include <mgba-util/memory.h>
17
18static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model);
19static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer);
20static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
21static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value);
22static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address);
23static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam);
24static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax);
25static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
26static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
27static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
28static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
29
30static void _cleanOAM(struct GBVideo* video, int y);
31
32static void _endMode0(struct mTiming* timing, void* context, uint32_t cyclesLate);
33static void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate);
34static void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate);
35static void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate);
36static void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate);
37
38static struct GBVideoRenderer dummyRenderer = {
39 .init = GBVideoDummyRendererInit,
40 .deinit = GBVideoDummyRendererDeinit,
41 .writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
42 .writeVRAM = GBVideoDummyRendererWriteVRAM,
43 .writeOAM = GBVideoDummyRendererWriteOAM,
44 .writePalette = GBVideoDummyRendererWritePalette,
45 .drawRange = GBVideoDummyRendererDrawRange,
46 .finishScanline = GBVideoDummyRendererFinishScanline,
47 .finishFrame = GBVideoDummyRendererFinishFrame,
48 .getPixels = GBVideoDummyRendererGetPixels,
49 .putPixels = GBVideoDummyRendererPutPixels,
50};
51
52void GBVideoInit(struct GBVideo* video) {
53 video->renderer = &dummyRenderer;
54 video->renderer->cache = NULL;
55 video->vram = 0;
56 video->frameskip = 0;
57
58 video->modeEvent.context = video;
59 video->modeEvent.name = "GB Video Mode";
60 video->modeEvent.callback = NULL;
61 video->modeEvent.priority = 8;
62 video->frameEvent.context = video;
63 video->frameEvent.name = "GB Video Frame";
64 video->frameEvent.callback = _updateFrameCount;
65 video->frameEvent.priority = 9;
66
67 video->dmgPalette[0] = 0x7FFF;
68 video->dmgPalette[1] = 0x56B5;
69 video->dmgPalette[2] = 0x294A;
70 video->dmgPalette[3] = 0x0000;
71}
72
73void GBVideoReset(struct GBVideo* video) {
74 video->ly = 0;
75 video->x = 0;
76 video->mode = 1;
77 video->stat = 1;
78
79 video->frameCounter = 0;
80 video->frameskipCounter = 0;
81
82 if (video->vram) {
83 mappedMemoryFree(video->vram, GB_SIZE_VRAM);
84 }
85 video->vram = anonymousMemoryMap(GB_SIZE_VRAM);
86 GBVideoSwitchBank(video, 0);
87 video->renderer->vram = video->vram;
88 memset(&video->oam, 0, sizeof(video->oam));
89 video->renderer->oam = &video->oam;
90 memset(&video->palette, 0, sizeof(video->palette));
91
92 video->renderer->deinit(video->renderer);
93 video->renderer->init(video->renderer, video->p->model);
94}
95
96void GBVideoDeinit(struct GBVideo* video) {
97 GBVideoAssociateRenderer(video, &dummyRenderer);
98 mappedMemoryFree(video->vram, GB_SIZE_VRAM);
99}
100
101void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
102 video->renderer->deinit(video->renderer);
103 renderer->cache = video->renderer->cache;
104 video->renderer = renderer;
105 renderer->vram = video->vram;
106 video->renderer->init(video->renderer, video->p->model);
107}
108
109static bool _statIRQAsserted(struct GBVideo* video, GBRegisterSTAT stat) {
110 // TODO: variable for the IRQ line value?
111 if (GBRegisterSTATIsLYCIRQ(stat) && GBRegisterSTATIsLYC(stat)) {
112 return true;
113 }
114 switch (GBRegisterSTATGetMode(stat)) {
115 case 0:
116 if (GBRegisterSTATIsHblankIRQ(stat)) {
117 return true;
118 }
119 break;
120 case 1:
121 if (GBRegisterSTATIsVblankIRQ(stat)) {
122 return true;
123 }
124 break;
125 case 2:
126 if (GBRegisterSTATIsOAMIRQ(stat)) {
127 return true;
128 }
129 break;
130 case 3:
131 break;
132 }
133 return false;
134}
135
136void _endMode0(struct mTiming* timing, void* context, uint32_t cyclesLate) {
137 struct GBVideo* video = context;
138 if (video->frameskipCounter <= 0) {
139 video->renderer->finishScanline(video->renderer, video->ly);
140 }
141 int lyc = video->p->memory.io[REG_LYC];
142 int32_t next;
143 ++video->ly;
144 video->p->memory.io[REG_LY] = video->ly;
145 GBRegisterSTAT oldStat = video->stat;
146 video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
147 if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
148 // TODO: Cache SCX & 7 in case it changes during mode 2
149 next = GB_VIDEO_MODE_2_LENGTH + (video->p->memory.io[REG_SCX] & 7);
150 video->mode = 2;
151 video->modeEvent.callback = _endMode2;
152 } else {
153 next = GB_VIDEO_HORIZONTAL_LENGTH;
154 video->mode = 1;
155 video->modeEvent.callback = _endMode1;
156
157 mTimingDeschedule(&video->p->timing, &video->frameEvent);
158 mTimingSchedule(&video->p->timing, &video->frameEvent, -cyclesLate);
159
160 if (!_statIRQAsserted(video, oldStat) && GBRegisterSTATIsOAMIRQ(video->stat)) {
161 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
162 }
163 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
164 }
165 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
166 if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
167 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
168 }
169 GBUpdateIRQs(video->p);
170 video->p->memory.io[REG_STAT] = video->stat;
171 mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
172}
173
174void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate) {
175 struct GBVideo* video = context;
176 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
177 return;
178 }
179 int lyc = video->p->memory.io[REG_LYC];
180 // TODO: One M-cycle delay
181 ++video->ly;
182 int32_t next;
183 if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS + 1) {
184 video->ly = 0;
185 video->p->memory.io[REG_LY] = video->ly;
186 next = GB_VIDEO_MODE_2_LENGTH + (video->p->memory.io[REG_SCX] & 7);
187 video->mode = 2;
188 video->modeEvent.callback = _endMode2;
189 } else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
190 video->p->memory.io[REG_LY] = 0;
191 next = GB_VIDEO_HORIZONTAL_LENGTH - 8;
192 } else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) {
193 video->p->memory.io[REG_LY] = video->ly;
194 next = 8;
195 } else {
196 video->p->memory.io[REG_LY] = video->ly;
197 next = GB_VIDEO_HORIZONTAL_LENGTH;
198 }
199
200 GBRegisterSTAT oldStat = video->stat;
201 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
202 video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->p->memory.io[REG_LY]);
203 if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
204 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
205 GBUpdateIRQs(video->p);
206 }
207 video->p->memory.io[REG_STAT] = video->stat;
208 mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
209}
210
211void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate) {
212 struct GBVideo* video = context;
213 _cleanOAM(video, video->ly);
214 video->x = 0;
215 video->dotClock = timing->masterCycles - cyclesLate;
216 int32_t next = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 6 - (video->p->memory.io[REG_SCX] & 7);
217 video->mode = 3;
218 video->modeEvent.callback = _endMode3;
219 GBRegisterSTAT oldStat = video->stat;
220 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
221 if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
222 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
223 GBUpdateIRQs(video->p);
224 }
225 video->p->memory.io[REG_STAT] = video->stat;
226 mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
227}
228
229void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate) {
230 struct GBVideo* video = context;
231 GBVideoProcessDots(video);
232 if (video->ly < GB_VIDEO_VERTICAL_PIXELS && video->p->memory.isHdma && video->p->memory.io[REG_HDMA5] != 0xFF) {
233 video->p->memory.hdmaRemaining = 0x10;
234 video->p->cpuBlocked = true;
235 mTimingDeschedule(timing, &video->p->memory.hdmaEvent);
236 mTimingSchedule(timing, &video->p->memory.hdmaEvent, 0);
237 }
238 video->mode = 0;
239 video->modeEvent.callback = _endMode0;
240 GBRegisterSTAT oldStat = video->stat;
241 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
242 if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
243 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
244 GBUpdateIRQs(video->p);
245 }
246 video->p->memory.io[REG_STAT] = video->stat;
247 int32_t next = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 6;
248 mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
249}
250
251void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate) {
252 UNUSED(cyclesLate);
253 struct GBVideo* video = context;
254 if (video->p->cpu->executionState != LR35902_CORE_FETCH) {
255 mTimingSchedule(timing, &video->frameEvent, 4 - ((video->p->cpu->executionState + 1) & 3));
256 return;
257 }
258
259 size_t c;
260 for (c = 0; c < mCoreCallbacksListSize(&video->p->coreCallbacks); ++c) {
261 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&video->p->coreCallbacks, c);
262 if (callbacks->videoFrameEnded) {
263 callbacks->videoFrameEnded(callbacks->context);
264 }
265 }
266
267 GBFrameEnded(video->p);
268 mCoreSyncPostFrame(video->p->sync);
269 --video->frameskipCounter;
270 if (video->frameskipCounter < 0) {
271 video->renderer->finishFrame(video->renderer);
272 video->frameskipCounter = video->frameskip;
273 }
274 ++video->frameCounter;
275
276 // TODO: Move to common code
277 if (video->p->stream && video->p->stream->postVideoFrame) {
278 const color_t* pixels;
279 size_t stride;
280 video->renderer->getPixels(video->renderer, &stride, (const void**) &pixels);
281 video->p->stream->postVideoFrame(video->p->stream, pixels, stride);
282 }
283
284 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
285 mTimingSchedule(timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
286 }
287
288 for (c = 0; c < mCoreCallbacksListSize(&video->p->coreCallbacks); ++c) {
289 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&video->p->coreCallbacks, c);
290 if (callbacks->videoFrameStarted) {
291 callbacks->videoFrameStarted(callbacks->context);
292 }
293 }
294}
295
296static void _cleanOAM(struct GBVideo* video, int y) {
297 // TODO: GBC differences
298 // TODO: Optimize
299 video->objMax = 0;
300 int spriteHeight = 8;
301 if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
302 spriteHeight = 16;
303 }
304 int o = 0;
305 int i;
306 for (i = 0; i < 40; ++i) {
307 uint8_t oy = video->oam.obj[i].y;
308 if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
309 continue;
310 }
311 // TODO: Sort
312 video->objThisLine[o] = video->oam.obj[i];
313 ++o;
314 if (o == 10) {
315 break;
316 }
317 }
318 video->objMax = o;
319}
320
321void GBVideoProcessDots(struct GBVideo* video) {
322 if (video->mode != 3) {
323 return;
324 }
325 int oldX = video->x;
326 video->x = (video->p->timing.masterCycles - video->dotClock + video->p->cpu->cycles) >> video->p->doubleSpeed;
327 if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
328 video->x = GB_VIDEO_HORIZONTAL_PIXELS;
329 } else if (video->x < 0) {
330 mLOG(GB, FATAL, "Video dot clock went negative!");
331 video->x = oldX;
332 }
333 if (video->frameskipCounter <= 0) {
334 video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
335 }
336}
337
338void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
339 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
340 video->mode = 2;
341 video->modeEvent.callback = _endMode2;
342 int32_t next = GB_VIDEO_MODE_2_LENGTH - 5; // TODO: Why is this fudge factor needed? Might be related to T-cycles for load/store differing
343 mTimingSchedule(&video->p->timing, &video->modeEvent, next << video->p->doubleSpeed);
344
345 video->ly = 0;
346 video->p->memory.io[REG_LY] = 0;
347 GBRegisterSTAT oldStat = video->stat;
348 video->stat = GBRegisterSTATSetMode(video->stat, 0);
349 video->stat = GBRegisterSTATSetLYC(video->stat, video->ly == video->p->memory.io[REG_LYC]);
350 if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
351 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
352 GBUpdateIRQs(video->p);
353 }
354 video->p->memory.io[REG_STAT] = video->stat;
355 video->renderer->writePalette(video->renderer, 0, video->palette[0]);
356
357 mTimingDeschedule(&video->p->timing, &video->frameEvent);
358 }
359 if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
360 // TODO: Fix serialization; this gets internal and visible modes out of sync
361 video->stat = GBRegisterSTATSetMode(video->stat, 0);
362 video->p->memory.io[REG_STAT] = video->stat;
363 video->ly = 0;
364 video->p->memory.io[REG_LY] = 0;
365 video->renderer->writePalette(video->renderer, 0, video->dmgPalette[0]);
366
367 mTimingDeschedule(&video->p->timing, &video->modeEvent);
368 mTimingSchedule(&video->p->timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
369 }
370 video->p->memory.io[REG_STAT] = video->stat;
371}
372
373void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
374 GBRegisterSTAT oldStat = video->stat;
375 video->stat = (video->stat & 0x7) | (value & 0x78);
376 if (video->p->model == GB_MODEL_DMG && !_statIRQAsserted(video, oldStat) && video->mode < 3) {
377 // TODO: variable for the IRQ line value?
378 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
379 GBUpdateIRQs(video->p);
380 }
381}
382
383void GBVideoWriteLYC(struct GBVideo* video, uint8_t value) {
384 GBRegisterSTAT oldStat = video->stat;
385 video->stat = GBRegisterSTATSetLYC(video->stat, value == video->ly);
386 if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
387 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
388 GBUpdateIRQs(video->p);
389 }
390 video->p->memory.io[REG_STAT] = video->stat;
391}
392
393void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
394 if (video->p->model < GB_MODEL_CGB) {
395 switch (address) {
396 case REG_BGP:
397 video->palette[0] = video->dmgPalette[value & 3];
398 video->palette[1] = video->dmgPalette[(value >> 2) & 3];
399 video->palette[2] = video->dmgPalette[(value >> 4) & 3];
400 video->palette[3] = video->dmgPalette[(value >> 6) & 3];
401 video->renderer->writePalette(video->renderer, 0, video->palette[0]);
402 video->renderer->writePalette(video->renderer, 1, video->palette[1]);
403 video->renderer->writePalette(video->renderer, 2, video->palette[2]);
404 video->renderer->writePalette(video->renderer, 3, video->palette[3]);
405 break;
406 case REG_OBP0:
407 video->palette[8 * 4 + 0] = video->dmgPalette[value & 3];
408 video->palette[8 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
409 video->palette[8 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
410 video->palette[8 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
411 video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
412 video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
413 video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
414 video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
415 break;
416 case REG_OBP1:
417 video->palette[9 * 4 + 0] = video->dmgPalette[value & 3];
418 video->palette[9 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
419 video->palette[9 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
420 video->palette[9 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
421 video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
422 video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
423 video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
424 video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
425 break;
426 }
427 } else {
428 switch (address) {
429 case REG_BCPD:
430 if (video->bcpIndex & 1) {
431 video->palette[video->bcpIndex >> 1] &= 0x00FF;
432 video->palette[video->bcpIndex >> 1] |= value << 8;
433 } else {
434 video->palette[video->bcpIndex >> 1] &= 0xFF00;
435 video->palette[video->bcpIndex >> 1] |= value;
436 }
437 video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
438 if (video->bcpIncrement) {
439 ++video->bcpIndex;
440 video->bcpIndex &= 0x3F;
441 video->p->memory.io[REG_BCPS] &= 0x80;
442 video->p->memory.io[REG_BCPS] |= video->bcpIndex;
443 }
444 video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
445 break;
446 case REG_OCPD:
447 if (video->ocpIndex & 1) {
448 video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
449 video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
450 } else {
451 video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
452 video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
453 }
454 video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
455 if (video->ocpIncrement) {
456 ++video->ocpIndex;
457 video->ocpIndex &= 0x3F;
458 video->p->memory.io[REG_OCPS] &= 0x80;
459 video->p->memory.io[REG_OCPS] |= video->ocpIndex;
460 }
461 video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
462 break;
463 }
464 }
465}
466
467void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
468 value &= 1;
469 video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
470 video->vramCurrentBank = value;
471}
472
473void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
474 if (index >= 4) {
475 return;
476 }
477 video->dmgPalette[index] = M_RGB8_TO_RGB5(color);
478}
479
480static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
481 UNUSED(renderer);
482 UNUSED(model);
483 // Nothing to do
484}
485
486static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
487 UNUSED(renderer);
488 // Nothing to do
489}
490
491static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
492 UNUSED(renderer);
493 UNUSED(address);
494 return value;
495}
496
497static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
498 if (renderer->cache) {
499 mTileCacheWriteVRAM(renderer->cache, address);
500 }
501}
502
503static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
504 UNUSED(renderer);
505 UNUSED(oam);
506 // Nothing to do
507}
508
509static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
510 UNUSED(value);
511 if (renderer->cache) {
512 mTileCacheWritePalette(renderer->cache, index << 1);
513 }
514}
515
516static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
517 UNUSED(renderer);
518 UNUSED(endX);
519 UNUSED(startX);
520 UNUSED(y);
521 UNUSED(obj);
522 UNUSED(oamMax);
523 // Nothing to do
524}
525
526static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
527 UNUSED(renderer);
528 UNUSED(y);
529 // Nothing to do
530}
531
532static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
533 UNUSED(renderer);
534 // Nothing to do
535}
536
537static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
538 UNUSED(renderer);
539 UNUSED(stride);
540 UNUSED(pixels);
541 // Nothing to do
542}
543
544static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
545 UNUSED(renderer);
546 UNUSED(stride);
547 UNUSED(pixels);
548 // Nothing to do
549}
550
551void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
552 STORE_16LE(video->x, 0, &state->video.x);
553 STORE_16LE(video->ly, 0, &state->video.ly);
554 STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
555 state->video.vramCurrentBank = video->vramCurrentBank;
556
557 GBSerializedVideoFlags flags = 0;
558 flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
559 flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
560 flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
561 flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
562 flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
563 state->video.flags = flags;
564 STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
565 STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
566
567 size_t i;
568 for (i = 0; i < 64; ++i) {
569 STORE_16LE(video->palette[i], i * 2, state->video.palette);
570 }
571
572 STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
573 STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
574
575 memcpy(state->vram, video->vram, GB_SIZE_VRAM);
576 memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
577}
578
579void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
580 LOAD_16LE(video->x, 0, &state->video.x);
581 LOAD_16LE(video->ly, 0, &state->video.ly);
582 LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
583 video->vramCurrentBank = state->video.vramCurrentBank;
584
585 GBSerializedVideoFlags flags = state->video.flags;
586 video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
587 video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
588 video->mode = GBSerializedVideoFlagsGetMode(flags);
589 LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
590 video->bcpIndex &= 0x3F;
591 LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
592 video->ocpIndex &= 0x3F;
593
594 switch (video->mode) {
595 case 0:
596 video->modeEvent.callback = _endMode0;
597 break;
598 case 1:
599 video->modeEvent.callback = _endMode1;
600 break;
601 case 2:
602 video->modeEvent.callback = _endMode2;
603 break;
604 case 3:
605 video->modeEvent.callback = _endMode3;
606 break;
607 }
608
609 uint32_t when;
610 if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
611 LOAD_32LE(when, 0, &state->video.nextMode);
612 mTimingSchedule(&video->p->timing, &video->modeEvent, when);
613 }
614 if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
615 LOAD_32LE(when, 0, &state->video.nextFrame);
616 mTimingSchedule(&video->p->timing, &video->frameEvent, when);
617 }
618
619 size_t i;
620 for (i = 0; i < 64; ++i) {
621 LOAD_16LE(video->palette[i], i * 2, state->video.palette);
622 video->renderer->writePalette(video->renderer, i, video->palette[i]);
623 }
624
625 memcpy(video->vram, state->vram, GB_SIZE_VRAM);
626 memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
627
628 _cleanOAM(video, video->ly);
629 GBVideoSwitchBank(video, video->vramCurrentBank);
630
631 video->renderer->deinit(video->renderer);
632 video->renderer->init(video->renderer, video->p->model);
633}