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 mTimingSchedule(&video->p->timing, &video->frameEvent, -cyclesLate);
158
159 if (!_statIRQAsserted(video, oldStat) && GBRegisterSTATIsOAMIRQ(video->stat)) {
160 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
161 }
162 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
163 }
164 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
165 if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
166 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
167 }
168 GBUpdateIRQs(video->p);
169 video->p->memory.io[REG_STAT] = video->stat;
170 mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
171}
172
173void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate) {
174 struct GBVideo* video = context;
175 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
176 return;
177 }
178 int lyc = video->p->memory.io[REG_LYC];
179 // TODO: One M-cycle delay
180 ++video->ly;
181 int32_t next;
182 if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS + 1) {
183 video->ly = 0;
184 video->p->memory.io[REG_LY] = video->ly;
185 next = GB_VIDEO_MODE_2_LENGTH + (video->p->memory.io[REG_SCX] & 7);
186 video->mode = 2;
187 video->modeEvent.callback = _endMode2;
188 if (video->p->memory.mbcType == GB_MBC7 && video->p->memory.rotation && video->p->memory.rotation->sample) {
189 video->p->memory.rotation->sample(video->p->memory.rotation);
190 }
191 } else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
192 video->p->memory.io[REG_LY] = 0;
193 next = GB_VIDEO_HORIZONTAL_LENGTH - 8;
194 } else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) {
195 video->p->memory.io[REG_LY] = video->ly;
196 next = 8;
197 } else {
198 video->p->memory.io[REG_LY] = video->ly;
199 next = GB_VIDEO_HORIZONTAL_LENGTH;
200 }
201
202 GBRegisterSTAT oldStat = video->stat;
203 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
204 video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->p->memory.io[REG_LY]);
205 if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
206 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
207 GBUpdateIRQs(video->p);
208 }
209 video->p->memory.io[REG_STAT] = video->stat;
210 mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
211}
212
213void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate) {
214 struct GBVideo* video = context;
215 _cleanOAM(video, video->ly);
216 video->x = 0;
217 video->dotClock = timing->masterCycles - cyclesLate;
218 int32_t next = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 6 - (video->p->memory.io[REG_SCX] & 7);
219 video->mode = 3;
220 video->modeEvent.callback = _endMode3;
221 GBRegisterSTAT oldStat = video->stat;
222 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
223 if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
224 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
225 GBUpdateIRQs(video->p);
226 }
227 video->p->memory.io[REG_STAT] = video->stat;
228 mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
229}
230
231void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate) {
232 struct GBVideo* video = context;
233 GBVideoProcessDots(video);
234 if (video->ly < GB_VIDEO_VERTICAL_PIXELS && video->p->memory.isHdma && video->p->memory.io[REG_HDMA5] != 0xFF) {
235 video->p->memory.hdmaRemaining = 0x10;
236 mTimingDeschedule(timing, &video->p->memory.hdmaEvent);
237 mTimingSchedule(timing, &video->p->memory.hdmaEvent, 0);
238 }
239 video->mode = 0;
240 video->modeEvent.callback = _endMode0;
241 GBRegisterSTAT oldStat = video->stat;
242 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
243 if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
244 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
245 GBUpdateIRQs(video->p);
246 }
247 video->p->memory.io[REG_STAT] = video->stat;
248 int32_t next = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 6;
249 mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
250}
251
252void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate) {
253 UNUSED(cyclesLate);
254 struct GBVideo* video = context;
255 if (video->p->cpu->executionState != LR35902_CORE_FETCH) {
256 mTimingSchedule(timing, &video->frameEvent, 4 - ((video->p->cpu->executionState + 1) & 3));
257 return;
258 }
259
260 size_t c;
261 for (c = 0; c < mCoreCallbacksListSize(&video->p->coreCallbacks); ++c) {
262 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&video->p->coreCallbacks, c);
263 if (callbacks->videoFrameEnded) {
264 callbacks->videoFrameEnded(callbacks->context);
265 }
266 }
267
268 GBFrameEnded(video->p);
269 --video->frameskipCounter;
270 if (video->frameskipCounter < 0) {
271 video->renderer->finishFrame(video->renderer);
272 mCoreSyncPostFrame(video->p->sync);
273 video->frameskipCounter = video->frameskip;
274 }
275 ++video->frameCounter;
276
277 // TODO: Move to common code
278 if (video->p->stream && video->p->stream->postVideoFrame) {
279 const color_t* pixels;
280 size_t stride;
281 video->renderer->getPixels(video->renderer, &stride, (const void**) &pixels);
282 video->p->stream->postVideoFrame(video->p->stream, pixels, stride);
283 }
284
285 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
286 mTimingSchedule(timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
287 }
288
289 for (c = 0; c < mCoreCallbacksListSize(&video->p->coreCallbacks); ++c) {
290 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&video->p->coreCallbacks, c);
291 if (callbacks->videoFrameStarted) {
292 callbacks->videoFrameStarted(callbacks->context);
293 }
294 }
295}
296
297static void _cleanOAM(struct GBVideo* video, int y) {
298 // TODO: GBC differences
299 // TODO: Optimize
300 video->objMax = 0;
301 int spriteHeight = 8;
302 if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
303 spriteHeight = 16;
304 }
305 int o = 0;
306 int i;
307 for (i = 0; i < 40; ++i) {
308 uint8_t oy = video->oam.obj[i].y;
309 if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
310 continue;
311 }
312 // TODO: Sort
313 video->objThisLine[o] = video->oam.obj[i];
314 ++o;
315 if (o == 10) {
316 break;
317 }
318 }
319 video->objMax = o;
320}
321
322void GBVideoProcessDots(struct GBVideo* video) {
323 if (video->mode != 3) {
324 return;
325 }
326 int oldX = video->x;
327 video->x = (video->p->timing.masterCycles - video->dotClock + video->p->cpu->cycles) >> video->p->doubleSpeed;
328 if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
329 video->x = GB_VIDEO_HORIZONTAL_PIXELS;
330 } else if (video->x < 0) {
331 mLOG(GB, FATAL, "Video dot clock went negative!");
332 video->x = oldX;
333 }
334 if (video->frameskipCounter <= 0) {
335 video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
336 }
337}
338
339void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
340 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
341 video->mode = 2;
342 video->modeEvent.callback = _endMode2;
343 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
344 mTimingSchedule(&video->p->timing, &video->modeEvent, next << video->p->doubleSpeed);
345
346 video->ly = 0;
347 video->p->memory.io[REG_LY] = 0;
348 // TODO: Does this read as 0 for 4 T-cycles?
349 GBRegisterSTAT oldStat = video->stat;
350 video->stat = GBRegisterSTATSetMode(video->stat, 2);
351 video->stat = GBRegisterSTATSetLYC(video->stat, video->ly == video->p->memory.io[REG_LYC]);
352 if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
353 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
354 GBUpdateIRQs(video->p);
355 }
356 video->p->memory.io[REG_STAT] = video->stat;
357 video->renderer->writePalette(video->renderer, 0, video->palette[0]);
358
359 mTimingDeschedule(&video->p->timing, &video->frameEvent);
360 }
361 if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
362 // TODO: Fix serialization; this gets internal and visible modes out of sync
363 video->stat = GBRegisterSTATSetMode(video->stat, 0);
364 video->p->memory.io[REG_STAT] = video->stat;
365 video->ly = 0;
366 video->p->memory.io[REG_LY] = 0;
367 video->renderer->writePalette(video->renderer, 0, video->dmgPalette[0]);
368
369 mTimingDeschedule(&video->p->timing, &video->modeEvent);
370 mTimingSchedule(&video->p->timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
371 }
372 video->p->memory.io[REG_STAT] = video->stat;
373}
374
375void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
376 GBRegisterSTAT oldStat = video->stat;
377 video->stat = (video->stat & 0x7) | (value & 0x78);
378 if (video->p->model == GB_MODEL_DMG && !_statIRQAsserted(video, oldStat) && video->mode < 3) {
379 // TODO: variable for the IRQ line value?
380 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
381 GBUpdateIRQs(video->p);
382 }
383}
384
385void GBVideoWriteLYC(struct GBVideo* video, uint8_t value) {
386 GBRegisterSTAT oldStat = video->stat;
387 video->stat = GBRegisterSTATSetLYC(video->stat, value == video->ly);
388 if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
389 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
390 GBUpdateIRQs(video->p);
391 }
392}
393
394void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
395 if (video->p->model < GB_MODEL_CGB) {
396 switch (address) {
397 case REG_BGP:
398 video->palette[0] = video->dmgPalette[value & 3];
399 video->palette[1] = video->dmgPalette[(value >> 2) & 3];
400 video->palette[2] = video->dmgPalette[(value >> 4) & 3];
401 video->palette[3] = video->dmgPalette[(value >> 6) & 3];
402 video->renderer->writePalette(video->renderer, 0, video->palette[0]);
403 video->renderer->writePalette(video->renderer, 1, video->palette[1]);
404 video->renderer->writePalette(video->renderer, 2, video->palette[2]);
405 video->renderer->writePalette(video->renderer, 3, video->palette[3]);
406 break;
407 case REG_OBP0:
408 video->palette[8 * 4 + 0] = video->dmgPalette[value & 3];
409 video->palette[8 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
410 video->palette[8 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
411 video->palette[8 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
412 video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
413 video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
414 video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
415 video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
416 break;
417 case REG_OBP1:
418 video->palette[9 * 4 + 0] = video->dmgPalette[value & 3];
419 video->palette[9 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
420 video->palette[9 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
421 video->palette[9 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
422 video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
423 video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
424 video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
425 video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
426 break;
427 }
428 } else {
429 switch (address) {
430 case REG_BCPD:
431 if (video->bcpIndex & 1) {
432 video->palette[video->bcpIndex >> 1] &= 0x00FF;
433 video->palette[video->bcpIndex >> 1] |= value << 8;
434 } else {
435 video->palette[video->bcpIndex >> 1] &= 0xFF00;
436 video->palette[video->bcpIndex >> 1] |= value;
437 }
438 video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
439 if (video->bcpIncrement) {
440 ++video->bcpIndex;
441 video->bcpIndex &= 0x3F;
442 video->p->memory.io[REG_BCPS] &= 0x80;
443 video->p->memory.io[REG_BCPS] |= video->bcpIndex;
444 }
445 video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
446 break;
447 case REG_OCPD:
448 if (video->ocpIndex & 1) {
449 video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
450 video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
451 } else {
452 video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
453 video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
454 }
455 video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
456 if (video->ocpIncrement) {
457 ++video->ocpIndex;
458 video->ocpIndex &= 0x3F;
459 video->p->memory.io[REG_OCPS] &= 0x80;
460 video->p->memory.io[REG_OCPS] |= video->ocpIndex;
461 }
462 video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
463 break;
464 }
465 }
466}
467
468void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
469 value &= 1;
470 video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
471 video->vramCurrentBank = value;
472}
473
474void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
475 if (index >= 4) {
476 return;
477 }
478 video->dmgPalette[index] = M_RGB8_TO_RGB5(color);
479}
480
481static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
482 UNUSED(renderer);
483 UNUSED(model);
484 // Nothing to do
485}
486
487static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
488 UNUSED(renderer);
489 // Nothing to do
490}
491
492static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
493 UNUSED(renderer);
494 UNUSED(address);
495 return value;
496}
497
498static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
499 if (renderer->cache) {
500 mTileCacheWriteVRAM(renderer->cache, address);
501 }
502}
503
504static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
505 UNUSED(renderer);
506 UNUSED(oam);
507 // Nothing to do
508}
509
510static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
511 UNUSED(value);
512 if (renderer->cache) {
513 mTileCacheWritePalette(renderer->cache, index << 1);
514 }
515}
516
517static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
518 UNUSED(renderer);
519 UNUSED(endX);
520 UNUSED(startX);
521 UNUSED(y);
522 UNUSED(obj);
523 UNUSED(oamMax);
524 // Nothing to do
525}
526
527static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
528 UNUSED(renderer);
529 UNUSED(y);
530 // Nothing to do
531}
532
533static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
534 UNUSED(renderer);
535 // Nothing to do
536}
537
538static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
539 UNUSED(renderer);
540 UNUSED(stride);
541 UNUSED(pixels);
542 // Nothing to do
543}
544
545static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
546 UNUSED(renderer);
547 UNUSED(stride);
548 UNUSED(pixels);
549 // Nothing to do
550}
551
552void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
553 STORE_16LE(video->x, 0, &state->video.x);
554 STORE_16LE(video->ly, 0, &state->video.ly);
555 STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
556 state->video.vramCurrentBank = video->vramCurrentBank;
557
558 GBSerializedVideoFlags flags = 0;
559 flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
560 flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
561 flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
562 flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
563 flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
564 state->video.flags = flags;
565 STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
566 STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
567
568 size_t i;
569 for (i = 0; i < 64; ++i) {
570 STORE_16LE(video->palette[i], i * 2, state->video.palette);
571 }
572
573 STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
574 STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
575
576 memcpy(state->vram, video->vram, GB_SIZE_VRAM);
577 memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
578}
579
580void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
581 LOAD_16LE(video->x, 0, &state->video.x);
582 LOAD_16LE(video->ly, 0, &state->video.ly);
583 LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
584 video->vramCurrentBank = state->video.vramCurrentBank;
585
586 GBSerializedVideoFlags flags = state->video.flags;
587 video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
588 video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
589 video->mode = GBSerializedVideoFlagsGetMode(flags);
590 LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
591 video->bcpIndex &= 0x3F;
592 LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
593 video->ocpIndex &= 0x3F;
594
595 switch (video->mode) {
596 case 0:
597 video->modeEvent.callback = _endMode0;
598 break;
599 case 1:
600 video->modeEvent.callback = _endMode1;
601 break;
602 case 2:
603 video->modeEvent.callback = _endMode2;
604 break;
605 case 3:
606 video->modeEvent.callback = _endMode3;
607 break;
608 }
609
610 uint32_t when;
611 if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
612 LOAD_32LE(when, 0, &state->video.nextMode);
613 mTimingSchedule(&video->p->timing, &video->modeEvent, when);
614 }
615 if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
616 LOAD_32LE(when, 0, &state->video.nextFrame);
617 mTimingSchedule(&video->p->timing, &video->frameEvent, when);
618 }
619
620 size_t i;
621 for (i = 0; i < 64; ++i) {
622 LOAD_16LE(video->palette[i], i * 2, state->video.palette);
623 video->renderer->writePalette(video->renderer, i, video->palette[i]);
624 }
625
626 memcpy(video->vram, state->vram, GB_SIZE_VRAM);
627 memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
628
629 _cleanOAM(video, video->ly);
630 GBVideoSwitchBank(video, video->vramCurrentBank);
631
632 video->renderer->deinit(video->renderer);
633 video->renderer->init(video->renderer, video->p->model);
634}