include/mgba/internal/gb/audio.h (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#ifndef GB_AUDIO_H
7#define GB_AUDIO_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/core/timing.h>
14
15DECL_BITFIELD(GBAudioRegisterDuty, uint8_t);
16DECL_BITS(GBAudioRegisterDuty, Length, 0, 6);
17DECL_BITS(GBAudioRegisterDuty, Duty, 6, 2);
18
19DECL_BITFIELD(GBAudioRegisterSweep, uint8_t);
20DECL_BITS(GBAudioRegisterSweep, StepTime, 0, 3);
21DECL_BIT(GBAudioRegisterSweep, Direction, 3);
22DECL_BITS(GBAudioRegisterSweep, InitialVolume, 4, 4);
23
24DECL_BITFIELD(GBAudioRegisterControl, uint16_t);
25DECL_BITS(GBAudioRegisterControl, Rate, 0, 11);
26DECL_BITS(GBAudioRegisterControl, Frequency, 0, 11);
27DECL_BIT(GBAudioRegisterControl, Stop, 14);
28DECL_BIT(GBAudioRegisterControl, Restart, 15);
29
30DECL_BITFIELD(GBAudioRegisterSquareSweep, uint8_t);
31DECL_BITS(GBAudioRegisterSquareSweep, Shift, 0, 3);
32DECL_BIT(GBAudioRegisterSquareSweep, Direction, 3);
33DECL_BITS(GBAudioRegisterSquareSweep, Time, 4, 3);
34
35DECL_BITFIELD(GBAudioRegisterBank, uint8_t);
36DECL_BIT(GBAudioRegisterBank, Size, 5);
37DECL_BIT(GBAudioRegisterBank, Bank, 6);
38DECL_BIT(GBAudioRegisterBank, Enable, 7);
39
40DECL_BITFIELD(GBAudioRegisterBankVolume, uint8_t);
41DECL_BITS(GBAudioRegisterBankVolume, VolumeGB, 5, 2);
42DECL_BITS(GBAudioRegisterBankVolume, VolumeGBA, 5, 3);
43
44DECL_BITFIELD(GBAudioRegisterNoiseFeedback, uint8_t);
45DECL_BITS(GBAudioRegisterNoiseFeedback, Ratio, 0, 3);
46DECL_BIT(GBAudioRegisterNoiseFeedback, Power, 3);
47DECL_BITS(GBAudioRegisterNoiseFeedback, Frequency, 4, 4);
48
49DECL_BITFIELD(GBAudioRegisterNoiseControl, uint8_t);
50DECL_BIT(GBAudioRegisterNoiseControl, Stop, 6);
51DECL_BIT(GBAudioRegisterNoiseControl, Restart, 7);
52
53DECL_BITFIELD(GBRegisterNR50, uint8_t);
54DECL_BITS(GBRegisterNR50, VolumeRight, 0, 3);
55DECL_BITS(GBRegisterNR50, VolumeLeft, 4, 3);
56
57DECL_BITFIELD(GBRegisterNR51, uint8_t);
58DECL_BIT(GBRegisterNR51, Ch1Right, 0);
59DECL_BIT(GBRegisterNR51, Ch2Right, 1);
60DECL_BIT(GBRegisterNR51, Ch3Right, 2);
61DECL_BIT(GBRegisterNR51, Ch4Right, 3);
62DECL_BIT(GBRegisterNR51, Ch1Left, 4);
63DECL_BIT(GBRegisterNR51, Ch2Left, 5);
64DECL_BIT(GBRegisterNR51, Ch3Left, 6);
65DECL_BIT(GBRegisterNR51, Ch4Left, 7);
66
67DECL_BITFIELD(GBAudioEnable, uint8_t);
68DECL_BIT(GBAudioEnable, PlayingCh1, 0);
69DECL_BIT(GBAudioEnable, PlayingCh2, 1);
70DECL_BIT(GBAudioEnable, PlayingCh3, 2);
71DECL_BIT(GBAudioEnable, PlayingCh4, 3);
72DECL_BIT(GBAudioEnable, Enable, 7);
73
74struct GB;
75struct GBAudioEnvelope {
76 int length;
77 int duty;
78 int stepTime;
79 int initialVolume;
80 int currentVolume;
81 bool direction;
82 int dead;
83 int nextStep;
84};
85
86struct GBAudioSquareControl {
87 int frequency;
88 int length;
89 bool stop;
90 int hi;
91};
92
93struct GBAudioSweep {
94 int shift;
95 int time;
96 int step;
97 bool direction;
98 bool enable;
99 bool occurred;
100 int realFrequency;
101};
102
103struct GBAudioSquareChannel {
104 struct GBAudioSweep sweep;
105 struct GBAudioEnvelope envelope;
106 struct GBAudioSquareControl control;
107 int8_t sample;
108};
109
110struct GBAudioWaveChannel {
111 bool size;
112 bool bank;
113 bool enable;
114
115 unsigned length;
116 int volume;
117
118 int rate;
119 bool stop;
120
121 int window;
122 bool readable;
123 union {
124 uint32_t wavedata32[8];
125 uint8_t wavedata8[16];
126 };
127 int8_t sample;
128};
129
130struct GBAudioNoiseChannel {
131 struct GBAudioEnvelope envelope;
132
133 int ratio;
134 int frequency;
135 bool power;
136 bool stop;
137 int length;
138
139 uint32_t lfsr;
140 int nSamples;
141 int samples;
142 uint32_t lastEvent;
143
144 int8_t sample;
145};
146
147enum GBAudioStyle {
148 GB_AUDIO_DMG,
149 GB_AUDIO_MGB = GB_AUDIO_DMG, // TODO
150 GB_AUDIO_CGB,
151 GB_AUDIO_AGB, // GB in GBA
152 GB_AUDIO_GBA, // GBA PSG
153};
154
155struct GBAudio {
156 struct GB* p;
157 struct mTiming* timing;
158 unsigned timingFactor;
159 struct GBAudioSquareChannel ch1;
160 struct GBAudioSquareChannel ch2;
161 struct GBAudioWaveChannel ch3;
162 struct GBAudioNoiseChannel ch4;
163
164 struct blip_t* left;
165 struct blip_t* right;
166 int16_t lastLeft;
167 int16_t lastRight;
168 int32_t capLeft;
169 int32_t capRight;
170 int clock;
171 int32_t clockRate;
172
173 uint8_t volumeRight;
174 uint8_t volumeLeft;
175 bool ch1Right;
176 bool ch2Right;
177 bool ch3Right;
178 bool ch4Right;
179 bool ch1Left;
180 bool ch2Left;
181 bool ch3Left;
182 bool ch4Left;
183
184 bool playingCh1;
185 bool playingCh2;
186 bool playingCh3;
187 bool playingCh4;
188 uint8_t* nr52;
189
190 int frame;
191 bool skipFrame;
192
193 int32_t sampleInterval;
194 enum GBAudioStyle style;
195
196 struct mTimingEvent frameEvent;
197 struct mTimingEvent ch1Event;
198 struct mTimingEvent ch2Event;
199 struct mTimingEvent ch3Event;
200 struct mTimingEvent ch3Fade;
201 struct mTimingEvent ch4Event;
202 struct mTimingEvent sampleEvent;
203 bool enable;
204
205 size_t samples;
206 bool forceDisableCh[4];
207 int masterVolume;
208};
209
210void GBAudioInit(struct GBAudio* audio, size_t samples, uint8_t* nr52, enum GBAudioStyle style);
211void GBAudioDeinit(struct GBAudio* audio);
212void GBAudioReset(struct GBAudio* audio);
213
214void GBAudioResizeBuffer(struct GBAudio* audio, size_t samples);
215
216void GBAudioWriteNR10(struct GBAudio* audio, uint8_t);
217void GBAudioWriteNR11(struct GBAudio* audio, uint8_t);
218void GBAudioWriteNR12(struct GBAudio* audio, uint8_t);
219void GBAudioWriteNR13(struct GBAudio* audio, uint8_t);
220void GBAudioWriteNR14(struct GBAudio* audio, uint8_t);
221
222void GBAudioWriteNR21(struct GBAudio* audio, uint8_t);
223void GBAudioWriteNR22(struct GBAudio* audio, uint8_t);
224void GBAudioWriteNR23(struct GBAudio* audio, uint8_t);
225void GBAudioWriteNR24(struct GBAudio* audio, uint8_t);
226
227void GBAudioWriteNR30(struct GBAudio* audio, uint8_t);
228void GBAudioWriteNR31(struct GBAudio* audio, uint8_t);
229void GBAudioWriteNR32(struct GBAudio* audio, uint8_t);
230void GBAudioWriteNR33(struct GBAudio* audio, uint8_t);
231void GBAudioWriteNR34(struct GBAudio* audio, uint8_t);
232
233void GBAudioWriteNR41(struct GBAudio* audio, uint8_t);
234void GBAudioWriteNR42(struct GBAudio* audio, uint8_t);
235void GBAudioWriteNR43(struct GBAudio* audio, uint8_t);
236void GBAudioWriteNR44(struct GBAudio* audio, uint8_t);
237
238void GBAudioWriteNR50(struct GBAudio* audio, uint8_t);
239void GBAudioWriteNR51(struct GBAudio* audio, uint8_t);
240void GBAudioWriteNR52(struct GBAudio* audio, uint8_t);
241
242void GBAudioUpdateFrame(struct GBAudio* audio);
243
244void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right);
245
246struct GBSerializedPSGState;
247void GBAudioPSGSerialize(const struct GBAudio* audio, struct GBSerializedPSGState* state, uint32_t* flagsOut);
248void GBAudioPSGDeserialize(struct GBAudio* audio, const struct GBSerializedPSGState* state, const uint32_t* flagsIn);
249
250struct GBSerializedState;
251void GBAudioSerialize(const struct GBAudio* audio, struct GBSerializedState* state);
252void GBAudioDeserialize(struct GBAudio* audio, const struct GBSerializedState* state);
253
254CXX_GUARD_END
255
256#endif