Debian Codegraph @

src/engine/platform/saa.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "saa.h"
21 #include "../engine.h"
22 #include "../bsr.h"
23 #include "sound/saa1099.h"
24 #include <string.h>
25 #include <math.h>
26
27 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
28
29 #define CHIP_DIVIDER 2
30
31 const char* regCheatSheetSAA[]={
32 "Vol0", "00",
33 "Vol1", "01",
34 "Vol2", "02",
35 "Vol3", "03",
36 "Vol4", "04",
37 "Vol5", "05",
38 "Freq0", "08",
39 "Freq1", "09",
40 "Freq2", "0A",
41 "Freq3", "0B",
42 "Freq4", "0C",
43 "Freq5", "0D",
44 "Octave10", "10",
45 "Octave32", "11",
46 "Octave54", "12",
47 "ToneOn", "14",
48 "NoiseOn", "15",
49 "NoiseCtl", "16",
50 "EnvCtl0", "18",
51 "EnvCtl1", "19",
52 "Power", "1C",
53 NULL
54 };
55
56 const char** DivPlatformSAA1099::getRegisterSheet() {
57 return regCheatSheetSAA;
58 }
59
60 void DivPlatformSAA1099::acquire_saaSound(short** buf, size_t len) {
61 if (saaBufLen<len*2) {
62 saaBufLen=len*2;
63 for (int i=0; i<2; i++) {
64 delete[] saaBuf[i];
65 saaBuf[i]=new short[saaBufLen];
66 }
67 }
68 while (!writes.empty()) {
69 QueuedWrite w=writes.front();
70 saa_saaSound->WriteAddressData(w.addr,w.val);
71 regPool[w.addr&0x1f]=w.val;
72 writes.pop();
73 }
74 for (int i=0; i<6; i++) {
75 oscBuf[i]->begin(len);
76 }
77 saa_saaSound->GenerateMany((unsigned char*)saaBuf[0],len,oscBuf);
78 for (int i=0; i<6; i++) {
79 oscBuf[i]->end(len);
80 }
81 #ifdef TA_BIG_ENDIAN
82 for (size_t i=0; i<len; i++) {
83 buf[0][i]=(short)((((unsigned short)saaBuf[0][i<<1])<<8)|(((unsigned short)saaBuf[0][i<<1])>>8));
84 buf[1][i]=(short)((((unsigned short)saaBuf[0][1+(i<<1)])<<8)|(((unsigned short)saaBuf[0][1+(i<<1)])>>8));
85 }
86 #else
87 for (size_t i=0; i<len; i++) {
88 buf[0][i]=saaBuf[0][i<<1];
89 buf[1][i]=saaBuf[0][1+(i<<1)];
90 }
91 #endif
92 }
93
94 void DivPlatformSAA1099::acquire(short** buf, size_t len) {
95 acquire_saaSound(buf,len);
96 }
97
98 inline unsigned char applyPan(unsigned char vol, unsigned char pan) {
99 return ((vol*(pan>>4))/15)|(((vol*(pan&15))/15)<<4);
100 }
101
102 void DivPlatformSAA1099::tick(bool sysTick) {
103 for (int i=0; i<6; i++) {
104 chan[i].std.next();
105 if (chan[i].std.vol.had) {
106 chan[i].outVol=VOL_SCALE_LINEAR_BROKEN(chan[i].vol&15,MIN(15,chan[i].std.vol.val),15);
107 if (chan[i].outVol<0) chan[i].outVol=0;
108 if (isMuted[i]) {
109 rWrite(i,0);
110 } else {
111 rWrite(i,applyPan(chan[i].outVol&15,chan[i].pan));
112 }
113 }
114 if (NEW_ARP_STRAT) {
115 chan[i].handleArp();
116 } else if (chan[i].std.arp.had) {
117 if (!chan[i].inPorta) {
118 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
119 }
120 chan[i].freqChanged=true;
121 }
122 if (chan[i].std.duty.had) {
123 saaNoise[i/3]=chan[i].std.duty.val&3;
124 rWrite(0x16,saaNoise[0]|(saaNoise[1]<<4));
125 }
126 if (chan[i].std.wave.had) {
127 chan[i].psgMode=chan[i].std.wave.val&3;
128 }
129 if (chan[i].std.panL.had) {
130 chan[i].pan&=0x0f;
131 chan[i].pan|=(chan[i].std.panL.val&15)<<4;
132 }
133
134 if (chan[i].std.panR.had) {
135 chan[i].pan&=0xf0;
136 chan[i].pan|=chan[i].std.panR.val&15;
137 }
138 if (chan[i].std.panL.had || chan[i].std.panR.had) {
139 if (isMuted[i]) {
140 rWrite(i,0);
141 } else {
142 if (chan[i].std.vol.had) {
143 if (chan[i].active) rWrite(i,applyPan(chan[i].outVol&15,chan[i].pan));
144 } else {
145 if (chan[i].active) rWrite(i,applyPan(chan[i].vol&15,chan[i].pan));
146 }
147 }
148 }
149 if (chan[i].std.pitch.had) {
150 if (chan[i].std.pitch.mode) {
151 chan[i].pitch2+=chan[i].std.pitch.val;
152 CLAMP_VAR(chan[i].pitch2,-32768,32767);
153 } else {
154 chan[i].pitch2=chan[i].std.pitch.val;
155 }
156 chan[i].freqChanged=true;
157 }
158 if (chan[i].std.ex1.had) {
159 saaEnv[i/3]=chan[i].std.ex1.val;
160 rWrite(0x18+(i/3),saaEnv[i/3]);
161 }
162 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
163 chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,true,0,chan[i].pitch2,chipClock,CHIP_DIVIDER);
164 if (chan[i].freq>65535) chan[i].freq=65535;
165 chan[i].freqH=0;
166 if (chan[i].freq>511) {
167 chan[i].freqH=bsr((unsigned short)chan[i].freq)-9;
168 }
169 chan[i].freqL=0xff-(chan[i].freq>>chan[i].freqH);
170 chan[i].freqH=7-chan[i].freqH;
171 if (chan[i].freq>4095) chan[i].freq=4095;
172 if (chan[i].keyOn) {
173 }
174 if (chan[i].keyOff) {
175 rWrite(i,0);
176 }
177 rWrite(0x08+i,chan[i].freqL);
178 rWrite(0x10+(i>>1),chan[i&6].freqH|(chan[1+(i&6)].freqH<<4));
179 if (chan[i].keyOn) chan[i].keyOn=false;
180 if (chan[i].keyOff) chan[i].keyOff=false;
181 chan[i].freqChanged=false;
182 }
183 }
184
185 rWrite(0x14,(chan[0].psgMode&1)|
186 ((chan[1].psgMode&1)<<1)|
187 ((chan[2].psgMode&1)<<2)|
188 ((chan[3].psgMode&1)<<3)|
189 ((chan[4].psgMode&1)<<4)|
190 ((chan[5].psgMode&1)<<5)
191 );
192
193 rWrite(0x15,((chan[0].psgMode&2)>>1)|
194 (chan[1].psgMode&2)|
195 ((chan[2].psgMode&2)<<1)|
196 ((chan[3].psgMode&2)<<2)|
197 ((chan[4].psgMode&2)<<3)|
198 ((chan[5].psgMode&2)<<4)
199 );
200 }
201
202 int DivPlatformSAA1099::dispatch(DivCommand c) {
203 switch (c.cmd) {
204 case DIV_CMD_NOTE_ON: {
205 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SAA1099);
206 if (c.value!=DIV_NOTE_NULL) {
207 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
208 chan[c.chan].freqChanged=true;
209 chan[c.chan].note=c.value;
210 }
211 chan[c.chan].active=true;
212 chan[c.chan].keyOn=true;
213 chan[c.chan].macroInit(ins);
214 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
215 chan[c.chan].outVol=chan[c.chan].vol;
216 }
217 if (isMuted[c.chan]) {
218 rWrite(c.chan,0);
219 } else {
220 rWrite(c.chan,applyPan(chan[c.chan].vol&15,chan[c.chan].pan));
221 }
222 break;
223 }
224 case DIV_CMD_NOTE_OFF:
225 chan[c.chan].keyOff=true;
226 chan[c.chan].active=false;
227 chan[c.chan].macroInit(NULL);
228 break;
229 case DIV_CMD_NOTE_OFF_ENV:
230 case DIV_CMD_ENV_RELEASE:
231 chan[c.chan].std.release();
232 break;
233 case DIV_CMD_VOLUME: {
234 chan[c.chan].vol=c.value;
235 if (!chan[c.chan].std.vol.has) {
236 chan[c.chan].outVol=c.value;
237 }
238 if (isMuted[c.chan]) {
239 rWrite(c.chan,0);
240 } else {
241 if (chan[c.chan].active) rWrite(c.chan,applyPan(chan[c.chan].vol&15,chan[c.chan].pan));
242 }
243 break;
244 }
245 case DIV_CMD_GET_VOLUME: {
246 return chan[c.chan].vol;
247 break;
248 }
249 case DIV_CMD_INSTRUMENT:
250 if (chan[c.chan].ins!=c.value || c.value2==1) {
251 chan[c.chan].insChanged=true;
252 }
253 chan[c.chan].ins=c.value;
254 break;
255 case DIV_CMD_PITCH: {
256 chan[c.chan].pitch=c.value;
257 chan[c.chan].freqChanged=true;
258 break;
259 }
260 case DIV_CMD_NOTE_PORTA: {
261 int destFreq=NOTE_PERIODIC(c.value2);
262 bool return2=false;
263 if (destFreq>chan[c.chan].baseFreq) {
264 chan[c.chan].baseFreq+=c.value*((parent->song.linearPitch==2)?1:(8-chan[c.chan].freqH));
265 if (chan[c.chan].baseFreq>=destFreq) {
266 chan[c.chan].baseFreq=destFreq;
267 return2=true;
268 }
269 } else {
270 chan[c.chan].baseFreq-=c.value*((parent->song.linearPitch==2)?1:(8-chan[c.chan].freqH));
271 if (chan[c.chan].baseFreq<=destFreq) {
272 chan[c.chan].baseFreq=destFreq;
273 return2=true;
274 }
275 }
276 chan[c.chan].freqChanged=true;
277 if (return2) {
278 chan[c.chan].inPorta=false;
279 return 2;
280 }
281 break;
282 }
283 case DIV_CMD_PANNING:
284 chan[c.chan].pan=(c.value&0xf0)|(c.value2>>4);
285 if (isMuted[c.chan]) {
286 rWrite(c.chan,0);
287 } else {
288 if (chan[c.chan].active) rWrite(c.chan,applyPan(chan[c.chan].vol&15,chan[c.chan].pan));
289 }
290 break;
291 case DIV_CMD_LEGATO: {
292 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
293 chan[c.chan].freqChanged=true;
294 break;
295 }
296 case DIV_CMD_STD_NOISE_MODE:
297 chan[c.chan].psgMode=(c.value&1)|((c.value&16)>>3);
298 break;
299 case DIV_CMD_STD_NOISE_FREQ:
300 saaNoise[c.chan/3]=(c.value&1)|((c.value&16)>>3);
301 rWrite(0x16,saaNoise[0]|(saaNoise[1]<<4));
302 break;
303 case DIV_CMD_SAA_ENVELOPE:
304 saaEnv[c.chan/3]=c.value;
305 rWrite(0x18+(c.chan/3),c.value);
306 break;
307 case DIV_CMD_MACRO_OFF:
308 chan[c.chan].std.mask(c.value,true);
309 break;
310 case DIV_CMD_MACRO_ON:
311 chan[c.chan].std.mask(c.value,false);
312 break;
313 case DIV_CMD_MACRO_RESTART:
314 chan[c.chan].std.restart(c.value);
315 break;
316 case DIV_CMD_GET_VOLMAX:
317 return 15;
318 break;
319 case DIV_CMD_PRE_PORTA:
320 if (chan[c.chan].active && c.value2) {
321 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_SAA1099));
322 }
323 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_PERIODIC(chan[c.chan].note);
324 chan[c.chan].inPorta=c.value;
325 break;
326 case DIV_CMD_PRE_NOTE:
327 break;
328 default:
329 //printf("WARNING: unimplemented command %d\n",c.cmd);
330 break;
331 }
332 return 1;
333 }
334
335 void DivPlatformSAA1099::muteChannel(int ch, bool mute) {
336 isMuted[ch]=mute;
337 if (isMuted[ch]) {
338 rWrite(ch,0);
339 } else {
340 if (chan[ch].active) rWrite(ch,applyPan(chan[ch].outVol&15,chan[ch].pan));
341 }
342 }
343
344 void DivPlatformSAA1099::forceIns() {
345 for (int i=0; i<6; i++) {
346 chan[i].insChanged=true;
347 chan[i].freqChanged=true;
348 }
349 rWrite(0x18,saaEnv[0]);
350 rWrite(0x19,saaEnv[1]);
351 rWrite(0x16,saaNoise[0]|(saaNoise[1]<<4));
352 }
353
354 void* DivPlatformSAA1099::getChanState(int ch) {
355 return &chan[ch];
356 }
357
358 DivMacroInt* DivPlatformSAA1099::getChanMacroInt(int ch) {
359 return &chan[ch].std;
360 }
361
362 unsigned short DivPlatformSAA1099::getPan(int ch) {
363 return ((chan[ch].pan&0xf0)<<4)|(chan[ch].pan&15);
364 }
365
366 DivDispatchOscBuffer* DivPlatformSAA1099::getOscBuffer(int ch) {
367 return oscBuf[ch];
368 }
369
370 unsigned char* DivPlatformSAA1099::getRegisterPool() {
371 return regPool;
372 }
373
374 int DivPlatformSAA1099::getRegisterPoolSize() {
375 return 32;
376 }
377
378 void DivPlatformSAA1099::reset() {
379 while (!writes.empty()) writes.pop();
380 memset(regPool,0,32);
381 saa_saaSound->Clear();
382 for (int i=0; i<6; i++) {
383 chan[i]=DivPlatformSAA1099::Channel();
384 chan[i].std.setEngine(parent);
385 chan[i].vol=0x0f;
386 }
387 if (dumpWrites) {
388 addWrite(0xffffffff,0);
389 }
390
391 lastBusy=60;
392 dacMode=0;
393 dacPeriod=0;
394 dacPos=0;
395 dacRate=0;
396 dacSample=-1;
397 sampleBank=0;
398 saaEnv[0]=0;
399 saaEnv[1]=0;
400 saaNoise[0]=0;
401 saaNoise[1]=0;
402
403 delay=0;
404
405 extMode=false;
406
407 rWrite(8,255);
408 rWrite(9,255);
409 rWrite(10,255);
410 rWrite(11,255);
411 rWrite(12,255);
412 rWrite(13,255);
413 rWrite(16,0x77);
414 rWrite(17,0x77);
415 rWrite(18,0x77);
416 rWrite(0x1c,2);
417 rWrite(0x1c,1);
418 }
419
420 int DivPlatformSAA1099::getOutputCount() {
421 return 2;
422 }
423
424 int DivPlatformSAA1099::getPortaFloor(int ch) {
425 return 12;
426 }
427
428 bool DivPlatformSAA1099::keyOffAffectsArp(int ch) {
429 return true;
430 }
431
432 bool DivPlatformSAA1099::getLegacyAlwaysSetVolume() {
433 return false;
434 }
435
436 void DivPlatformSAA1099::notifyInsDeletion(void* ins) {
437 for (int i=0; i<6; i++) {
438 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
439 }
440 }
441
442 void DivPlatformSAA1099::setFlags(const DivConfig& flags) {
443 int clockSel=flags.getInt("clockSel",0);
444 if (clockSel==2) {
445 chipClock=COLOR_PAL*8.0/5.0;
446 } else if (clockSel==1) {
447 chipClock=COLOR_NTSC*2.0;
448 } else {
449 chipClock=8000000;
450 }
451 CHECK_CUSTOM_CLOCK;
452 rate=chipClock/coreQuality;
453
454 for (int i=0; i<6; i++) {
455 oscBuf[i]->rate=rate;
456 }
457
458 saa_saaSound->SetClockRate(chipClock);
459 saa_saaSound->SetSampleRate(rate);
460 }
461
462 void DivPlatformSAA1099::poke(unsigned int addr, unsigned short val) {
463 rWrite(addr,val);
464 }
465
466 void DivPlatformSAA1099::poke(std::vector<DivRegWrite>& wlist) {
467 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
468 }
469
470 void DivPlatformSAA1099::setCoreQuality(unsigned char q) {
471 switch (q) {
472 case 0:
473 coreQuality=256;
474 break;
475 case 1:
476 coreQuality=128;
477 break;
478 case 2:
479 coreQuality=64;
480 break;
481 case 3:
482 coreQuality=32;
483 break;
484 case 4:
485 coreQuality=8;
486 break;
487 case 5:
488 coreQuality=1;
489 break;
490 default:
491 coreQuality=32;
492 break;
493 }
494 }
495
496 int DivPlatformSAA1099::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
497 parent=p;
498 dumpWrites=false;
499 skipRegisterWrites=false;
500 saa_saaSound=NULL;
501 for (int i=0; i<6; i++) {
502 isMuted[i]=false;
503 oscBuf[i]=new DivDispatchOscBuffer;
504 }
505 saa_saaSound=CreateCSAASound();
506 saa_saaSound->SetOversample(1);
507 saa_saaSound->SetSoundParameters(SAAP_NOFILTER|SAAP_16BIT|SAAP_STEREO);
508 setFlags(flags);
509 saaBufLen=65536;
510 for (int i=0; i<2; i++) saaBuf[i]=new short[saaBufLen];
511 reset();
512 return 3;
513 }
514
515 void DivPlatformSAA1099::quit() {
516 for (int i=0; i<6; i++) {
517 delete oscBuf[i];
518 }
519 if (saa_saaSound!=NULL) {
520 DestroyCSAASound(saa_saaSound);
521 saa_saaSound=NULL;
522 }
523 for (int i=0; i<2; i++) delete[] saaBuf[i];
524 }