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 <mgba/internal/gba/video.h>
7
8#include <mgba/core/sync.h>
9#include <mgba/core/cache-set.h>
10#include <mgba/internal/arm/macros.h>
11#include <mgba/internal/gba/dma.h>
12#include <mgba/internal/gba/gba.h>
13#include <mgba/internal/gba/io.h>
14#include <mgba/internal/gba/renderers/cache-set.h>
15#include <mgba/internal/gba/serialize.h>
16
17#include <mgba-util/memory.h>
18
19mLOG_DEFINE_CATEGORY(GBA_VIDEO, "GBA Video", "gba.video");
20
21static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer);
22static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer);
23static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer);
24static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
25static void GBAVideoDummyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address);
26static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
27static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
28static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
29static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer);
30static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels);
31static void GBAVideoDummyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels);
32
33static void _startHblank(struct mTiming*, void* context, uint32_t cyclesLate);
34static void _midHblank(struct mTiming*, void* context, uint32_t cyclesLate);
35static void _startHdraw(struct mTiming*, void* context, uint32_t cyclesLate);
36
37MGBA_EXPORT const int GBAVideoObjSizes[16][2] = {
38 { 8, 8 },
39 { 16, 16 },
40 { 32, 32 },
41 { 64, 64 },
42 { 16, 8 },
43 { 32, 8 },
44 { 32, 16 },
45 { 64, 32 },
46 { 8, 16 },
47 { 8, 32 },
48 { 16, 32 },
49 { 32, 64 },
50 { 0, 0 },
51 { 0, 0 },
52 { 0, 0 },
53 { 0, 0 },
54};
55
56static struct GBAVideoRenderer dummyRenderer = {
57 .init = GBAVideoDummyRendererInit,
58 .reset = GBAVideoDummyRendererReset,
59 .deinit = GBAVideoDummyRendererDeinit,
60 .writeVideoRegister = GBAVideoDummyRendererWriteVideoRegister,
61 .writeVRAM = GBAVideoDummyRendererWriteVRAM,
62 .writePalette = GBAVideoDummyRendererWritePalette,
63 .writeOAM = GBAVideoDummyRendererWriteOAM,
64 .drawScanline = GBAVideoDummyRendererDrawScanline,
65 .finishFrame = GBAVideoDummyRendererFinishFrame,
66 .getPixels = GBAVideoDummyRendererGetPixels,
67 .putPixels = GBAVideoDummyRendererPutPixels,
68};
69
70void GBAVideoInit(struct GBAVideo* video) {
71 video->renderer = &dummyRenderer;
72 video->renderer->cache = NULL;
73 video->vram = anonymousMemoryMap(SIZE_VRAM);
74 video->frameskip = 0;
75 video->event.name = "GBA Video";
76 video->event.callback = NULL;
77 video->event.context = video;
78 video->event.priority = 8;
79}
80
81void GBAVideoReset(struct GBAVideo* video) {
82 int32_t nextEvent = VIDEO_HDRAW_LENGTH;
83 if (video->p->memory.fullBios) {
84 video->vcount = 0;
85 } else {
86 // TODO: Verify exact scanline on hardware
87 video->vcount = 0x7E;
88 nextEvent = 170;
89 }
90 video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
91
92 video->event.callback = _startHblank;
93 mTimingSchedule(&video->p->timing, &video->event, nextEvent);
94
95 video->frameCounter = 0;
96 video->frameskipCounter = 0;
97 video->renderer->vram = video->vram;
98 video->shouldStall = 0;
99
100 memset(video->palette, 0, sizeof(video->palette));
101 memset(video->oam.raw, 0, sizeof(video->oam.raw));
102
103 video->renderer->reset(video->renderer);
104}
105
106void GBAVideoDeinit(struct GBAVideo* video) {
107 video->renderer->deinit(video->renderer);
108 mappedMemoryFree(video->vram, SIZE_VRAM);
109}
110
111void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
112 video->renderer->deinit(video->renderer);
113 renderer->cache = video->renderer->cache;
114 video->renderer = renderer;
115 renderer->palette = video->palette;
116 renderer->vram = video->vram;
117 renderer->oam = &video->oam;
118 video->renderer->init(video->renderer);
119}
120
121void _midHblank(struct mTiming* timing, void* context, uint32_t cyclesLate) {
122 struct GBAVideo* video = context;
123 GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
124 dispstat = GBARegisterDISPSTATClearInHblank(dispstat);
125 video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
126 video->event.callback = _startHdraw;
127 mTimingSchedule(timing, &video->event, VIDEO_HBLANK_FLIP - cyclesLate);
128}
129
130void _startHdraw(struct mTiming* timing, void* context, uint32_t cyclesLate) {
131 struct GBAVideo* video = context;
132 video->event.callback = _startHblank;
133 mTimingSchedule(timing, &video->event, VIDEO_HDRAW_LENGTH - cyclesLate);
134
135 ++video->vcount;
136 if (video->vcount == VIDEO_VERTICAL_TOTAL_PIXELS) {
137 video->vcount = 0;
138 }
139 video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
140
141 if (video->vcount < GBA_VIDEO_VERTICAL_PIXELS && video->frameskipCounter <= 0) {
142 video->renderer->drawScanline(video->renderer, video->vcount);
143 video->shouldStall = 1;
144 }
145
146 GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
147 if (video->vcount == GBARegisterDISPSTATGetVcountSetting(dispstat)) {
148 dispstat = GBARegisterDISPSTATFillVcounter(dispstat);
149 if (GBARegisterDISPSTATIsVcounterIRQ(dispstat)) {
150 GBARaiseIRQ(video->p, IRQ_VCOUNTER, cyclesLate);
151 }
152 } else {
153 dispstat = GBARegisterDISPSTATClearVcounter(dispstat);
154 }
155 video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
156
157 // Note: state may be recorded during callbacks, so ensure it is consistent!
158 switch (video->vcount) {
159 case 0:
160 GBAFrameStarted(video->p);
161 break;
162 case GBA_VIDEO_VERTICAL_PIXELS:
163 video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATFillInVblank(dispstat);
164 if (video->frameskipCounter <= 0) {
165 video->renderer->finishFrame(video->renderer);
166 }
167 GBADMARunVblank(video->p, -cyclesLate);
168 if (GBARegisterDISPSTATIsVblankIRQ(dispstat)) {
169 GBARaiseIRQ(video->p, IRQ_VBLANK, cyclesLate);
170 }
171 GBAFrameEnded(video->p);
172 mCoreSyncPostFrame(video->p->sync);
173 --video->frameskipCounter;
174 if (video->frameskipCounter < 0) {
175 video->frameskipCounter = video->frameskip;
176 }
177 ++video->frameCounter;
178 video->p->earlyExit = true;
179 break;
180 case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
181 video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATClearInVblank(dispstat);
182 break;
183 }
184}
185
186void _startHblank(struct mTiming* timing, void* context, uint32_t cyclesLate) {
187 struct GBAVideo* video = context;
188 video->event.callback = _midHblank;
189 mTimingSchedule(timing, &video->event, VIDEO_HBLANK_LENGTH - VIDEO_HBLANK_FLIP - cyclesLate);
190
191 // Begin Hblank
192 GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
193 dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
194
195 if (video->vcount < GBA_VIDEO_VERTICAL_PIXELS) {
196 GBADMARunHblank(video->p, -cyclesLate);
197 }
198 if (video->vcount >= 2 && video->vcount < GBA_VIDEO_VERTICAL_PIXELS + 2) {
199 GBADMARunDisplayStart(video->p, -cyclesLate);
200 }
201 if (GBARegisterDISPSTATIsHblankIRQ(dispstat)) {
202 GBARaiseIRQ(video->p, IRQ_HBLANK, cyclesLate);
203 }
204 video->shouldStall = 0;
205 video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
206}
207
208void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
209 video->p->memory.io[REG_DISPSTAT >> 1] &= 0x7;
210 video->p->memory.io[REG_DISPSTAT >> 1] |= value;
211 // TODO: Does a VCounter IRQ trigger on write?
212}
213
214static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
215 UNUSED(renderer);
216 // Nothing to do
217}
218
219static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer) {
220 UNUSED(renderer);
221 // Nothing to do
222}
223
224static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
225 UNUSED(renderer);
226 // Nothing to do
227}
228
229static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
230 if (renderer->cache) {
231 GBAVideoCacheWriteVideoRegister(renderer->cache, address, value);
232 }
233 switch (address) {
234 case REG_DISPCNT:
235 value &= 0xFFF7;
236 break;
237 case REG_BG0CNT:
238 case REG_BG1CNT:
239 value &= 0xDFFF;
240 break;
241 case REG_BG2CNT:
242 case REG_BG3CNT:
243 value &= 0xFFFF;
244 break;
245 case REG_BG0HOFS:
246 case REG_BG0VOFS:
247 case REG_BG1HOFS:
248 case REG_BG1VOFS:
249 case REG_BG2HOFS:
250 case REG_BG2VOFS:
251 case REG_BG3HOFS:
252 case REG_BG3VOFS:
253 value &= 0x01FF;
254 break;
255 case REG_BLDCNT:
256 value &= 0x3FFF;
257 break;
258 case REG_BLDALPHA:
259 value &= 0x1F1F;
260 break;
261 case REG_WININ:
262 case REG_WINOUT:
263 value &= 0x3F3F;
264 break;
265 default:
266 break;
267 }
268 return value;
269}
270
271static void GBAVideoDummyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
272 if (renderer->cache) {
273 mCacheSetWriteVRAM(renderer->cache, address);
274 }
275}
276
277static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
278 if (renderer->cache) {
279 mCacheSetWritePalette(renderer->cache, address >> 1, mColorFrom555(value));
280 }
281}
282
283static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
284 UNUSED(renderer);
285 UNUSED(oam);
286 // Nothing to do
287}
288
289static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
290 UNUSED(renderer);
291 UNUSED(y);
292 // Nothing to do
293}
294
295static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
296 UNUSED(renderer);
297 // Nothing to do
298}
299
300static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
301 UNUSED(renderer);
302 UNUSED(stride);
303 UNUSED(pixels);
304 // Nothing to do
305}
306
307static void GBAVideoDummyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
308 UNUSED(renderer);
309 UNUSED(stride);
310 UNUSED(pixels);
311 // Nothing to do
312}
313
314void GBAVideoSerialize(const struct GBAVideo* video, struct GBASerializedState* state) {
315 memcpy(state->vram, video->vram, SIZE_VRAM);
316 memcpy(state->oam, video->oam.raw, SIZE_OAM);
317 memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
318 STORE_32(video->event.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextEvent);
319 int32_t flags = 0;
320 if (video->event.callback == _startHdraw) {
321 flags = GBASerializedVideoFlagsSetMode(flags, 1);
322 } else if (video->event.callback == _startHblank) {
323 flags = GBASerializedVideoFlagsSetMode(flags, 2);
324 } else if (video->event.callback == _midHblank) {
325 flags = GBASerializedVideoFlagsSetMode(flags, 3);
326 }
327 STORE_32(flags, 0, &state->video.flags);
328 STORE_32(video->frameCounter, 0, &state->video.frameCounter);
329}
330
331void GBAVideoDeserialize(struct GBAVideo* video, const struct GBASerializedState* state) {
332 memcpy(video->vram, state->vram, SIZE_VRAM);
333 uint16_t value;
334 int i;
335 for (i = 0; i < SIZE_OAM; i += 2) {
336 LOAD_16(value, i, state->oam);
337 GBAStore16(video->p->cpu, BASE_OAM | i, value, 0);
338 }
339 for (i = 0; i < SIZE_PALETTE_RAM; i += 2) {
340 LOAD_16(value, i, state->pram);
341 GBAStore16(video->p->cpu, BASE_PALETTE_RAM | i, value, 0);
342 }
343 LOAD_32(video->frameCounter, 0, &state->video.frameCounter);
344
345 video->shouldStall = 0;
346 int32_t flags;
347 LOAD_32(flags, 0, &state->video.flags);
348 GBARegisterDISPSTAT dispstat = state->io[REG_DISPSTAT >> 1];
349 switch (GBASerializedVideoFlagsGetMode(flags)) {
350 case 0:
351 if (GBARegisterDISPSTATIsInHblank(dispstat)) {
352 video->event.callback = _startHdraw;
353 } else {
354 video->event.callback = _startHblank;
355 }
356 break;
357 case 1:
358 video->event.callback = _startHdraw;
359 break;
360 case 2:
361 video->event.callback = _startHblank;
362 video->shouldStall = 1;
363 break;
364 case 3:
365 video->event.callback = _midHblank;
366 break;
367 }
368 uint32_t when;
369 LOAD_32(when, 0, &state->video.nextEvent);
370 mTimingSchedule(&video->p->timing, &video->event, when);
371
372 LOAD_16(video->vcount, REG_VCOUNT, state->io);
373 video->renderer->reset(video->renderer);
374}