src/gba/video.c (view raw)
1/* Copyright (c) 2013-2015 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 "gba/context/sync.h"
9#include "gba/gba.h"
10#include "gba/io.h"
11#include "gba/rr/rr.h"
12#include "gba/serialize.h"
13
14#include "util/memory.h"
15
16static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer);
17static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer);
18static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer);
19static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
20static void GBAVideoDummyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address);
21static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
22static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
23static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
24static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer);
25static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, const void** pixels);
26
27const int GBAVideoObjSizes[16][2] = {
28 { 8, 8 },
29 { 16, 16 },
30 { 32, 32 },
31 { 64, 64 },
32 { 16, 8 },
33 { 32, 8 },
34 { 32, 16 },
35 { 64, 32 },
36 { 8, 16 },
37 { 8, 32 },
38 { 16, 32 },
39 { 32, 64 },
40 { 0, 0 },
41 { 0, 0 },
42 { 0, 0 },
43 { 0, 0 },
44};
45
46static struct GBAVideoRenderer dummyRenderer = {
47 .init = GBAVideoDummyRendererInit,
48 .reset = GBAVideoDummyRendererReset,
49 .deinit = GBAVideoDummyRendererDeinit,
50 .writeVideoRegister = GBAVideoDummyRendererWriteVideoRegister,
51 .writeVRAM = GBAVideoDummyRendererWriteVRAM,
52 .writePalette = GBAVideoDummyRendererWritePalette,
53 .writeOAM = GBAVideoDummyRendererWriteOAM,
54 .drawScanline = GBAVideoDummyRendererDrawScanline,
55 .finishFrame = GBAVideoDummyRendererFinishFrame,
56 .getPixels = GBAVideoDummyRendererGetPixels
57};
58
59void GBAVideoInit(struct GBAVideo* video) {
60 video->renderer = &dummyRenderer;
61 video->vram = 0;
62 video->frameskip = 0;
63}
64
65void GBAVideoReset(struct GBAVideo* video) {
66 if (video->p->memory.fullBios) {
67 video->vcount = 0;
68 } else {
69 // TODO: Verify exact scanline hardware
70 video->vcount = 0x7E;
71 }
72 video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
73
74 video->nextHblank = VIDEO_HDRAW_LENGTH;
75 video->nextEvent = video->nextHblank;
76 video->eventDiff = 0;
77
78 video->nextHblankIRQ = 0;
79 video->nextVblankIRQ = 0;
80 video->nextVcounterIRQ = 0;
81
82 video->frameCounter = 0;
83 video->frameskipCounter = 0;
84
85 if (video->vram) {
86 mappedMemoryFree(video->vram, SIZE_VRAM);
87 }
88 video->vram = anonymousMemoryMap(SIZE_VRAM);
89 video->renderer->vram = video->vram;
90
91 memset(video->palette, 0, sizeof(video->palette));
92 memset(video->oam.raw, 0, sizeof(video->oam.raw));
93
94 video->renderer->deinit(video->renderer);
95 video->renderer->init(video->renderer);
96}
97
98void GBAVideoDeinit(struct GBAVideo* video) {
99 GBAVideoAssociateRenderer(video, &dummyRenderer);
100 mappedMemoryFree(video->vram, SIZE_VRAM);
101}
102
103void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
104 video->renderer->deinit(video->renderer);
105 video->renderer = renderer;
106 renderer->palette = video->palette;
107 renderer->vram = video->vram;
108 renderer->oam = &video->oam;
109 video->renderer->init(video->renderer);
110}
111
112int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
113 video->nextEvent -= cycles;
114 video->eventDiff += cycles;
115 if (video->nextEvent <= 0) {
116 int32_t lastEvent = video->nextEvent;
117 video->nextHblank -= video->eventDiff;
118 video->nextHblankIRQ -= video->eventDiff;
119 video->nextVcounterIRQ -= video->eventDiff;
120 video->eventDiff = 0;
121 uint16_t dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
122
123 if (GBARegisterDISPSTATIsInHblank(dispstat)) {
124 // End Hblank
125 dispstat = GBARegisterDISPSTATClearInHblank(dispstat);
126 video->nextEvent = video->nextHblank;
127
128 ++video->vcount;
129 if (video->vcount == VIDEO_VERTICAL_TOTAL_PIXELS) {
130 video->vcount = 0;
131 }
132 video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
133
134 if (video->vcount == GBARegisterDISPSTATGetVcountSetting(dispstat)) {
135 dispstat = GBARegisterDISPSTATFillVcounter(dispstat);
136 if (GBARegisterDISPSTATIsVcounterIRQ(dispstat)) {
137 GBARaiseIRQ(video->p, IRQ_VCOUNTER);
138 video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
139 }
140 } else {
141 dispstat = GBARegisterDISPSTATClearVcounter(dispstat);
142 }
143 video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
144
145 // Note: state may be recorded during callbacks, so ensure it is consistent!
146 switch (video->vcount) {
147 case 0:
148 GBAFrameStarted(video->p);
149 break;
150 case VIDEO_VERTICAL_PIXELS:
151 video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATFillInVblank(dispstat);
152 if (video->frameskipCounter <= 0) {
153 video->renderer->finishFrame(video->renderer);
154 }
155 video->nextVblankIRQ = video->nextEvent + VIDEO_TOTAL_LENGTH;
156 GBAMemoryRunVblankDMAs(video->p, lastEvent);
157 if (GBARegisterDISPSTATIsVblankIRQ(dispstat)) {
158 GBARaiseIRQ(video->p, IRQ_VBLANK);
159 }
160 GBAFrameEnded(video->p);
161 --video->frameskipCounter;
162 if (video->frameskipCounter < 0) {
163 GBASyncPostFrame(video->p->sync);
164 video->frameskipCounter = video->frameskip;
165 }
166 ++video->frameCounter;
167 break;
168 case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
169 video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATClearInVblank(dispstat);
170 break;
171 }
172 } else {
173 // Begin Hblank
174 dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
175 video->nextEvent = video->nextHblank + VIDEO_HBLANK_LENGTH;
176 video->nextHblank = video->nextEvent + VIDEO_HDRAW_LENGTH;
177 video->nextHblankIRQ = video->nextHblank;
178
179 if (video->vcount < VIDEO_VERTICAL_PIXELS && video->frameskipCounter <= 0) {
180 video->renderer->drawScanline(video->renderer, video->vcount);
181 }
182
183 if (video->vcount < VIDEO_VERTICAL_PIXELS) {
184 GBAMemoryRunHblankDMAs(video->p, lastEvent);
185 }
186 if (GBARegisterDISPSTATIsHblankIRQ(dispstat)) {
187 GBARaiseIRQ(video->p, IRQ_HBLANK);
188 }
189 video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
190 }
191 }
192 return video->nextEvent;
193}
194
195void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
196 video->p->memory.io[REG_DISPSTAT >> 1] &= 0x7;
197 video->p->memory.io[REG_DISPSTAT >> 1] |= value;
198
199 uint16_t dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
200
201 if (GBARegisterDISPSTATIsVcounterIRQ(dispstat)) {
202 // FIXME: this can be too late if we're in the middle of an Hblank
203 video->nextVcounterIRQ = video->nextHblank + VIDEO_HBLANK_LENGTH + (GBARegisterDISPSTATGetVcountSetting(dispstat) - video->vcount) * VIDEO_HORIZONTAL_LENGTH;
204 if (video->nextVcounterIRQ < video->nextEvent) {
205 video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
206 }
207 }
208}
209
210static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
211 UNUSED(renderer);
212 // Nothing to do
213}
214
215static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer) {
216 UNUSED(renderer);
217 // Nothing to do
218}
219
220static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
221 UNUSED(renderer);
222 // Nothing to do
223}
224
225static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
226 UNUSED(renderer);
227 UNUSED(address);
228 return value;
229}
230
231static void GBAVideoDummyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
232 UNUSED(renderer);
233 UNUSED(address);
234 // Nothing to do
235}
236
237static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
238 UNUSED(renderer);
239 UNUSED(address);
240 UNUSED(value);
241 // Nothing to do
242}
243
244static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
245 UNUSED(renderer);
246 UNUSED(oam);
247 // Nothing to do
248}
249
250static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
251 UNUSED(renderer);
252 UNUSED(y);
253 // Nothing to do
254}
255
256static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
257 UNUSED(renderer);
258 // Nothing to do
259}
260
261static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, const void** pixels) {
262 UNUSED(renderer);
263 UNUSED(stride);
264 UNUSED(pixels);
265 // Nothing to do
266}
267
268void GBAVideoSerialize(const struct GBAVideo* video, struct GBASerializedState* state) {
269 memcpy(state->vram, video->renderer->vram, SIZE_VRAM);
270 memcpy(state->oam, video->oam.raw, SIZE_OAM);
271 memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
272 STORE_32(video->nextEvent, 0, &state->video.nextEvent);
273 STORE_32(video->eventDiff, 0, &state->video.eventDiff);
274 STORE_32(video->nextHblank, 0, &state->video.nextHblank);
275 STORE_32(video->nextHblankIRQ, 0, &state->video.nextHblankIRQ);
276 STORE_32(video->nextVblankIRQ, 0, &state->video.nextVblankIRQ);
277 STORE_32(video->nextVcounterIRQ, 0, &state->video.nextVcounterIRQ);
278 STORE_32(video->frameCounter, 0, &state->video.frameCounter);
279}
280
281void GBAVideoDeserialize(struct GBAVideo* video, const struct GBASerializedState* state) {
282 memcpy(video->renderer->vram, state->vram, SIZE_VRAM);
283 uint16_t value;
284 int i;
285 for (i = 0; i < SIZE_OAM; i += 2) {
286 LOAD_16(value, i, state->oam);
287 GBAStore16(video->p->cpu, BASE_OAM | i, value, 0);
288 }
289 for (i = 0; i < SIZE_PALETTE_RAM; i += 2) {
290 LOAD_16(value, i, state->pram);
291 GBAStore16(video->p->cpu, BASE_PALETTE_RAM | i, value, 0);
292 }
293 LOAD_32(video->nextEvent, 0, &state->video.nextEvent);
294 LOAD_32(video->eventDiff, 0, &state->video.eventDiff);
295 LOAD_32(video->nextHblank, 0, &state->video.nextHblank);
296 LOAD_32(video->nextHblankIRQ, 0, &state->video.nextHblankIRQ);
297 LOAD_32(video->nextVblankIRQ, 0, &state->video.nextVblankIRQ);
298 LOAD_32(video->nextVcounterIRQ, 0, &state->video.nextVcounterIRQ);
299 LOAD_32(video->frameCounter, 0, &state->video.frameCounter);
300 LOAD_16(video->vcount, REG_VCOUNT, state->io);
301}