2022-03-21 07:38:12 +00:00
|
|
|
/**
|
|
|
|
* 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 "snes.h"
|
|
|
|
#include "../engine.h"
|
2022-06-10 15:59:44 +00:00
|
|
|
#include "../../ta-log.h"
|
2022-03-21 07:38:12 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2022-09-18 10:59:58 +00:00
|
|
|
#define CHIP_FREQBASE 131072
|
2022-03-21 07:38:12 +00:00
|
|
|
|
2022-09-24 21:28:51 +00:00
|
|
|
#define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
|
2022-09-18 10:59:58 +00:00
|
|
|
#define chWrite(c,a,v) {rWrite((a)+(c)*16,v)}
|
|
|
|
#define sampleTableAddr(c) (sampleTableBase+(c)*4)
|
|
|
|
#define waveTableAddr(c) (sampleTableBase+8*4+(c)*9*16)
|
2022-03-21 07:38:12 +00:00
|
|
|
|
|
|
|
const char* regCheatSheetSNESDSP[]={
|
|
|
|
"VxVOLL", "x0",
|
|
|
|
"VxVOLR", "x1",
|
|
|
|
"VxPITCHL", "x2",
|
|
|
|
"VxPITCHH", "x3",
|
|
|
|
"VxSRCN", "x4",
|
|
|
|
"VxADSR1", "x5",
|
|
|
|
"VxADSR2", "x6",
|
|
|
|
"VxGAIN", "x7",
|
|
|
|
"VxENVX", "x8",
|
|
|
|
"VxOUTX", "x9",
|
|
|
|
"FIRx", "xF",
|
|
|
|
|
|
|
|
"MVOLL", "0C",
|
|
|
|
"MVOLR", "1C",
|
|
|
|
"EVOLL", "2C",
|
|
|
|
"EVOLR", "3C",
|
|
|
|
"KON", "4C",
|
|
|
|
"KOFF", "5C",
|
|
|
|
"FLG", "6C",
|
|
|
|
"ENDX", "7C",
|
|
|
|
|
|
|
|
"EFB", "0D",
|
|
|
|
"PMON", "2D",
|
|
|
|
"NON", "3D",
|
|
|
|
"EON", "4D",
|
|
|
|
"DIR", "5D",
|
|
|
|
"ESA", "6D",
|
|
|
|
"EDL", "7D",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
const char** DivPlatformSNES::getRegisterSheet() {
|
|
|
|
return regCheatSheetSNESDSP;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformSNES::acquire(short* bufL, short* bufR, size_t start, size_t len) {
|
2022-06-10 15:59:44 +00:00
|
|
|
short out[2];
|
|
|
|
short chOut[16];
|
2022-03-21 07:38:12 +00:00
|
|
|
for (size_t h=start; h<start+len; h++) {
|
2022-09-24 09:27:53 +00:00
|
|
|
// TODO: delay
|
|
|
|
if (!writes.empty()) {
|
|
|
|
QueuedWrite w=writes.front();
|
|
|
|
dsp.write(w.addr,w.val);
|
|
|
|
regPool[w.addr&0x7f]=w.val;
|
|
|
|
writes.pop();
|
|
|
|
}
|
|
|
|
|
2022-06-10 15:59:44 +00:00
|
|
|
dsp.set_output(out,1);
|
|
|
|
dsp.run(32);
|
|
|
|
dsp.get_voice_outputs(chOut);
|
2022-03-21 07:38:12 +00:00
|
|
|
bufL[h]=out[0];
|
|
|
|
bufR[h]=out[1];
|
2022-06-10 15:59:44 +00:00
|
|
|
for (int i=0; i<8; i++) {
|
2022-09-26 09:07:51 +00:00
|
|
|
int next=(3*(chOut[i*2]+chOut[i*2+1]))>>2;
|
2022-09-26 08:23:34 +00:00
|
|
|
if (next<-32768) next=-32768;
|
|
|
|
if (next>32767) next=32767;
|
|
|
|
next=(next*254)/MAX(1,globalVolL+globalVolR);
|
|
|
|
if (next<-32768) next=-32768;
|
|
|
|
if (next>32767) next=32767;
|
|
|
|
oscBuf[i]->data[oscBuf[i]->needle++]=next;
|
2022-06-10 15:59:44 +00:00
|
|
|
}
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 07:42:16 +00:00
|
|
|
void DivPlatformSNES::tick(bool sysTick) {
|
2022-06-10 15:59:44 +00:00
|
|
|
// KON/KOFF can't be written several times per one sample
|
|
|
|
// so they have to be accumulated
|
|
|
|
unsigned char kon=0;
|
|
|
|
unsigned char koff=0;
|
2022-03-21 07:38:12 +00:00
|
|
|
for (int i=0; i<8; i++) {
|
|
|
|
chan[i].std.next();
|
2022-09-25 00:40:03 +00:00
|
|
|
if (chan[i].std.vol.had) {
|
|
|
|
chan[i].outVol=VOL_SCALE_LOG(chan[i].vol&127,MIN(127,chan[i].std.vol.val),127);
|
|
|
|
}
|
2022-06-10 15:59:44 +00:00
|
|
|
if (chan[i].std.arp.had) {
|
2022-03-21 07:38:12 +00:00
|
|
|
if (!chan[i].inPorta) {
|
2022-09-27 08:57:35 +00:00
|
|
|
chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
|
|
|
chan[i].freqChanged=true;
|
|
|
|
}
|
2022-09-25 08:33:39 +00:00
|
|
|
if (chan[i].std.duty.had) {
|
|
|
|
noiseFreq=chan[i].std.duty.val;
|
|
|
|
writeControl=true;
|
|
|
|
}
|
2022-09-18 10:59:58 +00:00
|
|
|
if (chan[i].useWave && chan[i].std.wave.had) {
|
|
|
|
if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) {
|
|
|
|
chan[i].wave=chan[i].std.wave.val;
|
|
|
|
chan[i].ws.changeWave1(chan[i].wave);
|
|
|
|
}
|
|
|
|
}
|
2022-06-10 15:59:44 +00:00
|
|
|
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;
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
2022-06-10 15:59:44 +00:00
|
|
|
chan[i].freqChanged=true;
|
|
|
|
}
|
|
|
|
if (chan[i].std.panL.had) {
|
2022-09-26 08:23:34 +00:00
|
|
|
chan[i].panL=chan[i].std.panL.val&0x7f;
|
2022-06-10 15:59:44 +00:00
|
|
|
}
|
|
|
|
if (chan[i].std.panR.had) {
|
2022-09-26 08:23:34 +00:00
|
|
|
chan[i].panR=chan[i].std.panR.val&0x7f;
|
2022-06-10 15:59:44 +00:00
|
|
|
}
|
2022-09-25 08:33:39 +00:00
|
|
|
bool hasInverted=false;
|
|
|
|
if (chan[i].std.ex1.had) {
|
2022-09-25 08:50:33 +00:00
|
|
|
if (chan[i].invertL!=(bool)(chan[i].std.ex1.val&16)) {
|
2022-09-25 08:33:39 +00:00
|
|
|
chan[i].invertL=chan[i].std.ex1.val&16;
|
|
|
|
hasInverted=true;
|
|
|
|
}
|
2022-09-25 08:50:33 +00:00
|
|
|
if (chan[i].invertR!=(bool)(chan[i].std.ex1.val&8)) {
|
2022-09-25 08:33:39 +00:00
|
|
|
chan[i].invertR=chan[i].std.ex1.val&8;
|
|
|
|
hasInverted=true;
|
|
|
|
}
|
2022-09-25 08:50:33 +00:00
|
|
|
if (chan[i].pitchMod!=(bool)(chan[i].std.ex1.val&4)) {
|
2022-09-25 08:33:39 +00:00
|
|
|
chan[i].pitchMod=chan[i].std.ex1.val&4;
|
|
|
|
writePitchMod=true;
|
|
|
|
}
|
2022-09-25 08:50:33 +00:00
|
|
|
if (chan[i].echo!=(bool)(chan[i].std.ex1.val&2)) {
|
2022-09-25 08:33:39 +00:00
|
|
|
chan[i].echo=chan[i].std.ex1.val&2;
|
|
|
|
writeEcho=true;
|
|
|
|
}
|
2022-09-25 08:50:33 +00:00
|
|
|
if (chan[i].noise!=(bool)(chan[i].std.ex1.val&1)) {
|
2022-09-25 08:33:39 +00:00
|
|
|
chan[i].noise=chan[i].std.ex1.val&1;
|
|
|
|
writeNoise=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chan[i].std.vol.had || chan[i].std.panL.had || chan[i].std.panR.had || hasInverted) {
|
2022-06-10 15:59:44 +00:00
|
|
|
writeOutVol(i);
|
|
|
|
}
|
|
|
|
if (chan[i].setPos) {
|
|
|
|
// force keyon
|
|
|
|
chan[i].keyOn=true;
|
|
|
|
chan[i].setPos=false;
|
|
|
|
} else {
|
|
|
|
chan[i].audPos=0;
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
2022-09-18 10:59:58 +00:00
|
|
|
if (chan[i].useWave && chan[i].active) {
|
|
|
|
if (chan[i].ws.tick()) {
|
|
|
|
updateWave(i);
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 07:38:12 +00:00
|
|
|
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
|
2022-06-10 15:59:44 +00:00
|
|
|
DivSample* s=parent->getSample(chan[i].sample);
|
|
|
|
double off=(s->centerRate>=1)?((double)s->centerRate/8363.0):1.0;
|
2022-09-25 07:39:25 +00:00
|
|
|
if (chan[i].useWave) off=(double)chan[i].wtLen/32.0;
|
2022-06-10 15:59:44 +00:00
|
|
|
chan[i].freq=(unsigned int)(off*parent->calcFreq(chan[i].baseFreq,chan[i].pitch,false,2,chan[i].pitch2,chipClock,CHIP_FREQBASE));
|
|
|
|
if (chan[i].freq>16383) chan[i].freq=16383;
|
2022-03-21 07:38:12 +00:00
|
|
|
if (chan[i].keyOn) {
|
2022-09-18 10:59:58 +00:00
|
|
|
unsigned int start, end, loop;
|
2022-09-25 00:40:03 +00:00
|
|
|
unsigned short tabAddr=sampleTableAddr(i);
|
2022-09-18 10:59:58 +00:00
|
|
|
if (chan[i].useWave) {
|
|
|
|
start=waveTableAddr(i);
|
|
|
|
loop=start;
|
2022-09-26 07:51:16 +00:00
|
|
|
} else if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
|
2022-09-26 01:07:21 +00:00
|
|
|
start=sampleOff[chan[i].sample];
|
2022-09-18 10:59:58 +00:00
|
|
|
end=MIN(start+MAX(s->lengthBRR,1),getSampleMemCapacity());
|
|
|
|
loop=MAX(start,end-1);
|
|
|
|
if (chan[i].audPos>0) {
|
|
|
|
start=start+MIN(chan[i].audPos,s->lengthBRR-1)/16*9;
|
|
|
|
}
|
|
|
|
if (s->loopStart>=0) {
|
|
|
|
loop=start+s->loopStart/16*9;
|
|
|
|
}
|
2022-09-26 07:51:16 +00:00
|
|
|
} else {
|
|
|
|
start=0;
|
|
|
|
end=0;
|
|
|
|
loop=0;
|
2022-09-18 10:59:58 +00:00
|
|
|
}
|
|
|
|
sampleMem[tabAddr+0]=start&0xff;
|
|
|
|
sampleMem[tabAddr+1]=start>>8;
|
|
|
|
sampleMem[tabAddr+2]=loop&0xff;
|
|
|
|
sampleMem[tabAddr+3]=loop>>8;
|
2022-06-10 15:59:44 +00:00
|
|
|
kon|=(1<<i);
|
|
|
|
chan[i].keyOn=false;
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
|
|
|
if (chan[i].keyOff) {
|
2022-06-10 15:59:44 +00:00
|
|
|
koff|=(1<<i);
|
|
|
|
chan[i].keyOff=false;
|
|
|
|
}
|
|
|
|
if (chan[i].freqChanged) {
|
2022-09-18 10:59:58 +00:00
|
|
|
chWrite(i,2,chan[i].freq&0xff);
|
|
|
|
chWrite(i,3,chan[i].freq>>8);
|
2022-06-10 15:59:44 +00:00
|
|
|
chan[i].freqChanged=false;
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 08:33:39 +00:00
|
|
|
if (writeControl) {
|
2022-09-26 09:07:51 +00:00
|
|
|
unsigned char control=(noiseFreq&0x1f)|(echoOn?0:0x20);
|
2022-09-25 08:33:39 +00:00
|
|
|
rWrite(0x6c,control);
|
2022-09-25 08:50:33 +00:00
|
|
|
writeControl=false;
|
2022-09-25 08:33:39 +00:00
|
|
|
}
|
|
|
|
if (writeNoise) {
|
|
|
|
unsigned char noiseBits=(
|
|
|
|
(chan[0].noise?1:0)|
|
|
|
|
(chan[1].noise?2:0)|
|
|
|
|
(chan[2].noise?4:0)|
|
|
|
|
(chan[3].noise?8:0)|
|
|
|
|
(chan[4].noise?0x10:0)|
|
|
|
|
(chan[5].noise?0x20:0)|
|
|
|
|
(chan[6].noise?0x40:0)|
|
|
|
|
(chan[7].noise?0x80:0)
|
|
|
|
);
|
|
|
|
rWrite(0x3d,noiseBits);
|
2022-09-25 08:50:33 +00:00
|
|
|
writeNoise=false;
|
2022-09-25 08:33:39 +00:00
|
|
|
}
|
|
|
|
if (writePitchMod) {
|
|
|
|
unsigned char pitchModBits=(
|
|
|
|
(chan[0].pitchMod?1:0)|
|
|
|
|
(chan[1].pitchMod?2:0)|
|
|
|
|
(chan[2].pitchMod?4:0)|
|
|
|
|
(chan[3].pitchMod?8:0)|
|
|
|
|
(chan[4].pitchMod?0x10:0)|
|
|
|
|
(chan[5].pitchMod?0x20:0)|
|
|
|
|
(chan[6].pitchMod?0x40:0)|
|
|
|
|
(chan[7].pitchMod?0x80:0)
|
|
|
|
);
|
|
|
|
rWrite(0x2d,pitchModBits);
|
2022-09-25 08:50:33 +00:00
|
|
|
writePitchMod=false;
|
2022-09-25 08:33:39 +00:00
|
|
|
}
|
|
|
|
if (writeEcho) {
|
|
|
|
unsigned char echoBits=(
|
|
|
|
(chan[0].echo?1:0)|
|
|
|
|
(chan[1].echo?2:0)|
|
|
|
|
(chan[2].echo?4:0)|
|
|
|
|
(chan[3].echo?8:0)|
|
|
|
|
(chan[4].echo?0x10:0)|
|
|
|
|
(chan[5].echo?0x20:0)|
|
|
|
|
(chan[6].echo?0x40:0)|
|
|
|
|
(chan[7].echo?0x80:0)
|
|
|
|
);
|
|
|
|
rWrite(0x4d,echoBits);
|
2022-09-25 08:50:33 +00:00
|
|
|
writeEcho=false;
|
2022-09-25 08:33:39 +00:00
|
|
|
}
|
2022-09-18 18:01:46 +00:00
|
|
|
if (kon!=0) {
|
|
|
|
rWrite(0x4c,kon);
|
|
|
|
}
|
|
|
|
// always write KOFF as it's constantly polled
|
2022-06-10 15:59:44 +00:00
|
|
|
rWrite(0x5c,koff);
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DivPlatformSNES::dispatch(DivCommand c) {
|
|
|
|
switch (c.cmd) {
|
|
|
|
case DIV_CMD_NOTE_ON: {
|
2022-09-25 00:40:03 +00:00
|
|
|
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SNES);
|
2022-09-18 10:59:58 +00:00
|
|
|
if (ins->amiga.useWave) {
|
|
|
|
chan[c.chan].useWave=true;
|
2022-09-18 18:01:46 +00:00
|
|
|
chan[c.chan].wtLen=ins->amiga.waveLen+1;
|
2022-09-18 10:59:58 +00:00
|
|
|
if (chan[c.chan].insChanged) {
|
|
|
|
if (chan[c.chan].wave<0) {
|
|
|
|
chan[c.chan].wave=0;
|
|
|
|
chan[c.chan].ws.setWidth(chan[c.chan].wtLen);
|
|
|
|
chan[c.chan].ws.changeWave1(chan[c.chan].wave);
|
|
|
|
}
|
|
|
|
}
|
2022-09-18 18:01:46 +00:00
|
|
|
chan[c.chan].ws.init(ins,chan[c.chan].wtLen,15,chan[c.chan].insChanged);
|
2022-09-18 10:59:58 +00:00
|
|
|
} else {
|
|
|
|
chan[c.chan].sample=ins->amiga.getSample(c.value);
|
|
|
|
chan[c.chan].useWave=false;
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
2022-06-10 15:59:44 +00:00
|
|
|
if (chan[c.chan].useWave || chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) {
|
2022-03-21 07:38:12 +00:00
|
|
|
chan[c.chan].sample=-1;
|
|
|
|
}
|
2022-09-25 00:40:03 +00:00
|
|
|
if (chan[c.chan].insChanged) {
|
|
|
|
chan[c.chan].state=ins->snes;
|
2022-09-28 23:28:01 +00:00
|
|
|
writeEnv(c.chan);
|
2022-09-26 09:07:51 +00:00
|
|
|
}
|
2022-03-21 07:38:12 +00:00
|
|
|
if (c.value!=DIV_NOTE_NULL) {
|
2022-09-18 10:59:58 +00:00
|
|
|
chan[c.chan].baseFreq=round(NOTE_FREQUENCY(c.value));
|
2022-03-21 07:38:12 +00:00
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
chan[c.chan].note=c.value;
|
|
|
|
}
|
|
|
|
chan[c.chan].active=true;
|
|
|
|
chan[c.chan].keyOn=true;
|
2022-06-10 15:59:44 +00:00
|
|
|
chan[c.chan].macroInit(ins);
|
|
|
|
chan[c.chan].insChanged=false;
|
2022-03-21 07:38:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIV_CMD_NOTE_OFF:
|
|
|
|
chan[c.chan].sample=-1;
|
|
|
|
chan[c.chan].active=false;
|
|
|
|
chan[c.chan].keyOff=true;
|
2022-06-10 15:59:44 +00:00
|
|
|
chan[c.chan].macroInit(NULL);
|
2022-03-21 07:38:12 +00:00
|
|
|
break;
|
|
|
|
case DIV_CMD_NOTE_OFF_ENV:
|
|
|
|
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].ins=c.value;
|
2022-06-10 15:59:44 +00:00
|
|
|
chan[c.chan].insChanged=true;
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIV_CMD_VOLUME:
|
|
|
|
if (chan[c.chan].vol!=c.value) {
|
|
|
|
chan[c.chan].vol=c.value;
|
2022-09-27 08:57:35 +00:00
|
|
|
if (!chan[c.chan].std.vol.has) {
|
|
|
|
chan[c.chan].outVol=c.value;
|
|
|
|
writeOutVol(c.chan);
|
|
|
|
}
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIV_CMD_GET_VOLUME:
|
2022-06-10 15:59:44 +00:00
|
|
|
return chan[c.chan].vol;
|
|
|
|
break;
|
|
|
|
case DIV_CMD_PANNING:
|
2022-09-26 08:23:34 +00:00
|
|
|
chan[c.chan].panL=c.value>>1;
|
|
|
|
chan[c.chan].panR=c.value2>>1;
|
2022-06-10 15:59:44 +00:00
|
|
|
writeOutVol(c.chan);
|
2022-03-21 07:38:12 +00:00
|
|
|
break;
|
|
|
|
case DIV_CMD_PITCH:
|
|
|
|
chan[c.chan].pitch=c.value;
|
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
break;
|
2022-09-28 23:28:01 +00:00
|
|
|
case DIV_CMD_WAVE:
|
|
|
|
if (!chan[c.chan].useWave) break;
|
|
|
|
chan[c.chan].wave=c.value;
|
|
|
|
chan[c.chan].ws.changeWave1(chan[c.chan].wave);
|
|
|
|
break;
|
2022-03-21 07:38:12 +00:00
|
|
|
case DIV_CMD_NOTE_PORTA: {
|
2022-06-10 15:59:44 +00:00
|
|
|
int destFreq=round(NOTE_FREQUENCY(c.value2));
|
2022-03-21 07:38:12 +00:00
|
|
|
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: {
|
2022-06-10 15:59:44 +00:00
|
|
|
chan[c.chan].baseFreq=round(NOTE_FREQUENCY(c.value+((chan[c.chan].std.arp.will && !chan[c.chan].std.arp.mode)?(chan[c.chan].std.arp.val):(0))));
|
2022-03-21 07:38:12 +00:00
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
chan[c.chan].note=c.value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIV_CMD_PRE_PORTA:
|
|
|
|
if (chan[c.chan].active && c.value2) {
|
2022-09-25 00:40:03 +00:00
|
|
|
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_SNES));
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
|
|
|
chan[c.chan].inPorta=c.value;
|
|
|
|
break;
|
|
|
|
case DIV_CMD_SAMPLE_POS:
|
2022-09-28 23:28:01 +00:00
|
|
|
// may have to remove this
|
2022-03-21 07:38:12 +00:00
|
|
|
chan[c.chan].audPos=c.value;
|
|
|
|
chan[c.chan].setPos=true;
|
|
|
|
break;
|
2022-09-28 23:28:01 +00:00
|
|
|
case DIV_CMD_STD_NOISE_MODE:
|
|
|
|
chan[c.chan].noise=c.value;
|
|
|
|
writeNoise=true;
|
|
|
|
break;
|
|
|
|
case DIV_CMD_SNES_PITCH_MOD:
|
|
|
|
chan[c.chan].pitchMod=c.value;
|
|
|
|
writePitchMod=true;
|
|
|
|
break;
|
|
|
|
case DIV_CMD_SNES_INVERT:
|
|
|
|
chan[c.chan].invertL=(c.value>>4);
|
|
|
|
chan[c.chan].invertR=c.chan&15;
|
|
|
|
writeOutVol(c.chan);
|
|
|
|
break;
|
|
|
|
case DIV_CMD_SNES_GAIN_MODE:
|
|
|
|
if (c.value) {
|
|
|
|
chan[c.chan].state.useEnv=false;
|
|
|
|
switch (c.value) {
|
|
|
|
case 1:
|
|
|
|
chan[c.chan].state.gainMode=DivInstrumentSNES::GAIN_MODE_DIRECT;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
chan[c.chan].state.gainMode=DivInstrumentSNES::GAIN_MODE_DEC_LINEAR;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
chan[c.chan].state.gainMode=DivInstrumentSNES::GAIN_MODE_DEC_LOG;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
chan[c.chan].state.gainMode=DivInstrumentSNES::GAIN_MODE_INC_LINEAR;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
chan[c.chan].state.gainMode=DivInstrumentSNES::GAIN_MODE_INC_INVLOG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chan[c.chan].state.useEnv=true;
|
|
|
|
}
|
|
|
|
writeEnv(c.chan);
|
|
|
|
break;
|
|
|
|
case DIV_CMD_SNES_GAIN:
|
|
|
|
if (chan[c.chan].state.gainMode==DivInstrumentSNES::GAIN_MODE_DIRECT) {
|
|
|
|
chan[c.chan].state.gain=c.value&0x7f;
|
|
|
|
} else {
|
|
|
|
chan[c.chan].state.gain=c.value&0x1f;
|
|
|
|
}
|
|
|
|
if (!chan[c.chan].state.useEnv) writeEnv(c.chan);
|
|
|
|
break;
|
|
|
|
case DIV_CMD_STD_NOISE_FREQ:
|
|
|
|
noiseFreq=c.value&0x1f;
|
|
|
|
writeControl=true;
|
|
|
|
break;
|
|
|
|
case DIV_CMD_FM_AR:
|
|
|
|
chan[c.chan].state.a=c.value&15;
|
|
|
|
if (chan[c.chan].state.useEnv) writeEnv(c.chan);
|
|
|
|
break;
|
|
|
|
case DIV_CMD_FM_DR:
|
|
|
|
chan[c.chan].state.d=c.value&7;
|
|
|
|
if (chan[c.chan].state.useEnv) writeEnv(c.chan);
|
|
|
|
break;
|
|
|
|
case DIV_CMD_FM_SL:
|
|
|
|
chan[c.chan].state.s=c.value&7;
|
|
|
|
if (chan[c.chan].state.useEnv) writeEnv(c.chan);
|
|
|
|
break;
|
|
|
|
case DIV_CMD_FM_RR:
|
|
|
|
chan[c.chan].state.r=c.value&0x1f;
|
|
|
|
if (chan[c.chan].state.useEnv) writeEnv(c.chan);
|
|
|
|
break;
|
2022-09-26 09:07:51 +00:00
|
|
|
case DIV_CMD_SNES_ECHO:
|
|
|
|
chan[c.chan].echo=c.value;
|
|
|
|
writeEcho=true;
|
|
|
|
break;
|
|
|
|
case DIV_CMD_SNES_ECHO_DELAY: {
|
|
|
|
echoDelay=c.value&15;
|
|
|
|
unsigned char esa=0xf8-(echoDelay<<3);
|
|
|
|
if (echoOn) {
|
|
|
|
rWrite(0x6d,esa);
|
|
|
|
rWrite(0x7d,echoDelay);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIV_CMD_SNES_ECHO_ENABLE:
|
|
|
|
echoOn=c.value;
|
|
|
|
initEcho();
|
|
|
|
break;
|
|
|
|
case DIV_CMD_SNES_ECHO_FEEDBACK:
|
|
|
|
echoFeedback=c.value;
|
|
|
|
if (echoOn) {
|
|
|
|
rWrite(0x0d,echoFeedback);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIV_CMD_SNES_ECHO_FIR:
|
|
|
|
echoFIR[c.value&7]=c.value2;
|
|
|
|
if (echoOn) {
|
|
|
|
rWrite(0x0f+((c.value&7)<<4),echoFIR[c.value&7]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIV_CMD_SNES_ECHO_VOL_LEFT:
|
|
|
|
echoVolL=c.value;
|
|
|
|
if (echoOn) {
|
|
|
|
rWrite(0x2c,echoVolL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIV_CMD_SNES_ECHO_VOL_RIGHT:
|
|
|
|
echoVolR=c.value;
|
|
|
|
if (echoOn) {
|
|
|
|
rWrite(0x3c,echoVolR);
|
|
|
|
}
|
|
|
|
break;
|
2022-03-21 07:38:12 +00:00
|
|
|
case DIV_CMD_GET_VOLMAX:
|
|
|
|
return 127;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-09-18 10:59:58 +00:00
|
|
|
void DivPlatformSNES::updateWave(int ch) {
|
|
|
|
// Due to the overflow bug in hardware's resampler, the written amplitude here is half of maximum
|
2022-09-25 00:40:03 +00:00
|
|
|
unsigned short pos=waveTableAddr(ch);
|
2022-09-18 10:59:58 +00:00
|
|
|
for (int i=0; i<chan[ch].wtLen/16; i++) {
|
|
|
|
sampleMem[pos++]=0xb0;
|
|
|
|
for (int j=0; j<8; j++) {
|
|
|
|
int nibble1=(chan[ch].ws.output[i*16+j*2]-8)&15;
|
|
|
|
int nibble2=(chan[ch].ws.output[i*16+j*2+1]-8)&15;
|
|
|
|
sampleMem[pos++]=(nibble1<<4)|nibble2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sampleMem[pos-9]=0xb3; // mark loop
|
|
|
|
}
|
|
|
|
|
2022-06-10 15:59:44 +00:00
|
|
|
void DivPlatformSNES::writeOutVol(int ch) {
|
|
|
|
int outL=0;
|
|
|
|
int outR=0;
|
|
|
|
if (!isMuted[ch]) {
|
2022-09-25 00:40:03 +00:00
|
|
|
outL=(globalVolL*((chan[ch].outVol*chan[ch].panL)/127))/127;
|
|
|
|
outR=(globalVolR*((chan[ch].outVol*chan[ch].panR)/127))/127;
|
2022-09-25 08:33:39 +00:00
|
|
|
if (chan[ch].invertL) outL=-outL;
|
|
|
|
if (chan[ch].invertR) outR=-outR;
|
2022-06-10 15:59:44 +00:00
|
|
|
}
|
2022-09-18 10:59:58 +00:00
|
|
|
chWrite(ch,0,outL);
|
|
|
|
chWrite(ch,1,outR);
|
2022-06-10 15:59:44 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 23:28:01 +00:00
|
|
|
void DivPlatformSNES::writeEnv(int ch) {
|
|
|
|
if (chan[ch].state.useEnv) {
|
|
|
|
chWrite(ch,5,chan[ch].state.a|(chan[ch].state.d<<4)|0x80);
|
|
|
|
chWrite(ch,6,chan[ch].state.r|(chan[ch].state.s<<5));
|
|
|
|
} else {
|
|
|
|
chWrite(ch,5,0);
|
|
|
|
switch (chan[ch].state.gainMode) {
|
|
|
|
case DivInstrumentSNES::GAIN_MODE_DIRECT:
|
|
|
|
chWrite(ch,7,chan[ch].state.gain&127);
|
|
|
|
break;
|
|
|
|
case DivInstrumentSNES::GAIN_MODE_DEC_LINEAR:
|
|
|
|
chWrite(ch,7,0x80|(chan[ch].state.gain&31));
|
|
|
|
break;
|
|
|
|
case DivInstrumentSNES::GAIN_MODE_INC_LINEAR:
|
|
|
|
chWrite(ch,7,0xc0|(chan[ch].state.gain&31));
|
|
|
|
break;
|
|
|
|
case DivInstrumentSNES::GAIN_MODE_DEC_LOG:
|
|
|
|
chWrite(ch,7,0xa0|(chan[ch].state.gain&31));
|
|
|
|
break;
|
|
|
|
case DivInstrumentSNES::GAIN_MODE_INC_INVLOG:
|
|
|
|
chWrite(ch,7,0xe0|(chan[ch].state.gain&31));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 07:38:12 +00:00
|
|
|
void DivPlatformSNES::muteChannel(int ch, bool mute) {
|
|
|
|
isMuted[ch]=mute;
|
2022-06-10 15:59:44 +00:00
|
|
|
writeOutVol(ch);
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformSNES::forceIns() {
|
|
|
|
for (int i=0; i<8; i++) {
|
|
|
|
chan[i].insChanged=true;
|
|
|
|
chan[i].freqChanged=true;
|
|
|
|
chan[i].sample=-1;
|
2022-09-25 07:47:30 +00:00
|
|
|
if (chan[i].active && chan[i].useWave) {
|
|
|
|
updateWave(i);
|
|
|
|
}
|
|
|
|
writeOutVol(i);
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
2022-09-25 08:50:33 +00:00
|
|
|
writeControl=true;
|
|
|
|
writeNoise=true;
|
|
|
|
writePitchMod=true;
|
|
|
|
writeEcho=true;
|
2022-09-26 09:07:51 +00:00
|
|
|
initEcho();
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* DivPlatformSNES::getChanState(int ch) {
|
|
|
|
return &chan[ch];
|
|
|
|
}
|
|
|
|
|
2022-06-10 15:59:44 +00:00
|
|
|
DivMacroInt* DivPlatformSNES::getChanMacroInt(int ch) {
|
|
|
|
return &chan[ch].std;
|
|
|
|
}
|
|
|
|
|
|
|
|
DivDispatchOscBuffer* DivPlatformSNES::getOscBuffer(int ch) {
|
|
|
|
return oscBuf[ch];
|
|
|
|
}
|
|
|
|
|
2022-03-21 07:38:12 +00:00
|
|
|
unsigned char* DivPlatformSNES::getRegisterPool() {
|
|
|
|
// get states from emulator
|
|
|
|
for (int i=0; i<0x80; i+=0x10) {
|
2022-06-10 15:59:44 +00:00
|
|
|
regPool[i+8]=dsp.read(i+8);
|
|
|
|
regPool[i+9]=dsp.read(i+9);
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
2022-06-10 15:59:44 +00:00
|
|
|
regPool[0x7c]=dsp.read(0x7c); // ENDX
|
2022-03-21 07:38:12 +00:00
|
|
|
return regPool;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DivPlatformSNES::getRegisterPoolSize() {
|
|
|
|
return 128;
|
|
|
|
}
|
|
|
|
|
2022-09-26 09:07:51 +00:00
|
|
|
void DivPlatformSNES::initEcho() {
|
|
|
|
unsigned char esa=0xf8-(echoDelay<<3);
|
|
|
|
if (echoOn) {
|
|
|
|
rWrite(0x6d,esa);
|
|
|
|
rWrite(0x7d,echoDelay);
|
|
|
|
rWrite(0x0d,echoFeedback);
|
|
|
|
rWrite(0x2c,echoVolL);
|
|
|
|
rWrite(0x3c,echoVolR);
|
|
|
|
for (int i=0; i<8; i++) {
|
|
|
|
rWrite(0x0f+(i<<4),echoFIR[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rWrite(0x6d,0);
|
|
|
|
rWrite(0x7d,0);
|
|
|
|
rWrite(0x2c,0);
|
|
|
|
rWrite(0x3c,0);
|
|
|
|
}
|
|
|
|
writeControl=true;
|
|
|
|
}
|
|
|
|
|
2022-03-21 07:38:12 +00:00
|
|
|
void DivPlatformSNES::reset() {
|
2022-09-26 09:07:51 +00:00
|
|
|
memcpy(sampleMem,copyOfSampleMem,65536);
|
2022-06-10 15:59:44 +00:00
|
|
|
dsp.init(sampleMem);
|
|
|
|
dsp.set_output(NULL,0);
|
|
|
|
memset(regPool,0,128);
|
2022-09-24 09:27:53 +00:00
|
|
|
// this can't be 0 or channel 1 won't play
|
|
|
|
// this can't be 0x100 either as that's used by SPC700 page 1 and the stack
|
2022-09-25 00:40:03 +00:00
|
|
|
// this may not even be 0x200 as some space will be taken by the playback routine and variables
|
2022-09-24 09:27:53 +00:00
|
|
|
sampleTableBase=0x200;
|
2022-06-10 15:59:44 +00:00
|
|
|
rWrite(0x5d,sampleTableBase>>8);
|
|
|
|
rWrite(0x0c,127); // global volume left
|
|
|
|
rWrite(0x1c,127); // global volume right
|
2022-09-18 10:59:58 +00:00
|
|
|
rWrite(0x6c,0); // get DSP out of reset
|
2022-03-21 07:38:12 +00:00
|
|
|
for (int i=0; i<8; i++) {
|
|
|
|
chan[i]=Channel();
|
2022-06-10 15:59:44 +00:00
|
|
|
chan[i].std.setEngine(parent);
|
2022-09-18 10:59:58 +00:00
|
|
|
chan[i].ws.setEngine(parent);
|
2022-09-26 08:23:34 +00:00
|
|
|
chan[i].ws.init(NULL,32,15);
|
2022-06-10 15:59:44 +00:00
|
|
|
writeOutVol(i);
|
2022-09-18 10:59:58 +00:00
|
|
|
chWrite(i,4,i); // source number
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
2022-09-25 08:50:33 +00:00
|
|
|
writeControl=false;
|
|
|
|
writeNoise=false;
|
|
|
|
writePitchMod=false;
|
|
|
|
writeEcho=false;
|
2022-09-26 09:07:51 +00:00
|
|
|
|
|
|
|
echoDelay=0;
|
|
|
|
echoFeedback=0;
|
|
|
|
echoFIR[0]=127;
|
|
|
|
echoFIR[1]=0;
|
|
|
|
echoFIR[2]=0;
|
|
|
|
echoFIR[3]=0;
|
|
|
|
echoFIR[4]=0;
|
|
|
|
echoFIR[5]=0;
|
|
|
|
echoFIR[6]=0;
|
|
|
|
echoFIR[7]=0;
|
|
|
|
echoVolL=127;
|
|
|
|
echoVolR=127;
|
|
|
|
echoOn=false;
|
|
|
|
|
|
|
|
initEcho();
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DivPlatformSNES::isStereo() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformSNES::notifyInsChange(int ins) {
|
|
|
|
for (int i=0; i<8; i++) {
|
|
|
|
if (chan[i].ins==ins) {
|
|
|
|
chan[i].insChanged=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 10:59:58 +00:00
|
|
|
void DivPlatformSNES::notifyWaveChange(int wave) {
|
|
|
|
for (int i=0; i<8; i++) {
|
|
|
|
if (chan[i].useWave && chan[i].wave==wave) {
|
|
|
|
chan[i].ws.changeWave1(wave);
|
|
|
|
if (chan[i].active) {
|
|
|
|
updateWave(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 07:38:12 +00:00
|
|
|
void DivPlatformSNES::notifyInsDeletion(void* ins) {
|
|
|
|
for (int i=0; i<8; i++) {
|
|
|
|
chan[i].std.notifyInsDeletion((DivInstrument*)ins);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformSNES::poke(unsigned int addr, unsigned short val) {
|
|
|
|
rWrite(addr,val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformSNES::poke(std::vector<DivRegWrite>& wlist) {
|
|
|
|
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
|
|
|
|
}
|
|
|
|
|
2022-06-10 15:59:44 +00:00
|
|
|
const void* DivPlatformSNES::getSampleMem(int index) {
|
|
|
|
return index == 0 ? sampleMem : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DivPlatformSNES::getSampleMemCapacity(int index) {
|
|
|
|
// TODO change it based on current echo buffer size
|
|
|
|
return index == 0 ? 65536 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DivPlatformSNES::getSampleMemUsage(int index) {
|
|
|
|
return index == 0 ? sampleMemLen : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformSNES::renderSamples() {
|
2022-09-26 09:07:51 +00:00
|
|
|
memset(copyOfSampleMem,0,getSampleMemCapacity());
|
2022-09-26 01:07:21 +00:00
|
|
|
memset(sampleOff,0,256*sizeof(unsigned int));
|
2022-06-10 15:59:44 +00:00
|
|
|
|
2022-09-18 10:59:58 +00:00
|
|
|
// skip past sample table and wavetable buffer
|
|
|
|
size_t memPos=sampleTableBase+8*4+8*9*16;
|
2022-06-10 15:59:44 +00:00
|
|
|
for (int i=0; i<parent->song.sampleLen; i++) {
|
|
|
|
DivSample* s=parent->song.sample[i];
|
|
|
|
int length=s->lengthBRR;
|
|
|
|
int actualLength=MIN((int)(getSampleMemCapacity()-memPos)/9*9,length);
|
|
|
|
if (actualLength>0) {
|
2022-09-26 01:07:21 +00:00
|
|
|
sampleOff[i]=memPos;
|
2022-09-26 09:07:51 +00:00
|
|
|
memcpy(©OfSampleMem[memPos],s->dataBRR,actualLength);
|
2022-06-10 15:59:44 +00:00
|
|
|
memPos+=actualLength;
|
|
|
|
}
|
|
|
|
if (actualLength<length) {
|
|
|
|
// terminate the sample
|
2022-09-26 09:07:51 +00:00
|
|
|
copyOfSampleMem[memPos-9]=1;
|
2022-06-10 15:59:44 +00:00
|
|
|
logW("out of BRR memory for sample %d!",i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sampleMemLen=memPos;
|
2022-09-28 21:42:46 +00:00
|
|
|
memcpy(sampleMem,copyOfSampleMem,65536);
|
2022-06-10 15:59:44 +00:00
|
|
|
}
|
|
|
|
|
2022-09-25 00:40:03 +00:00
|
|
|
void DivPlatformSNES::setFlags(unsigned int flags) {
|
|
|
|
globalVolL=127-(flags&127);
|
|
|
|
globalVolR=127-((flags>>8)&127);
|
|
|
|
}
|
|
|
|
|
2022-03-21 07:38:12 +00:00
|
|
|
int DivPlatformSNES::init(DivEngine* p, int channels, int sugRate, unsigned int flags) {
|
|
|
|
parent=p;
|
|
|
|
dumpWrites=false;
|
|
|
|
skipRegisterWrites=false;
|
2022-09-26 09:07:51 +00:00
|
|
|
sampleMemLen=0;
|
|
|
|
chipClock=1024000;
|
|
|
|
rate=chipClock/32;
|
2022-03-21 07:38:12 +00:00
|
|
|
for (int i=0; i<8; i++) {
|
2022-06-10 15:59:44 +00:00
|
|
|
oscBuf[i]=new DivDispatchOscBuffer;
|
2022-09-26 09:07:51 +00:00
|
|
|
oscBuf[i]->rate=rate;
|
2022-03-21 07:38:12 +00:00
|
|
|
isMuted[i]=false;
|
|
|
|
}
|
2022-09-25 00:40:03 +00:00
|
|
|
setFlags(flags);
|
2022-03-21 07:38:12 +00:00
|
|
|
reset();
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformSNES::quit() {
|
2022-06-10 15:59:44 +00:00
|
|
|
for (int i=0; i<8; i++) {
|
|
|
|
delete oscBuf[i];
|
|
|
|
}
|
2022-03-21 07:38:12 +00:00
|
|
|
}
|