Debian Codegraph @

src/engine/platform/ay8930.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 "ay8930.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include "sound/ay8910.h"
24 #include <string.h>
25 #include <math.h>
26
27 #define rWrite(a,v) if (!skipRegisterWrites) {pendingWrites[a]=v;}
28 #define immWrite2(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
29
30 #define CHIP_DIVIDER (clockSel?8:4)
31
32 const char* regCheatSheetAY8930[]={
33 "FreqL_A", "00",
34 "FreqH_A", "01",
35 "FreqL_B", "02",
36 "FreqH_B", "03",
37 "FreqL_C", "04",
38 "FreqH_C", "05",
39 "FreqNoise", "06",
40 "Enable", "07",
41 "Volume_A", "08",
42 "Volume_B", "09",
43 "Volume_C", "0A",
44 "FreqL_EnvA", "0B",
45 "FreqH_EnvA", "0C",
46 "Control_EnvA", "0D",
47 "PortA", "0E",
48 "PortB", "0F",
49 "FreqL_EnvB", "10",
50 "FreqH_EnvB", "11",
51 "FreqL_EnvC", "12",
52 "FreqH_EnvC", "13",
53 "Control_EnvB", "14",
54 "Control_EnvC", "15",
55 "Duty_A", "16",
56 "Duty_B", "17",
57 "Duty_C", "18",
58 "NoiseAND", "19",
59 "NoiseOR", "1A",
60 "TEST", "1F",
61 NULL
62 };
63
64 void DivPlatformAY8930::immWrite(unsigned char a, unsigned char v) {
65 if ((int)bank!=(a>>4)) {
66 bank=a>>4;
67 immWrite2(0x0d, 0xa0|(bank<<4)|chan[0].envelope.mode);
68 }
69 if (a==0x0d) {
70 immWrite2(0x0d,0xa0|(bank<<4)|(v&15));
71 } else {
72 immWrite2(a&15,v);
73 }
74 }
75
76 const char** DivPlatformAY8930::getRegisterSheet() {
77 return regCheatSheetAY8930;
78 }
79
80 /* C program to generate this table:
81
82 #include <stdio.h>
83 #include <math.h>
84
85 int main(int argc, char** argv) {
86 for (int i=0; i<256; i++) {
87 if ((i&15)==0) printf("\n ");
88 printf(" %d,",(int)round(pow((double)i/255.0,0.3)*31.0));
89 }
90 }
91 */
92
93 const unsigned char dacLogTableAY8930[256]={
94 0, 6, 7, 8, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13,
95 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16,
96 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19,
97 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20,
98 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
99 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
100 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
101 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
102 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
103 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
104 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28,
105 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
106 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
107 29, 29, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30,
108 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
109 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31
110 };
111
112 void DivPlatformAY8930::runDAC(int advance) {
113 for (int i=0; i<3; i++) {
114 if (chan[i].active && (chan[i].curPSGMode.val&8) && chan[i].dac.sample!=-1) {
115 chan[i].dac.period+=chan[i].dac.rate*advance;
116 bool end=false;
117 bool changed=false;
118 int prevOut=chan[i].dac.out;
119 while (chan[i].dac.period>=rate && !end) {
120 DivSample* s=parent->getSample(chan[i].dac.sample);
121 if (s->samples<=0 || chan[i].dac.pos<0 || chan[i].dac.pos>=(int)s->samples) {
122 chan[i].dac.sample=-1;
123 immWrite(0x08+i,0);
124 end=true;
125 break;
126 }
127 unsigned char dacData=dacLogTableAY8930[(unsigned char)s->data8[chan[i].dac.pos]^0x80];
128 chan[i].dac.out=(chan[i].active && !isMuted[i])?MAX(0,dacData-(31-chan[i].outVol)):0;
129 if (prevOut!=chan[i].dac.out) {
130 prevOut=chan[i].dac.out;
131 changed=true;
132 }
133 chan[i].dac.pos++;
134 if (s->isLoopable() && chan[i].dac.pos>=s->loopEnd) {
135 chan[i].dac.pos=s->loopStart;
136 } else if (chan[i].dac.pos>=(int)s->samples) {
137 chan[i].dac.sample=-1;
138 //immWrite(0x08+i,0);
139 end=true;
140 break;
141 }
142 chan[i].dac.period-=rate;
143 }
144 if (changed && !end) {
145 if (!isMuted[i]) {
146 immWrite(0x08+i,chan[i].dac.out);
147 }
148 }
149 }
150 }
151 }
152
153 void DivPlatformAY8930::checkWrites() {
154 while (!writes.empty()) {
155 QueuedWrite w=writes.front();
156 ay->address_w(w.addr);
157 ay->data_w(w.val);
158 if (w.addr!=0x0d && (regPool[0x0d]&0xf0)==0xb0) {
159 regPool[(w.addr&0x0f)|0x10]=w.val;
160 } else {
161 regPool[w.addr&0x0f]=w.val;
162 }
163 writes.pop();
164 }
165 }
166
167 void DivPlatformAY8930::acquireDirect(blip_buffer_t** bb, size_t len) {
168 thread_local short ayBuf[3];
169 for (int i=0; i<3; i++) {
170 oscBuf[i]->begin(len);
171 }
172
173 for (size_t i=0; i<len; i++) {
174 int advance=len-i;
175 bool careAboutNoise=false;
176 // heuristic
177 if (!writes.empty()) {
178 advance=1;
179 } else {
180 for (int j=0; j<3; j++) {
181 // tone counter
182 if (!ay->tone_enable(j) && ay->m_tone[j].volume!=0) {
183 const int period=MAX(1,ay->m_tone[j].period)*(ay->m_step_mul<<1);
184 const int remain=(period-ay->m_tone[j].count)>>5;
185 if (remain<advance) {
186 advance=remain;
187 }
188 }
189
190 // count me in if I have noise enabled
191 if (!ay->noise_enable(j) && ay->m_tone[j].volume!=0) {
192 careAboutNoise=true;
193 }
194
195 // envelope check
196 if (ay->m_tone[j].volume&32) {
197 if (ay->m_envelope[j].holding==0) {
198 const int periodEnv=MAX(1,ay->m_envelope[j].period)*ay->m_env_step_mul;
199 const int remainEnv=periodEnv-ay->m_envelope[j].count;
200 if (remainEnv<advance) {
201 advance=remainEnv;
202 }
203 }
204 }
205
206 // DAC
207 if (chan[j].active && (chan[j].curPSGMode.val&8) && chan[j].dac.sample!=-1) {
208 if (chan[j].dac.rate<=0) continue;
209 const int remainTime=(rate-chan[j].dac.period+chan[j].dac.rate-1)/chan[j].dac.rate;
210 if (remainTime<advance) advance=remainTime;
211 }
212
213 if (advance<=1) break;
214 }
215 // noise
216 if (careAboutNoise) {
217 const int noisePeriod=((int)ay->noise_period())*ay->m_step_mul;
218 const int noiseRemain=noisePeriod-ay->m_count_noise;
219 if (noiseRemain<advance) {
220 advance=noiseRemain;
221 }
222 }
223 }
224
225 if (advance<1) advance=1;
226
227 runDAC(advance);
228 checkWrites();
229
230 ay->sound_stream_update(ayBuf,advance);
231 i+=advance-1;
232
233 if (stereo) {
234 int out0=ayBuf[0]+ayBuf[1]+((ayBuf[2]*stereoSep)>>8);
235 int out1=((ayBuf[0]*stereoSep)>>8)+ayBuf[1]+ayBuf[2];
236 if (lastOut[0]!=out0) {
237 blip_add_delta(bb[0],i,out0-lastOut[0]);
238 lastOut[0]=out0;
239 }
240 if (lastOut[1]!=out1) {
241 blip_add_delta(bb[1],i,out1-lastOut[1]);
242 lastOut[1]=out1;
243 }
244 } else {
245 int out=ayBuf[0]+ayBuf[1]+ayBuf[2];
246 if (lastOut[0]!=out) {
247 blip_add_delta(bb[0],i,out-lastOut[0]);
248 blip_add_delta(bb[1],i,out-lastOut[0]);
249 lastOut[0]=out;
250 }
251 }
252
253 oscBuf[0]->putSample(i,ayBuf[0]<<2);
254 oscBuf[1]->putSample(i,ayBuf[1]<<2);
255 oscBuf[2]->putSample(i,ayBuf[2]<<2);
256 }
257
258 for (int i=0; i<3; i++) {
259 oscBuf[i]->end(len);
260 }
261 }
262
263 void DivPlatformAY8930::updateOutSel(bool immediate) {
264 if (immediate) {
265 immWrite(0x07,
266 ~((chan[0].curPSGMode.getTone())|
267 ((chan[1].curPSGMode.getTone())<<1)|
268 ((chan[2].curPSGMode.getTone())<<2)|
269 ((chan[0].curPSGMode.getNoise())<<2)|
270 ((chan[1].curPSGMode.getNoise())<<3)|
271 ((chan[2].curPSGMode.getNoise())<<4)|
272 ((!ioPortA)<<6)|
273 ((!ioPortB)<<7)));
274 } else {
275 rWrite(0x07,
276 ~((chan[0].curPSGMode.getTone())|
277 ((chan[1].curPSGMode.getTone())<<1)|
278 ((chan[2].curPSGMode.getTone())<<2)|
279 ((chan[0].curPSGMode.getNoise())<<2)|
280 ((chan[1].curPSGMode.getNoise())<<3)|
281 ((chan[2].curPSGMode.getNoise())<<4)|
282 ((!ioPortA)<<6)|
283 ((!ioPortB)<<7)));
284 }
285 }
286
287 const unsigned char regPeriodL[3]={
288 0x0b, 0x10, 0x12
289 };
290
291 const unsigned char regPeriodH[3]={
292 0x0c, 0x11, 0x13
293 };
294
295 const unsigned char regMode[3]={
296 0x0d, 0x14, 0x15
297 };
298
299 void DivPlatformAY8930::tick(bool sysTick) {
300 // PSG
301 for (int i=0; i<3; i++) {
302 chan[i].std.next();
303 if (chan[i].std.vol.had) {
304 chan[i].outVol=MIN(31,chan[i].std.vol.val)-(31-(chan[i].vol&31));
305 if (chan[i].outVol<0) chan[i].outVol=0;
306 if (!(chan[i].nextPSGMode.val&8)) {
307 if (isMuted[i]) {
308 rWrite(0x08+i,0);
309 } else {
310 rWrite(0x08+i,(chan[i].outVol&31)|((chan[i].nextPSGMode.getEnvelope())<<3));
311 }
312 }
313 }
314 if (NEW_ARP_STRAT) {
315 chan[i].handleArp();
316 } else if (chan[i].std.arp.had) {
317 if (!chan[i].inPorta) {
318 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
319 }
320 chan[i].freqChanged=true;
321 }
322 if (chan[i].std.duty.had) {
323 rWrite(0x06,chan[i].std.duty.val);
324 }
325 if (chan[i].std.wave.had) {
326 if (!(chan[i].nextPSGMode.val&8)) {
327 chan[i].nextPSGMode.val=chan[i].std.wave.val&7;
328 if (chan[i].active) {
329 chan[i].curPSGMode.val=chan[i].nextPSGMode.val;
330 }
331 if (isMuted[i]) {
332 rWrite(0x08+i,0);
333 } else {
334 rWrite(0x08+i,(chan[i].outVol&31)|((chan[i].nextPSGMode.getEnvelope())<<3));
335 }
336 }
337 }
338 if (chan[i].std.pitch.had) {
339 if (chan[i].std.pitch.mode) {
340 chan[i].pitch2+=chan[i].std.pitch.val;
341 CLAMP_VAR(chan[i].pitch2,-65535,65535);
342 } else {
343 chan[i].pitch2=chan[i].std.pitch.val;
344 }
345 chan[i].freqChanged=true;
346 }
347 if (chan[i].std.phaseReset.had) {
348 if (chan[i].std.phaseReset.val==1) {
349 if (chan[i].nextPSGMode.val&8) {
350 if (dumpWrites) addWrite(0xffff0002+(i<<8),0);
351 if (chan[i].dac.sample<0 || chan[i].dac.sample>=parent->song.sampleLen) {
352 if (dumpWrites) {
353 rWrite(0x08+i,0);
354 addWrite(0xffff0000+(i<<8),chan[i].dac.sample);
355 }
356 if (chan[i].dac.setPos) {
357 chan[i].dac.setPos=false;
358 } else {
359 chan[i].dac.pos=0;
360 }
361 chan[i].dac.period=0;
362 chan[i].keyOn=true;
363 }
364 }
365 oldWrites[0x08+i]=-1;
366 oldWrites[regMode[i]]=-1;
367 }
368 }
369 if (chan[i].std.ex1.had) { // duty
370 immWrite(0x16+i,chan[i].std.ex1.val);
371 }
372 if (chan[i].std.ex2.had) {
373 chan[i].envelope.mode=chan[i].std.ex2.val;
374 rWrite(regMode[i],chan[i].envelope.mode);
375 }
376 if (chan[i].std.ex3.had) {
377 chan[i].autoEnvNum=chan[i].std.ex3.val;
378 chan[i].freqChanged=true;
379 if (!chan[i].std.alg.will) chan[i].autoEnvDen=1;
380 }
381 if (chan[i].std.alg.had) {
382 chan[i].autoEnvDen=chan[i].std.alg.val;
383 chan[i].freqChanged=true;
384 if (!chan[i].std.ex3.will) chan[i].autoEnvNum=1;
385 }
386 if (chan[i].std.fb.had) {
387 ayNoiseAnd=chan[i].std.fb.val;
388 immWrite(0x19,ayNoiseAnd);
389 }
390 if (chan[i].std.fms.had) {
391 ayNoiseOr=chan[i].std.fms.val;
392 immWrite(0x1a,ayNoiseOr);
393 }
394 if (chan[i].std.ex4.had) {
395 chan[i].fixedFreq=chan[i].std.ex4.val;
396 chan[i].freqChanged=true;
397 }
398 if (chan[i].std.ex5.had) {
399 chan[i].envelope.period=chan[i].std.ex5.val;
400 immWrite(regPeriodL[i],chan[i].envelope.period);
401 immWrite(regPeriodH[i],chan[i].envelope.period>>8);
402 }
403 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
404 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);
405 if (chan[i].dac.furnaceDAC) {
406 double off=1.0;
407 if (chan[i].dac.sample>=0 && chan[i].dac.sample<parent->song.sampleLen) {
408 DivSample* s=parent->getSample(chan[i].dac.sample);
409 if (s->centerRate<1) {
410 off=1.0;
411 } else {
412 off=parent->getCenterRate()/(double)s->centerRate;
413 }
414 }
415 chan[i].dac.rate=((double)chipClock*4.0)/(double)(MAX(1,off*chan[i].freq));
416 if (dumpWrites) addWrite(0xffff0001+(i<<8),chan[i].dac.rate);
417 }
418 if (chan[i].freq<0) chan[i].freq=0;
419 if (chan[i].freq>65535) chan[i].freq=65535;
420 if (chan[i].keyOn) {
421 if (!(chan[i].nextPSGMode.val&8)) {
422 chan[i].curPSGMode.val=chan[i].nextPSGMode.val;
423 }
424 if (chan[i].insChanged) {
425 if (!chan[i].std.ex1.will) immWrite(0x16+i,chan[i].duty);
426 chan[i].insChanged=false;
427 }
428 }
429 if (chan[i].keyOff) {
430 chan[i].curPSGMode.val=0;
431 rWrite(0x08+i,0);
432 }
433 if (chan[i].fixedFreq>0) {
434 rWrite((i)<<1,chan[i].fixedFreq&0xff);
435 rWrite(1+((i)<<1),chan[i].fixedFreq>>8);
436 } else {
437 rWrite((i)<<1,chan[i].freq&0xff);
438 rWrite(1+((i)<<1),chan[i].freq>>8);
439 }
440 if (chan[i].keyOn) chan[i].keyOn=false;
441 if (chan[i].keyOff) chan[i].keyOff=false;
442 if (chan[i].freqChanged && chan[i].autoEnvNum>0 && chan[i].autoEnvDen>0) {
443 chan[i].envelope.period=(chan[i].freq*chan[i].autoEnvDen/chan[i].autoEnvNum)>>4;
444 immWrite(regPeriodL[i],chan[i].envelope.period);
445 immWrite(regPeriodH[i],chan[i].envelope.period>>8);
446 }
447 if (chan[i].freqChanged && chan[i].autoNoiseMode) {
448 int noiseFreq=chan[i].freq;
449 switch (chan[i].autoNoiseMode) {
450 case 1: // noise
451 noiseFreq+=chan[i].autoNoiseOff;
452 if (noiseFreq<0) noiseFreq=0;
453 if (noiseFreq>255) noiseFreq=255;
454 rWrite(0x06,noiseFreq);
455 break;
456 case 2: { // noise + OR mask
457 if (noiseFreq<0) noiseFreq=0;
458 int noiseDiv=(noiseFreq>>8)+1;
459 noiseFreq/=noiseDiv;
460 ayNoiseOr=noiseDiv;
461 immWrite(0x1a,ayNoiseOr);
462 noiseFreq+=chan[i].autoNoiseOff;
463 if (noiseFreq<0) noiseFreq=0;
464 if (noiseFreq>255) noiseFreq=255;
465 rWrite(0x06,noiseFreq);
466 break;
467 }
468 }
469 }
470 chan[i].freqChanged=false;
471 }
472
473 if (chan[i].envelope.slide!=0) {
474 chan[i].envelope.slideLow+=chan[i].envelope.slide;
475 while (chan[i].envelope.slideLow>7) {
476 chan[i].envelope.slideLow-=8;
477 if (chan[i].envelope.period<0xffff) {
478 chan[i].envelope.period++;
479 immWrite(regPeriodL[i],chan[i].envelope.period);
480 immWrite(regPeriodH[i],chan[i].envelope.period>>8);
481 }
482 }
483 while (chan[i].envelope.slideLow<-7) {
484 chan[i].envelope.slideLow+=8;
485 if (chan[i].envelope.period>0) {
486 chan[i].envelope.period--;
487 immWrite(regPeriodL[i],chan[i].envelope.period);
488 immWrite(regPeriodH[i],chan[i].envelope.period>>8);
489 }
490 }
491 }
492 }
493
494 updateOutSel();
495
496 for (int i=0; i<32; i++) {
497 if (pendingWrites[i]!=oldWrites[i]) {
498 immWrite(i,pendingWrites[i]&0xff);
499 oldWrites[i]=pendingWrites[i];
500 }
501 }
502 }
503
504 int DivPlatformAY8930::dispatch(DivCommand c) {
505 switch (c.cmd) {
506 case DIV_CMD_NOTE_ON: {
507 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AY8930);
508 if (ins->type==DIV_INS_AMIGA || ins->amiga.useSample) {
509 chan[c.chan].nextPSGMode.val|=8;
510 } else if (chan[c.chan].dac.furnaceDAC) {
511 chan[c.chan].nextPSGMode.val&=~8;
512 }
513 if (chan[c.chan].nextPSGMode.val&8) {
514 if (skipRegisterWrites) break;
515 if (ins->type==DIV_INS_AMIGA || ins->amiga.useSample) {
516 if (c.value!=DIV_NOTE_NULL) {
517 chan[c.chan].dac.sample=ins->amiga.getSample(c.value);
518 chan[c.chan].sampleNote=c.value;
519 c.value=ins->amiga.getFreq(c.value);
520 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
521 } else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) {
522 chan[c.chan].dac.sample=ins->amiga.getSample(chan[c.chan].sampleNote);
523 c.value=ins->amiga.getFreq(chan[c.chan].sampleNote);
524 }
525 if (chan[c.chan].dac.sample<0 || chan[c.chan].dac.sample>=parent->song.sampleLen) {
526 chan[c.chan].dac.sample=-1;
527 if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
528 break;
529 } else {
530 if (dumpWrites) {
531 rWrite(0x08+c.chan,0);
532 addWrite(0xffff0000+(c.chan<<8),chan[c.chan].dac.sample);
533 }
534 }
535 if (chan[c.chan].dac.setPos) {
536 chan[c.chan].dac.setPos=false;
537 } else {
538 chan[c.chan].dac.pos=0;
539 }
540 chan[c.chan].dac.period=0;
541 if (c.value!=DIV_NOTE_NULL) {
542 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
543 chan[c.chan].freqChanged=true;
544 chan[c.chan].note=c.value;
545 }
546 chan[c.chan].active=true;
547 chan[c.chan].macroInit(ins);
548 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
549 chan[c.chan].outVol=chan[c.chan].vol;
550 }
551 //chan[c.chan].keyOn=true;
552 chan[c.chan].dac.furnaceDAC=true;
553 } else {
554 if (c.value!=DIV_NOTE_NULL) {
555 chan[c.chan].note=c.value;
556 }
557 chan[c.chan].dac.sample=12*sampleBank+chan[c.chan].note%12;
558 if (chan[c.chan].dac.sample>=parent->song.sampleLen) {
559 chan[c.chan].dac.sample=-1;
560 if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
561 break;
562 } else {
563 if (dumpWrites) addWrite(0xffff0000+(c.chan<<8),chan[c.chan].dac.sample);
564 }
565 if (chan[c.chan].dac.setPos) {
566 chan[c.chan].dac.setPos=false;
567 } else {
568 chan[c.chan].dac.pos=0;
569 }
570 chan[c.chan].dac.period=0;
571 chan[c.chan].dac.rate=parent->getSample(chan[c.chan].dac.sample)->rate*4096;
572 if (dumpWrites) {
573 rWrite(0x08+c.chan,0);
574 addWrite(0xffff0001+(c.chan<<8),chan[c.chan].dac.rate);
575 }
576 chan[c.chan].dac.furnaceDAC=false;
577 }
578 chan[c.chan].curPSGMode.val&=~8;
579 chan[c.chan].curPSGMode.val|=chan[c.chan].nextPSGMode.val&8;
580 break;
581 }
582 if (c.value!=DIV_NOTE_NULL) {
583 chan[c.chan].sampleNote=DIV_NOTE_NULL;
584 chan[c.chan].sampleNoteDelta=0;
585 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
586 chan[c.chan].freqChanged=true;
587 chan[c.chan].note=c.value;
588 }
589 chan[c.chan].fixedFreq=0;
590 chan[c.chan].active=true;
591 chan[c.chan].keyOn=true;
592 chan[c.chan].macroInit(ins);
593 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
594 chan[c.chan].outVol=chan[c.chan].vol;
595 }
596 if (!(chan[c.chan].nextPSGMode.val&8)) {
597 if (isMuted[c.chan]) {
598 rWrite(0x08+c.chan,0);
599 } else {
600 rWrite(0x08+c.chan,(chan[c.chan].vol&31)|((chan[c.chan].nextPSGMode.getEnvelope())<<3));
601 }
602 }
603 break;
604 }
605 case DIV_CMD_NOTE_OFF:
606 chan[c.chan].dac.sample=-1;
607 if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
608 chan[c.chan].nextPSGMode.val&=~8;
609 chan[c.chan].keyOff=true;
610 chan[c.chan].active=false;
611 chan[c.chan].macroInit(NULL);
612 break;
613 case DIV_CMD_NOTE_OFF_ENV:
614 case DIV_CMD_ENV_RELEASE:
615 chan[c.chan].std.release();
616 break;
617 case DIV_CMD_VOLUME: {
618 chan[c.chan].vol=c.value;
619 if (!chan[c.chan].std.vol.has) {
620 chan[c.chan].outVol=c.value;
621 }
622 if (!(chan[c.chan].nextPSGMode.val&8)) {
623 if (isMuted[c.chan]) {
624 rWrite(0x08+c.chan,0);
625 } else {
626 if (chan[c.chan].active) rWrite(0x08+c.chan,(chan[c.chan].vol&31)|((chan[c.chan].nextPSGMode.getEnvelope())<<3));
627 }
628 break;
629 }
630 break;
631 }
632 case DIV_CMD_GET_VOLUME: {
633 return chan[c.chan].vol;
634 break;
635 }
636 case DIV_CMD_INSTRUMENT:
637 if (chan[c.chan].ins!=c.value || c.value2==1) {
638 chan[c.chan].insChanged=true;
639 }
640 chan[c.chan].ins=c.value;
641 break;
642 case DIV_CMD_PITCH: {
643 chan[c.chan].pitch=c.value;
644 chan[c.chan].freqChanged=true;
645 break;
646 }
647 case DIV_CMD_NOTE_PORTA: {
648 int destFreq=NOTE_PERIODIC(c.value2+chan[c.chan].sampleNoteDelta);
649 bool return2=false;
650 if (destFreq>chan[c.chan].baseFreq) {
651 chan[c.chan].baseFreq+=c.value;
652 if (chan[c.chan].baseFreq>=destFreq) {
653 chan[c.chan].baseFreq=destFreq;
654 return2=true;
655 }
656 } else {
657 chan[c.chan].baseFreq-=c.value;
658 if (chan[c.chan].baseFreq<=destFreq) {
659 chan[c.chan].baseFreq=destFreq;
660 return2=true;
661 }
662 }
663 chan[c.chan].freqChanged=true;
664 if (return2) {
665 chan[c.chan].inPorta=false;
666 return 2;
667 }
668 break;
669 }
670 case DIV_CMD_LEGATO: {
671 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+chan[c.chan].sampleNoteDelta);
672 chan[c.chan].freqChanged=true;
673 break;
674 }
675 case DIV_CMD_STD_NOISE_MODE:
676 if (c.value<0x10) {
677 if (!(chan[c.chan].nextPSGMode.val&8)) {
678 chan[c.chan].nextPSGMode.val=(c.value+1)&7;
679 if (chan[c.chan].active) {
680 chan[c.chan].curPSGMode.val=chan[c.chan].nextPSGMode.val;
681 }
682 if (isMuted[c.chan]) {
683 rWrite(0x08+c.chan,0);
684 } else if (chan[c.chan].active) {
685 rWrite(0x08+c.chan,(chan[c.chan].outVol&31)|((chan[c.chan].nextPSGMode.getEnvelope())<<3));
686 }
687 }
688 } else {
689 chan[c.chan].duty=c.value&15;
690 immWrite(0x16+c.chan,chan[c.chan].duty);
691 }
692 break;
693 case DIV_CMD_STD_NOISE_FREQ:
694 rWrite(0x06,c.value);
695 break;
696 case DIV_CMD_AY_ENVELOPE_SET:
697 chan[c.chan].envelope.mode=c.value>>4;
698 rWrite(regMode[c.chan],chan[c.chan].envelope.mode);
699 if (c.value&15) {
700 chan[c.chan].nextPSGMode.val|=4;
701 } else {
702 chan[c.chan].nextPSGMode.val&=~4;
703 }
704 if (!(chan[c.chan].nextPSGMode.val&8) && chan[c.chan].active) {
705 chan[c.chan].curPSGMode.val=chan[c.chan].nextPSGMode.val;
706 }
707 if (isMuted[c.chan]) {
708 rWrite(0x08+c.chan,0);
709 } else {
710 rWrite(0x08+c.chan,(chan[c.chan].vol&31)|((chan[c.chan].nextPSGMode.getEnvelope())<<3));
711 }
712 break;
713 case DIV_CMD_AY_ENVELOPE_LOW:
714 chan[c.chan].envelope.period&=0xff00;
715 chan[c.chan].envelope.period|=c.value;
716 immWrite(regPeriodL[c.chan],chan[c.chan].envelope.period);
717 immWrite(regPeriodH[c.chan],chan[c.chan].envelope.period>>8);
718 break;
719 case DIV_CMD_AY_ENVELOPE_HIGH:
720 chan[c.chan].envelope.period&=0xff;
721 chan[c.chan].envelope.period|=c.value<<8;
722 immWrite(regPeriodL[c.chan],chan[c.chan].envelope.period);
723 immWrite(regPeriodH[c.chan],chan[c.chan].envelope.period>>8);
724 break;
725 case DIV_CMD_AY_ENVELOPE_SLIDE:
726 chan[c.chan].envelope.slide=c.value;
727 break;
728 case DIV_CMD_AY_NOISE_MASK_AND:
729 ayNoiseAnd=c.value;
730 immWrite(0x19,ayNoiseAnd);
731 break;
732 case DIV_CMD_AY_NOISE_MASK_OR:
733 ayNoiseOr=c.value;
734 immWrite(0x1a,ayNoiseOr);
735 break;
736 case DIV_CMD_AY_AUTO_ENVELOPE:
737 chan[c.chan].autoEnvNum=c.value>>4;
738 chan[c.chan].autoEnvDen=c.value&15;
739 chan[c.chan].freqChanged=true;
740 break;
741 case DIV_CMD_AY_AUTO_PWM:
742 chan[c.chan].autoNoiseMode=c.value>>4;
743 chan[c.chan].autoNoiseOff=c.value&15;
744 if (chan[c.chan].autoNoiseOff>=8) chan[c.chan].autoNoiseOff-=16;
745 chan[c.chan].freqChanged=true;
746 break;
747 case DIV_CMD_AY_IO_WRITE:
748 if (c.value==255) {
749 immWrite(0x1f,c.value2);
750 break;
751 }
752 if (c.value) { // port B
753 ioPortB=true;
754 portBVal=c.value2;
755 logI("AY I/O port B write: %x",portBVal);
756 } else { // port A
757 ioPortA=true;
758 portAVal=c.value2;
759 logI("AY I/O port A write: %x",portAVal);
760 }
761 updateOutSel(true);
762 immWrite(14+(c.value?1:0),(c.value?portBVal:portAVal));
763 break;
764 case DIV_CMD_SAMPLE_MODE:
765 if (c.value>0) {
766 chan[c.chan].nextPSGMode.val|=8;
767 } else {
768 chan[c.chan].nextPSGMode.val&=~8;
769 }
770 if (chan[c.chan].active) {
771 chan[c.chan].curPSGMode.val&=~8;
772 chan[c.chan].curPSGMode.val|=chan[c.chan].nextPSGMode.val&8;
773 }
774 break;
775 case DIV_CMD_SAMPLE_BANK:
776 sampleBank=c.value;
777 if (sampleBank>(parent->song.sample.size()/12)) {
778 sampleBank=parent->song.sample.size()/12;
779 }
780 break;
781 case DIV_CMD_SAMPLE_POS:
782 chan[c.chan].dac.pos=c.value;
783 chan[c.chan].dac.setPos=true;
784 if (dumpWrites) addWrite(0xffff0005,chan[c.chan].dac.pos);
785 break;
786 case DIV_CMD_MACRO_OFF:
787 chan[c.chan].std.mask(c.value,true);
788 break;
789 case DIV_CMD_MACRO_ON:
790 chan[c.chan].std.mask(c.value,false);
791 break;
792 case DIV_CMD_MACRO_RESTART:
793 chan[c.chan].std.restart(c.value);
794 break;
795 case DIV_CMD_GET_VOLMAX:
796 return 31;
797 break;
798 case DIV_CMD_PRE_PORTA:
799 if (chan[c.chan].active && c.value2) {
800 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AY8930));
801 }
802 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);
803 chan[c.chan].inPorta=c.value;
804 break;
805 case DIV_CMD_PRE_NOTE:
806 break;
807 default:
808 //printf("WARNING: unimplemented command %d\n",c.cmd);
809 break;
810 }
811 return 1;
812 }
813
814 void DivPlatformAY8930::muteChannel(int ch, bool mute) {
815 isMuted[ch]=mute;
816 if (isMuted[ch]) {
817 rWrite(0x08+ch,0);
818 } else if (chan[ch].active) {
819 if (chan[ch].nextPSGMode.val&8) {
820 rWrite(0x08+ch,chan[ch].dac.out&31);
821 } else {
822 rWrite(0x08+ch,(chan[ch].outVol&31)|((chan[ch].nextPSGMode.getEnvelope())<<3));
823 }
824 }
825 }
826
827 void DivPlatformAY8930::forceIns() {
828 for (int i=0; i<3; i++) {
829 chan[i].insChanged=true;
830 chan[i].curPSGMode.val&=~8;
831 chan[i].nextPSGMode.val&=~8;
832 immWrite(regPeriodL[i],chan[i].envelope.period);
833 immWrite(regPeriodH[i],chan[i].envelope.period>>8);
834 immWrite(regMode[i],chan[i].envelope.mode);
835 }
836 }
837
838 void* DivPlatformAY8930::getChanState(int ch) {
839 return &chan[ch];
840 }
841
842 DivMacroInt* DivPlatformAY8930::getChanMacroInt(int ch) {
843 return &chan[ch].std;
844 }
845
846 DivSamplePos DivPlatformAY8930::getSamplePos(int ch) {
847 if (ch>=3) return DivSamplePos();
848 return DivSamplePos(
849 chan[ch].dac.sample,
850 chan[ch].dac.pos,
851 chan[ch].dac.rate
852 );
853 }
854
855 DivDispatchOscBuffer* DivPlatformAY8930::getOscBuffer(int ch) {
856 return oscBuf[ch];
857 }
858
859 int DivPlatformAY8930::mapVelocity(int ch, float vel) {
860 return round(31.0*pow(vel,0.22));
861 }
862
863 float DivPlatformAY8930::getGain(int ch, int vol) {
864 if (vol==0) return 0;
865 return 1.0/pow(10.0,(float)(31-vol)*1.5/20.0);
866 }
867
868 unsigned char* DivPlatformAY8930::getRegisterPool() {
869 return regPool;
870 }
871
872 int DivPlatformAY8930::getRegisterPoolSize() {
873 return 32;
874 }
875
876 void DivPlatformAY8930::reset() {
877 while (!writes.empty()) writes.pop();
878 ay->device_reset();
879 memset(regPool,0,32);
880 for (int i=0; i<3; i++) {
881 chan[i]=DivPlatformAY8930::Channel();
882 chan[i].std.setEngine(parent);
883 chan[i].vol=31;
884 chan[i].envelope.period=0;
885 chan[i].envelope.mode=0;
886 chan[i].envelope.slide=0;
887 chan[i].envelope.slideLow=0;
888 }
889 if (dumpWrites) {
890 addWrite(0xffffffff,0);
891 }
892
893 for (int i=0; i<32; i++) {
894 oldWrites[i]=-1;
895 pendingWrites[i]=-1;
896 }
897
898 sampleBank=0;
899 ayNoiseAnd=2;
900 ayNoiseOr=0;
901 delay=0;
902 lastOut[0]=0;
903 lastOut[1]=0;
904
905 extMode=false;
906 bank=false;
907
908 ioPortA=false;
909 ioPortB=false;
910 portAVal=0;
911 portBVal=0;
912
913 immWrite(0x0d,0xa0);
914 immWrite(0x19,2); // and mask
915 immWrite(0x1a,0x00); // or mask
916 }
917
918 int DivPlatformAY8930::getOutputCount() {
919 return 2;
920 }
921
922 bool DivPlatformAY8930::keyOffAffectsArp(int ch) {
923 return true;
924 }
925
926 bool DivPlatformAY8930::hasAcquireDirect() {
927 return true;
928 }
929
930 bool DivPlatformAY8930::getLegacyAlwaysSetVolume() {
931 return false;
932 }
933
934 void DivPlatformAY8930::notifyInsDeletion(void* ins) {
935 for (int i=0; i<3; i++) {
936 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
937 }
938 }
939
940 void DivPlatformAY8930::poke(unsigned int addr, unsigned short val) {
941 immWrite(addr,val);
942 }
943
944 void DivPlatformAY8930::poke(std::vector<DivRegWrite>& wlist) {
945 for (DivRegWrite& i: wlist) immWrite(i.addr,i.val);
946 }
947
948 void DivPlatformAY8930::setFlags(const DivConfig& flags) {
949 clockSel=flags.getBool("halfClock",false);
950 switch (flags.getInt("clockSel",0)) {
951 case 1:
952 chipClock=COLOR_PAL*2.0/5.0;
953 break;
954 case 2:
955 chipClock=1750000;
956 break;
957 case 3:
958 chipClock=2000000;
959 break;
960 case 4:
961 chipClock=1500000;
962 break;
963 case 5:
964 chipClock=1000000;
965 break;
966 case 6:
967 chipClock=COLOR_NTSC/4.0;
968 break;
969 case 7:
970 chipClock=COLOR_PAL*3.0/8.0;
971 break;
972 case 8:
973 chipClock=COLOR_PAL*3.0/16.0;
974 break;
975 case 9:
976 chipClock=COLOR_PAL/4.0;
977 break;
978 case 10:
979 chipClock=2097152;
980 break;
981 case 11:
982 chipClock=COLOR_NTSC;
983 break;
984 case 12:
985 chipClock=3600000;
986 break;
987 case 13:
988 chipClock=20000000/16;
989 break;
990 case 14:
991 chipClock=1536000;
992 break;
993 default:
994 chipClock=COLOR_NTSC/2.0;
995 break;
996 }
997 CHECK_CUSTOM_CLOCK;
998 rate=chipClock/4;
999 for (int i=0; i<3; i++) {
1000 oscBuf[i]->setRate(rate);
1001 }
1002
1003 stereo=flags.getBool("stereo",false);
1004 stereoSep=flags.getInt("stereoSep",0)&255;
1005 }
1006
1007 int DivPlatformAY8930::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
1008 parent=p;
1009 dumpWrites=false;
1010 skipRegisterWrites=false;
1011 for (int i=0; i<3; i++) {
1012 isMuted[i]=false;
1013 oscBuf[i]=new DivDispatchOscBuffer;
1014 }
1015 setFlags(flags);
1016 ay=new ay8930_device(rate,clockSel);
1017 ay->device_start();
1018 reset();
1019 return 3;
1020 }
1021
1022 void DivPlatformAY8930::quit() {
1023 for (int i=0; i<3; i++) {
1024 delete oscBuf[i];
1025 }
1026 delete ay;
1027 }