all repos — mgba @ 5b50c43857a5aa5bcefc7b339648bfc73d74fc2f

mGBA Game Boy Advance Emulator

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 GBAudioSweep {
 92	int shift;
 93	int time;
 94	int step;
 95	bool direction;
 96	bool enable;
 97	bool occurred;
 98	int realFrequency;
 99};
100
101struct GBAudioSquareChannel {
102	struct GBAudioSweep sweep;
103	struct GBAudioEnvelope envelope;
104	struct GBAudioSquareControl control;
105	int8_t sample;
106};
107
108struct GBAudioWaveChannel {
109	bool size;
110	bool bank;
111	bool enable;
112
113	unsigned length;
114	int volume;
115
116	int rate;
117	bool stop;
118
119	int window;
120	bool readable;
121	union {
122		uint32_t wavedata32[8];
123		uint8_t wavedata8[16];
124	};
125	int8_t sample;
126};
127
128struct GBAudioNoiseChannel {
129	struct GBAudioEnvelope envelope;
130
131	int ratio;
132	int frequency;
133	bool power;
134	bool stop;
135	int length;
136
137	uint32_t lfsr;
138	int8_t sample;
139};
140
141enum GBAudioStyle {
142	GB_AUDIO_DMG,
143	GB_AUDIO_CGB,
144	GB_AUDIO_AGB, // GB in GBA
145	GB_AUDIO_GBA, // GBA PSG
146};
147
148struct GBAudio {
149	struct GB* p;
150	struct GBAudioSquareChannel ch1;
151	struct GBAudioSquareChannel ch2;
152	struct GBAudioWaveChannel ch3;
153	struct GBAudioNoiseChannel ch4;
154
155	blip_t* left;
156	blip_t* right;
157	int16_t lastLeft;
158	int16_t lastRight;
159	int clock;
160	int32_t clockRate;
161
162	uint8_t volumeRight;
163	uint8_t volumeLeft;
164	bool ch1Right;
165	bool ch2Right;
166	bool ch3Right;
167	bool ch4Right;
168	bool ch1Left;
169	bool ch2Left;
170	bool ch3Left;
171	bool ch4Left;
172
173	bool playingCh1;
174	bool playingCh2;
175	bool playingCh3;
176	bool playingCh4;
177	uint8_t* nr52;
178
179	int32_t nextEvent;
180	int32_t eventDiff;
181	int32_t nextFrame;
182	int frame;
183	int32_t nextSample;
184
185	int32_t sampleInterval;
186	enum GBAudioStyle style;
187
188	int32_t nextCh1;
189	int32_t nextCh2;
190	int32_t nextCh3;
191	int32_t fadeCh3;
192	int32_t nextCh4;
193	bool enable;
194
195	size_t samples;
196	bool forceDisableCh[4];
197	int masterVolume;
198};
199
200void GBAudioInit(struct GBAudio* audio, size_t samples, uint8_t* nr52, enum GBAudioStyle style);
201void GBAudioDeinit(struct GBAudio* audio);
202void GBAudioReset(struct GBAudio* audio);
203
204void GBAudioResizeBuffer(struct GBAudio* audio, size_t samples);
205
206void GBAudioWriteNR10(struct GBAudio* audio, uint8_t);
207void GBAudioWriteNR11(struct GBAudio* audio, uint8_t);
208void GBAudioWriteNR12(struct GBAudio* audio, uint8_t);
209void GBAudioWriteNR13(struct GBAudio* audio, uint8_t);
210void GBAudioWriteNR14(struct GBAudio* audio, uint8_t);
211
212void GBAudioWriteNR21(struct GBAudio* audio, uint8_t);
213void GBAudioWriteNR22(struct GBAudio* audio, uint8_t);
214void GBAudioWriteNR23(struct GBAudio* audio, uint8_t);
215void GBAudioWriteNR24(struct GBAudio* audio, uint8_t);
216
217void GBAudioWriteNR30(struct GBAudio* audio, uint8_t);
218void GBAudioWriteNR31(struct GBAudio* audio, uint8_t);
219void GBAudioWriteNR32(struct GBAudio* audio, uint8_t);
220void GBAudioWriteNR33(struct GBAudio* audio, uint8_t);
221void GBAudioWriteNR34(struct GBAudio* audio, uint8_t);
222
223void GBAudioWriteNR41(struct GBAudio* audio, uint8_t);
224void GBAudioWriteNR42(struct GBAudio* audio, uint8_t);
225void GBAudioWriteNR43(struct GBAudio* audio, uint8_t);
226void GBAudioWriteNR44(struct GBAudio* audio, uint8_t);
227
228void GBAudioWriteNR50(struct GBAudio* audio, uint8_t);
229void GBAudioWriteNR51(struct GBAudio* audio, uint8_t);
230void GBAudioWriteNR52(struct GBAudio* audio, uint8_t);
231
232int32_t GBAudioProcessEvents(struct GBAudio* audio, int32_t cycles);
233void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right);
234
235struct GBSerializedPSGState;
236void GBAudioPSGSerialize(const struct GBAudio* audio, struct GBSerializedPSGState* state, uint32_t* flagsOut);
237void GBAudioPSGDeserialize(struct GBAudio* audio, const struct GBSerializedPSGState* state, const uint32_t* flagsIn);
238
239struct GBSerializedState;
240void GBAudioSerialize(const struct GBAudio* audio, struct GBSerializedState* state);
241void GBAudioDeserialize(struct GBAudio* audio, const struct GBSerializedState* state);
242
243#endif