furnace/src/engine/platform/amiga.cpp

480 lines
14 KiB
C++
Raw Normal View History

2022-02-15 03:12:20 +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.
*/
2022-03-27 05:02:17 +00:00
#define _USE_MATH_DEFINES
2022-01-15 20:50:53 +00:00
#include "amiga.h"
#include "../engine.h"
#include <math.h>
#define AMIGA_DIVIDER 8
#define CHIP_DIVIDER 16
2022-01-15 20:50:53 +00:00
2022-02-02 07:14:42 +00:00
const char* regCheatSheetAmiga[]={
"DMACON", "96",
"INTENA", "9A",
"ADKCON", "9E",
"AUD0LCH", "A0",
"AUD0LCL", "A2",
"AUD0LEN", "A4",
"AUD0PER", "A6",
"AUD0VOL", "A8",
"AUD0DAT", "AA",
"AUD1LCH", "B0",
"AUD1LCL", "B2",
"AUD1LEN", "B4",
"AUD1PER", "B6",
"AUD1VOL", "B8",
"AUD1DAT", "BA",
"AUD2LCH", "C0",
"AUD2LCL", "C2",
"AUD2LEN", "C4",
"AUD2PER", "C6",
"AUD2VOL", "C8",
"AUD2DAT", "CA",
"AUD3LCH", "D0",
"AUD3LCL", "D2",
"AUD3LEN", "D4",
"AUD3PER", "D6",
"AUD3VOL", "D8",
"AUD3DAT", "DA",
NULL
};
2022-02-03 23:38:57 +00:00
const char** DivPlatformAmiga::getRegisterSheet() {
return regCheatSheetAmiga;
}
2022-03-27 04:39:20 +00:00
const char* DivPlatformAmiga::getEffectName(unsigned char effect) {
switch (effect) {
case 0x10:
return "10xx: Toggle filter (0 disables; 1 enables)";
break;
2022-03-27 05:02:17 +00:00
case 0x11:
return "11xx: Toggle AM with next channel";
break;
case 0x12:
return "12xx: Toggle period modulation with next channel";
break;
2022-03-27 04:39:20 +00:00
}
return NULL;
}
#define writeAudDat(x) \
chan[i].audDat=x; \
if (i<3 && chan[i].useV) { \
chan[i+1].outVol=(unsigned char)chan[i].audDat^0x80; \
if (chan[i+1].outVol>64) chan[i+1].outVol=64; \
} \
if (i<3 && chan[i].useP) { \
chan[i+1].freq=(unsigned char)chan[i].audDat^0x80; \
if (chan[i+1].freq<AMIGA_DIVIDER) chan[i+1].freq=AMIGA_DIVIDER; \
}
2022-01-15 20:50:53 +00:00
void DivPlatformAmiga::acquire(short* bufL, short* bufR, size_t start, size_t len) {
2022-03-27 04:39:20 +00:00
static int outL, outR;
2022-01-15 20:50:53 +00:00
for (size_t h=start; h<start+len; h++) {
2022-03-27 04:39:20 +00:00
outL=0;
outR=0;
2022-01-15 20:50:53 +00:00
for (int i=0; i<4; i++) {
if (chan[i].useWave || (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen)) {
2022-01-15 20:50:53 +00:00
chan[i].audSub-=AMIGA_DIVIDER;
2022-01-15 22:28:33 +00:00
if (chan[i].audSub<0) {
if (chan[i].useWave) {
writeAudDat(chan[i].ws.output[chan[i].audPos++]^0x80);
if (chan[i].audPos>=(unsigned int)(chan[i].audLen<<1)) {
chan[i].audPos=0;
2022-03-27 05:02:17 +00:00
}
} else {
DivSample* s=parent->getSample(chan[i].sample);
if (s->samples>0) {
writeAudDat(s->data8[chan[i].audPos++]);
if (chan[i].audPos>=s->samples || chan[i].audPos>=131071) {
if (s->loopStart>=0 && s->loopStart<(int)s->samples) {
chan[i].audPos=s->loopStart;
} else {
chan[i].sample=-1;
}
}
} else {
chan[i].sample=-1;
}
2022-01-15 20:50:53 +00:00
}
/*if (chan[i].freq<124) {
if (++chan[i].busClock>=512) {
unsigned int rAmount=(124-chan[i].freq)*2;
if (chan[i].audPos>=rAmount) {
chan[i].audPos-=rAmount;
}
chan[i].busClock=0;
}
}*/
if (bypassLimits) {
chan[i].audSub+=MAX(AMIGA_DIVIDER,chan[i].freq);
} else {
chan[i].audSub+=MAX(114,chan[i].freq);
}
2022-01-15 20:50:53 +00:00
}
}
2022-01-15 22:28:33 +00:00
if (!isMuted[i]) {
if (i==0 || i==3) {
2022-03-27 04:39:20 +00:00
outL+=((chan[i].audDat*chan[i].outVol)*sep1)>>7;
outR+=((chan[i].audDat*chan[i].outVol)*sep2)>>7;
2022-01-15 22:28:33 +00:00
} else {
2022-03-27 04:39:20 +00:00
outL+=((chan[i].audDat*chan[i].outVol)*sep2)>>7;
outR+=((chan[i].audDat*chan[i].outVol)*sep1)>>7;
2022-01-15 22:28:33 +00:00
}
}
2022-01-15 20:50:53 +00:00
}
2022-03-27 04:39:20 +00:00
filter[0][0]+=(filtConst*(outL-filter[0][0]))>>12;
filter[0][1]+=(filtConst*(filter[0][0]-filter[0][1]))>>12;
filter[1][0]+=(filtConst*(outR-filter[1][0]))>>12;
filter[1][1]+=(filtConst*(filter[1][0]-filter[1][1]))>>12;
bufL[h]=filter[0][1];
bufR[h]=filter[1][1];
2022-01-15 20:50:53 +00:00
}
}
void DivPlatformAmiga::tick() {
for (int i=0; i<4; i++) {
chan[i].std.next();
2022-04-10 05:01:55 +00:00
if (chan[i].std.vol.had) {
chan[i].outVol=((chan[i].vol%65)*MIN(64,chan[i].std.vol.val))>>6;
2022-01-15 20:50:53 +00:00
}
2022-02-04 08:29:40 +00:00
double off=1.0;
if (!chan[i].useWave && chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
2022-02-25 03:52:20 +00:00
DivSample* s=parent->getSample(chan[i].sample);
2022-02-04 08:29:40 +00:00
if (s->centerRate<1) {
off=1.0;
} else {
off=8363.0/(double)s->centerRate;
}
}
2022-04-10 05:01:55 +00:00
if (chan[i].std.arp.had) {
2022-01-15 20:50:53 +00:00
if (!chan[i].inPorta) {
2022-04-10 05:01:55 +00:00
if (chan[i].std.arp.mode) {
chan[i].baseFreq=round(off*NOTE_PERIODIC_NOROUND(chan[i].std.arp.val));
2022-01-15 20:50:53 +00:00
} else {
2022-04-10 05:01:55 +00:00
chan[i].baseFreq=round(off*NOTE_PERIODIC_NOROUND(chan[i].note+chan[i].std.arp.val));
2022-01-15 20:50:53 +00:00
}
}
chan[i].freqChanged=true;
} else {
2022-04-10 05:01:55 +00:00
if (chan[i].std.arp.mode && chan[i].std.arp.finished) {
chan[i].baseFreq=round(off*NOTE_PERIODIC_NOROUND(chan[i].note));
2022-01-15 20:50:53 +00:00
chan[i].freqChanged=true;
}
}
if (chan[i].useWave && chan[i].std.wave.had) {
if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) {
2022-04-10 05:01:55 +00:00
chan[i].wave=chan[i].std.wave.val;
chan[i].ws.changeWave1(chan[i].wave);
2022-01-15 20:50:53 +00:00
if (!chan[i].keyOff) chan[i].keyOn=true;
}
}
if (chan[i].useWave && chan[i].active) {
chan[i].ws.tick();
}
2022-01-15 20:50:53 +00:00
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
//DivInstrument* ins=parent->getIns(chan[i].ins);
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,true);
if (chan[i].freq>4095) chan[i].freq=4095;
if (chan[i].keyOn) {
}
if (chan[i].keyOff) {
}
if (chan[i].keyOn) chan[i].keyOn=false;
if (chan[i].keyOff) chan[i].keyOff=false;
chan[i].freqChanged=false;
}
}
}
int DivPlatformAmiga::dispatch(DivCommand c) {
switch (c.cmd) {
2022-01-15 22:28:33 +00:00
case DIV_CMD_NOTE_ON: {
DivInstrument* ins=parent->getIns(chan[c.chan].ins);
2022-02-04 08:29:40 +00:00
double off=1.0;
if (ins->amiga.useWave) {
chan[c.chan].useWave=true;
chan[c.chan].audLen=(ins->amiga.waveLen+1)>>1;
if (chan[c.chan].insChanged) {
if (chan[c.chan].wave<0) {
chan[c.chan].wave=0;
chan[c.chan].ws.setWidth(chan[c.chan].audLen<<1);
chan[c.chan].ws.changeWave1(chan[c.chan].wave);
}
}
} else {
chan[c.chan].sample=ins->amiga.initSample;
chan[c.chan].useWave=false;
if (chan[c.chan].sample>=0 && chan[c.chan].sample<parent->song.sampleLen) {
DivSample* s=parent->getSample(chan[c.chan].sample);
if (s->centerRate<1) {
off=1.0;
} else {
off=8363.0/(double)s->centerRate;
}
2022-02-04 08:29:40 +00:00
}
}
if (c.value!=DIV_NOTE_NULL) {
chan[c.chan].baseFreq=round(off*NOTE_PERIODIC_NOROUND(c.value));
}
if (chan[c.chan].useWave || chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) {
2022-01-15 22:28:33 +00:00
chan[c.chan].sample=-1;
}
2022-03-14 14:50:52 +00:00
if (chan[c.chan].setPos) {
chan[c.chan].setPos=false;
} else {
chan[c.chan].audPos=0;
}
2022-01-15 22:28:33 +00:00
chan[c.chan].audSub=0;
if (c.value!=DIV_NOTE_NULL) {
chan[c.chan].freqChanged=true;
chan[c.chan].note=c.value;
}
2022-01-15 20:50:53 +00:00
chan[c.chan].active=true;
chan[c.chan].keyOn=true;
2022-01-15 22:28:33 +00:00
chan[c.chan].std.init(ins);
if (chan[c.chan].useWave) {
chan[c.chan].ws.init(ins,chan[c.chan].audLen<<1,255,chan[c.chan].insChanged);
}
chan[c.chan].insChanged=false;
2022-01-15 20:50:53 +00:00
break;
2022-01-15 22:28:33 +00:00
}
2022-01-15 20:50:53 +00:00
case DIV_CMD_NOTE_OFF:
2022-01-15 22:28:33 +00:00
chan[c.chan].sample=-1;
2022-01-15 20:50:53 +00:00
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;
2022-01-15 20:50:53 +00:00
case DIV_CMD_INSTRUMENT:
if (chan[c.chan].ins!=c.value || c.value2==1) {
2022-01-15 20:50:53 +00:00
chan[c.chan].ins=c.value;
chan[c.chan].insChanged=true;
2022-01-15 20:50:53 +00:00
}
break;
case DIV_CMD_VOLUME:
if (chan[c.chan].vol!=c.value) {
chan[c.chan].vol=c.value;
2022-04-10 05:01:55 +00:00
if (!chan[c.chan].std.vol.has) {
2022-01-15 20:50:53 +00:00
chan[c.chan].outVol=c.value;
}
}
break;
case DIV_CMD_GET_VOLUME:
2022-04-10 05:01:55 +00:00
if (chan[c.chan].std.vol.has) {
2022-01-15 20:50:53 +00:00
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_WAVE:
if (!chan[c.chan].useWave) break;
2022-01-15 20:50:53 +00:00
chan[c.chan].wave=c.value;
chan[c.chan].keyOn=true;
chan[c.chan].ws.changeWave1(chan[c.chan].wave);
2022-01-15 20:50:53 +00:00
break;
case DIV_CMD_NOTE_PORTA: {
DivInstrument* ins=parent->getIns(chan[c.chan].ins);
chan[c.chan].sample=ins->amiga.initSample;
double off=1.0;
if (!chan[c.chan].useWave && chan[c.chan].sample>=0 && chan[c.chan].sample<parent->song.sampleLen) {
DivSample* s=parent->getSample(chan[c.chan].sample);
if (s->centerRate<1) {
off=1.0;
} else {
off=8363.0/(double)s->centerRate;
}
}
int destFreq=round(off*NOTE_PERIODIC_NOROUND(c.value2));
2022-01-15 20:50:53 +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;
}
2022-02-04 08:29:40 +00:00
case DIV_CMD_LEGATO: {
double off=1.0;
if (!chan[c.chan].useWave && chan[c.chan].sample>=0 && chan[c.chan].sample<parent->song.sampleLen) {
2022-02-25 03:52:20 +00:00
DivSample* s=parent->getSample(chan[c.chan].sample);
2022-02-04 08:29:40 +00:00
if (s->centerRate<1) {
off=1.0;
} else {
off=8363.0/(double)s->centerRate;
}
}
2022-04-10 05:01:55 +00:00
chan[c.chan].baseFreq=round(off*NOTE_PERIODIC_NOROUND(c.value+((chan[c.chan].std.arp.will && !chan[c.chan].std.arp.mode)?(chan[c.chan].std.arp.val-12):(0))));
2022-01-15 20:50:53 +00:00
chan[c.chan].freqChanged=true;
chan[c.chan].note=c.value;
break;
2022-02-04 08:29:40 +00:00
}
2022-01-15 20:50:53 +00:00
case DIV_CMD_PRE_PORTA:
2022-02-09 03:49:52 +00:00
if (chan[c.chan].active && c.value2) {
if (parent->song.resetMacroOnPorta) chan[c.chan].std.init(parent->getIns(chan[c.chan].ins));
}
2022-01-15 20:50:53 +00:00
chan[c.chan].inPorta=c.value;
break;
2022-03-14 14:50:52 +00:00
case DIV_CMD_SAMPLE_POS:
if (chan[c.chan].useWave) break;
2022-03-14 14:50:52 +00:00
chan[c.chan].audPos=c.value;
chan[c.chan].setPos=true;
break;
2022-03-27 04:39:20 +00:00
case DIV_CMD_AMIGA_FILTER:
filterOn=c.value;
filtConst=filterOn?filtConstOn:filtConstOff;
break;
2022-03-27 05:02:17 +00:00
case DIV_CMD_AMIGA_AM:
chan[c.chan].useV=c.value;
break;
case DIV_CMD_AMIGA_PM:
chan[c.chan].useP=c.value;
break;
2022-01-15 20:50:53 +00:00
case DIV_CMD_GET_VOLMAX:
2022-01-15 22:54:09 +00:00
return 64;
2022-01-15 20:50:53 +00:00
break;
case DIV_ALWAYS_SET_VOLUME:
return 1;
break;
default:
break;
}
return 1;
}
void DivPlatformAmiga::muteChannel(int ch, bool mute) {
isMuted[ch]=mute;
}
void DivPlatformAmiga::forceIns() {
for (int i=0; i<4; i++) {
chan[i].insChanged=true;
chan[i].freqChanged=true;
chan[i].audPos=131072;
chan[i].audDat=0;
chan[i].sample=-1;
2022-01-15 20:50:53 +00:00
}
}
2022-01-27 05:29:16 +00:00
void* DivPlatformAmiga::getChanState(int ch) {
return &chan[ch];
}
2022-01-15 20:50:53 +00:00
void DivPlatformAmiga::reset() {
for (int i=0; i<4; i++) {
chan[i]=DivPlatformAmiga::Channel();
chan[i].ws.setEngine(parent);
chan[i].ws.init(NULL,32,255);
2022-03-27 04:39:20 +00:00
filter[0][i]=0;
filter[1][i]=0;
2022-01-15 20:50:53 +00:00
}
2022-03-27 04:39:20 +00:00
filterOn=false;
filtConst=filterOn?filtConstOn:filtConstOff;
2022-01-15 20:50:53 +00:00
}
bool DivPlatformAmiga::isStereo() {
return true;
}
bool DivPlatformAmiga::keyOffAffectsArp(int ch) {
return true;
}
2022-01-18 04:59:52 +00:00
void DivPlatformAmiga::notifyInsChange(int ins) {
for (int i=0; i<4; i++) {
if (chan[i].ins==ins) {
chan[i].insChanged=true;
}
}
}
2022-01-18 05:25:10 +00:00
void DivPlatformAmiga::notifyWaveChange(int wave) {
for (int i=0; i<4; i++) {
if (chan[i].useWave && chan[i].wave==wave) {
chan[i].ws.changeWave1(wave);
}
}
2022-01-18 05:25:10 +00:00
}
2022-01-15 20:50:53 +00:00
void DivPlatformAmiga::notifyInsDeletion(void* ins) {
for (int i=0; i<4; i++) {
chan[i].std.notifyInsDeletion((DivInstrument*)ins);
}
}
2022-01-28 17:59:53 +00:00
void DivPlatformAmiga::setFlags(unsigned int flags) {
2022-01-29 05:20:27 +00:00
if (flags&1) {
chipClock=COLOR_PAL*4.0/5.0;
2022-01-15 20:50:53 +00:00
} else {
chipClock=COLOR_NTSC;
2022-01-15 20:50:53 +00:00
}
rate=chipClock/AMIGA_DIVIDER;
2022-02-04 22:59:55 +00:00
sep1=((flags>>8)&127)+127;
sep2=127-((flags>>8)&127);
amigaModel=flags&2;
bypassLimits=flags&4;
2022-03-27 04:39:20 +00:00
if (amigaModel) {
filtConstOff=4000;
filtConstOn=sin(M_PI*8000.0/(double)rate)*4096.0;
} else {
filtConstOff=sin(M_PI*16000.0/(double)rate)*4096.0;
filtConstOn=sin(M_PI*5500.0/(double)rate)*4096.0;
}
2022-01-15 20:50:53 +00:00
}
2022-01-28 17:59:53 +00:00
int DivPlatformAmiga::init(DivEngine* p, int channels, int sugRate, unsigned int flags) {
2022-01-15 20:50:53 +00:00
parent=p;
2022-01-17 04:21:27 +00:00
dumpWrites=false;
2022-01-15 20:50:53 +00:00
skipRegisterWrites=false;
for (int i=0; i<4; i++) {
isMuted[i]=false;
}
2022-01-28 17:59:53 +00:00
setFlags(flags);
2022-01-15 20:50:53 +00:00
reset();
return 6;
}
void DivPlatformAmiga::quit() {
}