all repos — mgba @ b6f3a2929d9fe2a7ca31a0f2d420031898196216

mGBA Game Boy Advance Emulator

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