| 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_OPN_H |
| 21 |
#define _FMSHARED_OPN_H |
| 22 |
|
| 23 |
#include "fmsharedbase.h" |
| 24 |
#include "../../../extern/opn/ym3438.h" |
| 25 |
#include "sound/ymfm/ymfm_opn.h" |
| 26 |
|
| 27 |
#define PLEASE_HELP_ME(_targetChan,blk) \ |
| 28 |
int boundaryBottom=parent->calcBaseFreq(chipClock,CHIP_FREQBASE,0,false); \ |
| 29 |
int boundaryTop=parent->calcBaseFreq(chipClock,CHIP_FREQBASE,12,false); \ |
| 30 |
int destFreq=NOTE_FNUM_BLOCK(c.value2,11,blk); \ |
| 31 |
int newFreq; \ |
| 32 |
bool return2=false; \ |
| 33 |
if (_targetChan.portaPause) { \ |
| 34 |
if (parent->song.oldOctaveBoundary) { \ |
| 35 |
if ((_targetChan.portaPauseFreq&0xf800)>(_targetChan.baseFreq&0xf800)) { \ |
| 36 |
_targetChan.baseFreq=((_targetChan.baseFreq&0x7ff)>>1)|(_targetChan.portaPauseFreq&0xf800); \ |
| 37 |
} else { \ |
| 38 |
_targetChan.baseFreq=((_targetChan.baseFreq&0x7ff)<<1)|(_targetChan.portaPauseFreq&0xf800); \ |
| 39 |
} \ |
| 40 |
c.value*=2; \ |
| 41 |
} else { \ |
| 42 |
_targetChan.baseFreq=_targetChan.portaPauseFreq; \ |
| 43 |
} \ |
| 44 |
} \ |
| 45 |
if (destFreq>_targetChan.baseFreq) { \ |
| 46 |
newFreq=_targetChan.baseFreq+c.value; \ |
| 47 |
if (newFreq>=destFreq) { \ |
| 48 |
newFreq=destFreq; \ |
| 49 |
return2=true; \ |
| 50 |
} \ |
| 51 |
} else { \ |
| 52 |
newFreq=_targetChan.baseFreq-c.value; \ |
| 53 |
if (newFreq<=destFreq) { \ |
| 54 |
newFreq=destFreq; \ |
| 55 |
return2=true; \ |
| 56 |
} \ |
| 57 |
} \ |
| 58 |
/* check for octave boundary */ \ |
| 59 |
/* what the heck! */ \ |
| 60 |
if (!_targetChan.portaPause) { \ |
| 61 |
if ((newFreq&0x7ff)>boundaryTop && (newFreq&0xf800)<0x3800) { \ |
| 62 |
if (parent->song.fbPortaPause) { \ |
| 63 |
_targetChan.portaPauseFreq=(boundaryBottom)|((newFreq+0x800)&0xf800); \ |
| 64 |
_targetChan.portaPause=true; \ |
| 65 |
break; \ |
| 66 |
} else { \ |
| 67 |
newFreq=((newFreq&0x7ff)>>1)|((newFreq+0x800)&0xf800); \ |
| 68 |
} \ |
| 69 |
} \ |
| 70 |
if ((newFreq&0x7ff)<boundaryBottom && (newFreq&0xf800)>0) { \ |
| 71 |
if (parent->song.fbPortaPause) { \ |
| 72 |
_targetChan.portaPauseFreq=newFreq=(boundaryTop-1)|((newFreq-0x800)&0xf800); \ |
| 73 |
_targetChan.portaPause=true; \ |
| 74 |
break; \ |
| 75 |
} else { \ |
| 76 |
newFreq=((newFreq&0x7ff)<<1)|((newFreq-0x800)&0xf800); \ |
| 77 |
} \ |
| 78 |
} \ |
| 79 |
} \ |
| 80 |
_targetChan.portaPause=false; \ |
| 81 |
_targetChan.freqChanged=true; \ |
| 82 |
_targetChan.baseFreq=newFreq; \ |
| 83 |
if (return2) { \ |
| 84 |
_targetChan.inPorta=false; \ |
| 85 |
return 2; \ |
| 86 |
} |
| 87 |
|
| 88 |
#define IS_EXTCH_MUTED (isOpMuted[0] && isOpMuted[1] && isOpMuted[2] && isOpMuted[3]) |
| 89 |
|
| 90 |
class DivOPNInterface: public ymfm::ymfm_interface { |
| 91 |
int setA, setB; |
| 92 |
int countA, countB; |
| 93 |
|
| 94 |
public: |
| 95 |
void clock(int cycles=144); |
| 96 |
void ymfm_set_timer(uint32_t tnum, int32_t duration_in_clocks); |
| 97 |
DivOPNInterface(): |
| 98 |
ymfm::ymfm_interface(), |
| 99 |
countA(0), |
| 100 |
countB(0) {} |
| 101 |
}; |
| 102 |
|
| 103 |
class DivPlatformOPN: public DivPlatformFMBase { |
| 104 |
protected: |
| 105 |
const unsigned short ADDR_MULT_DT=0x30; |
| 106 |
const unsigned short ADDR_TL=0x40; |
| 107 |
const unsigned short ADDR_RS_AR=0x50; |
| 108 |
const unsigned short ADDR_AM_DR=0x60; |
| 109 |
const unsigned short ADDR_DT2_D2R=0x70; |
| 110 |
const unsigned short ADDR_SL_RR=0x80; |
| 111 |
const unsigned short ADDR_SSG=0x90; |
| 112 |
const unsigned short ADDR_FREQ=0xa0; |
| 113 |
const unsigned short ADDR_FREQH=0xa4; |
| 114 |
const unsigned short ADDR_FB_ALG=0xb0; |
| 115 |
const unsigned short ADDR_LRAF=0xb4; |
| 116 |
|
| 117 |
const unsigned short opOffs[4]={ |
| 118 |
0x00, 0x04, 0x08, 0x0c |
| 119 |
}; |
| 120 |
|
| 121 |
struct OPNChannel: public FMChannel { |
| 122 |
unsigned char psgMode, autoEnvNum, autoEnvDen; |
| 123 |
bool furnacePCM; |
| 124 |
int sample, macroVolMul; |
| 125 |
|
| 126 |
OPNChannel(): |
| 127 |
FMChannel(), |
| 128 |
psgMode(1), |
| 129 |
autoEnvNum(0), |
| 130 |
autoEnvDen(0), |
| 131 |
furnacePCM(false), |
| 132 |
sample(-1), |
| 133 |
macroVolMul(255) {} |
| 134 |
}; |
| 135 |
|
| 136 |
struct OPNChannelStereo: public OPNChannel { |
| 137 |
unsigned char pan; |
| 138 |
OPNChannelStereo(): |
| 139 |
OPNChannel(), |
| 140 |
pan(3) {} |
| 141 |
}; |
| 142 |
|
| 143 |
struct OPNOpChannel: public SharedChannel<int> { |
| 144 |
unsigned char freqH, freqL; |
| 145 |
int portaPauseFreq; |
| 146 |
signed char konCycles; |
| 147 |
bool mask, hardReset; |
| 148 |
OPNOpChannel(): |
| 149 |
SharedChannel<int>(0), |
| 150 |
freqH(0), |
| 151 |
freqL(0), |
| 152 |
portaPauseFreq(0), |
| 153 |
konCycles(0), |
| 154 |
mask(true), |
| 155 |
hardReset(false) {} |
| 156 |
}; |
| 157 |
|
| 158 |
struct OPNOpChannelStereo: public OPNOpChannel { |
| 159 |
unsigned char pan; |
| 160 |
OPNOpChannelStereo(): |
| 161 |
OPNOpChannel(), |
| 162 |
pan(3) {} |
| 163 |
}; |
| 164 |
|
| 165 |
int extChanOffs, psgChanOffs, adpcmAChanOffs, adpcmBChanOffs, chanNum; // i really wanted to keep this constant... |
| 166 |
|
| 167 |
double fmFreqBase; |
| 168 |
unsigned int fmDivBase; |
| 169 |
unsigned int ayDiv; |
| 170 |
unsigned char csmChan; |
| 171 |
unsigned char lfoValue; |
| 172 |
unsigned char lastExtChPan; |
| 173 |
unsigned short ssgVol; |
| 174 |
unsigned short fmVol; |
| 175 |
bool extSys, fbAllOps; |
| 176 |
unsigned char useCombo; |
| 177 |
|
| 178 |
DivConfig ayFlags; |
| 179 |
|
| 180 |
friend void putDispatchChip(void*,int); |
| 181 |
friend void putDispatchChan(void*,int,int); |
| 182 |
DivPlatformOPN(int ext, int psg, int adpcmA, int adpcmB, int chanCount, double f=9440540.0, unsigned int d=72, unsigned int a=32, bool isExtSys=false, unsigned char cc=255): |
| 183 |
DivPlatformFMBase(), |
| 184 |
extChanOffs(ext), |
| 185 |
psgChanOffs(psg), |
| 186 |
adpcmAChanOffs(adpcmA), |
| 187 |
adpcmBChanOffs(adpcmB), |
| 188 |
chanNum(chanCount), |
| 189 |
fmFreqBase(f), |
| 190 |
fmDivBase(d), |
| 191 |
ayDiv(a), |
| 192 |
csmChan(cc), |
| 193 |
lfoValue(0), |
| 194 |
lastExtChPan(3), |
| 195 |
ssgVol(128), |
| 196 |
fmVol(256), |
| 197 |
extSys(isExtSys), |
| 198 |
fbAllOps(false), |
| 199 |
useCombo(0) {} |
| 200 |
virtual ~DivPlatformOPN() {} |
| 201 |
public: |
| 202 |
void setCombo(unsigned char combo) { |
| 203 |
useCombo=combo; |
| 204 |
} |
| 205 |
virtual int mapVelocity(int ch, float vel) { |
| 206 |
if (ch==csmChan) return vel*127.0; |
| 207 |
if (ch==adpcmBChanOffs) return vel*255.0; |
| 208 |
if (ch>=adpcmAChanOffs) { |
| 209 |
if (vel==0) return 0; |
| 210 |
if (vel>=1.0) return 31; |
| 211 |
return CLAMP(round(32.0-(56.0-log2(vel*127.0)*8.0)),0,31); |
| 212 |
} |
| 213 |
if (ch>=psgChanOffs) return round(15.0*pow(vel,0.33)); |
| 214 |
return DivPlatformFMBase::mapVelocity(ch,vel); |
| 215 |
} |
| 216 |
virtual float getGain(int ch, int vol) { |
| 217 |
if (vol==0) return 0; |
| 218 |
if (ch==csmChan) return 1; |
| 219 |
if (ch==adpcmBChanOffs) return (float)vol/255.0; |
| 220 |
if (ch>=adpcmAChanOffs) { |
| 221 |
return 1.0/pow(10.0,(float)(31-vol)*0.75/20.0); |
| 222 |
} |
| 223 |
if (ch>=psgChanOffs) { |
| 224 |
return 1.0/pow(10.0,(float)(15-vol)*1.5/20.0); |
| 225 |
} |
| 226 |
return DivPlatformFMBase::getGain(ch,vol); |
| 227 |
} |
| 228 |
|
| 229 |
}; |
| 230 |
|
| 231 |
#endif |