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 mTimingDeschedule(&video->p->timing, &video->modeEvent);
325 mTimingSchedule(&video->p->timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
326 }
327 video->p->memory.io[REG_STAT] = video->stat;
328}
329
330void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
331 video->stat = (video->stat & 0x7) | (value & 0x78);
332 if (video->p->model == GB_MODEL_DMG && video->mode == 1) {
333 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
334 GBUpdateIRQs(video->p);
335 }
336}
337
338void GBVideoWriteLYC(struct GBVideo* video, uint8_t value) {
339 if (video->mode == 2) {
340 video->stat = GBRegisterSTATSetLYC(video->stat, value == video->ly);
341 if (GBRegisterSTATIsLYCIRQ(video->stat) && value == video->ly) {
342 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
343 GBUpdateIRQs(video->p);
344 }
345 }
346}
347
348void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
349 static const uint16_t dmgPalette[4] = { 0x7FFF, 0x56B5, 0x294A, 0x0000};
350 if (video->p->model < GB_MODEL_CGB) {
351 switch (address) {
352 case REG_BGP:
353 video->palette[0] = dmgPalette[value & 3];
354 video->palette[1] = dmgPalette[(value >> 2) & 3];
355 video->palette[2] = dmgPalette[(value >> 4) & 3];
356 video->palette[3] = dmgPalette[(value >> 6) & 3];
357 video->renderer->writePalette(video->renderer, 0, video->palette[0]);
358 video->renderer->writePalette(video->renderer, 1, video->palette[1]);
359 video->renderer->writePalette(video->renderer, 2, video->palette[2]);
360 video->renderer->writePalette(video->renderer, 3, video->palette[3]);
361 break;
362 case REG_OBP0:
363 video->palette[8 * 4 + 0] = dmgPalette[value & 3];
364 video->palette[8 * 4 + 1] = dmgPalette[(value >> 2) & 3];
365 video->palette[8 * 4 + 2] = dmgPalette[(value >> 4) & 3];
366 video->palette[8 * 4 + 3] = dmgPalette[(value >> 6) & 3];
367 video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
368 video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
369 video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
370 video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
371 break;
372 case REG_OBP1:
373 video->palette[9 * 4 + 0] = dmgPalette[value & 3];
374 video->palette[9 * 4 + 1] = dmgPalette[(value >> 2) & 3];
375 video->palette[9 * 4 + 2] = dmgPalette[(value >> 4) & 3];
376 video->palette[9 * 4 + 3] = dmgPalette[(value >> 6) & 3];
377 video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
378 video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
379 video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
380 video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
381 break;
382 }
383 } else {
384 switch (address) {
385 case REG_BCPD:
386 if (video->bcpIndex & 1) {
387 video->palette[video->bcpIndex >> 1] &= 0x00FF;
388 video->palette[video->bcpIndex >> 1] |= value << 8;
389 } else {
390 video->palette[video->bcpIndex >> 1] &= 0xFF00;
391 video->palette[video->bcpIndex >> 1] |= value;
392 }
393 video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
394 if (video->bcpIncrement) {
395 ++video->bcpIndex;
396 video->bcpIndex &= 0x3F;
397 video->p->memory.io[REG_BCPS] &= 0x80;
398 video->p->memory.io[REG_BCPS] |= video->bcpIndex;
399 }
400 video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
401 break;
402 case REG_OCPD:
403 if (video->ocpIndex & 1) {
404 video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
405 video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
406 } else {
407 video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
408 video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
409 }
410 video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
411 if (video->ocpIncrement) {
412 ++video->ocpIndex;
413 video->ocpIndex &= 0x3F;
414 video->p->memory.io[REG_OCPS] &= 0x80;
415 video->p->memory.io[REG_OCPS] |= video->ocpIndex;
416 }
417 video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
418 break;
419 }
420 }
421}
422
423void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
424 value &= 1;
425 video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
426 video->vramCurrentBank = value;
427}
428
429static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
430 UNUSED(renderer);
431 UNUSED(model);
432 // Nothing to do
433}
434
435static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
436 UNUSED(renderer);
437 // Nothing to do
438}
439
440static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
441 UNUSED(renderer);
442 UNUSED(address);
443 return value;
444}
445
446static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
447 if (renderer->cache) {
448 mTileCacheWriteVRAM(renderer->cache, address);
449 }
450}
451
452static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
453 UNUSED(value);
454 if (renderer->cache) {
455 mTileCacheWritePalette(renderer->cache, index << 1);
456 }
457}
458
459static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
460 UNUSED(renderer);
461 UNUSED(endX);
462 UNUSED(startX);
463 UNUSED(y);
464 UNUSED(obj);
465 UNUSED(oamMax);
466 // Nothing to do
467}
468
469static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
470 UNUSED(renderer);
471 UNUSED(y);
472 // Nothing to do
473}
474
475static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
476 UNUSED(renderer);
477 // Nothing to do
478}
479
480static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
481 UNUSED(renderer);
482 UNUSED(stride);
483 UNUSED(pixels);
484 // Nothing to do
485}
486
487static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
488 UNUSED(renderer);
489 UNUSED(stride);
490 UNUSED(pixels);
491 // Nothing to do
492}
493
494void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
495 STORE_16LE(video->x, 0, &state->video.x);
496 STORE_16LE(video->ly, 0, &state->video.ly);
497 STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
498 state->video.vramCurrentBank = video->vramCurrentBank;
499
500 GBSerializedVideoFlags flags = 0;
501 flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
502 flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
503 flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
504 flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
505 flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
506 state->video.flags = flags;
507 STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
508 STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
509
510 size_t i;
511 for (i = 0; i < 64; ++i) {
512 STORE_16LE(video->palette[i], i * 2, state->video.palette);
513 }
514
515 STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
516 STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
517
518 memcpy(state->vram, video->vram, GB_SIZE_VRAM);
519 memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
520}
521
522void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
523 LOAD_16LE(video->x, 0, &state->video.x);
524 LOAD_16LE(video->ly, 0, &state->video.ly);
525 LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
526 video->vramCurrentBank = state->video.vramCurrentBank;
527
528 GBSerializedVideoFlags flags = state->video.flags;
529 video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
530 video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
531 video->mode = GBSerializedVideoFlagsGetMode(flags);
532 LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
533 video->bcpIndex &= 0x3F;
534 LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
535 video->ocpIndex &= 0x3F;
536
537 switch (video->mode) {
538 case 0:
539 video->modeEvent.callback = _endMode0;
540 break;
541 case 1:
542 video->modeEvent.callback = _endMode1;
543 break;
544 case 2:
545 video->modeEvent.callback = _endMode2;
546 break;
547 case 3:
548 video->modeEvent.callback = _endMode3;
549 break;
550 }
551
552 uint32_t when;
553 mTimingDeschedule(&video->p->timing, &video->modeEvent);
554 if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
555 LOAD_32LE(when, 0, &state->video.nextMode);
556 mTimingSchedule(&video->p->timing, &video->modeEvent, when);
557 }
558 mTimingDeschedule(&video->p->timing, &video->frameEvent);
559 if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
560 LOAD_32LE(when, 0, &state->video.nextFrame);
561 mTimingSchedule(&video->p->timing, &video->frameEvent, when);
562 }
563
564 size_t i;
565 for (i = 0; i < 64; ++i) {
566 LOAD_16LE(video->palette[i], i * 2, state->video.palette);
567 video->renderer->writePalette(video->renderer, i, video->palette[i]);
568 }
569
570 memcpy(video->vram, state->vram, GB_SIZE_VRAM);
571 memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
572
573 _cleanOAM(video, video->ly);
574 GBVideoSwitchBank(video, video->vramCurrentBank);
575}