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/thread.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 GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
21static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
22static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
23static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer);
24static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, void** pixels);
25
26static struct GBAVideoRenderer dummyRenderer = {
27 .init = GBAVideoDummyRendererInit,
28 .reset = GBAVideoDummyRendererReset,
29 .deinit = GBAVideoDummyRendererDeinit,
30 .writeVideoRegister = GBAVideoDummyRendererWriteVideoRegister,
31 .writePalette = GBAVideoDummyRendererWritePalette,
32 .writeOAM = GBAVideoDummyRendererWriteOAM,
33 .drawScanline = GBAVideoDummyRendererDrawScanline,
34 .finishFrame = GBAVideoDummyRendererFinishFrame,
35 .getPixels = GBAVideoDummyRendererGetPixels
36};
37
38void GBAVideoInit(struct GBAVideo* video) {
39 video->renderer = &dummyRenderer;
40 video->vram = 0;
41}
42
43void GBAVideoReset(struct GBAVideo* video) {
44 video->vcount = VIDEO_VERTICAL_TOTAL_PIXELS - 1;
45
46 video->lastHblank = 0;
47 video->nextHblank = VIDEO_HDRAW_LENGTH;
48 video->nextEvent = video->nextHblank;
49 video->eventDiff = video->nextEvent;
50
51 video->nextHblankIRQ = 0;
52 video->nextVblankIRQ = 0;
53 video->nextVcounterIRQ = 0;
54
55 video->frameCounter = 0;
56
57 if (video->vram) {
58 mappedMemoryFree(video->vram, SIZE_VRAM);
59 }
60 video->vram = anonymousMemoryMap(SIZE_VRAM);
61 video->renderer->vram = video->vram;
62
63 int i;
64 for (i = 0; i < 128; ++i) {
65 video->oam.raw[i * 4] = 0x0200;
66 video->oam.raw[i * 4 + 1] = 0x0000;
67 video->oam.raw[i * 4 + 2] = 0x0000;
68 video->oam.raw[i * 4 + 3] = 0x0000;
69 }
70
71 video->renderer->deinit(video->renderer);
72 video->renderer->init(video->renderer);
73}
74
75void GBAVideoDeinit(struct GBAVideo* video) {
76 GBAVideoAssociateRenderer(video, &dummyRenderer);
77 mappedMemoryFree(video->vram, SIZE_VRAM);
78}
79
80void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
81 video->renderer->deinit(video->renderer);
82 video->renderer = renderer;
83 renderer->palette = video->palette;
84 renderer->vram = video->vram;
85 renderer->oam = &video->oam;
86 video->renderer->init(video->renderer);
87}
88
89int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
90 video->nextEvent -= cycles;
91 video->eventDiff += cycles;
92 if (video->nextEvent <= 0) {
93 int32_t lastEvent = video->nextEvent;
94 video->lastHblank -= video->eventDiff;
95 video->nextHblank -= video->eventDiff;
96 video->nextHblankIRQ -= video->eventDiff;
97 video->nextVcounterIRQ -= video->eventDiff;
98 video->eventDiff = 0;
99 uint16_t dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
100
101 if (GBARegisterDISPSTATIsInHblank(dispstat)) {
102 // End Hblank
103 dispstat = GBARegisterDISPSTATClearInHblank(dispstat);
104 video->nextEvent = video->nextHblank;
105
106 ++video->vcount;
107 if (video->vcount == VIDEO_VERTICAL_TOTAL_PIXELS) {
108 video->vcount = 0;
109 }
110 video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
111
112 if (video->vcount == GBARegisterDISPSTATGetVcountSetting(dispstat)) {
113 dispstat = GBARegisterDISPSTATFillVcounter(dispstat);
114 if (GBARegisterDISPSTATIsVcounterIRQ(dispstat)) {
115 GBARaiseIRQ(video->p, IRQ_VCOUNTER);
116 video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
117 }
118 } else {
119 dispstat = GBARegisterDISPSTATClearVcounter(dispstat);
120 }
121 video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
122
123 // Note: state may be recorded during callbacks, so ensure it is consistent!
124 switch (video->vcount) {
125 case 0:
126 GBAFrameStarted(video->p);
127 break;
128 case VIDEO_VERTICAL_PIXELS:
129 video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATFillInVblank(dispstat);
130 if (GBASyncDrawingFrame(video->p->sync)) {
131 video->renderer->finishFrame(video->renderer);
132 }
133 video->nextVblankIRQ = video->nextEvent + VIDEO_TOTAL_LENGTH;
134 GBAMemoryRunVblankDMAs(video->p, lastEvent);
135 if (GBARegisterDISPSTATIsVblankIRQ(dispstat)) {
136 GBARaiseIRQ(video->p, IRQ_VBLANK);
137 }
138 GBAFrameEnded(video->p);
139 GBASyncPostFrame(video->p->sync);
140 ++video->frameCounter;
141 break;
142 case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
143 video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATClearInVblank(dispstat);
144 break;
145 }
146 } else {
147 // Begin Hblank
148 dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
149 video->lastHblank = video->nextHblank;
150 video->nextEvent = video->lastHblank + VIDEO_HBLANK_LENGTH;
151 video->nextHblank = video->nextEvent + VIDEO_HDRAW_LENGTH;
152 video->nextHblankIRQ = video->nextHblank;
153
154 if (video->vcount < VIDEO_VERTICAL_PIXELS && GBASyncDrawingFrame(video->p->sync)) {
155 video->renderer->drawScanline(video->renderer, video->vcount);
156 }
157
158 if (video->vcount < VIDEO_VERTICAL_PIXELS) {
159 GBAMemoryRunHblankDMAs(video->p, lastEvent);
160 }
161 if (GBARegisterDISPSTATIsHblankIRQ(dispstat)) {
162 GBARaiseIRQ(video->p, IRQ_HBLANK);
163 }
164 video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
165 }
166 }
167 return video->nextEvent;
168}
169
170void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
171 video->p->memory.io[REG_DISPSTAT >> 1] &= 0x7;
172 video->p->memory.io[REG_DISPSTAT >> 1] |= value;
173
174 uint16_t dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
175
176 if (GBARegisterDISPSTATIsVcounterIRQ(dispstat)) {
177 // FIXME: this can be too late if we're in the middle of an Hblank
178 video->nextVcounterIRQ = video->nextHblank + VIDEO_HBLANK_LENGTH + (GBARegisterDISPSTATGetVcountSetting(dispstat) - video->vcount) * VIDEO_HORIZONTAL_LENGTH;
179 if (video->nextVcounterIRQ < video->nextEvent) {
180 video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
181 }
182 }
183}
184
185static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
186 UNUSED(renderer);
187 // Nothing to do
188}
189
190static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer) {
191 UNUSED(renderer);
192 // Nothing to do
193}
194
195static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
196 UNUSED(renderer);
197 // Nothing to do
198}
199
200static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
201 UNUSED(renderer);
202 UNUSED(address);
203 return value;
204}
205
206static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
207 UNUSED(renderer);
208 UNUSED(address);
209 UNUSED(value);
210 // Nothing to do
211}
212
213static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
214 UNUSED(renderer);
215 UNUSED(oam);
216 // Nothing to do
217}
218
219static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
220 UNUSED(renderer);
221 UNUSED(y);
222 // Nothing to do
223}
224
225static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
226 UNUSED(renderer);
227 // Nothing to do
228}
229
230static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, void** pixels) {
231 UNUSED(renderer);
232 UNUSED(stride);
233 UNUSED(pixels);
234 // Nothing to do
235}
236
237
238void GBAVideoSerialize(struct GBAVideo* video, struct GBASerializedState* state) {
239 memcpy(state->vram, video->renderer->vram, SIZE_VRAM);
240 memcpy(state->oam, video->oam.raw, SIZE_OAM);
241 memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
242 state->video.nextEvent = video->nextEvent;
243 state->video.eventDiff = video->eventDiff;
244 state->video.lastHblank = video->lastHblank;
245 state->video.nextHblank = video->nextHblank;
246 state->video.nextHblankIRQ = video->nextHblankIRQ;
247 state->video.nextVblankIRQ = video->nextVblankIRQ;
248 state->video.nextVcounterIRQ = video->nextVcounterIRQ;
249 state->video.frameCounter = video->frameCounter;
250}
251
252void GBAVideoDeserialize(struct GBAVideo* video, struct GBASerializedState* state) {
253 memcpy(video->renderer->vram, state->vram, SIZE_VRAM);
254 int i;
255 for (i = 0; i < SIZE_OAM; i += 2) {
256 GBAStore16(video->p->cpu, BASE_OAM | i, state->oam[i >> 1], 0);
257 }
258 for (i = 0; i < SIZE_PALETTE_RAM; i += 2) {
259 GBAStore16(video->p->cpu, BASE_PALETTE_RAM | i, state->pram[i >> 1], 0);
260 }
261 video->nextEvent = state->video.nextEvent;
262 video->eventDiff = state->video.eventDiff;
263 video->lastHblank = state->video.lastHblank;
264 video->nextHblank = state->video.nextHblank;
265 video->nextHblankIRQ = state->video.nextHblankIRQ;
266 video->nextVblankIRQ = state->video.nextVblankIRQ;
267 video->nextVcounterIRQ = state->video.nextVcounterIRQ;
268 video->frameCounter = state->video.frameCounter;
269 video->vcount = state->io[REG_VCOUNT >> 1];
270}