Genesis: separate channel state from instrument

paves way for FM macros
with this change, extended channel 3 mode breaks! do not use
until I diagnose the problem.
This commit is contained in:
tildearrow 2022-01-22 17:43:57 -05:00
parent c5956b939e
commit d62b111c78
6 changed files with 97 additions and 59 deletions

View file

@ -0,0 +1,9 @@
#define ADDR_MULT_DT 0x40
#define ADDR_TL 0x60
#define ADDR_RS_AR 0x80
#define ADDR_AM_DR 0xa0
#define ADDR_DT2_D2R 0xc0
#define ADDR_SL_RR 0xe0
#define ADDR_NOTE 0x28
#define ADDR_KF 0x30
#define ADDR_LR_FB_ALG 0x20

View file

@ -0,0 +1,16 @@
#ifndef _FMSHARED_OPN_H
#define _FMSHARED_OPN_H
#define ADDR_MULT_DT 0x30
#define ADDR_TL 0x40
#define ADDR_RS_AR 0x50
#define ADDR_AM_DR 0x60
#define ADDR_DT2_D2R 0x70
#define ADDR_SL_RR 0x80
#define ADDR_SSG 0x90
#define ADDR_FREQ 0xa0
#define ADDR_FREQH 0xa4
#define ADDR_FB_ALG 0xb0
#define ADDR_LRAF 0xb4
#endif

View file

@ -100,8 +100,8 @@ void DivPlatformGenesis::tick() {
if (chan[i].freqChanged) { if (chan[i].freqChanged) {
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch); chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch);
int freqt=toFreq(chan[i].freq); int freqt=toFreq(chan[i].freq);
immWrite(chanOffs[i]+0xa4,freqt>>8); immWrite(chanOffs[i]+ADDR_FREQH,freqt>>8);
immWrite(chanOffs[i]+0xa0,freqt&0xff); immWrite(chanOffs[i]+ADDR_FREQ,freqt&0xff);
if (chan[i].furnaceDac) { if (chan[i].furnaceDac) {
dacRate=(1280000*1.25)/chan[i].baseFreq; dacRate=(1280000*1.25)/chan[i].baseFreq;
} }
@ -163,8 +163,7 @@ void DivPlatformGenesis::muteChannel(int ch, bool mute) {
return; return;
} }
isMuted[ch]=mute; isMuted[ch]=mute;
DivInstrument* ins=parent->getIns(chan[ch].ins); rWrite(chanOffs[ch]+ADDR_LRAF,(isMuted[ch]?0:(chan[ch].pan<<6))|(chan[ch].state.fms&7)|((chan[ch].state.ams&3)<<4));
rWrite(chanOffs[ch]+0xb4,(isMuted[ch]?0:(chan[ch].pan<<6))|(ins->fm.fms&7)|((ins->fm.ams&3)<<4));
} }
int DivPlatformGenesis::dispatch(DivCommand c) { int DivPlatformGenesis::dispatch(DivCommand c) {
@ -211,30 +210,34 @@ int DivPlatformGenesis::dispatch(DivCommand c) {
break; break;
} }
if (chan[c.chan].insChanged) {
chan[c.chan].state=ins->fm;
}
for (int i=0; i<4; i++) { for (int i=0; i<4; i++) {
unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; unsigned short baseAddr=chanOffs[c.chan]|opOffs[i];
DivInstrumentFM::Operator op=ins->fm.op[i]; DivInstrumentFM::Operator& op=chan[c.chan].state.op[i];
if (isOutput[ins->fm.alg][i]) { if (isOutput[chan[c.chan].state.alg][i]) {
if (!chan[c.chan].active || chan[c.chan].insChanged) { if (!chan[c.chan].active || chan[c.chan].insChanged) {
rWrite(baseAddr+0x40,127-(((127-op.tl)*(chan[c.chan].vol&0x7f))/127)); rWrite(baseAddr+ADDR_TL,127-(((127-op.tl)*(chan[c.chan].vol&0x7f))/127));
} }
} else { } else {
if (chan[c.chan].insChanged) { if (chan[c.chan].insChanged) {
rWrite(baseAddr+0x40,op.tl); rWrite(baseAddr+ADDR_TL,op.tl);
} }
} }
if (chan[c.chan].insChanged) { if (chan[c.chan].insChanged) {
rWrite(baseAddr+0x30,(op.mult&15)|(dtTable[op.dt&7]<<4)); rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4));
rWrite(baseAddr+0x50,(op.ar&31)|(op.rs<<6)); rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6));
rWrite(baseAddr+0x60,(op.dr&31)|(op.am<<7)); rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7));
rWrite(baseAddr+0x70,op.d2r&31); rWrite(baseAddr+ADDR_DT2_D2R,op.d2r&31);
rWrite(baseAddr+0x80,(op.rr&15)|(op.sl<<4)); rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4));
rWrite(baseAddr+0x90,op.ssgEnv&15); rWrite(baseAddr+ADDR_SSG,op.ssgEnv&15);
} }
} }
if (chan[c.chan].insChanged) { if (chan[c.chan].insChanged) {
rWrite(chanOffs[c.chan]+0xb0,(ins->fm.alg&7)|(ins->fm.fb<<3)); rWrite(chanOffs[c.chan]+ADDR_FB_ALG,(chan[c.chan].state.alg&7)|(chan[c.chan].state.fb<<3));
rWrite(chanOffs[c.chan]+0xb4,(isMuted[c.chan]?0:(chan[c.chan].pan<<6))|(ins->fm.fms&7)|((ins->fm.ams&3)<<4)); 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; chan[c.chan].insChanged=false;
@ -255,14 +258,13 @@ int DivPlatformGenesis::dispatch(DivCommand c) {
break; break;
case DIV_CMD_VOLUME: { case DIV_CMD_VOLUME: {
chan[c.chan].vol=c.value; chan[c.chan].vol=c.value;
DivInstrument* ins=parent->getIns(chan[c.chan].ins);
for (int i=0; i<4; i++) { for (int i=0; i<4; i++) {
unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; unsigned short baseAddr=chanOffs[c.chan]|opOffs[i];
DivInstrumentFM::Operator op=ins->fm.op[i]; DivInstrumentFM::Operator& op=chan[c.chan].state.op[i];
if (isOutput[ins->fm.alg][i]) { if (isOutput[chan[c.chan].state.alg][i]) {
rWrite(baseAddr+0x40,127-(((127-op.tl)*(chan[c.chan].vol&0x7f))/127)); rWrite(baseAddr+ADDR_TL,127-(((127-op.tl)*(chan[c.chan].vol&0x7f))/127));
} else { } else {
rWrite(baseAddr+0x40,op.tl); rWrite(baseAddr+ADDR_TL,op.tl);
} }
} }
break; break;
@ -289,8 +291,7 @@ int DivPlatformGenesis::dispatch(DivCommand c) {
chan[c.chan].pan=3; chan[c.chan].pan=3;
break; break;
} }
DivInstrument* ins=parent->getIns(chan[c.chan].ins); 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));
rWrite(chanOffs[c.chan]+0xb4,(isMuted[c.chan]?0:(chan[c.chan].pan<<6))|(ins->fm.fms&7)|((ins->fm.ams&3)<<4));
break; break;
} }
case DIV_CMD_PITCH: { case DIV_CMD_PITCH: {
@ -349,33 +350,35 @@ int DivPlatformGenesis::dispatch(DivCommand c) {
} }
case DIV_CMD_FM_MULT: { case DIV_CMD_FM_MULT: {
unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]];
DivInstrument* ins=parent->getIns(chan[c.chan].ins); DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]];
DivInstrumentFM::Operator op=ins->fm.op[orderedOps[c.value]]; op.mult=c.value2&15;
rWrite(baseAddr+0x30,(c.value2&15)|(dtTable[op.dt&7]<<4)); rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4));
break; break;
} }
case DIV_CMD_FM_TL: { case DIV_CMD_FM_TL: {
unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]];
DivInstrument* ins=parent->getIns(chan[c.chan].ins); DivInstrumentFM::Operator& op=chan[c.chan].state.op[orderedOps[c.value]];
if (isOutput[ins->fm.alg][c.value]) { op.tl=c.value2;
rWrite(baseAddr+0x40,127-(((127-c.value2)*(chan[c.chan].vol&0x7f))/127)); if (isOutput[chan[c.chan].state.alg][c.value]) {
rWrite(baseAddr+ADDR_TL,127-(((127-op.tl)*(chan[c.chan].vol&0x7f))/127));
} else { } else {
rWrite(baseAddr+0x40,c.value2); rWrite(baseAddr+ADDR_TL,op.tl);
} }
break; break;
} }
case DIV_CMD_FM_AR: { case DIV_CMD_FM_AR: {
DivInstrument* ins=parent->getIns(chan[c.chan].ins);
if (c.value<0) { if (c.value<0) {
for (int i=0; i<4; i++) { for (int i=0; i<4; i++) {
DivInstrumentFM::Operator op=ins->fm.op[i]; DivInstrumentFM::Operator& op=chan[c.chan].state.op[i];
op.ar=c.value2&31;
unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; unsigned short baseAddr=chanOffs[c.chan]|opOffs[i];
rWrite(baseAddr+0x50,(c.value2&31)|(op.rs<<6)); rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6));
} }
} else { } else {
DivInstrumentFM::Operator op=ins->fm.op[orderedOps[c.value]]; 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]]; unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]];
rWrite(baseAddr+0x50,(c.value2&31)|(op.rs<<6)); rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6));
} }
break; break;

View file

@ -9,6 +9,7 @@
class DivPlatformGenesis: public DivDispatch { class DivPlatformGenesis: public DivDispatch {
protected: protected:
struct Channel { struct Channel {
DivInstrumentFM state;
unsigned char freqH, freqL; unsigned char freqH, freqL;
int freq, baseFreq, pitch; int freq, baseFreq, pitch;
unsigned char ins; unsigned char ins;

View file

@ -18,11 +18,19 @@ int DivPlatformGenesisExt::dispatch(DivCommand c) {
case DIV_CMD_NOTE_ON: { case DIV_CMD_NOTE_ON: {
DivInstrument* ins=parent->getIns(opChan[ch].ins); DivInstrument* ins=parent->getIns(opChan[ch].ins);
if (opChan[ch].insChanged) {
chan[2].state.alg=ins->fm.alg;
chan[2].state.fb=ins->fm.fb;
chan[2].state.fb=ins->fm.fms;
chan[2].state.ams=ins->fm.ams;
chan[2].state.op[ordch]=ins->fm.op[ordch];
}
unsigned short baseAddr=chanOffs[2]|opOffs[ordch]; unsigned short baseAddr=chanOffs[2]|opOffs[ordch];
DivInstrumentFM::Operator op=ins->fm.op[ordch]; DivInstrumentFM::Operator& op=chan[2].state.op[ordch];
if (isOpMuted[ch]) { if (isOpMuted[ch]) {
rWrite(baseAddr+0x40,127); rWrite(baseAddr+0x40,127);
} else if (isOutput[ins->fm.alg][ordch]) { } else if (isOutput[chan[2].state.alg][ordch]) {
if (!opChan[ch].active || opChan[ch].insChanged) { if (!opChan[ch].active || opChan[ch].insChanged) {
rWrite(baseAddr+0x40,127-(((127-op.tl)*(opChan[ch].vol&0x7f))/127)); rWrite(baseAddr+0x40,127-(((127-op.tl)*(opChan[ch].vol&0x7f))/127));
} }
@ -40,8 +48,8 @@ int DivPlatformGenesisExt::dispatch(DivCommand c) {
rWrite(baseAddr+0x90,op.ssgEnv&15); rWrite(baseAddr+0x90,op.ssgEnv&15);
} }
if (opChan[ch].insChanged) { // TODO how does this work? if (opChan[ch].insChanged) { // TODO how does this work?
rWrite(chanOffs[2]+0xb0,(ins->fm.alg&7)|(ins->fm.fb<<3)); rWrite(chanOffs[2]+0xb0,(chan[2].state.alg&7)|(chan[2].state.fb<<3));
rWrite(chanOffs[2]+0xb4,(opChan[ch].pan<<6)|(ins->fm.fms&7)|((ins->fm.ams&3)<<4)); rWrite(chanOffs[2]+0xb4,(opChan[ch].pan<<6)|(chan[2].state.fms&7)|((chan[2].state.ams&3)<<4));
} }
opChan[ch].insChanged=false; opChan[ch].insChanged=false;
@ -59,9 +67,8 @@ int DivPlatformGenesisExt::dispatch(DivCommand c) {
break; break;
case DIV_CMD_VOLUME: { case DIV_CMD_VOLUME: {
opChan[ch].vol=c.value; opChan[ch].vol=c.value;
DivInstrument* ins=parent->getIns(opChan[ch].ins);
unsigned short baseAddr=chanOffs[2]|opOffs[ordch]; unsigned short baseAddr=chanOffs[2]|opOffs[ordch];
DivInstrumentFM::Operator op=ins->fm.op[ordch]; DivInstrumentFM::Operator& op=chan[2].state.op[ordch];
if (isOpMuted[ch]) { if (isOpMuted[ch]) {
rWrite(baseAddr+0x40,127); rWrite(baseAddr+0x40,127);
} else { } else {
@ -91,9 +98,8 @@ int DivPlatformGenesisExt::dispatch(DivCommand c) {
opChan[ch].pan=3; opChan[ch].pan=3;
break; break;
} }
DivInstrument* ins=parent->getIns(opChan[ch].ins);
// TODO: ??? // TODO: ???
rWrite(chanOffs[2]+0xb4,(opChan[ch].pan<<6)|(ins->fm.fms&7)|((ins->fm.ams&3)<<4)); rWrite(chanOffs[2]+0xb4,(opChan[ch].pan<<6)|(chan[2].state.fms&7)|((chan[2].state.ams&3)<<4));
break; break;
} }
case DIV_CMD_PITCH: { case DIV_CMD_PITCH: {
@ -141,33 +147,35 @@ int DivPlatformGenesisExt::dispatch(DivCommand c) {
} }
case DIV_CMD_FM_MULT: { // TODO case DIV_CMD_FM_MULT: { // TODO
unsigned short baseAddr=chanOffs[2]|opOffs[orderedOps[c.value]]; unsigned short baseAddr=chanOffs[2]|opOffs[orderedOps[c.value]];
DivInstrument* ins=parent->getIns(opChan[ch].ins); DivInstrumentFM::Operator& op=chan[2].state.op[orderedOps[c.value]];
DivInstrumentFM::Operator op=ins->fm.op[orderedOps[c.value]]; op.mult=c.value2&15;
rWrite(baseAddr+0x30,(c.value2&15)|(dtTable[op.dt&7]<<4)); rWrite(baseAddr+0x30,(op.mult&15)|(dtTable[op.dt&7]<<4));
break; break;
} }
case DIV_CMD_FM_TL: { // TODO case DIV_CMD_FM_TL: { // TODO
unsigned short baseAddr=chanOffs[2]|opOffs[orderedOps[c.value]]; unsigned short baseAddr=chanOffs[2]|opOffs[orderedOps[c.value]];
DivInstrument* ins=parent->getIns(opChan[ch].ins); DivInstrumentFM::Operator& op=chan[2].state.op[orderedOps[c.value]];
if (isOutput[ins->fm.alg][c.value]) { op.tl=c.value2;
rWrite(baseAddr+0x40,127-(((127-c.value2)*(opChan[ch].vol&0x7f))/127)); if (isOutput[chan[2].state.alg][c.value]) {
rWrite(baseAddr+0x40,127-(((127-op.tl)*(opChan[ch].vol&0x7f))/127));
} else { } else {
rWrite(baseAddr+0x40,c.value2); rWrite(baseAddr+0x40,op.tl);
} }
break; break;
} }
case DIV_CMD_FM_AR: { case DIV_CMD_FM_AR: {
DivInstrument* ins=parent->getIns(opChan[ch].ins);
if (c.value<0) { if (c.value<0) {
for (int i=0; i<4; i++) { for (int i=0; i<4; i++) {
DivInstrumentFM::Operator op=ins->fm.op[i]; DivInstrumentFM::Operator& op=chan[2].state.op[i];
op.ar=c.value2&31;
unsigned short baseAddr=chanOffs[2]|opOffs[i]; unsigned short baseAddr=chanOffs[2]|opOffs[i];
rWrite(baseAddr+0x50,(c.value2&31)|(op.rs<<6)); rWrite(baseAddr+0x50,(op.ar&31)|(op.rs<<6));
} }
} else { } else {
DivInstrumentFM::Operator op=ins->fm.op[orderedOps[c.value]]; DivInstrumentFM::Operator& op=chan[2].state.op[orderedOps[c.value]];
op.ar=c.value2&31;
unsigned short baseAddr=chanOffs[2]|opOffs[orderedOps[c.value]]; unsigned short baseAddr=chanOffs[2]|opOffs[orderedOps[c.value]];
rWrite(baseAddr+0x50,(c.value2&31)|(op.rs<<6)); rWrite(baseAddr+0x50,(op.ar&31)|(op.rs<<6));
} }
break; break;
} }
@ -198,12 +206,11 @@ void DivPlatformGenesisExt::muteChannel(int ch, bool mute) {
isOpMuted[ch-2]=mute; isOpMuted[ch-2]=mute;
int ordch=orderedOps[ch-2]; int ordch=orderedOps[ch-2];
DivInstrument* ins=parent->getIns(opChan[ch].ins);
unsigned short baseAddr=chanOffs[2]|opOffs[ordch]; unsigned short baseAddr=chanOffs[2]|opOffs[ordch];
DivInstrumentFM::Operator op=ins->fm.op[ordch]; DivInstrumentFM::Operator op=chan[2].state.op[ordch];
if (isOpMuted[ch]) { if (isOpMuted[ch]) {
rWrite(baseAddr+0x40,127); rWrite(baseAddr+0x40,127);
} else if (isOutput[ins->fm.alg][ordch]) { } else if (isOutput[chan[2].state.alg][ordch]) {
rWrite(baseAddr+0x40,127-(((127-op.tl)*(opChan[ch].vol&0x7f))/127)); rWrite(baseAddr+0x40,127-(((127-op.tl)*(opChan[ch].vol&0x7f))/127));
} else { } else {
rWrite(baseAddr+0x40,op.tl); rWrite(baseAddr+0x40,op.tl);

View file

@ -25,3 +25,5 @@ static int orderedOps[4]={
#define rWrite(a,v) if (!skipRegisterWrites) {pendingWrites[a]=v;} #define rWrite(a,v) if (!skipRegisterWrites) {pendingWrites[a]=v;}
#define immWrite(a,v) if (!skipRegisterWrites) {writes.emplace(a,v); if (dumpWrites) {addWrite(a,v);} } #define immWrite(a,v) if (!skipRegisterWrites) {writes.emplace(a,v); if (dumpWrites) {addWrite(a,v);} }
#include "fmshared_OPN.h"