Debian Codegraph @

src/engine/platform/powernoise.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 "powernoise.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include "furIcons.h"
24 #include <math.h>
25 #include "../bsr.h"
26
27 #define rWrite(a,v) if (!skipRegisterWrites) {regPool[a]=(v); pwrnoise_write(&pn,(unsigned char)(a),(unsigned char)(v)); if (dumpWrites) {addWrite(a,v);}}
28 #define chWrite(c,a,v) rWrite((c<<3)|((a)+1),(v))
29 #define noiseCtl(enable,am,tapB) (((enable)?0x80:0x00)|((am)?0x02:0x00)|((tapB)?0x01:0x00))
30 #define slopeCtl(enable,rst,a,b) (((enable)?0x80:0x00)| \
31 (rst?0x40:0x00)| \
32 (a.clip?0x20:0x00)| \
33 (b.clip?0x10:0x00)| \
34 (a.reset?0x08:0x00)| \
35 (b.reset?0x04:0x00)| \
36 (a.dir?0x02:0x00)| \
37 (b.dir?0x01:0x00))
38 #define volPan(v,p) (((v*(p>>4)/15)<<4)|((v*(p&0xf)/15)&0xf))
39 // TODO: optimize!
40 #define mapAmp(a) ((((a)*65535/63-32768)*(pn.flags&0x7)/7)>>1)
41
42 const char* regCheatSheetPowerNoise[]={
43 "ACTL", "00",
44 "N1CTL", "01",
45 "N1FL", "02",
46 "N1FH", "03",
47 "N1SRL", "04",
48 "N1SRH", "05",
49 "N1TAP", "06",
50 "N1V", "07",
51 "IOA", "08",
52 "N2CTL", "09",
53 "N2FL", "0A",
54 "N2FH", "0B",
55 "N2SRL", "0C",
56 "N2SRH", "0D",
57 "N2TAP", "0E",
58 "N2V", "0F",
59 "IOB", "10",
60 "N3CTL", "11",
61 "N3FL", "12",
62 "N3FH", "13",
63 "N3SRL", "14",
64 "N3SRH", "15",
65 "N3TAP", "16",
66 "N3V", "17",
67 "SLACC", "18",
68 "SLCTL", "19",
69 "SLFL", "1A",
70 "SLFH", "1B",
71 "SLPA", "1C",
72 "SLPB", "1D",
73 "SLPO", "1E",
74 "SLV", "1F",
75 NULL
76 };
77
78 const char** DivPlatformPowerNoise::getRegisterSheet() {
79 return regCheatSheetPowerNoise;
80 }
81
82 void DivPlatformPowerNoise::acquire(short** buf, size_t len) {
83 short left, right;
84
85 for (int i=0; i<4; i++) {
86 oscBuf[i]->begin(len);
87 }
88
89 for (size_t h=0; h<len; h++) {
90 pwrnoise_step(&pn,coreQuality,&left,&right);
91
92 oscBuf[0]->putSample(h,mapAmp((pn.n1.out_latch&0xf)+(pn.n1.out_latch>>4)));
93 oscBuf[1]->putSample(h,mapAmp((pn.n2.out_latch&0xf)+(pn.n2.out_latch>>4)));
94 oscBuf[2]->putSample(h,mapAmp((pn.n3.out_latch&0xf)+(pn.n3.out_latch>>4)));
95 oscBuf[3]->putSample(h,mapAmp((pn.s.out_latch&0xf)+(pn.s.out_latch>>4)));
96
97 buf[0][h]=left;
98 buf[1][h]=right;
99 }
100
101 for (int i=0; i<4; i++) {
102 oscBuf[i]->end(len);
103 }
104 }
105
106 /* macros:
107 * EX1 - control (0-63)
108 * EX2 - portion A length (0-255) - slope only
109 * EX3 - portion B length (0-255) - slope only
110 * EX4 - tap A location (0-15) - noise only
111 * EX5 - tap B location (0-15) - noise only
112 * EX6 - portion A offset (0-15) - slope only
113 * EX7 - portion B offset (0-15) - slope only
114 * EX8 - load LFSR (0-65535) - noise only
115 */
116
117 void DivPlatformPowerNoise::tick(bool sysTick) {
118 for (int i=0; i<4; i++) {
119 int CHIP_DIVIDER=2;
120 if (i==3) CHIP_DIVIDER=128;
121 chan[i].std.next();
122
123 if (chan[i].std.ex1.had) {
124 int val=chan[i].std.ex1.val;
125
126 if (chan[i].slope) {
127 chan[i].slopeA.clip=(val&0x20);
128 chan[i].slopeB.clip=(val&0x10);
129 chan[i].slopeA.reset=(val&0x08);
130 chan[i].slopeB.reset=(val&0x04);
131 chan[i].slopeA.dir=(val&0x02);
132 chan[i].slopeB.dir=(val&0x01);
133 chWrite(i,0x00,slopeCtl(chan[i].active,false,chan[i].slopeA,chan[i].slopeB));
134 } else {
135 chan[i].am=(val&0x02);
136 chan[i].tapBEnable=(val&0x01);
137 chWrite(i,0x00,noiseCtl(chan[i].active, chan[i].am, chan[i].tapBEnable));
138 }
139 }
140 if (chan[i].slope) {
141 if (chan[i].std.ex2.had) {
142 chan[i].slopeA.len=chan[i].std.ex2.val;
143 chWrite(i,0x03,chan[i].std.ex2.val);
144 }
145 if (chan[i].std.ex3.had) {
146 chan[i].slopeB.len=chan[i].std.ex3.val;
147 chWrite(i,0x04,chan[i].std.ex3.val);
148 }
149 if (chan[i].std.ex6.had) {
150 chan[i].slopeA.offset=chan[i].std.ex6.val;
151 }
152 if (chan[i].std.ex7.had) {
153 chan[i].slopeB.offset=chan[i].std.ex7.val;
154 }
155 if (chan[i].std.ex6.had || chan[i].std.ex7.had) {
156 chWrite(i,0x05,(chan[i].slopeA.offset<<4)|chan[i].slopeB.offset);
157 }
158 } else {
159 if (chan[i].std.ex4.had) {
160 chan[i].tapA=chan[i].std.ex4.val;
161 }
162 if (chan[i].std.ex5.had) {
163 chan[i].tapB=chan[i].std.ex5.val;
164 }
165 if (chan[i].std.ex4.had || chan[i].std.ex5.had) {
166 chWrite(i,0x05,(chan[i].tapA<<4)|chan[i].tapB);
167 }
168 if (chan[i].std.ex8.had) {
169 if (chan[i].initLFSR!=(chan[i].std.ex8.val&0xffff)) {
170 chan[i].initLFSR=chan[i].std.ex8.val&0xffff;
171 chWrite(i,0x03,chan[i].std.ex8.val&0xff);
172 chWrite(i,0x04,chan[i].std.ex8.val>>8);
173 }
174 }
175 }
176
177 if (chan[i].std.vol.had) {
178 chan[i].outVol=VOL_SCALE_LINEAR(chan[i].vol&15,MIN(15,chan[i].std.vol.val),15);
179 if (chan[i].outVol<0) chan[i].outVol=0;
180 }
181 if (NEW_ARP_STRAT) {
182 chan[i].handleArp();
183 } else if (chan[i].std.arp.had) {
184 if (!chan[i].inPorta) {
185 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
186 }
187 chan[i].freqChanged=true;
188 }
189 if (chan[i].std.panL.had) {
190 chan[i].pan&=0x0f;
191 chan[i].pan|=(chan[i].std.panL.val&15)<<4;
192 }
193 if (chan[i].std.panR.had) {
194 chan[i].pan&=0xf0;
195 chan[i].pan|=chan[i].std.panR.val&15;
196 }
197
198 if (chan[i].std.vol.had || chan[i].std.panL.had || chan[i].std.panR.had) {
199 chWrite(i,0x06,isMuted[i]?0:volPan(chan[i].outVol,chan[i].pan));
200 }
201 if (chan[i].std.pitch.had) {
202 if (chan[i].std.pitch.mode) {
203 chan[i].pitch2+=chan[i].std.pitch.val;
204 CLAMP_VAR(chan[i].pitch2,-32768,32767);
205 } else {
206 chan[i].pitch2=chan[i].std.pitch.val;
207 }
208 chan[i].freqChanged=true;
209 }
210 if (chan[i].std.phaseReset.had && chan[i].std.phaseReset.val==1) {
211 if (chan[i].slope) {
212 chWrite(i,0x00,slopeCtl(chan[i].active,true,chan[i].slopeA,chan[i].slopeB));
213 chan[i].keyOn=true;
214 } else {
215 chWrite(i,0x03,chan[i].initLFSR&0xff);
216 chWrite(i,0x04,chan[i].initLFSR>>8);
217 }
218 }
219
220 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
221 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);
222 chan[i].freq>>=chan[i].octaveOff;
223
224 if (chan[i].freq<0) chan[i].freq=0;
225 if (chan[i].freq>0x7ffffff) chan[i].freq=0x7ffffff;
226 int bsr32Val=bsr32(chan[i].freq);
227 chan[i].octave=MAX(bsr32Val-12,0);
228 if (chan[i].octave>15) chan[i].octave=15;
229 chan[i].fNum=0x1000-(chan[i].freq>>chan[i].octave);
230 if (chan[i].fNum<0) chan[i].fNum=0;
231 if (chan[i].fNum>4095) chan[i].fNum=4095;
232
233 chWrite(i,0x01,chan[i].fNum&0xff);
234 chWrite(i,0x02,(chan[i].fNum>>8)|(chan[i].octave<<4));
235
236 if (chan[i].keyOn) {
237 if (chan[i].slope) {
238 chWrite(i,0x00,slopeCtl(true,false,chan[i].slopeA,chan[i].slopeB));
239 } else {
240 chWrite(i,0x00,noiseCtl(true,chan[i].am,chan[i].tapBEnable));
241 }
242 }
243 if (chan[i].keyOff) {
244 if (chan[i].slope) {
245 chWrite(i,0x00,slopeCtl(false,false,chan[i].slopeA,chan[i].slopeB));
246 } else {
247 chWrite(i,0x00,noiseCtl(false,chan[i].am,chan[i].tapBEnable));
248 }
249 }
250
251 if (chan[i].keyOn) chan[i].keyOn=false;
252 if (chan[i].keyOff) chan[i].keyOff=false;
253 chan[i].freqChanged=false;
254 }
255
256 if (chan[i].slope) {
257 unsigned char counter=pn.s.accum;
258 regPool[0x18]=counter;
259 } else {
260 unsigned short lfsr;
261 if (i==0) {
262 lfsr=pn.n1.lfsr;
263 } else if (i==1) {
264 lfsr=pn.n2.lfsr;
265 } else {
266 lfsr=pn.n3.lfsr;
267 }
268 regPool[(i<<3)+0x4]=lfsr&0xff;
269 regPool[(i<<3)+0x5]=lfsr>>8;
270 }
271 }
272 }
273
274 int DivPlatformPowerNoise::dispatch(DivCommand c) {
275 int CHIP_DIVIDER=2;
276 if (c.chan==3) CHIP_DIVIDER=128;
277
278 switch (c.cmd) {
279 case DIV_CMD_NOTE_ON: {
280 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_POWERNOISE);
281 if (c.value!=DIV_NOTE_NULL) {
282 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
283 chan[c.chan].freqChanged=true;
284 chan[c.chan].note=c.value;
285 }
286 if (chan[c.chan].insChanged) {
287 chan[c.chan].octaveOff=ins->powernoise.octave;
288 }
289 chan[c.chan].active=true;
290 chan[c.chan].macroInit(ins);
291 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
292 chan[c.chan].outVol=chan[c.chan].vol;
293 }
294 chan[c.chan].keyOn=true;
295 chan[c.chan].insChanged=false;
296 break;
297 }
298 case DIV_CMD_NOTE_OFF:
299 chan[c.chan].active=false;
300 chan[c.chan].keyOff=true;
301 chan[c.chan].macroInit(NULL);
302 break;
303 case DIV_CMD_NOTE_OFF_ENV:
304 case DIV_CMD_ENV_RELEASE:
305 chan[c.chan].std.release();
306 break;
307 case DIV_CMD_INSTRUMENT:
308 if (chan[c.chan].ins!=c.value || c.value2==1) {
309 chan[c.chan].ins=c.value;
310 chan[c.chan].insChanged=true;
311 }
312 break;
313 case DIV_CMD_VOLUME:
314 if (chan[c.chan].vol!=c.value) {
315 chan[c.chan].vol=c.value;
316 if (!chan[c.chan].std.vol.has) {
317 chan[c.chan].outVol=c.value;
318 chWrite(c.chan,0x06,isMuted[c.chan]?0:volPan(chan[c.chan].outVol,chan[c.chan].pan));
319 }
320 }
321 break;
322 case DIV_CMD_GET_VOLUME:
323 if (chan[c.chan].std.vol.has) {
324 return chan[c.chan].vol;
325 }
326 return chan[c.chan].outVol;
327 break;
328 case DIV_CMD_PITCH:
329 chan[c.chan].pitch=c.value;
330 chan[c.chan].freqChanged=true;
331 break;
332 case DIV_CMD_NOTE_PORTA: {
333 int destFreq=NOTE_PERIODIC(c.value2);
334
335 bool return2=false;
336 if (destFreq>chan[c.chan].baseFreq) {
337 chan[c.chan].baseFreq+=c.value;
338 if (chan[c.chan].baseFreq>0x7ffffff) {
339 chan[c.chan].baseFreq=0x7ffffff;
340 }
341 if (chan[c.chan].baseFreq>=destFreq) {
342 chan[c.chan].baseFreq=destFreq;
343 return2=true;
344 }
345 } else {
346 chan[c.chan].baseFreq-=c.value;
347 if (chan[c.chan].baseFreq<0) {
348 chan[c.chan].baseFreq=0;
349 }
350 if (chan[c.chan].baseFreq<=destFreq) {
351 chan[c.chan].baseFreq=destFreq;
352 return2=true;
353 }
354 }
355 chan[c.chan].freqChanged=true;
356 if (return2) {
357 chan[c.chan].inPorta=false;
358 return 2;
359 }
360 break;
361 }
362 case DIV_CMD_PANNING:
363 chan[c.chan].pan=(c.value&0xf0)|(c.value2>>4);
364 break;
365 case DIV_CMD_LEGATO: {
366 int whatAMess=c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0));
367
368 chan[c.chan].baseFreq=NOTE_PERIODIC(whatAMess);
369 chan[c.chan].freqChanged=true;
370 chan[c.chan].note=c.value;
371 break;
372 }
373 case DIV_CMD_PRE_PORTA: {
374 if (chan[c.chan].active && c.value2) {
375 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_POWERNOISE));
376 }
377 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) {
378 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
379 }
380 chan[c.chan].inPorta=c.value;
381 break;
382 }
383 case DIV_CMD_GET_VOLMAX:
384 return 15;
385 break;
386 case DIV_CMD_POWERNOISE_COUNTER_LOAD: {
387 if (chan[c.chan].slope && c.value==0) {
388 rWrite(0x18,c.value2&0x7f);
389 } else if (!chan[c.chan].slope) {
390 chWrite(c.chan,0x03+c.value,c.value2);
391 }
392 break;
393 }
394 case DIV_CMD_POWERNOISE_IO_WRITE:
395 rWrite(0x08+(c.value<<3),c.value2);
396 break;
397 case DIV_CMD_MACRO_OFF:
398 chan[c.chan].std.mask(c.value,true);
399 break;
400 case DIV_CMD_MACRO_ON:
401 chan[c.chan].std.mask(c.value,false);
402 break;
403 case DIV_CMD_MACRO_RESTART:
404 chan[c.chan].std.restart(c.value);
405 break;
406 default:
407 break;
408 }
409 return 1;
410 }
411
412 void DivPlatformPowerNoise::muteChannel(int ch, bool mute) {
413 isMuted[ch]=mute;
414 chWrite(ch,0x06,isMuted[ch]?0:volPan(chan[ch].outVol,chan[ch].pan));
415 }
416
417 void DivPlatformPowerNoise::forceIns() {
418 for (int i=0; i<4; i++) {
419 chan[i].insChanged=true;
420 chan[i].freqChanged=true;
421 if (i<3) {
422 chWrite(i,0x03,chan[i].initLFSR&0xff);
423 chWrite(i,0x04,chan[i].initLFSR>>8);
424 }
425 }
426 }
427
428 void* DivPlatformPowerNoise::getChanState(int ch) {
429 return &chan[ch];
430 }
431
432 DivMacroInt* DivPlatformPowerNoise::getChanMacroInt(int ch) {
433 return &chan[ch].std;
434 }
435
436 unsigned short DivPlatformPowerNoise::getPan(int ch) {
437 return ((chan[ch].pan&0xf0)<<4)|(chan[ch].pan&15);
438 }
439
440 DivChannelModeHints DivPlatformPowerNoise::getModeHints(int ch) {
441 DivChannelModeHints ret;
442 ret.count=0;
443 return ret;
444 }
445
446 bool DivPlatformPowerNoise::getDCOffRequired() {
447 return true;
448 }
449
450 DivDispatchOscBuffer* DivPlatformPowerNoise::getOscBuffer(int ch) {
451 return oscBuf[ch];
452 }
453
454 unsigned char* DivPlatformPowerNoise::getRegisterPool() {
455 return regPool;
456 }
457
458 int DivPlatformPowerNoise::getRegisterPoolSize() {
459 return 32;
460 }
461
462 void DivPlatformPowerNoise::reset() {
463 memset(regPool,0,32);
464 for (int i=0; i<4; i++) {
465 chan[i]=DivPlatformPowerNoise::Channel();
466 chan[i].std.setEngine(parent);
467 chan[i].slope=(i==3);
468 }
469
470 pwrnoise_reset(&pn);
471
472 // set master volume to full
473 rWrite(0,0x87);
474 // set per-channel panning
475 for (int i=0; i<4; i++) {
476 chWrite(i,0x06,isMuted[i]?0:volPan(chan[i].outVol,chan[i].pan));
477 }
478 // set default params so we have sound
479 // noise
480 for (int i=0; i<3; i++) {
481 chWrite(i,0x03,chan[i].initLFSR&0xff);
482 chWrite(i,0x04,chan[i].initLFSR>>8);
483 chWrite(i,0x05,(chan[i].tapA<<4)|chan[i].tapB);
484 }
485 // slope
486 chWrite(3,0x03,chan[3].slopeA.len);
487 chWrite(3,0x04,chan[3].slopeB.len);
488 chWrite(3,0x05,(chan[3].slopeA.offset<<4)|chan[3].slopeB.offset);
489
490 addWrite(0xffffffff,0);
491 }
492
493 int DivPlatformPowerNoise::getOutputCount() {
494 return 2;
495 }
496
497 bool DivPlatformPowerNoise::keyOffAffectsArp(int ch) {
498 return true;
499 }
500
501 void DivPlatformPowerNoise::notifyWaveChange(int wave) {
502 }
503
504 void DivPlatformPowerNoise::notifyInsDeletion(void* ins) {
505 for (int i=0; i<4; i++) {
506 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
507 }
508 }
509
510 void DivPlatformPowerNoise::setFlags(const DivConfig& flags) {
511 chipClock=16000000;
512
513 CHECK_CUSTOM_CLOCK;
514 rate=chipClock/coreQuality;
515
516 for (int i=0; i<4; i++) {
517 oscBuf[i]->setRate(rate);
518 }
519 }
520
521 void DivPlatformPowerNoise::poke(unsigned int addr, unsigned short val) {
522 rWrite(addr,val);
523 }
524
525 void DivPlatformPowerNoise::poke(std::vector<DivRegWrite>& wlist) {
526 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
527 }
528
529 void DivPlatformPowerNoise::setCoreQuality(unsigned char q) {
530 switch (q) {
531 case 0:
532 coreQuality=256;
533 break;
534 case 1:
535 coreQuality=128;
536 break;
537 case 2:
538 coreQuality=64;
539 break;
540 case 3:
541 coreQuality=32;
542 break;
543 case 4:
544 coreQuality=8;
545 break;
546 case 5:
547 coreQuality=1;
548 break;
549 default:
550 coreQuality=32;
551 break;
552 }
553 }
554
555 int DivPlatformPowerNoise::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
556 parent=p;
557 dumpWrites=false;
558 skipRegisterWrites=false;
559
560 for (int i=0; i<4; i++) {
561 oscBuf[i]=new DivDispatchOscBuffer;
562 isMuted[i]=false;
563 }
564 setFlags(flags);
565 reset();
566 return 4;
567 }
568
569 void DivPlatformPowerNoise::quit() {
570 for (int i=0; i<4; i++) {
571 delete oscBuf[i];
572 }
573 }
574
575 DivPlatformPowerNoise::~DivPlatformPowerNoise() {
576 }