/** * Furnace Tracker - multi-system chiptune tracker * Copyright (C) 2021-2022 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 "genesis.h" #include "../engine.h" #include #include #include "genesisshared.h" static unsigned char konOffs[6]={ 0, 1, 2, 4, 5, 6 }; #define CHIP_FREQBASE 9440540 const char* DivPlatformGenesis::getEffectName(unsigned char effect) { switch (effect) { case 0x10: return "10xy: Setup LFO (x: enable; y: speed)"; break; case 0x11: return "11xx: Set feedback (0 to 7)"; break; case 0x12: return "12xx: Set level of operator 1 (0 highest, 7F lowest)"; break; case 0x13: return "13xx: Set level of operator 2 (0 highest, 7F lowest)"; break; case 0x14: return "14xx: Set level of operator 3 (0 highest, 7F lowest)"; break; case 0x15: return "15xx: Set level of operator 4 (0 highest, 7F lowest)"; break; case 0x16: return "16xy: Set operator multiplier (x: operator from 1 to 4; y: multiplier)"; break; case 0x17: return "17xx: Enable channel 6 DAC"; break; case 0x18: return "18xx: Toggle extended channel 3 mode"; break; case 0x19: return "19xx: Set attack of all operators (0 to 1F)"; break; case 0x1a: return "1Axx: Set attack of operator 1 (0 to 1F)"; break; case 0x1b: return "1Bxx: Set attack of operator 2 (0 to 1F)"; break; case 0x1c: return "1Cxx: Set attack of operator 3 (0 to 1F)"; break; case 0x1d: return "1Dxx: Set attack of operator 4 (0 to 1F)"; break; case 0x30: return "30xx: Toggle hard envelope reset on new notes"; break; case 0x50: return "50xy: Set AM (x: operator from 1 to 4 (0 for all ops); y: AM)"; break; case 0x51: return "51xy: Set sustain level (x: operator from 1 to 4 (0 for all ops); y: sustain)"; break; case 0x52: return "52xy: Set release (x: operator from 1 to 4 (0 for all ops); y: release)"; break; case 0x53: return "53xy: Set detune (x: operator from 1 to 4 (0 for all ops); y: detune where 3 is center)"; break; case 0x54: return "54xy: Set envelope scale (x: operator from 1 to 4 (0 for all ops); y: scale from 0 to 3)"; break; case 0x55: return "55xy: Set SSG envelope (x: operator from 1 to 4 (0 for all ops); y: 0-7 on, 8 off)"; break; case 0x56: return "56xx: Set decay of all operators (0 to 1F)"; break; case 0x57: return "57xx: Set decay of operator 1 (0 to 1F)"; break; case 0x58: return "58xx: Set decay of operator 2 (0 to 1F)"; break; case 0x59: return "59xx: Set decay of operator 3 (0 to 1F)"; break; case 0x5a: return "5Axx: Set decay of operator 4 (0 to 1F)"; break; case 0x5b: return "5Bxx: Set decay 2 of all operators (0 to 1F)"; break; case 0x5c: return "5Cxx: Set decay 2 of operator 1 (0 to 1F)"; break; case 0x5d: return "5Dxx: Set decay 2 of operator 2 (0 to 1F)"; break; case 0x5e: return "5Exx: Set decay 2 of operator 3 (0 to 1F)"; break; case 0x5f: return "5Fxx: Set decay 2 of operator 4 (0 to 1F)"; break; case 0xdf: return "DFxx: Set sample playback direction (0: normal; 1: reverse)"; break; } return NULL; } void DivPlatformGenesis::acquire_nuked(short* bufL, short* bufR, size_t start, size_t len) { static short o[2]; static int os[2]; for (size_t h=start; h=rate) { dacDelay-=rate; dacReady=true; } } if (dacMode && dacSample!=-1) { dacPeriod+=dacRate; if (dacPeriod>=rate) { DivSample* s=parent->getSample(dacSample); if (s->samples>0) { if (!isMuted[5]) { if (dacReady && writes.size()<16) { urgentWrite(0x2a,(unsigned char)s->data8[dacDirection?(s->samples-dacPos-1):dacPos]+0x80); dacReady=false; } } if (++dacPos>=s->samples) { if (s->loopStart>=0 && s->loopStart<(int)s->samples && !dacDirection) { dacPos=s->loopStart; } else { dacSample=-1; if (parent->song.brokenDACMode) { rWrite(0x2b,0); } } } while (dacPeriod>=rate) dacPeriod-=rate; } else { dacSample=-1; } } } os[0]=0; os[1]=0; for (int i=0; i<6; i++) { if (!writes.empty() && --delay<0) { delay=0; QueuedWrite& w=writes.front(); if (w.addrOrVal) { OPN2_Write(&fm,0x1+((w.addr>>8)<<1),w.val); //printf("write: %x = %.2x\n",w.addr,w.val); lastBusy=0; regPool[w.addr&0x1ff]=w.val; writes.pop_front(); } else { lastBusy++; if (fm.write_busy==0) { //printf("busycounter: %d\n",lastBusy); OPN2_Write(&fm,0x0+((w.addr>>8)<<1),w.addr); w.addrOrVal=true; } } } OPN2_Clock(&fm,o); os[0]+=o[0]; os[1]+=o[1]; //OPN2_Write(&fm,0,0); oscBuf[i]->data[oscBuf[i]->needle++]=fm.ch_out[i]<<7; } os[0]=(os[0]<<5); if (os[0]<-32768) os[0]=-32768; if (os[0]>32767) os[0]=32767; os[1]=(os[1]<<5); if (os[1]<-32768) os[1]=-32768; if (os[1]>32767) os[1]=32767; bufL[h]=os[0]; bufR[h]=os[1]; } } void DivPlatformGenesis::acquire_ymfm(short* bufL, short* bufR, size_t start, size_t len) { static int os[2]; ymfm::ym2612::fm_engine* fme=fm_ymfm->debug_engine(); for (size_t h=start; h=rate) { dacDelay-=rate; dacReady=true; } } if (dacMode && dacSample!=-1) { dacPeriod+=dacRate; if (dacPeriod>=rate) { DivSample* s=parent->getSample(dacSample); if (s->samples>0) { if (!isMuted[5]) { if (dacReady && writes.size()<16) { urgentWrite(0x2a,(unsigned char)s->data8[dacDirection?(s->samples-dacPos-1):dacPos]+0x80); dacReady=false; } } if (++dacPos>=s->samples) { if (s->loopStart>=0 && s->loopStart<(int)s->samples && !dacDirection) { dacPos=s->loopStart; } else { dacSample=-1; if (parent->song.brokenDACMode) { rWrite(0x2b,0); } } } while (dacPeriod>=rate) dacPeriod-=rate; } else { dacSample=-1; } } } os[0]=0; os[1]=0; if (!writes.empty()) { QueuedWrite& w=writes.front(); fm_ymfm->write(0x0+((w.addr>>8)<<1),w.addr); fm_ymfm->write(0x1+((w.addr>>8)<<1),w.val); regPool[w.addr&0x1ff]=w.val; writes.pop_front(); lastBusy=1; } if (ladder) { fm_ymfm->generate(&out_ymfm); } else { ((ymfm::ym3438*)fm_ymfm)->generate(&out_ymfm); } os[0]=out_ymfm.data[0]; os[1]=out_ymfm.data[1]; //OPN2_Write(&fm,0,0); for (int i=0; i<6; i++) { oscBuf[i]->data[oscBuf[i]->needle++]=(fme->debug_channel(i)->debug_output(0)+fme->debug_channel(i)->debug_output(1))<<6; } if (os[0]<-32768) os[0]=-32768; if (os[0]>32767) os[0]=32767; if (os[1]<-32768) os[1]=-32768; if (os[1]>32767) os[1]=32767; bufL[h]=os[0]; bufR[h]=os[1]; } } void DivPlatformGenesis::acquire(short* bufL, short* bufR, size_t start, size_t len) { if (useYMFM) { acquire_ymfm(bufL,bufR,start,len); } else { acquire_nuked(bufL,bufR,start,len); } } void DivPlatformGenesis::tick(bool sysTick) { for (int i=0; i<6; i++) { if (i==2 && extMode) continue; chan[i].std.next(); if (chan[i].std.vol.had) { chan[i].outVol=(chan[i].vol*MIN(127,chan[i].std.vol.val))/127; for (int j=0; j<4; j++) { unsigned short baseAddr=chanOffs[i]|opOffs[j]; DivInstrumentFM::Operator& op=chan[i].state.op[j]; if (isMuted[i]) { rWrite(baseAddr+ADDR_TL,127); } else { if (isOutput[chan[i].state.alg][j]) { rWrite(baseAddr+ADDR_TL,127-(((127-op.tl)*(chan[i].outVol&0x7f))/127)); } else { rWrite(baseAddr+ADDR_TL,op.tl); } } } } if (chan[i].std.arp.had) { if (!chan[i].inPorta) { if (chan[i].std.arp.mode) { chan[i].baseFreq=NOTE_FNUM_BLOCK(chan[i].std.arp.val,11); } else { chan[i].baseFreq=NOTE_FNUM_BLOCK(chan[i].note+(signed char)chan[i].std.arp.val,11); } } chan[i].freqChanged=true; } else { if (chan[i].std.arp.mode && chan[i].std.arp.finished) { chan[i].baseFreq=NOTE_FNUM_BLOCK(chan[i].note,11); chan[i].freqChanged=true; } } if (chan[i].std.panL.had) { chan[i].pan=chan[i].std.panL.val&3; rWrite(chanOffs[i]+ADDR_LRAF,(isMuted[i]?0:(chan[i].pan<<6))|(chan[i].state.fms&7)|((chan[i].state.ams&3)<<4)); } if (chan[i].std.pitch.had) { if (chan[i].std.pitch.mode) { chan[i].pitch2+=chan[i].std.pitch.val; CLAMP_VAR(chan[i].pitch2,-32768,32767); } else { chan[i].pitch2=chan[i].std.pitch.val; } chan[i].freqChanged=true; } if (chan[i].std.phaseReset.had) { if (chan[i].std.phaseReset.val==1 && chan[i].active) { chan[i].keyOn=true; } } if (chan[i].std.alg.had) { chan[i].state.alg=chan[i].std.alg.val; rWrite(chanOffs[i]+ADDR_FB_ALG,(chan[i].state.alg&7)|(chan[i].state.fb<<3)); if (!parent->song.algMacroBehavior) for (int j=0; j<4; j++) { unsigned short baseAddr=chanOffs[i]|opOffs[j]; DivInstrumentFM::Operator& op=chan[i].state.op[j]; if (isMuted[i]) { rWrite(baseAddr+ADDR_TL,127); } else { if (isOutput[chan[i].state.alg][j]) { rWrite(baseAddr+ADDR_TL,127-(((127-op.tl)*(chan[i].outVol&0x7f))/127)); } else { rWrite(baseAddr+ADDR_TL,op.tl); } } } } if (chan[i].std.fb.had) { chan[i].state.fb=chan[i].std.fb.val; rWrite(chanOffs[i]+ADDR_FB_ALG,(chan[i].state.alg&7)|(chan[i].state.fb<<3)); } if (chan[i].std.fms.had) { chan[i].state.fms=chan[i].std.fms.val; rWrite(chanOffs[i]+ADDR_LRAF,(isMuted[i]?0:(chan[i].pan<<6))|(chan[i].state.fms&7)|((chan[i].state.ams&3)<<4)); } if (chan[i].std.ams.had) { chan[i].state.ams=chan[i].std.ams.val; rWrite(chanOffs[i]+ADDR_LRAF,(isMuted[i]?0:(chan[i].pan<<6))|(chan[i].state.fms&7)|((chan[i].state.ams&3)<<4)); } for (int j=0; j<4; j++) { unsigned short baseAddr=chanOffs[i]|opOffs[j]; DivInstrumentFM::Operator& op=chan[i].state.op[j]; DivMacroInt::IntOp& m=chan[i].std.op[j]; if (m.am.had) { op.am=m.am.val; rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7)); } if (m.ar.had) { op.ar=m.ar.val; rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6)); } if (m.dr.had) { op.dr=m.dr.val; rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7)); } if (m.mult.had) { op.mult=m.mult.val; rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4)); } if (m.rr.had) { op.rr=m.rr.val; rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4)); } if (m.sl.had) { op.sl=m.sl.val; rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4)); } if (m.tl.had) { op.tl=127-m.tl.val; if (isMuted[i]) { rWrite(baseAddr+ADDR_TL,127); } else { if (isOutput[chan[i].state.alg][j]) { rWrite(baseAddr+ADDR_TL,127-(((127-op.tl)*(chan[i].outVol&0x7f))/127)); } else { rWrite(baseAddr+ADDR_TL,op.tl); } } } if (m.rs.had) { op.rs=m.rs.val; rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6)); } if (m.dt.had) { op.dt=m.dt.val; rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4)); } if (m.d2r.had) { op.d2r=m.d2r.val; rWrite(baseAddr+ADDR_DT2_D2R,op.d2r&31); } if (m.ssg.had) { op.ssgEnv=m.ssg.val; rWrite(baseAddr+ADDR_SSG,op.ssgEnv&15); } } } for (int i=0; i<512; i++) { if (pendingWrites[i]!=oldWrites[i]) { immWrite(i,pendingWrites[i]&0xff); oldWrites[i]=pendingWrites[i]; } } for (int i=0; i<6; i++) { if (i==2 && extMode) continue; if (chan[i].keyOn || chan[i].keyOff) { if (chan[i].hardReset && chan[i].keyOn) { for (int j=0; j<4; j++) { unsigned short baseAddr=chanOffs[i]|opOffs[j]; immWrite(baseAddr+ADDR_SL_RR,0x0f); immWrite(baseAddr+ADDR_TL,0x7f); oldWrites[baseAddr+ADDR_SL_RR]=-1; oldWrites[baseAddr+ADDR_TL]=-1; //rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4)); } } immWrite(0x28,0x00|konOffs[i]); if (chan[i].hardReset && chan[i].keyOn) { for (int j=0; j<4; j++) { unsigned short baseAddr=chanOffs[i]|opOffs[j]; for (int k=0; k<5; k++) { immWrite(baseAddr+ADDR_SL_RR,0x0f); } } } chan[i].keyOff=false; } } for (int i=0; i<6; i++) { if (i==2 && extMode) continue; if (chan[i].freqChanged) { if (parent->song.linearPitch==2) { chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,false,2,chan[i].pitch2,chipClock,CHIP_FREQBASE,11); } else { int fNum=parent->calcFreq(chan[i].baseFreq&0x7ff,chan[i].pitch,false,2,chan[i].pitch2,chipClock,CHIP_FREQBASE,11); int block=(chan[i].baseFreq&0xf800)>>11; if (fNum<0) fNum=0; if (fNum>2047) { while (block<7) { fNum>>=1; block++; } if (fNum>2047) fNum=2047; } chan[i].freq=(block<<11)|fNum; } if (chan[i].freq>0x3fff) chan[i].freq=0x3fff; immWrite(chanOffs[i]+ADDR_FREQH,chan[i].freq>>8); immWrite(chanOffs[i]+ADDR_FREQ,chan[i].freq&0xff); if (chan[i].furnaceDac && dacMode) { double off=1.0; if (dacSample>=0 && dacSamplesong.sampleLen) { DivSample* s=parent->getSample(dacSample); if (s->centerRate<1) { off=1.0; } else { off=(double)s->centerRate/8363.0; } } chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,false,2,chan[i].pitch2,1,1); dacRate=chan[i].freq*off; if (dacRate<1) dacRate=1; if (dumpWrites) addWrite(0xffff0001,dacRate); } chan[i].freqChanged=false; } if (chan[i].keyOn) { immWrite(0x28,0xf0|konOffs[i]); chan[i].keyOn=false; } } } void DivPlatformGenesis::muteChannel(int ch, bool mute) { isMuted[ch]=mute; for (int j=0; j<4; j++) { unsigned short baseAddr=chanOffs[ch]|opOffs[j]; DivInstrumentFM::Operator& op=chan[ch].state.op[j]; if (isMuted[ch]) { rWrite(baseAddr+ADDR_TL,127); } else { if (isOutput[chan[ch].state.alg][j]) { rWrite(baseAddr+ADDR_TL,127-(((127-op.tl)*(chan[ch].outVol&0x7f))/127)); } else { rWrite(baseAddr+ADDR_TL,op.tl); } } } rWrite(chanOffs[ch]+ADDR_LRAF,(isMuted[ch]?0:(chan[ch].pan<<6))|(chan[ch].state.fms&7)|((chan[ch].state.ams&3)<<4)); } int DivPlatformGenesis::dispatch(DivCommand c) { switch (c.cmd) { case DIV_CMD_NOTE_ON: { DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_FM); if (c.chan==5) { if (ins->type==DIV_INS_AMIGA) { dacMode=1; rWrite(0x2b,1<<7); } else if (chan[c.chan].furnaceDac) { dacMode=0; rWrite(0x2b,0<<7); } } if (c.chan==5 && dacMode) { if (skipRegisterWrites) break; if (ins->type==DIV_INS_AMIGA) { // Furnace mode dacSample=ins->amiga.getSample(c.value); if (dacSample<0 || dacSample>=parent->song.sampleLen) { dacSample=-1; if (dumpWrites) addWrite(0xffff0002,0); break; } else { rWrite(0x2b,1<<7); if (dumpWrites) addWrite(0xffff0000,dacSample); } dacPos=0; dacPeriod=0; if (c.value!=DIV_NOTE_NULL) { chan[c.chan].baseFreq=parent->calcBaseFreq(1,1,c.value,false); chan[c.chan].freqChanged=true; } chan[c.chan].furnaceDac=true; } else { // compatible mode if (c.value!=DIV_NOTE_NULL) { chan[c.chan].note=c.value; } dacSample=12*sampleBank+chan[c.chan].note%12; if (dacSample>=parent->song.sampleLen) { dacSample=-1; if (dumpWrites) addWrite(0xffff0002,0); break; } else { rWrite(0x2b,1<<7); if (dumpWrites) addWrite(0xffff0000,dacSample); } dacPos=0; dacPeriod=0; dacRate=MAX(1,parent->getSample(dacSample)->rate); if (dumpWrites) addWrite(0xffff0001,parent->getSample(dacSample)->rate); chan[c.chan].furnaceDac=false; } break; } if (chan[c.chan].insChanged) { chan[c.chan].state=ins->fm; } chan[c.chan].macroInit(ins); if (!chan[c.chan].std.vol.will) { chan[c.chan].outVol=chan[c.chan].vol; } for (int i=0; i<4; i++) { unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; DivInstrumentFM::Operator& op=chan[c.chan].state.op[i]; if (isMuted[c.chan]) { rWrite(baseAddr+ADDR_TL,127); } else { if (isOutput[chan[c.chan].state.alg][i]) { if (!chan[c.chan].active || chan[c.chan].insChanged) { rWrite(baseAddr+ADDR_TL,127-(((127-op.tl)*(chan[c.chan].outVol&0x7f))/127)); } } else { if (chan[c.chan].insChanged) { rWrite(baseAddr+ADDR_TL,op.tl); } } } if (chan[c.chan].insChanged) { rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4)); rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6)); rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7)); rWrite(baseAddr+ADDR_DT2_D2R,op.d2r&31); rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4)); rWrite(baseAddr+ADDR_SSG,op.ssgEnv&15); } } if (chan[c.chan].insChanged) { rWrite(chanOffs[c.chan]+ADDR_FB_ALG,(chan[c.chan].state.alg&7)|(chan[c.chan].state.fb<<3)); rWrite(chanOffs[c.chan]+ADDR_LRAF,(isMuted[c.chan]?0:(chan[c.chan].pan<<6))|(chan[c.chan].state.fms&7)|((chan[c.chan].state.ams&3)<<4)); } chan[c.chan].insChanged=false; if (c.value!=DIV_NOTE_NULL) { chan[c.chan].baseFreq=NOTE_FNUM_BLOCK(c.value,11); chan[c.chan].portaPause=false; chan[c.chan].note=c.value; chan[c.chan].freqChanged=true; } chan[c.chan].keyOn=true; chan[c.chan].active=true; break; } case DIV_CMD_NOTE_OFF: if (c.chan==5) { dacSample=-1; if (dumpWrites) addWrite(0xffff0002,0); if (parent->song.brokenDACMode) { rWrite(0x2b,0); if (dacMode) break; } } chan[c.chan].keyOff=true; chan[c.chan].keyOn=false; chan[c.chan].active=false; break; case DIV_CMD_NOTE_OFF_ENV: if (c.chan==5) { dacSample=-1; if (dumpWrites) addWrite(0xffff0002,0); } chan[c.chan].keyOff=true; chan[c.chan].keyOn=false; chan[c.chan].active=false; chan[c.chan].std.release(); break; case DIV_CMD_ENV_RELEASE: chan[c.chan].std.release(); break; case DIV_CMD_VOLUME: { chan[c.chan].vol=c.value; if (!chan[c.chan].std.vol.has) { chan[c.chan].outVol=c.value; } for (int i=0; i<4; i++) { unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; DivInstrumentFM::Operator& op=chan[c.chan].state.op[i]; if (isMuted[c.chan]) { rWrite(baseAddr+ADDR_TL,127); } else { if (isOutput[chan[c.chan].state.alg][i]) { rWrite(baseAddr+ADDR_TL,127-(((127-op.tl)*(chan[c.chan].outVol&0x7f))/127)); } else { rWrite(baseAddr+ADDR_TL,op.tl); } } } break; } case DIV_CMD_GET_VOLUME: { return chan[c.chan].vol; 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_PANNING: { if (c.value==0 && c.value2==0) { chan[c.chan].pan=3; } else { chan[c.chan].pan=(c.value2>0)|((c.value>0)<<1); } rWrite(chanOffs[c.chan]+ADDR_LRAF,(isMuted[c.chan]?0:(chan[c.chan].pan<<6))|(chan[c.chan].state.fms&7)|((chan[c.chan].state.ams&3)<<4)); break; } case DIV_CMD_PITCH: { chan[c.chan].pitch=c.value; chan[c.chan].freqChanged=true; break; } case DIV_CMD_NOTE_PORTA: { if (parent->song.linearPitch==2) { 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; } if (c.chan==5 && chan[c.chan].furnaceDac && dacMode) { int destFreq=parent->calcBaseFreq(1,1,c.value2,false); bool return2=false; if (destFreq>chan[c.chan].baseFreq) { chan[c.chan].baseFreq+=c.value*16; if (chan[c.chan].baseFreq>=destFreq) { chan[c.chan].baseFreq=destFreq; return2=true; } } else { chan[c.chan].baseFreq-=c.value*16; 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; } PLEASE_HELP_ME(chan[c.chan]); break; } case DIV_CMD_SAMPLE_MODE: { dacMode=c.value; rWrite(0x2b,c.value<<7); break; } case DIV_CMD_SAMPLE_BANK: sampleBank=c.value; if (sampleBank>(parent->song.sample.size()/12)) { sampleBank=parent->song.sample.size()/12; } break; case DIV_CMD_SAMPLE_DIR: { dacDirection=c.value; if (dumpWrites) addWrite(0xffff0003,dacDirection); break; } case DIV_CMD_LEGATO: { if (c.chan==5 && chan[c.chan].furnaceDac && dacMode) { chan[c.chan].baseFreq=parent->calcBaseFreq(1,1,c.value,false); } else { chan[c.chan].baseFreq=NOTE_FNUM_BLOCK(c.value,11); } chan[c.chan].note=c.value; chan[c.chan].freqChanged=true; break; } case DIV_CMD_FM_LFO: { lfoValue=(c.value&7)|((c.value>>4)<<3); rWrite(0x22,lfoValue); break; } case DIV_CMD_FM_FB: { chan[c.chan].state.fb=c.value&7; rWrite(chanOffs[c.chan]+ADDR_FB_ALG,(chan[c.chan].state.alg&7)|(chan[c.chan].state.fb<<3)); break; } case DIV_CMD_FM_MULT: { unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]]; op.mult=c.value2&15; rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4)); break; } case DIV_CMD_FM_TL: { unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]]; op.tl=c.value2; if (isMuted[c.chan]) { rWrite(baseAddr+ADDR_TL,127); } else { if (isOutput[chan[c.chan].state.alg][c.value]) { rWrite(baseAddr+ADDR_TL,127-(((127-op.tl)*(chan[c.chan].outVol&0x7f))/127)); } else { rWrite(baseAddr+ADDR_TL,op.tl); } } break; } case DIV_CMD_FM_AR: { if (c.value<0) { for (int i=0; i<4; i++) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[i]; op.ar=c.value2&31; unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6)); } } else if (c.value<4) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]]; op.ar=c.value2&31; unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6)); } break; } case DIV_CMD_FM_RS: { if (c.value<0) { for (int i=0; i<4; i++) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[i]; op.rs=c.value2&3; unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6)); } } else if (c.value<4) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]]; op.rs=c.value2&3; unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6)); } break; } case DIV_CMD_FM_AM: { if (c.value<0) { for (int i=0; i<4; i++) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[i]; op.am=c.value2&1; unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7)); } } else if (c.value<4) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]]; op.am=c.value2&1; unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7)); } break; } case DIV_CMD_FM_DR: { if (c.value<0) { for (int i=0; i<4; i++) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[i]; op.dr=c.value2&31; unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7)); } } else if (c.value<4) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]]; op.dr=c.value2&31; unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7)); } break; } case DIV_CMD_FM_SL: { if (c.value<0) { for (int i=0; i<4; i++) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[i]; op.sl=c.value2&15; unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4)); } } else if (c.value<4) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]]; op.sl=c.value2&15; unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4)); } break; } case DIV_CMD_FM_RR: { if (c.value<0) { for (int i=0; i<4; i++) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[i]; op.rr=c.value2&15; unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4)); } } else if (c.value<4) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]]; op.rr=c.value2&15; unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4)); } break; } case DIV_CMD_FM_D2R: { if (c.value<0) { for (int i=0; i<4; i++) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[i]; op.d2r=c.value2&31; unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; rWrite(baseAddr+ADDR_DT2_D2R,op.d2r&31); } } else if (c.value<4) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]]; op.d2r=c.value2&31; unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; rWrite(baseAddr+ADDR_DT2_D2R,op.d2r&31); } break; } case DIV_CMD_FM_DT: { if (c.value<0) { for (int i=0; i<4; i++) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[i]; op.dt=c.value&7; unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4)); } } else if (c.value<4) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]]; op.dt=c.value2&7; unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4)); } break; } case DIV_CMD_FM_SSG: { if (c.value<0) { for (int i=0; i<4; i++) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[i]; op.ssgEnv=8^(c.value2&15); unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; rWrite(baseAddr+ADDR_SSG,op.ssgEnv&15); } } else if (c.value<4) { DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]]; op.ssgEnv=8^(c.value2&15); unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; rWrite(baseAddr+ADDR_SSG,op.ssgEnv&15); } break; } case DIV_CMD_FM_HARD_RESET: chan[c.chan].hardReset=c.value; break; case DIV_ALWAYS_SET_VOLUME: return 0; break; case DIV_CMD_GET_VOLMAX: return 127; break; case DIV_CMD_PRE_PORTA: chan[c.chan].inPorta=c.value; break; case DIV_CMD_PRE_NOTE: break; default: //printf("WARNING: unimplemented command %d\n",c.cmd); break; } return 1; } void DivPlatformGenesis::forceIns() { for (int i=0; i<6; i++) { for (int j=0; j<4; j++) { unsigned short baseAddr=chanOffs[i]|opOffs[j]; DivInstrumentFM::Operator& op=chan[i].state.op[j]; if (isMuted[i]) { rWrite(baseAddr+ADDR_TL,127); } else { if (isOutput[chan[i].state.alg][j]) { rWrite(baseAddr+ADDR_TL,127-(((127-op.tl)*(chan[i].outVol&0x7f))/127)); } else { rWrite(baseAddr+ADDR_TL,op.tl); } } rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4)); rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6)); rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7)); rWrite(baseAddr+ADDR_DT2_D2R,op.d2r&31); rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4)); rWrite(baseAddr+ADDR_SSG,op.ssgEnv&15); } rWrite(chanOffs[i]+ADDR_FB_ALG,(chan[i].state.alg&7)|(chan[i].state.fb<<3)); rWrite(chanOffs[i]+ADDR_LRAF,(isMuted[i]?0:(chan[i].pan<<6))|(chan[i].state.fms&7)|((chan[i].state.ams&3)<<4)); if (chan[i].active) { chan[i].keyOn=true; chan[i].freqChanged=true; } } if (dacMode) { rWrite(0x2b,0x80); } immWrite(0x22,lfoValue); } void DivPlatformGenesis::toggleRegisterDump(bool enable) { DivDispatch::toggleRegisterDump(enable); } void* DivPlatformGenesis::getChanState(int ch) { return &chan[ch]; } DivDispatchOscBuffer* DivPlatformGenesis::getOscBuffer(int ch) { return oscBuf[ch]; } unsigned char* DivPlatformGenesis::getRegisterPool() { return regPool; } int DivPlatformGenesis::getRegisterPoolSize() { return 512; } void DivPlatformGenesis::reset() { while (!writes.empty()) writes.pop_front(); memset(regPool,0,512); if (useYMFM) { fm_ymfm->reset(); } OPN2_Reset(&fm); OPN2_SetChipType(ladder?ym3438_mode_ym2612:0); if (dumpWrites) { addWrite(0xffffffff,0); } for (int i=0; i<10; i++) { chan[i]=DivPlatformGenesis::Channel(); chan[i].std.setEngine(parent); chan[i].vol=0x7f; chan[i].outVol=0x7f; } for (int i=0; i<512; i++) { oldWrites[i]=-1; pendingWrites[i]=-1; } lastBusy=60; dacMode=0; dacPeriod=0; dacPos=0; dacRate=0; dacDelay=0; dacReady=true; dacSample=-1; dacDirection=false; sampleBank=0; lfoValue=8; extMode=false; if (dumpWrites) addWrite(0xffff0003,dacDirection); // LFO immWrite(0x22,lfoValue); delay=0; } bool DivPlatformGenesis::isStereo() { return true; } bool DivPlatformGenesis::keyOffAffectsArp(int ch) { return (ch>5); } bool DivPlatformGenesis::keyOffAffectsPorta(int ch) { return (ch>5); } void DivPlatformGenesis::notifyInsChange(int ins) { for (int i=0; i<6; i++) { if (chan[i].ins==ins) { chan[i].insChanged=true; } } } void DivPlatformGenesis::notifyInsDeletion(void* ins) { } void DivPlatformGenesis::poke(unsigned int addr, unsigned short val) { immWrite(addr,val); } void DivPlatformGenesis::poke(std::vector& wlist) { for (DivRegWrite& i: wlist) immWrite(i.addr,i.val); } int DivPlatformGenesis::getPortaFloor(int ch) { return (ch>5)?12:0; } void DivPlatformGenesis::setYMFM(bool use) { useYMFM=use; } void DivPlatformGenesis::setFlags(unsigned int flags) { switch (flags) { case 1: chipClock=COLOR_PAL*12.0/7.0; break; case 2: chipClock=8000000.0; break; case 3: chipClock=COLOR_NTSC*12.0/7.0; break; case 4: chipClock=COLOR_NTSC*9.0/4.0; break; default: chipClock=COLOR_NTSC*15.0/7.0; break; } ladder=flags&0x80000000; OPN2_SetChipType(ladder?ym3438_mode_ym2612:0); if (useYMFM) { if (fm_ymfm!=NULL) delete fm_ymfm; if (ladder) { fm_ymfm=new ymfm::ym2612(iface); } else { fm_ymfm=new ymfm::ym3438(iface); } rate=chipClock/144; } else { rate=chipClock/36; } for (int i=0; i<10; i++) { oscBuf[i]->rate=rate; } } int DivPlatformGenesis::init(DivEngine* p, int channels, int sugRate, unsigned int flags) { parent=p; dumpWrites=false; ladder=false; skipRegisterWrites=false; for (int i=0; i<10; i++) { isMuted[i]=false; oscBuf[i]=new DivDispatchOscBuffer; } fm_ymfm=NULL; setFlags(flags); reset(); return 10; } void DivPlatformGenesis::quit() { for (int i=0; i<10; i++) { delete oscBuf[i]; } if (fm_ymfm!=NULL) delete fm_ymfm; } DivPlatformGenesis::~DivPlatformGenesis() { }