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 = renderer;
48 renderer->palette = video->palette;
49 renderer->vram = video->vram;
50 renderer->oam = &video->oam;
51}
52
53int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
54 video->nextEvent -= cycles;
55 if (video->nextEvent <= 0) {
56 video->lastHblank -= video->eventDiff;
57 video->nextHblank -= video->eventDiff;
58 video->nextHblankIRQ -= video->eventDiff;
59 video->nextVcounterIRQ -= video->eventDiff;
60
61 if (video->inHblank) {
62 // End Hblank
63 video->inHblank = 0;
64 video->nextEvent = video->nextHblank;
65
66 ++video->vcount;
67 video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
68
69 switch (video->vcount) {
70 case VIDEO_VERTICAL_PIXELS:
71 video->inVblank = 1;
72 video->renderer->finishFrame(video->renderer);
73 video->nextVblankIRQ = video->nextEvent + VIDEO_TOTAL_LENGTH;
74 //video->cpu.mmu.runVblankDmas();
75 if (video->vblankIRQ) {
76 GBARaiseIRQ(video->p, IRQ_VBLANK);
77 }
78 //video->vblankCallback();
79 break;
80 case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
81 video->inVblank = 0;
82 break;
83 case VIDEO_VERTICAL_TOTAL_PIXELS:
84 video->vcount = 0;
85 //video->renderPath.startDraw();
86 break;
87 }
88
89 video->vcounter = video->vcount == video->vcountSetting;
90 if (video->vcounter && video->vcounterIRQ) {
91 GBARaiseIRQ(video->p, IRQ_VCOUNTER);
92 video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
93 }
94
95 if (video->vcount < VIDEO_VERTICAL_PIXELS) {
96 video->renderer->drawScanline(video->renderer, video->vcount);
97 }
98 } else {
99 // Begin Hblank
100 video->inHblank = 1;
101 video->lastHblank = video->nextHblank;
102 video->nextEvent = video->lastHblank + VIDEO_HBLANK_LENGTH;
103 video->nextHblank = video->nextEvent + VIDEO_HDRAW_LENGTH;
104 video->nextHblankIRQ = video->nextHblank;
105
106 if (video->vcount < VIDEO_VERTICAL_PIXELS) {
107 //video->cpu.mmu.runHblankDmas();
108 }
109 if (video->hblankIRQ) {
110 GBARaiseIRQ(video->p, IRQ_HBLANK);
111 }
112 }
113
114 video->eventDiff = video->nextEvent;
115 }
116 return video->nextEvent;
117}
118
119void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
120 video->vblankIRQ = value & 0x0008;
121 video->hblankIRQ = value & 0x0010;
122 video->vcounterIRQ = value & 0x0020;
123 video->vcountSetting = (value & 0xFF00) >> 8;
124
125 if (video->vcounterIRQ) {
126 // FIXME: this can be too late if we're in the middle of an Hblank
127 video->nextVcounterIRQ = video->nextHblank + VIDEO_HBLANK_LENGTH + (video->vcountSetting - video->vcount) * VIDEO_HORIZONTAL_LENGTH;
128 if (video->nextVcounterIRQ < video->nextEvent) {
129 video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
130 }
131 }
132}
133
134uint16_t GBAVideoReadDISPSTAT(struct GBAVideo* video) {
135 return (video->inVblank) | (video->inHblank << 1) | (video->vcounter << 2);
136}
137
138static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
139 (void)(renderer);
140 // Nothing to do
141}
142
143static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
144 (void)(renderer);
145 // Nothing to do
146}
147
148static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
149 (void)(renderer);
150 (void)(address);
151 return value;
152}
153
154static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
155 (void)(renderer);
156 (void)(y);
157 // Nothing to do
158}
159
160static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
161 (void)(renderer);
162 printf("Drawing a frame\n");
163 // Nothing to do
164}