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