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