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