Debian Codegraph @

src/engine/platform/sid3.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 "sid3.h"
21 #include "../engine.h"
22 #include "IconsFontAwesome4.h"
23 #include <math.h>
24 #include "../../ta-log.h"
25
26 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
27
28 #define CHIP_FREQBASE 524288*64
29 #define CHIP_DIVIDER 1
30
31 #define CURRENT_FREQ_IN_HZ() ((double)chipClock / pow(2.0, (double)SID3_ACC_BITS) * (double)chan[i].freq)
32 #define c_5_FREQ() (parent->song.tuning / pow(2, (12.0 * 9.0 + 9.0) / 12.0))
33 #define FREQ_FOR_NOTE(note) (c_5_FREQ() * pow(2, (double)note / 12.0))
34
35 const char* regCheatSheetSID3[]={
36 "FreqL0", "00",
37 "FreqH0", "01",
38 "PWL0", "02",
39 "PWH0Vol", "03",
40 "Control0", "04",
41 "AtkDcy0", "05",
42 "StnRis0", "06",
43 "FreqL1", "07",
44 "FreqH1", "08",
45 "PWL1", "09",
46 "PWH1Vol", "0A",
47 "Control1", "0B",
48 "AtkDcy1", "0C",
49 "StnRis1", "0D",
50 "FreqL2", "0E",
51 "FreqH2", "0F",
52 "PWL2", "10",
53 "PWH2Vol", "11",
54 "Control2", "12",
55 "AtkDcy2", "13",
56 "StnRis2", "14",
57
58 "FCL0Ctrl", "15",
59 "FCH0", "16",
60 "FilterRes0", "17",
61
62 "FCL1Ctrl", "18",
63 "FCH1", "19",
64 "FilterRes1", "1A",
65
66 "FCL2Ctrl", "1B",
67 "FCH2", "1C",
68 "FilterRes2", "1D",
69
70 "NoiModeFrMSB01", "1E",
71 "WaveMixModeFrMSB2", "1F",
72 NULL
73 };
74
75 typedef struct
76 {
77 uint32_t LFSRmask;
78 float freqScaling;
79 } noiseFreqData;
80
81 const noiseFreqData noiseInterestingWavesData[] =
82 {
83 {524288, 523.0f / 1675.0f}, //wave very close to SID2 noise mode 1 wave
84 {541065280U, 523.0f / 104.0f / 2.0f}, //smth like vocals
85 {2068, 1.0f}, //wave very close to SID2 noise mode 3 wave
86 {66, 523.0f / 540.0f / 2.0f}, //wave very close to SID2 noise mode 2 wave
87
88 {0, 0.0f}, //end marker
89 };
90
91 const char** DivPlatformSID3::getRegisterSheet() {
92 return regCheatSheetSID3;
93 }
94
95 void DivPlatformSID3::acquire(short** buf, size_t len)
96 {
97 for (int i=0; i<SID3_NUM_CHANNELS; i++) {
98 oscBuf[i]->begin(len);
99 }
100
101 for (size_t i=0; i<len; i++)
102 {
103 if (chan[SID3_NUM_CHANNELS - 1].pcm && chan[SID3_NUM_CHANNELS - 1].dacSample!=-1)
104 {
105 chan[SID3_NUM_CHANNELS - 1].dacPeriod+=chan[SID3_NUM_CHANNELS - 1].dacRate;
106 if (chan[SID3_NUM_CHANNELS - 1].dacPeriod>rate)
107 {
108 DivSample* s=parent->getSample(chan[SID3_NUM_CHANNELS - 1].dacSample);
109 if (s->samples<=0 || chan[SID3_NUM_CHANNELS - 1].dacPos>=s->samples)
110 {
111 chan[SID3_NUM_CHANNELS - 1].dacSample=-1;
112 continue;
113 }
114
115 int dacData=s->data16[chan[SID3_NUM_CHANNELS - 1].dacPos] + 32767;
116 chan[SID3_NUM_CHANNELS - 1].dacOut=CLAMP(dacData,0,65535);
117 updateSample = true;
118
119 if (sampleTick > 2)
120 {
121 sampleTick = 0;
122 }
123
124 chan[SID3_NUM_CHANNELS - 1].dacPos++;
125 if (s->isLoopable() && chan[SID3_NUM_CHANNELS - 1].dacPos>=(unsigned int)s->loopEnd)
126 {
127 chan[SID3_NUM_CHANNELS - 1].dacPos=s->loopStart;
128 }
129 else if (chan[SID3_NUM_CHANNELS - 1].dacPos>=s->samples)
130 {
131 chan[SID3_NUM_CHANNELS - 1].dacSample=-1;
132 }
133 chan[SID3_NUM_CHANNELS - 1].dacPeriod-=rate;
134 }
135 while (chan[SID3_NUM_CHANNELS - 1].dacPeriod > rate)
136 {
137 chan[SID3_NUM_CHANNELS - 1].dacPeriod -= rate;
138 }
139 }
140
141 sampleTick++;
142
143 if(chan[SID3_NUM_CHANNELS - 1].pcm)
144 {
145 if(sampleTick == 2 && updateSample)
146 {
147 if (!isMuted[SID3_NUM_CHANNELS - 1])
148 {
149 sid3_write(sid3, SID3_REGISTER_STREAMED_SAMPLE_HIGH + (SID3_NUM_CHANNELS - 1) * SID3_REGISTERS_PER_CHANNEL, chan[SID3_NUM_CHANNELS - 1].dacOut >> 8);
150 regPool[SID3_REGISTER_STREAMED_SAMPLE_HIGH + (SID3_NUM_CHANNELS - 1) * SID3_REGISTERS_PER_CHANNEL]=chan[SID3_NUM_CHANNELS - 1].dacOut >> 8;
151 }
152 else
153 {
154 sid3_write(sid3, SID3_REGISTER_STREAMED_SAMPLE_HIGH + (SID3_NUM_CHANNELS - 1) * SID3_REGISTERS_PER_CHANNEL, 32768 >> 8);
155 regPool[SID3_REGISTER_STREAMED_SAMPLE_HIGH + (SID3_NUM_CHANNELS - 1) * SID3_REGISTERS_PER_CHANNEL]=32768 >> 8;
156 }
157 }
158 else if(sampleTick == 3 && updateSample)
159 {
160 if (!isMuted[SID3_NUM_CHANNELS - 1])
161 {
162 sid3_write(sid3, SID3_REGISTER_STREAMED_SAMPLE_LOW + (SID3_NUM_CHANNELS - 1) * SID3_REGISTERS_PER_CHANNEL, chan[SID3_NUM_CHANNELS - 1].dacOut & 0xff);
163 regPool[SID3_REGISTER_STREAMED_SAMPLE_LOW + (SID3_NUM_CHANNELS - 1) * SID3_REGISTERS_PER_CHANNEL]=chan[SID3_NUM_CHANNELS - 1].dacOut & 0xff;
164 }
165 else
166 {
167 sid3_write(sid3, SID3_REGISTER_STREAMED_SAMPLE_LOW + (SID3_NUM_CHANNELS - 1) * SID3_REGISTERS_PER_CHANNEL, 32768 & 0xff);
168 regPool[SID3_REGISTER_STREAMED_SAMPLE_LOW + (SID3_NUM_CHANNELS - 1) * SID3_REGISTERS_PER_CHANNEL]=32768 & 0xff;
169 }
170
171 sampleTick = 0;
172 updateSample = false;
173 }
174 else
175 {
176 if (!writes.empty())
177 {
178 QueuedWrite w=writes.front();
179 sid3_write(sid3, w.addr, w.val);
180 regPool[w.addr % SID3_NUM_REGISTERS]=w.val;
181 writes.pop();
182 }
183 }
184 }
185 else
186 {
187 if (!writes.empty())
188 {
189 QueuedWrite w=writes.front();
190 sid3_write(sid3, w.addr, w.val);
191 regPool[w.addr % SID3_NUM_REGISTERS]=w.val;
192 writes.pop();
193 }
194 }
195
196 sid3_clock(sid3);
197
198 buf[0][i]=sid3->output_l;
199 buf[1][i]=sid3->output_r;
200
201 if (++writeOscBuf>=8)
202 {
203 writeOscBuf=0;
204
205 for(int j = 0; j < SID3_NUM_CHANNELS - 1; j++)
206 {
207 oscBuf[j]->putSample(i,sid3->muted[j] ? 0 : (sid3->channel_output[j] / 4));
208 }
209
210 oscBuf[SID3_NUM_CHANNELS - 1]->putSample(i,sid3->muted[SID3_NUM_CHANNELS - 1] ? 0 : (sid3->wave_channel_output / 4));
211 }
212 }
213
214 for (int i=0; i<SID3_NUM_CHANNELS; i++) {
215 oscBuf[i]->end(len);
216 }
217 }
218
219 void DivPlatformSID3::updateFlags(int channel, bool gate)
220 {
221 rWrite(SID3_REGISTER_FLAGS + channel * SID3_REGISTERS_PER_CHANNEL, (gate ? 1 : 0) |
222 (chan[channel].ring ? SID3_CHAN_ENABLE_RING_MOD : 0) |
223 (chan[channel].sync ? SID3_CHAN_ENABLE_HARD_SYNC : 0) |
224 (chan[channel].phase ? SID3_CHAN_ENABLE_PHASE_MOD : 0) |
225 (chan[channel].phaseReset ? SID3_CHAN_PHASE_RESET : 0) |
226 (chan[channel].envReset ? SID3_CHAN_ENV_RESET : 0) |
227 (chan[channel].phaseResetNoise ? SID3_CHAN_NOISE_PHASE_RESET : 0) |
228 (chan[channel].oneBitNoise ? SID3_CHAN_1_BIT_NOISE : 0));
229 }
230
231 void DivPlatformSID3::updateFilter(int channel, int filter)
232 {
233 rWrite(SID3_REGISTER_FILT_MODE + filter * SID3_REGISTERS_PER_FILTER + channel*SID3_REGISTERS_PER_CHANNEL,
234 chan[channel].filt[filter].mode | (chan[channel].filt[filter].enabled ? SID3_FILTER_ENABLE : 0));
235
236 rWrite(SID3_REGISTER_FILT_CUTOFF_HIGH + filter * SID3_REGISTERS_PER_FILTER + channel*SID3_REGISTERS_PER_CHANNEL,
237 chan[channel].filt[filter].cutoff >> 8);
238 rWrite(SID3_REGISTER_FILT_CUTOFF_LOW + filter * SID3_REGISTERS_PER_FILTER + channel*SID3_REGISTERS_PER_CHANNEL,
239 chan[channel].filt[filter].cutoff & 0xff);
240
241 rWrite(SID3_REGISTER_FILT_RESONANCE + filter * SID3_REGISTERS_PER_FILTER + channel*SID3_REGISTERS_PER_CHANNEL,
242 chan[channel].filt[filter].resonance);
243
244 rWrite(SID3_REGISTER_FILT_DISTORTION + filter * SID3_REGISTERS_PER_FILTER + channel*SID3_REGISTERS_PER_CHANNEL,
245 chan[channel].filt[filter].distortion_level);
246
247 rWrite(SID3_REGISTER_FILT_CONNECTION + filter * SID3_REGISTERS_PER_FILTER + channel*SID3_REGISTERS_PER_CHANNEL,
248 chan[channel].filt[filter].filter_matrix);
249
250 rWrite(SID3_REGISTER_FILT_OUTPUT_VOLUME + filter * SID3_REGISTERS_PER_FILTER + channel*SID3_REGISTERS_PER_CHANNEL,
251 chan[channel].filt[filter].output_volume);
252 }
253
254 void DivPlatformSID3::updateFreq(int channel)
255 {
256 rWrite(SID3_REGISTER_FREQ_HIGH + channel*SID3_REGISTERS_PER_CHANNEL,(chan[channel].freq >> 16) & 0xff);
257 rWrite(SID3_REGISTER_FREQ_MID + channel*SID3_REGISTERS_PER_CHANNEL,(chan[channel].freq >> 8) & 0xff);
258 rWrite(SID3_REGISTER_FREQ_LOW + channel*SID3_REGISTERS_PER_CHANNEL,chan[channel].freq & 0xff);
259 }
260
261 void DivPlatformSID3::updateNoiseFreq(int channel)
262 {
263 rWrite(SID3_REGISTER_NOISE_FREQ_HIGH + channel*SID3_REGISTERS_PER_CHANNEL,(chan[channel].noiseFreq >> 16) & 0xff);
264 rWrite(SID3_REGISTER_NOISE_FREQ_MID + channel*SID3_REGISTERS_PER_CHANNEL,(chan[channel].noiseFreq >> 8) & 0xff);
265 rWrite(SID3_REGISTER_NOISE_FREQ_LOW + channel*SID3_REGISTERS_PER_CHANNEL,chan[channel].noiseFreq & 0xff);
266 }
267
268 void DivPlatformSID3::updateNoiseLFSRMask(int channel)
269 {
270 rWrite(SID3_REGISTER_NOISE_LFSR_HIGHEST + channel*SID3_REGISTERS_PER_CHANNEL,(chan[channel].noiseLFSRMask >> 24) & 0xff);
271 rWrite(SID3_REGISTER_NOISE_LFSR_HIGH + channel*SID3_REGISTERS_PER_CHANNEL,(chan[channel].noiseLFSRMask >> 16) & 0xff);
272 rWrite(SID3_REGISTER_NOISE_LFSR_MID + channel*SID3_REGISTERS_PER_CHANNEL,(chan[channel].noiseLFSRMask >> 8) & 0xff);
273 rWrite(SID3_REGISTER_NOISE_LFSR_LOW + channel*SID3_REGISTERS_PER_CHANNEL,chan[channel].noiseLFSRMask & 0xff);
274 }
275
276 void DivPlatformSID3::updateDuty(int channel)
277 {
278 rWrite(SID3_REGISTER_PW_HIGH + channel*SID3_REGISTERS_PER_CHANNEL,(chan[channel].duty >> 8) & 0xff);
279 rWrite(SID3_REGISTER_PW_LOW + channel*SID3_REGISTERS_PER_CHANNEL,chan[channel].duty & 0xff);
280 }
281
282 void DivPlatformSID3::updateEnvelope(int channel)
283 {
284 rWrite(SID3_REGISTER_ADSR_A + channel * SID3_REGISTERS_PER_CHANNEL, chan[channel].attack); //attack
285 rWrite(SID3_REGISTER_ADSR_D + channel * SID3_REGISTERS_PER_CHANNEL, chan[channel].decay); //decay
286 rWrite(SID3_REGISTER_ADSR_S + channel * SID3_REGISTERS_PER_CHANNEL, chan[channel].sustain); //sustain
287 rWrite(SID3_REGISTER_ADSR_SR + channel * SID3_REGISTERS_PER_CHANNEL, chan[channel].sr); //sr
288 rWrite(SID3_REGISTER_ADSR_R + channel * SID3_REGISTERS_PER_CHANNEL, chan[channel].release); //release
289 }
290
291 void DivPlatformSID3::updatePanning(int channel)
292 {
293 rWrite(SID3_REGISTER_PAN_LEFT + channel*SID3_REGISTERS_PER_CHANNEL,chan[channel].panLeft);
294 rWrite(SID3_REGISTER_PAN_RIGHT + channel*SID3_REGISTERS_PER_CHANNEL,chan[channel].panRight);
295 }
296
297 void DivPlatformSID3::updateWave()
298 {
299 int channel = SID3_NUM_CHANNELS - 1;
300
301 for(int i = 0; i < 256; i++)
302 {
303 uint8_t val = ws.output[i & 255];
304 rWrite(SID3_REGISTER_PW_HIGH + channel*SID3_REGISTERS_PER_CHANNEL,i);
305 rWrite(SID3_REGISTER_PW_LOW + channel*SID3_REGISTERS_PER_CHANNEL,val);
306 }
307 }
308
309 void DivPlatformSID3::tick(bool sysTick)
310 {
311 bool doUpdateWave = false;
312
313 for (int i=0; i<SID3_NUM_CHANNELS; i++)
314 {
315 chan[i].std.next();
316
317 DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_SID3);
318
319 bool panChanged = false;
320 bool flagsChanged = false;
321 bool envChanged = false;
322
323 if(sysTick)
324 {
325 if(chan[i].pw_slide != 0)
326 {
327 chan[i].duty -= chan[i].pw_slide;
328 chan[i].duty = CLAMP(chan[i].duty, 0, 0xffff);
329 updateDuty(i);
330 }
331 for(int j = 0; j < SID3_NUM_FILTERS; j++) //filters' slides
332 {
333 if(chan[i].filt[j].cutoff_slide != 0)
334 {
335 chan[i].filt[j].cutoff += chan[i].filt[j].cutoff_slide;
336 chan[i].filt[j].cutoff = CLAMP(chan[i].filt[j].cutoff, 0, 0xffff);
337 updateFilter(i, j);
338 }
339 }
340
341 if(chan[i].phase_reset_counter >= 0)
342 {
343 if(chan[i].phase_reset_counter == 0)
344 {
345 chan[i].phaseReset = true;
346 flagsChanged = true;
347 }
348 chan[i].phase_reset_counter--;
349 }
350 if(chan[i].noise_phase_reset_counter >= 0)
351 {
352 if(chan[i].noise_phase_reset_counter == 0)
353 {
354 chan[i].phaseResetNoise = true;
355 flagsChanged = true;
356 }
357 chan[i].noise_phase_reset_counter--;
358 }
359 if(chan[i].envelope_reset_counter >= 0)
360 {
361 if(chan[i].envelope_reset_counter == 0)
362 {
363 chan[i].envReset = true;
364 flagsChanged = true;
365 }
366 chan[i].envelope_reset_counter--;
367 }
368 }
369
370 if (chan[i].std.vol.had)
371 {
372 chan[i].outVol=VOL_SCALE_LINEAR(chan[i].vol&255,MIN(255,chan[i].std.vol.val),255);
373 rWrite(13 + i * SID3_REGISTERS_PER_CHANNEL, chan[i].outVol);
374 }
375 if (NEW_ARP_STRAT) {
376 chan[i].handleArp();
377 } else if (chan[i].std.arp.had) {
378 if (!chan[i].inPorta) {
379 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
380 }
381 chan[i].freqChanged=true;
382 }
383 if (chan[i].std.pitch.had) {
384 if (chan[i].std.pitch.mode) {
385 chan[i].pitch2+=chan[i].std.pitch.val;
386 CLAMP_VAR(chan[i].pitch2,-65535,65535);
387 } else {
388 chan[i].pitch2=chan[i].std.pitch.val;
389 }
390 chan[i].freqChanged=true;
391 }
392 if (chan[i].std.duty.had) {
393 DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_SID3);
394 if (ins->sid3.dutyIsAbs) {
395 chan[i].duty=chan[i].std.duty.val;
396 } else {
397 chan[i].duty-=chan[i].std.duty.val;
398 }
399 chan[i].duty&=65535;
400 updateDuty(i);
401 }
402 if (chan[i].std.wave.had) {
403 DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_SID3);
404
405 if(i == SID3_NUM_CHANNELS - 1 && ins->sid3.doWavetable)
406 {
407 chan[i].wavetable = chan[i].std.wave.val & 0xff;
408 ws.changeWave1(chan[i].wavetable, true);
409 doUpdateWave = true;
410 }
411 else
412 {
413 chan[i].wave = chan[i].std.wave.val & 0xff;
414 rWrite(SID3_REGISTER_WAVEFORM + i * SID3_REGISTERS_PER_CHANNEL, chan[i].wave);
415 }
416 }
417 if (chan[i].std.alg.had) { //special wave
418 chan[i].special_wave = chan[i].std.alg.val & 0xff;
419 rWrite(SID3_REGISTER_SPECIAL_WAVE + i * SID3_REGISTERS_PER_CHANNEL, chan[i].special_wave);
420 }
421 if (chan[i].std.op[3].am.had) { //noise arpeggio
422 //chan[i].handleArpNoise(0);
423 chan[i].noiseFreqChanged = true;
424 }
425 chan[i].handleArpNoise(0);
426 chan[i].handlePitchNoise();
427 if (chan[i].std.op[0].ar.had) { //noise pitch
428 //chan[i].handlePitchNoise();
429 chan[i].noiseFreqChanged = true;
430 }
431 if (chan[i].std.panL.had) {
432 panChanged = true;
433 chan[i].panLeft = chan[i].std.panL.val & 0xff;
434 }
435 if (chan[i].std.panR.had) {
436 panChanged = true;
437 chan[i].panRight = chan[i].std.panR.val & 0xff;
438 }
439 if (chan[i].std.op[2].ar.had) { //channel signal inversion
440 chan[i].phaseInv = chan[i].std.op[2].ar.val & 3;
441 rWrite(SID3_REGISTER_PHASE_INVERSION + i * SID3_REGISTERS_PER_CHANNEL, chan[i].phaseInv);
442 }
443 if (chan[i].std.op[0].am.had) { //key on/off
444 chan[i].gate = chan[i].std.op[0].am.val & 1;
445 flagsChanged = true;
446 }
447 if (chan[i].std.ex1.had) { //ring mod, hard sync, phase mod
448 chan[i].phase = chan[i].std.ex1.val & 1;
449 chan[i].sync = chan[i].std.ex1.val & 2;
450 chan[i].ring = chan[i].std.ex1.val & 4;
451 flagsChanged = true;
452 }
453 if (chan[i].std.ams.had) { //hard sync source
454 chan[i].syncSrc = chan[i].std.ams.val & 0xff;
455 rWrite(SID3_REGISTER_SYNC_SRC + i * SID3_REGISTERS_PER_CHANNEL, chan[i].syncSrc);
456 }
457 if (chan[i].std.fms.had) { //ring mod source
458 chan[i].ringSrc = chan[i].std.fms.val & 0xff;
459 rWrite(SID3_REGISTER_RING_MOD_SRC + i * SID3_REGISTERS_PER_CHANNEL, chan[i].ringSrc);
460 }
461 if (chan[i].std.fb.had) { //phase mod source
462 chan[i].phaseSrc = chan[i].std.fb.val & 0xff;
463 rWrite(SID3_REGISTER_PHASE_MOD_SRC + i * SID3_REGISTERS_PER_CHANNEL, chan[i].phaseSrc);
464 }
465 if (chan[i].std.op[3].ar.had) { //feedback
466 chan[i].feedback = chan[i].std.op[3].ar.val & 0xff;
467 rWrite(SID3_REGISTER_FEEDBACK + i * SID3_REGISTERS_PER_CHANNEL, chan[i].feedback);
468 }
469 if (chan[i].std.phaseReset.had) {
470 chan[i].phaseReset = chan[i].std.phaseReset.val & 1;
471
472 if(chan[i].phaseReset)
473 {
474 flagsChanged = true;
475 }
476
477 if (chan[i].pcm)
478 {
479 if (chan[i].active && chan[i].dacSample>=0 && chan[i].dacSample<parent->song.sampleLen)
480 {
481 chan[i].dacPos=0;
482 chan[i].dacPeriod=0;
483 }
484 }
485 }
486 if (chan[i].std.op[1].am.had) { //noise phase reset
487 chan[i].phaseResetNoise = chan[i].std.op[1].am.val & 1;
488
489 if(chan[i].phaseResetNoise)
490 {
491 flagsChanged = true;
492 }
493 }
494 if (chan[i].std.op[2].am.had) { //envelope reset
495 chan[i].envReset = chan[i].std.op[2].am.val & 1;
496
497 if(chan[i].envReset)
498 {
499 flagsChanged = true;
500 }
501 }
502 if (chan[i].std.ex2.had) { //attack
503 chan[i].attack = chan[i].std.ex2.val & 0xff;
504 envChanged = true;
505 }
506 if (chan[i].std.ex3.had) { //decay
507 chan[i].decay = chan[i].std.ex3.val & 0xff;
508 envChanged = true;
509 }
510 if (chan[i].std.ex4.had) { //sustain
511 chan[i].sustain = chan[i].std.ex4.val & 0xff;
512 envChanged = true;
513 }
514 if (chan[i].std.ex5.had) { //sustain rate
515 chan[i].sr = chan[i].std.ex5.val & 0xff;
516 envChanged = true;
517 }
518 if (chan[i].std.ex6.had) { //release
519 chan[i].release = chan[i].std.ex6.val & 0xff;
520 envChanged = true;
521 }
522 if (chan[i].std.ex7.had) { //noise LFSR feedback bits
523 chan[i].noiseLFSRMask = chan[i].std.ex7.val & 0x3fffffff;
524 updateNoiseLFSRMask(i);
525 chan[i].noiseFreqChanged = true;
526 }
527 if (chan[i].std.op[1].ar.had) { //1-bit noise / PCM mode for wavetable chan
528 if(i == SID3_NUM_CHANNELS - 1) //wave chan
529 {
530 rWrite(SID3_REGISTER_WAVEFORM + i * SID3_REGISTERS_PER_CHANNEL, chan[i].std.op[1].ar.val & 1);
531 }
532 else
533 {
534 if((uint32_t)chan[i].oneBitNoise != (chan[i].std.op[1].ar.val & 1))
535 {
536 chan[i].oneBitNoise = chan[i].std.op[1].ar.val & 1;
537 flagsChanged = true;
538 }
539 }
540 }
541 if (chan[i].std.ex8.had) { //wave mix mode
542 chan[i].mix_mode = chan[i].std.ex8.val & 0xff;
543 rWrite(SID3_REGISTER_MIXMODE + i * SID3_REGISTERS_PER_CHANNEL, chan[i].mix_mode); //mixmode
544 }
545
546 for(int j = 0; j < SID3_NUM_FILTERS; j++) //filter macros
547 {
548 DivMacroInt::IntOp* op = &chan[i].std.op[j];
549 DivPlatformSID3::Channel::Filter* ch_filt = &chan[i].filt[j];
550 DivInstrumentSID3::Filter* ins_filt = &ins->sid3.filt[j];
551
552 bool doUpdateFilter = false;
553
554 if (op->d2r.had) { //cutoff
555 if (ins_filt->absoluteCutoff) {
556 ch_filt->cutoff=op->d2r.val;
557 } else {
558 ch_filt->cutoff+=op->d2r.val;
559 }
560 ch_filt->cutoff&=65535;
561 doUpdateFilter = true;
562 }
563 if (op->dam.had) { //resonance
564 ch_filt->resonance=op->dam.val & 0xff;
565 doUpdateFilter = true;
566 }
567 if (op->dr.had) { //filter toggle
568 ch_filt->enabled=op->dr.val & 1;
569 doUpdateFilter = true;
570 }
571 if (op->dt2.had) { //distortion level
572 ch_filt->distortion_level=op->dt2.val & 0xff;
573 doUpdateFilter = true;
574 }
575 if (op->dt.had) { //output volume
576 ch_filt->output_volume=op->dt.val & 0xff;
577 doUpdateFilter = true;
578 }
579 if (op->dvb.had) { //connect to channel input
580 ch_filt->mode &= ~SID3_FILTER_CHANNEL_INPUT;
581 ch_filt->mode |= (op->dvb.val & 1) ? SID3_FILTER_CHANNEL_INPUT : 0;
582 doUpdateFilter = true;
583 }
584 if (op->egt.had) { //connect to channel output
585 ch_filt->mode &= ~SID3_FILTER_OUTPUT;
586 ch_filt->mode |= (op->egt.val & 1) ? SID3_FILTER_OUTPUT : 0;
587 doUpdateFilter = true;
588 }
589 if (op->ksl.had) { //connection matrix row
590 ch_filt->filter_matrix=op->ksl.val & 0xf;
591 doUpdateFilter = true;
592 }
593 if (op->ksr.had) { //filter mode
594 ch_filt->mode &= ~(SID3_FILTER_LP | SID3_FILTER_HP | SID3_FILTER_BP);
595 if(op->ksr.val & 1) ch_filt->mode |= SID3_FILTER_LP;
596 if(op->ksr.val & 2) ch_filt->mode |= SID3_FILTER_BP;
597 if(op->ksr.val & 4) ch_filt->mode |= SID3_FILTER_HP;
598 doUpdateFilter = true;
599 }
600
601 if(doUpdateFilter)
602 {
603 updateFilter(i, j);
604 }
605 }
606
607 if(panChanged)
608 {
609 updatePanning(i);
610 }
611 if(flagsChanged)
612 {
613 updateFlags(i, chan[i].gate);
614
615 chan[i].phaseReset = false;
616 chan[i].phaseResetNoise = false;
617 chan[i].envReset = false;
618 }
619 if(envChanged)
620 {
621 updateEnvelope(i);
622 }
623
624 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff)
625 {
626 chan[i].freq=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,chipClock,CHIP_FREQBASE);
627
628 if (chan[i].keyOn)
629 {
630 DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_SID3);
631 if(i == SID3_NUM_CHANNELS - 1)
632 {
633 if(ins->sid3.doWavetable && !ins->amiga.useSample)
634 {
635 rWrite(SID3_REGISTER_WAVEFORM + i * SID3_REGISTERS_PER_CHANNEL, 0); //wave channel mode
636 }
637 else
638 {
639 rWrite(SID3_REGISTER_WAVEFORM + i * SID3_REGISTERS_PER_CHANNEL, 1); //wave channel mode
640 }
641 }
642 else
643 {
644 rWrite(SID3_REGISTER_WAVEFORM + i * SID3_REGISTERS_PER_CHANNEL, chan[i].wave);
645 }
646
647 rWrite(SID3_REGISTER_SPECIAL_WAVE + i * SID3_REGISTERS_PER_CHANNEL, chan[i].special_wave); //special wave
648
649 rWrite(SID3_REGISTER_ADSR_VOL + i * SID3_REGISTERS_PER_CHANNEL, chan[i].outVol); //set volume
650 rWrite(SID3_REGISTER_MIXMODE + i * SID3_REGISTERS_PER_CHANNEL, chan[i].mix_mode); //mixmode
651
652 rWrite(SID3_REGISTER_RING_MOD_SRC + i * SID3_REGISTERS_PER_CHANNEL, chan[i].ringSrc); //ring mod source
653 rWrite(SID3_REGISTER_SYNC_SRC + i * SID3_REGISTERS_PER_CHANNEL, chan[i].syncSrc); //hard sync source
654 rWrite(SID3_REGISTER_PHASE_MOD_SRC + i * SID3_REGISTERS_PER_CHANNEL, chan[i].phaseSrc); //phase mod source
655
656 rWrite(SID3_REGISTER_PHASE_INVERSION + i * SID3_REGISTERS_PER_CHANNEL, chan[i].phaseInv); //signal inversion
657 rWrite(SID3_REGISTER_FEEDBACK + i * SID3_REGISTERS_PER_CHANNEL, chan[i].feedback); //feedback
658
659 updateEnvelope(i);
660
661 updateFlags(i, false); //gate off TODO: make it properly?
662 updateFlags(i, true); //gate on
663 chan[i].gate = true;
664 }
665 if (chan[i].keyOff)
666 {
667 updateFlags(i, false); //gate off
668 chan[i].gate = false;
669 }
670
671 if (chan[i].freq<0) chan[i].freq=0;
672 if (chan[i].freq>0xffffff) chan[i].freq=0xffffff;
673
674 updateFreq(i);
675
676 for(int j = 0; j < SID3_NUM_FILTERS; j++)
677 {
678 bool doUpdateFilter = false;
679
680 if(chan[i].filt[j].bindCutoffToNote && (!ins->sid3.filt[j].bindCutoffOnNote || chan[i].keyOn))
681 {
682 double scaling = CURRENT_FREQ_IN_HZ() / FREQ_FOR_NOTE(chan[i].filt[j].bindCutoffToNoteCenter) - 1.0;
683 if (chan[i].filt[j].bindCutoffToNoteDir)
684 {
685 scaling *= -1.0;
686 }
687
688 int cutoff = ins->sid3.filt[j].cutoff + (int)(scaling * (double)chan[i].filt[j].bindCutoffToNoteStrength * 80.0);
689
690 if(cutoff > 0xffff) cutoff = 0xffff;
691 if(cutoff < 0) cutoff = 0;
692
693 chan[i].filt[j].cutoff = cutoff;
694 doUpdateFilter = true;
695 }
696
697 if(chan[i].filt[j].bindResonanceToNote && (!ins->sid3.filt[j].bindResonanceOnNote || chan[i].keyOn))
698 {
699 double scaling = CURRENT_FREQ_IN_HZ() / FREQ_FOR_NOTE(chan[i].filt[j].bindResonanceToNoteCenter) - 1.0;
700 if (chan[i].filt[j].bindResonanceToNoteDir)
701 {
702 scaling *= -1.0;
703 }
704
705 int res = ins->sid3.filt[j].resonance + (int)(scaling * (double)chan[i].filt[j].bindResonanceToNoteStrength * 80.0 / 256.0);
706
707 if(res > 0xff) res = 0xff;
708 if(res < 0) res = 0;
709
710 chan[i].filt[j].resonance = res;
711 doUpdateFilter = true;
712 }
713
714 if(doUpdateFilter)
715 {
716 updateFilter(i, j);
717 }
718 }
719
720 if (chan[i].pcm && i == SID3_NUM_CHANNELS - 1) {
721 double off=1.0;
722 if (chan[i].dacSample>=0 && chan[i].dacSample<parent->song.sampleLen) {
723 DivSample* s=parent->getSample(chan[i].dacSample);
724 if (s->centerRate<1) {
725 off=1.0;
726 } else {
727 off=(double)s->centerRate/parent->getCenterRate();
728 }
729 }
730 chan[i].dacRate=chan[i].freq*(off / 32.0)*(double)chipClock/1000000.0;
731 }
732
733 chan[i].noiseFreqChanged = true;
734
735 if(chan[i].independentNoiseFreq)
736 {
737 chan[i].noise_pitch2 = chan[i].pitch2;
738 }
739
740 if (chan[i].keyOn) chan[i].keyOn=false;
741 if (chan[i].keyOff) chan[i].keyOff=false;
742 chan[i].freqChanged=false;
743 }
744
745 if(chan[i].noiseFreqChanged)
746 {
747 if(chan[i].independentNoiseFreq)
748 {
749 chan[i].noiseFreq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].noise_fixedArp?chan[i].noise_baseNoteOverride:chan[i].noise_arpOff,chan[i].noise_fixedArp,false,2,chan[i].noise_pitch2,chipClock,CHIP_FREQBASE);
750 }
751 else
752 {
753 chan[i].noiseFreq = chan[i].freq;
754 }
755
756 bool found = false;
757 int index = 0;
758 while(noiseInterestingWavesData[index].LFSRmask != 0 && !found)
759 {
760 if(noiseInterestingWavesData[index].LFSRmask == chan[i].noiseLFSRMask)
761 {
762 chan[i].noiseFreq *= noiseInterestingWavesData[index].freqScaling;
763 found = true;
764 }
765 index++;
766 }
767
768 if (chan[i].noiseFreq<0) chan[i].noiseFreq=0;
769 if (chan[i].noiseFreq>0xffffff) chan[i].noiseFreq=0xffffff;
770
771 updateNoiseFreq(i);
772
773 chan[i].noiseFreqChanged = false;
774 }
775 }
776
777 if (chan[SID3_NUM_CHANNELS - 1].active && !chan[SID3_NUM_CHANNELS - 1].pcm)
778 {
779 if (ws.tick())
780 {
781 doUpdateWave = true;
782 }
783 }
784
785 if(doUpdateWave)
786 {
787 updateWave();
788 doUpdateWave = false;
789 }
790 }
791
792 int DivPlatformSID3::dispatch(DivCommand c) {
793 if (c.chan>SID3_NUM_CHANNELS - 1) return 0;
794
795 bool updEnv = false;
796 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SID3);
797 int filter = 0;
798
799 switch (c.cmd) {
800 case DIV_CMD_NOTE_ON: {
801 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SID3);
802 if (c.value!=DIV_NOTE_NULL) {
803 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
804 chan[c.chan].freqChanged=true;
805 chan[c.chan].note=c.value;
806 }
807 chan[c.chan].active=true;
808 chan[c.chan].keyOn=true;
809
810 if (ins->amiga.useSample)
811 {
812 chan[c.chan].pcm=true;
813 }
814 else
815 {
816 chan[c.chan].pcm=false;
817 }
818
819 if (chan[c.chan].pcm && c.chan == SID3_NUM_CHANNELS - 1)
820 {
821 if (ins->amiga.useSample)
822 {
823 if (skipRegisterWrites) break;
824 if (c.value!=DIV_NOTE_NULL) {
825 chan[c.chan].dacSample=ins->amiga.getSample(c.value);
826 chan[c.chan].sampleNote=c.value;
827 c.value=ins->amiga.getFreq(c.value);
828 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
829 } else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) {
830 chan[c.chan].dacSample=ins->amiga.getSample(chan[c.chan].sampleNote);
831 c.value=ins->amiga.getFreq(chan[c.chan].sampleNote);
832 }
833 if (chan[c.chan].dacSample<0 || chan[c.chan].dacSample>=parent->song.sampleLen)
834 {
835 chan[c.chan].dacSample=-1;
836 break;
837 }
838 chan[c.chan].dacPos=0;
839 chan[c.chan].dacPeriod=0;
840 if (c.value!=DIV_NOTE_NULL)
841 {
842 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
843 chan[c.chan].freqChanged=true;
844 chan[c.chan].note=c.value;
845 }
846 chan[c.chan].active=true;
847 chan[c.chan].macroInit(ins);
848 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
849 chan[c.chan].outVol=chan[c.chan].vol;
850 }
851 //chan[c.chan].keyOn=true;
852 }
853 }
854
855 if (chan[c.chan].insChanged)
856 {
857 chan[c.chan].wave = (ins->sid3.triOn ? SID3_WAVE_TRIANGLE : 0) | (ins->sid3.sawOn ? SID3_WAVE_SAW : 0) |
858 (ins->sid3.pulseOn ? SID3_WAVE_PULSE : 0) | (ins->sid3.noiseOn ? SID3_WAVE_NOISE : 0) | (ins->sid3.specialWaveOn ? SID3_WAVE_SPECIAL : 0); //waveform
859 chan[c.chan].special_wave = ins->sid3.special_wave; //special wave
860
861 chan[c.chan].attack=ins->sid3.a;
862 chan[c.chan].decay=ins->sid3.d;
863 chan[c.chan].sustain=ins->sid3.s;
864 chan[c.chan].sr=ins->sid3.sr;
865 chan[c.chan].release=ins->sid3.r;
866
867 chan[c.chan].sync = ins->sid3.oscSync;
868 chan[c.chan].ring = ins->sid3.ringMod;
869 chan[c.chan].phase = ins->sid3.phase_mod;
870 chan[c.chan].oneBitNoise = ins->sid3.oneBitNoise;
871
872 chan[c.chan].mix_mode = ins->sid3.mixMode;
873
874 chan[c.chan].ringSrc = ins->sid3.ring_mod_source;
875 chan[c.chan].syncSrc = ins->sid3.sync_source;
876 chan[c.chan].phaseSrc = ins->sid3.phase_mod_source;
877
878 chan[c.chan].independentNoiseFreq = ins->sid3.separateNoisePitch;
879
880 chan[c.chan].phaseInv = ins->sid3.phaseInv;
881 chan[c.chan].feedback = ins->sid3.feedback;
882
883 for(int j = 0; j < SID3_NUM_FILTERS; j++)
884 {
885 if(ins->sid3.filt[j].init)
886 {
887 chan[c.chan].filt[j].cutoff = ins->sid3.filt[j].cutoff;
888 chan[c.chan].filt[j].resonance = ins->sid3.filt[j].resonance;
889 chan[c.chan].filt[j].distortion_level = ins->sid3.filt[j].distortion_level;
890 chan[c.chan].filt[j].enabled = ins->sid3.filt[j].enabled;
891 chan[c.chan].filt[j].filter_matrix = ins->sid3.filt[j].filter_matrix;
892 chan[c.chan].filt[j].mode = ins->sid3.filt[j].mode;
893 chan[c.chan].filt[j].output_volume = ins->sid3.filt[j].output_volume;
894
895 chan[c.chan].filt[j].bindCutoffToNote = ins->sid3.filt[j].bindCutoffToNote;
896 chan[c.chan].filt[j].bindCutoffToNoteStrength = ins->sid3.filt[j].bindCutoffToNoteStrength;
897 chan[c.chan].filt[j].bindCutoffToNoteCenter = ins->sid3.filt[j].bindCutoffToNoteCenter;
898 chan[c.chan].filt[j].bindCutoffToNoteDir = ins->sid3.filt[j].bindCutoffToNoteDir;
899
900 chan[c.chan].filt[j].bindResonanceToNote = ins->sid3.filt[j].bindResonanceToNote;
901 chan[c.chan].filt[j].bindResonanceToNoteStrength = ins->sid3.filt[j].bindResonanceToNoteStrength;
902 chan[c.chan].filt[j].bindResonanceToNoteCenter = ins->sid3.filt[j].bindResonanceToNoteCenter;
903 chan[c.chan].filt[j].bindResonanceToNoteDir = ins->sid3.filt[j].bindResonanceToNoteDir;
904
905 updateFilter(c.chan, j);
906 }
907 }
908
909 if(c.chan == SID3_NUM_CHANNELS - 1)
910 {
911 if(!chan[c.chan].pcm)
912 {
913 ws.changeWave1(chan[c.chan].wavetable, false);
914 ws.init(ins,256,255,chan[c.chan].insChanged);
915 }
916 }
917 }
918 if(ins->sid3.resetDuty||chan[c.chan].insChanged)
919 {
920 chan[c.chan].duty=ins->sid3.duty;
921 updateDuty(c.chan);
922 }
923 if (chan[c.chan].insChanged) {
924 chan[c.chan].insChanged=false;
925 }
926 chan[c.chan].macroInit(ins);
927 break;
928 }
929 case DIV_CMD_NOTE_OFF:
930 chan[c.chan].active=false;
931 chan[c.chan].keyOff=true;
932 chan[c.chan].keyOn=false;
933 //chan[c.chan].macroInit(NULL);
934 break;
935 case DIV_CMD_NOTE_OFF_ENV:
936 chan[c.chan].active=false;
937 chan[c.chan].keyOff=true;
938 chan[c.chan].keyOn=false;
939 chan[c.chan].std.release();
940 break;
941 case DIV_CMD_ENV_RELEASE:
942 chan[c.chan].std.release();
943 break;
944 case DIV_CMD_INSTRUMENT:
945 if (chan[c.chan].ins!=c.value || c.value2==1) {
946 chan[c.chan].insChanged=true;
947 chan[c.chan].ins=c.value;
948 }
949 break;
950 case DIV_CMD_VOLUME:
951 if (chan[c.chan].vol!=c.value) {
952 chan[c.chan].vol=c.value;
953 if (!chan[c.chan].std.vol.has) {
954 chan[c.chan].outVol=c.value;
955 chan[c.chan].vol=chan[c.chan].outVol;
956 rWrite(SID3_REGISTER_ADSR_VOL + c.chan * SID3_REGISTERS_PER_CHANNEL, chan[c.chan].vol);
957 }
958 }
959 break;
960 case DIV_CMD_GET_VOLUME:
961 if (chan[c.chan].std.vol.has) {
962 return chan[c.chan].vol;
963 }
964 return chan[c.chan].outVol;
965 break;
966 case DIV_CMD_PITCH:
967 chan[c.chan].pitch=c.value;
968 chan[c.chan].freqChanged=true;
969 break;
970 case DIV_CMD_NOTE_PORTA: {
971 int destFreq=NOTE_FREQUENCY(c.value2);
972 bool return2=false;
973 if (destFreq>chan[c.chan].baseFreq) {
974 chan[c.chan].baseFreq+=c.value;
975 if (chan[c.chan].baseFreq>=destFreq) {
976 chan[c.chan].baseFreq=destFreq;
977 return2=true;
978 }
979 } else {
980 chan[c.chan].baseFreq-=c.value;
981 if (chan[c.chan].baseFreq<=destFreq) {
982 chan[c.chan].baseFreq=destFreq;
983 return2=true;
984 }
985 }
986 chan[c.chan].freqChanged=true;
987 if (return2) {
988 chan[c.chan].inPorta=false;
989 return 2;
990 }
991 break;
992 }
993 case DIV_CMD_LEGATO:
994 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
995 chan[c.chan].freqChanged=true;
996 chan[c.chan].note=c.value;
997 break;
998 case DIV_CMD_PRE_PORTA:
999 if (chan[c.chan].active && c.value2) {
1000 if (parent->song.resetMacroOnPorta || parent->song.preNoteNoEffect) {
1001 chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_SID3));
1002 chan[c.chan].keyOn=true;
1003 }
1004 }
1005 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);
1006 chan[c.chan].inPorta=c.value;
1007 break;
1008 case DIV_CMD_PANNING: {
1009 bool updPan = false;
1010 if (!chan[c.chan].std.panL.has)
1011 {
1012 chan[c.chan].panLeft = c.value;
1013 updPan = true;
1014 }
1015 if (!chan[c.chan].std.panR.has)
1016 {
1017 chan[c.chan].panRight = c.value2;
1018 updPan = true;
1019 }
1020 if(updPan)
1021 {
1022 updatePanning(c.chan);
1023 }
1024 break;
1025 }
1026 case DIV_CMD_GET_VOLMAX:
1027 return SID3_MAX_VOL;
1028 break;
1029 case DIV_CMD_WAVE:
1030 if(c.chan == SID3_NUM_CHANNELS - 1 && ins->sid3.doWavetable)
1031 {
1032 chan[c.chan].wavetable = c.value & 0xff;
1033 ws.changeWave1(chan[c.chan].wave);
1034 }
1035 else
1036 {
1037 chan[c.chan].wave = c.value & 0xff;
1038 rWrite(SID3_REGISTER_WAVEFORM + c.chan * SID3_REGISTERS_PER_CHANNEL, chan[c.chan].wave);
1039 }
1040 break;
1041 case DIV_CMD_SID3_SPECIAL_WAVE:
1042 chan[c.chan].special_wave = c.value % SID3_NUM_SPECIAL_WAVES;
1043 rWrite(SID3_REGISTER_SPECIAL_WAVE + c.chan * SID3_REGISTERS_PER_CHANNEL, chan[c.chan].special_wave);
1044 break;
1045 case DIV_CMD_C64_EXTENDED:
1046 chan[c.chan].ring = c.value & 1;
1047 chan[c.chan].sync = c.value & 2;
1048 chan[c.chan].phase = c.value & 4;
1049 updateFlags(c.chan, chan[c.chan].gate);
1050 break;
1051 case DIV_CMD_C64_DUTY_RESET:
1052 if (c.value&15) {
1053 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SID3);
1054 chan[c.chan].duty=ins->sid3.duty;
1055 updateDuty(c.chan);
1056 }
1057 chan[c.chan].resetDuty=c.value>>4;
1058 break;
1059 case DIV_CMD_SID3_RING_MOD_SRC:
1060 chan[c.chan].ringSrc = c.value % (SID3_NUM_CHANNELS + 1);
1061 rWrite(SID3_REGISTER_RING_MOD_SRC + c.chan * SID3_REGISTERS_PER_CHANNEL, chan[c.chan].ringSrc);
1062 break;
1063 case DIV_CMD_SID3_HARD_SYNC_SRC:
1064 chan[c.chan].syncSrc = c.value % SID3_NUM_CHANNELS;
1065 rWrite(SID3_REGISTER_SYNC_SRC + c.chan * SID3_REGISTERS_PER_CHANNEL, chan[c.chan].syncSrc);
1066 break;
1067 case DIV_CMD_SID3_PHASE_MOD_SRC:
1068 chan[c.chan].phaseSrc = c.value % SID3_NUM_CHANNELS;
1069 rWrite(SID3_REGISTER_PHASE_MOD_SRC + c.chan * SID3_REGISTERS_PER_CHANNEL, chan[c.chan].phaseSrc);
1070 break;
1071 case DIV_CMD_FM_AR:
1072 chan[c.chan].attack = c.value & 0xff;
1073 updEnv = true;
1074 break;
1075 case DIV_CMD_FM_DR:
1076 chan[c.chan].decay = c.value & 0xff;
1077 updEnv = true;
1078 break;
1079 case DIV_CMD_FM_SL:
1080 chan[c.chan].sustain = c.value & 0xff;
1081 updEnv = true;
1082 break;
1083 case DIV_CMD_FM_D2R:
1084 chan[c.chan].sr = c.value & 0xff;
1085 updEnv = true;
1086 break;
1087 case DIV_CMD_FM_RR:
1088 chan[c.chan].release = c.value & 0xff;
1089 updEnv = true;
1090 break;
1091 case DIV_CMD_SID3_WAVE_MIX:
1092 chan[c.chan].mix_mode = c.value % 5;
1093 rWrite(SID3_REGISTER_MIXMODE + c.chan * SID3_REGISTERS_PER_CHANNEL, chan[c.chan].mix_mode);
1094 break;
1095 case DIV_CMD_SID3_LFSR_FEEDBACK_BITS:
1096 chan[c.chan].noiseLFSRMask &= ~(0xffU << (8 * c.value2));
1097 chan[c.chan].noiseLFSRMask |= ((c.value & (c.value2 == 3 ? 0x3f : 0xff)) << (8 * c.value2));
1098 updateNoiseLFSRMask(c.chan);
1099 chan[c.chan].noiseFreqChanged = true;
1100 break;
1101 case DIV_CMD_SID3_1_BIT_NOISE:
1102 if(c.chan == SID3_NUM_CHANNELS - 1) //wave chan
1103 {
1104 rWrite(SID3_REGISTER_WAVEFORM + c.chan * SID3_REGISTERS_PER_CHANNEL, c.value & 1); //PCM mode
1105 }
1106 else
1107 {
1108 if((uint32_t)chan[c.chan].oneBitNoise != (c.value & 1))
1109 {
1110 chan[c.chan].oneBitNoise = c.value & 1;
1111 updateFlags(c.chan, chan[c.chan].gate);
1112 }
1113 }
1114 break;
1115 case DIV_CMD_C64_FINE_DUTY:
1116 chan[c.chan].duty = (c.value & 0xfff) << 4;
1117 updateDuty(c.chan);
1118 break;
1119 case DIV_CMD_FM_FB:
1120 chan[c.chan].feedback = c.value & 0xff;
1121 rWrite(SID3_REGISTER_FEEDBACK + c.chan * SID3_REGISTERS_PER_CHANNEL, chan[c.chan].feedback);
1122 break;
1123 case DIV_CMD_SID3_CHANNEL_INVERSION:
1124 chan[c.chan].phaseInv = c.value & 3;
1125 rWrite(SID3_REGISTER_PHASE_INVERSION + c.chan * SID3_REGISTERS_PER_CHANNEL, chan[c.chan].phaseInv);
1126 break;
1127 case DIV_CMD_C64_FINE_CUTOFF:
1128 chan[c.chan].filt[c.value2].cutoff = (c.value & 0xfff) << 4;
1129 updateFilter(c.chan, c.value2);
1130 break;
1131 case DIV_CMD_C64_RESONANCE:
1132 chan[c.chan].filt[c.value2].resonance = c.value & 0xff;
1133 updateFilter(c.chan, c.value2);
1134 break;
1135 case DIV_CMD_SID3_FILTER_OUTPUT_VOLUME:
1136 chan[c.chan].filt[c.value2].output_volume = c.value & 0xff;
1137 updateFilter(c.chan, c.value2);
1138 break;
1139 case DIV_CMD_SID3_FILTER_DISTORTION:
1140 chan[c.chan].filt[c.value2].distortion_level = c.value & 0xff;
1141 updateFilter(c.chan, c.value2);
1142 break;
1143 case DIV_CMD_C64_FILTER_MODE:
1144 chan[c.chan].filt[(c.value >> 4) & 3].mode &= ~(SID3_FILTER_LP | SID3_FILTER_HP | SID3_FILTER_BP);
1145 if(c.value & 1) chan[c.chan].filt[(c.value >> 4) & 3].mode |= SID3_FILTER_LP;
1146 if(c.value & 2) chan[c.chan].filt[(c.value >> 4) & 3].mode |= SID3_FILTER_BP;
1147 if(c.value & 4) chan[c.chan].filt[(c.value >> 4) & 3].mode |= SID3_FILTER_HP;
1148 updateFilter(c.chan, (c.value >> 4) & 3);
1149 break;
1150 case DIV_CMD_SID3_FILTER_CONNECTION:
1151 chan[c.chan].filt[(c.value >> 4) & 3].mode &= ~(SID3_FILTER_CHANNEL_INPUT | SID3_FILTER_OUTPUT);
1152 if(c.value & 1) chan[c.chan].filt[(c.value >> 4) & 3].mode |= SID3_FILTER_CHANNEL_INPUT;
1153 if(c.value & 2) chan[c.chan].filt[(c.value >> 4) & 3].mode |= SID3_FILTER_OUTPUT;
1154 updateFilter(c.chan, (c.value >> 4) & 3);
1155 break;
1156 case DIV_CMD_SID3_FILTER_MATRIX:
1157 chan[c.chan].filt[(c.value >> 4) & 3].filter_matrix = c.value & 0xf;
1158 updateFilter(c.chan, (c.value >> 4) & 3);
1159 break;
1160 case DIV_CMD_SID3_FILTER_ENABLE:
1161 chan[c.chan].filt[(c.value >> 4) & 3].enabled = c.value & 1;
1162 updateFilter(c.chan, (c.value >> 4) & 3);
1163 break;
1164 case DIV_CMD_C64_PW_SLIDE:
1165 chan[c.chan].pw_slide = c.value * c.value2 * 16;
1166 break;
1167 case DIV_CMD_C64_CUTOFF_SLIDE:
1168 filter = abs(c.value2) - 1;
1169 chan[c.chan].filt[filter].cutoff_slide = c.value * (c.value2 > 0 ? 1 : -1) * 16;
1170 break;
1171 case DIV_CMD_SID3_PHASE_RESET:
1172 chan[c.chan].phase_reset_counter = c.value;
1173 break;
1174 case DIV_CMD_SID3_NOISE_PHASE_RESET:
1175 chan[c.chan].noise_phase_reset_counter = c.value;
1176 break;
1177 case DIV_CMD_SID3_ENVELOPE_RESET:
1178 chan[c.chan].envelope_reset_counter = c.value;
1179 break;
1180 case DIV_CMD_SID3_CUTOFF_SCALING:
1181 chan[c.chan].filt[(c.value >> 4) & 3].bindCutoffToNote = c.value & 1;
1182 chan[c.chan].filt[(c.value >> 4) & 3].bindCutoffToNoteDir = c.value & 2;
1183 chan[c.chan].freqChanged = true;
1184 break;
1185 case DIV_CMD_SID3_RESONANCE_SCALING:
1186 chan[c.chan].filt[(c.value >> 4) & 3].bindResonanceToNote = c.value & 1;
1187 chan[c.chan].filt[(c.value >> 4) & 3].bindResonanceToNoteDir = c.value & 2;
1188 chan[c.chan].freqChanged = true;
1189 break;
1190 case DIV_CMD_SAMPLE_POS:
1191 chan[c.chan].dacPos=c.value;
1192 break;
1193 case DIV_CMD_MACRO_OFF:
1194 chan[c.chan].std.mask(c.value,true);
1195 break;
1196 case DIV_CMD_MACRO_ON:
1197 chan[c.chan].std.mask(c.value,false);
1198 break;
1199 case DIV_CMD_MACRO_RESTART:
1200 chan[c.chan].std.restart(c.value);
1201 break;
1202 default:
1203 break;
1204 }
1205
1206 if(updEnv)
1207 {
1208 updateEnvelope(c.chan);
1209 }
1210 return 1;
1211 }
1212
1213 void DivPlatformSID3::muteChannel(int ch, bool mute) {
1214 isMuted[ch]=mute;
1215 sid3_set_is_muted(sid3,ch,mute);
1216 }
1217
1218 void DivPlatformSID3::forceIns() {
1219 for (int i=0; i<SID3_NUM_CHANNELS; i++) {
1220 chan[i].insChanged=true;
1221 if (chan[i].active) {
1222 chan[i].keyOn=true;
1223 chan[i].freqChanged=true;
1224 }
1225 //updateFilter(i);
1226 }
1227 }
1228
1229 void DivPlatformSID3::notifyInsChange(int ins) {
1230 for (int i=0; i<SID3_NUM_CHANNELS; i++) {
1231 if (chan[i].ins==ins) {
1232 chan[i].insChanged=true;
1233 }
1234 }
1235 }
1236
1237 void DivPlatformSID3::notifyWaveChange(int wave)
1238 {
1239 if (chan[SID3_NUM_CHANNELS - 1].wavetable==wave)
1240 {
1241 ws.changeWave1(wave, false);
1242 updateWave();
1243 }
1244 }
1245
1246 void DivPlatformSID3::notifyInsDeletion(void* ins) {
1247 for (int i=0; i<SID3_NUM_CHANNELS; i++) {
1248 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
1249 }
1250 }
1251
1252 void* DivPlatformSID3::getChanState(int ch) {
1253 return &chan[ch];
1254 }
1255
1256 DivMacroInt* DivPlatformSID3::getChanMacroInt(int ch) {
1257 return &chan[ch].std;
1258 }
1259
1260 DivChannelModeHints DivPlatformSID3::getModeHints(int ch) {
1261 DivChannelModeHints ret;
1262 ret.count=1;
1263 ret.hint[0]=ICON_FA_BELL_SLASH_O;
1264 ret.type[0]=0;
1265 if (!chan[ch].gate) {
1266 ret.type[0]=4;
1267 }
1268
1269 return ret;
1270 }
1271
1272 DivDispatchOscBuffer* DivPlatformSID3::getOscBuffer(int ch) {
1273 return oscBuf[ch];
1274 }
1275
1276 unsigned short DivPlatformSID3::getPan(int ch) {
1277 return (chan[ch].panLeft<<8)|chan[ch].panRight;
1278 }
1279
1280 unsigned char* DivPlatformSID3::getRegisterPool() {
1281 return regPool;
1282 }
1283
1284 int DivPlatformSID3::getRegisterPoolSize() {
1285 return SID3_NUM_REGISTERS;
1286 }
1287
1288 float DivPlatformSID3::getPostAmp() {
1289 return 1.0f;
1290 }
1291
1292 void DivPlatformSID3::reset() {
1293 while (!writes.empty()) writes.pop();
1294 for (int i=0; i<SID3_NUM_CHANNELS; i++) {
1295 chan[i]=DivPlatformSID3::Channel();
1296 chan[i].std.setEngine(parent);
1297 chan[i].vol = SID3_MAX_VOL;
1298
1299 for(int j = 0; j < SID3_NUM_FILTERS; j++)
1300 {
1301 chan[i].filt[j].enabled = false;
1302 updateFilter(i, j);
1303
1304 chan[i].filt[j].cutoff_slide = 0;
1305 }
1306
1307 chan[i].panLeft = chan[i].panRight = 0xff;
1308
1309 chan[i].freq = chan[i].noiseFreq = 0;
1310 updatePanning(i);
1311
1312 chan[i].noiseLFSRMask = 1 | (1 << 23) | (1 << 25) | (1 << 29); //https://docs.amd.com/v/u/en-US/xapp052 for 30 bits: 30, 6, 4, 1; but inverted since our LFSR is moving in different direction
1313
1314 chan[i].pw_slide = 0;
1315
1316 chan[i].phase_reset_counter = -1;
1317 chan[i].noise_phase_reset_counter = -1;
1318 chan[i].envelope_reset_counter = -1;
1319 }
1320
1321 sampleTick = 0;
1322 updateSample = false;
1323
1324 ws.setEngine(parent);
1325 ws.init(NULL,256,255,false);
1326
1327 sid3_reset(sid3);
1328 memset(regPool,0,SID3_NUM_REGISTERS);
1329 }
1330
1331 int DivPlatformSID3::getOutputCount() {
1332 return 2;
1333 }
1334
1335 bool DivPlatformSID3::getDCOffRequired()
1336 {
1337 return false;
1338 }
1339
1340 void DivPlatformSID3::poke(unsigned int addr, unsigned short val) {
1341 rWrite(addr,val);
1342 }
1343
1344 void DivPlatformSID3::poke(std::vector<DivRegWrite>& wlist) {
1345 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
1346 }
1347
1348 void DivPlatformSID3::setFlags(const DivConfig& flags) {
1349 chipClock=1000000;
1350 CHECK_CUSTOM_CLOCK;
1351
1352 quarterClock=flags.getBool("quarterClock",false);
1353
1354 if(quarterClock && chipClock >= 1000000 && !parent->isExporting())
1355 {
1356 chipClock /= 4;
1357 }
1358
1359 rate=chipClock;
1360 sid3_set_clock_rate(sid3, chipClock);
1361 for (int i=0; i<SID3_NUM_CHANNELS; i++) {
1362 oscBuf[i]->setRate(rate);
1363 }
1364 }
1365
1366 void DivPlatformSID3::getPaired(int ch, std::vector<DivChannelPair>& ret)
1367 {
1368 if(chan[ch].phase)
1369 {
1370 ret.push_back(DivChannelPair(_("phase"), chan[ch].phaseSrc));
1371 }
1372 if(chan[ch].ring)
1373 {
1374 if(chan[ch].ringSrc == SID3_NUM_CHANNELS)
1375 {
1376 ret.push_back(DivChannelPair(_("ring"), ch));
1377 }
1378 else
1379 {
1380 ret.push_back(DivChannelPair(_("ring"), chan[ch].ringSrc));
1381 }
1382 }
1383 if(chan[ch].sync)
1384 {
1385 ret.push_back(DivChannelPair(_("sync"), chan[ch].syncSrc));
1386 }
1387 }
1388
1389 int DivPlatformSID3::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
1390 parent=p;
1391 dumpWrites=false;
1392 skipRegisterWrites=false;
1393 writeOscBuf=0;
1394
1395 for (int i=0; i<SID3_NUM_CHANNELS; i++)
1396 {
1397 isMuted[i]=false;
1398 oscBuf[i]=new DivDispatchOscBuffer;
1399 }
1400
1401 sid3 = sid3_create();
1402
1403 setFlags(flags);
1404
1405 reset();
1406
1407 return SID3_NUM_CHANNELS;
1408 }
1409
1410 void DivPlatformSID3::quit() {
1411 for (int i=0; i<SID3_NUM_CHANNELS; i++)
1412 {
1413 delete oscBuf[i];
1414 }
1415 if (sid3!=NULL)
1416 {
1417 sid3_free(sid3);
1418 sid3 = NULL;
1419 }
1420 }
1421
1422 DivPlatformSID3::~DivPlatformSID3() {
1423 }