Debian Codegraph @

src/engine/platform/sid3.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 _SID3_H
21 #define _SID3_H
22
23 #include "../dispatch.h"
24 #include "../../fixedQueue.h"
25 #include "../waveSynth.h"
26 #include "sound/sid3.h"
27
28 class DivPlatformSID3: public DivDispatch {
29 struct Channel: public SharedChannel<signed short> {
30 int prevFreq;
31 unsigned char wave, special_wave, attack, decay, sustain, sr, release;
32 int duty;
33 bool resetMask, resetFilter, resetDuty, gate, ring, sync, phase, oneBitNoise;
34 bool phaseReset, envReset, phaseResetNoise;
35 bool noiseFreqChanged;
36 unsigned char vol;
37 unsigned char noise_mode;
38 unsigned char mix_mode;
39 unsigned char ringSrc, syncSrc, phaseSrc;
40 unsigned char panLeft, panRight;
41 int noiseFreq;
42 bool independentNoiseFreq;
43 unsigned int noiseLFSRMask;
44
45 struct Filter
46 {
47 int cutoff;
48 unsigned char resonance;
49 unsigned char output_volume;
50 unsigned char distortion_level;
51 unsigned char mode;
52 bool enabled;
53 unsigned char filter_matrix;
54
55 short cutoff_slide;
56
57 bool bindCutoffToNote; //cutoff scaling
58 unsigned char bindCutoffToNoteStrength; //how much cutoff changes over e.g. 1 semitone
59 unsigned char bindCutoffToNoteCenter; //central note of the cutoff change
60 bool bindCutoffToNoteDir; //if we decrease or increase cutoff if e.g. we go upper in note space
61
62 bool bindResonanceToNote;
63 unsigned char bindResonanceToNoteStrength; //how much resonance changes over e.g. 1 semitone
64 unsigned char bindResonanceToNoteCenter; //central note of the resonance change
65 bool bindResonanceToNoteDir; //if we decrease or increase resonance if e.g. we go upper in note space
66
67 Filter():
68 cutoff(0),
69 resonance(0),
70 output_volume(0),
71 distortion_level(0),
72 mode(0),
73 enabled(false),
74 filter_matrix(0),
75 cutoff_slide(0),
76 bindCutoffToNote(false),
77 bindCutoffToNoteStrength(0),
78 bindCutoffToNoteCenter(0),
79 bindCutoffToNoteDir(false),
80 bindResonanceToNote(false),
81 bindResonanceToNoteStrength(0),
82 bindResonanceToNoteCenter(0),
83 bindResonanceToNoteDir(false) {}
84 } filt[SID3_NUM_FILTERS];
85
86 int noise_baseNoteOverride;
87 bool noise_fixedArp;
88 int noise_arpOff;
89 int noise_pitch2;
90 bool noise_hasArp;
91 bool noise_hasPitch;
92
93 bool pcm;
94 int wavetable;
95
96 long long dacPeriod, dacRate, dacOut;
97 unsigned long long dacPos;
98 int dacSample;
99
100 unsigned char phaseInv;
101 unsigned char feedback;
102
103 short pw_slide;
104
105 short phase_reset_counter;
106 short noise_phase_reset_counter;
107 short envelope_reset_counter;
108
109 void handleArpNoise(int offset=0)
110 {
111 DivMacroStruct& m = this->std.op[3].am;
112
113 if (m.had) {
114 noise_hasArp=true;
115
116 if (m.val<0) {
117 if (!(m.val&0x40000000)) {
118 noise_baseNoteOverride=(m.val|0x40000000)+offset;
119 noise_fixedArp=true;
120 } else {
121 noise_arpOff=m.val;
122 noise_fixedArp=false;
123 }
124 } else {
125 if (m.val&0x40000000) {
126 noise_baseNoteOverride=(m.val&(~0x40000000))+offset;
127 noise_fixedArp=true;
128 } else {
129 noise_arpOff=m.val;
130 noise_fixedArp=false;
131 }
132 }
133 noiseFreqChanged=true;
134 }
135
136 else
137 {
138 noise_hasArp=false;
139 }
140 }
141
142 void handlePitchNoise()
143 {
144 DivMacroStruct& m = this->std.op[0].ar;
145
146 if (m.had) {
147 noise_hasPitch=true;
148
149 if (m.mode) {
150 noise_pitch2+=m.val;
151 CLAMP_VAR(noise_pitch2,-131071,131071);
152 } else {
153 noise_pitch2=m.val;
154 }
155 noiseFreqChanged=true;
156 }
157
158 else
159 {
160 noise_hasPitch=false;
161 }
162 }
163
164 Channel():
165 SharedChannel<signed short>(SID3_MAX_VOL),
166 prevFreq(0xffffff),
167 wave(0),
168 special_wave(0),
169 attack(0),
170 decay(0),
171 sustain(0),
172 sr(0),
173 release(0),
174 duty(0),
175 resetMask(false),
176 resetFilter(false),
177 resetDuty(false),
178 gate(true),
179 ring(false),
180 sync(false),
181 phase(false),
182 oneBitNoise(false),
183 phaseReset(false),
184 envReset(false),
185 phaseResetNoise(false),
186 noiseFreqChanged(false),
187 vol(SID3_MAX_VOL),
188 noise_mode(0),
189 mix_mode(0),
190 ringSrc(0),
191 syncSrc(0),
192 phaseSrc(0),
193 panLeft(0xff),
194 panRight(0xff),
195 noiseFreq(0),
196 independentNoiseFreq(false),
197 noiseLFSRMask((1 << 29) | (1 << 5) | (1 << 3) | 1), //https://docs.amd.com/v/u/en-US/xapp052 for 30 bits: 30, 6, 4, 1
198 pcm(false),
199 wavetable(-1),
200 dacPeriod(0),
201 dacRate(0),
202 dacOut(0),
203 dacPos(0),
204 dacSample(-1),
205 phaseInv(0),
206 feedback(0),
207 pw_slide(0),
208 phase_reset_counter(-1),
209 noise_phase_reset_counter(-1),
210 envelope_reset_counter(-1) {}
211 };
212 Channel chan[SID3_NUM_CHANNELS];
213 DivDispatchOscBuffer* oscBuf[SID3_NUM_CHANNELS];
214 struct QueuedWrite {
215 unsigned short addr;
216 unsigned char val;
217 QueuedWrite(): addr(0), val(0) {}
218 QueuedWrite(unsigned short a, unsigned char v): addr(a), val(v) {}
219 };
220 FixedQueue<QueuedWrite,SID3_NUM_REGISTERS * 4> writes;
221 DivWaveSynth ws;
222
223 unsigned char writeOscBuf;
224
225 SID3* sid3;
226 unsigned char regPool[SID3_NUM_REGISTERS];
227
228 bool isMuted[SID3_NUM_CHANNELS];
229
230 unsigned char sampleTick; //used to update streamed sample and not clash with other reg writes at high rate samples
231 bool updateSample;
232 bool quarterClock;
233
234 friend void putDispatchChip(void*,int);
235 friend void putDispatchChan(void*,int,int);
236
237 void updateFlags(int channel, bool gate);
238 void updateFilter(int channel, int filter);
239 void updateFreq(int channel);
240 void updateNoiseFreq(int channel);
241 void updateNoiseLFSRMask(int channel);
242 void updateDuty(int channel);
243 void updateEnvelope(int channel);
244 void updatePanning(int channel);
245 void updateWave();
246
247 public:
248 void acquire(short** buf, size_t len);
249 int dispatch(DivCommand c);
250 void* getChanState(int chan);
251 DivDispatchOscBuffer* getOscBuffer(int chan);
252 unsigned char* getRegisterPool();
253 int getRegisterPoolSize();
254 void reset();
255 void forceIns();
256 void tick(bool sysTick=true);
257 void muteChannel(int ch, bool mute);
258 void setFlags(const DivConfig& flags);
259 void notifyInsChange(int ins);
260 void notifyWaveChange(int wave);
261 float getPostAmp();
262 bool getDCOffRequired();
263 unsigned short getPan(int chan);
264 DivMacroInt* getChanMacroInt(int ch);
265 DivChannelModeHints getModeHints(int chan);
266 void notifyInsDeletion(void* ins);
267 void poke(unsigned int addr, unsigned short val);
268 void poke(std::vector<DivRegWrite>& wlist);
269 const char** getRegisterSheet();
270 int init(DivEngine* parent, int channels, int sugRate, const DivConfig& flags);
271 int getOutputCount();
272 void getPaired(int ch, std::vector<DivChannelPair>& ret);
273 void quit();
274 ~DivPlatformSID3();
275 };
276
277 #endif