Debian Codegraph @

src/engine/platform/snes.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 "snes.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include "furIcons.h"
24 #include <math.h>
25
26 #define CHIP_FREQBASE 131072
27
28 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
29 #define chWrite(c,a,v) {rWrite((a)+(c)*16,v)}
30 #define rWriteDelay(a,v,d) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v,d)); if (dumpWrites) {addWrite(a,v);} }
31 #define chWriteDelay(c,a,v,d) {rWrite((a)+(c)*16,v,d)}
32 #define sampleTableAddr(c) (sampleTableBase+(c)*4)
33 #define waveTableAddr(c) (sampleTableBase+8*4+(c)*9*16)
34
35 const char* regCheatSheetSNESDSP[]={
36 "VxVOLL", "x0",
37 "VxVOLR", "x1",
38 "VxPITCHL", "x2",
39 "VxPITCHH", "x3",
40 "VxSRCN", "x4",
41 "VxADSR1", "x5",
42 "VxADSR2", "x6",
43 "VxGAIN", "x7",
44 "VxENVX", "x8",
45 "VxOUTX", "x9",
46 "FIRx", "xF",
47
48 "MVOLL", "0C",
49 "MVOLR", "1C",
50 "EVOLL", "2C",
51 "EVOLR", "3C",
52 "KON", "4C",
53 "KOFF", "5C",
54 "FLG", "6C",
55 "ENDX", "7C",
56
57 "EFB", "0D",
58 "PMON", "2D",
59 "NON", "3D",
60 "EON", "4D",
61 "DIR", "5D",
62 "ESA", "6D",
63 "EDL", "7D",
64 NULL
65 };
66
67 const char** DivPlatformSNES::getRegisterSheet() {
68 return regCheatSheetSNESDSP;
69 }
70
71 void DivPlatformSNES::acquire(short** buf, size_t len) {
72 short out[2];
73 short chOut[16];
74 for (int i=0; i<8; i++) {
75 oscBuf[i]->begin(len);
76 }
77 for (size_t h=0; h<len; h++) {
78 if (--delay<=0) {
79 delay=0;
80 if (!writes.empty()) {
81 QueuedWrite w=writes.front();
82 dsp.write(w.addr,w.val);
83 regPool[w.addr&0x7f]=w.val;
84 writes.pop();
85 delay=w.delay;
86 }
87 }
88 dsp.set_output(out,1);
89 dsp.run(32);
90 dsp.get_voice_outputs(chOut);
91 buf[0][h]=out[0];
92 buf[1][h]=out[1];
93 for (int i=0; i<8; i++) {
94 int next=(3*(chOut[i*2]+chOut[i*2+1]))>>2;
95 if (next<-32768) next=-32768;
96 if (next>32767) next=32767;
97 next=(next*254)/MAX(1,globalVolL+globalVolR);
98 if (next<-32768) next=-32768;
99 if (next>32767) next=32767;
100 oscBuf[i]->putSample(h,next>>1);
101 }
102 }
103 for (int i=0; i<8; i++) {
104 oscBuf[i]->end(len);
105 }
106 }
107
108 void DivPlatformSNES::tick(bool sysTick) {
109 // KON/KOFF can't be written several times per one sample
110 // so they have to be accumulated
111 unsigned char kon=0;
112 unsigned char koff=0;
113 for (int i=0; i<8; i++) {
114 chan[i].std.next();
115 if (chan[i].std.vol.had) {
116 chan[i].outVol=VOL_SCALE_LINEAR(chan[i].vol&127,MIN(127,chan[i].std.vol.val),127);
117 }
118 if (NEW_ARP_STRAT) {
119 chan[i].handleArp();
120 } else if (chan[i].std.arp.had) {
121 if (!chan[i].inPorta) {
122 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
123 }
124 chan[i].freqChanged=true;
125 }
126 if (chan[i].std.duty.had) {
127 noiseFreq=chan[i].std.duty.val;
128 writeControl=true;
129 }
130 if (chan[i].useWave && chan[i].std.wave.had) {
131 if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) {
132 chan[i].wave=chan[i].std.wave.val;
133 chan[i].ws.changeWave1(chan[i].wave);
134 }
135 }
136 if (chan[i].std.pitch.had) {
137 if (chan[i].std.pitch.mode) {
138 chan[i].pitch2+=chan[i].std.pitch.val;
139 CLAMP_VAR(chan[i].pitch2,-32768,32767);
140 } else {
141 chan[i].pitch2=chan[i].std.pitch.val;
142 }
143 chan[i].freqChanged=true;
144 }
145 if (chan[i].std.panL.had) {
146 chan[i].panL=chan[i].std.panL.val&0x7f;
147 }
148 if (chan[i].std.panR.had) {
149 chan[i].panR=chan[i].std.panR.val&0x7f;
150 }
151 bool hasInverted=false;
152 if (chan[i].std.ex1.had) {
153 if (chan[i].invertL!=(bool)(chan[i].std.ex1.val&16)) {
154 chan[i].invertL=chan[i].std.ex1.val&16;
155 hasInverted=true;
156 }
157 if (chan[i].invertR!=(bool)(chan[i].std.ex1.val&8)) {
158 chan[i].invertR=chan[i].std.ex1.val&8;
159 hasInverted=true;
160 }
161 if (chan[i].pitchMod!=(bool)(chan[i].std.ex1.val&4)) {
162 chan[i].pitchMod=chan[i].std.ex1.val&4;
163 writePitchMod=true;
164 }
165 if (chan[i].echo!=(bool)(chan[i].std.ex1.val&2)) {
166 chan[i].echo=chan[i].std.ex1.val&2;
167 writeEcho=true;
168 }
169 if (chan[i].noise!=(bool)(chan[i].std.ex1.val&1)) {
170 chan[i].noise=chan[i].std.ex1.val&1;
171 writeNoise=true;
172 }
173 }
174 if (chan[i].std.vol.had || chan[i].std.panL.had || chan[i].std.panR.had || hasInverted) {
175 chan[i].shallWriteVol=true;
176 }
177 if (chan[i].std.ex2.had) {
178 if (chan[i].std.ex2.val&0x80) {
179 switch (chan[i].std.ex2.val&0x60) {
180 case 0x00:
181 chan[i].state.gainMode=DivInstrumentSNES::GAIN_MODE_DEC_LINEAR;
182 break;
183 case 0x20:
184 chan[i].state.gainMode=DivInstrumentSNES::GAIN_MODE_DEC_LOG;
185 break;
186 case 0x40:
187 chan[i].state.gainMode=DivInstrumentSNES::GAIN_MODE_INC_LINEAR;
188 break;
189 case 0x60:
190 chan[i].state.gainMode=DivInstrumentSNES::GAIN_MODE_INC_INVLOG;
191 break;
192 }
193 chan[i].state.gain=chan[i].std.ex2.val&31;
194 } else {
195 chan[i].state.gainMode=DivInstrumentSNES::GAIN_MODE_DIRECT;
196 chan[i].state.gain=chan[i].std.ex2.val&127;
197 }
198 chan[i].shallWriteEnv=true;
199 }
200 if (chan[i].setPos) {
201 // force keyon
202 chan[i].keyOn=true;
203 chan[i].setPos=false;
204 } else {
205 chan[i].audPos=0;
206 }
207 if (chan[i].useWave && chan[i].active) {
208 if (chan[i].ws.tick()) {
209 updateWave(i);
210 }
211 }
212 }
213 for (int i=0; i<8; i++) {
214 // TODO: if wavetable length is higher than 32, we lose precision!
215 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
216 DivSample* s=parent->getSample(chan[i].sample);
217 double off=(s->centerRate>=1)?((double)s->centerRate/parent->getCenterRate()):1.0;
218 if (chan[i].useWave) off=(double)chan[i].wtLen/32.0;
219 chan[i].freq=(unsigned int)(off*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));
220 if (chan[i].freq>16383) chan[i].freq=16383;
221 if (chan[i].keyOn) {
222 unsigned int start, end, loop;
223 unsigned short tabAddr=sampleTableAddr(i);
224 if (chan[i].useWave) {
225 start=waveTableAddr(i);
226 loop=start;
227 } else if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
228 start=sampleOff[chan[i].sample];
229 end=MIN(start+MAX(s->lengthBRR+((s->loop && s->depth!=DIV_SAMPLE_DEPTH_BRR)?9:0),1),getSampleMemCapacity());
230 loop=MAX(start,end-1);
231 if (chan[i].audPos>0) {
232 start=start+MIN(chan[i].audPos/16*9,end-start);
233 }
234 if (s->isLoopable()) {
235 loop=((s->depth!=DIV_SAMPLE_DEPTH_BRR)?9:0)+start+((s->loopStart/16)*9);
236 }
237 } else {
238 start=0;
239 end=0;
240 loop=0;
241 }
242 sampleMem[tabAddr+0]=start&0xff;
243 sampleMem[tabAddr+1]=start>>8;
244 sampleMem[tabAddr+2]=loop&0xff;
245 sampleMem[tabAddr+3]=loop>>8;
246 kon|=(1<<i);
247 koff|=(1<<i);
248 chan[i].keyOn=false;
249 }
250 if (chan[i].keyOff) {
251 if (!chan[i].state.sus) {
252 koff|=(1<<i);
253 }
254 chan[i].keyOff=false;
255 }
256 if (chan[i].freqChanged) {
257 chWrite(i,2,chan[i].freq&0xff);
258 chWrite(i,3,chan[i].freq>>8);
259 chan[i].freqChanged=false;
260 }
261 }
262 }
263 if (koff!=0) {
264 // TODO: improve
265 if (antiClick) {
266 for (int i=0; i<8; i++) {
267 if (koff&(1<<i)) {
268 chWrite(i,5,0);
269 chWrite(i,7,0x9f);
270 chan[i].shallWriteEnv=true;
271 }
272 }
273 rWriteDelay(0x7e,0,64);
274 }
275 rWriteDelay(0x5c,koff,8);
276 }
277 if (writeControl) {
278 unsigned char control=(noiseFreq&0x1f)|(echoOn?0:0x20);
279 rWrite(0x6c,control);
280 writeControl=false;
281 }
282 if (writeNoise) {
283 unsigned char noiseBits=(
284 (chan[0].noise?1:0)|
285 (chan[1].noise?2:0)|
286 (chan[2].noise?4:0)|
287 (chan[3].noise?8:0)|
288 (chan[4].noise?0x10:0)|
289 (chan[5].noise?0x20:0)|
290 (chan[6].noise?0x40:0)|
291 (chan[7].noise?0x80:0)
292 );
293 rWrite(0x3d,noiseBits);
294 writeNoise=false;
295 }
296 if (writePitchMod) {
297 unsigned char pitchModBits=(
298 (chan[0].pitchMod?1:0)|
299 (chan[1].pitchMod?2:0)|
300 (chan[2].pitchMod?4:0)|
301 (chan[3].pitchMod?8:0)|
302 (chan[4].pitchMod?0x10:0)|
303 (chan[5].pitchMod?0x20:0)|
304 (chan[6].pitchMod?0x40:0)|
305 (chan[7].pitchMod?0x80:0)
306 );
307 rWrite(0x2d,pitchModBits);
308 writePitchMod=false;
309 }
310 if (writeEcho) {
311 unsigned char echoBits=(
312 (chan[0].echo?1:0)|
313 (chan[1].echo?2:0)|
314 (chan[2].echo?4:0)|
315 (chan[3].echo?8:0)|
316 (chan[4].echo?0x10:0)|
317 (chan[5].echo?0x20:0)|
318 (chan[6].echo?0x40:0)|
319 (chan[7].echo?0x80:0)
320 );
321 rWrite(0x4d,echoBits);
322 writeEcho=false;
323 }
324 if (writeDryVol) {
325 rWrite(0x0c,dryVolL);
326 rWrite(0x1c,dryVolR);
327 writeDryVol=false;
328 }
329 for (int i=0; i<8; i++) {
330 if (chan[i].shallWriteEnv) {
331 writeEnv(i);
332 chan[i].shallWriteEnv=false;
333 }
334 }
335 if (koff!=0) {
336 rWriteDelay(0x5c,0,8);
337 }
338 for (int i=0; i<8; i++) {
339 if (chan[i].shallWriteVol) {
340 writeOutVol(i);
341 chan[i].shallWriteVol=false;
342 }
343 }
344 if (kon!=0) {
345 rWrite(0x4c,kon);
346 }
347 }
348
349 int DivPlatformSNES::dispatch(DivCommand c) {
350 switch (c.cmd) {
351 case DIV_CMD_NOTE_ON: {
352 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SNES);
353 if (ins->amiga.useWave) {
354 chan[c.chan].useWave=true;
355 chan[c.chan].sampleNote=DIV_NOTE_NULL;
356 chan[c.chan].sampleNoteDelta=0;
357 chan[c.chan].wtLen=ins->amiga.waveLen+1;
358 if (chan[c.chan].insChanged) {
359 if (chan[c.chan].wave<0) {
360 chan[c.chan].wave=0;
361 }
362 chan[c.chan].ws.setWidth(chan[c.chan].wtLen);
363 chan[c.chan].ws.changeWave1(chan[c.chan].wave);
364 }
365 chan[c.chan].ws.init(ins,chan[c.chan].wtLen,15,chan[c.chan].insChanged);
366 } else {
367 if (c.value!=DIV_NOTE_NULL) {
368 chan[c.chan].sample=ins->amiga.getSample(c.value);
369 chan[c.chan].sampleNote=c.value;
370 c.value=ins->amiga.getFreq(c.value);
371 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
372 }
373 chan[c.chan].useWave=false;
374 }
375 if (chan[c.chan].useWave || chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) {
376 chan[c.chan].sample=-1;
377 }
378 if (chan[c.chan].insChanged) {
379 chan[c.chan].state=ins->snes;
380 }
381 chan[c.chan].active=true;
382 if (chan[c.chan].insChanged || chan[c.chan].state.sus) {
383 chan[c.chan].shallWriteEnv=true;
384 }
385 if (c.value!=DIV_NOTE_NULL) {
386 chan[c.chan].baseFreq=round(NOTE_FREQUENCY(c.value));
387 chan[c.chan].freqChanged=true;
388 chan[c.chan].note=c.value;
389 }
390 chan[c.chan].keyOn=true;
391 chan[c.chan].macroInit(ins);
392 // this is the fix. it needs testing.
393 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
394 if (chan[c.chan].outVol!=chan[c.chan].vol) chan[c.chan].shallWriteVol=true;
395 chan[c.chan].outVol=chan[c.chan].vol;
396 }
397 chan[c.chan].insChanged=false;
398 break;
399 }
400 case DIV_CMD_NOTE_OFF:
401 chan[c.chan].active=false;
402 chan[c.chan].keyOff=true;
403 chan[c.chan].keyOn=false;
404 if (chan[c.chan].state.sus) {
405 chan[c.chan].shallWriteEnv=true;
406 } else {
407 chan[c.chan].macroInit(NULL);
408 }
409 break;
410 case DIV_CMD_NOTE_OFF_ENV:
411 chan[c.chan].active=false;
412 chan[c.chan].keyOff=true;
413 chan[c.chan].keyOn=false;
414 if (chan[c.chan].state.sus) {
415 chan[c.chan].shallWriteEnv=true;
416 }
417 chan[c.chan].std.release();
418 break;
419 case DIV_CMD_ENV_RELEASE:
420 chan[c.chan].std.release();
421 break;
422 case DIV_CMD_INSTRUMENT:
423 if (chan[c.chan].ins!=c.value || c.value2==1) {
424 chan[c.chan].ins=c.value;
425 chan[c.chan].insChanged=true;
426 }
427 break;
428 case DIV_CMD_VOLUME:
429 if (chan[c.chan].vol!=c.value) {
430 chan[c.chan].vol=c.value;
431 if (!chan[c.chan].std.vol.has) {
432 chan[c.chan].outVol=c.value;
433 chan[c.chan].shallWriteVol=true;
434 }
435 }
436 break;
437 case DIV_CMD_GET_VOLUME:
438 return chan[c.chan].vol;
439 break;
440 case DIV_CMD_PANNING:
441 chan[c.chan].panL=c.value>>1;
442 chan[c.chan].panR=c.value2>>1;
443 chan[c.chan].shallWriteVol=true;
444 break;
445 case DIV_CMD_PITCH:
446 chan[c.chan].pitch=c.value;
447 chan[c.chan].freqChanged=true;
448 break;
449 case DIV_CMD_WAVE:
450 if (!chan[c.chan].useWave) break;
451 chan[c.chan].wave=c.value;
452 chan[c.chan].ws.changeWave1(chan[c.chan].wave);
453 break;
454 case DIV_CMD_NOTE_PORTA: {
455 int destFreq=round(NOTE_FREQUENCY(c.value2+chan[c.chan].sampleNoteDelta));
456 bool return2=false;
457 if (destFreq>chan[c.chan].baseFreq) {
458 chan[c.chan].baseFreq+=c.value;
459 if (chan[c.chan].baseFreq>=destFreq) {
460 chan[c.chan].baseFreq=destFreq;
461 return2=true;
462 }
463 } else {
464 chan[c.chan].baseFreq-=c.value;
465 if (chan[c.chan].baseFreq<=destFreq) {
466 chan[c.chan].baseFreq=destFreq;
467 return2=true;
468 }
469 }
470 chan[c.chan].freqChanged=true;
471 if (return2) {
472 chan[c.chan].inPorta=false;
473 return 2;
474 }
475 break;
476 }
477 case DIV_CMD_LEGATO: {
478 chan[c.chan].baseFreq=round(NOTE_FREQUENCY(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))));
479 chan[c.chan].freqChanged=true;
480 chan[c.chan].note=c.value;
481 break;
482 }
483 case DIV_CMD_PRE_PORTA:
484 if (chan[c.chan].active && c.value2) {
485 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_SNES));
486 }
487 chan[c.chan].inPorta=c.value;
488 break;
489 case DIV_CMD_SAMPLE_POS:
490 chan[c.chan].audPos=c.value;
491 chan[c.chan].setPos=true;
492 break;
493 case DIV_CMD_STD_NOISE_MODE:
494 chan[c.chan].noise=c.value;
495 writeNoise=true;
496 break;
497 case DIV_CMD_SNES_PITCH_MOD:
498 chan[c.chan].pitchMod=c.value;
499 writePitchMod=true;
500 break;
501 case DIV_CMD_SNES_INVERT:
502 chan[c.chan].invertL=(c.value>>4);
503 chan[c.chan].invertR=c.value&15;
504 chan[c.chan].shallWriteVol=true;
505 break;
506 case DIV_CMD_SNES_GAIN_MODE:
507 if (c.value) {
508 chan[c.chan].state.useEnv=false;
509 switch (c.value) {
510 case 1:
511 chan[c.chan].state.gainMode=DivInstrumentSNES::GAIN_MODE_DIRECT;
512 break;
513 case 2:
514 chan[c.chan].state.gainMode=DivInstrumentSNES::GAIN_MODE_DEC_LINEAR;
515 break;
516 case 3:
517 chan[c.chan].state.gainMode=DivInstrumentSNES::GAIN_MODE_DEC_LOG;
518 break;
519 case 4:
520 chan[c.chan].state.gainMode=DivInstrumentSNES::GAIN_MODE_INC_LINEAR;
521 break;
522 case 5:
523 chan[c.chan].state.gainMode=DivInstrumentSNES::GAIN_MODE_INC_INVLOG;
524 break;
525 }
526 } else {
527 chan[c.chan].state.useEnv=true;
528 }
529 chan[c.chan].shallWriteEnv=true;
530 break;
531 case DIV_CMD_SNES_GAIN:
532 if (chan[c.chan].state.gainMode==DivInstrumentSNES::GAIN_MODE_DIRECT) {
533 chan[c.chan].state.gain=c.value&0x7f;
534 } else {
535 chan[c.chan].state.gain=c.value&0x1f;
536 }
537 if (!chan[c.chan].state.useEnv) chan[c.chan].shallWriteEnv=true;
538 break;
539 case DIV_CMD_STD_NOISE_FREQ:
540 noiseFreq=c.value&0x1f;
541 writeControl=true;
542 break;
543 case DIV_CMD_FM_AR:
544 chan[c.chan].state.a=c.value&15;
545 if (chan[c.chan].state.useEnv) chan[c.chan].shallWriteEnv=true;
546 break;
547 case DIV_CMD_FM_DR:
548 chan[c.chan].state.d=c.value&7;
549 if (chan[c.chan].state.useEnv) chan[c.chan].shallWriteEnv=true;
550 break;
551 case DIV_CMD_FM_SL:
552 chan[c.chan].state.s=c.value&7;
553 if (chan[c.chan].state.useEnv) chan[c.chan].shallWriteEnv=true;
554 break;
555 case DIV_CMD_FM_RR:
556 chan[c.chan].state.r=c.value&0x1f;
557 if (chan[c.chan].state.useEnv) chan[c.chan].shallWriteEnv=true;
558 break;
559 case DIV_CMD_SNES_ECHO:
560 chan[c.chan].echo=c.value;
561 writeEcho=true;
562 break;
563 case DIV_CMD_SNES_ECHO_DELAY: {
564 echoDelay=c.value&15;
565 unsigned char esa=0xf8-(echoDelay<<3);
566 if (echoOn) {
567 rWrite(0x6d,esa);
568 rWrite(0x7d,echoDelay);
569 }
570 break;
571 }
572 case DIV_CMD_SNES_ECHO_ENABLE:
573 echoOn=c.value;
574 initEcho();
575 break;
576 case DIV_CMD_SNES_ECHO_FEEDBACK:
577 echoFeedback=c.value;
578 if (echoOn) {
579 rWrite(0x0d,echoFeedback);
580 }
581 break;
582 case DIV_CMD_SNES_ECHO_FIR:
583 echoFIR[c.value&7]=c.value2;
584 if (echoOn) {
585 rWrite(0x0f+((c.value&7)<<4),echoFIR[c.value&7]);
586 }
587 break;
588 case DIV_CMD_SNES_ECHO_VOL_LEFT:
589 echoVolL=c.value;
590 if (echoOn) {
591 rWrite(0x2c,echoVolL);
592 }
593 break;
594 case DIV_CMD_SNES_ECHO_VOL_RIGHT:
595 echoVolR=c.value;
596 if (echoOn) {
597 rWrite(0x3c,echoVolR);
598 }
599 break;
600 case DIV_CMD_SNES_GLOBAL_VOL_LEFT:
601 dryVolL=c.value;
602 writeDryVol=true;
603 break;
604 case DIV_CMD_SNES_GLOBAL_VOL_RIGHT:
605 dryVolR=c.value;
606 writeDryVol=true;
607 break;
608 case DIV_CMD_GET_VOLMAX:
609 return 127;
610 break;
611 case DIV_CMD_MACRO_OFF:
612 chan[c.chan].std.mask(c.value,true);
613 break;
614 case DIV_CMD_MACRO_ON:
615 chan[c.chan].std.mask(c.value,false);
616 break;
617 case DIV_CMD_MACRO_RESTART:
618 chan[c.chan].std.restart(c.value);
619 break;
620 default:
621 break;
622 }
623 return 1;
624 }
625
626 void DivPlatformSNES::updateWave(int ch) {
627 // Due to the overflow bug in hardware's resampler, the written amplitude here is half of maximum
628 unsigned short pos=waveTableAddr(ch);
629 for (int i=0; i<chan[ch].wtLen/16; i++) {
630 sampleMem[pos++]=0xb0;
631 for (int j=0; j<8; j++) {
632 int nibble1=(chan[ch].ws.output[i*16+j*2]-8)&15;
633 int nibble2=(chan[ch].ws.output[i*16+j*2+1]-8)&15;
634 sampleMem[pos++]=(nibble1<<4)|nibble2;
635 }
636 }
637 sampleMem[pos-9]=0xb3; // mark loop
638 }
639
640 void DivPlatformSNES::writeOutVol(int ch) {
641 int outL=0;
642 int outR=0;
643 if (!isMuted[ch]) {
644 outL=(globalVolL*((chan[ch].outVol*chan[ch].panL)/127))/127;
645 outR=(globalVolR*((chan[ch].outVol*chan[ch].panR)/127))/127;
646 if (chan[ch].invertL) outL=-outL;
647 if (chan[ch].invertR) outR=-outR;
648 }
649 chWrite(ch,0,outL);
650 chWrite(ch,1,outR);
651 }
652
653 void DivPlatformSNES::writeEnv(int ch) {
654 if (chan[ch].state.useEnv) {
655 if (chan[ch].state.sus) {
656 if (chan[ch].active) {
657 chWrite(ch,5,chan[ch].state.a|(chan[ch].state.d<<4)|0x80);
658 chWrite(ch,6,(chan[ch].state.s<<5)|(chan[ch].state.d2&31));
659 } else {
660 switch (chan[ch].state.sus) {
661 case 1: // dec linear
662 chWrite(ch,7,0x80|chan[ch].state.r);
663 chWrite(ch,5,0);
664 break;
665 case 2: // dec exp
666 chWrite(ch,7,0xa0|chan[ch].state.r);
667 chWrite(ch,5,0);
668 break;
669 case 3: // update r
670 chWrite(ch,6,(chan[ch].state.s<<5)|(chan[ch].state.r&31));
671 break;
672 default: // what?
673 break;
674 }
675 }
676 } else {
677 chWrite(ch,5,chan[ch].state.a|(chan[ch].state.d<<4)|0x80);
678 chWrite(ch,6,chan[ch].state.r|(chan[ch].state.s<<5));
679 }
680 } else {
681 chWrite(ch,5,0);
682 switch (chan[ch].state.gainMode) {
683 case DivInstrumentSNES::GAIN_MODE_DIRECT:
684 chWrite(ch,7,chan[ch].state.gain&127);
685 break;
686 case DivInstrumentSNES::GAIN_MODE_DEC_LINEAR:
687 chWrite(ch,7,0x80|(chan[ch].state.gain&31));
688 break;
689 case DivInstrumentSNES::GAIN_MODE_INC_LINEAR:
690 chWrite(ch,7,0xc0|(chan[ch].state.gain&31));
691 break;
692 case DivInstrumentSNES::GAIN_MODE_DEC_LOG:
693 chWrite(ch,7,0xa0|(chan[ch].state.gain&31));
694 break;
695 case DivInstrumentSNES::GAIN_MODE_INC_INVLOG:
696 chWrite(ch,7,0xe0|(chan[ch].state.gain&31));
697 break;
698 }
699 }
700 }
701
702 void DivPlatformSNES::muteChannel(int ch, bool mute) {
703 isMuted[ch]=mute;
704 chan[ch].shallWriteVol=true;
705 }
706
707 void DivPlatformSNES::forceIns() {
708 for (int i=0; i<8; i++) {
709 chan[i].insChanged=true;
710 chan[i].freqChanged=true;
711 chan[i].sample=-1;
712 if (chan[i].active && chan[i].useWave) {
713 updateWave(i);
714 }
715 writeOutVol(i);
716 }
717 writeControl=true;
718 writeNoise=true;
719 writePitchMod=true;
720 writeEcho=true;
721 writeDryVol=true;
722 initEcho();
723 }
724
725 void* DivPlatformSNES::getChanState(int ch) {
726 return &chan[ch];
727 }
728
729 DivMacroInt* DivPlatformSNES::getChanMacroInt(int ch) {
730 return &chan[ch].std;
731 }
732
733 unsigned short DivPlatformSNES::getPan(int ch) {
734 return (chan[ch].panL<<8)|chan[ch].panR;
735 }
736
737 void DivPlatformSNES::getPaired(int ch, std::vector<DivChannelPair>& ret) {
738 if (chan[ch].pitchMod) {
739 ret.push_back(DivChannelPair(_("mod"),(ch-1)&7));
740 }
741 }
742
743 DivChannelModeHints DivPlatformSNES::getModeHints(int ch) {
744 DivChannelModeHints ret;
745 ret.count=1;
746 ret.hint[0]="-";
747 ret.type[0]=0;
748
749 const SPC_DSP::voice_t* v=dsp.get_voice(ch);
750 if (v!=NULL) {
751 if (v->regs[5]&128) {
752 switch (v->env_mode) {
753 case SPC_DSP::env_attack:
754 ret.hint[0]=ICON_FUR_ADSR_A;
755 ret.type[0]=12;
756 break;
757 case SPC_DSP::env_decay:
758 ret.hint[0]=ICON_FUR_ADSR_D;
759 ret.type[0]=13;
760 break;
761 case SPC_DSP::env_sustain:
762 ret.hint[0]=ICON_FUR_ADSR_S;
763 ret.type[0]=14;
764 break;
765 case SPC_DSP::env_release:
766 ret.hint[0]=ICON_FUR_ADSR_R;
767 ret.type[0]=15;
768 break;
769 }
770 } else {
771 if (v->regs[7]&128) {
772 switch (v->regs[7]&0x60) {
773 case 0:
774 ret.hint[0]=ICON_FUR_DEC_LINEAR;
775 ret.type[0]=16;
776 break;
777 case 32:
778 ret.hint[0]=ICON_FUR_DEC_EXP;
779 ret.type[0]=17;
780 break;
781 case 64:
782 ret.hint[0]=ICON_FUR_INC_LINEAR;
783 ret.type[0]=18;
784 break;
785 case 96:
786 ret.hint[0]=ICON_FUR_INC_BENT;
787 ret.type[0]=19;
788 break;
789 }
790 } else {
791 ret.hint[0]=ICON_FUR_VOL_DIRECT;
792 ret.type[0]=20;
793 }
794 }
795 }
796
797 return ret;
798 }
799
800 DivSamplePos DivPlatformSNES::getSamplePos(int ch) {
801 if (ch>=8) return DivSamplePos();
802 if (!chan[ch].active) return DivSamplePos();
803 if (chan[ch].sample<0 || chan[ch].sample>=parent->song.sampleLen) return DivSamplePos();
804 const SPC_DSP::voice_t* v=dsp.get_voice(ch);
805 // TODO: fix?
806 if (sampleMem[v->brr_addr&0xffff]==0) return DivSamplePos();
807 return DivSamplePos(
808 chan[ch].sample,
809 ((v->brr_addr-sampleOff[chan[ch].sample])*16/9)+v->brr_offset,
810 (chan[ch].freq*125)/16
811 );
812 }
813
814 DivDispatchOscBuffer* DivPlatformSNES::getOscBuffer(int ch) {
815 return oscBuf[ch];
816 }
817
818 unsigned char* DivPlatformSNES::getRegisterPool() {
819 // get states from emulator
820 for (int i=0; i<0x80; i+=0x10) {
821 regPool[i+8]=dsp.read(i+8);
822 regPool[i+9]=dsp.read(i+9);
823 }
824 regPool[0x7c]=dsp.read(0x7c); // ENDX
825 return regPool;
826 }
827
828 int DivPlatformSNES::getRegisterPoolSize() {
829 return 128;
830 }
831
832 void DivPlatformSNES::initEcho() {
833 unsigned char esa=0xf8-(echoDelay<<3);
834 unsigned char control=(noiseFreq&0x1f)|(echoOn?0:0x20);
835 if (echoOn) {
836 rWrite(0x6d,esa);
837 rWrite(0x7d,echoDelay);
838 rWrite(0x0d,echoFeedback);
839 rWrite(0x2c,echoVolL);
840 rWrite(0x3c,echoVolR);
841 for (int i=0; i<8; i++) {
842 rWrite(0x0f+(i<<4),echoFIR[i]);
843 }
844 rWrite(0x6c,control);
845 } else {
846 rWrite(0x2c,0);
847 rWrite(0x3c,0);
848 rWrite(0x6c,control);
849 rWrite(0x7d,0);
850 rWrite(0x6d,0xff);
851 }
852
853 for (DivMemoryEntry& i: memCompo.entries) {
854 if (i.type==DIV_MEMORY_ECHO) {
855 i.begin=(0xf800-echoDelay*2048);
856 }
857 }
858 memCompo.used=sampleMemLen+echoDelay*2048;
859 }
860
861 void DivPlatformSNES::reset() {
862 writes.clear();
863
864 memcpy(sampleMem,copyOfSampleMem,65536);
865 dsp.init(sampleMem);
866 dsp.set_output(NULL,0);
867 dsp.setupInterpolation(!interpolationOff);
868
869 memset(regPool,0,128);
870 // this can't be 0 or channel 1 won't play
871 // this can't be 0x100 either as that's used by SPC700 page 1 and the stack
872 // this may not even be 0x200 as some space will be taken by the playback routine and variables
873 sampleTableBase=0x200;
874 rWrite(0x5d,sampleTableBase>>8);
875 rWrite(0x0c,127); // global volume left
876 rWrite(0x1c,127); // global volume right
877 rWrite(0x6c,0); // get DSP out of reset
878 for (int i=0; i<8; i++) {
879 chan[i]=Channel();
880 chan[i].std.setEngine(parent);
881 chan[i].ws.setEngine(parent);
882 chan[i].ws.init(NULL,32,15);
883 writeOutVol(i);
884 chWrite(i,4,i); // source number
885 }
886 writeControl=false;
887 writeNoise=false;
888 writePitchMod=false;
889 writeEcho=true;
890 writeDryVol=false;
891
892 dryVolL=127;
893 dryVolR=127;
894
895 echoDelay=initEchoDelay;
896 echoFeedback=initEchoFeedback;
897 echoFIR[0]=initEchoFIR[0];
898 echoFIR[1]=initEchoFIR[1];
899 echoFIR[2]=initEchoFIR[2];
900 echoFIR[3]=initEchoFIR[3];
901 echoFIR[4]=initEchoFIR[4];
902 echoFIR[5]=initEchoFIR[5];
903 echoFIR[6]=initEchoFIR[6];
904 echoFIR[7]=initEchoFIR[7];
905 echoVolL=initEchoVolL;
906 echoVolR=initEchoVolR;
907 echoOn=initEchoOn;
908
909 for (int i=0; i<8; i++) {
910 if (initEchoMask&(1<<i)) {
911 chan[i].echo=true;
912 }
913 }
914
915 initEcho();
916 }
917
918 int DivPlatformSNES::getOutputCount() {
919 return 2;
920 }
921
922 void DivPlatformSNES::notifyInsChange(int ins) {
923 for (int i=0; i<8; i++) {
924 if (chan[i].ins==ins) {
925 chan[i].insChanged=true;
926 }
927 }
928 }
929
930 void DivPlatformSNES::notifyWaveChange(int wave) {
931 for (int i=0; i<8; i++) {
932 if (chan[i].useWave && chan[i].wave==wave) {
933 chan[i].ws.changeWave1(wave);
934 if (chan[i].active) {
935 updateWave(i);
936 }
937 }
938 }
939 }
940
941 void DivPlatformSNES::notifyInsDeletion(void* ins) {
942 for (int i=0; i<8; i++) {
943 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
944 }
945 }
946
947 void DivPlatformSNES::poke(unsigned int addr, unsigned short val) {
948 rWrite(addr,val);
949 }
950
951 void DivPlatformSNES::poke(std::vector<DivRegWrite>& wlist) {
952 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
953 }
954
955 const void* DivPlatformSNES::getSampleMem(int index) {
956 return index == 0 ? sampleMem : NULL;
957 }
958
959 size_t DivPlatformSNES::getSampleMemCapacity(int index) {
960 return index == 0 ? (0xf800-echoDelay*2048) : 0;
961 }
962
963 size_t DivPlatformSNES::getSampleMemUsage(int index) {
964 return index == 0 ? sampleMemLen : 0;
965 }
966
967 bool DivPlatformSNES::isSampleLoaded(int index, int sample) {
968 if (index!=0) return false;
969 if (sample<0 || sample>32767) return false;
970 return sampleLoaded[sample];
971 }
972
973 const DivMemoryComposition* DivPlatformSNES::getMemCompo(int index) {
974 if (index!=0) return NULL;
975 return &memCompo;
976 }
977
978 void DivPlatformSNES::renderSamples(int sysID) {
979 memset(copyOfSampleMem,0,65536);
980 memset(sampleOff,0,32768*sizeof(unsigned int));
981 memset(sampleLoaded,0,32768*sizeof(bool));
982
983 memCompo=DivMemoryComposition();
984 memCompo.name="SPC/DSP Memory";
985
986 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_RESERVED,"State",-1,0,sampleTableBase));
987 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_RESERVED,"Sample Directory",-1,sampleTableBase,sampleTableBase+8*4));
988 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_WAVE_RAM,"Wave RAM",-1,sampleTableBase+8*4,sampleTableBase+8*4+8*9*16));
989
990 // skip past sample table and wavetable buffer
991 size_t memPos=sampleTableBase+8*4+8*9*16;
992 for (int i=0; i<parent->song.sampleLen; i++) {
993 DivSample* s=parent->song.sample[i];
994 if (!s->renderOn[0][sysID]) {
995 sampleOff[i]=0;
996 continue;
997 }
998
999 int length=s->lengthBRR+((s->loop && s->depth!=DIV_SAMPLE_DEPTH_BRR)?9:0);
1000 int actualLength=MIN((int)(getSampleMemCapacity()-memPos)/9*9,length);
1001 if (actualLength>0) {
1002 sampleOff[i]=memPos;
1003 memcpy(&copyOfSampleMem[memPos],s->dataBRR,actualLength);
1004 // inject loop if needed
1005 if (s->loop) {
1006 copyOfSampleMem[memPos+actualLength-9]|=3;
1007 } else {
1008 copyOfSampleMem[memPos+actualLength-9]&=~3;
1009 copyOfSampleMem[memPos+actualLength-9]|=1;
1010 }
1011 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+actualLength));
1012 memPos+=actualLength;
1013 }
1014 if (actualLength<length) {
1015 // terminate the sample
1016 copyOfSampleMem[memPos-9]=1;
1017 logW("out of BRR memory for sample %d!",i);
1018 break;
1019 }
1020 sampleLoaded[i]=true;
1021 }
1022 sampleMemLen=memPos;
1023
1024 // even if the delay is 0, the DSP will still operate the first buffer sample
1025 // so the ARAM buffer size becomes 4 bytes when the delay is 0
1026 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_ECHO,"Echo Buffer",-1,(0xf800-echoDelay*2048),echoDelay==0?0xf804:0xf800));
1027
1028 memCompo.capacity=65536;
1029 memCompo.used=sampleMemLen+echoDelay*2048;
1030 memcpy(sampleMem,copyOfSampleMem,65536);
1031 }
1032
1033 void DivPlatformSNES::setFlags(const DivConfig& flags) {
1034 globalVolL=127-flags.getInt("volScaleL",0);
1035 globalVolR=127-flags.getInt("volScaleR",0);
1036
1037 initEchoOn=flags.getBool("echo",false);
1038 initEchoVolL=flags.getInt("echoVolL",127);
1039 initEchoVolR=flags.getInt("echoVolR",127);
1040 initEchoDelay=flags.getInt("echoDelay",0)&15;
1041 initEchoFeedback=flags.getInt("echoFeedback",0);
1042
1043 initEchoFIR[0]=flags.getInt("echoFilter0",127);
1044 initEchoFIR[1]=flags.getInt("echoFilter1",0);
1045 initEchoFIR[2]=flags.getInt("echoFilter2",0);
1046 initEchoFIR[3]=flags.getInt("echoFilter3",0);
1047 initEchoFIR[4]=flags.getInt("echoFilter4",0);
1048 initEchoFIR[5]=flags.getInt("echoFilter5",0);
1049 initEchoFIR[6]=flags.getInt("echoFilter6",0);
1050 initEchoFIR[7]=flags.getInt("echoFilter7",0);
1051
1052 initEchoMask=flags.getInt("echoMask",0);
1053
1054 interpolationOff=flags.getBool("interpolationOff",false);
1055 antiClick=flags.getBool("antiClick",true);
1056 }
1057
1058 int DivPlatformSNES::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
1059 parent=p;
1060 dumpWrites=false;
1061 skipRegisterWrites=false;
1062 sampleMemLen=0;
1063 chipClock=1024000;
1064 rate=chipClock/32;
1065 for (int i=0; i<8; i++) {
1066 oscBuf[i]=new DivDispatchOscBuffer;
1067 oscBuf[i]->setRate(rate);
1068 isMuted[i]=false;
1069 }
1070 setFlags(flags);
1071 reset();
1072 return 8;
1073 }
1074
1075 void DivPlatformSNES::quit() {
1076 for (int i=0; i<8; i++) {
1077 delete oscBuf[i];
1078 }
1079 }
1080
1081 // initialization of important arrays
1082 DivPlatformSNES::DivPlatformSNES() {
1083 sampleOff=new unsigned int[32768];
1084 sampleLoaded=new bool[32768];
1085 }
1086
1087 DivPlatformSNES::~DivPlatformSNES() {
1088 delete[] sampleOff;
1089 delete[] sampleLoaded;
1090 }