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 "gb/gb.h"
9#include "gb/io.h"
10
11#include "util/memory.h"
12
13static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer);
14static void GBVideoDummyRendererReset(struct GBVideoRenderer* renderer);
15static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer);
16static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
17static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address);
18static void GBVideoDummyRendererDrawScanline(struct GBVideoRenderer* renderer, int y);
19static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
20static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
21
22static struct GBVideoRenderer dummyRenderer = {
23 .init = GBVideoDummyRendererInit,
24 .reset = GBVideoDummyRendererReset,
25 .deinit = GBVideoDummyRendererDeinit,
26 .writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
27 .writeVRAM = GBVideoDummyRendererWriteVRAM,
28 .drawScanline = GBVideoDummyRendererDrawScanline,
29 .finishFrame = GBVideoDummyRendererFinishFrame,
30 .getPixels = GBVideoDummyRendererGetPixels
31};
32
33void GBVideoInit(struct GBVideo* video) {
34 video->renderer = &dummyRenderer;
35 video->vram = 0;
36 video->frameskip = 0;
37}
38
39void GBVideoReset(struct GBVideo* video) {
40 video->ly = 0;
41 video->mode = 1;
42
43 video->nextEvent = INT_MAX;
44 video->eventDiff = 0;
45
46 video->nextMode = INT_MAX;
47
48 video->frameCounter = 0;
49 video->frameskipCounter = 0;
50
51 if (video->vram) {
52 mappedMemoryFree(video->vram, GB_SIZE_VRAM);
53 }
54 video->vram = anonymousMemoryMap(GB_SIZE_VRAM);
55 video->renderer->vram = video->vram;
56
57 video->renderer->deinit(video->renderer);
58 video->renderer->init(video->renderer);
59}
60
61void GBVideoDeinit(struct GBVideo* video) {
62 GBVideoAssociateRenderer(video, &dummyRenderer);
63 mappedMemoryFree(video->vram, GB_SIZE_VRAM);
64}
65
66void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
67 video->renderer->deinit(video->renderer);
68 video->renderer = renderer;
69 renderer->vram = video->vram;
70 video->renderer->init(video->renderer);
71}
72
73int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles) {
74 video->eventDiff += cycles;
75 if (video->nextEvent != INT_MAX) {
76 video->nextEvent -= cycles;
77 }
78 if (video->nextEvent <= 0) {
79 if (video->nextEvent != INT_MAX) {
80 video->nextMode -= video->eventDiff;
81 }
82 if (video->nextMode <= 0) {
83 switch (video->mode) {
84 case 0:
85 if (video->ly < GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
86 video->renderer->drawScanline(video->renderer, video->ly);
87 }
88 ++video->ly;
89 video->p->memory.io[REG_LY] = video->ly;
90 if (video->ly >= GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
91 video->ly = 0;
92 ++video->frameCounter;
93 video->renderer->finishFrame(video->renderer);
94 video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
95 video->mode = 1;
96 if (GBRegisterSTATIsVblankIRQ(video->stat)) {
97 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
98 }
99 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
100 GBUpdateIRQs(video->p);
101 } else {
102 video->nextMode = GB_VIDEO_MODE_2_LENGTH;
103 video->mode = 2;
104 if (GBRegisterSTATIsOAMIRQ(video->stat)) {
105 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
106 GBUpdateIRQs(video->p);
107 }
108 }
109 break;
110 case 1:
111 video->nextMode = GB_VIDEO_MODE_2_LENGTH;
112 video->mode = 2;
113 if (GBRegisterSTATIsOAMIRQ(video->stat)) {
114 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
115 GBUpdateIRQs(video->p);
116 }
117 break;
118 case 2:
119 video->nextMode = GB_VIDEO_MODE_3_LENGTH;
120 video->mode = 3;
121 break;
122 case 3:
123 video->nextMode = GB_VIDEO_MODE_0_LENGTH;
124 video->mode = 0;
125 if (GBRegisterSTATIsHblankIRQ(video->stat)) {
126 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
127 GBUpdateIRQs(video->p);
128 }
129 break;
130 }
131 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
132 video->p->memory.io[REG_STAT] = video->stat;
133 }
134
135 video->nextEvent = video->nextMode;
136 video->eventDiff = 0;
137 }
138 return video->nextEvent;
139}
140
141void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
142 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
143 // TODO: Does enabling the LCD start in vblank?
144 video->mode = 2;
145 video->nextMode = GB_VIDEO_MODE_2_LENGTH;
146 video->nextEvent = video->nextMode;
147 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
148 video->p->memory.io[REG_STAT] = video->stat;
149 video->eventDiff = 0;
150 if (video->p->cpu->cycles + video->nextEvent < video->p->cpu->nextEvent) {
151 video->p->cpu->nextEvent = video->p->cpu->cycles + video->nextEvent;
152 }
153 return;
154 }
155 if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
156 video->mode = 0;
157 video->nextMode = INT_MAX;
158 video->nextEvent = INT_MAX;
159 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
160 video->p->memory.io[REG_STAT] = video->stat;
161 video->ly = 0;
162 video->p->memory.io[REG_LY] = 0;
163 }
164}
165
166void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
167 video->stat = (video->stat & 0x7) | (value & 0x78);
168}
169
170static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer) {
171 UNUSED(renderer);
172 // Nothing to do
173}
174
175static void GBVideoDummyRendererReset(struct GBVideoRenderer* renderer) {
176 UNUSED(renderer);
177 // Nothing to do
178}
179
180static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
181 UNUSED(renderer);
182 // Nothing to do
183}
184
185static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
186 UNUSED(renderer);
187 UNUSED(address);
188 return value;
189}
190
191static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
192 UNUSED(renderer);
193 UNUSED(address);
194 // Nothing to do
195}
196
197static void GBVideoDummyRendererDrawScanline(struct GBVideoRenderer* renderer, int y) {
198 UNUSED(renderer);
199 UNUSED(y);
200 // Nothing to do
201}
202
203static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
204 UNUSED(renderer);
205 // Nothing to do
206}
207
208static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
209 UNUSED(renderer);
210 UNUSED(stride);
211 UNUSED(pixels);
212 // Nothing to do
213}