src/gb/video.c (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "video.h"
7
8#include "core/sync.h"
9#include "gb/gb.h"
10#include "gb/io.h"
11
12#include "util/memory.h"
13
14static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer);
15static void GBVideoDummyRendererReset(struct GBVideoRenderer* renderer);
16static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer);
17static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
18static void GBVideoDummyRendererDrawDot(struct GBVideoRenderer* renderer, int x, int y, struct GBObj** obj, size_t oamMax);
19static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
20static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
21
22static void _cleanOAM(struct GBVideo* video, int y);
23
24static struct GBVideoRenderer dummyRenderer = {
25 .init = GBVideoDummyRendererInit,
26 .reset = GBVideoDummyRendererReset,
27 .deinit = GBVideoDummyRendererDeinit,
28 .writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
29 .drawDot = GBVideoDummyRendererDrawDot,
30 .finishFrame = GBVideoDummyRendererFinishFrame,
31 .getPixels = GBVideoDummyRendererGetPixels
32};
33
34void GBVideoInit(struct GBVideo* video) {
35 video->renderer = &dummyRenderer;
36 video->vram = 0;
37 video->frameskip = 0;
38}
39
40void GBVideoReset(struct GBVideo* video) {
41 video->ly = 0;
42 video->mode = 1;
43 video->stat = 1;
44
45 video->nextEvent = INT_MAX;
46 video->eventDiff = 0;
47
48 video->nextMode = INT_MAX;
49 video->nextDot = INT_MAX;
50
51 video->frameCounter = 0;
52 video->frameskipCounter = 0;
53
54 if (video->vram) {
55 mappedMemoryFree(video->vram, GB_SIZE_VRAM);
56 }
57 video->vram = anonymousMemoryMap(GB_SIZE_VRAM);
58 video->renderer->vram = video->vram;
59 memset(&video->oam, 0, sizeof(video->oam));
60 video->renderer->oam = &video->oam;
61
62 video->renderer->deinit(video->renderer);
63 video->renderer->init(video->renderer);
64}
65
66void GBVideoDeinit(struct GBVideo* video) {
67 GBVideoAssociateRenderer(video, &dummyRenderer);
68 mappedMemoryFree(video->vram, GB_SIZE_VRAM);
69}
70
71void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
72 video->renderer->deinit(video->renderer);
73 video->renderer = renderer;
74 renderer->vram = video->vram;
75 video->renderer->init(video->renderer);
76}
77
78int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles) {
79 video->eventDiff += cycles;
80 if (video->nextEvent != INT_MAX) {
81 video->nextEvent -= cycles;
82 }
83 if (video->nextEvent <= 0) {
84 if (video->nextEvent != INT_MAX) {
85 video->nextMode -= video->eventDiff;
86 video->nextDot -= video->eventDiff;
87 }
88 video->nextEvent = INT_MAX;
89 if (video->nextDot <= 0) {
90 video->renderer->drawDot(video->renderer, video->x, video->ly, video->objThisLine, video->objMax);
91 ++video->x;
92 if (video->x < GB_VIDEO_HORIZONTAL_PIXELS) {
93 video->nextDot = 1;
94 } else {
95 video->nextDot = INT_MAX;
96 }
97 }
98 if (video->nextMode <= 0) {
99 int lyc = video->p->memory.io[REG_LYC];
100 switch (video->mode) {
101 case 0:
102 ++video->ly;
103 video->p->memory.io[REG_LY] = video->ly;
104 video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
105 if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
106 video->nextMode = GB_VIDEO_MODE_2_LENGTH;
107 video->mode = 2;
108 if (GBRegisterSTATIsOAMIRQ(video->stat)) {
109 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
110 }
111 } else {
112 video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
113 video->mode = 1;
114 ++video->frameCounter;
115 video->renderer->finishFrame(video->renderer);
116 mCoreSyncPostFrame(video->p->sync);
117 if (GBRegisterSTATIsVblankIRQ(video->stat) || GBRegisterSTATIsOAMIRQ(video->stat)) {
118 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
119 }
120 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
121 }
122 if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) {
123 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
124 }
125 GBUpdateIRQs(video->p);
126 break;
127 case 1:
128 ++video->ly;
129 video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
130 if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) {
131 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
132 }
133 if (video->ly >= GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
134 video->ly = 0;
135 video->nextMode = GB_VIDEO_MODE_2_LENGTH;
136 video->mode = 2;
137 if (GBRegisterSTATIsOAMIRQ(video->stat)) {
138 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
139 GBUpdateIRQs(video->p);
140 }
141 } else {
142 video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
143 }
144 video->p->memory.io[REG_LY] = video->ly;
145 break;
146 case 2:
147 _cleanOAM(video, video->ly);
148 video->nextDot = 1;
149 video->nextEvent = video->nextDot;
150 video->x = 0;
151 video->nextMode = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 8;
152 video->mode = 3;
153 break;
154 case 3:
155 video->nextMode = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 8;
156 video->mode = 0;
157 if (GBRegisterSTATIsHblankIRQ(video->stat)) {
158 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
159 GBUpdateIRQs(video->p);
160 }
161 break;
162 }
163 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
164 video->p->memory.io[REG_STAT] = video->stat;
165 }
166 if (video->nextDot < video->nextEvent) {
167 video->nextEvent = video->nextDot;
168 }
169 if (video->nextMode < video->nextEvent) {
170 video->nextEvent = video->nextMode;
171 }
172 video->eventDiff = 0;
173 }
174 return video->nextEvent;
175}
176
177static void _cleanOAM(struct GBVideo* video, int y) {
178 // TODO: GBC differences
179 // TODO: Optimize
180 video->objMax = 0;
181 int spriteHeight = 8;
182 if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
183 spriteHeight = 16;
184 }
185 int o = 0;
186 int i;
187 for (i = 0; i < 40; ++i) {
188 uint8_t oy = video->oam.obj[i].y;
189 if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
190 continue;
191 }
192 // TODO: Sort
193 video->objThisLine[o] = &video->oam.obj[i];
194 ++o;
195 if (o == 10) {
196 break;
197 }
198 }
199 video->objMax = o;
200}
201
202void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
203 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
204 video->mode = 2;
205 video->nextMode = GB_VIDEO_MODE_2_LENGTH - 5; // TODO: Why is this fudge factor needed? Might be related to T-cycles for load/store differing
206 video->nextEvent = video->nextMode;
207 video->eventDiff = -video->p->cpu->cycles;
208 // TODO: Does this read as 0 for 4 T-cycles?
209 video->stat = GBRegisterSTATSetMode(video->stat, 2);
210 video->p->memory.io[REG_STAT] = video->stat;
211 video->ly = 0;
212 video->p->memory.io[REG_LY] = 0;
213
214 if (video->p->cpu->cycles + video->nextEvent < video->p->cpu->nextEvent) {
215 video->p->cpu->nextEvent = video->p->cpu->cycles + video->nextEvent;
216 }
217 return;
218 }
219 if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
220 video->mode = 0;
221 video->nextMode = INT_MAX;
222 video->nextEvent = INT_MAX;
223 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
224 video->p->memory.io[REG_STAT] = video->stat;
225 video->ly = 0;
226 video->p->memory.io[REG_LY] = 0;
227 }
228}
229
230void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
231 video->stat = (video->stat & 0x7) | (value & 0x78);
232}
233
234static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer) {
235 UNUSED(renderer);
236 // Nothing to do
237}
238
239static void GBVideoDummyRendererReset(struct GBVideoRenderer* renderer) {
240 UNUSED(renderer);
241 // Nothing to do
242}
243
244static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
245 UNUSED(renderer);
246 // Nothing to do
247}
248
249static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
250 UNUSED(renderer);
251 UNUSED(address);
252 return value;
253}
254
255static void GBVideoDummyRendererDrawDot(struct GBVideoRenderer* renderer, int x, int y, struct GBObj** obj, size_t oamMax) {
256 UNUSED(renderer);
257 UNUSED(x);
258 UNUSED(y);
259 UNUSED(obj);
260 UNUSED(oamMax);
261 // Nothing to do
262}
263
264static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
265 UNUSED(renderer);
266 // Nothing to do
267}
268
269static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
270 UNUSED(renderer);
271 UNUSED(stride);
272 UNUSED(pixels);
273 // Nothing to do
274}