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