Debian Codegraph @

src/engine/platform/fds.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 "fds.h"
21 #include "sound/nes/cpu_inline.h"
22 #include "../engine.h"
23 #include "sound/nes_nsfplay/nes_fds.h"
24 #include <math.h>
25
26 #define CHIP_FREQBASE 262144
27
28 #define rWrite(a,v) if (!skipRegisterWrites) {doWrite(a,v); regPool[(a)&0x7f]=v; if (dumpWrites) {addWrite(a,v);} }
29
30 const char* regCheatSheetFDS[]={
31 "IOCtrl", "4023",
32 "Wave", "4040",
33 "Volume", "4080",
34 "FreqL", "4082",
35 "FreqH", "4083",
36 "ModCtrl", "4084",
37 "ModCount", "4085",
38 "ModFreqL", "4086",
39 "ModFreqH", "4087",
40 "ModWrite", "4088",
41 "WaveCtrl", "4089",
42 "EnvSpeed", "408A",
43 "ReadVol", "4090",
44 "ReadPos", "4091",
45 "ReadModV", "4092",
46 "ReadModP", "4093",
47 "ReadModCG", "4094",
48 "ReadModInc", "4095",
49 "ReadWave", "4096",
50 "ReadModCount", "4097",
51 NULL
52 };
53
54 const char** DivPlatformFDS::getRegisterSheet() {
55 return regCheatSheetFDS;
56 }
57
58 void DivPlatformFDS::acquire_puNES(short* buf, size_t len) {
59 oscBuf->begin(len);
60 for (size_t i=0; i<len; i++) {
61 extcl_apu_tick_FDS(fds);
62 int sample=isMuted[0]?0:fds->snd.main.output;
63 if (sample>32767) sample=32767;
64 if (sample<-32768) sample=-32768;
65 buf[i]=sample;
66 if (++writeOscBuf>=32) {
67 writeOscBuf=0;
68 oscBuf->putSample(i,sample*3);
69 }
70 }
71 oscBuf->end(len);
72 }
73
74 void DivPlatformFDS::acquire_NSFPlay(short* buf, size_t len) {
75 int out[2];
76 oscBuf->begin(len);
77 for (size_t i=0; i<len; i++) {
78 fds_NP->Tick(16);
79 fds_NP->Render(out);
80 int sample=isMuted[0]?0:(out[0]<<1);
81 if (sample>32767) sample=32767;
82 if (sample<-32768) sample=-32768;
83 buf[i]=sample;
84 oscBuf->putSample(i,sample*3);
85 }
86 oscBuf->end(len);
87 }
88
89 void DivPlatformFDS::doWrite(unsigned short addr, unsigned char data) {
90 if (useNP) {
91 fds_NP->Write(addr,data);
92 } else {
93 fds_wr_mem(fds,addr,data);
94 }
95 }
96
97 void DivPlatformFDS::acquire(short** buf, size_t len) {
98 if (useNP) {
99 acquire_NSFPlay(buf[0],len);
100 } else {
101 acquire_puNES(buf[0],len);
102 }
103 }
104
105 void DivPlatformFDS::updateWave() {
106 // TODO: master volume
107 rWrite(0x4089,0x80);
108 for (int i=0; i<64; i++) {
109 rWrite(0x4040+i,ws.output[i]);
110 }
111 rWrite(0x4089,0);
112 }
113
114 void DivPlatformFDS::tick(bool sysTick) {
115 for (int i=0; i<1; i++) {
116 chan[i].std.next();
117 if (chan[i].std.vol.had) {
118 // ok, why are the volumes like that?
119 chan[i].outVol=VOL_SCALE_LINEAR_BROKEN(chan[i].vol,chan[i].std.vol.val,32);
120 if (chan[i].outVol<0) chan[i].outVol=0;
121 rWrite(0x4080,0x80|chan[i].outVol);
122 }
123 if (NEW_ARP_STRAT) {
124 chan[i].handleArp();
125 } else if (chan[i].std.arp.had) {
126 if (!chan[i].inPorta) {
127 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
128 }
129 chan[i].freqChanged=true;
130 }
131 /*
132 if (chan[i].std.duty.had) {
133 chan[i].duty=chan[i].std.duty.val;
134 if (i==3) {
135 if (parent->song.properNoiseLayout) {
136 chan[i].duty&=1;
137 } else if (chan[i].duty>1) {
138 chan[i].duty=1;
139 }
140 }
141 if (i!=2) {
142 rWrite(0x4000+i*4,0x30|chan[i].outVol|((chan[i].duty&3)<<6));
143 }
144 if (i==3) { // noise
145 chan[i].freqChanged=true;
146 }
147 }*/
148 if (chan[i].std.wave.had) {
149 if (chan[i].wave!=chan[i].std.wave.val || ws.activeChanged()) {
150 chan[i].wave=chan[i].std.wave.val;
151 ws.changeWave1(chan[i].wave);
152 //if (!chan[i].keyOff) chan[i].keyOn=true;
153 }
154 }
155 if (chan[i].std.pitch.had) {
156 if (chan[i].std.pitch.mode) {
157 chan[i].pitch2+=chan[i].std.pitch.val;
158 CLAMP_VAR(chan[i].pitch2,-32768,32767);
159 } else {
160 chan[i].pitch2=chan[i].std.pitch.val;
161 }
162 chan[i].freqChanged=true;
163 }
164 if (chan[i].active) {
165 if (ws.tick()) {
166 updateWave();
167 if (!chan[i].keyOff) chan[i].keyOn=true;
168 }
169 }
170 if (chan[i].std.ex1.had) { // mod depth
171 chan[i].modOn=chan[i].std.ex1.val;
172 chan[i].modDepth=chan[i].std.ex1.val;
173 rWrite(0x4084,(chan[i].modOn<<7)|0x40|chan[i].modDepth);
174 }
175 if (chan[i].std.ex2.had) { // mod speed
176 chan[i].modFreq=chan[i].std.ex2.val;
177 rWrite(0x4086,chan[i].modFreq&0xff);
178 rWrite(0x4087,chan[i].modFreq>>8);
179 }
180 if (chan[i].std.ex3.had) { // mod position
181 chan[i].modPos=chan[i].std.ex3.val;
182 rWrite(0x4087,0x80|chan[i].modFreq>>8);
183 rWrite(0x4085,chan[i].modPos);
184 rWrite(0x4087,chan[i].modFreq>>8);
185 }
186 if (chan[i].sweepChanged) {
187 chan[i].sweepChanged=false;
188 if (i==0) {
189 //rWrite(16+i*5,chan[i].sweep);
190 }
191 }
192 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
193 chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,2,chan[i].pitch2,chipClock,CHIP_FREQBASE);
194 if (chan[i].freq>4095) chan[i].freq=4095;
195 if (chan[i].freq<0) chan[i].freq=0;
196 if (chan[i].keyOn) {
197 // ???
198 }
199 if (chan[i].keyOff) {
200 rWrite(0x4080,0x80);
201 }
202 rWrite(0x4082,chan[i].freq&0xff);
203 rWrite(0x4083,(chan[i].freq>>8)&15);
204
205 if (chan[i].autoModNum>0 && chan[i].autoModDen>0) {
206 chan[i].modFreq=(chan[i].freq*chan[i].autoModNum)/chan[i].autoModDen;
207 if (chan[i].modFreq>4095) chan[i].modFreq=4095;
208 if (chan[i].modFreq<0) chan[i].modFreq=0;
209 rWrite(0x4086,chan[i].modFreq&0xff);
210 rWrite(0x4087,chan[i].modFreq>>8);
211 }
212
213 if (chan[i].keyOn) chan[i].keyOn=false;
214 if (chan[i].keyOff) chan[i].keyOff=false;
215 chan[i].freqChanged=false;
216 }
217 }
218 }
219
220 int DivPlatformFDS::dispatch(DivCommand c) {
221 switch (c.cmd) {
222 case DIV_CMD_NOTE_ON: {
223 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_FDS);
224 if (c.value!=DIV_NOTE_NULL) {
225 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
226 chan[c.chan].freqChanged=true;
227 chan[c.chan].note=c.value;
228 }
229 if (chan[c.chan].insChanged) {
230 if (ins->fds.initModTableWithFirstWave) { // compatible
231 if (chan[c.chan].wave==-1) {
232 DivWavetable* wt=parent->getWave(0);
233 for (int i=0; i<32; i++) {
234 if (wt->max<1 || wt->len<1) {
235 chan[c.chan].modTable[i]=0;
236 } else {
237 int data=wt->data[i*MIN(32,wt->len)/32]*7/wt->max;
238 if (data<0) data=0;
239 if (data>7) data=7;
240 chan[c.chan].modTable[i]=data;
241 }
242 }
243 rWrite(0x4087,0x80|chan[c.chan].modFreq>>8);
244 for (int i=0; i<32; i++) {
245 rWrite(0x4088,chan[c.chan].modTable[i]);
246 }
247 rWrite(0x4087,chan[c.chan].modFreq>>8);
248 }
249 } else { // The Familiar Way
250 chan[c.chan].modDepth=ins->fds.modDepth;
251 chan[c.chan].modOn=ins->fds.modDepth;
252 chan[c.chan].modFreq=ins->fds.modSpeed;
253 rWrite(0x4084,(chan[c.chan].modOn<<7)|0x40|chan[c.chan].modDepth);
254 rWrite(0x4086,chan[c.chan].modFreq&0xff);
255
256 rWrite(0x4087,0x80|chan[c.chan].modFreq>>8);
257 for (int i=0; i<32; i++) {
258 chan[c.chan].modTable[i]=ins->fds.modTable[i]&7;
259 rWrite(0x4088,chan[c.chan].modTable[i]);
260 }
261 rWrite(0x4087,chan[c.chan].modFreq>>8);
262 }
263 }
264 chan[c.chan].active=true;
265 chan[c.chan].keyOn=true;
266 chan[c.chan].macroInit(ins);
267 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
268 chan[c.chan].outVol=chan[c.chan].vol;
269 }
270 if (chan[c.chan].wave<0) {
271 chan[c.chan].wave=0;
272 ws.changeWave1(chan[c.chan].wave);
273 }
274 ws.init(ins,64,63,chan[c.chan].insChanged);
275 rWrite(0x4080,0x80|chan[c.chan].vol);
276 chan[c.chan].insChanged=false;
277 break;
278 }
279 case DIV_CMD_NOTE_OFF:
280 chan[c.chan].active=false;
281 chan[c.chan].keyOff=true;
282 chan[c.chan].macroInit(NULL);
283 break;
284 case DIV_CMD_NOTE_OFF_ENV:
285 case DIV_CMD_ENV_RELEASE:
286 chan[c.chan].std.release();
287 break;
288 case DIV_CMD_INSTRUMENT:
289 if (chan[c.chan].ins!=c.value || c.value2==1) {
290 chan[c.chan].ins=c.value;
291 chan[c.chan].insChanged=true;
292 }
293 break;
294 case DIV_CMD_VOLUME:
295 if (chan[c.chan].vol!=c.value) {
296 chan[c.chan].vol=c.value;
297 if (!chan[c.chan].std.vol.has) {
298 chan[c.chan].outVol=c.value;
299 }
300 rWrite(0x4080,0x80|chan[c.chan].vol);
301 }
302 break;
303 case DIV_CMD_GET_VOLUME:
304 return chan[c.chan].vol;
305 break;
306 case DIV_CMD_PITCH:
307 chan[c.chan].pitch=c.value;
308 chan[c.chan].freqChanged=true;
309 break;
310 case DIV_CMD_WAVE:
311 if (chan[c.chan].wave!=c.value) {
312 chan[c.chan].wave=c.value;
313 ws.changeWave1(chan[c.chan].wave);
314 }
315 break;
316 case DIV_CMD_FDS_MOD_DEPTH:
317 chan[c.chan].modDepth=c.value;
318 rWrite(0x4084,(chan[c.chan].modOn<<7)|0x40|chan[c.chan].modDepth);
319 break;
320 case DIV_CMD_FDS_MOD_HIGH:
321 chan[c.chan].modOn=c.value>>4;
322 chan[c.chan].modFreq=((c.value&15)<<8)|(chan[c.chan].modFreq&0xff);
323 rWrite(0x4084,(chan[c.chan].modOn<<7)|0x40|chan[c.chan].modDepth);
324 rWrite(0x4087,chan[c.chan].modFreq>>8);
325 break;
326 case DIV_CMD_FDS_MOD_LOW:
327 chan[c.chan].modFreq=(chan[c.chan].modFreq&0xf00)|c.value;
328 rWrite(0x4086,chan[c.chan].modFreq&0xff);
329 break;
330 case DIV_CMD_FDS_MOD_POS:
331 chan[c.chan].modPos=c.value&0x7f;
332 rWrite(0x4087,0x80|chan[c.chan].modFreq>>8);
333 rWrite(0x4085,chan[c.chan].modPos);
334 rWrite(0x4087,chan[c.chan].modFreq>>8);
335 break;
336 case DIV_CMD_FDS_MOD_WAVE: {
337 DivWavetable* wt=parent->getWave(c.value);
338 for (int i=0; i<32; i++) {
339 if (wt->max<1 || wt->len<1) {
340 rWrite(0x4040+i,0);
341 } else {
342 int data=wt->data[i*MIN(32,wt->len)/32]*7/wt->max;
343 if (data<0) data=0;
344 if (data>7) data=7;
345 chan[c.chan].modTable[i]=data;
346 }
347 }
348 rWrite(0x4087,0x80|chan[c.chan].modFreq>>8);
349 for (int i=0; i<32; i++) {
350 rWrite(0x4088,chan[c.chan].modTable[i]);
351 }
352 rWrite(0x4087,chan[c.chan].modFreq>>8);
353 break;
354 }
355 case DIV_CMD_FDS_MOD_AUTO:
356 chan[c.chan].autoModNum=c.value>>4;
357 chan[c.chan].autoModDen=c.value&15;
358 chan[c.chan].freqChanged=true;
359 chan[c.chan].modOn=(chan[c.chan].autoModNum && chan[c.chan].autoModDen);
360 rWrite(0x4084,(chan[c.chan].modOn<<7)|0x40|chan[c.chan].modDepth);
361 break;
362 case DIV_CMD_NOTE_PORTA: {
363 int destFreq=NOTE_FREQUENCY(c.value2);
364 bool return2=false;
365 if (destFreq>chan[c.chan].baseFreq) {
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 } else {
372 chan[c.chan].baseFreq-=c.value;
373 if (chan[c.chan].baseFreq<=destFreq) {
374 chan[c.chan].baseFreq=destFreq;
375 return2=true;
376 }
377 }
378 chan[c.chan].freqChanged=true;
379 if (return2) {
380 chan[c.chan].inPorta=false;
381 return 2;
382 }
383 break;
384 }
385 case DIV_CMD_LEGATO:
386 if (c.chan==3) break;
387 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
388 chan[c.chan].freqChanged=true;
389 chan[c.chan].note=c.value;
390 break;
391 case DIV_CMD_PRE_PORTA:
392 if (chan[c.chan].active && c.value2) {
393 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_FDS));
394 }
395 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_FREQUENCY(chan[c.chan].note);
396 chan[c.chan].inPorta=c.value;
397 break;
398 case DIV_CMD_GET_VOLMAX:
399 return 32;
400 break;
401 case DIV_CMD_MACRO_OFF:
402 chan[c.chan].std.mask(c.value,true);
403 break;
404 case DIV_CMD_MACRO_ON:
405 chan[c.chan].std.mask(c.value,false);
406 break;
407 case DIV_CMD_MACRO_RESTART:
408 chan[c.chan].std.restart(c.value);
409 break;
410 default:
411 break;
412 }
413 return 1;
414 }
415
416 void DivPlatformFDS::muteChannel(int ch, bool mute) {
417 isMuted[ch]=mute;
418 }
419
420 void DivPlatformFDS::forceIns() {
421 for (int i=0; i<1; i++) {
422 chan[i].insChanged=true;
423 chan[i].prevFreq=65535;
424 }
425 updateWave();
426 }
427
428 void* DivPlatformFDS::getChanState(int ch) {
429 return &chan[ch];
430 }
431
432 DivMacroInt* DivPlatformFDS::getChanMacroInt(int ch) {
433 return &chan[ch].std;
434 }
435
436 DivDispatchOscBuffer* DivPlatformFDS::getOscBuffer(int ch) {
437 return oscBuf;
438 }
439
440 unsigned char* DivPlatformFDS::getRegisterPool() {
441 return regPool;
442 }
443
444 int DivPlatformFDS::getRegisterPoolSize() {
445 return 128;
446 }
447
448 void DivPlatformFDS::reset() {
449 for (int i=0; i<1; i++) {
450 chan[i]=DivPlatformFDS::Channel();
451 chan[i].std.setEngine(parent);
452 }
453 ws.setEngine(parent);
454 ws.init(NULL,64,63,false);
455 if (dumpWrites) {
456 addWrite(0xffffffff,0);
457 }
458
459 if (useNP) {
460 fds_NP->Reset();
461 } else {
462 fds_reset(fds);
463 }
464 memset(regPool,0,128);
465
466 rWrite(0x4023,0);
467 rWrite(0x4023,0x83);
468 rWrite(0x4089,0);
469 }
470
471 bool DivPlatformFDS::keyOffAffectsArp(int ch) {
472 return true;
473 }
474
475 void DivPlatformFDS::setNSFPlay(bool use) {
476 useNP=use;
477 }
478
479 void DivPlatformFDS::setFlags(const DivConfig& flags) {
480 int clockSel=flags.getInt("clockSel",0);
481 if (clockSel==2) { // Dendy
482 chipClock=COLOR_PAL*2.0/5.0;
483 } else if (clockSel==1) { // PAL
484 chipClock=COLOR_PAL*3.0/8.0;
485 } else { // NTSC
486 chipClock=COLOR_NTSC/2.0;
487 }
488 CHECK_CUSTOM_CLOCK;
489 if (useNP) {
490 rate=chipClock/16;
491 oscBuf->setRate(rate);
492 fds_NP->SetClock(chipClock);
493 fds_NP->SetRate(rate);
494 } else {
495 rate=chipClock;
496 oscBuf->setRate(rate);
497 }
498 }
499
500 void DivPlatformFDS::notifyInsDeletion(void* ins) {
501 for (int i=0; i<1; i++) {
502 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
503 }
504 }
505
506 float DivPlatformFDS::getPostAmp() {
507 return useNP?2.0f:1.0f;
508 }
509
510 void DivPlatformFDS::poke(unsigned int addr, unsigned short val) {
511 rWrite(addr,val);
512 }
513
514 void DivPlatformFDS::poke(std::vector<DivRegWrite>& wlist) {
515 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
516 }
517
518 int DivPlatformFDS::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
519 parent=p;
520 dumpWrites=false;
521 skipRegisterWrites=false;
522 writeOscBuf=0;
523 if (useNP) {
524 fds_NP=new xgm::NES_FDS;
525 } else {
526 fds=new struct _fds;
527 }
528 oscBuf=new DivDispatchOscBuffer;
529 for (int i=0; i<1; i++) {
530 isMuted[i]=false;
531 }
532 setFlags(flags);
533
534 reset();
535 return 1;
536 }
537
538 void DivPlatformFDS::quit() {
539 delete oscBuf;
540 if (useNP) {
541 delete fds_NP;
542 } else {
543 delete fds;
544 }
545 }
546
547 DivPlatformFDS::~DivPlatformFDS() {
548 }