Debian Codegraph @

src/engine/platform/vb.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 #include "vb.h"
21 #include "../engine.h"
22 #include "IconsFontAwesome4.h"
23 #include "furIcons.h"
24 #include <math.h>
25
26 //#define rWrite(a,v) pendingWrites[a]=v;
27 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
28 #define chWrite(c,a,v) rWrite(0x400+((c)<<6)+((a)<<2),v);
29
30 #define CHIP_DIVIDER 16
31
32 const char* regCheatSheetVB[]={
33 "Wave0", "000",
34 "Wave1", "080",
35 "Wave2", "100",
36 "Wave3", "180",
37 "Wave4", "200",
38 "ModTable", "280",
39
40 "S1INT", "400",
41 "S1LRV", "404",
42 "S1FQL", "408",
43 "S1FQH", "40C",
44 "S1EV0", "410",
45 "S1EV1", "414",
46 "S1RAM", "418",
47
48 "S2INT", "440",
49 "S2LRV", "444",
50 "S2FQL", "448",
51 "S2FQH", "44C",
52 "S2EV0", "450",
53 "S2EV1", "454",
54 "S2RAM", "458",
55
56 "S3INT", "480",
57 "S3LRV", "484",
58 "S3FQL", "488",
59 "S3FQH", "48C",
60 "S3EV0", "480",
61 "S3EV1", "484",
62 "S3RAM", "488",
63
64 "S4INT", "4C0",
65 "S4LRV", "4C4",
66 "S4FQL", "4C8",
67 "S4FQH", "4CC",
68 "S4EV0", "4C0",
69 "S4EV1", "4C4",
70 "S4RAM", "4C8",
71
72 "S5INT", "500",
73 "S5LRV", "504",
74 "S5FQL", "508",
75 "S5FQH", "50C",
76 "S5EV0", "510",
77 "S5EV1", "514",
78 "S5RAM", "518",
79
80 "S5SWP", "51C",
81
82 "S6INT", "540",
83 "S6LRV", "544",
84 "S6FQL", "548",
85 "S6FQH", "54C",
86 "S6EV0", "550",
87 "S6EV1", "554",
88 "S6RAM", "558",
89
90 "RESET", "580",
91 NULL
92 };
93
94 const char** DivPlatformVB::getRegisterSheet() {
95 return regCheatSheetVB;
96 }
97
98 void DivPlatformVB::acquireDirect(blip_buffer_t** bb, size_t len) {
99 for (int i=0; i<6; i++) {
100 oscBuf[i]->begin(len);
101 vb->oscBuf[i]=oscBuf[i];
102 }
103
104 vb->bb[0]=bb[0];
105 vb->bb[1]=bb[1];
106
107 for (size_t h=0; h<len; h++) {
108 if (!writes.empty()) {
109 QueuedWrite w=writes.front();
110 vb->Write(h,w.addr,w.val);
111 regPool[w.addr>>2]=w.val;
112 writes.pop();
113 } else {
114 break;
115 }
116 }
117 vb->EndFrame(len);
118
119 for (int i=0; i<6; i++) {
120 oscBuf[i]->end(len);
121 }
122 }
123
124 void DivPlatformVB::updateWave(int ch) {
125 if (romMode) return;
126 if (ch>=5) return;
127
128 for (int i=0; i<32; i++) {
129 rWrite((ch<<7)+(i<<2),chan[ch].ws.output[(i+chan[ch].antiClickWavePos)&31]);
130 }
131 chan[ch].antiClickWavePos&=31;
132 }
133
134 void DivPlatformVB::writeEnv(int ch, bool upperByteToo) {
135 chWrite(ch,0x04,(chan[ch].outVol<<4)|(chan[ch].envLow&15));
136 if (ch<5 || upperByteToo) {
137 chWrite(ch,0x05,chan[ch].envHigh);
138 }
139 }
140
141 void DivPlatformVB::tick(bool sysTick) {
142 bool mustUpdateWaves=false;
143
144 for (int i=0; i<6; i++) {
145 // anti-click
146 int actualFreq=2047-chan[i].freq;
147 if (antiClickEnabled && !screwThis && sysTick && actualFreq>0) {
148 chan[i].antiClickPeriodCount+=(chipClock/MAX(parent->getCurHz(),1.0f));
149 chan[i].antiClickWavePos+=chan[i].antiClickPeriodCount/actualFreq;
150 chan[i].antiClickPeriodCount%=actualFreq;
151 }
152
153 chan[i].std.next();
154 // this is handled first to work around an envelope problem
155 // once envelope is over, you cannot enable it again unless you retrigger the channel
156 if (chan[i].std.phaseReset.had && chan[i].std.phaseReset.val==1) {
157 chWrite(i,0x00,0x80);
158 chan[i].intWritten=true;
159 chan[i].antiClickWavePos=0;
160 chan[i].antiClickPeriodCount=0;
161 }
162 if (chan[i].std.vol.had) {
163 chan[i].outVol=VOL_SCALE_LINEAR(chan[i].vol&15,MIN(15,chan[i].std.vol.val),15);
164 writeEnv(i);
165 }
166 if (NEW_ARP_STRAT) {
167 chan[i].handleArp();
168 } else if (chan[i].std.arp.had) {
169 if (!chan[i].inPorta) {
170 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
171 }
172 chan[i].freqChanged=true;
173 }
174 if (i==5 && chan[i].std.duty.had) {
175 if ((chan[i].std.duty.val&7)!=((chan[i].envHigh>>4)&7)) {
176 chan[i].envHigh&=~0x70;
177 chan[i].envHigh|=(chan[i].std.duty.val&7)<<4;
178 writeEnv(i,true);
179 }
180 }
181 if (chan[i].std.wave.had) {
182 if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) {
183 chan[i].wave=chan[i].std.wave.val;
184 if (romMode) {
185 chWrite(i,0x06,chan[i].wave);
186 }
187 chan[i].ws.changeWave1(chan[i].wave);
188 if (!chan[i].keyOff) chan[i].keyOn=true;
189 }
190 }
191 if (chan[i].std.panL.had) {
192 chan[i].pan&=0x0f;
193 chan[i].pan|=(chan[i].std.panL.val&15)<<4;
194 }
195 if (chan[i].std.panR.had) {
196 chan[i].pan&=0xf0;
197 chan[i].pan|=chan[i].std.panR.val&15;
198 }
199 if (chan[i].std.panL.had || chan[i].std.panR.had) {
200 chWrite(i,0x01,isMuted[i]?0:chan[i].pan);
201 }
202 if (chan[i].std.pitch.had) {
203 if (chan[i].std.pitch.mode) {
204 chan[i].pitch2+=chan[i].std.pitch.val;
205 CLAMP_VAR(chan[i].pitch2,-32768,32767);
206 } else {
207 chan[i].pitch2=chan[i].std.pitch.val;
208 }
209 chan[i].freqChanged=true;
210 }
211 if (chan[i].active) {
212 if (chan[i].ws.tick() || (chan[i].std.phaseReset.had && chan[i].std.phaseReset.val==1)) {
213 if (!romMode) {
214 chan[i].deferredWaveUpdate=true;
215 }
216 mustUpdateWaves=true;
217 }
218 }
219 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
220 chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,true,0,chan[i].pitch2,chipClock,CHIP_DIVIDER);
221 if (chan[i].freq<1) chan[i].freq=1;
222 if (chan[i].freq>2047) chan[i].freq=2047;
223 chan[i].freq=2048-chan[i].freq;
224 chWrite(i,0x02,chan[i].freq&0xff);
225 chWrite(i,0x03,chan[i].freq>>8);
226 if (chan[i].keyOn) {
227 }
228 if (chan[i].keyOff) {
229 chWrite(i,0x04,0);
230 }
231 if (chan[i].keyOn) chan[i].keyOn=false;
232 if (chan[i].keyOff) chan[i].keyOff=false;
233 chan[i].freqChanged=false;
234 }
235 }
236
237 // trigger wave changes
238 if (mustUpdateWaves && !romMode) {
239 if (!screwThis) {
240 rWrite(0x580,1);
241 }
242 for (int i=0; i<5; i++) {
243 //if (chan[i].deferredWaveUpdate) {
244 //chan[i].deferredWaveUpdate=false;
245 updateWave(i);
246 //}
247 }
248 if (!screwThis) {
249 // restore channel state...
250 for (int i=0; i<6; i++) {
251 if (chan[i].intWritten) {
252 chWrite(i,0x00,0x80);
253 }
254 }
255 }
256 }
257
258 for (int i=0; i<6; i++) {
259 if ((chan[i].envHigh&3)==0) {
260 chan[i].hasEnvWarning=0;
261 } else {
262 switch (vb->EnvelopeModMask[i]) {
263 case 0: // envelope OK
264 chan[i].hasEnvWarning=0;
265 break;
266 case 1: // envelope has finished
267 chan[i].hasEnvWarning=21;
268 break;
269 case 2: // can't envelope
270 chan[i].hasEnvWarning=22;
271 break;
272 }
273 }
274 }
275 /*if (vb->ModLock) {
276 chan[4].hasEnvWarning=4;
277 }*/
278 }
279
280 int DivPlatformVB::dispatch(DivCommand c) {
281 switch (c.cmd) {
282 case DIV_CMD_NOTE_ON: {
283 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_PCE);
284 if (c.value!=DIV_NOTE_NULL) {
285 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
286 chan[c.chan].freqChanged=true;
287 chan[c.chan].note=c.value;
288 }
289 chan[c.chan].active=true;
290 chan[c.chan].keyOn=true;
291 chan[c.chan].macroInit(ins);
292 if (c.chan==4 && chan[c.chan].insChanged && ins->fds.initModTableWithFirstWave) {
293 chWrite(4,0x00,0x00);
294 for (int i=0; i<32; i++) {
295 modTable[i]=ins->fds.modTable[i];
296 rWrite(0x280+(i<<2),modTable[i]);
297 }
298 chWrite(4,0x00,0x80);
299 }
300 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
301 chan[c.chan].outVol=chan[c.chan].vol;
302 writeEnv(c.chan);
303 }
304 if (chan[c.chan].wave<0) {
305 chan[c.chan].wave=0;
306 chan[c.chan].ws.changeWave1(chan[c.chan].wave);
307 }
308 chan[c.chan].ws.init(ins,32,63,chan[c.chan].insChanged);
309 chan[c.chan].insChanged=false;
310 break;
311 }
312 case DIV_CMD_NOTE_OFF:
313 chan[c.chan].active=false;
314 chan[c.chan].keyOff=true;
315 chan[c.chan].macroInit(NULL);
316 break;
317 case DIV_CMD_NOTE_OFF_ENV:
318 case DIV_CMD_ENV_RELEASE:
319 chan[c.chan].std.release();
320 break;
321 case DIV_CMD_INSTRUMENT:
322 if (chan[c.chan].ins!=c.value || c.value2==1) {
323 chan[c.chan].ins=c.value;
324 chan[c.chan].insChanged=true;
325 }
326 break;
327 case DIV_CMD_VOLUME:
328 if (chan[c.chan].vol!=c.value) {
329 chan[c.chan].vol=c.value;
330 if (!chan[c.chan].std.vol.has) {
331 chan[c.chan].outVol=c.value;
332 if (chan[c.chan].active) {
333 writeEnv(c.chan);
334 }
335 }
336 }
337 break;
338 case DIV_CMD_GET_VOLUME:
339 if (chan[c.chan].std.vol.has) {
340 return chan[c.chan].vol;
341 }
342 return chan[c.chan].outVol;
343 break;
344 case DIV_CMD_PITCH:
345 chan[c.chan].pitch=c.value;
346 chan[c.chan].freqChanged=true;
347 break;
348 case DIV_CMD_WAVE:
349 chan[c.chan].wave=c.value;
350 chan[c.chan].ws.changeWave1(chan[c.chan].wave);
351 if (romMode) {
352 chWrite(c.chan,0x06,chan[c.chan].wave);
353 }
354 chan[c.chan].keyOn=true;
355 break;
356 case DIV_CMD_NOTE_PORTA: {
357 int destFreq=NOTE_PERIODIC(c.value2);
358 bool return2=false;
359 if (destFreq>chan[c.chan].baseFreq) {
360 chan[c.chan].baseFreq+=c.value;
361 if (chan[c.chan].baseFreq>=destFreq) {
362 chan[c.chan].baseFreq=destFreq;
363 return2=true;
364 }
365 } else {
366 chan[c.chan].baseFreq-=c.value;
367 if (chan[c.chan].baseFreq<=destFreq) {
368 chan[c.chan].baseFreq=destFreq;
369 return2=true;
370 }
371 }
372 chan[c.chan].freqChanged=true;
373 if (return2) {
374 chan[c.chan].inPorta=false;
375 return 2;
376 }
377 break;
378 }
379 case DIV_CMD_STD_NOISE_MODE:
380 if (c.chan!=5) break;
381 chan[c.chan].envHigh&=~0x70;
382 chan[c.chan].envHigh|=(c.value&7)<<4;
383 writeEnv(c.chan,true);
384 break;
385 case DIV_CMD_STD_NOISE_FREQ:
386 chan[c.chan].envHigh&=~3;
387 chan[c.chan].envHigh|=(c.value>>4)&3;
388 chan[c.chan].envLow=c.value&15;
389 writeEnv(c.chan,true);
390 break;
391 case DIV_CMD_FDS_MOD_DEPTH: // set modulation
392 if (c.chan!=4) break;
393 modulation=(c.value&15)<<4;
394 modType=true;
395 chWrite(4,0x07,modulation);
396 if (modulation!=0) {
397 chan[c.chan].envHigh&=~0x70;
398 chan[c.chan].envHigh|=0x40|(c.value&0xf0);
399 } else {
400 chan[c.chan].envHigh&=~0x70;
401 }
402 writeEnv(4);
403 break;
404 case DIV_CMD_GB_SWEEP_TIME: // set sweep
405 if (c.chan!=4) break;
406 modulation=c.value;
407 modType=false;
408 chWrite(4,0x07,modulation);
409 if (modulation!=0) {
410 chan[c.chan].envHigh&=~0x70;
411 chan[c.chan].envHigh|=0x40;
412 } else {
413 chan[c.chan].envHigh&=~0x70;
414 }
415 writeEnv(4);
416 break;
417 case DIV_CMD_FDS_MOD_WAVE: { // set modulation wave
418 if (c.chan!=4) break;
419 DivWavetable* wt=parent->getWave(c.value);
420 chWrite(4,0x00,0x00);
421 for (int i=0; i<32; i++) {
422 if (wt->max<1 || wt->len<1) {
423 modTable[i]=0;
424 rWrite(0x280+(i<<2),0);
425 } else {
426 int data=(wt->data[i*wt->len/32]*255/wt->max)-128;
427 if (data<-128) data=-128;
428 if (data>127) data=127;
429 modTable[i]=data;
430 rWrite(0x280+(i<<2),modTable[i]);
431 }
432 }
433 chWrite(4,0x00,0x80);
434 break;
435 }
436 case DIV_CMD_PANNING: {
437 chan[c.chan].pan=(c.value&0xf0)|(c.value2>>4);
438 chWrite(c.chan,0x01,isMuted[c.chan]?0:chan[c.chan].pan);
439 break;
440 }
441 case DIV_CMD_LEGATO:
442 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
443 chan[c.chan].freqChanged=true;
444 chan[c.chan].note=c.value;
445 break;
446 case DIV_CMD_PRE_PORTA:
447 if (chan[c.chan].active && c.value2) {
448 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_PCE));
449 }
450 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_PERIODIC(chan[c.chan].note);
451 chan[c.chan].inPorta=c.value;
452 break;
453 case DIV_CMD_GET_VOLMAX:
454 return 15;
455 break;
456 case DIV_CMD_MACRO_OFF:
457 chan[c.chan].std.mask(c.value,true);
458 break;
459 case DIV_CMD_MACRO_ON:
460 chan[c.chan].std.mask(c.value,false);
461 break;
462 case DIV_CMD_MACRO_RESTART:
463 chan[c.chan].std.restart(c.value);
464 break;
465 default:
466 break;
467 }
468 return 1;
469 }
470
471 void DivPlatformVB::muteChannel(int ch, bool mute) {
472 isMuted[ch]=mute;
473 chWrite(ch,0x01,isMuted[ch]?0:chan[ch].pan);
474 }
475
476 void DivPlatformVB::forceIns() {
477 for (int i=0; i<6; i++) {
478 chan[i].insChanged=true;
479 chan[i].freqChanged=true;
480 updateWave(i);
481 if (romMode) {
482 chWrite(i,0x06,chan[i].wave);
483 }
484 chWrite(i,0x01,isMuted[i]?0:chan[i].pan);
485 }
486 if (chan[5].active) {
487 writeEnv(5,true);
488 }
489 }
490
491 void* DivPlatformVB::getChanState(int ch) {
492 return &chan[ch];
493 }
494
495 DivMacroInt* DivPlatformVB::getChanMacroInt(int ch) {
496 return &chan[ch].std;
497 }
498
499 unsigned short DivPlatformVB::getPan(int ch) {
500 return ((chan[ch].pan&0xf0)<<4)|(chan[ch].pan&15);
501 }
502
503 DivChannelModeHints DivPlatformVB::getModeHints(int ch) {
504 DivChannelModeHints ret;
505 //if (ch>4) return ret;
506 ret.count=1;
507 ret.hint[0]=ICON_FA_EXCLAMATION_TRIANGLE;
508 ret.type[0]=chan[ch].hasEnvWarning;
509
510 return ret;
511 }
512
513 DivDispatchOscBuffer* DivPlatformVB::getOscBuffer(int ch) {
514 return oscBuf[ch];
515 }
516
517 unsigned char* DivPlatformVB::getRegisterPool() {
518 return regPool;
519 }
520
521 int DivPlatformVB::getRegisterPoolSize() {
522 return 0x180;
523 }
524
525 int DivPlatformVB::getRegisterPoolDepth() {
526 return 8;
527 }
528
529 void DivPlatformVB::reset() {
530 while (!writes.empty()) writes.pop();
531 memset(regPool,0,0x600);
532 for (int i=0; i<6; i++) {
533 chan[i]=DivPlatformVB::Channel();
534 chan[i].std.setEngine(parent);
535 chan[i].ws.setEngine(parent);
536 chan[i].ws.init(NULL,32,63,false);
537 }
538 if (dumpWrites) {
539 addWrite(0xffffffff,0);
540 }
541 vb->Power();
542 tempL=0;
543 tempR=0;
544 curChan=-1;
545 modulation=0;
546 modType=false;
547 memset(modTable,0,32);
548 updateROMWaves();
549 // set per-channel initial values
550 for (int i=0; i<6; i++) {
551 chWrite(i,0x01,isMuted[i]?0:chan[i].pan);
552 chWrite(i,0x05,0x00);
553 chan[i].intWritten=true;
554 chWrite(i,0x00,0x80);
555 if (romMode) {
556 chWrite(i,0x06,0);
557 } else {
558 chWrite(i,0x06,i);
559 }
560 }
561 delay=500;
562 }
563
564 int DivPlatformVB::getOutputCount() {
565 return 2;
566 }
567
568 bool DivPlatformVB::keyOffAffectsArp(int ch) {
569 return true;
570 }
571
572 bool DivPlatformVB::hasAcquireDirect() {
573 return true;
574 }
575
576 float DivPlatformVB::getPostAmp() {
577 return 6.0f;
578 }
579
580 void DivPlatformVB::updateROMWaves() {
581 if (romMode) {
582 // copy wavetables
583 for (int i=0; i<5; i++) {
584 int data=0;
585 DivWavetable* w=parent->getWave(i);
586
587 for (int j=0; j<32; j++) {
588 if (w->max<1 || w->len<1) {
589 data=0;
590 } else {
591 data=w->data[j*w->len/32]*63/w->max;
592 if (data<0) data=0;
593 if (data>63) data=63;
594 }
595 rWrite((i<<7)+(j<<2),data);
596 }
597 }
598 }
599 }
600
601 void DivPlatformVB::notifyWaveChange(int wave) {
602 for (int i=0; i<6; i++) {
603 if (chan[i].wave==wave) {
604 chan[i].ws.changeWave1(wave);
605 updateWave(i);
606 }
607 }
608 updateROMWaves();
609 }
610
611 void DivPlatformVB::notifyInsDeletion(void* ins) {
612 for (int i=0; i<6; i++) {
613 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
614 }
615 }
616
617 void DivPlatformVB::setFlags(const DivConfig& flags) {
618 chipClock=5000000.0;
619 CHECK_CUSTOM_CLOCK;
620 rate=chipClock;
621 for (int i=0; i<6; i++) {
622 oscBuf[i]->setRate(rate);
623 }
624
625 romMode=flags.getBool("romMode",false);
626 antiClickEnabled=!flags.getBool("noAntiClick",false);
627 screwThis=flags.getBool("screwThis",false);
628
629 if (vb!=NULL) {
630 delete vb;
631 vb=NULL;
632 }
633 vb=new VSU;
634 }
635
636 void DivPlatformVB::poke(unsigned int addr, unsigned short val) {
637 rWrite(addr,val);
638 }
639
640 void DivPlatformVB::poke(std::vector<DivRegWrite>& wlist) {
641 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
642 }
643
644 int DivPlatformVB::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
645 parent=p;
646 dumpWrites=false;
647 skipRegisterWrites=false;
648 for (int i=0; i<6; i++) {
649 isMuted[i]=false;
650 oscBuf[i]=new DivDispatchOscBuffer;
651 }
652 vb=NULL;
653 setFlags(flags);
654 reset();
655 return 6;
656 }
657
658 void DivPlatformVB::quit() {
659 for (int i=0; i<6; i++) {
660 delete oscBuf[i];
661 }
662 if (vb!=NULL) {
663 delete vb;
664 vb=NULL;
665 }
666 }
667
668 DivPlatformVB::~DivPlatformVB() {
669 }