| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef _FMSHARED_BASE_H |
| 21 |
#define _FMSHARED_BASE_H |
| 22 |
|
| 23 |
#include "../dispatch.h" |
| 24 |
#include "../instrument.h" |
| 25 |
#include "../../fixedQueue.h" |
| 26 |
|
| 27 |
#define KVS(x,y) ((chan[x].state.op[y].kvs==2 && isOutput[chan[x].state.alg][y]) || chan[x].state.op[y].kvs==1) |
| 28 |
|
| 29 |
class DivPlatformFMBase: public DivDispatch { |
| 30 |
protected: |
| 31 |
const bool isOutput[8][4]={ |
| 32 |
// 1 3 2 4 |
| 33 |
{false,false,false,true}, |
| 34 |
{false,false,false,true}, |
| 35 |
{false,false,false,true}, |
| 36 |
{false,false,false,true}, |
| 37 |
{false,false,true ,true}, |
| 38 |
{false,true ,true ,true}, |
| 39 |
{false,true ,true ,true}, |
| 40 |
{true ,true ,true ,true}, |
| 41 |
}; |
| 42 |
const unsigned char dtTable[8]={ |
| 43 |
7,6,5,0,1,2,3,4 |
| 44 |
}; |
| 45 |
|
| 46 |
const int orderedOps[4]={ |
| 47 |
0,2,1,3 |
| 48 |
}; |
| 49 |
|
| 50 |
const unsigned int hardResetCycles=127; |
| 51 |
|
| 52 |
struct FMChannel: public SharedChannel<int> { |
| 53 |
DivInstrumentFM state; |
| 54 |
unsigned char freqH, freqL; |
| 55 |
int portaPauseFreq; |
| 56 |
unsigned char opMask; |
| 57 |
signed char konCycles; |
| 58 |
bool hardReset, opMaskChanged; |
| 59 |
|
| 60 |
FMChannel(): |
| 61 |
SharedChannel<int>(0), |
| 62 |
freqH(0), |
| 63 |
freqL(0), |
| 64 |
portaPauseFreq(0), |
| 65 |
opMask(15), |
| 66 |
konCycles(0), |
| 67 |
hardReset(false), |
| 68 |
opMaskChanged(false) {} |
| 69 |
}; |
| 70 |
|
| 71 |
struct FMChannelStereo: public FMChannel { |
| 72 |
unsigned char pan; |
| 73 |
FMChannelStereo(): |
| 74 |
FMChannel(), |
| 75 |
pan(3) {} |
| 76 |
}; |
| 77 |
|
| 78 |
struct QueuedWrite { |
| 79 |
unsigned int addr; |
| 80 |
unsigned short val; |
| 81 |
bool addrOrVal; |
| 82 |
bool urgent; |
| 83 |
QueuedWrite(): addr(0), val(0), addrOrVal(false), urgent(false) {} |
| 84 |
QueuedWrite(unsigned int a, unsigned char v): addr(a), val(v), addrOrVal(false), urgent(false) {} |
| 85 |
QueuedWrite(unsigned int a, unsigned char v, bool u): addr(a), val(v), addrOrVal(false), urgent(u) {} |
| 86 |
}; |
| 87 |
FixedQueue<QueuedWrite,2048> writes; |
| 88 |
|
| 89 |
unsigned char lastBusy; |
| 90 |
int delay; |
| 91 |
bool flushFirst; |
| 92 |
|
| 93 |
unsigned char regPool[512]; |
| 94 |
short oldWrites[512]; |
| 95 |
short pendingWrites[512]; |
| 96 |
|
| 97 |
inline void rWrite(unsigned short a, short v) { |
| 98 |
if (!skipRegisterWrites) { |
| 99 |
pendingWrites[a]=v; |
| 100 |
} |
| 101 |
} |
| 102 |
inline void immWrite(unsigned int a, unsigned short v) { |
| 103 |
if (!skipRegisterWrites) { |
| 104 |
writes.push_back(QueuedWrite(a,v)); |
| 105 |
if (dumpWrites) { |
| 106 |
addWrite(a,v); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
// only used by OPN2 for DAC writes |
| 111 |
inline void urgentWrite(unsigned short a, unsigned char v) { |
| 112 |
if (!skipRegisterWrites && !flushFirst) { |
| 113 |
writes.push_front(QueuedWrite(a,v,true)); |
| 114 |
if (dumpWrites) { |
| 115 |
addWrite(a,v); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
virtual int mapVelocity(int ch, float vel) { |
| 121 |
// -0.75dB per step |
| 122 |
// -6: 64: 8 |
| 123 |
// -12: 32: 16 |
| 124 |
// -18: 16: 24 |
| 125 |
// -24: 8: 32 |
| 126 |
// -30: 4: 40 |
| 127 |
// -36: 2: 48 |
| 128 |
// -42: 1: 56 |
| 129 |
if (vel==0) return 0; |
| 130 |
if (vel>=1.0) return 127; |
| 131 |
return CLAMP(round(128.0-(56.0-log2(vel*127.0)*8.0)),0,127); |
| 132 |
} |
| 133 |
|
| 134 |
virtual float getGain(int ch, int vol) { |
| 135 |
if (vol==0) return 0; |
| 136 |
return 1.0/pow(10.0,(float)(127-vol)*0.75/20.0); |
| 137 |
} |
| 138 |
|
| 139 |
bool getLegacyAlwaysSetVolume() { |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
friend void putDispatchChan(void*,int,int); |
| 144 |
|
| 145 |
DivPlatformFMBase(): |
| 146 |
DivDispatch(), |
| 147 |
lastBusy(0), |
| 148 |
delay(0), |
| 149 |
flushFirst(false) {} |
| 150 |
virtual ~DivPlatformFMBase() {} |
| 151 |
}; |
| 152 |
|
| 153 |
#endif |