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 video->lastHblank -= video->eventDiff;
70 video->nextHblank -= video->eventDiff;
71 video->nextHblankIRQ -= video->eventDiff;
72 video->nextVcounterIRQ -= video->eventDiff;
73
74 if (video->inHblank) {
75 // End Hblank
76 video->inHblank = 0;
77 video->nextEvent = video->nextHblank;
78
79 ++video->vcount;
80 video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
81
82 switch (video->vcount) {
83 case VIDEO_VERTICAL_PIXELS:
84 video->inVblank = 1;
85 if (GBASyncDrawingFrame(video->p->sync)) {
86 video->renderer->finishFrame(video->renderer);
87 }
88 video->nextVblankIRQ = video->nextEvent + VIDEO_TOTAL_LENGTH;
89 GBAMemoryRunVblankDMAs(&video->p->memory);
90 if (video->vblankIRQ) {
91 GBARaiseIRQ(video->p, IRQ_VBLANK);
92 }
93 GBASyncPostFrame(video->p->sync);
94 break;
95 case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
96 video->inVblank = 0;
97 break;
98 case VIDEO_VERTICAL_TOTAL_PIXELS:
99 video->vcount = 0;
100 video->p->memory.io[REG_VCOUNT >> 1] = 0;
101 break;
102 }
103
104 video->vcounter = video->vcount == video->vcountSetting;
105 if (video->vcounter && video->vcounterIRQ) {
106 GBARaiseIRQ(video->p, IRQ_VCOUNTER);
107 video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
108 }
109
110 if (video->vcount < VIDEO_VERTICAL_PIXELS && GBASyncDrawingFrame(video->p->sync)) {
111 video->renderer->drawScanline(video->renderer, video->vcount);
112 }
113 } else {
114 // Begin Hblank
115 video->inHblank = 1;
116 video->lastHblank = video->nextHblank;
117 video->nextEvent = video->lastHblank + VIDEO_HBLANK_LENGTH;
118 video->nextHblank = video->nextEvent + VIDEO_HDRAW_LENGTH;
119 video->nextHblankIRQ = video->nextHblank;
120
121 if (video->vcount < VIDEO_VERTICAL_PIXELS) {
122 GBAMemoryRunHblankDMAs(&video->p->memory);
123 }
124 if (video->hblankIRQ) {
125 GBARaiseIRQ(video->p, IRQ_HBLANK);
126 }
127 }
128
129 video->eventDiff = 0;
130 }
131 return video->nextEvent;
132}
133
134void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
135 union GBARegisterDISPSTAT dispstat;
136 dispstat.packed = value;
137 video->vblankIRQ = dispstat.vblankIRQ;
138 video->hblankIRQ = dispstat.hblankIRQ;
139 video->vcounterIRQ = dispstat.vcounterIRQ;
140 video->vcountSetting = dispstat.vcountSetting;
141
142 if (video->vcounterIRQ) {
143 // FIXME: this can be too late if we're in the middle of an Hblank
144 video->nextVcounterIRQ = video->nextHblank + VIDEO_HBLANK_LENGTH + (video->vcountSetting - video->vcount) * VIDEO_HORIZONTAL_LENGTH;
145 if (video->nextVcounterIRQ < video->nextEvent) {
146 video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
147 }
148 }
149}
150
151uint16_t GBAVideoReadDISPSTAT(struct GBAVideo* video) {
152 return (video->inVblank) | (video->inHblank << 1) | (video->vcounter << 2);
153}
154
155static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
156 (void)(renderer);
157 // Nothing to do
158}
159
160static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
161 (void)(renderer);
162 // Nothing to do
163}
164
165static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
166 (void)(renderer);
167 (void)(address);
168 return value;
169}
170
171static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
172 (void)(renderer);
173 (void)(y);
174 // Nothing to do
175}
176
177static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
178 (void)(renderer);
179 // Nothing to do
180}
181
182void GBAVideoSerialize(struct GBAVideo* video, struct GBASerializedState* state) {
183 memcpy(state->vram, video->renderer->vram, SIZE_VRAM);
184 memcpy(state->oam, video->oam.raw, SIZE_OAM);
185 memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
186 union GBARegisterDISPSTAT dispstat;
187 dispstat.inVblank = video->inVblank;
188 dispstat.inHblank = video->inHblank;
189 dispstat.vcounter = video->vcounter;
190 dispstat.vblankIRQ = video->vblankIRQ;
191 dispstat.hblankIRQ = video->hblankIRQ;
192 dispstat.vcounterIRQ = video->vcounterIRQ;
193 dispstat.vcountSetting = video->vcountSetting;
194 state->io[REG_DISPSTAT >> 1] = dispstat.packed;
195 state->video.nextEvent = video->nextEvent;
196 state->video.eventDiff = video->eventDiff;
197 state->video.lastHblank = video->lastHblank;
198 state->video.nextHblank = video->nextHblank;
199 state->video.nextHblankIRQ = video->nextHblankIRQ;
200 state->video.nextVblankIRQ = video->nextVblankIRQ;
201 state->video.nextVcounterIRQ = video->nextVcounterIRQ;
202}
203
204void GBAVideoDeserialize(struct GBAVideo* video, struct GBASerializedState* state) {
205 memcpy(video->renderer->vram, state->vram, SIZE_VRAM);
206 memcpy(video->oam.raw, state->oam, SIZE_OAM);
207 int i;
208 for (i = 0; i < SIZE_PALETTE_RAM; i += 2) {
209 GBAStore16(&video->p->memory.d, BASE_PALETTE_RAM | i, state->pram[i >> 1], 0);
210 }
211 union GBARegisterDISPSTAT dispstat;
212 dispstat.packed = state->io[REG_DISPSTAT >> 1];
213 video->inVblank = dispstat.inVblank;
214 video->inHblank = dispstat.inHblank;
215 video->vcounter = dispstat.vcounter;
216 video->vblankIRQ = dispstat.vblankIRQ;
217 video->hblankIRQ = dispstat.hblankIRQ;
218 video->vcounterIRQ = dispstat.vcounterIRQ;
219 video->vcountSetting = dispstat.vcountSetting;
220 video->nextEvent = state->video.nextEvent;
221 video->eventDiff = state->video.eventDiff;
222 video->lastHblank = state->video.lastHblank;
223 video->nextHblank = state->video.nextHblank;
224 video->nextHblankIRQ = state->video.nextHblankIRQ;
225 video->nextVblankIRQ = state->video.nextVblankIRQ;
226 video->nextVcounterIRQ = state->video.nextVcounterIRQ;
227 video->vcount = state->io[REG_VCOUNT >> 1];
228}