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
11#include "third-party/blip_buf/blip_buf.h"
12
13DECL_BITFIELD(GBAudioRegisterDuty, uint8_t);
14DECL_BITS(GBAudioRegisterDuty, Length, 0, 6);
15DECL_BITS(GBAudioRegisterDuty, Duty, 6, 2);
16
17DECL_BITFIELD(GBAudioRegisterSweep, uint8_t);
18DECL_BITS(GBAudioRegisterSweep, StepTime, 0, 3);
19DECL_BIT(GBAudioRegisterSweep, Direction, 3);
20DECL_BITS(GBAudioRegisterSweep, InitialVolume, 4, 4);
21
22DECL_BITFIELD(GBAudioRegisterControl, uint16_t);
23DECL_BITS(GBAudioRegisterControl, Rate, 0, 11);
24DECL_BITS(GBAudioRegisterControl, Frequency, 0, 11);
25DECL_BIT(GBAudioRegisterControl, Stop, 14);
26DECL_BIT(GBAudioRegisterControl, Restart, 15);
27
28DECL_BITFIELD(GBAudioRegisterSquareSweep, uint8_t);
29DECL_BITS(GBAudioRegisterSquareSweep, Shift, 0, 3);
30DECL_BIT(GBAudioRegisterSquareSweep, Direction, 3);
31DECL_BITS(GBAudioRegisterSquareSweep, Time, 4, 3);
32
33DECL_BITFIELD(GBAudioRegisterBank, uint8_t);
34DECL_BIT(GBAudioRegisterBank, Size, 5);
35DECL_BIT(GBAudioRegisterBank, Bank, 6);
36DECL_BIT(GBAudioRegisterBank, Enable, 7);
37
38DECL_BITFIELD(GBAudioRegisterBankVolume, uint8_t);
39DECL_BITS(GBAudioRegisterBankVolume, VolumeGB, 5, 2);
40DECL_BITS(GBAudioRegisterBankVolume, VolumeGBA, 5, 3);
41
42DECL_BITFIELD(GBAudioRegisterNoiseFeedback, uint8_t);
43DECL_BITS(GBAudioRegisterNoiseFeedback, Ratio, 0, 3);
44DECL_BIT(GBAudioRegisterNoiseFeedback, Power, 3);
45DECL_BITS(GBAudioRegisterNoiseFeedback, Frequency, 4, 4);
46
47DECL_BITFIELD(GBAudioRegisterNoiseControl, uint8_t);
48DECL_BIT(GBAudioRegisterNoiseControl, Stop, 6);
49DECL_BIT(GBAudioRegisterNoiseControl, Restart, 7);
50
51DECL_BITFIELD(GBRegisterNR50, uint8_t);
52DECL_BITS(GBRegisterNR50, VolumeRight, 0, 3);
53DECL_BITS(GBRegisterNR50, VolumeLeft, 4, 3);
54
55DECL_BITFIELD(GBRegisterNR51, uint8_t);
56DECL_BIT(GBRegisterNR51, Ch1Right, 0);
57DECL_BIT(GBRegisterNR51, Ch2Right, 1);
58DECL_BIT(GBRegisterNR51, Ch3Right, 2);
59DECL_BIT(GBRegisterNR51, Ch4Right, 3);
60DECL_BIT(GBRegisterNR51, Ch1Left, 4);
61DECL_BIT(GBRegisterNR51, Ch2Left, 5);
62DECL_BIT(GBRegisterNR51, Ch3Left, 6);
63DECL_BIT(GBRegisterNR51, Ch4Left, 7);
64
65DECL_BITFIELD(GBAudioEnable, uint8_t);
66DECL_BIT(GBAudioEnable, PlayingCh1, 0);
67DECL_BIT(GBAudioEnable, PlayingCh2, 1);
68DECL_BIT(GBAudioEnable, PlayingCh3, 2);
69DECL_BIT(GBAudioEnable, PlayingCh4, 3);
70DECL_BIT(GBAudioEnable, Enable, 7);
71
72struct GB;
73struct GBAudioEnvelope {
74 int length;
75 int duty;
76 int stepTime;
77 int initialVolume;
78 int currentVolume;
79 bool direction;
80 int dead;
81 int nextStep;
82};
83
84struct GBAudioSquareControl {
85 int frequency;
86 int length;
87 bool stop;
88 int hi;
89};
90
91struct GBAudioChannel1 {
92 int shift;
93 int time;
94 int sweepStep;
95 bool direction;
96 bool sweepEnable;
97 bool sweepOccurred;
98 int realFrequency;
99
100 struct GBAudioEnvelope envelope;
101 struct GBAudioSquareControl control;
102 int8_t sample;
103};
104
105struct GBAudioChannel2 {
106 struct GBAudioEnvelope envelope;
107 struct GBAudioSquareControl control;
108 int8_t sample;
109};
110
111struct GBAudioChannel3 {
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 GBAudioChannel4 {
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 GBAudioChannel1 ch1;
154 struct GBAudioChannel2 ch2;
155 struct GBAudioChannel3 ch3;
156 struct GBAudioChannel4 ch4;
157
158 blip_t* left;
159 blip_t* right;
160 int16_t lastLeft;
161 int16_t lastRight;
162 int clock;
163 int32_t clockRate;
164
165 uint8_t volumeRight;
166 uint8_t volumeLeft;
167 bool ch1Right;
168 bool ch2Right;
169 bool ch3Right;
170 bool ch4Right;
171 bool ch1Left;
172 bool ch2Left;
173 bool ch3Left;
174 bool ch4Left;
175
176 bool playingCh1;
177 bool playingCh2;
178 bool playingCh3;
179 bool playingCh4;
180 uint8_t* nr52;
181
182 int32_t nextEvent;
183 int32_t eventDiff;
184 int32_t nextFrame;
185 int frame;
186 int32_t nextSample;
187
188 int32_t sampleInterval;
189 enum GBAudioStyle style;
190
191 int32_t nextCh1;
192 int32_t nextCh2;
193 int32_t nextCh3;
194 int32_t fadeCh3;
195 int32_t nextCh4;
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
235int32_t GBAudioProcessEvents(struct GBAudio* audio, int32_t cycles);
236void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right);
237
238#endif