Debian Codegraph @

src/engine/platform/c64.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 "c64.h"
21 #include "../engine.h"
22 #include "sound/c64_fp/siddefs-fp.h"
23 #include "IconsFontAwesome4.h"
24 #include <math.h>
25 #include "../../ta-log.h"
26
27 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
28
29 #define CHIP_FREQBASE 524288
30
31 const char* regCheatSheetSID[]={
32 "FreqL0", "00",
33 "FreqH0", "01",
34 "PWL0", "02",
35 "PWH0", "03",
36 "Control0", "04",
37 "AtkDcy0", "05",
38 "StnRis0", "06",
39 "FreqL1", "07",
40 "FreqH1", "08",
41 "PWL1", "09",
42 "PWH1", "0A",
43 "Control1", "0B",
44 "AtkDcy1", "0C",
45 "StnRis1", "0D",
46 "FreqL2", "0E",
47 "FreqH2", "0F",
48 "PWL2", "10",
49 "PWH2", "11",
50 "Control2", "12",
51 "AtkDcy2", "13",
52 "StnRis2", "14",
53 "FCL", "15",
54 "FCH", "16",
55 "FilterRes", "17",
56 "FilterMode", "18",
57 "PotX", "19",
58 "PotY", "1A",
59 "Osc3", "1B",
60 "Env3", "1C",
61 NULL
62 };
63
64 const char** DivPlatformC64::getRegisterSheet() {
65 return regCheatSheetSID;
66 }
67
68 short DivPlatformC64::runFakeFilter(unsigned char ch, int in) {
69 if (!(regPool[0x17]&(1<<ch))) {
70 if (regPool[0x18]&0x80 && ch==2) return 0;
71 float fin=in;
72 if (noSoftPCM) {
73 fin*=(float)(regPool[0x18]&15)/20.0f;
74 }
75 return CLAMP(fin,-32768,32767);
76 }
77
78 // taken from dSID
79 float fin=in;
80 float fout=0;
81 float ctf=fakeCutTable[((regPool[0x15]&7)|(regPool[0x16]<<3))&0x7ff];
82 float reso=(sidIs6581?
83 ((regPool[0x17]>0x5F)?8.0/(float)(regPool[0x17]>>4):1.41):
84 (pow(2,((float)(4-(float)(regPool[0x17]>>4))/8)))
85 );
86 float tmp=fin+fakeBand[ch]*reso+fakeLow[ch];
87 if (regPool[0x18]&0x40) {
88 fout-=tmp;
89 }
90 tmp=fakeBand[ch]-tmp*ctf;
91 fakeBand[ch]=tmp;
92 if (regPool[0x18]&0x20) {
93 fout-=tmp;
94 }
95 tmp=fakeLow[ch]+tmp*ctf;
96 fakeLow[ch]=tmp;
97 if (regPool[0x18]&0x10) {
98 fout+=tmp;
99 }
100
101 if (noSoftPCM) {
102 fout*=(float)(regPool[0x18]&15)/20.0f;
103 }
104 return CLAMP(fout,-32768,32767);
105 }
106
107 void DivPlatformC64::processDAC(int sRate) {
108 bool didWrite=false;
109 if (chan[3].sample>=0 && chan[3].sample<parent->song.sampleLen) {
110 chan[3].pcmPeriod-=chan[3].pcmRate*4;
111 while (chan[3].pcmPeriod<0) {
112 chan[3].pcmPeriod+=sRate;
113 DivSample* s=parent->getSample(chan[3].sample);
114 if (s!=NULL) {
115 if (chan[3].pcmPos<0 || chan[3].pcmPos>=(int)s->samples) {
116 chan[3].pcmOut=15;
117 didWrite=true;
118 } else {
119 chan[3].pcmOut=(0x80+s->data8[chan[3].pcmPos])>>4;
120 didWrite=true;
121 }
122 chan[3].pcmPos++;
123
124 if (s->isLoopable() && chan[3].pcmPos>=s->loopEnd) {
125 chan[3].pcmPos=s->loopStart;
126 } else if (chan[3].pcmPos>=(int)s->samples) {
127 chan[3].sample=-1;
128 didWrite=true;
129 }
130 } else {
131 chan[3].sample=-1;
132 didWrite=true;
133 }
134 }
135 }
136
137 if (didWrite && !isMuted[3]) updateVolume();
138 }
139
140 void DivPlatformC64::acquire(short** buf, size_t len) {
141 int dcOff=(sidCore)?0:sid->get_dc(0);
142 for (int i=0; i<4; i++) {
143 oscBuf[i]->begin(len);
144 }
145 for (size_t i=0; i<len; i++) {
146 // run PCM
147 pcmCycle+=lineRate;
148 while (pcmCycle>=(rate*2)) {
149 pcmCycle-=(rate*2);
150 processDAC(lineRate);
151 }
152
153 // the rest
154 if (!writes.empty()) {
155 QueuedWrite w=writes.front();
156 if (sidCore==2) {
157 dSID_write(sid_d,w.addr,w.val);
158 } else if (sidCore==1) {
159 sid_fp->write(w.addr,w.val);
160 } else {
161 sid->write(w.addr,w.val);
162 }
163 regPool[w.addr&0x1f]=w.val;
164 writes.pop();
165 }
166 if (sidCore==2) {
167 double o=dSID_render(sid_d);
168 buf[0][i]=32767*CLAMP(o,-1.0,1.0);
169 if (++writeOscBuf>=4) {
170 writeOscBuf=0;
171 oscBuf[0]->putSample(i,sid_d->lastOut[0]);
172 oscBuf[1]->putSample(i,sid_d->lastOut[1]);
173 oscBuf[2]->putSample(i,sid_d->lastOut[2]);
174 oscBuf[3]->putSample(i,isMuted[3]?0:(chan[3].pcmOut<<11));
175 }
176 } else if (sidCore==1) {
177 sid_fp->clock(4,&buf[0][i]);
178 if (++writeOscBuf>=4) {
179 writeOscBuf=0;
180 oscBuf[0]->putSample(i,runFakeFilter(0,(sid_fp->lastChanOut[0]-dcOff)>>5));
181 oscBuf[1]->putSample(i,runFakeFilter(1,(sid_fp->lastChanOut[1]-dcOff)>>5));
182 oscBuf[2]->putSample(i,runFakeFilter(2,(sid_fp->lastChanOut[2]-dcOff)>>5));
183 oscBuf[3]->putSample(i,isMuted[3]?0:(chan[3].pcmOut<<11));
184 }
185 } else {
186 sid->clock();
187 buf[0][i]=sid->output();
188 if (++writeOscBuf>=16) {
189 writeOscBuf=0;
190 oscBuf[0]->putSample(i,runFakeFilter(0,(sid->last_chan_out[0]-dcOff)>>5));
191 oscBuf[1]->putSample(i,runFakeFilter(1,(sid->last_chan_out[1]-dcOff)>>5));
192 oscBuf[2]->putSample(i,runFakeFilter(2,(sid->last_chan_out[2]-dcOff)>>5));
193 oscBuf[3]->putSample(i,isMuted[3]?0:(chan[3].pcmOut<<11));
194 }
195 }
196 }
197 for (int i=0; i<4; i++) {
198 oscBuf[i]->end(len);
199 }
200 }
201
202 void DivPlatformC64::updateFilter() {
203 rWrite(0x15,filtCut&7);
204 rWrite(0x16,filtCut>>3);
205 rWrite(0x17,(filtRes<<4)|(chan[2].filter<<2)|(chan[1].filter<<1)|(int)(chan[0].filter));
206 updateVolume();
207 }
208
209 void DivPlatformC64::updateVolume() {
210 if (chan[3].sample>=0 && !isMuted[3]) {
211 rWrite(0x18,(filtControl<<4)|chan[3].pcmOut);
212 } else {
213 rWrite(0x18,(filtControl<<4)|vol);
214 }
215 }
216
217 void DivPlatformC64::tick(bool sysTick) {
218 bool willUpdateFilter=false;
219 for (int _i=0; _i<3; _i++) {
220 int i=chanOrder[_i];
221
222 chan[i].std.next();
223
224 if (sysTick) {
225 if (chan[i].pw_slide!=0) {
226 chan[i].duty-=chan[i].pw_slide;
227 chan[i].duty=CLAMP(chan[i].duty,0,0xfff);
228 rWrite(i*7+2,chan[i].duty&0xff);
229 rWrite(i*7+3,(chan[i].duty>>8)|(chan[i].outVol<<4));
230 }
231 if (cutoff_slide!=0) {
232 filtCut+=cutoff_slide;
233 filtCut=CLAMP(filtCut,0,0x7ff);
234 updateFilter();
235 }
236 }
237
238 if (chan[i].std.vol.had) {
239 vol=MIN(15,chan[i].std.vol.val);
240 willUpdateFilter=true;
241 }
242
243 if (NEW_ARP_STRAT) {
244 chan[i].handleArp();
245 } else if (chan[i].std.arp.had) {
246 if (!chan[i].inPorta) {
247 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
248 }
249 chan[i].freqChanged=true;
250 }
251 if (chan[i].std.duty.had) {
252 DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_C64);
253 if (ins->c64.dutyIsAbs) {
254 chan[i].duty=chan[i].std.duty.val;
255 } else {
256 if (multiplyRel) {
257 chan[i].duty-=((signed char)chan[i].std.duty.val)*4;
258 } else {
259 chan[i].duty-=chan[i].std.duty.val;
260 }
261 }
262 chan[i].duty&=4095;
263 rWrite(i*7+2,chan[i].duty&0xff);
264 rWrite(i*7+3,chan[i].duty>>8);
265 }
266 if (chan[i].std.wave.had) {
267 chan[i].wave=chan[i].std.wave.val;
268 rWrite(i*7+4,(chan[i].wave<<4)|(chan[i].test<<3)|(chan[i].ring<<2)|(chan[i].sync<<1)|(int)(chan[i].active && chan[i].gate));
269 }
270 if (chan[i].std.pitch.had) {
271 if (chan[i].std.pitch.mode) {
272 chan[i].pitch2+=chan[i].std.pitch.val;
273 CLAMP_VAR(chan[i].pitch2,-65535,65535);
274 } else {
275 chan[i].pitch2=chan[i].std.pitch.val;
276 }
277 chan[i].freqChanged=true;
278 }
279 bool condition=chan[i].std.alg.will;
280 DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_C64);
281 if ((!ins->c64.filterIsAbs) || macroRace) {
282 condition=chan[i].std.alg.had;
283 }
284 if (condition) { // new cutoff macro
285 if (ins->c64.filterIsAbs) {
286 filtCut=MIN(2047,chan[i].std.alg.val);
287 } else {
288 if (multiplyRel) {
289 filtCut+=((signed char)chan[i].std.alg.val)*7;
290 } else {
291 filtCut+=chan[i].std.alg.val;
292 }
293 if (filtCut>2047) filtCut=2047;
294 if (filtCut<0) filtCut=0;
295 }
296 willUpdateFilter=true;
297 }
298 if (chan[i].std.ex1.had) {
299 filtControl=chan[i].std.ex1.val&15;
300 willUpdateFilter=true;
301 }
302 if (chan[i].std.ex2.had) {
303 filtRes=chan[i].std.ex2.val&15;
304 willUpdateFilter=true;
305 }
306 if (chan[i].std.ex3.had) {
307 chan[i].filter=(chan[i].std.ex3.val&1);
308 willUpdateFilter=true;
309 }
310 if (chan[i].std.ex4.had) {
311 chan[i].gate=chan[i].std.ex4.val&1;
312 chan[i].sync=chan[i].std.ex4.val&2;
313 chan[i].ring=chan[i].std.ex4.val&4;
314 chan[i].test=chan[i].std.ex4.val&8;
315 chan[i].freqChanged=true;
316 rWrite(i*7+4,(chan[i].wave<<4)|(chan[i].test<<3)|(chan[i].ring<<2)|(chan[i].sync<<1)|(int)(chan[i].active && chan[i].gate));
317 }
318
319 if (chan[i].std.ex5.had) {
320 chan[i].attack=chan[i].std.ex5.val&15;
321 rWrite(i*7+5,(chan[i].attack<<4)|(chan[i].decay));
322 }
323
324 if (chan[i].std.ex6.had) {
325 chan[i].decay=chan[i].std.ex6.val&15;
326 rWrite(i*7+5,(chan[i].attack<<4)|(chan[i].decay));
327 }
328
329 if (chan[i].std.ex7.had) {
330 chan[i].sustain=chan[i].std.ex7.val&15;
331 rWrite(i*7+6,(chan[i].sustain<<4)|(chan[i].release));
332 }
333
334 if (chan[i].std.ex8.had) {
335 chan[i].release=chan[i].std.ex8.val&15;
336 rWrite(i*7+6,(chan[i].sustain<<4)|(chan[i].release));
337 }
338
339 if (sysTick) {
340 if (chan[i].testWhen>0) {
341 if (--chan[i].testWhen<1) {
342 if (!chan[i].resetMask && !chan[i].inPorta) {
343 DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_C64);
344 rWrite(i*7+5,testAD);
345 rWrite(i*7+6,testSR);
346 rWrite(i*7+4,(chan[i].wave<<4)|(ins->c64.noTest?0:8)|(chan[i].test<<3)|(chan[i].ring<<2)|(chan[i].sync<<1));
347 }
348 }
349 }
350 }
351
352 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
353 chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,8,chan[i].pitch2,chipClock,CHIP_FREQBASE);
354 if (chan[i].freq<0) chan[i].freq=0;
355 if (chan[i].freq>0xffff) chan[i].freq=0xffff;
356 if (chan[i].keyOn) {
357 rWrite(i*7+5,(chan[i].attack<<4)|(chan[i].decay));
358 rWrite(i*7+6,(chan[i].sustain<<4)|(chan[i].release));
359 rWrite(i*7+4,(chan[i].wave<<4)|(chan[i].test<<3)|(chan[i].ring<<2)|(chan[i].sync<<1)|(chan[i].gate?1:0));
360 }
361 if (chan[i].keyOff) {
362 rWrite(i*7+5,(chan[i].attack<<4)|(chan[i].decay));
363 rWrite(i*7+6,(chan[i].sustain<<4)|(chan[i].release));
364 rWrite(i*7+4,(chan[i].wave<<4)|(chan[i].test<<3)|(chan[i].ring<<2)|(chan[i].sync<<1)|0);
365 }
366 rWrite(i*7,chan[i].freq&0xff);
367 rWrite(i*7+1,chan[i].freq>>8);
368 if (chan[i].keyOn) chan[i].keyOn=false;
369 if (chan[i].keyOff) chan[i].keyOff=false;
370 chan[i].freqChanged=false;
371 }
372 }
373
374 if (chan[3].freqChanged) {
375 int i=3;
376 double off=1.0;
377 if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
378 DivSample* s=parent->getSample(chan[i].sample);
379 if (s->centerRate<1) {
380 off=1.0;
381 } else {
382 off=(double)s->centerRate/parent->getCenterRate();
383 }
384 }
385 chan[i].pcmRate=off*parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,2,chan[i].pitch2,2,1);
386 if (dumpWrites) addWrite(0xffff0001+(i<<8),chan[i].pcmRate);
387 }
388
389 if (willUpdateFilter) updateFilter();
390 }
391
392 int DivPlatformC64::dispatch(DivCommand c) {
393 if (c.chan>3) return 0;
394 switch (c.cmd) {
395 case DIV_CMD_NOTE_ON: {
396 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_C64);
397
398 if (chan[c.chan].pcm || c.chan>2) {
399 if (skipRegisterWrites) break;
400 if (c.value!=DIV_NOTE_NULL) {
401 chan[c.chan].sample=ins->amiga.getSample(c.value);
402 chan[c.chan].sampleNote=c.value;
403 c.value=ins->amiga.getFreq(c.value);
404 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
405 } else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) {
406 chan[c.chan].sample=ins->amiga.getSample(chan[c.chan].sampleNote);
407 c.value=ins->amiga.getFreq(chan[c.chan].sampleNote);
408 }
409 if (chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) {
410 chan[c.chan].sample=-1;
411 if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
412 break;
413 }
414 if (chan[c.chan].setPos) {
415 chan[c.chan].setPos=false;
416 } else {
417 chan[c.chan].pcmPos=0;
418 }
419 chan[c.chan].pcmPeriod=0;
420 if (c.value!=DIV_NOTE_NULL) {
421 chan[c.chan].baseFreq=parent->calcBaseFreq(2,1,c.value,false);
422 chan[c.chan].freqChanged=true;
423 chan[c.chan].note=c.value;
424 }
425 chan[c.chan].active=true;
426 chan[c.chan].macroInit(ins);
427 chan[c.chan].keyOn=true;
428 break;
429 }
430
431 if (c.value!=DIV_NOTE_NULL) {
432 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
433 chan[c.chan].freqChanged=true;
434 chan[c.chan].note=c.value;
435 }
436 chan[c.chan].active=true;
437 chan[c.chan].keyOn=true;
438 chan[c.chan].test=false;
439 if (((chan[c.chan].insChanged || chan[c.chan].resetDuty || ins->std.waveMacro.len>0) && ins->c64.resetDuty) || chan[c.chan].resetDuty) {
440 chan[c.chan].duty=ins->c64.duty;
441 rWrite(c.chan*7+2,chan[c.chan].duty&0xff);
442 rWrite(c.chan*7+3,chan[c.chan].duty>>8);
443 }
444 if (chan[c.chan].insChanged) {
445 chan[c.chan].wave=(ins->c64.noiseOn<<3)|(ins->c64.pulseOn<<2)|(ins->c64.sawOn<<1)|(int)(ins->c64.triOn);
446 chan[c.chan].attack=ins->c64.a;
447 chan[c.chan].decay=(ins->c64.s==15)?0:ins->c64.d;
448 chan[c.chan].sustain=ins->c64.s;
449 chan[c.chan].release=ins->c64.r;
450 chan[c.chan].ring=ins->c64.ringMod;
451 chan[c.chan].sync=ins->c64.oscSync;
452 }
453 if (chan[c.chan].insChanged || chan[c.chan].resetFilter) {
454 chan[c.chan].filter=ins->c64.toFilter;
455 if (ins->c64.initFilter) {
456 filtCut=ins->c64.cut;
457 filtRes=ins->c64.res;
458 filtControl=(int)(ins->c64.lp)|(ins->c64.bp<<1)|(ins->c64.hp<<2)|(ins->c64.ch3off<<3);
459 }
460 updateFilter();
461 }
462 if (chan[c.chan].insChanged) {
463 chan[c.chan].insChanged=false;
464 }
465 if (keyPriority) {
466 if (chanOrder[1]==c.chan) {
467 chanOrder[1]=chanOrder[2];
468 chanOrder[2]=c.chan;
469 } else if (chanOrder[0]==c.chan) {
470 chanOrder[0]=chanOrder[1];
471 chanOrder[1]=chanOrder[2];
472 chanOrder[2]=c.chan;
473 }
474 }
475 chan[c.chan].macroInit(ins);
476 break;
477 }
478 case DIV_CMD_NOTE_OFF:
479 chan[c.chan].sample=-1;
480 chan[c.chan].pcm=false;
481 chan[c.chan].active=false;
482 chan[c.chan].keyOff=true;
483 chan[c.chan].keyOn=false;
484 //chan[c.chan].macroInit(NULL);
485 break;
486 case DIV_CMD_NOTE_OFF_ENV:
487 if (c.chan>2) break;
488 chan[c.chan].active=false;
489 chan[c.chan].keyOff=true;
490 chan[c.chan].keyOn=false;
491 chan[c.chan].std.release();
492 break;
493 case DIV_CMD_ENV_RELEASE:
494 if (c.chan>2) break;
495 chan[c.chan].std.release();
496 break;
497 case DIV_CMD_INSTRUMENT:
498 if (chan[c.chan].ins!=c.value || c.value2==1) {
499 chan[c.chan].insChanged=true;
500 chan[c.chan].ins=c.value;
501 }
502 break;
503 case DIV_CMD_VOLUME:
504 if (c.chan>2) break;
505 if (chan[c.chan].vol!=c.value) {
506 chan[c.chan].vol=c.value;
507 if (!chan[c.chan].std.vol.has) {
508 chan[c.chan].outVol=c.value;
509 vol=chan[c.chan].outVol;
510 } else {
511 vol=chan[c.chan].vol;
512 }
513 updateFilter();
514 }
515 break;
516 case DIV_CMD_GET_VOLUME:
517 return chan[c.chan].vol;
518 break;
519 case DIV_CMD_PITCH:
520 chan[c.chan].pitch=c.value;
521 chan[c.chan].freqChanged=true;
522 break;
523 case DIV_CMD_NOTE_PORTA: {
524 int destFreq=NOTE_FREQUENCY(c.value2);
525 if (c.chan>2 || chan[c.chan].pcm) {
526 destFreq=parent->calcBaseFreq(2,1,c.value2+chan[c.chan].sampleNoteDelta,false);
527 }
528 bool return2=false;
529 if (destFreq>chan[c.chan].baseFreq) {
530 chan[c.chan].baseFreq+=c.value;
531 if (chan[c.chan].baseFreq>=destFreq) {
532 chan[c.chan].baseFreq=destFreq;
533 return2=true;
534 }
535 } else {
536 chan[c.chan].baseFreq-=c.value;
537 if (chan[c.chan].baseFreq<=destFreq) {
538 chan[c.chan].baseFreq=destFreq;
539 return2=true;
540 }
541 }
542 chan[c.chan].freqChanged=true;
543 if (return2) {
544 chan[c.chan].inPorta=false;
545 return 2;
546 }
547 break;
548 }
549 case DIV_CMD_STD_NOISE_MODE:
550 if (c.chan>2) break;
551 chan[c.chan].duty=(c.value*4095)/100;
552 rWrite(c.chan*7+2,chan[c.chan].duty&0xff);
553 rWrite(c.chan*7+3,chan[c.chan].duty>>8);
554 break;
555 case DIV_CMD_C64_FINE_DUTY:
556 if (c.chan>2) break;
557 chan[c.chan].duty=c.value;
558 rWrite(c.chan*7+2,chan[c.chan].duty&0xff);
559 rWrite(c.chan*7+3,chan[c.chan].duty>>8);
560 break;
561 case DIV_CMD_WAVE:
562 if (c.chan>2) break;
563 chan[c.chan].wave=c.value;
564 rWrite(c.chan*7+4,(chan[c.chan].wave<<4)|(chan[c.chan].test<<3)|(chan[c.chan].ring<<2)|(chan[c.chan].sync<<1)|(int)(chan[c.chan].active && chan[c.chan].gate));
565 break;
566 case DIV_CMD_LEGATO:
567 if (c.chan>2 || chan[c.chan].pcm) {
568 chan[c.chan].baseFreq=parent->calcBaseFreq(2,1,c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)),false);
569 } else {
570 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
571 }
572 chan[c.chan].freqChanged=true;
573 chan[c.chan].note=c.value;
574 break;
575 case DIV_CMD_PRE_PORTA:
576 if (chan[c.chan].active && c.value2) {
577 if (parent->song.resetMacroOnPorta || parent->song.preNoteNoEffect) {
578 chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_C64));
579 chan[c.chan].keyOn=true;
580 }
581 }
582 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_FREQUENCY(chan[c.chan].note);
583 chan[c.chan].inPorta=c.value;
584 break;
585 case DIV_CMD_PRE_NOTE:
586 if (c.chan>2) break;
587 if (resetTime) chan[c.chan].testWhen=c.value-resetTime+1;
588 break;
589 case DIV_CMD_GET_VOLMAX:
590 return 15;
591 break;
592 case DIV_CMD_C64_CUTOFF:
593 if (c.chan>2) break;
594 if (c.value>100) c.value=100;
595 filtCut=(c.value+2)*2047/102;
596 updateFilter();
597 break;
598 case DIV_CMD_C64_FINE_CUTOFF:
599 if (c.chan>2) break;
600 filtCut=c.value;
601 updateFilter();
602 break;
603 case DIV_CMD_C64_RESONANCE:
604 if (c.chan>2) break;
605 if (c.value>15) c.value=15;
606 filtRes=c.value;
607 updateFilter();
608 break;
609 case DIV_CMD_C64_FILTER_MODE:
610 if (c.chan>2) break;
611 filtControl=c.value&7;
612 updateFilter();
613 break;
614 case DIV_CMD_C64_RESET_TIME:
615 if (c.chan>2) break;
616 resetTime=c.value;
617 break;
618 case DIV_CMD_C64_RESET_MASK:
619 if (c.chan>2) break;
620 chan[c.chan].resetMask=c.value;
621 break;
622 case DIV_CMD_C64_FILTER_RESET:
623 if (c.chan>2) break;
624 if (c.value&15) {
625 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_C64);
626 if (ins->c64.initFilter) {
627 filtCut=ins->c64.cut;
628 updateFilter();
629 }
630 }
631 chan[c.chan].resetFilter=c.value>>4;
632 break;
633 case DIV_CMD_C64_DUTY_RESET:
634 if (c.chan>2) break;
635 if (c.value&15) {
636 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_C64);
637 chan[c.chan].duty=ins->c64.duty;
638 rWrite(c.chan*7+2,chan[c.chan].duty&0xff);
639 rWrite(c.chan*7+3,chan[c.chan].duty>>8);
640 }
641 chan[c.chan].resetDuty=c.value>>4;
642 break;
643 case DIV_CMD_C64_EXTENDED:
644 if (c.chan>2) break;
645 switch (c.value>>4) {
646 case 0:
647 chan[c.chan].attack=c.value&15;
648 if (!no1EUpdate) {
649 rWrite(c.chan*7+5,(chan[c.chan].attack<<4)|(chan[c.chan].decay));
650 }
651 break;
652 case 1:
653 chan[c.chan].decay=c.value&15;
654 if (!no1EUpdate) {
655 rWrite(c.chan*7+5,(chan[c.chan].attack<<4)|(chan[c.chan].decay));
656 }
657 break;
658 case 2:
659 chan[c.chan].sustain=c.value&15;
660 if (!no1EUpdate) {
661 rWrite(c.chan*7+6,(chan[c.chan].sustain<<4)|(chan[c.chan].release));
662 }
663 break;
664 case 3:
665 chan[c.chan].release=c.value&15;
666 if (!no1EUpdate) {
667 rWrite(c.chan*7+6,(chan[c.chan].sustain<<4)|(chan[c.chan].release));
668 }
669 break;
670 case 4:
671 chan[c.chan].ring=c.value;
672 rWrite(c.chan*7+4,(chan[c.chan].wave<<4)|(chan[c.chan].test<<3)|(chan[c.chan].ring<<2)|(chan[c.chan].sync<<1)|(int)(chan[c.chan].active && chan[c.chan].gate));
673 break;
674 case 5:
675 chan[c.chan].sync=c.value;
676 rWrite(c.chan*7+4,(chan[c.chan].wave<<4)|(chan[c.chan].test<<3)|(chan[c.chan].ring<<2)|(chan[c.chan].sync<<1)|(int)(chan[c.chan].active && chan[c.chan].gate));
677 break;
678 case 6:
679 filtControl&=7;
680 filtControl|=(!!c.value)<<3;
681 break;
682 }
683 break;
684 case DIV_CMD_C64_AD:
685 if (c.chan>2) break;
686 chan[c.chan].attack=c.value>>4;
687 chan[c.chan].decay=c.value&15;
688 rWrite(c.chan*7+5,(chan[c.chan].attack<<4)|(chan[c.chan].decay));
689 break;
690 case DIV_CMD_C64_SR:
691 if (c.chan>2) break;
692 chan[c.chan].sustain=c.value>>4;
693 chan[c.chan].release=c.value&15;
694 rWrite(c.chan*7+6,(chan[c.chan].sustain<<4)|(chan[c.chan].release));
695 break;
696 case DIV_CMD_C64_PW_SLIDE:
697 if (c.chan>2) break;
698 chan[c.chan].pw_slide=c.value*c.value2;
699 break;
700 case DIV_CMD_C64_CUTOFF_SLIDE:
701 if (c.chan>2) break;
702 cutoff_slide=c.value*c.value2;
703 break;
704 case DIV_CMD_MACRO_OFF:
705 chan[c.chan].std.mask(c.value,true);
706 break;
707 case DIV_CMD_MACRO_ON:
708 chan[c.chan].std.mask(c.value,false);
709 break;
710 case DIV_CMD_MACRO_RESTART:
711 chan[c.chan].std.restart(c.value);
712 break;
713 default:
714 break;
715 }
716 return 1;
717 }
718
719 void DivPlatformC64::muteChannel(int ch, bool mute) {
720 isMuted[ch]=mute;
721 if (sidCore==2) {
722 dSID_setMuteMask(
723 sid_d,
724 (isMuted[0]?0:1)|
725 (isMuted[1]?0:2)|
726 (isMuted[2]?0:4)
727 );
728 } else if (sidCore==1) {
729 sid_fp->mute(ch,mute);
730 } else {
731 sid->set_is_muted(ch,mute);
732 }
733 if (ch==3) updateVolume();
734 }
735
736 void DivPlatformC64::forceIns() {
737 for (int i=0; i<4; i++) {
738 chan[i].insChanged=true;
739 chan[i].testWhen=0;
740 if (chan[i].active) {
741 chan[i].keyOn=true;
742 chan[i].freqChanged=true;
743 }
744 }
745 updateFilter();
746 }
747
748 void DivPlatformC64::notifyInsChange(int ins) {
749 for (int i=0; i<4; i++) {
750 if (chan[i].ins==ins) {
751 chan[i].insChanged=true;
752 }
753 }
754 }
755
756 void DivPlatformC64::notifyInsDeletion(void* ins) {
757 for (int i=0; i<4; i++) {
758 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
759 }
760 }
761
762 void* DivPlatformC64::getChanState(int ch) {
763 return &chan[ch];
764 }
765
766 DivMacroInt* DivPlatformC64::getChanMacroInt(int ch) {
767 return &chan[ch].std;
768 }
769
770 void DivPlatformC64::getPaired(int ch, std::vector<DivChannelPair>& ret) {
771 if (chan[ch].ring) {
772 if (ch==0){
773 ret.push_back(DivChannelPair(_("ring"),2));
774 } else {
775 ret.push_back(DivChannelPair(_("ring"),(ch-1)%3));
776 }
777 }
778 if (chan[ch].sync) {
779 if (ch==0) {
780 ret.push_back(DivChannelPair(_("sync"),2));
781 } else {
782 ret.push_back(DivChannelPair(_("sync"),(ch-1)%3));
783 }
784 }
785 }
786
787 DivChannelModeHints DivPlatformC64::getModeHints(int ch) {
788 DivChannelModeHints ret;
789 ret.count=1;
790 ret.hint[0]=ICON_FA_BELL_SLASH_O;
791 ret.type[0]=0;
792 if (ch==2 && (filtControl&8)) {
793 ret.type[0]=7;
794 } else if (chan[ch].test && !chan[ch].gate) {
795 ret.type[0]=5;
796 } else if (chan[ch].test) {
797 ret.type[0]=6;
798 } else if (!chan[ch].gate) {
799 ret.type[0]=4;
800 }
801
802 return ret;
803 }
804
805 DivDispatchOscBuffer* DivPlatformC64::getOscBuffer(int ch) {
806 return oscBuf[ch];
807 }
808
809 unsigned char* DivPlatformC64::getRegisterPool() {
810 return regPool;
811 }
812
813 int DivPlatformC64::getRegisterPoolSize() {
814 return 32;
815 }
816
817 bool DivPlatformC64::getDCOffRequired() {
818 return true;
819 }
820
821 bool DivPlatformC64::getWantPreNote() {
822 return true;
823 }
824
825 bool DivPlatformC64::isVolGlobal() {
826 return true;
827 }
828
829 float DivPlatformC64::getPostAmp() {
830 return (sidCore==1)?3.0f:1.0f;
831 }
832
833 void DivPlatformC64::reset() {
834 while (!writes.empty()) writes.pop();
835 for (int i=0; i<4; i++) {
836 chan[i]=DivPlatformC64::Channel();
837 chan[i].std.setEngine(parent);
838 fakeLow[i]=0;
839 fakeBand[i]=0;
840 chan[i].pw_slide=0;
841 }
842
843 cutoff_slide=0;
844 pcmCycle=0;
845
846 if (sidCore==2) {
847 dSID_init(sid_d,chipClock,rate,sidIs6581?6581:8580,needInitTables);
848 dSID_setMuteMask(
849 sid_d,
850 (isMuted[0]?0:1)|
851 (isMuted[1]?0:2)|
852 (isMuted[2]?0:4)
853 );
854 needInitTables=false;
855 } else if (sidCore==1) {
856 sid_fp->reset();
857 for (int i=0; i<3; i++) {
858 sid_fp->write(i*7+5,testAD);
859 sid_fp->write(i*7+6,testSR);
860 sid_fp->write(i*7+4,8);
861 }
862 sid_fp->clockSilent(30000);
863 for (int i=0; i<3; i++) {
864 sid_fp->write(i*7+5,testAD);
865 sid_fp->write(i*7+6,testSR);
866 sid_fp->write(i*7+4,0);
867 }
868 sid_fp->clockSilent(30000);
869 } else {
870 sid->reset();
871 }
872 memset(regPool,0,32);
873
874 rWrite(0x18,0x0f);
875
876 filtControl=7;
877 filtRes=0;
878 filtCut=2047;
879 resetTime=initResetTime;
880 vol=15;
881
882 chanOrder[0]=0;
883 chanOrder[1]=1;
884 chanOrder[2]=2;
885 }
886
887 void DivPlatformC64::poke(unsigned int addr, unsigned short val) {
888 rWrite(addr,val);
889 }
890
891 void DivPlatformC64::poke(std::vector<DivRegWrite>& wlist) {
892 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
893 }
894
895 void DivPlatformC64::setChipModel(bool is6581) {
896 sidIs6581=is6581;
897 }
898
899 void DivPlatformC64::setCore(unsigned char which) {
900 sidCore=which;
901 }
902
903 void DivPlatformC64::setFlags(const DivConfig& flags) {
904 switch (flags.getInt("clockSel",0)) {
905 case 0x0: // NTSC C64
906 chipClock=COLOR_NTSC*2.0/7.0;
907 lineRate=15734;
908 break;
909 case 0x1: // PAL C64
910 chipClock=COLOR_PAL*2.0/9.0;
911 lineRate=15625;
912 break;
913 case 0x2: // SSI 2001
914 default:
915 chipClock=14318180.0/16.0;
916 lineRate=15734;
917 break;
918 }
919 CHECK_CUSTOM_CLOCK;
920 rate=chipClock;
921 if (sidCore>0) {
922 rate/=(sidCore==2)?coreQuality:4;
923 if (sidCore==1) sid_fp->setSamplingParameters(chipClock,reSIDfp::DECIMATE,rate,0);
924 }
925 for (int i=0; i<4; i++) {
926 oscBuf[i]->setRate(rate);
927 }
928 keyPriority=flags.getBool("keyPriority",true);
929 no1EUpdate=flags.getBool("no1EUpdate",false);
930 multiplyRel=flags.getBool("multiplyRel",false);
931 macroRace=flags.getBool("macroRace",false);
932 testAD=((flags.getInt("testAttack",0)&15)<<4)|(flags.getInt("testDecay",0)&15);
933 testSR=((flags.getInt("testSustain",0)&15)<<4)|(flags.getInt("testRelease",0)&15);
934 initResetTime=flags.getInt("initResetTime",2);
935 if (initResetTime<0) initResetTime=1;
936
937 // init fake filter table
938 // taken from dSID
939 double oscBufRate=(double)rate/((sidCore==0)?16.0:4.0);
940 double cutRatio=-2.0*3.14*(sidIs6581?((oscBufRate/44100.0)*(20000.0/256.0)):(12500.0/256.0))/oscBufRate;
941
942 for (int i=0; i<2048; i++) {
943 double c=(double)i/8.0+0.2;
944 if (sidIs6581) {
945 if (c<24) {
946 c=2.0*sin(771.78/oscBufRate);
947 } else {
948 c=(44100.0/oscBufRate)-1.263*(44100.0/oscBufRate)*exp(c*cutRatio);
949 }
950 } else {
951 c=1-exp(c*cutRatio);
952 }
953 fakeCutTable[i]=c;
954 }
955 }
956
957 void DivPlatformC64::setCoreQuality(unsigned char q) {
958 switch (q) {
959 case 0:
960 coreQuality=32;
961 break;
962 case 1:
963 coreQuality=16;
964 break;
965 case 2:
966 coreQuality=8;
967 break;
968 case 3:
969 coreQuality=4;
970 break;
971 case 4:
972 coreQuality=2;
973 break;
974 case 5:
975 coreQuality=1;
976 break;
977 default:
978 coreQuality=4;
979 break;
980 }
981 }
982
983 void DivPlatformC64::setSoftPCM(bool isSoft) {
984 noSoftPCM=!isSoft;
985 }
986
987 int DivPlatformC64::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
988 parent=p;
989 dumpWrites=false;
990 skipRegisterWrites=false;
991 needInitTables=true;
992 writeOscBuf=0;
993 for (int i=0; i<4; i++) {
994 isMuted[i]=false;
995 oscBuf[i]=new DivDispatchOscBuffer;
996 }
997
998 if (sidCore==2) {
999 sid=NULL;
1000 sid_fp=NULL;
1001 sid_d=new struct SID_chip;
1002 } else if (sidCore==1) {
1003 sid=NULL;
1004 sid_fp=new reSIDfp::SID;
1005 sid_d=NULL;
1006 } else {
1007 sid=new SID;
1008 sid_fp=NULL;
1009 sid_d=NULL;
1010 }
1011
1012 if (sidIs6581) {
1013 if (sidCore==2) {
1014 // do nothing
1015 } else if (sidCore==1) {
1016 sid_fp->setChipModel(reSIDfp::MOS6581);
1017 } else {
1018 sid->set_chip_model(MOS6581);
1019 }
1020 } else {
1021 if (sidCore==2) {
1022 // do nothing
1023 } else if (sidCore==1) {
1024 sid_fp->setChipModel(reSIDfp::MOS8580);
1025 } else {
1026 sid->set_chip_model(MOS8580);
1027 }
1028 }
1029
1030 setFlags(flags);
1031
1032 reset();
1033
1034 return 3;
1035 }
1036
1037 void DivPlatformC64::quit() {
1038 for (int i=0; i<4; i++) {
1039 delete oscBuf[i];
1040 }
1041 if (sid!=NULL) delete sid;
1042 if (sid_fp!=NULL) delete sid_fp;
1043 if (sid_d!=NULL) delete sid_d;
1044 }
1045
1046 DivPlatformC64::~DivPlatformC64() {
1047 }