Debian Codegraph @

src/engine/platform/opl.h

raw
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 _OPL_H
21 #define _OPL_H
22
23 #include "../dispatch.h"
24 #include "../../fixedQueue.h"
25 #include "../../../extern/opl/opl3.h"
26 extern "C" {
27 #include "../../../extern/YM3812-LLE/fmopl2.h"
28 #include "../../../extern/YMF262-LLE/fmopl3.h"
29 }
30 #include "sound/ymfm/ymfm_adpcm.h"
31 #include "sound/ymfm/ymfm_opl.h"
32 #include "sound/ymfm/ymfm_pcm.h"
33 #include "sound/ymf278b/ymf278.h"
34
35 class DivOPLAInterface: public ymfm::ymfm_interface {
36 public:
37 unsigned char* adpcmBMem;
38 unsigned char* pcmMem;
39 int sampleBank;
40 uint8_t ymfm_external_read(ymfm::access_class type, uint32_t address);
41 void ymfm_external_write(ymfm::access_class type, uint32_t address, uint8_t data);
42 DivOPLAInterface(): adpcmBMem(NULL), pcmMem(NULL), sampleBank(0) {}
43 };
44
45 class DivYMF278MemoryInterface: public MemoryInterface {
46 public:
47 unsigned char* memory;
48 DivYMF278MemoryInterface(unsigned size_) : memory(NULL), size(size_) {};
49 byte operator[](unsigned address) const override;
50 unsigned getSize() const override { return size; };
51 void write(unsigned address, byte value) override {};
52 void clear(byte value) override {};
53 private:
54 unsigned size;
55 };
56
57 class DivPlatformOPL: public DivDispatch {
58 protected:
59 struct Channel: public SharedChannel<int> {
60 DivInstrumentFM state;
61 unsigned int freqH, freqL;
62 int sample, fixedFreq;
63 bool furnacePCM, fourOp, hardReset, writeCtrl;
64 bool levelDirect, damp, pseudoReverb, lfoReset, ch;
65 int lfo, vib, am, ar, d1r, d2r, dl, rc, rr;
66 int pan;
67 int macroVolMul;
68 Channel():
69 SharedChannel<int>(0),
70 freqH(0),
71 freqL(0),
72 sample(-1),
73 fixedFreq(0),
74 furnacePCM(false),
75 fourOp(false),
76 hardReset(false),
77 writeCtrl(false),
78 levelDirect(true),
79 damp(false),
80 pseudoReverb(false),
81 lfoReset(false),
82 ch(false),
83 lfo(0),
84 vib(0),
85 am(0),
86 ar(15),
87 d1r(15),
88 d2r(0),
89 dl(0),
90 rc(15),
91 rr(15),
92 pan(3),
93 macroVolMul(64) {
94 state.ops=2;
95 }
96 };
97 Channel chan[44];
98 DivDispatchOscBuffer* oscBuf[44];
99 bool isMuted[44];
100 struct QueuedWrite {
101 unsigned int addr;
102 unsigned char val;
103 bool addrOrVal;
104 QueuedWrite(): addr(0), val(0), addrOrVal(false) {}
105 QueuedWrite(unsigned int a, unsigned char v): addr(a), val(v), addrOrVal(false) {}
106 };
107 FixedQueue<QueuedWrite,4096> writes;
108
109 unsigned int dacVal;
110 unsigned int dacVal2;
111 int dacOut;
112 int dacOut3[4];
113 bool lastSH;
114 bool lastSH2;
115 bool lastSY;
116 bool waitingBusy;
117 int downsamplerStep;
118 unsigned char oldOpMask;
119
120 unsigned char* adpcmBMem;
121 size_t adpcmBMemLen;
122 unsigned char* pcmMem;
123 size_t pcmMemLen;
124 DivOPLAInterface iface;
125 DivYMF278MemoryInterface pcmMemory;
126 unsigned int* sampleOffB;
127 unsigned int* sampleOffPCM;
128 bool* sampleLoaded;
129
130 ymfm::adpcm_b_engine* adpcmB;
131 const unsigned char** slotsNonDrums;
132 const unsigned char** slotsDrums;
133 const unsigned char** slots;
134 const unsigned short* chanMap;
135 const unsigned char* outChanMap;
136 int chipFreqBase, chipRateBase;
137 int delay, chipType, oplType, chans, melodicChans, totalChans, adpcmChan=-1, pcmChanOffs=-1, sampleBank, totalOutputs, ramSize;
138 int fmMixL=7, fmMixR=7, pcmMixL=7, pcmMixR=7;
139 unsigned char lastBusy;
140 unsigned char drumState;
141 unsigned char drumVol[5];
142
143 unsigned char regPool[768];
144
145 bool properDrums, properDrumsSys, dam, dvb;
146
147 unsigned char lfoValue;
148
149 // 0: Nuked-OPL3
150 // 1: ymfm
151 // 2: YM3812-LLE/YMF262-LLE
152 unsigned char emuCore;
153
154 bool update4OpMask, pretendYMU, downsample, compatPan, compatYPitch;
155
156 short oldWrites[768];
157 short pendingWrites[768];
158
159 // chips
160 opl3_chip fm;
161 YMF278 pcm;
162 ymfm::ym3526* fm_ymfm1;
163 ymfm::ym3812* fm_ymfm2;
164 ymfm::y8950* fm_ymfm8950;
165 ymfm::ymf262* fm_ymfm3;
166 ymfm::ymf278b* fm_ymfm4;
167 fmopl2_t fm_lle2;
168 fmopl3_t fm_lle3;
169
170 DivMemoryComposition memCompo;
171
172 int octave(int freq, int fixedBlock);
173 int toFreq(int freq, int fixedBlock);
174 double NOTE_ADPCMB(int note);
175 void commitState(int ch, DivInstrument* ins);
176
177 friend void putDispatchChip(void*,int);
178 friend void putDispatchChan(void*,int,int);
179
180 void acquire_nukedLLE2(short** buf, size_t len);
181 void acquire_nukedLLE3(short** buf, size_t len);
182 void acquire_nuked(short** buf, size_t len);
183 void acquire_ymfm3(short** buf, size_t len);
184 void acquire_ymfm4(short** buf, size_t len);
185 void acquire_ymfm8950(short** buf, size_t len);
186 void acquire_ymfm2(short** buf, size_t len);
187 void acquire_ymfm1(short** buf, size_t len);
188
189 public:
190 void acquire(short** buf, size_t len);
191 int dispatch(DivCommand c);
192 void* getChanState(int chan);
193 DivMacroInt* getChanMacroInt(int ch);
194 unsigned short getPan(int chan);
195 void getPaired(int ch, std::vector<DivChannelPair>& ret);
196 DivDispatchOscBuffer* getOscBuffer(int chan);
197 int mapVelocity(int ch, float vel);
198 float getGain(int ch, int vol);
199 unsigned char* getRegisterPool();
200 int getRegisterPoolSize();
201 void reset();
202 void forceIns();
203 void tick(bool sysTick=true);
204 void muteChannel(int ch, bool mute);
205 int getOutputCount();
206 void setCore(unsigned char which);
207 void setOPLType(int type, bool drums);
208 bool keyOffAffectsArp(int ch);
209 bool keyOffAffectsPorta(int ch);
210 bool getLegacyAlwaysSetVolume();
211 void toggleRegisterDump(bool enable);
212 void setFlags(const DivConfig& flags);
213 void notifyInsChange(int ins);
214 void notifyInsDeletion(void* ins);
215 int getPortaFloor(int ch);
216 void poke(unsigned int addr, unsigned short val);
217 void poke(std::vector<DivRegWrite>& wlist);
218 const void* getSampleMem(int index);
219 size_t getSampleMemCapacity(int index);
220 size_t getSampleMemUsage(int index);
221 bool isSampleLoaded(int index, int sample);
222 const DivMemoryComposition* getMemCompo(int index);
223 void renderSamples(int chipID);
224 int init(DivEngine* parent, int channels, int sugRate, const DivConfig& flags);
225 void quit();
226 DivPlatformOPL();
227 ~DivPlatformOPL();
228 };
229 #endif