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