/** * Furnace Tracker - multi-system chiptune tracker * Copyright (C) 2021-2024 tildearrow and contributors * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "sid3.h" #include "../engine.h" #include "IconsFontAwesome4.h" #include #include "../../ta-log.h" #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} } #define CHIP_FREQBASE 524288 const char* regCheatSheetSID3[]={ "FreqL0", "00", "FreqH0", "01", "PWL0", "02", "PWH0Vol", "03", "Control0", "04", "AtkDcy0", "05", "StnRis0", "06", "FreqL1", "07", "FreqH1", "08", "PWL1", "09", "PWH1Vol", "0A", "Control1", "0B", "AtkDcy1", "0C", "StnRis1", "0D", "FreqL2", "0E", "FreqH2", "0F", "PWL2", "10", "PWH2Vol", "11", "Control2", "12", "AtkDcy2", "13", "StnRis2", "14", "FCL0Ctrl", "15", "FCH0", "16", "FilterRes0", "17", "FCL1Ctrl", "18", "FCH1", "19", "FilterRes1", "1A", "FCL2Ctrl", "1B", "FCH2", "1C", "FilterRes2", "1D", "NoiModeFrMSB01", "1E", "WaveMixModeFrMSB2", "1F", NULL }; const char** DivPlatformSID3::getRegisterSheet() { return regCheatSheetSID3; } void DivPlatformSID3::acquire(short** buf, size_t len) { for (size_t i=0; ioutput_l; buf[1][i]=sid3->output_r; if (++writeOscBuf>=16) { writeOscBuf=0; for(int j = 0; j < SID3_NUM_CHANNELS; j++) { oscBuf[j]->data[oscBuf[j]->needle++] = sid3->channel_output[j] / 4; } } } } void DivPlatformSID3::updateFilter(int channel) { //rWrite(0x15 + 3 * channel,(chan[channel].filtCut&15) | ((chan[channel].filtControl & 7) << 4) | (chan[channel].filter << 7)); //rWrite(0x16 + 3 * channel,(chan[channel].filtCut >> 4)); //rWrite(0x17 + 3 * channel,chan[channel].filtRes); } void DivPlatformSID3::updateFreq(int channel) { rWrite(10 + channel*SID3_REGISTERS_PER_CHANNEL,(chan[channel].freq >> 16) & 0xff); rWrite(11 + channel*SID3_REGISTERS_PER_CHANNEL,(chan[channel].freq >> 8) & 0xff); rWrite(12 + channel*SID3_REGISTERS_PER_CHANNEL,chan[channel].freq & 0xff); } void DivPlatformSID3::updateDuty(int channel) { rWrite(7 + channel*SID3_REGISTERS_PER_CHANNEL,(chan[channel].duty >> 8) & 0xff); rWrite(8 + channel*SID3_REGISTERS_PER_CHANNEL,chan[channel].duty & 0xff); } void DivPlatformSID3::tick(bool sysTick) { for (int i=0; icalcFreq(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 * 64); //if (chan[i].freq<0) chan[i].freq=0; //if (chan[i].freq>0x1ffff) chan[i].freq=0x1ffff; if (chan[i].keyOn) { rWrite(i*SID3_REGISTERS_PER_CHANNEL,SID3_CHAN_ENABLE_GATE); rWrite(6 + i*SID3_REGISTERS_PER_CHANNEL,SID3_WAVE_PULSE); chan[i].duty = 0x1000; updateDuty(i); } if (chan[i].keyOff) { rWrite(i*SID3_REGISTERS_PER_CHANNEL,0); } if (chan[i].freq<0) chan[i].freq=0; if (chan[i].freq>0xffffff) chan[i].freq=0xffffff; updateFreq(i); //rWrite(i*7,chan[i].freq&0xff); //rWrite(i*7+1,chan[i].freq>>8); //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)); //rWrite(0x1f, (chan[0].mix_mode) | (chan[1].mix_mode << 2) | (chan[2].mix_mode << 4) | ((chan[2].freq >> 16) << 6)); if (chan[i].keyOn) chan[i].keyOn=false; if (chan[i].keyOff) chan[i].keyOff=false; chan[i].freqChanged=false; } } } int DivPlatformSID3::dispatch(DivCommand c) { if (c.chan>SID3_NUM_CHANNELS - 1) return 0; switch (c.cmd) { case DIV_CMD_NOTE_ON: { DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SID3); if (c.value!=DIV_NOTE_NULL) { chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value); chan[c.chan].freqChanged=true; chan[c.chan].note=c.value; } chan[c.chan].active=true; chan[c.chan].keyOn=true; chan[c.chan].test=false; if (chan[c.chan].insChanged || chan[c.chan].resetDuty || ins->std.waveMacro.len>0) { chan[c.chan].duty=ins->c64.duty; //rWrite(c.chan*7+2,chan[c.chan].duty&0xff); //rWrite(c.chan*7+3,(chan[c.chan].duty>>8) | (chan[c.chan].outVol << 4)); } if (chan[c.chan].insChanged) { /*chan[c.chan].wave = (ins->c64.noiseOn << 3) | (ins->c64.pulseOn << 2) | (ins->c64.sawOn << 1) | (int)(ins->c64.triOn); chan[c.chan].attack=ins->c64.a; chan[c.chan].decay=(ins->c64.s==15)?0:ins->c64.d; chan[c.chan].sustain=ins->c64.s; chan[c.chan].release=ins->c64.r; chan[c.chan].ring=ins->c64.ringMod; chan[c.chan].sync=ins->c64.oscSync; chan[c.chan].noise_mode = ins->sid3.noiseMode; chan[c.chan].mix_mode = ins->sid3.mixMode;*/ } if (chan[c.chan].insChanged || chan[c.chan].resetFilter) { chan[c.chan].filter=ins->c64.toFilter; if (ins->c64.initFilter) { chan[c.chan].filtCut=ins->c64.cut; chan[c.chan].filtRes=ins->c64.res; chan[c.chan].filtControl=(int)(ins->c64.lp)|(ins->c64.bp<<1)|(ins->c64.hp<<2); } updateFilter(c.chan); } if (chan[c.chan].insChanged) { chan[c.chan].insChanged=false; } chan[c.chan].macroInit(ins); break; } case DIV_CMD_NOTE_OFF: chan[c.chan].active=false; chan[c.chan].keyOff=true; chan[c.chan].keyOn=false; //chan[c.chan].macroInit(NULL); break; case DIV_CMD_NOTE_OFF_ENV: chan[c.chan].active=false; chan[c.chan].keyOff=true; chan[c.chan].keyOn=false; chan[c.chan].std.release(); break; case DIV_CMD_ENV_RELEASE: chan[c.chan].std.release(); break; case DIV_CMD_INSTRUMENT: if (chan[c.chan].ins!=c.value || c.value2==1) { chan[c.chan].insChanged=true; chan[c.chan].ins=c.value; } break; case DIV_CMD_VOLUME: if (chan[c.chan].vol!=c.value) { chan[c.chan].vol=c.value; if (!chan[c.chan].std.vol.has) { chan[c.chan].outVol=c.value; chan[c.chan].vol=chan[c.chan].outVol; rWrite(c.chan*7+3,(chan[c.chan].duty>>8) | (chan[c.chan].vol << 4)); } } break; case DIV_CMD_GET_VOLUME: if (chan[c.chan].std.vol.has) { return chan[c.chan].vol; } return chan[c.chan].outVol; break; case DIV_CMD_PITCH: chan[c.chan].pitch=c.value; chan[c.chan].freqChanged=true; break; case DIV_CMD_NOTE_PORTA: { int destFreq=NOTE_FREQUENCY(c.value2); bool return2=false; if (destFreq>chan[c.chan].baseFreq) { chan[c.chan].baseFreq+=c.value; if (chan[c.chan].baseFreq>=destFreq) { chan[c.chan].baseFreq=destFreq; return2=true; } } else { chan[c.chan].baseFreq-=c.value; if (chan[c.chan].baseFreq<=destFreq) { chan[c.chan].baseFreq=destFreq; return2=true; } } chan[c.chan].freqChanged=true; if (return2) { chan[c.chan].inPorta=false; return 2; } break; } case DIV_CMD_LEGATO: chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))); chan[c.chan].freqChanged=true; chan[c.chan].note=c.value; break; case DIV_CMD_PRE_PORTA: if (chan[c.chan].active && c.value2) { if (parent->song.resetMacroOnPorta || parent->song.preNoteNoEffect) { chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_SID3)); chan[c.chan].keyOn=true; } } 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); chan[c.chan].inPorta=c.value; break; case DIV_CMD_GET_VOLMAX: return SID3_MAX_VOL; break; case DIV_CMD_MACRO_OFF: chan[c.chan].std.mask(c.value,true); break; case DIV_CMD_MACRO_ON: chan[c.chan].std.mask(c.value,false); break; case DIV_CMD_MACRO_RESTART: chan[c.chan].std.restart(c.value); break; default: break; } return 1; } void DivPlatformSID3::muteChannel(int ch, bool mute) { isMuted[ch]=mute; sid3_set_is_muted(sid3,ch,mute); } void DivPlatformSID3::forceIns() { for (int i=0; i& wlist) { for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); } void DivPlatformSID3::setFlags(const DivConfig& flags) { switch (flags.getInt("clockSel",0)) { case 0x0: // NTSC C64 chipClock=COLOR_NTSC*2.0/7.0; break; case 0x1: // PAL C64 chipClock=COLOR_PAL*2.0/9.0; break; case 0x2: // SSI 2001 default: chipClock=14318180.0/16.0; break; } CHECK_CUSTOM_CLOCK; rate=chipClock; for (int i=0; irate=rate/16; } } int DivPlatformSID3::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { parent=p; dumpWrites=false; skipRegisterWrites=false; writeOscBuf=0; for (int i=0; i