2021-05-12 22:19:18 +00:00
|
|
|
#include "genesis.h"
|
|
|
|
#include "../engine.h"
|
|
|
|
#include <string.h>
|
2021-05-14 08:23:40 +00:00
|
|
|
#include <math.h>
|
2021-05-12 22:19:18 +00:00
|
|
|
|
|
|
|
// TODO fix all the writes.
|
|
|
|
// i think there is no wait for data writes, just for ON/OFF writes
|
|
|
|
void DivPlatformGenesis::acquire(short& l, short& r) {
|
|
|
|
short o[2];
|
2021-05-13 07:39:26 +00:00
|
|
|
|
|
|
|
if (dacMode && dacSample!=-1) {
|
|
|
|
if (--dacPeriod<1) {
|
|
|
|
DivSample* s=parent->song.sample[dacSample];
|
|
|
|
writes.emplace(0x2a,((unsigned short)s->rendData[dacPos++]+0x8000)>>8);
|
|
|
|
if (dacPos>s->rendLength) {
|
|
|
|
dacSample=-1;
|
|
|
|
}
|
|
|
|
dacPeriod=dacRate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 22:19:18 +00:00
|
|
|
if (!writes.empty() && --delay<0) {
|
2021-05-13 07:39:26 +00:00
|
|
|
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;
|
|
|
|
writes.pop();
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 22:19:18 +00:00
|
|
|
}
|
|
|
|
OPN2_Clock(&fm,o);
|
|
|
|
//OPN2_Write(&fm,0,0);
|
|
|
|
l=o[0]<<7;
|
|
|
|
r=o[1]<<7;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned short chanOffs[6]={
|
|
|
|
0x00, 0x01, 0x02, 0x100, 0x101, 0x102
|
|
|
|
};
|
|
|
|
static unsigned short opOffs[4]={
|
|
|
|
0x00, 0x04, 0x08, 0x0c
|
|
|
|
};
|
|
|
|
static unsigned char konOffs[6]={
|
|
|
|
0, 1, 2, 4, 5, 6
|
|
|
|
};
|
2021-05-13 07:39:26 +00:00
|
|
|
static bool isOutput[8][4]={
|
|
|
|
// 1 3 2 4
|
|
|
|
{false,false,false,true},
|
|
|
|
{false,false,false,true},
|
|
|
|
{false,false,false,true},
|
|
|
|
{false,false,false,true},
|
|
|
|
{false,false,true ,true},
|
|
|
|
{false,true ,true ,true},
|
|
|
|
{false,true ,true ,true},
|
|
|
|
{true ,true ,true ,true},
|
|
|
|
};
|
|
|
|
static unsigned char dtTable[8]={
|
|
|
|
7,6,5,0,1,2,3,0
|
|
|
|
};
|
|
|
|
static int dacRates[6]={
|
|
|
|
160,160,116,80,58,40
|
|
|
|
};
|
2021-05-12 22:19:18 +00:00
|
|
|
|
2021-05-13 08:09:56 +00:00
|
|
|
void DivPlatformGenesis::tick() {
|
|
|
|
for (int i=0; i<6; i++) {
|
2021-05-13 08:18:54 +00:00
|
|
|
if (chan[i].keyOn || chan[i].keyOff) {
|
2021-05-13 08:09:56 +00:00
|
|
|
writes.emplace(0x28,0x00|konOffs[i]);
|
2021-05-13 08:18:54 +00:00
|
|
|
chan[i].keyOff=false;
|
2021-05-13 08:09:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i<512; i++) {
|
|
|
|
if (pendingWrites[i]!=-1) {
|
|
|
|
writes.emplace(i,pendingWrites[i]&0xff);
|
|
|
|
pendingWrites[i]=-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i<6; i++) {
|
2021-05-14 08:23:40 +00:00
|
|
|
if (chan[i].freqChanged) {
|
|
|
|
if (chan[i].freq>=131072) {
|
|
|
|
chan[i].freqH=((chan[i].freq>>15)&7)|0x38;
|
|
|
|
chan[i].freqL=(chan[i].freq>>7)&0xff;
|
|
|
|
} else if (chan[i].freq>=65536) {
|
|
|
|
chan[i].freqH=((chan[i].freq>>14)&7)|0x30;
|
|
|
|
chan[i].freqL=(chan[i].freq>>6)&0xff;
|
|
|
|
} else if (chan[i].freq>=32768) {
|
|
|
|
chan[i].freqH=((chan[i].freq>>13)&7)|0x28;
|
|
|
|
chan[i].freqL=(chan[i].freq>>5)&0xff;
|
|
|
|
} else if (chan[i].freq>=16384) {
|
|
|
|
chan[i].freqH=((chan[i].freq>>12)&7)|0x20;
|
|
|
|
chan[i].freqL=(chan[i].freq>>4)&0xff;
|
|
|
|
} else if (chan[i].freq>=8192) {
|
|
|
|
chan[i].freqH=((chan[i].freq>>11)&7)|0x18;
|
|
|
|
chan[i].freqL=(chan[i].freq>>3)&0xff;
|
|
|
|
} else if (chan[i].freq>=4096) {
|
|
|
|
chan[i].freqH=((chan[i].freq>>10)&7)|0x10;
|
|
|
|
chan[i].freqL=(chan[i].freq>>2)&0xff;
|
|
|
|
} else if (chan[i].freq>=2048) {
|
|
|
|
chan[i].freqH=((chan[i].freq>>9)&7)|0x08;
|
|
|
|
chan[i].freqL=(chan[i].freq>>1)&0xff;
|
|
|
|
} else {
|
|
|
|
chan[i].freqH=(chan[i].freq>>8)&7;
|
|
|
|
chan[i].freqL=chan[i].freq&0xff;
|
|
|
|
}
|
2021-05-13 08:09:56 +00:00
|
|
|
writes.emplace(chanOffs[i]+0xa4,chan[i].freqH);
|
|
|
|
writes.emplace(chanOffs[i]+0xa0,chan[i].freqL);
|
2021-05-14 08:23:40 +00:00
|
|
|
}
|
|
|
|
if (chan[i].keyOn) {
|
2021-05-13 08:09:56 +00:00
|
|
|
writes.emplace(0x28,0xf0|konOffs[i]);
|
|
|
|
chan[i].keyOn=false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define rWrite(a,v) pendingWrites[a]=v;
|
|
|
|
|
2021-05-12 22:19:18 +00:00
|
|
|
int DivPlatformGenesis::dispatch(DivCommand c) {
|
2021-05-14 08:23:40 +00:00
|
|
|
if (c.chan>5) return 0;
|
2021-05-12 22:19:18 +00:00
|
|
|
switch (c.cmd) {
|
|
|
|
case DIV_CMD_NOTE_ON: {
|
2021-05-13 07:39:26 +00:00
|
|
|
if (c.chan==5 && dacMode) {
|
|
|
|
dacSample=c.value%12;
|
|
|
|
if (dacSample>=parent->song.sampleLen) {
|
|
|
|
dacSample=-1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dacPos=0;
|
|
|
|
dacPeriod=0;
|
|
|
|
dacRate=dacRates[parent->song.sample[dacSample]->rate];
|
|
|
|
break;
|
|
|
|
}
|
2021-05-12 22:19:18 +00:00
|
|
|
DivInstrument* ins=parent->song.ins[chan[c.chan].ins];
|
2021-05-13 08:09:56 +00:00
|
|
|
|
2021-05-13 07:39:26 +00:00
|
|
|
if (chan[c.chan].insChanged) {
|
|
|
|
chan[c.chan].insChanged=false;
|
|
|
|
for (int i=0; i<4; i++) {
|
|
|
|
unsigned short baseAddr=chanOffs[c.chan]|opOffs[i];
|
|
|
|
DivInstrumentFM::Operator op=ins->fm.op[i];
|
2021-05-13 08:09:56 +00:00
|
|
|
rWrite(baseAddr+0x30,(op.mult&15)|(dtTable[op.dt&7]<<4));
|
2021-05-13 07:39:26 +00:00
|
|
|
if (isOutput[ins->fm.alg][i]) {
|
2021-05-13 08:09:56 +00:00
|
|
|
rWrite(baseAddr+0x40,127-(((127-op.tl)*chan[c.chan].vol)/127));
|
2021-05-13 07:39:26 +00:00
|
|
|
} else {
|
2021-05-13 08:09:56 +00:00
|
|
|
rWrite(baseAddr+0x40,op.tl);
|
2021-05-13 07:39:26 +00:00
|
|
|
}
|
2021-05-13 08:09:56 +00:00
|
|
|
rWrite(baseAddr+0x50,(op.ar&31)|(op.rs<<6));
|
|
|
|
rWrite(baseAddr+0x60,(op.dr&31)|(op.am<<7));
|
|
|
|
rWrite(baseAddr+0x70,op.d2r&31);
|
|
|
|
rWrite(baseAddr+0x80,(op.rr&15)|(op.sl<<4));
|
|
|
|
rWrite(baseAddr+0x90,op.ssgEnv&15);
|
2021-05-13 07:39:26 +00:00
|
|
|
}
|
2021-05-13 08:09:56 +00:00
|
|
|
rWrite(chanOffs[c.chan]+0xb0,(ins->fm.alg&7)|(ins->fm.fb<<3));
|
|
|
|
rWrite(chanOffs[c.chan]+0xb4,(chan[c.chan].pan<<6)|(ins->fm.fms&7)|((ins->fm.ams&3)<<4));
|
2021-05-12 22:19:18 +00:00
|
|
|
}
|
2021-05-14 19:16:48 +00:00
|
|
|
chan[c.chan].baseFreq=644.0f*pow(2.0f,((float)c.value/12.0f));
|
|
|
|
chan[c.chan].freq=(chan[c.chan].baseFreq*(2048+chan[c.chan].pitch))>>11;
|
2021-05-14 08:23:40 +00:00
|
|
|
chan[c.chan].freqChanged=true;
|
2021-05-13 08:09:56 +00:00
|
|
|
chan[c.chan].keyOn=true;
|
2021-05-12 22:19:18 +00:00
|
|
|
chan[c.chan].active=true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIV_CMD_NOTE_OFF:
|
2021-05-13 08:18:54 +00:00
|
|
|
chan[c.chan].keyOff=true;
|
2021-05-12 22:19:18 +00:00
|
|
|
chan[c.chan].active=false;
|
|
|
|
break;
|
2021-05-13 07:39:26 +00:00
|
|
|
case DIV_CMD_VOLUME: {
|
2021-05-12 22:19:18 +00:00
|
|
|
chan[c.chan].vol=c.value;
|
2021-05-13 07:39:26 +00:00
|
|
|
DivInstrument* ins=parent->song.ins[chan[c.chan].ins];
|
|
|
|
for (int i=0; i<4; i++) {
|
|
|
|
unsigned short baseAddr=chanOffs[c.chan]|opOffs[i];
|
|
|
|
DivInstrumentFM::Operator op=ins->fm.op[i];
|
|
|
|
if (isOutput[ins->fm.alg][i]) {
|
2021-05-13 08:09:56 +00:00
|
|
|
rWrite(baseAddr+0x40,127-(((127-op.tl)*chan[c.chan].vol)/127));
|
2021-05-13 07:39:26 +00:00
|
|
|
} else {
|
2021-05-13 08:09:56 +00:00
|
|
|
rWrite(baseAddr+0x40,op.tl);
|
2021-05-13 07:39:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-12 22:19:18 +00:00
|
|
|
break;
|
2021-05-13 07:39:26 +00:00
|
|
|
}
|
2021-05-12 22:19:18 +00:00
|
|
|
case DIV_CMD_INSTRUMENT:
|
2021-05-13 07:39:26 +00:00
|
|
|
if (chan[c.chan].ins!=c.value) {
|
|
|
|
chan[c.chan].insChanged=true;
|
|
|
|
}
|
2021-05-12 22:19:18 +00:00
|
|
|
chan[c.chan].ins=c.value;
|
|
|
|
break;
|
2021-05-13 07:39:26 +00:00
|
|
|
case DIV_CMD_PANNING: {
|
|
|
|
switch (c.value) {
|
|
|
|
case 0x01:
|
|
|
|
chan[c.chan].pan=1;
|
|
|
|
break;
|
|
|
|
case 0x10:
|
|
|
|
chan[c.chan].pan=2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
chan[c.chan].pan=3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DivInstrument* ins=parent->song.ins[chan[c.chan].ins];
|
2021-05-13 08:09:56 +00:00
|
|
|
rWrite(chanOffs[c.chan]+0xb4,(chan[c.chan].pan<<6)|(ins->fm.fms&7)|((ins->fm.ams&3)<<4));
|
2021-05-13 07:39:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-05-14 19:16:48 +00:00
|
|
|
case DIV_CMD_PITCH: {
|
|
|
|
chan[c.chan].pitch=c.value;
|
|
|
|
chan[c.chan].freq=(chan[c.chan].baseFreq*(2048+chan[c.chan].pitch))>>11;
|
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIV_CMD_NOTE_PORTA: {
|
|
|
|
int destFreq=644.0f*pow(2.0f,((float)c.value2/12.0f));
|
|
|
|
bool return2=false;
|
|
|
|
if (destFreq>chan[c.chan].baseFreq) {
|
|
|
|
chan[c.chan].baseFreq=(chan[c.chan].baseFreq*(960+c.value))/960;
|
|
|
|
if (chan[c.chan].baseFreq>=destFreq) {
|
|
|
|
chan[c.chan].baseFreq=destFreq;
|
|
|
|
return2=true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chan[c.chan].baseFreq=(chan[c.chan].baseFreq*(960-c.value))/960;
|
|
|
|
if (chan[c.chan].baseFreq<=destFreq) {
|
|
|
|
chan[c.chan].baseFreq=destFreq;
|
|
|
|
return2=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chan[c.chan].freq=(chan[c.chan].baseFreq*(2048+chan[c.chan].pitch))>>11;
|
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
if (return2) return 2;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-13 07:39:26 +00:00
|
|
|
case DIV_CMD_SAMPLE_MODE: {
|
|
|
|
if (c.chan==5) {
|
|
|
|
dacMode=c.value;
|
2021-05-13 08:09:56 +00:00
|
|
|
rWrite(0x2b,c.value<<7);
|
2021-05-13 07:39:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-05-12 22:19:18 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DivPlatformGenesis::init(DivEngine* p, int channels, int sugRate) {
|
|
|
|
parent=p;
|
|
|
|
rate=1278409;
|
|
|
|
OPN2_Reset(&fm);
|
2021-05-13 07:39:26 +00:00
|
|
|
for (int i=0; i<10; i++) {
|
|
|
|
chan[i].vol=0x7f;
|
|
|
|
}
|
|
|
|
|
2021-05-13 08:09:56 +00:00
|
|
|
for (int i=0; i<512; i++) {
|
|
|
|
pendingWrites[i]=-1;
|
|
|
|
}
|
|
|
|
|
2021-05-13 07:39:26 +00:00
|
|
|
lastBusy=60;
|
|
|
|
dacMode=0;
|
|
|
|
dacPeriod=0;
|
|
|
|
dacPos=0;
|
|
|
|
dacRate=0;
|
|
|
|
dacSample=-1;
|
|
|
|
|
|
|
|
// LFO
|
|
|
|
writes.emplace(0x22,0x08);
|
2021-05-12 22:19:18 +00:00
|
|
|
|
2021-05-13 07:39:26 +00:00
|
|
|
delay=0;
|
2021-05-12 22:19:18 +00:00
|
|
|
return 10;
|
|
|
|
}
|