Debian Codegraph @

android/app/jni/extern/SAASound/src/SAADevice.cpp

raw
1 // Part of SAASound copyright 2020 Dave Hooper <[email protected]>
2 //
3 // SAADevice.cpp: connecting the subcomponents of the SAA1099 together.
4 // This class handles device inputs and outputs (clocking, data and
5 // address bus, and simulated output)
6 //
7 //////////////////////////////////////////////////////////////////////
8
9 #include "SAASound.h"
10 #include "types.h"
11 #include "SAAEnv.h"
12 #include "SAANoise.h"
13 #include "SAAFreq.h"
14 #include "SAAAmp.h"
15 #include "SAASound.h"
16 #include "SAAImpl.h"
17 #include "defns.h"
18
19
20 //////////////////////////////////////////////////////////////////////
21 // Construction/Destruction
22 //////////////////////////////////////////////////////////////////////
23
24 CSAADevice::CSAADevice()
25 :
26 m_nCurrentSaaReg(0),
27 m_bOutputEnabled(false),
28 m_bSync(false),
29 m_nOversample(0),
30 m_Noise0(0xffffffff),
31 m_Noise1(0xffffffff),
32 m_Env0(),
33 m_Env1(),
34 m_Osc0(&m_Noise0, NULL),
35 m_Osc1(NULL, &m_Env0),
36 m_Osc2(NULL, NULL),
37 m_Osc3(&m_Noise1, NULL),
38 m_Osc4(NULL, &m_Env1),
39 m_Osc5(NULL, NULL),
40 m_Amp0(&m_Osc0, &m_Noise0, NULL),
41 m_Amp1(&m_Osc1, &m_Noise0, NULL),
42 m_Amp2(&m_Osc2, &m_Noise0, &m_Env0),
43 m_Amp3(&m_Osc3, &m_Noise1, NULL),
44 m_Amp4(&m_Osc4, &m_Noise1, NULL),
45 m_Amp5(&m_Osc5, &m_Noise1, &m_Env1)
46 {
47 // Create and link up the objects that make up the emulator
48 Noise[0] = &m_Noise0;
49 Noise[1] = &m_Noise1;
50 Env[0] = &m_Env0;
51 Env[1] = &m_Env1;
52
53 // Create oscillators (tone generators) and link to noise generators and
54 // envelope controllers
55 Osc[0] = &m_Osc0;
56 Osc[1] = &m_Osc1;
57 Osc[2] = &m_Osc2;
58 Osc[3] = &m_Osc3;
59 Osc[4] = &m_Osc4;
60 Osc[5] = &m_Osc5;
61
62 // Create amplification/mixing stages and link to appropriate oscillators,
63 // noise generators and envelope controllers
64 Amp[0] = &m_Amp0;
65 Amp[1] = &m_Amp1;
66 Amp[2] = &m_Amp2;
67 Amp[3] = &m_Amp3;
68 Amp[4] = &m_Amp4;
69 Amp[5] = &m_Amp5;
70
71 _SetClockRate(EXTERNAL_CLK_HZ);
72 _SetOversample(DEFAULT_OVERSAMPLE);
73 }
74
75 CSAADevice::~CSAADevice()
76 {
77 }
78
79 //////////////////////////////////////////////////////////////////////
80 // CSAASound members
81 //////////////////////////////////////////////////////////////////////
82
83 void CSAADevice::_SetClockRate(unsigned int nClockRate)
84 {
85 m_Osc0._SetClockRate(nClockRate);
86 m_Osc1._SetClockRate(nClockRate);
87 m_Osc2._SetClockRate(nClockRate);
88 m_Osc3._SetClockRate(nClockRate);
89 m_Osc4._SetClockRate(nClockRate);
90 m_Osc5._SetClockRate(nClockRate);
91 m_Noise0._SetClockRate(nClockRate);
92 m_Noise1._SetClockRate(nClockRate);
93 }
94
95 void CSAADevice::_SetSampleRate(unsigned int nSampleRate)
96 {
97 m_Osc0._SetSampleRate(nSampleRate);
98 m_Osc1._SetSampleRate(nSampleRate);
99 m_Osc2._SetSampleRate(nSampleRate);
100 m_Osc3._SetSampleRate(nSampleRate);
101 m_Osc4._SetSampleRate(nSampleRate);
102 m_Osc5._SetSampleRate(nSampleRate);
103 m_Noise0._SetSampleRate(nSampleRate);
104 m_Noise1._SetSampleRate(nSampleRate);
105 }
106
107 void CSAADevice::_SetOversample(unsigned int nOversample)
108 {
109 if (nOversample != (unsigned int)m_nOversample)
110 {
111 m_nOversample = nOversample;
112 m_Osc0._SetOversample(nOversample);
113 m_Osc1._SetOversample(nOversample);
114 m_Osc2._SetOversample(nOversample);
115 m_Osc3._SetOversample(nOversample);
116 m_Osc4._SetOversample(nOversample);
117 m_Osc5._SetOversample(nOversample);
118 m_Noise0._SetOversample(nOversample);
119 m_Noise1._SetOversample(nOversample);
120 }
121 }
122
123 void CSAADevice::_WriteData(BYTE nData)
124 {
125 #if defined(DEBUG) || defined(DEBUGSAA)
126 m_Reg[m_nCurrentSaaReg] = nData;
127 #endif
128
129 // route nData to the appropriate place
130 switch (m_nCurrentSaaReg)
131 {
132 // Amplitude data (==> Amp)
133 case 0:
134 m_Amp0.SetAmpLevel(nData);
135 break;
136 case 1:
137 m_Amp1.SetAmpLevel(nData);
138 break;
139 case 2:
140 m_Amp2.SetAmpLevel(nData);
141 break;
142 case 3:
143 m_Amp3.SetAmpLevel(nData);
144 break;
145 case 4:
146 m_Amp4.SetAmpLevel(nData);
147 break;
148 case 5:
149 m_Amp5.SetAmpLevel(nData);
150 break;
151
152 // Freq data (==> Osc)
153 case 8:
154 m_Osc0.SetFreqOffset(nData);
155 break;
156 case 9:
157 m_Osc1.SetFreqOffset(nData);
158 break;
159 case 10:
160 m_Osc2.SetFreqOffset(nData);
161 break;
162 case 11:
163 m_Osc3.SetFreqOffset(nData);
164 break;
165 case 12:
166 m_Osc4.SetFreqOffset(nData);
167 break;
168 case 13:
169 m_Osc5.SetFreqOffset(nData);
170 break;
171
172 // Freq octave data (==> Osc) for channels 0,1
173 case 16:
174 m_Osc0.SetFreqOctave(nData & 0x07);
175 m_Osc1.SetFreqOctave((nData >> 4) & 0x07);
176 break;
177
178 // Freq octave data (==> Osc) for channels 2,3
179 case 17:
180 m_Osc2.SetFreqOctave(nData & 0x07);
181 m_Osc3.SetFreqOctave((nData >> 4) & 0x07);
182 break;
183
184 // Freq octave data (==> Osc) for channels 4,5
185 case 18:
186 m_Osc4.SetFreqOctave(nData & 0x07);
187 m_Osc5.SetFreqOctave((nData >> 4) & 0x07);
188 break;
189
190 // Tone mixer control (==> Amp)
191 case 20:
192 m_Amp0.SetToneMixer(nData & 0x01);
193 m_Amp1.SetToneMixer(nData & 0x02);
194 m_Amp2.SetToneMixer(nData & 0x04);
195 m_Amp3.SetToneMixer(nData & 0x08);
196 m_Amp4.SetToneMixer(nData & 0x10);
197 m_Amp5.SetToneMixer(nData & 0x20);
198 break;
199
200 // Noise mixer control (==> Amp)
201 case 21:
202 m_Amp0.SetNoiseMixer(nData & 0x01);
203 m_Amp1.SetNoiseMixer(nData & 0x02);
204 m_Amp2.SetNoiseMixer(nData & 0x04);
205 m_Amp3.SetNoiseMixer(nData & 0x08);
206 m_Amp4.SetNoiseMixer(nData & 0x10);
207 m_Amp5.SetNoiseMixer(nData & 0x20);
208 break;
209
210 // Noise frequency/source control (==> Noise)
211 case 22:
212 m_Noise0.SetSource(nData & 0x03);
213 m_Noise1.SetSource((nData >> 4) & 0x03);
214 break;
215
216 // Envelope control data (==> Env) for envelope controller #0
217 case 24:
218 m_Env0.SetEnvControl(nData);
219 break;
220
221 // Envelope control data (==> Env) for envelope controller #1
222 case 25:
223 m_Env1.SetEnvControl(nData);
224 break;
225
226 // Global enable and reset (sync) controls
227 case 28:
228 {
229 // Reset (sync) bit
230 bool bSync = bool(nData & 0x02);
231 if (bSync != m_bSync)
232 {
233 // Sync all devices
234 // This amounts to telling them all to reset to a
235 // known state, which is also a state that doesn't change
236 // (i.e. no audio output, although there are some exceptions)
237 // bSync=true => all devices are sync (aka reset);
238 // bSync=false => all devices are allowed to run and generate changing output
239 m_Osc0.Sync(bSync);
240 m_Osc1.Sync(bSync);
241 m_Osc2.Sync(bSync);
242 m_Osc3.Sync(bSync);
243 m_Osc4.Sync(bSync);
244 m_Osc5.Sync(bSync);
245 m_Noise0.Sync(bSync);
246 m_Noise1.Sync(bSync);
247 m_Amp0.Sync(bSync);
248 m_Amp1.Sync(bSync);
249 m_Amp2.Sync(bSync);
250 m_Amp3.Sync(bSync);
251 m_Amp4.Sync(bSync);
252 m_Amp5.Sync(bSync);
253 m_bSync = bSync;
254 }
255
256 // Global mute bit
257 bool bOutputEnabled = bool(nData & 0x01);
258 if (bOutputEnabled != m_bOutputEnabled)
259 {
260 // unmute all amps - sound 'enabled'
261 m_Amp0.Mute(!bOutputEnabled);
262 m_Amp1.Mute(!bOutputEnabled);
263 m_Amp2.Mute(!bOutputEnabled);
264 m_Amp3.Mute(!bOutputEnabled);
265 m_Amp4.Mute(!bOutputEnabled);
266 m_Amp5.Mute(!bOutputEnabled);
267 m_bOutputEnabled = bOutputEnabled;
268 }
269 }
270 break;
271
272 default:
273 // anything else means data is being written to a register
274 // that is not used within the SAA-1099 architecture
275 // hence, we ignore it.
276 {}
277 }
278 }
279
280 void CSAADevice::_WriteAddress(BYTE nReg)
281 {
282 m_nCurrentSaaReg = nReg & 31;
283 if (m_nCurrentSaaReg == 24)
284 {
285 m_Env0.ExternalClock();
286 }
287 else if (m_nCurrentSaaReg == 25)
288 {
289 m_Env1.ExternalClock();
290 }
291 }
292
293 #if defined(DEBUG) || defined(DEBUGSAA) || defined(USE_CONFIG_FILE)
294 BYTE CSAADevice::_ReadAddress(void)
295 {
296 // Not a real hardware function of the SAA-1099, which is write-only
297 // However, this is used by SAAImpl to generate debug logs (if enabled)
298 return(m_nCurrentSaaReg);
299 }
300 #endif
301 #if defined(DEBUG)
302 BYTE CSAADevice::_ReadData(void)
303 {
304 // Not a real hardware function of the SAA-1099, which is write-only
305 // This is only compiled for Debug builds
306 return(m_Reg[m_nCurrentSaaReg]);
307 }
308 #endif
309
310 void CSAADevice::_TickAndOutputStereo(unsigned int& left_mixed, unsigned int& right_mixed, DivDispatchOscBuffer** oscBuf, unsigned int where)
311 {
312 unsigned int temp_left, temp_right;
313 unsigned int accum_left = 0, accum_right = 0;
314 for (int i = 1 << m_nOversample; i > 0; i--)
315 {
316 m_Noise0.Tick();
317 m_Noise1.Tick();
318 m_Amp0.TickAndOutputStereo(temp_left, temp_right);
319 oscBuf[0]->putSample(where,(temp_left+temp_right)<<5);
320 accum_left += temp_left;
321 accum_right += temp_right;
322 m_Amp1.TickAndOutputStereo(temp_left, temp_right);
323 oscBuf[1]->putSample(where,(temp_left+temp_right)<<5);
324 accum_left += temp_left;
325 accum_right += temp_right;
326 m_Amp2.TickAndOutputStereo(temp_left, temp_right);
327 oscBuf[2]->putSample(where,(temp_left+temp_right)<<5);
328 accum_left += temp_left;
329 accum_right += temp_right;
330 m_Amp3.TickAndOutputStereo(temp_left, temp_right);
331 oscBuf[3]->putSample(where,(temp_left+temp_right)<<5);
332 accum_left += temp_left;
333 accum_right += temp_right;
334 m_Amp4.TickAndOutputStereo(temp_left, temp_right);
335 oscBuf[4]->putSample(where,(temp_left+temp_right)<<5);
336 accum_left += temp_left;
337 accum_right += temp_right;
338 m_Amp5.TickAndOutputStereo(temp_left, temp_right);
339 oscBuf[5]->putSample(where,(temp_left+temp_right)<<5);
340 accum_left += temp_left;
341 accum_right += temp_right;
342 }
343 left_mixed = accum_left;
344 right_mixed = accum_right;
345 }
346
347 void CSAADevice::_TickAndOutputSeparate(unsigned int& left_mixed, unsigned int& right_mixed,
348 unsigned int& left0, unsigned int& right0,
349 unsigned int& left1, unsigned int& right1,
350 unsigned int& left2, unsigned int& right2,
351 unsigned int& left3, unsigned int& right3,
352 unsigned int& left4, unsigned int& right4,
353 unsigned int& left5, unsigned int& right5
354 )
355 {
356 unsigned int temp_left, temp_right;
357 unsigned int accum_left = 0, accum_right = 0;
358 left0 = left1 = left2 = left3 = left4 = left5 = 0;
359 right0 = right1 = right2 = right3 = right4 = right5 = 0;
360 for (int i = 1 << m_nOversample; i > 0; i--)
361 {
362 m_Noise0.Tick();
363 m_Noise1.Tick();
364 m_Amp0.TickAndOutputStereo(temp_left, temp_right);
365 left0 += temp_left;
366 right0 += temp_right;
367 accum_left += temp_left;
368 accum_right += temp_right;
369 m_Amp1.TickAndOutputStereo(temp_left, temp_right);
370 left1 += temp_left;
371 right1 += temp_right;
372 accum_left += temp_left;
373 accum_right += temp_right;
374 m_Amp2.TickAndOutputStereo(temp_left, temp_right);
375 left2 += temp_left;
376 right2 += temp_right;
377 accum_left += temp_left;
378 accum_right += temp_right;
379 m_Amp3.TickAndOutputStereo(temp_left, temp_right);
380 left3 += temp_left;
381 right3 += temp_right;
382 accum_left += temp_left;
383 accum_right += temp_right;
384 m_Amp4.TickAndOutputStereo(temp_left, temp_right);
385 left4 += temp_left;
386 right4 += temp_right;
387 accum_left += temp_left;
388 accum_right += temp_right;
389 m_Amp5.TickAndOutputStereo(temp_left, temp_right);
390 left5 += temp_left;
391 right5 += temp_right;
392 accum_left += temp_left;
393 accum_right += temp_right;
394 }
395 left_mixed = accum_left;
396 right_mixed = accum_right;
397 }