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 "core/sync.h"
9#include "core/tile-cache.h"
10#include "gba/dma.h"
11#include "gba/gba.h"
12#include "gba/io.h"
13#include "gba/renderers/tile-cache.h"
14#include "gba/rr/rr.h"
15#include "gba/serialize.h"
16
17#include "util/memory.h"
18
19mLOG_DEFINE_CATEGORY(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 = 0;
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 if (video->p->memory.fullBios) {
82 video->vcount = 0;
83 } else {
84 // TODO: Verify exact scanline hardware
85 video->vcount = 0x7E;
86 }
87 video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
88
89 video->event.callback = _startHblank;
90 mTimingSchedule(&video->p->timing, &video->event, VIDEO_HDRAW_LENGTH);
91
92 video->frameCounter = 0;
93 video->frameskipCounter = 0;
94
95 if (video->vram) {
96 mappedMemoryFree(video->vram, SIZE_VRAM);
97 }
98 video->vram = anonymousMemoryMap(SIZE_VRAM);
99 video->renderer->vram = video->vram;
100
101 memset(video->palette, 0, sizeof(video->palette));
102 memset(video->oam.raw, 0, sizeof(video->oam.raw));
103
104 video->renderer->deinit(video->renderer);
105 video->renderer->init(video->renderer);
106}
107
108void GBAVideoDeinit(struct GBAVideo* video) {
109 GBAVideoAssociateRenderer(video, &dummyRenderer);
110 mappedMemoryFree(video->vram, SIZE_VRAM);
111}
112
113void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
114 video->renderer->deinit(video->renderer);
115 renderer->cache = video->renderer->cache;
116 video->renderer = renderer;
117 renderer->palette = video->palette;
118 renderer->vram = video->vram;
119 renderer->oam = &video->oam;
120 video->renderer->init(video->renderer);
121}
122
123void _startHdraw(struct mTiming* timing, void* context, uint32_t cyclesLate) {
124 struct GBAVideo* video = context;
125 GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
126 dispstat = GBARegisterDISPSTATClearInHblank(dispstat);
127 video->event.callback = _startHblank;
128 mTimingSchedule(timing, &video->event, VIDEO_HDRAW_LENGTH - cyclesLate);
129
130 ++video->vcount;
131 if (video->vcount == VIDEO_VERTICAL_TOTAL_PIXELS) {
132 video->vcount = 0;
133 }
134 video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
135
136 if (video->vcount == GBARegisterDISPSTATGetVcountSetting(dispstat)) {
137 dispstat = GBARegisterDISPSTATFillVcounter(dispstat);
138 if (GBARegisterDISPSTATIsVcounterIRQ(dispstat)) {
139 GBARaiseIRQ(video->p, IRQ_VCOUNTER);
140 }
141 } else {
142 dispstat = GBARegisterDISPSTATClearVcounter(dispstat);
143 }
144 video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
145
146 // Note: state may be recorded during callbacks, so ensure it is consistent!
147 switch (video->vcount) {
148 case 0:
149 GBAFrameStarted(video->p);
150 break;
151 case VIDEO_VERTICAL_PIXELS:
152 video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATFillInVblank(dispstat);
153 if (video->frameskipCounter <= 0) {
154 video->renderer->finishFrame(video->renderer);
155 }
156 GBADMARunVblank(video->p, -cyclesLate);
157 if (GBARegisterDISPSTATIsVblankIRQ(dispstat)) {
158 GBARaiseIRQ(video->p, IRQ_VBLANK);
159 }
160 GBAFrameEnded(video->p);
161 --video->frameskipCounter;
162 if (video->frameskipCounter < 0) {
163 mCoreSyncPostFrame(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}
173
174void _startHblank(struct mTiming* timing, void* context, uint32_t cyclesLate) {
175 struct GBAVideo* video = context;
176 GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
177 dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
178 video->event.callback = _startHdraw;
179 mTimingSchedule(timing, &video->event, VIDEO_HBLANK_LENGTH - cyclesLate);
180
181 // Begin Hblank
182 dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
183 if (video->vcount < VIDEO_VERTICAL_PIXELS && video->frameskipCounter <= 0) {
184 video->renderer->drawScanline(video->renderer, video->vcount);
185 }
186
187 if (video->vcount < VIDEO_VERTICAL_PIXELS) {
188 GBADMARunHblank(video->p, -cyclesLate);
189 }
190 if (GBARegisterDISPSTATIsHblankIRQ(dispstat)) {
191 GBARaiseIRQ(video->p, IRQ_HBLANK);
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 UNUSED(renderer);
219 switch (address) {
220 case REG_BG0CNT:
221 case REG_BG1CNT:
222 value &= 0xDFFF;
223 break;
224 case REG_BG2CNT:
225 case REG_BG3CNT:
226 value &= 0xFFFF;
227 break;
228 case REG_BG0HOFS:
229 case REG_BG0VOFS:
230 case REG_BG1HOFS:
231 case REG_BG1VOFS:
232 case REG_BG2HOFS:
233 case REG_BG2VOFS:
234 case REG_BG3HOFS:
235 case REG_BG3VOFS:
236 value &= 0x01FF;
237 break;
238 case REG_BLDCNT:
239 value &= 0x3FFF;
240 break;
241 case REG_BLDALPHA:
242 value &= 0x1F1F;
243 break;
244 case REG_WININ:
245 case REG_WINOUT:
246 value &= 0x3F3F;
247 break;
248 default:
249 break;
250 }
251 return value;
252}
253
254static void GBAVideoDummyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
255 if (renderer->cache) {
256 mTileCacheWriteVRAM(renderer->cache, address);
257 }
258}
259
260static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
261 UNUSED(value);
262 if (renderer->cache) {
263 mTileCacheWritePalette(renderer->cache, address);
264 }
265}
266
267static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
268 UNUSED(renderer);
269 UNUSED(oam);
270 // Nothing to do
271}
272
273static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
274 UNUSED(renderer);
275 UNUSED(y);
276 // Nothing to do
277}
278
279static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
280 UNUSED(renderer);
281 // Nothing to do
282}
283
284static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
285 UNUSED(renderer);
286 UNUSED(stride);
287 UNUSED(pixels);
288 // Nothing to do
289}
290
291static void GBAVideoDummyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
292 UNUSED(renderer);
293 UNUSED(stride);
294 UNUSED(pixels);
295 // Nothing to do
296}
297
298void GBAVideoSerialize(const struct GBAVideo* video, struct GBASerializedState* state) {
299 memcpy(state->vram, video->renderer->vram, SIZE_VRAM);
300 memcpy(state->oam, video->oam.raw, SIZE_OAM);
301 memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
302 STORE_32(video->event.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextEvent);
303 STORE_32(video->frameCounter, 0, &state->video.frameCounter);
304}
305
306void GBAVideoDeserialize(struct GBAVideo* video, const struct GBASerializedState* state) {
307 memcpy(video->renderer->vram, state->vram, SIZE_VRAM);
308 uint16_t value;
309 int i;
310 for (i = 0; i < SIZE_OAM; i += 2) {
311 LOAD_16(value, i, state->oam);
312 GBAStore16(video->p->cpu, BASE_OAM | i, value, 0);
313 }
314 for (i = 0; i < SIZE_PALETTE_RAM; i += 2) {
315 LOAD_16(value, i, state->pram);
316 GBAStore16(video->p->cpu, BASE_PALETTE_RAM | i, value, 0);
317 }
318 LOAD_32(video->frameCounter, 0, &state->video.frameCounter);
319
320 uint32_t when;
321 LOAD_32(when, 0, &state->video.nextEvent);
322 GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
323 if (GBARegisterDISPSTATIsInHblank(dispstat)) {
324 video->event.callback = _startHdraw;
325 } else {
326 video->event.callback = _startHblank;
327 }
328 mTimingDeschedule(&video->p->timing, &video->event);
329 mTimingSchedule(&video->p->timing, &video->event, when);
330
331 LOAD_16(video->vcount, REG_VCOUNT, state->io);
332 video->renderer->reset(video->renderer);
333}