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 uint8_t length;
75 uint8_t duty;
76 uint8_t stepTime;
77 uint8_t initialVolume;
78 bool direction;
79 int currentVolume;
80 int dead;
81 uint8_t nextStep;
82};
83
84struct GBAudioSquareControl {
85 uint16_t frequency;
86 uint8_t length;
87 bool stop;
88 int hi;
89};
90
91struct GBAudioChannel1 {
92 uint8_t shift;
93 uint8_t time;
94 uint8_t sweepStep;
95 bool direction;
96 bool sweepEnable;
97 bool sweepOccurred;
98 uint16_t 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 unsigned lengthShadow;
118 uint8_t volume;
119
120 uint16_t rate;
121 bool stop;
122
123 uint32_t wavedata[8];
124 int8_t sample;
125};
126
127struct GBAudioChannel4 {
128 struct GBAudioEnvelope envelope;
129
130 uint8_t ratio;
131 uint8_t frequency;
132 bool power;
133 bool stop;
134 uint8_t length;
135
136 uint32_t lfsr;
137 int8_t sample;
138};
139
140struct GBAudio {
141 struct GB* p;
142 struct GBAudioChannel1 ch1;
143 struct GBAudioChannel2 ch2;
144 struct GBAudioChannel3 ch3;
145 struct GBAudioChannel4 ch4;
146
147 blip_t* left;
148 blip_t* right;
149 int16_t lastLeft;
150 int16_t lastRight;
151 int clock;
152 int32_t clockRate;
153
154 uint8_t volumeRight;
155 uint8_t volumeLeft;
156 bool ch1Right;
157 bool ch2Right;
158 bool ch3Right;
159 bool ch4Right;
160 bool ch1Left;
161 bool ch2Left;
162 bool ch3Left;
163 bool ch4Left;
164
165 bool playingCh1;
166 bool playingCh2;
167 bool playingCh3;
168 bool playingCh4;
169
170 int32_t nextEvent;
171 int32_t eventDiff;
172 int32_t nextFrame;
173 int frame;
174 int32_t nextSample;
175
176 int32_t sampleInterval;
177
178 int32_t nextCh1;
179 int32_t nextCh2;
180 int32_t nextCh3;
181 int32_t nextCh4;
182 bool enable;
183
184 size_t samples;
185 bool forceDisableCh[4];
186};
187
188void GBAudioInit(struct GBAudio* audio, size_t samples);
189void GBAudioDeinit(struct GBAudio* audio);
190void GBAudioReset(struct GBAudio* audio);
191
192void GBAudioResizeBuffer(struct GBAudio* audio, size_t samples);
193
194void GBAudioWriteNR10(struct GBAudio* audio, uint8_t);
195void GBAudioWriteNR11(struct GBAudio* audio, uint8_t);
196void GBAudioWriteNR12(struct GBAudio* audio, uint8_t);
197void GBAudioWriteNR13(struct GBAudio* audio, uint8_t);
198void GBAudioWriteNR14(struct GBAudio* audio, uint8_t);
199
200void GBAudioWriteNR21(struct GBAudio* audio, uint8_t);
201void GBAudioWriteNR22(struct GBAudio* audio, uint8_t);
202void GBAudioWriteNR23(struct GBAudio* audio, uint8_t);
203void GBAudioWriteNR24(struct GBAudio* audio, uint8_t);
204
205void GBAudioWriteNR30(struct GBAudio* audio, uint8_t);
206void GBAudioWriteNR31(struct GBAudio* audio, uint8_t);
207void GBAudioWriteNR32(struct GBAudio* audio, uint8_t);
208void GBAudioWriteNR33(struct GBAudio* audio, uint8_t);
209void GBAudioWriteNR34(struct GBAudio* audio, uint8_t);
210
211void GBAudioWriteNR41(struct GBAudio* audio, uint8_t);
212void GBAudioWriteNR42(struct GBAudio* audio, uint8_t);
213void GBAudioWriteNR43(struct GBAudio* audio, uint8_t);
214void GBAudioWriteNR44(struct GBAudio* audio, uint8_t);
215
216void GBAudioWriteNR50(struct GBAudio* audio, uint8_t);
217void GBAudioWriteNR51(struct GBAudio* audio, uint8_t);
218void GBAudioWriteNR52(struct GBAudio* audio, uint8_t);
219
220int32_t GBAudioProcessEvents(struct GBAudio* audio, int32_t cycles);
221void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right);
222
223#endif