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
103struct GBAAudioFIFO {
104 struct CircleBuffer fifo;
105 int dmaSource;
106 int8_t sample;
107};
108
109struct GBAAudio {
110 struct GBA* p;
111
112 struct GBAAudioChannel1 ch1;
113 struct GBAAudioChannel2 ch2;
114 struct GBAAudioChannel3 ch3;
115 struct GBAAudioChannel4 ch4;
116
117 struct GBAAudioFIFO chA;
118 struct GBAAudioFIFO chB;
119
120 struct CircleBuffer left;
121 struct CircleBuffer right;
122
123 union {
124 struct {
125 unsigned volumeRight : 3;
126 unsigned : 1;
127 unsigned volumeLeft : 3;
128 unsigned : 1;
129 unsigned ch1Right : 1;
130 unsigned ch2Right : 1;
131 unsigned ch3Right : 1;
132 unsigned ch4Right : 1;
133 unsigned ch1Left : 1;
134 unsigned ch2Left : 1;
135 unsigned ch3Left : 1;
136 unsigned ch4Left : 1;
137 };
138 uint16_t soundcntLo;
139 };
140
141 union {
142 struct {
143 unsigned volume : 2;
144 unsigned volumeChA : 1;
145 unsigned volumeChB : 1;
146 unsigned : 4;
147 unsigned chARight : 1;
148 unsigned chALeft : 1;
149 unsigned chATimer : 1;
150 unsigned chAReset : 1;
151 unsigned chBRight : 1;
152 unsigned chBLeft : 1;
153 unsigned chBTimer : 1;
154 unsigned chBReset : 1;
155 };
156 uint16_t soundcntHi;
157 };
158
159 union {
160 struct {
161 unsigned playingCh1 : 1;
162 unsigned playingCh2 : 1;
163 unsigned playingCh3 : 1;
164 unsigned playingCh4 : 1;
165 unsigned : 3;
166 unsigned enable : 1;
167 unsigned : 8;
168 };
169 uint16_t soundcntX;
170 };
171
172 unsigned sampleRate;
173
174 int32_t nextEvent;
175 int32_t eventDiff;
176 int32_t nextSample;
177
178 int32_t sampleInterval;
179
180 pthread_mutex_t bufferMutex;
181};
182
183void GBAAudioInit(struct GBAAudio* audio);
184void GBAAudioDeinit(struct GBAAudio* audio);
185
186int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles);
187void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info);
188
189void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value);
190void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value);
191void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value);
192void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value);
193void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value);
194void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value);
195void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value);
196void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value);
197void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value);
198void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value);
199void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value);
200void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value);
201void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value);
202
203void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value);
204void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value);
205void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId);
206
207#endif