Debian Codegraph @

src/engine/platform/ay.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 "ay.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 immWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(regRemap(a),v)); if (dumpWrites) {addWrite(regRemap(a),v);} }
29
30 #define CHIP_DIVIDER (extMode?extDiv:((sunsoft||clockSel)?16:8))
31
32 const char* regCheatSheetAY[]={
33 "FreqL_A", "0",
34 "FreqH_A", "1",
35 "FreqL_B", "2",
36 "FreqH_B", "3",
37 "FreqL_C", "4",
38 "FreqH_C", "5",
39 "FreqNoise", "6",
40 "Enable", "7",
41 "Volume_A", "8",
42 "Volume_B", "9",
43 "Volume_C", "A",
44 "FreqL_Env", "B",
45 "FreqH_Env", "C",
46 "Control_Env", "D",
47 "PortA", "E",
48 "PortB", "F",
49 NULL
50 };
51
52 const char* regCheatSheetAY8914[]={
53 "FreqL_A", "0",
54 "FreqL_B", "1",
55 "FreqL_C", "2",
56 "FreqL_Env", "3",
57 "FreqH_A", "4",
58 "FreqH_B", "5",
59 "FreqH_C", "6",
60 "FreqH_Env", "7",
61 "Enable", "8",
62 "FreqNoise", "9",
63 "Control_Env", "A",
64 "Volume_A", "B",
65 "Volume_B", "C",
66 "Volume_C", "D",
67 "PortA", "E",
68 "PortB", "F",
69 NULL
70 };
71
72 // taken from ay8910.cpp
73 const int sunsoftVolTable[32]={
74 103350, 73770, 52657, 37586, 32125, 27458, 24269, 21451,
75 18447, 15864, 14009, 12371, 10506, 8922, 7787, 6796,
76 5689, 4763, 4095, 3521, 2909, 2403, 2043, 1737,
77 1397, 1123, 925, 762, 578, 438, 332, 251
78 };
79
80 const char** DivPlatformAY8910::getRegisterSheet() {
81 return intellivision?regCheatSheetAY8914:regCheatSheetAY;
82 }
83
84 /* C program to generate this table:
85
86 #include <stdio.h>
87 #include <math.h>
88
89 int main(int argc, char** argv) {
90 for (int i=0; i<256; i++) {
91 if ((i&15)==0) printf("\n ");
92 printf(" %d,",(int)round(pow((double)i/255.0,0.36)*15.0));
93 }
94 }
95 */
96
97 const unsigned char dacLogTableAY[256]={
98 0, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
99 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7,
100 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
101 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
102 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
103 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11,
104 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
105 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12,
106 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
107 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13,
108 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
109 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14,
110 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
111 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
112 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15,
113 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15
114 };
115
116 void DivPlatformAY8910::runDAC(int runRate, int advance) {
117 if (runRate==0) runRate=dacRate;
118 for (int i=0; i<3; i++) {
119 if (chan[i].active && (chan[i].curPSGMode.val&8) && chan[i].dac.sample!=-1) {
120 chan[i].dac.period+=chan[i].dac.rate*advance;
121 bool end=false;
122 bool changed=false;
123 int prevOut=chan[i].dac.out;
124 while (chan[i].dac.period>=runRate && !end) {
125 DivSample* s=parent->getSample(chan[i].dac.sample);
126 if (s->samples<=0 || chan[i].dac.pos<0 || chan[i].dac.pos>=(int)s->samples) {
127 chan[i].dac.sample=-1;
128 immWrite(0x08+i,0);
129 end=true;
130 break;
131 }
132 unsigned char dacData=dacLogTableAY[(unsigned char)s->data8[chan[i].dac.pos]^0x80];
133 chan[i].dac.out=(chan[i].active && !isMuted[i])?MAX(0,dacData-(15-chan[i].outVol)):0;
134 if (prevOut!=chan[i].dac.out) {
135 prevOut=chan[i].dac.out;
136 changed=true;
137 }
138 chan[i].dac.pos++;
139 if (s->isLoopable() && chan[i].dac.pos>=s->loopEnd) {
140 chan[i].dac.pos=s->loopStart;
141 } else if (chan[i].dac.pos>=(int)s->samples) {
142 chan[i].dac.sample=-1;
143 //immWrite(0x08+i,0);
144 end=true;
145 break;
146 }
147 chan[i].dac.period-=runRate;
148 }
149 if (changed && !end) {
150 if (!isMuted[i]) {
151 immWrite(0x08+i,chan[i].dac.out);
152 }
153 }
154 }
155 }
156 }
157
158 void DivPlatformAY8910::runTFX(int runRate, int advance) {
159 /*
160 developer's note: if you are checking for intellivision
161 make sure to add "&& selCore"
162 because for some reason, the register remap doesn't work
163 when the user uses AtomicSSG core
164 */
165 float counterRatio=advance;
166 if (runRate!=0) counterRatio=(double)rate/(double)runRate;
167 int timerPeriod, output;
168 for (int i=0; i<3; i++) {
169 if (chan[i].active && (chan[i].curPSGMode.val&16) && !(chan[i].curPSGMode.val&8) && chan[i].tfx.mode!=-1) {
170 if (chan[i].tfx.mode == -1 && !isMuted[i]) {
171 /*
172 bug: if in the timer FX macro the user enables
173 and then disables PWM while there is no volume macro
174 there is now a random chance that the resulting output
175 is silent or has volume set incorrectly
176 i've tried to implement a fix, but it seems to be
177 ineffective, so...
178 TODO: actually implement a proper fix
179 */
180 if (intellivision && chan[i].curPSGMode.getEnvelope()) {
181 immWrite(0x08+i,(chan[i].outVol&0xc)<<2);
182 continue;
183 } else {
184 immWrite(0x08+i,(chan[i].outVol&15)|((chan[i].curPSGMode.getEnvelope())<<2));
185 continue;
186 }
187 }
188 chan[i].tfx.counter += counterRatio;
189 if (chan[i].tfx.counter >= chan[i].tfx.period && chan[i].tfx.mode == 0) {
190 chan[i].tfx.counter -= chan[i].tfx.period;
191 chan[i].tfx.out ^= 1;
192 output = ((chan[i].tfx.out) ? chan[i].outVol : (chan[i].tfx.lowBound-(15-chan[i].outVol)));
193 // TODO: fix this stupid crackling noise that happens
194 // everytime the volume changes
195 output = (output <= 0) ? 0 : output; // underflow
196 output = (output >= 15) ? 15 : output; // overflow
197 output &= 15; // i don't know if i need this but i'm too scared to remove it
198 if (!isMuted[i]) {
199 // TODO: ???????
200 if (intellivision && selCore) {
201 immWrite(0x0b+i,(output&0xc)<<2);
202 } else {
203 immWrite(0x08+i,output|(chan[i].curPSGMode.getEnvelope()<<2));
204 }
205 }
206 }
207 if (chan[i].tfx.counter >= chan[i].tfx.period && chan[i].tfx.mode == 1) {
208 chan[i].tfx.counter -= chan[i].tfx.period;
209 if (!isMuted[i]) {
210 // TODO: ???????
211 if (intellivision && selCore) {
212 immWrite(0xa, ayEnvMode);
213 } else {
214 immWrite(0xd, ayEnvMode);
215 }
216 }
217 }
218 if (chan[i].tfx.counter >= chan[i].tfx.period && chan[i].tfx.mode == 2) {
219 chan[i].tfx.counter -= chan[i].tfx.period;
220 }
221 }
222 if (chan[i].tfx.num > 0) {
223 timerPeriod = chan[i].freq*chan[i].tfx.den/chan[i].tfx.num;
224 } else {
225 timerPeriod = chan[i].freq*chan[i].tfx.den;
226 }
227 if (chan[i].tfx.num > 0 && chan[i].tfx.den > 0) chan[i].tfx.period=timerPeriod+chan[i].tfx.offset;
228 // stupid pitch correction because:
229 // YM2149 half-clock and Sunsoft 5B: timers run an octave too high
230 // on AtomicSSG core timers run 2 octaves too high
231 if (clockSel || sunsoft) chan[i].tfx.period = chan[i].tfx.period * 2;
232 if (selCore && !intellivision) chan[i].tfx.period = chan[i].tfx.period * 4;
233 }
234 }
235
236 void DivPlatformAY8910::checkWrites() {
237 while (!writes.empty()) {
238 QueuedWrite w=writes.front();
239 if (intellivision) {
240 ay8914_device* ay8914=(ay8914_device*)ay;
241 ay8914->write(w.addr,w.val);
242 } else {
243 ay->address_w(w.addr);
244 ay->data_w(w.val);
245 }
246 regPool[w.addr&0x0f]=w.val;
247 writes.pop();
248 }
249 }
250
251 void DivPlatformAY8910::acquire_mame(blip_buffer_t** bb, size_t len) {
252 thread_local short ayBuf[3];
253
254 for (int i=0; i<3; i++) {
255 oscBuf[i]->begin(len);
256 }
257
258 for (size_t i=0; i<len; i++) {
259 int advance=len-i;
260 bool careAboutEnv=false;
261 bool careAboutNoise=false;
262 // heuristic
263 if (!writes.empty()) {
264 advance=1;
265 } else {
266 for (int j=0; j<3; j++) {
267 // tone counter
268 if (!ay->tone_enable(j) && ay->m_tone[j].volume!=0) {
269 const int period=MAX(1,ay->m_tone[j].period)*(ay->m_step_mul<<1);
270 const int remain=(period-ay->m_tone[j].count)>>1;
271 if (remain<advance) {
272 advance=remain;
273 }
274 }
275
276 // count me in if I have noise enabled
277 if (!ay->noise_enable(j) && ay->m_tone[j].volume!=0) {
278 careAboutNoise=true;
279 }
280
281 // envelope check
282 if (ay->m_tone[j].volume&16) {
283 careAboutEnv=true;
284 }
285
286 // DAC
287 if (chan[j].active && (chan[j].curPSGMode.val&8) && chan[j].dac.sample!=-1) {
288 if (chan[j].dac.rate<=0) continue;
289 const int remainTime=(rate-chan[j].dac.period+chan[j].dac.rate-1)/chan[j].dac.rate;
290 if (remainTime<advance) advance=remainTime;
291 }
292
293 // TFX
294 if (chan[j].active && (chan[j].curPSGMode.val&16) && !(chan[j].curPSGMode.val&8) && chan[j].tfx.mode!=-1) {
295 const int remainTime=chan[j].tfx.period-chan[j].tfx.counter;
296 if (remainTime<advance) advance=remainTime;
297 }
298
299 if (advance<=1) break;
300 }
301 // envelope
302 if (careAboutEnv) {
303 if (ay->m_envelope[0].holding==0) {
304 const int periodEnv=MAX(1,ay->m_envelope[0].period)*ay->m_env_step_mul;
305 const int remainEnv=periodEnv-ay->m_envelope[0].count;
306 if (remainEnv<advance) {
307 advance=remainEnv;
308 }
309 }
310 }
311 // noise
312 if (careAboutNoise) {
313 const int noisePeriod=((int)ay->noise_period())*ay->m_step_mul;
314 const int noiseRemain=noisePeriod-ay->m_count_noise;
315 if (noiseRemain<advance) {
316 advance=noiseRemain;
317 }
318 }
319 }
320
321 if (advance<1) advance=1;
322
323 runDAC(0,advance);
324 runTFX(0,advance);
325 checkWrites();
326
327 ay->sound_stream_update(ayBuf,advance);
328 i+=advance-1;
329
330 if (sunsoft) {
331 if (lastOut[0]!=ayBuf[0]) {
332 blip_add_delta(bb[0],i,ayBuf[0]-lastOut[0]);
333 blip_add_delta(bb[1],i,ayBuf[0]-lastOut[0]);
334 lastOut[0]=ayBuf[0];
335 }
336
337 oscBuf[0]->putSample(i,CLAMP(sunsoftVolTable[31-(ay->lastIndx&31)]<<3,-32768,32767));
338 oscBuf[1]->putSample(i,CLAMP(sunsoftVolTable[31-((ay->lastIndx>>5)&31)]<<3,-32768,32767));
339 oscBuf[2]->putSample(i,CLAMP(sunsoftVolTable[31-((ay->lastIndx>>10)&31)]<<3,-32768,32767));
340 } else {
341 if (stereo) {
342 int out0=ayBuf[0]+ayBuf[1]+((ayBuf[2]*stereoSep)>>8);
343 int out1=((ayBuf[0]*stereoSep)>>8)+ayBuf[1]+ayBuf[2];
344 if (lastOut[0]!=out0) {
345 blip_add_delta(bb[0],i,out0-lastOut[0]);
346 lastOut[0]=out0;
347 }
348 if (lastOut[1]!=out1) {
349 blip_add_delta(bb[1],i,out1-lastOut[1]);
350 lastOut[1]=out1;
351 }
352 } else {
353 int out=ayBuf[0]+ayBuf[1]+ayBuf[2];
354 if (lastOut[0]!=out) {
355 blip_add_delta(bb[0],i,out-lastOut[0]);
356 blip_add_delta(bb[1],i,out-lastOut[0]);
357 lastOut[0]=out;
358 }
359 }
360
361 oscBuf[0]->putSample(i,ayBuf[0]<<2);
362 oscBuf[1]->putSample(i,ayBuf[1]<<2);
363 oscBuf[2]->putSample(i,ayBuf[2]<<2);
364 }
365 }
366
367 for (int i=0; i<3; i++) {
368 oscBuf[i]->end(len);
369 }
370 }
371
372 void DivPlatformAY8910::acquire_atomic(short** buf, size_t len) {
373 for (int i=0; i<3; i++) {
374 oscBuf[i]->begin(len);
375 }
376 for (size_t i=0; i<len; i++) {
377 runDAC(0,1);
378 runTFX(0,1);
379
380 if (!writes.empty()) {
381 QueuedWrite w=writes.front();
382 SSG_Write(&ay_atomic,w.addr&0x0f,w.val);
383 regPool[w.addr&0x0f]=w.val;
384 writes.pop();
385 }
386
387 SSG_Clock(&ay_atomic,0);
388 SSG_Clock(&ay_atomic,1);
389
390 if (stereo) {
391 buf[0][i]=ay_atomic.o_analog[0]+ay_atomic.o_analog[1]+((ay_atomic.o_analog[2]*stereoSep)>>8);
392 buf[1][i]=((ay_atomic.o_analog[0]*stereoSep)>>8)+ay_atomic.o_analog[1]+ay_atomic.o_analog[2];
393 } else {
394 buf[0][i]=ay_atomic.o_analog[0]+ay_atomic.o_analog[1]+ay_atomic.o_analog[2];
395 buf[1][i]=buf[0][i];
396 }
397
398 oscBuf[0]->putSample(i,ay_atomic.o_analog[0]);
399 oscBuf[1]->putSample(i,ay_atomic.o_analog[1]);
400 oscBuf[2]->putSample(i,ay_atomic.o_analog[2]);
401 }
402 for (int i=0; i<3; i++) {
403 oscBuf[i]->end(len);
404 }
405 }
406
407 void DivPlatformAY8910::acquireDirect(blip_buffer_t** bb, size_t len) {
408 if (selCore && !intellivision) return;
409 acquire_mame(bb,len);
410 }
411
412 void DivPlatformAY8910::acquire(short** buf, size_t len) {
413 if (selCore && !intellivision) {
414 acquire_atomic(buf,len);
415 }
416 }
417
418 void DivPlatformAY8910::fillStream(std::vector<DivDelayedWrite>& stream, int sRate, size_t len) {
419 writes.clear();
420 for (size_t i=0; i<len; i++) {
421 runDAC(sRate,1);
422 runTFX(sRate,1);
423 while (!writes.empty()) {
424 QueuedWrite& w=writes.front();
425 stream.push_back(DivDelayedWrite(i,w.addr,w.val));
426 writes.pop_front();
427 }
428 }
429 }
430
431 void DivPlatformAY8910::updateOutSel(bool immediate) {
432 if (immediate) {
433 immWrite(0x07,
434 ~((chan[0].curPSGMode.getTone())|
435 ((chan[1].curPSGMode.getTone())<<1)|
436 ((chan[2].curPSGMode.getTone())<<2)|
437 ((chan[0].curPSGMode.getNoise())<<2)|
438 ((chan[1].curPSGMode.getNoise())<<3)|
439 ((chan[2].curPSGMode.getNoise())<<4)|
440 ((!ioPortA)<<6)|
441 ((!ioPortB)<<7)));
442 } else {
443 rWrite(0x07,
444 ~((chan[0].curPSGMode.getTone())|
445 ((chan[1].curPSGMode.getTone())<<1)|
446 ((chan[2].curPSGMode.getTone())<<2)|
447 ((chan[0].curPSGMode.getNoise())<<2)|
448 ((chan[1].curPSGMode.getNoise())<<3)|
449 ((chan[2].curPSGMode.getNoise())<<4)|
450 ((!ioPortA)<<6)|
451 ((!ioPortB)<<7)));
452 }
453 }
454
455 void DivPlatformAY8910::tick(bool sysTick) {
456 // PSG
457 for (int i=0; i<3; i++) {
458 chan[i].std.next();
459 if (chan[i].std.vol.had) {
460 chan[i].outVol=MIN(15,chan[i].std.vol.val)-(15-(chan[i].vol&15));
461 if (chan[i].outVol<0) chan[i].outVol=0;
462 if (!(chan[i].nextPSGMode.val&8) || !(chan[i].nextPSGMode.val&16)) {
463 if (isMuted[i]) {
464 rWrite(0x08+i,0);
465 } else if (intellivision && (chan[i].nextPSGMode.getEnvelope())) {
466 rWrite(0x08+i,(chan[i].outVol&0xc)<<2);
467 } else {
468 rWrite(0x08+i,(chan[i].outVol&15)|((chan[i].nextPSGMode.getEnvelope())<<2));
469 }
470 }
471 }
472 if (NEW_ARP_STRAT) {
473 chan[i].handleArp();
474 } else if (chan[i].std.arp.had) {
475 if (!chan[i].inPorta) {
476 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
477 }
478 chan[i].freqChanged=true;
479 }
480 if (chan[i].std.duty.had) {
481 rWrite(0x06,31-chan[i].std.duty.val);
482 }
483 if (chan[i].std.wave.had) {
484 if (!(chan[i].nextPSGMode.val&8)) {
485 chan[i].nextPSGMode.val=chan[i].std.wave.val&7;
486 chan[i].nextPSGMode.val|=(chan[i].curPSGMode.val&16);
487 if (chan[i].active) {
488 chan[i].curPSGMode.val=chan[i].nextPSGMode.val;
489 }
490 if (isMuted[i]) {
491 rWrite(0x08+i,0);
492 } else if (intellivision && (chan[i].nextPSGMode.getEnvelope())) {
493 rWrite(0x08+i,(chan[i].outVol&0xc)<<2);
494 } else {
495 rWrite(0x08+i,(chan[i].outVol&15)|((chan[i].nextPSGMode.getEnvelope())<<2));
496 }
497 }
498 }
499 if (chan[i].std.pitch.had) {
500 if (chan[i].std.pitch.mode) {
501 chan[i].pitch2+=chan[i].std.pitch.val;
502 CLAMP_VAR(chan[i].pitch2,-32768,32767);
503 } else {
504 chan[i].pitch2=chan[i].std.pitch.val;
505 }
506 chan[i].freqChanged=true;
507 }
508 if (chan[i].std.phaseReset.had) {
509 if (chan[i].std.phaseReset.val==1) {
510 chan[i].tfx.counter = 0;
511 chan[i].tfx.out = 0;
512 if (chan[i].nextPSGMode.val&8) {
513 //if (dumpWrites) addWrite(0xffff0002+(i<<8),0);
514 if (chan[i].dac.sample<0 || chan[i].dac.sample>=parent->song.sampleLen) {
515 if (dumpWrites) {
516 rWrite(0x08+i,0);
517 //addWrite(0xffff0000+(i<<8),chan[i].dac.sample);
518 }
519 if (chan[i].dac.setPos) {
520 chan[i].dac.setPos=false;
521 } else {
522 chan[i].dac.pos=0;
523 }
524 chan[i].dac.period=0;
525 chan[i].keyOn=true;
526 }
527 }
528 oldWrites[0x08+i]=-1;
529 oldWrites[0x0d]=-1;
530 }
531 }
532 if (chan[i].std.ex2.had) {
533 ayEnvMode=chan[i].std.ex2.val;
534 rWrite(0x0d,ayEnvMode);
535 }
536 if (chan[i].std.ex3.had) {
537 chan[i].autoEnvNum=chan[i].std.ex3.val;
538 chan[i].freqChanged=true;
539 if (!chan[i].std.alg.will) chan[i].autoEnvDen=1;
540 }
541 if (chan[i].std.alg.had) {
542 chan[i].autoEnvDen=chan[i].std.alg.val;
543 chan[i].freqChanged=true;
544 if (!chan[i].std.ex3.will) chan[i].autoEnvNum=1;
545 }
546 if (chan[i].std.ex4.had) {
547 chan[i].fixedFreq=chan[i].std.ex4.val;
548 chan[i].freqChanged=true;
549 }
550 if (chan[i].std.ex5.had) {
551 ayEnvPeriod=chan[i].std.ex5.val;
552 immWrite(0x0b,ayEnvPeriod);
553 immWrite(0x0c,ayEnvPeriod>>8);
554 }
555 if (chan[i].std.ex6.had) {
556 // 0 - disable timer
557 // 1 - pwm
558 // 2 - syncbuzzer
559 switch (chan[i].std.ex6.val) {
560 case 1:
561 chan[i].nextPSGMode.val|=16;
562 chan[i].tfx.mode = 0;
563 break;
564 case 2:
565 chan[i].nextPSGMode.val|=16;
566 chan[i].tfx.mode = 1;
567 break;
568 case 3:
569 chan[i].nextPSGMode.val|=16;
570 chan[i].tfx.mode = 2;
571 break;
572 default:
573 chan[i].nextPSGMode.val|=16;
574 chan[i].tfx.mode = -1; // this is a workaround!
575 break;
576 }
577 }
578 if (chan[i].std.ex7.had) {
579 chan[i].tfx.offset=chan[i].std.ex7.val;
580 }
581 if (chan[i].std.ex8.had) {
582 chan[i].tfx.num=chan[i].std.ex8.val;
583 chan[i].freqChanged=true;
584 if (!chan[i].std.fms.will) chan[i].tfx.den=1;
585 }
586 if (chan[i].std.fms.had) {
587 chan[i].tfx.den=chan[i].std.fms.val;
588 chan[i].freqChanged=true;
589 if (!chan[i].std.ex8.will) chan[i].tfx.num=1;
590 }
591 if (chan[i].std.ams.had) {
592 chan[i].tfx.lowBound=chan[i].std.ams.val;
593 }
594 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
595 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);
596 if (chan[i].dac.furnaceDAC) {
597 double off=1.0;
598 if (chan[i].dac.sample>=0 && chan[i].dac.sample<parent->song.sampleLen) {
599 DivSample* s=parent->getSample(chan[i].dac.sample);
600 if (s->centerRate<1) {
601 off=1.0;
602 } else {
603 off=parent->getCenterRate()/(double)s->centerRate;
604 }
605 }
606 chan[i].dac.rate=((double)rate*((sunsoft||clockSel)?8.0:16.0))/(double)(MAX(1,off*chan[i].freq));
607 //if (dumpWrites) addWrite(0xffff0001+(i<<8),chan[i].dac.rate);
608 }
609 if (chan[i].freq<0) chan[i].freq=0;
610 if (chan[i].freq>4095) chan[i].freq=4095;
611 if (chan[i].fixedFreq>4095) chan[i].fixedFreq=4095;
612 if (chan[i].keyOn) {
613 //rWrite(16+i*5+1,((chan[i].duty&3)<<6)|(63-(ins->gb.soundLen&63)));
614 //rWrite(16+i*5+2,((chan[i].vol<<4))|(ins->gb.envLen&7)|((ins->gb.envDir&1)<<3));
615 if (!(chan[i].nextPSGMode.val&8)) {
616 chan[i].curPSGMode.val=chan[i].nextPSGMode.val;
617 }
618 }
619 if (chan[i].keyOff) {
620 chan[i].curPSGMode.val=0;
621 rWrite(0x08+i,0);
622 }
623 if (chan[i].fixedFreq>0) {
624 rWrite((i)<<1,chan[i].fixedFreq&0xff);
625 rWrite(1+((i)<<1),chan[i].fixedFreq>>8);
626 } else {
627 rWrite((i)<<1,chan[i].freq&0xff);
628 rWrite(1+((i)<<1),chan[i].freq>>8);
629 }
630 if (chan[i].keyOn) chan[i].keyOn=false;
631 if (chan[i].keyOff) chan[i].keyOff=false;
632 if (chan[i].freqChanged && chan[i].autoEnvNum>0 && chan[i].autoEnvDen>0) {
633 ayEnvPeriod=(chan[i].freq*chan[i].autoEnvDen/chan[i].autoEnvNum)>>4;
634 immWrite(0x0b,ayEnvPeriod);
635 immWrite(0x0c,ayEnvPeriod>>8);
636 }
637 chan[i].freqChanged=false;
638 }
639 }
640
641 updateOutSel();
642
643 if (ayEnvSlide!=0) {
644 ayEnvSlideLow+=ayEnvSlide;
645 while (ayEnvSlideLow>7) {
646 ayEnvSlideLow-=8;
647 if (ayEnvPeriod<0xffff) {
648 ayEnvPeriod++;
649 immWrite(0x0b,ayEnvPeriod);
650 immWrite(0x0c,ayEnvPeriod>>8);
651 }
652 }
653 while (ayEnvSlideLow<-7) {
654 ayEnvSlideLow+=8;
655 if (ayEnvPeriod>0) {
656 ayEnvPeriod--;
657 immWrite(0x0b,ayEnvPeriod);
658 immWrite(0x0c,ayEnvPeriod>>8);
659 }
660 }
661 }
662
663 for (int i=0; i<16; i++) {
664 if (pendingWrites[i]!=oldWrites[i]) {
665 immWrite(i,pendingWrites[i]&0xff);
666 oldWrites[i]=pendingWrites[i];
667 }
668 }
669 }
670
671 int DivPlatformAY8910::dispatch(DivCommand c) {
672 switch (c.cmd) {
673 case DIV_CMD_NOTE_ON: {
674 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AY);
675 if (!parent->song.disableSampleMacro && (ins->type==DIV_INS_AMIGA || ins->amiga.useSample)) {
676 chan[c.chan].nextPSGMode.val|=8;
677 } else if (chan[c.chan].dac.furnaceDAC) {
678 chan[c.chan].nextPSGMode.val&=~8;
679 }
680 if (chan[c.chan].nextPSGMode.val&8) {
681 if (skipRegisterWrites) break;
682 if (!parent->song.disableSampleMacro && (ins->type==DIV_INS_AMIGA || ins->amiga.useSample)) {
683 if (c.value!=DIV_NOTE_NULL) {
684 chan[c.chan].dac.sample=ins->amiga.getSample(c.value);
685 chan[c.chan].sampleNote=c.value;
686 c.value=ins->amiga.getFreq(c.value);
687 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
688 } else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) {
689 chan[c.chan].dac.sample=ins->amiga.getSample(chan[c.chan].sampleNote);
690 c.value=ins->amiga.getFreq(chan[c.chan].sampleNote);
691 }
692 if (chan[c.chan].dac.sample<0 || chan[c.chan].dac.sample>=parent->song.sampleLen) {
693 chan[c.chan].dac.sample=-1;
694 //if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
695 break;
696 } else {
697 if (dumpWrites) {
698 rWrite(0x08+c.chan,0);
699 //addWrite(0xffff0000+(c.chan<<8),chan[c.chan].dac.sample);
700 }
701 }
702 if (chan[c.chan].dac.setPos) {
703 chan[c.chan].dac.setPos=false;
704 } else {
705 chan[c.chan].dac.pos=0;
706 }
707 chan[c.chan].dac.period=0;
708 if (c.value!=DIV_NOTE_NULL) {
709 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
710 chan[c.chan].freqChanged=true;
711 chan[c.chan].note=c.value;
712 }
713 chan[c.chan].active=true;
714 chan[c.chan].macroInit(ins);
715 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
716 chan[c.chan].outVol=chan[c.chan].vol;
717 }
718 //chan[c.chan].keyOn=true;
719 chan[c.chan].dac.furnaceDAC=true;
720 } else {
721 if (c.value!=DIV_NOTE_NULL) {
722 chan[c.chan].note=c.value;
723 }
724 chan[c.chan].dac.sample=12*sampleBank+chan[c.chan].note%12;
725 if (chan[c.chan].dac.sample>=parent->song.sampleLen) {
726 chan[c.chan].dac.sample=-1;
727 //if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
728 break;
729 } else {
730 //if (dumpWrites) addWrite(0xffff0000+(c.chan<<8),chan[c.chan].dac.sample);
731 }
732 if (chan[c.chan].dac.setPos) {
733 chan[c.chan].dac.setPos=false;
734 } else {
735 chan[c.chan].dac.pos=0;
736 }
737 chan[c.chan].dac.period=0;
738 chan[c.chan].dac.rate=parent->getSample(chan[c.chan].dac.sample)->rate*2048;
739 if (dumpWrites) {
740 rWrite(0x08+c.chan,0);
741 //addWrite(0xffff0001+(c.chan<<8),chan[c.chan].dac.rate);
742 }
743 chan[c.chan].dac.furnaceDAC=false;
744 }
745 chan[c.chan].curPSGMode.val&=~8;
746 chan[c.chan].curPSGMode.val|=chan[c.chan].nextPSGMode.val&8;
747 break;
748 }
749 if (c.value!=DIV_NOTE_NULL) {
750 chan[c.chan].sampleNote=DIV_NOTE_NULL;
751 chan[c.chan].sampleNoteDelta=0;
752 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
753 chan[c.chan].freqChanged=true;
754 chan[c.chan].note=c.value;
755 }
756 chan[c.chan].fixedFreq=0;
757 chan[c.chan].active=true;
758 chan[c.chan].keyOn=true;
759 chan[c.chan].macroInit(ins);
760 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
761 chan[c.chan].outVol=chan[c.chan].vol;
762 }
763 if (!(chan[c.chan].nextPSGMode.val&8)) {
764 if (isMuted[c.chan]) {
765 rWrite(0x08+c.chan,0);
766 } else if (intellivision && (chan[c.chan].nextPSGMode.getEnvelope())) {
767 rWrite(0x08+c.chan,(chan[c.chan].vol&0xc)<<2);
768 } else {
769 rWrite(0x08+c.chan,(chan[c.chan].vol&15)|((chan[c.chan].nextPSGMode.getEnvelope())<<2));
770 }
771 }
772 break;
773 }
774 case DIV_CMD_NOTE_OFF:
775 chan[c.chan].dac.sample=-1;
776 //if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
777 chan[c.chan].nextPSGMode.val&=~8;
778 chan[c.chan].keyOff=true;
779 chan[c.chan].active=false;
780 chan[c.chan].macroInit(NULL);
781 break;
782 case DIV_CMD_NOTE_OFF_ENV:
783 case DIV_CMD_ENV_RELEASE:
784 chan[c.chan].std.release();
785 break;
786 case DIV_CMD_VOLUME: {
787 chan[c.chan].vol=c.value;
788 if (!chan[c.chan].std.vol.has) {
789 chan[c.chan].outVol=c.value;
790 }
791 if (!(chan[c.chan].nextPSGMode.val&8)) {
792 if (isMuted[c.chan]) {
793 rWrite(0x08+c.chan,0);
794 } else {
795 if (chan[c.chan].active) {
796 if (intellivision && (chan[c.chan].nextPSGMode.getEnvelope())) {
797 rWrite(0x08+c.chan,(chan[c.chan].vol&0xc)<<2);
798 } else {
799 rWrite(0x08+c.chan,(chan[c.chan].vol&15)|((chan[c.chan].nextPSGMode.getEnvelope())<<2));
800 }
801 }
802 }
803 }
804 break;
805 }
806 case DIV_CMD_GET_VOLUME: {
807 return chan[c.chan].vol;
808 break;
809 }
810 case DIV_CMD_INSTRUMENT:
811 if (chan[c.chan].ins!=c.value || c.value2==1) {
812 chan[c.chan].insChanged=true;
813 }
814 chan[c.chan].ins=c.value;
815 break;
816 case DIV_CMD_PITCH: {
817 chan[c.chan].pitch=c.value;
818 chan[c.chan].freqChanged=true;
819 break;
820 }
821 case DIV_CMD_NOTE_PORTA: {
822 int destFreq=NOTE_PERIODIC(c.value2+chan[c.chan].sampleNoteDelta);
823 bool return2=false;
824 if (destFreq>chan[c.chan].baseFreq) {
825 chan[c.chan].baseFreq+=c.value;
826 if (chan[c.chan].baseFreq>=destFreq) {
827 chan[c.chan].baseFreq=destFreq;
828 return2=true;
829 }
830 } else {
831 chan[c.chan].baseFreq-=c.value;
832 if (chan[c.chan].baseFreq<=destFreq) {
833 chan[c.chan].baseFreq=destFreq;
834 return2=true;
835 }
836 }
837 chan[c.chan].freqChanged=true;
838 if (return2) {
839 chan[c.chan].inPorta=false;
840 return 2;
841 }
842 break;
843 }
844 case DIV_CMD_LEGATO: {
845 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+chan[c.chan].sampleNoteDelta);
846 chan[c.chan].freqChanged=true;
847 break;
848 }
849 case DIV_CMD_STD_NOISE_MODE:
850 if (!(chan[c.chan].nextPSGMode.val&8)) {
851 chan[c.chan].nextPSGMode.val|=16;
852 chan[c.chan].tfx.mode=(((c.value&0xf0)>>4)&3)-1;
853 if ((c.value&15)<16) {
854 chan[c.chan].nextPSGMode.val=(c.value+1)&7;
855 chan[c.chan].nextPSGMode.val|=chan[c.chan].curPSGMode.val&16;
856 if (chan[c.chan].active) {
857 chan[c.chan].curPSGMode.val=chan[c.chan].nextPSGMode.val;
858 }
859 if (isMuted[c.chan]) {
860 rWrite(0x08+c.chan,0);
861 } else if (chan[c.chan].active) {
862 if (intellivision && (chan[c.chan].nextPSGMode.getEnvelope())) {
863 rWrite(0x08+c.chan,(chan[c.chan].outVol&0xc)<<2);
864 } else {
865 rWrite(0x08+c.chan,(chan[c.chan].outVol&15)|((chan[c.chan].nextPSGMode.getEnvelope())<<2));
866 }
867 }
868 }
869 }
870 break;
871 case DIV_CMD_STD_NOISE_FREQ:
872 rWrite(0x06,31-c.value);
873 break;
874 case DIV_CMD_AY_ENVELOPE_SET:
875 ayEnvMode=c.value>>4;
876 rWrite(0x0d,ayEnvMode);
877 if (c.value&15) {
878 chan[c.chan].nextPSGMode.val|=4;
879 } else {
880 chan[c.chan].nextPSGMode.val&=~4;
881 }
882 if (!(chan[c.chan].nextPSGMode.val&8) && chan[c.chan].active) {
883 chan[c.chan].curPSGMode.val=chan[c.chan].nextPSGMode.val;
884 }
885 if (isMuted[c.chan]) {
886 rWrite(0x08+c.chan,0);
887 } else if (intellivision && (chan[c.chan].nextPSGMode.getEnvelope())) {
888 rWrite(0x08+c.chan,(chan[c.chan].vol&0xc)<<2);
889 } else {
890 rWrite(0x08+c.chan,(chan[c.chan].vol&15)|((chan[c.chan].nextPSGMode.getEnvelope())<<2));
891 }
892 break;
893 case DIV_CMD_AY_ENVELOPE_LOW:
894 ayEnvPeriod&=0xff00;
895 ayEnvPeriod|=c.value;
896 immWrite(0x0b,ayEnvPeriod);
897 immWrite(0x0c,ayEnvPeriod>>8);
898 break;
899 case DIV_CMD_AY_ENVELOPE_HIGH:
900 ayEnvPeriod&=0xff;
901 ayEnvPeriod|=c.value<<8;
902 immWrite(0x0b,ayEnvPeriod);
903 immWrite(0x0c,ayEnvPeriod>>8);
904 break;
905 case DIV_CMD_AY_ENVELOPE_SLIDE:
906 ayEnvSlide=c.value;
907 break;
908 case DIV_CMD_AY_AUTO_ENVELOPE:
909 chan[c.chan].autoEnvNum=c.value>>4;
910 chan[c.chan].autoEnvDen=c.value&15;
911 chan[c.chan].freqChanged=true;
912 break;
913 case DIV_CMD_AY_IO_WRITE:
914 if (c.value==255) break;
915 if (c.value) { // port B
916 ioPortB=true;
917 portBVal=c.value2;
918 logI("AY I/O port B write: %x",portBVal);
919 } else { // port A
920 ioPortA=true;
921 portAVal=c.value2;
922 logI("AY I/O port A write: %x",portAVal);
923 }
924 updateOutSel(true);
925 immWrite(14+(c.value?1:0),(c.value?portBVal:portAVal));
926 break;
927 case DIV_CMD_AY_NOISE_MASK_AND:
928 chan[c.chan].tfx.num=c.value>>4;
929 chan[c.chan].tfx.den=c.value&15;
930 break;
931 case DIV_CMD_AY_AUTO_PWM: {
932 // best way i could find to do signed :/
933 signed char signVal=c.value;
934 chan[c.chan].tfx.offset=signVal;
935 break;
936 }
937 case DIV_CMD_SAMPLE_MODE:
938 if (c.value>0) {
939 chan[c.chan].nextPSGMode.val|=8;
940 } else {
941 chan[c.chan].nextPSGMode.val&=~8;
942 }
943 if (chan[c.chan].active) {
944 chan[c.chan].curPSGMode.val&=~8;
945 chan[c.chan].curPSGMode.val|=chan[c.chan].nextPSGMode.val&8;
946 }
947 break;
948 case DIV_CMD_SAMPLE_BANK:
949 sampleBank=c.value;
950 if (sampleBank>(parent->song.sample.size()/12)) {
951 sampleBank=parent->song.sample.size()/12;
952 }
953 break;
954 case DIV_CMD_SAMPLE_POS:
955 chan[c.chan].dac.pos=c.value;
956 chan[c.chan].dac.setPos=true;
957 //if (dumpWrites) addWrite(0xffff0005,chan[c.chan].dac.pos);
958 break;
959 case DIV_CMD_MACRO_OFF:
960 chan[c.chan].std.mask(c.value,true);
961 break;
962 case DIV_CMD_MACRO_ON:
963 chan[c.chan].std.mask(c.value,false);
964 break;
965 case DIV_CMD_MACRO_RESTART:
966 chan[c.chan].std.restart(c.value);
967 break;
968 case DIV_CMD_GET_VOLMAX:
969 return 15;
970 break;
971 case DIV_CMD_PRE_PORTA:
972 if (chan[c.chan].active && c.value2) {
973 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AY));
974 }
975 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);
976 chan[c.chan].inPorta=c.value;
977 break;
978 case DIV_CMD_PRE_NOTE:
979 break;
980 default:
981 //printf("WARNING: unimplemented command %d\n",c.cmd);
982 break;
983 }
984 return 1;
985 }
986
987 void DivPlatformAY8910::muteChannel(int ch, bool mute) {
988 isMuted[ch]=mute;
989 if (isMuted[ch]) {
990 rWrite(0x08+ch,0);
991 } else if (chan[ch].active && (chan[ch].nextPSGMode.val&8)) {
992 rWrite(0x08+ch,chan[ch].dac.out);
993 } else {
994 if (intellivision && (chan[ch].nextPSGMode.getEnvelope()) && chan[ch].active) {
995 rWrite(0x08+ch,(chan[ch].vol&0xc)<<2);
996 } else if (chan[ch].active) {
997 rWrite(0x08+ch,(chan[ch].outVol&15)|((chan[ch].nextPSGMode.getEnvelope())<<2));
998 }
999 }
1000 }
1001
1002 void DivPlatformAY8910::forceIns() {
1003 for (int i=0; i<3; i++) {
1004 chan[i].curPSGMode.val&=~8;
1005 chan[i].nextPSGMode.val&=~8;
1006 chan[i].insChanged=true;
1007 chan[i].freqChanged=true;
1008 }
1009 immWrite(0x0b,ayEnvPeriod);
1010 immWrite(0x0c,ayEnvPeriod>>8);
1011 immWrite(0x0d,ayEnvMode);
1012 }
1013
1014 void* DivPlatformAY8910::getChanState(int ch) {
1015 return &chan[ch];
1016 }
1017
1018 DivMacroInt* DivPlatformAY8910::getChanMacroInt(int ch) {
1019 return &chan[ch].std;
1020 }
1021
1022 DivSamplePos DivPlatformAY8910::getSamplePos(int ch) {
1023 if (ch>=3) return DivSamplePos();
1024 return DivSamplePos(
1025 chan[ch].dac.sample,
1026 chan[ch].dac.pos,
1027 chan[ch].dac.rate
1028 );
1029 }
1030
1031 DivDispatchOscBuffer* DivPlatformAY8910::getOscBuffer(int ch) {
1032 return oscBuf[ch];
1033 }
1034
1035 int DivPlatformAY8910::mapVelocity(int ch, float vel) {
1036 return round(15.0*pow(vel,0.33));
1037 }
1038
1039 float DivPlatformAY8910::getGain(int ch, int vol) {
1040 if (vol==0) return 0;
1041 return 1.0/pow(10.0,(float)(15-vol)*2.0/20.0);
1042 }
1043
1044 unsigned char* DivPlatformAY8910::getRegisterPool() {
1045 return regPool;
1046 }
1047
1048 int DivPlatformAY8910::getRegisterPoolSize() {
1049 return 16;
1050 }
1051
1052 void DivPlatformAY8910::flushWrites() {
1053 while (!writes.empty()) writes.pop();
1054 }
1055
1056 bool DivPlatformAY8910::getDCOffRequired() {
1057 return true;
1058 }
1059
1060 void DivPlatformAY8910::reset() {
1061 while (!writes.empty()) writes.pop();
1062 ay->device_reset();
1063 memset(regPool,0,16);
1064 for (int i=0; i<3; i++) {
1065 chan[i]=DivPlatformAY8910::Channel();
1066 chan[i].std.setEngine(parent);
1067 chan[i].vol=0x0f;
1068 }
1069 if (dumpWrites) {
1070 addWrite(0xffffffff,0);
1071 }
1072
1073 SSG_Reset(&ay_atomic);
1074 SSG_SetType(&ay_atomic,
1075 (yamaha?0:1)|
1076 (intellivision?2:0)
1077 );
1078
1079 for (int i=0; i<16; i++) {
1080 oldWrites[i]=-1;
1081 pendingWrites[i]=-1;
1082 }
1083
1084 sampleBank=0;
1085 ayEnvPeriod=0;
1086 ayEnvMode=0;
1087 ayEnvSlide=0;
1088 ayEnvSlideLow=0;
1089
1090 delay=0;
1091 lastOut[0]=0;
1092 lastOut[1]=0;
1093
1094 ioPortA=false;
1095 ioPortB=false;
1096 portAVal=0;
1097 portBVal=0;
1098 }
1099
1100 int DivPlatformAY8910::getOutputCount() {
1101 return 2;
1102 }
1103
1104 bool DivPlatformAY8910::keyOffAffectsArp(int ch) {
1105 return true;
1106 }
1107
1108 bool DivPlatformAY8910::hasAcquireDirect() {
1109 return (!selCore || intellivision);
1110 }
1111
1112 bool DivPlatformAY8910::getLegacyAlwaysSetVolume() {
1113 return false;
1114 }
1115
1116 void DivPlatformAY8910::notifyInsDeletion(void* ins) {
1117 for (int i=0; i<3; i++) {
1118 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
1119 }
1120 }
1121
1122 void DivPlatformAY8910::poke(unsigned int addr, unsigned short val) {
1123 immWrite(addr,val);
1124 }
1125
1126 void DivPlatformAY8910::poke(std::vector<DivRegWrite>& wlist) {
1127 for (DivRegWrite& i: wlist) immWrite(i.addr,i.val);
1128 }
1129
1130 void DivPlatformAY8910::setExtClockDiv(unsigned int eclk, unsigned char ediv) {
1131 if (extMode) {
1132 extClock=eclk;
1133 extDiv=ediv;
1134 }
1135 }
1136
1137 void DivPlatformAY8910::setCore(unsigned char core) {
1138 selCore=core;
1139 }
1140
1141 void DivPlatformAY8910::setFlags(const DivConfig& flags) {
1142 if (extMode) {
1143 chipClock=extClock;
1144 rate=chipClock/extDiv;
1145 clockSel=false;
1146 dacRate=chipClock/dacRateDiv;
1147 } else {
1148 switch (flags.getInt("clockSel",0)) {
1149 case 1:
1150 chipClock=COLOR_PAL*2.0/5.0;
1151 break;
1152 case 2:
1153 chipClock=1750000;
1154 break;
1155 case 3:
1156 chipClock=2000000;
1157 break;
1158 case 4:
1159 chipClock=1500000;
1160 break;
1161 case 5:
1162 chipClock=1000000;
1163 break;
1164 case 6:
1165 chipClock=COLOR_NTSC/4.0;
1166 break;
1167 case 7:
1168 chipClock=COLOR_PAL*3.0/8.0;
1169 break;
1170 case 8:
1171 chipClock=COLOR_PAL*3.0/16.0;
1172 break;
1173 case 9:
1174 chipClock=COLOR_PAL/4.0;
1175 break;
1176 case 10:
1177 chipClock=2097152;
1178 break;
1179 case 11:
1180 chipClock=COLOR_NTSC;
1181 break;
1182 case 12:
1183 chipClock=3600000;
1184 break;
1185 case 13:
1186 chipClock=20000000/16;
1187 break;
1188 case 14:
1189 chipClock=1536000;
1190 break;
1191 case 15:
1192 chipClock=38400*13*4; // 31948800/16
1193 break;
1194 default:
1195 chipClock=COLOR_NTSC/2.0;
1196 break;
1197 }
1198 CHECK_CUSTOM_CLOCK;
1199 }
1200
1201 if (ay!=NULL) delete ay;
1202 switch (flags.getInt("chipType",0)) {
1203 case 1:
1204 clockSel=flags.getBool("halfClock",false);
1205 ay=new ym2149_device(rate,clockSel);
1206 yamaha=true;
1207 sunsoft=false;
1208 intellivision=false;
1209 break;
1210 case 2:
1211 ay=new sunsoft_5b_sound_device(rate);
1212 yamaha=true;
1213 sunsoft=true;
1214 intellivision=false;
1215 clockSel=false;
1216 break;
1217 case 3:
1218 ay=new ay8914_device(rate);
1219 yamaha=false;
1220 sunsoft=false;
1221 intellivision=true;
1222 clockSel=false;
1223 break;
1224 default:
1225 ay=new ay8910_device(rate);
1226 yamaha=false;
1227 sunsoft=false;
1228 intellivision=false;
1229 clockSel=false;
1230 break;
1231 }
1232 ay->device_start();
1233 ay->device_reset();
1234
1235 if (selCore && !intellivision) {
1236 rate=chipClock/2;
1237 dacRate=chipClock*2;
1238 if (sunsoft || clockSel) {
1239 rate=chipClock/4;
1240 dacRate=chipClock/2;
1241 }
1242 } else {
1243 rate=chipClock/8;
1244 dacRate=rate;
1245 }
1246
1247 for (int i=0; i<3; i++) {
1248 oscBuf[i]->setRate(rate);
1249 }
1250
1251 stereo=flags.getBool("stereo",false);
1252 stereoSep=flags.getInt("stereoSep",0)&255;
1253 }
1254
1255 int DivPlatformAY8910::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
1256 parent=p;
1257 dumpWrites=false;
1258 skipRegisterWrites=false;
1259 for (int i=0; i<3; i++) {
1260 isMuted[i]=false;
1261 oscBuf[i]=new DivDispatchOscBuffer;
1262 }
1263 ay=NULL;
1264 setFlags(flags);
1265 reset();
1266 return 3;
1267 }
1268
1269 void DivPlatformAY8910::quit() {
1270 for (int i=0; i<3; i++) {
1271 delete oscBuf[i];
1272 }
1273 if (ay!=NULL) delete ay;
1274 }