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
143 int8_t sample;
144};
145
146enum GBAudioStyle {
147 GB_AUDIO_DMG,
148 GB_AUDIO_MGB = GB_AUDIO_DMG, // TODO
149 GB_AUDIO_CGB,
150 GB_AUDIO_AGB, // GB in GBA
151 GB_AUDIO_GBA, // GBA PSG
152};
153
154struct GBAudio {
155 struct GB* p;
156 struct mTiming* timing;
157 unsigned timingFactor;
158 struct GBAudioSquareChannel ch1;
159 struct GBAudioSquareChannel ch2;
160 struct GBAudioWaveChannel ch3;
161 struct GBAudioNoiseChannel ch4;
162
163 struct blip_t* left;
164 struct blip_t* right;
165 int16_t lastLeft;
166 int16_t lastRight;
167 int32_t capLeft;
168 int32_t capRight;
169 int clock;
170 int32_t clockRate;
171
172 uint8_t volumeRight;
173 uint8_t volumeLeft;
174 bool ch1Right;
175 bool ch2Right;
176 bool ch3Right;
177 bool ch4Right;
178 bool ch1Left;
179 bool ch2Left;
180 bool ch3Left;
181 bool ch4Left;
182
183 bool playingCh1;
184 bool playingCh2;
185 bool playingCh3;
186 bool playingCh4;
187 uint8_t* nr52;
188
189 int frame;
190 bool skipFrame;
191
192 int32_t sampleInterval;
193 enum GBAudioStyle style;
194
195 struct mTimingEvent frameEvent;
196 struct mTimingEvent ch1Event;
197 struct mTimingEvent ch2Event;
198 struct mTimingEvent ch3Event;
199 struct mTimingEvent ch3Fade;
200 struct mTimingEvent ch4Event;
201 struct mTimingEvent sampleEvent;
202 bool enable;
203
204 size_t samples;
205 bool forceDisableCh[4];
206 int masterVolume;
207};
208
209void GBAudioInit(struct GBAudio* audio, size_t samples, uint8_t* nr52, enum GBAudioStyle style);
210void GBAudioDeinit(struct GBAudio* audio);
211void GBAudioReset(struct GBAudio* audio);
212
213void GBAudioResizeBuffer(struct GBAudio* audio, size_t samples);
214
215void GBAudioWriteNR10(struct GBAudio* audio, uint8_t);
216void GBAudioWriteNR11(struct GBAudio* audio, uint8_t);
217void GBAudioWriteNR12(struct GBAudio* audio, uint8_t);
218void GBAudioWriteNR13(struct GBAudio* audio, uint8_t);
219void GBAudioWriteNR14(struct GBAudio* audio, uint8_t);
220
221void GBAudioWriteNR21(struct GBAudio* audio, uint8_t);
222void GBAudioWriteNR22(struct GBAudio* audio, uint8_t);
223void GBAudioWriteNR23(struct GBAudio* audio, uint8_t);
224void GBAudioWriteNR24(struct GBAudio* audio, uint8_t);
225
226void GBAudioWriteNR30(struct GBAudio* audio, uint8_t);
227void GBAudioWriteNR31(struct GBAudio* audio, uint8_t);
228void GBAudioWriteNR32(struct GBAudio* audio, uint8_t);
229void GBAudioWriteNR33(struct GBAudio* audio, uint8_t);
230void GBAudioWriteNR34(struct GBAudio* audio, uint8_t);
231
232void GBAudioWriteNR41(struct GBAudio* audio, uint8_t);
233void GBAudioWriteNR42(struct GBAudio* audio, uint8_t);
234void GBAudioWriteNR43(struct GBAudio* audio, uint8_t);
235void GBAudioWriteNR44(struct GBAudio* audio, uint8_t);
236
237void GBAudioWriteNR50(struct GBAudio* audio, uint8_t);
238void GBAudioWriteNR51(struct GBAudio* audio, uint8_t);
239void GBAudioWriteNR52(struct GBAudio* audio, uint8_t);
240
241void GBAudioUpdateFrame(struct GBAudio* audio, struct mTiming* timing);
242
243void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right);
244
245struct GBSerializedPSGState;
246void GBAudioPSGSerialize(const struct GBAudio* audio, struct GBSerializedPSGState* state, uint32_t* flagsOut);
247void GBAudioPSGDeserialize(struct GBAudio* audio, const struct GBSerializedPSGState* state, const uint32_t* flagsIn);
248
249struct GBSerializedState;
250void GBAudioSerialize(const struct GBAudio* audio, struct GBSerializedState* state);
251void GBAudioDeserialize(struct GBAudio* audio, const struct GBSerializedState* state);
252
253CXX_GUARD_END
254
255#endif