all repos — mgba @ 76f02be7577549f92da7a4285679eacb471d1345

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 <pthread.h>
  7#include <stdint.h>
  8
  9struct GBADMA;
 10
 11const unsigned GBA_AUDIO_SAMPLES;
 12
 13struct GBAAudioEnvelope {
 14	union {
 15		struct {
 16			unsigned length : 6;
 17			unsigned duty : 2;
 18			unsigned stepTime : 3;
 19			unsigned direction : 1;
 20			unsigned initialVolume : 4;
 21		};
 22		uint16_t packed;
 23	};
 24	int currentVolume;
 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};
 41
 42struct GBAAudioChannel1 {
 43	union GBAAudioSquareSweep {
 44		struct {
 45			unsigned shift : 3;
 46			unsigned direction : 1;
 47			unsigned time : 3;
 48			unsigned : 9;
 49		};
 50		uint16_t packed;
 51	} sweep;
 52	int32_t nextSweep;
 53	int playing;
 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	union {
 88		struct {
 89			unsigned rate : 11;
 90			unsigned : 3;
 91			unsigned stop : 1;
 92			unsigned restart : 1;
 93		};
 94		uint16_t packed;
 95	} control;
 96
 97	uint32_t wavedata[8];
 98	int8_t sample;
 99};
100
101struct GBAAudioChannel4 {
102	struct GBAAudioEnvelope envelope;
103	union {
104		struct {
105			unsigned ratio : 3;
106			unsigned power : 1;
107			unsigned frequency : 4;
108			unsigned : 6;
109			unsigned stop : 1;
110			unsigned restart : 1;
111		};
112		uint16_t packed;
113	} control;
114
115	unsigned lfsr;
116	int8_t sample;
117};
118
119struct GBAAudioFIFO {
120	struct CircleBuffer fifo;
121	int dmaSource;
122	int8_t sample;
123};
124
125struct GBAAudio {
126	struct GBA* p;
127
128	struct GBAAudioChannel1 ch1;
129	struct GBAAudioChannel2 ch2;
130	struct GBAAudioChannel3 ch3;
131	struct GBAAudioChannel4 ch4;
132
133	struct GBAAudioFIFO chA;
134	struct GBAAudioFIFO chB;
135
136	struct CircleBuffer left;
137	struct CircleBuffer right;
138
139	union {
140		struct {
141			unsigned volumeRight : 3;
142			unsigned : 1;
143			unsigned volumeLeft : 3;
144			unsigned : 1;
145			unsigned ch1Right : 1;
146			unsigned ch2Right : 1;
147			unsigned ch3Right : 1;
148			unsigned ch4Right : 1;
149			unsigned ch1Left : 1;
150			unsigned ch2Left : 1;
151			unsigned ch3Left : 1;
152			unsigned ch4Left : 1;
153		};
154		uint16_t soundcntLo;
155	};
156
157	union {
158		struct {
159			unsigned volume : 2;
160			unsigned volumeChA : 1;
161			unsigned volumeChB : 1;
162			unsigned : 4;
163			unsigned chARight : 1;
164			unsigned chALeft : 1;
165			unsigned chATimer : 1;
166			unsigned chAReset : 1;
167			unsigned chBRight : 1;
168			unsigned chBLeft : 1;
169			unsigned chBTimer : 1;
170			unsigned chBReset : 1;
171		};
172		uint16_t soundcntHi;
173	};
174
175	union {
176		struct {
177			unsigned playingCh1 : 1;
178			unsigned playingCh2 : 1;
179			unsigned playingCh3 : 1;
180			unsigned playingCh4 : 1;
181			unsigned : 3;
182			unsigned enable : 1;
183			unsigned : 8;
184		};
185		uint16_t soundcntX;
186	};
187
188	unsigned sampleRate;
189
190	int32_t nextEvent;
191	int32_t eventDiff;
192	int32_t nextCh1;
193	int32_t nextCh2;
194	int32_t nextCh3;
195	int32_t nextCh4;
196	int32_t nextSample;
197
198	int32_t sampleInterval;
199
200	pthread_mutex_t bufferMutex;
201};
202
203void GBAAudioInit(struct GBAAudio* audio);
204void GBAAudioDeinit(struct GBAAudio* audio);
205
206int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles);
207void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info);
208
209void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value);
210void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value);
211void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value);
212void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value);
213void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value);
214void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value);
215void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value);
216void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value);
217void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value);
218void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value);
219void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value);
220void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value);
221void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value);
222
223void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value);
224void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value);
225void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId);
226
227#endif