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