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