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