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