furnace/src/engine/platform/swan.cpp
2022-03-07 19:04:20 +07:00

522 lines
13 KiB
C++

/**
* 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 "swan.h"
#include "../engine.h"
#include <math.h>
#define rWrite(a,v) if (!skipRegisterWrites) {writes.emplace(a,v); if (dumpWrites) {addWrite(a,v);}}
#define CHIP_DIVIDER 32
const char* regCheatSheetWS[]={
"CH1_Pitch", "00",
"CH2_Pitch", "02",
"CH3_Pitch", "04",
"CH4_Pitch", "06",
"CH1_Vol", "08",
"CH2_Vol", "09",
"CH3_Vol", "0A",
"CH4_Vol", "0B",
"Sweep_Value", "0C",
"Sweep_Time", "0D",
"Noise", "0E",
"Wave_Base", "0F",
"Ctrl", "10",
"Output", "11",
"Random", "12",
"Voice_Ctrl", "14",
"Wave_Mem", "40",
NULL
};
const char** DivPlatformSwan::getRegisterSheet() {
return regCheatSheetWS;
}
const char* DivPlatformSwan::getEffectName(unsigned char effect) {
switch (effect) {
case 0x10:
return "10xx: Change waveform";
break;
case 0x11:
return "11xx: Setup noise mode (0: disabled; 1-8: enabled/tap)";
break;
case 0x12:
return "12xx: Setup sweep period (0: disabled; 1-20: enabled/period)";
break;
case 0x13:
return "13xx: Set sweep amount";
break;
case 0x17:
return "17xx: Toggle PCM mode";
break;
}
return NULL;
}
void DivPlatformSwan::acquire(short* bufL, short* bufR, size_t start, size_t len) {
for (size_t h=start; h<start+len; h++) {
// PCM part
if (pcm && dacSample!=-1) {
dacPeriod+=dacRate;
while (dacPeriod>rate) {
DivSample* s=parent->getSample(dacSample);
if (s->samples<=0) {
dacSample=-1;
continue;
}
rWrite(0x09,(unsigned char)s->data8[dacPos++]+0x80);
if (dacPos>=s->samples) {
if (s->loopStart>=0 && s->loopStart<=(int)s->samples) {
dacPos=s->loopStart;
} else {
dacSample=-1;
}
}
dacPeriod-=rate;
}
}
// the rest
while (!writes.empty()) {
QueuedWrite w=writes.front();
if (w.addr<0x40) ws->SoundWrite(w.addr|0x80,w.val);
else ws->RAMWrite(w.addr&0x3f,w.val);
writes.pop();
}
int16_t samp[2]{0, 0};
ws->SoundUpdate(16);
ws->SoundFlush(samp, 1);
bufL[h]=samp[0];
bufR[h]=samp[1];
}
}
void DivPlatformSwan::updateWave(int ch) {
DivWavetable* wt=parent->getWave(chan[ch].wave);
unsigned char addr=0x40+ch*16;
if (wt->max<1 || wt->len<1) {
for (int i=0; i<16; i++) {
rWrite(addr+i,0);
}
} else {
for (int i=0; i<16; i++) {
unsigned char nibble1=(wt->data[(i*2)*wt->len/32]*15)/wt->max;
unsigned char nibble2=(wt->data[(1+i*2)*wt->len/32]*15)/wt->max;
rWrite(addr+i,nibble1|(nibble2<<4));
}
}
}
void DivPlatformSwan::calcAndWriteOutVol(int ch, int env) {
int vl=chan[ch].vol*((chan[ch].pan>>4)&0x0f)*env/225;
int vr=chan[ch].vol*(chan[ch].pan&0x0f)*env/225;
if (ch==1&&pcm) {
vl=(vl>0)?((vl>7)?3:2):0;
vr=(vr>0)?((vr>7)?3:2):0;
chan[1].outVol=vr|(vl<<2);
} else {
chan[ch].outVol=vr|(vl<<4);
}
writeOutVol(ch);
}
void DivPlatformSwan::writeOutVol(int ch) {
unsigned char val=isMuted[ch]?0:chan[ch].outVol;
if (ch==1&&pcm) {
rWrite(0x14,val)
} else {
rWrite(0x08+ch,val);
}
}
void DivPlatformSwan::tick() {
unsigned char sndCtrl=(pcm?0x20:0)|(sweep?0x40:0)|((noise>0)?0x80:0);
for (int i=0; i<4; i++) {
chan[i].std.next();
if (chan[i].std.hadVol) {
int env=chan[i].std.vol;
if(parent->getIns(chan[i].ins)->type==DIV_INS_AMIGA) {
env=MIN(env/4,15);
}
calcAndWriteOutVol(i,env);
}
if (chan[i].std.hadArp) {
if (!chan[i].inPorta) {
if (chan[i].std.arpMode) {
chan[i].baseFreq=NOTE_PERIODIC(chan[i].std.arp);
} else {
chan[i].baseFreq=NOTE_PERIODIC(chan[i].note+chan[i].std.arp);
}
}
chan[i].freqChanged=true;
} else {
if (chan[i].std.arpMode && chan[i].std.finishedArp) {
chan[i].baseFreq=NOTE_PERIODIC(chan[i].note);
chan[i].freqChanged=true;
}
}
if (chan[i].std.hadWave && !(i==1 && pcm)) {
if (chan[i].wave!=chan[i].std.wave) {
chan[i].wave=chan[i].std.wave;
updateWave(i);
}
}
if (chan[i].active) {
sndCtrl|=(1<<i);
}
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,true);
if (i==1 && pcm && furnaceDac) {
double off=1.0;
if (dacSample>=0 && dacSample<parent->song.sampleLen) {
DivSample* s=parent->getSample(dacSample);
if (s->centerRate<1) {
off=1.0;
} else {
off=8363.0/(double)s->centerRate;
}
}
dacRate=((double)chipClock/2)/MAX(1,off*chan[i].freq);
if (dumpWrites) addWrite(0xffff0001,dacRate);
}
if (chan[i].freq>2048) chan[i].freq=2048;
if (chan[i].freq<1) chan[i].freq=1;
int rVal=2048-chan[i].freq;
rWrite(i*2,rVal&0xff);
rWrite(i*2+1,rVal>>8);
if (chan[i].keyOn) {
if (!chan[i].std.hasVol) {
calcAndWriteOutVol(i,15);
}
if (chan[i].wave<0) {
chan[i].wave=0;
updateWave(i);
}
chan[i].keyOn=false;
}
if (chan[i].keyOff) {
chan[i].keyOff=false;
}
chan[i].freqChanged=false;
}
}
if (chan[3].std.hadDuty) {
noise=chan[3].std.duty;
if (noise>0) {
rWrite(0x0e,(noise-1)&0x07|0x18);
sndCtrl|=0x80;
} else {
sndCtrl&=~0x80;
}
}
rWrite(0x10,sndCtrl);
}
int DivPlatformSwan::dispatch(DivCommand c) {
switch (c.cmd) {
case DIV_CMD_NOTE_ON: {
DivInstrument* ins=parent->getIns(chan[c.chan].ins);
if (c.chan==1) {
if (ins->type==DIV_INS_AMIGA) {
pcm=true;
} else if (furnaceDac) {
pcm=false;
}
if (pcm) {
if (skipRegisterWrites) break;
dacPos=0;
dacPeriod=0;
if (ins->type==DIV_INS_AMIGA) {
dacSample=ins->amiga.initSample;
if (dacSample<0 || dacSample>=parent->song.sampleLen) {
dacSample=-1;
if (dumpWrites) addWrite(0xffff0002,0);
break;
} else {
if (dumpWrites) {
addWrite(0xffff0000,dacSample);
}
}
if (c.value!=DIV_NOTE_NULL) {
chan[1].baseFreq=NOTE_PERIODIC(c.value);
chan[1].freqChanged=true;
chan[1].note=c.value;
}
chan[1].active=true;
chan[1].keyOn=true;
chan[1].std.init(ins);
furnaceDac=true;
} else {
if (c.value!=DIV_NOTE_NULL) {
chan[1].note=c.value;
}
dacSample=12*sampleBank+chan[1].note%12;
if (dacSample>=parent->song.sampleLen) {
dacSample=-1;
if (dumpWrites) addWrite(0xffff0002,0);
break;
} else {
if (dumpWrites) addWrite(0xffff0000,dacSample);
}
dacRate=parent->getSample(dacSample)->rate;
if (dumpWrites) {
addWrite(0xffff0001,dacRate);
}
chan[1].active=true;
chan[1].keyOn=true;
furnaceDac=false;
}
break;
}
}
if (c.value!=DIV_NOTE_NULL) {
chan[c.chan].baseFreq=NOTE_PERIODIC(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].std.init(ins);
break;
}
case DIV_CMD_NOTE_OFF:
if (c.chan==1&&pcm) {
dacSample=-1;
if (dumpWrites) addWrite(0xffff0002,0);
pcm=false;
}
chan[c.chan].active=false;
chan[c.chan].keyOff=true;
chan[c.chan].std.init(NULL);
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;
}
break;
case DIV_CMD_VOLUME:
if (chan[c.chan].vol!=c.value) {
chan[c.chan].vol=c.value;
if (!chan[c.chan].std.hasVol) {
calcAndWriteOutVol(c.chan,15);
}
}
break;
case DIV_CMD_GET_VOLUME:
return chan[c.chan].vol;
break;
case DIV_CMD_PITCH:
chan[c.chan].pitch=c.value;
chan[c.chan].freqChanged=true;
break;
case DIV_CMD_WAVE:
chan[c.chan].wave=c.value;
updateWave(c.chan);
chan[c.chan].keyOn=true;
break;
case DIV_CMD_WS_SWEEP_TIME:
if (c.chan==2) {
if (c.value==0) {
sweep=false;
} else {
sweep=true;
rWrite(0x0d,(c.value-1)&0xff);
}
}
break;
case DIV_CMD_WS_SWEEP_AMOUNT:
if (c.chan==2) {
rWrite(0x0c,c.value&0xff);
}
break;
case DIV_CMD_NOTE_PORTA: {
int destFreq=NOTE_PERIODIC(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_STD_NOISE_MODE:
if (c.chan==3) {
noise=c.value&0xff;
if (noise>0) rWrite(0x0e,(noise-1)&0x07|0x18);
}
break;
case DIV_CMD_SAMPLE_MODE:
if (c.chan==1) pcm=c.value;
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_PANNING: {
chan[c.chan].pan=c.value;
if (!chan[c.chan].std.hasVol) {
calcAndWriteOutVol(c.chan,15);
}
break;
}
case DIV_CMD_LEGATO:
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((chan[c.chan].std.willArp && !chan[c.chan].std.arpMode)?(chan[c.chan].std.arp):(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) chan[c.chan].std.init(parent->getIns(chan[c.chan].ins));
}
chan[c.chan].inPorta=c.value;
break;
case DIV_CMD_GET_VOLMAX:
return 15;
break;
case DIV_ALWAYS_SET_VOLUME:
return 1;
break;
default:
break;
}
return 1;
}
void DivPlatformSwan::muteChannel(int ch, bool mute) {
isMuted[ch]=mute;
writeOutVol(ch);
}
void DivPlatformSwan::forceIns() {
for (int i=0; i<4; i++) {
chan[i].insChanged=true;
chan[i].freqChanged=true;
updateWave(i);
writeOutVol(i);
}
}
void* DivPlatformSwan::getChanState(int ch) {
return &chan[ch];
}
unsigned char* DivPlatformSwan::getRegisterPool() {
// get Random from emulator
regPool[0x12]=ws->SoundRead(0x92);
regPool[0x13]=ws->SoundRead(0x93);
return regPool;
}
int DivPlatformSwan::getRegisterPoolSize() {
return 128;
}
void DivPlatformSwan::reset() {
while (!writes.empty()) writes.pop();
memset(regPool,0,128);
for (int i=0; i<4; i++) {
chan[i]=Channel();
chan[i].vol=15;
chan[i].pan=0xff;
rWrite(0x08+i,0xff);
}
if (dumpWrites) {
addWrite(0xffffffff,0);
}
ws->SoundReset();
pcm=false;
sweep=false;
furnaceDac=false;
noise=0;
dacPeriod=0;
dacRate=0;
dacPos=0;
dacSample=-1;
sampleBank=0;
rWrite(0x0f,0x00); // wave table at 0x0000
rWrite(0x11,0x09); // enable speakers
}
bool DivPlatformSwan::isStereo() {
return true;
}
void DivPlatformSwan::notifyWaveChange(int wave) {
for (int i=0; i<4; i++) {
if (chan[i].wave==wave) {
updateWave(i);
}
}
}
void DivPlatformSwan::notifyInsDeletion(void* ins) {
for (int i=0; i<4; i++) {
chan[i].std.notifyInsDeletion((DivInstrument*)ins);
}
}
void DivPlatformSwan::poke(unsigned int addr, unsigned short val) {
rWrite(addr,val);
}
void DivPlatformSwan::poke(std::vector<DivRegWrite>& wlist) {
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
}
int DivPlatformSwan::init(DivEngine* p, int channels, int sugRate, unsigned int flags) {
parent=p;
dumpWrites=false;
skipRegisterWrites=false;
chipClock=3072000;
rate=chipClock/16; // = 192000kHz, should be enough
for (int i=0; i<4; i++) {
isMuted[i]=false;
}
ws=new WSwan();
reset();
return 4;
}
void DivPlatformSwan::quit() {
delete ws;
}
DivPlatformSwan::~DivPlatformSwan() {
}