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