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