src/gba/gba-video.c (view raw)
1#include "gba-video.h"
2
3#include "gba.h"
4#include "gba-io.h"
5
6#include <limits.h>
7#include <string.h>
8
9static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer);
10static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer);
11static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
12static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
13static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer);
14
15static struct GBAVideoRenderer dummyRenderer = {
16 .init = GBAVideoDummyRendererInit,
17 .deinit = GBAVideoDummyRendererDeinit,
18 .writeVideoRegister = GBAVideoDummyRendererWriteVideoRegister,
19 .drawScanline = GBAVideoDummyRendererDrawScanline,
20 .finishFrame = GBAVideoDummyRendererFinishFrame
21};
22
23void GBAVideoInit(struct GBAVideo* video) {
24 video->renderer = &dummyRenderer;
25
26 video->inHblank = 0;
27 video->inVblank = 0;
28 video->vcounter = 0;
29 video->vblankIRQ = 0;
30 video->hblankIRQ = 0;
31 video->vcounterIRQ = 0;
32 video->vcountSetting = 0;
33
34 video->vcount = -1;
35
36 video->lastHblank = 0;
37 video->nextHblank = VIDEO_HDRAW_LENGTH;
38 video->nextEvent = video->nextHblank;
39 video->eventDiff = video->nextEvent;
40
41 video->nextHblankIRQ = 0;
42 video->nextVblankIRQ = 0;
43 video->nextVcounterIRQ = 0;
44}
45
46void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
47 video->renderer->deinit(video->renderer);
48 video->renderer = renderer;
49 renderer->palette = video->palette;
50 renderer->vram = video->vram;
51 renderer->oam = &video->oam;
52 video->renderer->init(video->renderer);
53}
54
55int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
56 video->nextEvent -= cycles;
57 if (video->nextEvent <= 0) {
58 video->lastHblank -= video->eventDiff;
59 video->nextHblank -= video->eventDiff;
60 video->nextHblankIRQ -= video->eventDiff;
61 video->nextVcounterIRQ -= video->eventDiff;
62
63 if (video->inHblank) {
64 // End Hblank
65 video->inHblank = 0;
66 video->nextEvent = video->nextHblank;
67
68 ++video->vcount;
69 video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
70
71 switch (video->vcount) {
72 case VIDEO_VERTICAL_PIXELS:
73 video->inVblank = 1;
74 video->renderer->finishFrame(video->renderer);
75 video->nextVblankIRQ = video->nextEvent + VIDEO_TOTAL_LENGTH;
76 GBAMemoryRunVblankDMAs(&video->p->memory);
77 if (video->vblankIRQ) {
78 GBARaiseIRQ(video->p, IRQ_VBLANK);
79 }
80 //video->vblankCallback();
81 break;
82 case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
83 video->inVblank = 0;
84 break;
85 case VIDEO_VERTICAL_TOTAL_PIXELS:
86 video->vcount = 0;
87 //video->renderPath.startDraw();
88 break;
89 }
90
91 video->vcounter = video->vcount == video->vcountSetting;
92 if (video->vcounter && video->vcounterIRQ) {
93 GBARaiseIRQ(video->p, IRQ_VCOUNTER);
94 video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
95 }
96
97 if (video->vcount < VIDEO_VERTICAL_PIXELS) {
98 video->renderer->drawScanline(video->renderer, video->vcount);
99 }
100 } else {
101 // Begin Hblank
102 video->inHblank = 1;
103 video->lastHblank = video->nextHblank;
104 video->nextEvent = video->lastHblank + VIDEO_HBLANK_LENGTH;
105 video->nextHblank = video->nextEvent + VIDEO_HDRAW_LENGTH;
106 video->nextHblankIRQ = video->nextHblank;
107
108 if (video->vcount < VIDEO_VERTICAL_PIXELS) {
109 GBAMemoryRunHblankDMAs(&video->p->memory);
110 }
111 if (video->hblankIRQ) {
112 GBARaiseIRQ(video->p, IRQ_HBLANK);
113 }
114 }
115
116 video->eventDiff = video->nextEvent;
117 }
118 return video->nextEvent;
119}
120
121void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
122 video->vblankIRQ = value & 0x0008;
123 video->hblankIRQ = value & 0x0010;
124 video->vcounterIRQ = value & 0x0020;
125 video->vcountSetting = (value & 0xFF00) >> 8;
126
127 if (video->vcounterIRQ) {
128 // FIXME: this can be too late if we're in the middle of an Hblank
129 video->nextVcounterIRQ = video->nextHblank + VIDEO_HBLANK_LENGTH + (video->vcountSetting - video->vcount) * VIDEO_HORIZONTAL_LENGTH;
130 if (video->nextVcounterIRQ < video->nextEvent) {
131 video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
132 }
133 }
134}
135
136uint16_t GBAVideoReadDISPSTAT(struct GBAVideo* video) {
137 return (video->inVblank) | (video->inHblank << 1) | (video->vcounter << 2);
138}
139
140static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
141 (void)(renderer);
142 // Nothing to do
143}
144
145static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
146 (void)(renderer);
147 // Nothing to do
148}
149
150static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
151 (void)(renderer);
152 (void)(address);
153 return value;
154}
155
156static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
157 (void)(renderer);
158 (void)(y);
159 // Nothing to do
160}
161
162static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
163 (void)(renderer);
164 // Nothing to do
165}