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