Debian Codegraph @

src/engine/platform/msm5232.cpp

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 #define _USE_MATH_DEFINES
21 #include "msm5232.h"
22 #include "../engine.h"
23 #include "../../ta-log.h"
24 #include <math.h>
25
26 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
27
28 #define NOTE_LINEAR(x) ((x)<<7)
29
30 const char* regCheatSheetMSM5232[]={
31 "Select", "0",
32 "MasterVol", "1",
33 "FreqL", "2",
34 "FreqH", "3",
35 "DataCtl", "4",
36 "ChanVol", "5",
37 "WaveCtl", "6",
38 "NoiseCtl", "7",
39 "LFOFreq", "8",
40 "LFOCtl", "9",
41 NULL
42 };
43
44 const char** DivPlatformMSM5232::getRegisterSheet() {
45 return regCheatSheetMSM5232;
46 }
47
48 void DivPlatformMSM5232::acquire(short** buf, size_t len) {
49 for (int i=0; i<8; i++) {
50 oscBuf[i]->begin(len);
51 }
52
53 for (size_t h=0; h<len; h++) {
54 while (!writes.empty()) {
55 QueuedWrite w=writes.front();
56 msm->write(w.addr,w.val);
57 regPool[w.addr&0x0f]=w.val;
58 writes.pop();
59 }
60
61 for (int i=0; i<8; i++) {
62 if (isMuted[i]) {
63 oscBuf[i]->putSample(h,0);
64 } else {
65 int o=(
66 ((regPool[12+(i>>2)]&1)?((msm->vo16[i]*partVolume[3+(i&4)])>>8):0)+
67 ((regPool[12+(i>>2)]&2)?((msm->vo8[i]*partVolume[2+(i&4)])>>8):0)+
68 ((regPool[12+(i>>2)]&4)?((msm->vo4[i]*partVolume[1+(i&4)])>>8):0)+
69 ((regPool[12+(i>>2)]&8)?((msm->vo2[i]*partVolume[i&4])>>8):0)
70 )<<2;
71 oscBuf[i]->putSample(h,CLAMP(o,-32768,32767));
72 }
73 }
74
75 clockDriftLFOPos+=clockDriftLFOSpeed;
76 clockDriftLFOPos&=(1U<<21)-1;
77 clockDriftAccum+=clockDriftLFOWave[clockDriftLFOPos>>13];
78 if (clockDriftAccum>=2048) {
79 clockDriftAccum-=2048;
80 } else {
81 memset(temp,0,16*sizeof(short));
82 msm->sound_stream_update(temp);
83 }
84
85 //printf("tempL: %d tempR: %d\n",tempL,tempR);
86 buf[0][h]=0;
87 for (int i=0; i<8; i++) {
88 buf[0][h]+=(temp[i]*partVolume[i])>>8;
89 }
90 }
91
92 for (int i=0; i<8; i++) {
93 oscBuf[i]->end(len);
94 }
95 }
96
97 const int attackMap[8]={
98 0, 1, 2, 3, 4, 5, 5, 5
99 };
100
101 const int decayMap[16]={
102 0, 1, 2, 3, 8, 9, 4, 10, 5, 11, 12, 13, 13, 13, 13, 13
103 };
104
105 void DivPlatformMSM5232::tick(bool sysTick) {
106 for (int i=0; i<8; i++) {
107 chan[i].std.next();
108 if (chan[i].std.vol.had) {
109 chan[i].outVol=VOL_SCALE_LINEAR(chan[i].vol&127,MIN(127,chan[i].std.vol.val),127);
110 }
111 if (NEW_ARP_STRAT) {
112 chan[i].handleArp();
113 } else if (chan[i].std.arp.had) {
114 if (!chan[i].inPorta) {
115 chan[i].baseFreq=NOTE_LINEAR(parent->calcArp(chan[i].note,chan[i].std.arp.val));
116 }
117 chan[i].freqChanged=true;
118 }
119 if (chan[i].std.duty.had) {
120 groupControl[i>>2]=(chan[i].std.duty.val&0x1f)|(groupEnv[i>>2]?0x20:0);
121 updateGroup[i>>2]=true;
122 }
123 if (chan[i].std.ex1.had) { // attack
124 groupAR[i>>2]=attackMap[chan[i].std.ex1.val&7];
125 updateGroupAR[i>>2]=true;
126 }
127 if (chan[i].std.ex2.had) { // decay
128 groupDR[i>>2]=decayMap[chan[i].std.ex2.val&15];
129 updateGroupDR[i>>2]=true;
130 }
131 if (chan[i].std.ex3.had) { // noise
132 chan[i].noise=chan[i].std.ex3.val;
133 chan[i].freqChanged=true;
134 }
135 }
136
137 for (int i=0; i<2; i++) {
138 if (updateGroup[i]) {
139 rWrite(12+i,groupControl[i]);
140 // do not retrigger inactive channels
141 for (int j=i<<2; j<(i+1)<<2; j++) {
142 if (!chan[j].active) rWrite(j,0);
143 }
144 updateGroup[i]=false;
145 }
146 if (updateGroupAR[i]) {
147 rWrite(8+i,groupAR[i]);
148 updateGroupAR[i]=false;
149 }
150 if (updateGroupDR[i]) {
151 rWrite(10+i,groupDR[i]);
152 updateGroupDR[i]=false;
153 }
154 }
155
156 for (int i=0; i<8; i++) {
157 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
158 //DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_PCE);
159 chan[i].freq=chan[i].baseFreq+chan[i].pitch+chan[i].pitch2-(12<<7);
160 if (!parent->song.oldArpStrategy) {
161 if (chan[i].fixedArp) {
162 chan[i].freq=(chan[i].baseNoteOverride<<7)+(chan[i].pitch)-(12<<7);
163 } else {
164 chan[i].freq+=chan[i].arpOff<<7;
165 }
166 }
167 if (chan[i].freq<0) chan[i].freq=0;
168 if (chan[i].freq>0x2aff) chan[i].freq=0x2aff;
169 if (chan[i].keyOn) {
170 //rWrite(16+i*5,0x80);
171 //chWrite(i,0x04,0x80|chan[i].vol);
172 }
173 if (chan[i].active) {
174 rWrite(i,chan[i].noise?0xd8:(0x80|(chan[i].freq>>7)));
175 }
176 if (chan[i].keyOff) {
177 rWrite(i,0);
178 }
179 if (chan[i].keyOn) chan[i].keyOn=false;
180 if (chan[i].keyOff) chan[i].keyOff=false;
181 chan[i].freqChanged=false;
182 }
183 }
184
185 msm->set_vol_input(
186 chan[0].active?((double)chan[0].outVol/127.0):0.0,
187 chan[1].active?((double)chan[1].outVol/127.0):0.0,
188 chan[2].active?((double)chan[2].outVol/127.0):0.0,
189 chan[3].active?((double)chan[3].outVol/127.0):0.0,
190 chan[4].active?((double)chan[4].outVol/127.0):0.0,
191 chan[5].active?((double)chan[5].outVol/127.0):0.0,
192 chan[6].active?((double)chan[6].outVol/127.0):0.0,
193 chan[7].active?((double)chan[7].outVol/127.0):0.0
194 );
195 }
196
197 int DivPlatformMSM5232::dispatch(DivCommand c) {
198 switch (c.cmd) {
199 case DIV_CMD_NOTE_ON: {
200 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_PCE);
201 if (c.value!=DIV_NOTE_NULL) {
202 chan[c.chan].baseFreq=NOTE_LINEAR(c.value);
203 chan[c.chan].freqChanged=true;
204 chan[c.chan].note=c.value;
205 }
206 chan[c.chan].active=true;
207 chan[c.chan].keyOn=true;
208 chan[c.chan].macroInit(ins);
209 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
210 chan[c.chan].outVol=chan[c.chan].vol;
211 }
212 chan[c.chan].insChanged=false;
213 break;
214 }
215 case DIV_CMD_NOTE_OFF:
216 chan[c.chan].active=false;
217 chan[c.chan].keyOff=true;
218 chan[c.chan].macroInit(NULL);
219 break;
220 case DIV_CMD_NOTE_OFF_ENV:
221 case DIV_CMD_ENV_RELEASE:
222 chan[c.chan].std.release();
223 break;
224 case DIV_CMD_INSTRUMENT:
225 if (chan[c.chan].ins!=c.value || c.value2==1) {
226 chan[c.chan].ins=c.value;
227 chan[c.chan].insChanged=true;
228 }
229 break;
230 case DIV_CMD_VOLUME:
231 if (chan[c.chan].vol!=c.value) {
232 chan[c.chan].vol=c.value;
233 if (!chan[c.chan].std.vol.has) {
234 chan[c.chan].outVol=c.value;
235 }
236 }
237 break;
238 case DIV_CMD_GET_VOLUME:
239 if (chan[c.chan].std.vol.has) {
240 return chan[c.chan].vol;
241 }
242 return chan[c.chan].outVol;
243 break;
244 case DIV_CMD_PITCH:
245 chan[c.chan].pitch=c.value;
246 chan[c.chan].freqChanged=true;
247 break;
248 case DIV_CMD_NOTE_PORTA: {
249 int destFreq=NOTE_LINEAR(c.value2);
250 bool return2=false;
251 if (destFreq>chan[c.chan].baseFreq) {
252 chan[c.chan].baseFreq+=c.value*parent->song.pitchSlideSpeed;
253 if (chan[c.chan].baseFreq>=destFreq) {
254 chan[c.chan].baseFreq=destFreq;
255 return2=true;
256 }
257 } else {
258 chan[c.chan].baseFreq-=c.value*parent->song.pitchSlideSpeed;
259 if (chan[c.chan].baseFreq<=destFreq) {
260 chan[c.chan].baseFreq=destFreq;
261 return2=true;
262 }
263 }
264 chan[c.chan].freqChanged=true;
265 if (return2) {
266 chan[c.chan].inPorta=false;
267 return 2;
268 }
269 break;
270 }
271 case DIV_CMD_WAVE:
272 groupControl[c.chan>>2]=c.value&0x1f;
273 updateGroup[c.chan>>2]=true;
274 break;
275 case DIV_CMD_STD_NOISE_MODE:
276 chan[c.chan].noise=c.value;
277 chan[c.chan].freqChanged=true;
278 break;
279 case DIV_CMD_FM_AR:
280 groupAR[c.chan>>2]=attackMap[c.value&7];
281 updateGroupAR[c.chan>>2]=true;
282 break;
283 case DIV_CMD_FM_DR:
284 groupDR[c.chan>>2]=decayMap[c.value&15];
285 updateGroupDR[c.chan>>2]=true;
286 break;
287 case DIV_CMD_LEGATO:
288 chan[c.chan].baseFreq=NOTE_LINEAR(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
289 chan[c.chan].freqChanged=true;
290 chan[c.chan].note=c.value;
291 break;
292 case DIV_CMD_PRE_PORTA:
293 if (chan[c.chan].active && c.value2) {
294 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_PCE));
295 }
296 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_LINEAR(chan[c.chan].note);
297 chan[c.chan].inPorta=c.value;
298 break;
299 case DIV_CMD_GET_VOLMAX:
300 return 127;
301 break;
302 case DIV_CMD_MACRO_OFF:
303 chan[c.chan].std.mask(c.value,true);
304 break;
305 case DIV_CMD_MACRO_ON:
306 chan[c.chan].std.mask(c.value,false);
307 break;
308 case DIV_CMD_MACRO_RESTART:
309 chan[c.chan].std.restart(c.value);
310 break;
311 default:
312 break;
313 }
314 return 1;
315 }
316
317 void DivPlatformMSM5232::muteChannel(int ch, bool mute) {
318 isMuted[ch]=mute;
319 msm->mute(ch,mute);
320 }
321
322 void DivPlatformMSM5232::forceIns() {
323 for (int i=0; i<8; i++) {
324 chan[i].insChanged=true;
325 chan[i].freqChanged=true;
326 }
327 for (int i=0; i<2; i++) {
328 updateGroup[i]=true;
329 updateGroupAR[i]=true;
330 updateGroupDR[i]=true;
331 }
332 }
333
334 void* DivPlatformMSM5232::getChanState(int ch) {
335 return &chan[ch];
336 }
337
338 DivMacroInt* DivPlatformMSM5232::getChanMacroInt(int ch) {
339 return &chan[ch].std;
340 }
341
342 DivDispatchOscBuffer* DivPlatformMSM5232::getOscBuffer(int ch) {
343 return oscBuf[ch];
344 }
345
346 unsigned char* DivPlatformMSM5232::getRegisterPool() {
347 return regPool;
348 }
349
350 int DivPlatformMSM5232::getRegisterPoolSize() {
351 return 14;
352 }
353
354 void DivPlatformMSM5232::reset() {
355 while (!writes.empty()) writes.pop();
356 memset(regPool,0,128);
357 for (int i=0; i<8; i++) {
358 chan[i]=DivPlatformMSM5232::Channel();
359 chan[i].std.setEngine(parent);
360 }
361 if (dumpWrites) {
362 addWrite(0xffffffff,0);
363 }
364 msm->device_start();
365 msm->device_reset();
366 memset(temp,0,16*sizeof(short));
367 cycles=0;
368 curChan=-1;
369 delay=500;
370 clockDriftLFOPos=0;
371 clockDriftAccum=0;
372
373 for (int i=0; i<2; i++) {
374 groupControl[i]=15|(groupEnv[i]?0x20:0);
375 groupAR[i]=0;
376 groupDR[i]=5;
377
378 updateGroup[i]=true;
379 updateGroupAR[i]=true;
380 updateGroupDR[i]=true;
381 }
382
383 for (int i=0; i<8; i++) {
384 rWrite(i,0);
385 partVolume[i]=initPartVolume[i];
386 msm->mute(i,isMuted[i]);
387 }
388 }
389
390 int DivPlatformMSM5232::getOutputCount() {
391 return 1;
392 }
393
394 bool DivPlatformMSM5232::keyOffAffectsArp(int ch) {
395 return true;
396 }
397
398 void DivPlatformMSM5232::notifyInsDeletion(void* ins) {
399 for (int i=0; i<8; i++) {
400 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
401 }
402 }
403
404 void DivPlatformMSM5232::setFlags(const DivConfig& flags) {
405 chipClock=2119040;
406 CHECK_CUSTOM_CLOCK;
407 detune=flags.getInt("detune",0);
408 msm->set_clock(chipClock+detune*1024);
409 rate=msm->get_rate();
410 for (int i=0; i<8; i++) {
411 oscBuf[i]->setRate(rate);
412 }
413 initPartVolume[0]=flags.getInt("partVolume0",255);
414 initPartVolume[1]=flags.getInt("partVolume1",255);
415 initPartVolume[2]=flags.getInt("partVolume2",255);
416 initPartVolume[3]=flags.getInt("partVolume3",255);
417 initPartVolume[4]=flags.getInt("partVolume4",255);
418 initPartVolume[5]=flags.getInt("partVolume5",255);
419 initPartVolume[6]=flags.getInt("partVolume6",255);
420 initPartVolume[7]=flags.getInt("partVolume7",255);
421
422 capacitance[0]=flags.getFloat("capValue0",390.0f);
423 capacitance[1]=flags.getFloat("capValue1",390.0f);
424 capacitance[2]=flags.getFloat("capValue2",390.0f);
425 capacitance[3]=flags.getFloat("capValue3",390.0f);
426 capacitance[4]=flags.getFloat("capValue4",390.0f);
427 capacitance[5]=flags.getFloat("capValue5",390.0f);
428 capacitance[6]=flags.getFloat("capValue6",390.0f);
429 capacitance[7]=flags.getFloat("capValue7",390.0f);
430
431 groupEnv[0]=flags.getBool("groupEnv0",true);
432 groupEnv[1]=flags.getBool("groupEnv1",true);
433
434 msm->set_capacitors(
435 capacitance[0]*0.000000001,
436 capacitance[1]*0.000000001,
437 capacitance[2]*0.000000001,
438 capacitance[3]*0.000000001,
439 capacitance[4]*0.000000001,
440 capacitance[5]*0.000000001,
441 capacitance[6]*0.000000001,
442 capacitance[7]*0.000000001
443 );
444
445 for (int i=0; i<256; i++) {
446 clockDriftLFOWave[i]=(1.0+sin(M_PI*(double)i/128.0))*flags.getInt("vibDepth",0.0f);
447 }
448 clockDriftLFOSpeed=flags.getInt("vibSpeed",0);
449 }
450
451 void DivPlatformMSM5232::poke(unsigned int addr, unsigned short val) {
452 rWrite(addr,val);
453 }
454
455 void DivPlatformMSM5232::poke(std::vector<DivRegWrite>& wlist) {
456 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
457 }
458
459 int DivPlatformMSM5232::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
460 parent=p;
461 dumpWrites=false;
462 skipRegisterWrites=false;
463 for (int i=0; i<8; i++) {
464 isMuted[i]=false;
465 oscBuf[i]=new DivDispatchOscBuffer;
466 }
467 msm=new msm5232_device(2119040);
468 msm->device_start();
469 setFlags(flags);
470 reset();
471 return 8;
472 }
473
474 void DivPlatformMSM5232::quit() {
475 for (int i=0; i<8; i++) {
476 delete oscBuf[i];
477 }
478 if (msm!=NULL) {
479 msm->device_stop();
480 delete msm;
481 msm=NULL;
482 }
483 }
484
485 DivPlatformMSM5232::~DivPlatformMSM5232() {
486 }