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