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