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 // TODO
68}
69
70int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles) {
71 video->eventDiff += cycles;
72 if (video->nextEvent != INT_MAX) {
73 video->nextEvent -= cycles;
74 }
75 if (video->nextEvent <= 0) {
76 if (video->nextEvent != INT_MAX) {
77 video->nextMode -= video->eventDiff;
78 }
79 if (video->nextMode <= 0) {
80 switch (video->mode) {
81 case 0:
82 ++video->ly;
83 video->p->memory.io[REG_LY] = video->ly;
84 if (video->ly >= GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
85 video->ly = 0;
86 ++video->frameCounter;
87 video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
88 video->mode = 1;
89 if (GBRegisterSTATIsVblankIRQ(video->stat)) {
90 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
91 }
92 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
93 GBUpdateIRQs(video->p);
94 } else {
95 video->nextMode = GB_VIDEO_MODE_2_LENGTH;
96 video->mode = 2;
97 if (GBRegisterSTATIsOAMIRQ(video->stat)) {
98 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
99 GBUpdateIRQs(video->p);
100 }
101 }
102 break;
103 case 1:
104 video->nextMode = GB_VIDEO_MODE_2_LENGTH;
105 video->mode = 2;
106 if (GBRegisterSTATIsOAMIRQ(video->stat)) {
107 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
108 GBUpdateIRQs(video->p);
109 }
110 break;
111 case 2:
112 video->nextMode = GB_VIDEO_MODE_3_LENGTH;
113 video->mode = 3;
114 break;
115 case 3:
116 video->nextMode = GB_VIDEO_MODE_0_LENGTH;
117 video->mode = 0;
118 if (GBRegisterSTATIsHblankIRQ(video->stat)) {
119 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
120 GBUpdateIRQs(video->p);
121 }
122 break;
123 }
124 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
125 video->p->memory.io[REG_STAT] = video->stat;
126 }
127
128 video->nextEvent = video->nextMode;
129 video->eventDiff = 0;
130 }
131 return video->nextEvent;
132}
133
134void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
135 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
136 // TODO: Does enabling the LCD start in vblank?
137 video->mode = 2;
138 video->nextMode = GB_VIDEO_MODE_2_LENGTH;
139 video->nextEvent = video->nextMode;
140 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
141 video->p->memory.io[REG_STAT] = video->stat;
142 video->eventDiff = 0;
143 if (video->p->cpu->cycles + video->nextEvent < video->p->cpu->nextEvent) {
144 video->p->cpu->nextEvent = video->p->cpu->cycles + video->nextEvent;
145 }
146 return;
147 }
148 if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
149 video->mode = 0;
150 video->nextMode = INT_MAX;
151 video->nextEvent = INT_MAX;
152 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
153 video->p->memory.io[REG_STAT] = video->stat;
154 video->ly = 0;
155 video->p->memory.io[REG_LY] = 0;
156 }
157}
158
159void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
160 video->stat = (video->stat & 0x7) | (value & 0x78);
161}
162
163static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer) {
164 UNUSED(renderer);
165 // Nothing to do
166}
167
168static void GBVideoDummyRendererReset(struct GBVideoRenderer* renderer) {
169 UNUSED(renderer);
170 // Nothing to do
171}
172
173static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
174 UNUSED(renderer);
175 // Nothing to do
176}
177
178static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
179 UNUSED(renderer);
180 UNUSED(address);
181 return value;
182}
183
184static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
185 UNUSED(renderer);
186 UNUSED(address);
187 // Nothing to do
188}
189
190static void GBVideoDummyRendererDrawScanline(struct GBVideoRenderer* renderer, int y) {
191 UNUSED(renderer);
192 UNUSED(y);
193 // Nothing to do
194}
195
196static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
197 UNUSED(renderer);
198 // Nothing to do
199}
200
201static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
202 UNUSED(renderer);
203 UNUSED(stride);
204 UNUSED(pixels);
205 // Nothing to do
206}