all repos — mgba @ 7393351e279a5be8cd259ab30d1d79ea3636bea4

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