Debian Codegraph @

src/engine/platform/sid2.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 // thanks LTVA
21
22 #include "sid2.h"
23 #include "../engine.h"
24 #include "IconsFontAwesome4.h"
25 #include <math.h>
26 #include "../../ta-log.h"
27
28 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
29
30 #define CHIP_FREQBASE 524288
31
32 const char* regCheatSheetSID2[]={
33 "FreqL0", "00",
34 "FreqH0", "01",
35 "PWL0", "02",
36 "PWH0Vol", "03",
37 "Control0", "04",
38 "AtkDcy0", "05",
39 "StnRis0", "06",
40 "FreqL1", "07",
41 "FreqH1", "08",
42 "PWL1", "09",
43 "PWH1Vol", "0A",
44 "Control1", "0B",
45 "AtkDcy1", "0C",
46 "StnRis1", "0D",
47 "FreqL2", "0E",
48 "FreqH2", "0F",
49 "PWL2", "10",
50 "PWH2Vol", "11",
51 "Control2", "12",
52 "AtkDcy2", "13",
53 "StnRis2", "14",
54
55 "FCL0Ctrl", "15",
56 "FCH0", "16",
57 "FilterRes0", "17",
58
59 "FCL1Ctrl", "18",
60 "FCH1", "19",
61 "FilterRes1", "1A",
62
63 "FCL2Ctrl", "1B",
64 "FCH2", "1C",
65 "FilterRes2", "1D",
66
67 "NoiModeFrMSB01", "1E",
68 "WaveMixModeFrMSB2", "1F",
69 NULL
70 };
71
72 const char** DivPlatformSID2::getRegisterSheet() {
73 return regCheatSheetSID2;
74 }
75
76 void DivPlatformSID2::acquire(short** buf, size_t len) {
77 for (int i=0; i<3; i++) {
78 oscBuf[i]->begin(len);
79 }
80
81 for (size_t i=0; i<len; i++)
82 {
83 if (!writes.empty())
84 {
85 QueuedWrite w=writes.front();
86 sid2->write(w.addr,w.val);
87 regPool[w.addr&0x1f]=w.val;
88 writes.pop();
89 }
90
91 sid2->clock();
92 buf[0][i]=sid2->output();
93 if (++writeOscBuf>=16)
94 {
95 writeOscBuf=0;
96
97 for(int j = 0; j < 3; j++)
98 {
99 int co=sid2->chan_out[j]>>2;
100 if (co<-32768) co=-32768;
101 if (co>32767) co=32767;
102 oscBuf[j]->putSample(i,co);
103 }
104 }
105 }
106
107 for (int i=0; i<3; i++) {
108 oscBuf[i]->end(len);
109 }
110 }
111
112 void DivPlatformSID2::updateFilter(int channel)
113 {
114 rWrite(0x15 + 3 * channel,(chan[channel].filtCut&15) | ((chan[channel].filtControl & 7) << 4) | (chan[channel].filter << 7));
115 rWrite(0x16 + 3 * channel,(chan[channel].filtCut >> 4));
116 rWrite(0x17 + 3 * channel,chan[channel].filtRes);
117 }
118
119 void DivPlatformSID2::tick(bool sysTick) {
120 for (int _i=0; _i<3; _i++) {
121 int i=chanOrder[_i];
122
123 bool willUpdateFilter = false;
124
125 chan[i].std.next();
126
127 if (sysTick) {
128 if (chan[i].pw_slide!=0) {
129 chan[i].duty-=chan[i].pw_slide;
130 chan[i].duty=CLAMP(chan[i].duty,0,0xfff);
131 rWrite(i*7+2,chan[i].duty&0xff);
132 rWrite(i*7+3,(chan[i].duty>>8)|(chan[i].outVol<<4));
133 }
134 if (chan[i].cutoff_slide!=0) {
135 chan[i].filtCut+=chan[i].cutoff_slide;
136 chan[i].filtCut=CLAMP(chan[i].filtCut,0,0xfff);
137 updateFilter(i);
138 }
139 }
140
141 if (chan[i].std.vol.had) {
142 chan[i].outVol=VOL_SCALE_LINEAR(chan[i].vol&15,MIN(15,chan[i].std.vol.val),15);
143 rWrite(i*7+3,(chan[i].duty>>8) | (chan[i].outVol << 4));
144 }
145
146 if (NEW_ARP_STRAT) {
147 chan[i].handleArp();
148 } else if (chan[i].std.arp.had) {
149 if (!chan[i].inPorta) {
150 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
151 }
152 chan[i].freqChanged=true;
153 }
154 if (chan[i].std.duty.had) {
155 DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_SID2);
156 if (ins->c64.dutyIsAbs) {
157 chan[i].duty=chan[i].std.duty.val;
158 } else {
159 chan[i].duty-=chan[i].std.duty.val;
160 }
161 chan[i].duty&=4095;
162 rWrite(i*7+2,chan[i].duty&0xff);
163 rWrite(i*7+3,(chan[i].duty>>8) | (chan[i].outVol << 4));
164 }
165 if (chan[i].std.wave.had) {
166 chan[i].wave=chan[i].std.wave.val;
167 rWrite(i*7+4,(chan[i].wave<<4)|0|(chan[i].ring<<2)|(chan[i].sync<<1)|(int)(chan[i].active && chan[i].gate));
168
169 chan[i].freqChanged=true; //to update freq (if only noise was enabled/disabled)
170 }
171 if (chan[i].std.fms.had) {
172 chan[i].noise_mode=chan[i].std.fms.val;
173 rWrite(0x1e, (chan[0].noise_mode) | (chan[1].noise_mode << 2) | (chan[2].noise_mode << 4) | ((chan[0].freq >> 16) << 6) | ((chan[1].freq >> 16) << 7));
174
175 chan[i].freqChanged=true; //to update freq (if only noise was enabled and periodic noise mode is set)
176 }
177 if (chan[i].std.ams.had) {
178 chan[i].mix_mode=chan[i].std.ams.val;
179 rWrite(0x1f, (chan[0].mix_mode) | (chan[1].mix_mode << 2) | (chan[2].mix_mode << 4) | ((chan[2].freq >> 16) << 6));
180 }
181 if (chan[i].std.pitch.had) {
182 if (chan[i].std.pitch.mode) {
183 chan[i].pitch2+=chan[i].std.pitch.val;
184 CLAMP_VAR(chan[i].pitch2,-65535,65535);
185 } else {
186 chan[i].pitch2=chan[i].std.pitch.val;
187 }
188 chan[i].freqChanged=true;
189 }
190 if (chan[i].std.alg.had) { // new cutoff macro
191 DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_SID2);
192 if (ins->c64.filterIsAbs) {
193 chan[i].filtCut=MIN(4095,chan[i].std.alg.val);
194 } else {
195 chan[i].filtCut+=chan[i].std.alg.val;
196 if (chan[i].filtCut > 4095) chan[i].filtCut = 4095;
197 if (chan[i].filtCut<0) chan[i].filtCut=0;
198 }
199 willUpdateFilter=true;
200 }
201 if (chan[i].std.ex1.had) {
202 chan[i].filtControl=chan[i].std.ex1.val&15;
203 willUpdateFilter=true;
204 }
205 if (chan[i].std.ex2.had) {
206 chan[i].filtRes=chan[i].std.ex2.val&255;
207 willUpdateFilter=true;
208 }
209 if (chan[i].std.ex3.had) {
210 chan[i].filter=(chan[i].std.ex3.val&1);
211 willUpdateFilter=true;
212 }
213 if (chan[i].std.ex4.had) {
214 chan[i].gate=chan[i].std.ex4.val&1;
215 chan[i].sync=chan[i].std.ex4.val&2;
216 chan[i].ring=chan[i].std.ex4.val&4;
217 chan[i].freqChanged=true;
218 rWrite(i*7+4,(chan[i].wave<<4)|0|(chan[i].ring<<2)|(chan[i].sync<<1)|(int)(chan[i].active && chan[i].gate));
219 }
220
221 if (chan[i].std.phaseReset.had) {
222 chan[i].test=(chan[i].std.phaseReset.val&1);
223 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));
224 rWrite(i*7+4,(chan[i].wave<<4)|0|(chan[i].ring<<2)|(chan[i].sync<<1)|(int)(chan[i].active && chan[i].gate));
225 chan[i].test = false;
226 }
227
228 if (chan[i].std.ex5.had) {
229 chan[i].attack=chan[i].std.ex5.val&15;
230 rWrite(i*7+5,(chan[i].attack<<4)|(chan[i].decay));
231 }
232
233 if (chan[i].std.ex6.had) {
234 chan[i].decay=chan[i].std.ex6.val&15;
235 rWrite(i*7+5,(chan[i].attack<<4)|(chan[i].decay));
236 }
237
238 if (chan[i].std.ex7.had) {
239 chan[i].sustain=chan[i].std.ex7.val&15;
240 rWrite(i*7+6,(chan[i].sustain<<4)|(chan[i].release));
241 }
242
243 if (chan[i].std.ex8.had) {
244 chan[i].release=chan[i].std.ex8.val&15;
245 rWrite(i*7+6,(chan[i].sustain<<4)|(chan[i].release));
246 }
247
248 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
249 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);
250 if (chan[i].freq<0) chan[i].freq=0;
251 if (chan[i].freq>0x1ffff) chan[i].freq=0x1ffff;
252 if (chan[i].keyOn)
253 {
254 if(!chan[i].resetMask)
255 {
256 chan[i].gate = true;
257
258 rWrite(i*7+4,(chan[i].wave<<4)|0|(chan[i].ring<<2)|(chan[i].sync<<1)|(0));
259 rWrite(i*7+4,(chan[i].wave<<4)|0|(chan[i].ring<<2)|(chan[i].sync<<1)|(chan[i].gate?1:0));
260 rWrite(i*7+5,(chan[i].attack<<4)|(chan[i].decay));
261 rWrite(i*7+6,(chan[i].sustain<<4)|(chan[i].release));
262 }
263
264 rWrite(i*7+3, (chan[i].duty>>8) | (chan[i].outVol << 4)); //set volume
265
266 rWrite(0x1e, (chan[0].noise_mode) | (chan[1].noise_mode << 2) | (chan[2].noise_mode << 4) | ((chan[0].freq >> 16) << 6) | ((chan[1].freq >> 16) << 7));
267 rWrite(0x1f, (chan[0].mix_mode) | (chan[1].mix_mode << 2) | (chan[2].mix_mode << 4) | ((chan[2].freq >> 16) << 6));
268 }
269 if (chan[i].keyOff)
270 {
271 chan[i].gate = false;
272 rWrite(i*7+5,(chan[i].attack<<4)|(chan[i].decay));
273 rWrite(i*7+6,(chan[i].sustain<<4)|(chan[i].release));
274 rWrite(i*7+4,(chan[i].wave<<4)|0|(chan[i].ring<<2)|(chan[i].sync<<1)|0);
275 }
276
277 if(chan[i].wave == 0x8) //if we have noise (noise only, since tone frequency would be wrong)
278 {
279 if(chan[i].noise_mode == 1) //1st short noise
280 {
281 chan[i].freq = (int)((double)chan[i].freq * 523.25 / 349.0); //these numbers and later determined by spectrum analyzer peak of looped noise signal at known frequency (frequency known for tone that would play if tone wave was enabled)
282 }
283
284 if(chan[i].noise_mode == 2) //2nd short noise
285 {
286 chan[i].freq = (int)((double)chan[i].freq * 523.25 / 270.0);
287 }
288
289 if(chan[i].noise_mode == 3) //3rd short noise
290 {
291 chan[i].freq = (int)((double)chan[i].freq * 523.25 / 133.0);
292 }
293 }
294
295 if (chan[i].freq<0) chan[i].freq=0;
296 if (chan[i].freq>0x1ffff) chan[i].freq=0x1ffff;
297
298 rWrite(i*7,chan[i].freq&0xff);
299 rWrite(i*7+1,chan[i].freq>>8);
300 rWrite(0x1e, (chan[0].noise_mode) | (chan[1].noise_mode << 2) | (chan[2].noise_mode << 4) | ((chan[0].freq >> 16) << 6) | ((chan[1].freq >> 16) << 7));
301 rWrite(0x1f, (chan[0].mix_mode) | (chan[1].mix_mode << 2) | (chan[2].mix_mode << 4) | ((chan[2].freq >> 16) << 6));
302 if (chan[i].keyOn) chan[i].keyOn=false;
303 if (chan[i].keyOff) chan[i].keyOff=false;
304 chan[i].freqChanged=false;
305 }
306
307 if (willUpdateFilter) updateFilter(i);
308 }
309 }
310
311 int DivPlatformSID2::dispatch(DivCommand c) {
312 if (c.chan>2) return 0;
313 switch (c.cmd) {
314 case DIV_CMD_NOTE_ON: {
315 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SID2);
316 if (c.value!=DIV_NOTE_NULL) {
317 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
318 chan[c.chan].freqChanged=true;
319 chan[c.chan].note=c.value;
320 }
321 chan[c.chan].active=true;
322 chan[c.chan].keyOn=true;
323 chan[c.chan].test=false;
324
325 if (((chan[c.chan].insChanged || chan[c.chan].resetDuty || ins->std.waveMacro.len>0) && ins->c64.resetDuty) || chan[c.chan].resetDuty) {
326 chan[c.chan].duty=ins->c64.duty;
327 rWrite(c.chan*7+2,chan[c.chan].duty&0xff);
328 rWrite(c.chan*7+3,(chan[c.chan].duty>>8) | (chan[c.chan].outVol << 4));
329 }
330 if (chan[c.chan].insChanged) {
331 chan[c.chan].wave=(ins->c64.noiseOn<<3)|(ins->c64.pulseOn<<2)|(ins->c64.sawOn<<1)|(int)(ins->c64.triOn);
332 chan[c.chan].attack=ins->c64.a;
333 chan[c.chan].decay=(ins->c64.s==15)?0:ins->c64.d;
334 chan[c.chan].sustain=ins->c64.s;
335 chan[c.chan].release=ins->c64.r;
336 chan[c.chan].ring=ins->c64.ringMod;
337 chan[c.chan].sync=ins->c64.oscSync;
338
339 chan[c.chan].noise_mode = ins->sid2.noiseMode;
340 chan[c.chan].mix_mode = ins->sid2.mixMode;
341 }
342 if (chan[c.chan].insChanged || chan[c.chan].resetFilter) {
343 chan[c.chan].filter=ins->c64.toFilter;
344 if (ins->c64.initFilter) {
345 chan[c.chan].filtCut=ins->c64.cut;
346 chan[c.chan].filtRes=ins->c64.res;
347 chan[c.chan].filtControl=(int)(ins->c64.lp)|(ins->c64.bp<<1)|(ins->c64.hp<<2);
348 }
349 updateFilter(c.chan);
350 }
351 if (chan[c.chan].insChanged) {
352 chan[c.chan].insChanged=false;
353 }
354 if (keyPriority) {
355 if (chanOrder[1]==c.chan) {
356 chanOrder[1]=chanOrder[2];
357 chanOrder[2]=c.chan;
358 } else if (chanOrder[0]==c.chan) {
359 chanOrder[0]=chanOrder[1];
360 chanOrder[1]=chanOrder[2];
361 chanOrder[2]=c.chan;
362 }
363 }
364 chan[c.chan].macroInit(ins);
365 break;
366 }
367 case DIV_CMD_NOTE_OFF:
368 chan[c.chan].active=false;
369 chan[c.chan].keyOff=true;
370 chan[c.chan].keyOn=false;
371 //chan[c.chan].macroInit(NULL);
372 break;
373 case DIV_CMD_NOTE_OFF_ENV:
374 chan[c.chan].active=false;
375 chan[c.chan].keyOff=true;
376 chan[c.chan].keyOn=false;
377 chan[c.chan].std.release();
378 break;
379 case DIV_CMD_ENV_RELEASE:
380 chan[c.chan].std.release();
381 break;
382 case DIV_CMD_INSTRUMENT:
383 if (chan[c.chan].ins!=c.value || c.value2==1) {
384 chan[c.chan].insChanged=true;
385 chan[c.chan].ins=c.value;
386 }
387 break;
388 case DIV_CMD_VOLUME:
389 if (chan[c.chan].vol!=c.value) {
390 chan[c.chan].vol=c.value;
391 if (!chan[c.chan].std.vol.has) {
392 chan[c.chan].outVol=c.value;
393 chan[c.chan].vol=chan[c.chan].outVol;
394 rWrite(c.chan*7+3,(chan[c.chan].duty>>8) | (chan[c.chan].vol << 4));
395 }
396 }
397 break;
398 case DIV_CMD_GET_VOLUME:
399 if (chan[c.chan].std.vol.has) {
400 return chan[c.chan].vol;
401 }
402 return chan[c.chan].outVol;
403 break;
404 case DIV_CMD_PITCH:
405 chan[c.chan].pitch=c.value;
406 chan[c.chan].freqChanged=true;
407 break;
408 case DIV_CMD_NOTE_PORTA: {
409 int destFreq=NOTE_FREQUENCY(c.value2);
410 bool return2=false;
411 if (destFreq>chan[c.chan].baseFreq) {
412 chan[c.chan].baseFreq+=c.value;
413 if (chan[c.chan].baseFreq>=destFreq) {
414 chan[c.chan].baseFreq=destFreq;
415 return2=true;
416 }
417 } else {
418 chan[c.chan].baseFreq-=c.value;
419 if (chan[c.chan].baseFreq<=destFreq) {
420 chan[c.chan].baseFreq=destFreq;
421 return2=true;
422 }
423 }
424 chan[c.chan].freqChanged=true;
425 if (return2) {
426 chan[c.chan].inPorta=false;
427 return 2;
428 }
429 break;
430 }
431 case DIV_CMD_C64_FINE_DUTY:
432 chan[c.chan].duty=c.value;
433 rWrite(c.chan*7+2,chan[c.chan].duty&0xff);
434 rWrite(c.chan*7+3,(chan[c.chan].duty>>8) | (chan[c.chan].outVol << 4));
435 break;
436 case DIV_CMD_WAVE:
437 chan[c.chan].wave=c.value;
438 rWrite(c.chan*7+4,(chan[c.chan].wave<<4)|0|(chan[c.chan].ring<<2)|(chan[c.chan].sync<<1)|(int)(chan[c.chan].active && chan[c.chan].gate));
439 chan[c.chan].freqChanged=true;
440 break;
441 case DIV_CMD_LEGATO:
442 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
443 chan[c.chan].freqChanged=true;
444 chan[c.chan].note=c.value;
445 break;
446 case DIV_CMD_PRE_PORTA:
447 if (chan[c.chan].active && c.value2) {
448 if (parent->song.resetMacroOnPorta || parent->song.preNoteNoEffect) {
449 chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_SID2));
450 chan[c.chan].keyOn=true;
451 }
452 }
453 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);
454 chan[c.chan].inPorta=c.value;
455 break;
456 case DIV_CMD_GET_VOLMAX:
457 return 15;
458 break;
459 case DIV_CMD_C64_FINE_CUTOFF:
460 chan[c.chan].filtCut=c.value;
461 updateFilter(c.chan);
462 break;
463 case DIV_CMD_C64_RESONANCE:
464 if (c.value>255) c.value=255;
465 chan[c.chan].filtRes=c.value;
466 updateFilter(c.chan);
467 break;
468 case DIV_CMD_C64_FILTER_MODE:
469 chan[c.chan].filtControl=c.value&7;
470 updateFilter(c.chan);
471 break;
472 case DIV_CMD_C64_RESET_MASK:
473 chan[c.chan].resetMask=c.value;
474 break;
475 case DIV_CMD_C64_FILTER_RESET:
476 if (c.value&15) {
477 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SID2);
478 if (ins->c64.initFilter) {
479 chan[c.chan].filtCut=ins->c64.cut;
480 updateFilter(c.chan);
481 }
482 }
483 chan[c.chan].resetFilter=c.value>>4;
484 break;
485 case DIV_CMD_C64_DUTY_RESET:
486 if (c.value&15) {
487 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SID2);
488 chan[c.chan].duty=ins->c64.duty;
489 rWrite(c.chan*7+2,chan[c.chan].duty&0xff);
490 rWrite(c.chan*7+3,(chan[c.chan].duty>>8) | (chan[c.chan].outVol << 4));
491 }
492 chan[c.chan].resetDuty=c.value>>4;
493 break;
494 case DIV_CMD_C64_EXTENDED:
495 switch (c.value>>4) {
496 case 0:
497 chan[c.chan].attack=c.value&15;
498 rWrite(c.chan*7+5,(chan[c.chan].attack<<4)|(chan[c.chan].decay));
499 break;
500 case 1:
501 chan[c.chan].decay=c.value&15;
502 rWrite(c.chan*7+5,(chan[c.chan].attack<<4)|(chan[c.chan].decay));
503 break;
504 case 2:
505 chan[c.chan].sustain=c.value&15;
506 rWrite(c.chan*7+6,(chan[c.chan].sustain<<4)|(chan[c.chan].release));
507 break;
508 case 3:
509 chan[c.chan].release=c.value&15;
510 rWrite(c.chan*7+6,(chan[c.chan].sustain<<4)|(chan[c.chan].release));
511 break;
512 case 4:
513 chan[c.chan].ring=c.value;
514 rWrite(c.chan*7+4,(chan[c.chan].wave<<4)|0|(chan[c.chan].ring<<2)|(chan[c.chan].sync<<1)|(int)(chan[c.chan].active && chan[c.chan].gate));
515 break;
516 case 5:
517 chan[c.chan].sync=c.value;
518 rWrite(c.chan*7+4,(chan[c.chan].wave<<4)|0|(chan[c.chan].ring<<2)|(chan[c.chan].sync<<1)|(int)(chan[c.chan].active && chan[c.chan].gate));
519 break;
520 case 6:
521 chan[c.chan].filtControl = c.value;
522 updateFilter(c.chan);
523 break;
524 case 7:
525 chan[c.chan].mix_mode=(c.value & 3);
526 rWrite(0x1f, (chan[0].mix_mode) | (chan[1].mix_mode << 2) | (chan[2].mix_mode << 4) | ((chan[2].freq >> 16) << 6));
527 break;
528 case 8:
529 chan[c.chan].noise_mode=(c.value & 3);
530 chan[c.chan].freqChanged = true;
531 rWrite(0x1e, (chan[0].noise_mode) | (chan[1].noise_mode << 2) | (chan[2].noise_mode << 4) | ((chan[0].freq >> 16) << 6) | ((chan[1].freq >> 16) << 7));
532 break;
533 case 9: //phase reset
534 chan[c.chan].test=true;
535 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));
536 rWrite(c.chan*7+4,(chan[c.chan].wave<<4)|0|(chan[c.chan].ring<<2)|(chan[c.chan].sync<<1)|(int)(chan[c.chan].active && chan[c.chan].gate));
537 chan[c.chan].test = false;
538 break;
539 case 0xa: //envelope on/off
540 chan[c.chan].gate=(c.value & 1);
541 rWrite(c.chan*7+4,(chan[c.chan].wave<<4)|0|(chan[c.chan].ring<<2)|(chan[c.chan].sync<<1)|(int)(chan[c.chan].active && chan[c.chan].gate));
542 break;
543 case 0xb: //filter on/off
544 chan[c.chan].filter=(c.value & 1);
545 updateFilter(c.chan);
546 break;
547 }
548 break;
549 case DIV_CMD_C64_PW_SLIDE:
550 chan[c.chan].pw_slide=c.value*c.value2;
551 break;
552 case DIV_CMD_C64_CUTOFF_SLIDE:
553 chan[c.chan].cutoff_slide=c.value*c.value2;
554 break;
555 case DIV_CMD_MACRO_OFF:
556 chan[c.chan].std.mask(c.value,true);
557 break;
558 case DIV_CMD_MACRO_ON:
559 chan[c.chan].std.mask(c.value,false);
560 break;
561 case DIV_CMD_MACRO_RESTART:
562 chan[c.chan].std.restart(c.value);
563 break;
564 default:
565 break;
566 }
567 return 1;
568 }
569
570 void DivPlatformSID2::muteChannel(int ch, bool mute) {
571 isMuted[ch]=mute;
572 sid2->set_is_muted(ch,mute);
573 }
574
575 void DivPlatformSID2::forceIns() {
576 for (int i=0; i<3; i++) {
577 chan[i].insChanged=true;
578 if (chan[i].active) {
579 chan[i].keyOn=true;
580 chan[i].freqChanged=true;
581 }
582 updateFilter(i);
583 }
584 }
585
586 void DivPlatformSID2::notifyInsChange(int ins) {
587 for (int i=0; i<3; i++) {
588 if (chan[i].ins==ins) {
589 chan[i].insChanged=true;
590 }
591 }
592 }
593
594 void DivPlatformSID2::notifyInsDeletion(void* ins) {
595 for (int i=0; i<3; i++) {
596 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
597 }
598 }
599
600 void* DivPlatformSID2::getChanState(int ch) {
601 return &chan[ch];
602 }
603
604 DivMacroInt* DivPlatformSID2::getChanMacroInt(int ch) {
605 return &chan[ch].std;
606 }
607
608 void DivPlatformSID2::getPaired(int ch, std::vector<DivChannelPair>& ret) {
609 if (chan[ch].ring) {
610 if (ch==0) {
611 ret.push_back(DivChannelPair(_("ring"),2));
612 } else {
613 ret.push_back(DivChannelPair(_("ring"),(ch-1)%3));
614 }
615 }
616 if (chan[ch].sync) {
617 if (ch==0) {
618 ret.push_back(DivChannelPair(_("sync"),2));
619 } else {
620 ret.push_back(DivChannelPair(_("sync"),(ch-1)%3));
621 }
622 }
623 }
624
625 DivChannelModeHints DivPlatformSID2::getModeHints(int ch) {
626 DivChannelModeHints ret;
627 ret.count=1;
628 ret.hint[0]=ICON_FA_BELL_SLASH_O;
629 ret.type[0]=0;
630 if (ch==2 && (chan[ch].filtControl & 8)) {
631 ret.type[0]=7;
632 } else if (!chan[ch].gate) {
633 ret.type[0]=4;
634 }
635
636 return ret;
637 }
638
639 DivDispatchOscBuffer* DivPlatformSID2::getOscBuffer(int ch) {
640 return oscBuf[ch];
641 }
642
643 unsigned char* DivPlatformSID2::getRegisterPool() {
644 return regPool;
645 }
646
647 int DivPlatformSID2::getRegisterPoolSize() {
648 return 32;
649 }
650
651 bool DivPlatformSID2::getDCOffRequired() {
652 return true;
653 }
654
655 bool DivPlatformSID2::getWantPreNote() {
656 return true;
657 }
658
659 bool DivPlatformSID2::isVolGlobal() {
660 return true;
661 }
662
663 float DivPlatformSID2::getPostAmp() {
664 return 1.0f;
665 }
666
667 void DivPlatformSID2::reset() {
668 while (!writes.empty()) writes.pop();
669 for (int i=0; i<3; i++) {
670 chan[i]=DivPlatformSID2::Channel();
671 chan[i].std.setEngine(parent);
672 fakeLow[i]=0;
673 fakeBand[i]=0;
674 chan[i].vol = 15;
675
676 chan[i].filtControl = 7;
677 chan[i].filtRes = 0;
678 chan[i].filtCut = 4095;
679
680 chan[i].noise_mode = 0;
681
682 rWrite(0x3 + 7 * i,0xf0);
683
684 chan[i].cutoff_slide = 0;
685 chan[i].pw_slide = 0;
686 }
687
688 sid2->reset();
689 memset(regPool,0,32);
690
691 chanOrder[0]=0;
692 chanOrder[1]=1;
693 chanOrder[2]=2;
694 }
695
696 void DivPlatformSID2::poke(unsigned int addr, unsigned short val) {
697 rWrite(addr,val);
698 }
699
700 void DivPlatformSID2::poke(std::vector<DivRegWrite>& wlist) {
701 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
702 }
703
704 void DivPlatformSID2::setFlags(const DivConfig& flags) {
705 switch (flags.getInt("clockSel",0)) {
706 case 0x0: // NTSC C64
707 chipClock=COLOR_NTSC*2.0/7.0;
708 break;
709 case 0x1: // PAL C64
710 chipClock=COLOR_PAL*2.0/9.0;
711 break;
712 case 0x2: // SSI 2001
713 default:
714 chipClock=14318180.0/16.0;
715 break;
716 }
717 CHECK_CUSTOM_CLOCK;
718 rate=chipClock;
719 for (int i=0; i<3; i++) {
720 oscBuf[i]->setRate(rate);
721 }
722 keyPriority=flags.getBool("keyPriority",true);
723
724 // init fake filter table
725 // taken from dSID
726 double cutRatio=-2.0*3.14*(12500.0/256.0)/(double)oscBuf[0]->rate;
727
728 for (int i=0; i<4095; i++)
729 {
730 double c=(double)i/16.0+0.2;
731 c=1-exp(c*cutRatio);
732 fakeCutTable[i]=c;
733 }
734 }
735
736 int DivPlatformSID2::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
737 parent=p;
738 dumpWrites=false;
739 skipRegisterWrites=false;
740 needInitTables=true;
741 writeOscBuf=0;
742
743 for (int i=0; i<3; i++)
744 {
745 isMuted[i]=false;
746 oscBuf[i]=new DivDispatchOscBuffer;
747 }
748
749 sid2=new SID2;
750
751 sid2->set_chip_model(MOS8580_2);
752
753 setFlags(flags);
754
755 reset();
756
757 return 3;
758 }
759
760 void DivPlatformSID2::quit() {
761 for (int i=0; i<3; i++)
762 {
763 delete oscBuf[i];
764 }
765 if (sid2!=NULL) delete sid2;
766 }
767
768 DivPlatformSID2::~DivPlatformSID2() {
769 }