all repos — mgba @ 3c100a5e310f6031fb156cd8d9da262d6d1da663

mGBA Game Boy Advance Emulator

src/gba/gba-audio.h (view raw)

  1#ifndef GBA_AUDIO_H
  2#define GBA_AUDIO_H
  3
  4#include "circle-buffer.h"
  5
  6#include <stdint.h>
  7
  8struct GBADMA;
  9
 10const unsigned GBA_AUDIO_SAMPLES;
 11
 12struct GBAAudioEnvelope {
 13	union {
 14		struct {
 15			unsigned length : 6;
 16			unsigned duty : 2;
 17			unsigned stepTime : 3;
 18			unsigned direction : 1;
 19			unsigned initialVolume : 4;
 20		};
 21		uint16_t packed;
 22	};
 23	int currentVolume;
 24	int dead;
 25	int32_t nextStep;
 26};
 27
 28struct GBAAudioSquareControl {
 29	union {
 30		struct {
 31			unsigned frequency : 11;
 32			unsigned : 3;
 33			unsigned stop : 1;
 34			unsigned restart : 1;
 35		};
 36		uint16_t packed;
 37	};
 38	int hi;
 39	int32_t nextStep;
 40	int32_t endTime;
 41};
 42
 43struct GBAAudioChannel1 {
 44	union GBAAudioSquareSweep {
 45		struct {
 46			unsigned shift : 3;
 47			unsigned direction : 1;
 48			unsigned time : 3;
 49			unsigned : 9;
 50		};
 51		uint16_t packed;
 52	} sweep;
 53	int32_t nextSweep;
 54
 55	struct GBAAudioEnvelope envelope;
 56	struct GBAAudioSquareControl control;
 57	int8_t sample;
 58};
 59
 60struct GBAAudioChannel2 {
 61	struct GBAAudioEnvelope envelope;
 62	struct GBAAudioSquareControl control;
 63	int8_t sample;
 64};
 65
 66struct GBAAudioChannel3 {
 67	union {
 68		struct {
 69			unsigned : 5;
 70			unsigned size : 1;
 71			unsigned bank : 1;
 72			unsigned enable : 1;
 73			unsigned : 7;
 74		};
 75		uint16_t packed;
 76	} bank;
 77
 78	union {
 79		struct {
 80			unsigned length : 8;
 81			unsigned : 5;
 82			unsigned volume : 3;
 83		};
 84		uint16_t packed;
 85	} wave;
 86
 87	struct {
 88		union {
 89			struct {
 90				unsigned rate : 11;
 91				unsigned : 3;
 92				unsigned stop : 1;
 93				unsigned restart : 1;
 94			};
 95			uint16_t packed;
 96		};
 97		int32_t endTime;
 98	} control;
 99
100	uint32_t wavedata[8];
101	int8_t sample;
102};
103
104struct GBAAudioChannel4 {
105	struct GBAAudioEnvelope envelope;
106	struct {
107		union {
108			struct {
109				unsigned ratio : 3;
110				unsigned power : 1;
111				unsigned frequency : 4;
112				unsigned : 6;
113				unsigned stop : 1;
114				unsigned restart : 1;
115			};
116			uint16_t packed;
117		};
118		int32_t endTime;
119	} control;
120
121	unsigned lfsr;
122	int8_t sample;
123};
124
125struct GBAAudioFIFO {
126	struct CircleBuffer fifo;
127	int dmaSource;
128	int8_t sample;
129};
130
131struct GBAAudio {
132	struct GBA* p;
133
134	struct GBAAudioChannel1 ch1;
135	struct GBAAudioChannel2 ch2;
136	struct GBAAudioChannel3 ch3;
137	struct GBAAudioChannel4 ch4;
138
139	struct GBAAudioFIFO chA;
140	struct GBAAudioFIFO chB;
141
142	struct CircleBuffer left;
143	struct CircleBuffer right;
144
145	union {
146		struct {
147			unsigned volumeRight : 3;
148			unsigned : 1;
149			unsigned volumeLeft : 3;
150			unsigned : 1;
151			unsigned ch1Right : 1;
152			unsigned ch2Right : 1;
153			unsigned ch3Right : 1;
154			unsigned ch4Right : 1;
155			unsigned ch1Left : 1;
156			unsigned ch2Left : 1;
157			unsigned ch3Left : 1;
158			unsigned ch4Left : 1;
159		};
160		uint16_t soundcntLo;
161	};
162
163	union {
164		struct {
165			unsigned volume : 2;
166			unsigned volumeChA : 1;
167			unsigned volumeChB : 1;
168			unsigned : 4;
169			unsigned chARight : 1;
170			unsigned chALeft : 1;
171			unsigned chATimer : 1;
172			unsigned chAReset : 1;
173			unsigned chBRight : 1;
174			unsigned chBLeft : 1;
175			unsigned chBTimer : 1;
176			unsigned chBReset : 1;
177		};
178		uint16_t soundcntHi;
179	};
180
181	union {
182		struct {
183			unsigned playingCh1 : 1;
184			unsigned playingCh2 : 1;
185			unsigned playingCh3 : 1;
186			unsigned playingCh4 : 1;
187			unsigned : 3;
188			unsigned enable : 1;
189			unsigned : 8;
190		};
191		uint16_t soundcntX;
192	};
193
194	unsigned sampleRate;
195
196	int32_t nextEvent;
197	int32_t eventDiff;
198	int32_t nextCh1;
199	int32_t nextCh2;
200	int32_t nextCh3;
201	int32_t nextCh4;
202	int32_t nextSample;
203
204	int32_t sampleInterval;
205};
206
207void GBAAudioInit(struct GBAAudio* audio);
208void GBAAudioDeinit(struct GBAAudio* audio);
209
210int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles);
211void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info);
212
213void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value);
214void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value);
215void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value);
216void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value);
217void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value);
218void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value);
219void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value);
220void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value);
221void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value);
222void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value);
223void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value);
224void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value);
225void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value);
226
227void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value);
228void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value);
229void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId);
230
231unsigned GBAAudioCopy(struct GBAAudio* audio, void* left, void* right, unsigned nSamples);
232
233#endif