Debian Codegraph @

src/engine/platform/swan.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 "swan.h"
21 #include "../engine.h"
22 #include "furIcons.h"
23 #include "IconsFontAwesome4.h"
24 #include "sound/swan.h"
25 #include <math.h>
26
27 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);}}
28 #define postWrite(a,v) postDACWrites.push(DivRegWrite(a,v));
29
30 #define CHIP_DIVIDER 32
31
32 const char* regCheatSheetWS[]={
33 "CH1_Pitch", "00",
34 "CH2_Pitch", "02",
35 "CH3_Pitch", "04",
36 "CH4_Pitch", "06",
37 "CH1_Vol", "08",
38 "CH2_Vol", "09",
39 "CH3_Vol", "0A",
40 "CH4_Vol", "0B",
41 "Sweep_Value", "0C",
42 "Sweep_Time", "0D",
43 "Noise_Ctrl", "0E",
44 "Wave_Base", "0F",
45 "Channel_Ctrl", "10",
46 "Output_Ctrl", "11",
47 "Noise_Random", "12",
48 "CH2_Voice_Vol", "14",
49 "Test", "15",
50 "Output_Right", "16",
51 "Output_Left", "18",
52 "Output_Mono", "1A",
53 "Wave_Mem", "40",
54 NULL
55 };
56
57 const char** DivPlatformSwan::getRegisterSheet() {
58 return regCheatSheetWS;
59 }
60
61 // Mednafen
62 void DivPlatformSwan::acquireDirect(blip_buffer_t** bb, size_t len) {
63 for (int i=0; i<4; i++) {
64 oscBuf[i]->begin(len);
65 ws_mdfn->oscBuf[i]=oscBuf[i];
66 }
67
68 ws_mdfn->sbuf[0]=bb[0];
69 ws_mdfn->sbuf[1]=bb[1];
70
71 for (size_t h=0; h<len; h++) {
72 ws_mdfn->v30mz_timestamp=h;
73 // heuristic
74 int pcmAdvance=1;
75 if (writes.empty()) {
76 if (!pcm || dacSample==-1) {
77 break;
78 } else {
79 pcmAdvance=len-h;
80 if (dacRate>0) {
81 int remainTime=(rate-dacPeriod+dacRate-1)/dacRate;
82 if (remainTime<pcmAdvance) pcmAdvance=remainTime;
83 if (remainTime<1) pcmAdvance=1;
84 }
85 }
86 }
87
88 // PCM part
89 if (pcm && dacSample!=-1) {
90 dacPeriod+=dacRate*pcmAdvance;
91 while (dacPeriod>=rate) {
92 DivSample* s=parent->getSample(dacSample);
93 if (s->samples<=0 || dacPos>=s->samples) {
94 dacSample=-1;
95 dacPeriod=0;
96 break;
97 }
98 rWrite(0x09,(unsigned char)s->data8[dacPos++]+0x80);
99 if (s->isLoopable() && dacPos>=(unsigned int)s->loopEnd) {
100 dacPos=s->loopStart;
101 } else if (dacPos>=s->samples) {
102 dacSample=-1;
103 }
104 dacPeriod-=rate;
105 }
106 }
107
108 h+=pcmAdvance-1;
109
110 // the rest
111 while (!writes.empty()) {
112 QueuedWrite w=writes.front();
113 regPool[w.addr]=w.val;
114 if (w.addr<0x40) {
115 ws_mdfn->SoundWrite(w.addr|0x80,w.val);
116 } else {
117 ws_mdfn->SoundCheckRAMWrite(w.addr&0x3f);
118 ws_mdfn->RAMWrite(w.addr&0x3f,w.val);
119 }
120 writes.pop();
121 }
122 }
123
124 ws_mdfn->v30mz_timestamp=len;
125 ws_mdfn->SoundUpdate();
126 ws_mdfn->SoundFlush(NULL,0);
127
128 for (int i=0; i<4; i++) {
129 oscBuf[i]->end(len);
130 }
131 }
132
133 // asiekierka
134 void DivPlatformSwan::acquire(short** buf, size_t len) {
135 for (int i=0; i<4; i++) {
136 oscBuf[i]->begin(len);
137 }
138
139 for (size_t h=0; h<len; h++) {
140 // PCM
141 if (pcm && dacSample!=-1) {
142 dacPeriod+=dacRate;
143 while (dacPeriod>=rate) {
144 DivSample* s=parent->getSample(dacSample);
145 if (s->samples<=0 || dacPos>=s->samples) {
146 dacSample=-1;
147 dacPeriod=0;
148 break;
149 }
150 rWrite(0x09,(unsigned char)s->data8[dacPos++]+0x80);
151 if (s->isLoopable() && dacPos>=(unsigned int)s->loopEnd) {
152 dacPos=s->loopStart;
153 } else if (dacPos>=s->samples) {
154 dacSample=-1;
155 }
156 dacPeriod-=rate;
157 }
158 }
159
160 // Register writes
161 while (!writes.empty()) {
162 QueuedWrite w=writes.front();
163 if (w.addr < 0x40) {
164 swan_sound_out(&ws, w.addr|0x80, w.val);
165 } else {
166 ws.wave_ram[w.addr & 0x3f] = w.val;
167 regPool[w.addr]=w.val;
168 }
169 writes.pop();
170 }
171
172 swan_sound_tick(&ws, chipClock / rate);
173 swan_sound_sample(&ws);
174
175 // Update individual channel buffers
176 for (int i = 0; i < 4; i++) {
177 if (isMuted[i]) {
178 oscBuf[i]->putSample(h, 0);
179 } else {
180 oscBuf[i]->putSample(h, (ws.ch_output_left[i] + ws.ch_output_right[i]) << 5);
181 }
182 }
183
184 if (stereo) {
185 buf[0][h] = ws.output_left;
186 buf[1][h] = ws.output_right;
187 } else {
188 buf[0][h] = ((int)ws.output_speaker - 0x80) << 8;
189 }
190 }
191
192 for (int i=0; i<4; i++) {
193 oscBuf[i]->end(len);
194 }
195 }
196
197 void DivPlatformSwan::updateWave(int ch) {
198 unsigned char addr=0x40+ch*16;
199 for (int i=0; i<16; i++) {
200 int nibble1=chan[ch].ws.output[i<<1];
201 int nibble2=chan[ch].ws.output[1+(i<<1)];
202 rWrite(addr+i,nibble1|(nibble2<<4));
203 }
204 }
205
206 void DivPlatformSwan::calcAndWriteOutVol(int ch, int env) {
207 int vl=chan[ch].vol*((chan[ch].pan>>4)&0x0f)*env/225;
208 int vr=chan[ch].vol*(chan[ch].pan&0x0f)*env/225;
209 if (ch==1&&pcm) {
210 vl=(vl>0)?((vl>7)?3:2):0;
211 vr=(vr>0)?((vr>7)?3:2):0;
212 chan[1].outVol=vr|(vl<<2);
213 } else {
214 chan[ch].outVol=vr|(vl<<4);
215 }
216 writeOutVol(ch);
217 }
218
219 void DivPlatformSwan::writeOutVol(int ch) {
220 unsigned char val=isMuted[ch]?0:chan[ch].outVol;
221 if (ch==1&&pcm) {
222 rWrite(0x14,val)
223 } else {
224 rWrite(0x08+ch,val);
225 }
226 }
227
228 void DivPlatformSwan::tick(bool sysTick) {
229 unsigned char sndCtrl=(pcm?0x20:0)|(sweep?0x40:0)|((noise>0)?0x80:0);
230 for (int i=0; i<4; i++) {
231 chan[i].std.next();
232 if (chan[i].std.vol.had) {
233 int env=chan[i].std.vol.val;
234 if(parent->getIns(chan[i].ins,DIV_INS_SWAN)->type==DIV_INS_AMIGA) {
235 env=MIN(env/4,15);
236 }
237 calcAndWriteOutVol(i,env);
238 }
239 if (NEW_ARP_STRAT) {
240 chan[i].handleArp();
241 } else if (chan[i].std.arp.had) {
242 if (!chan[i].inPorta) {
243 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
244 }
245 chan[i].freqChanged=true;
246 }
247 if (chan[i].std.wave.had && !(i==1 && pcm)) {
248 if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) {
249 chan[i].wave=chan[i].std.wave.val;
250 chan[i].ws.changeWave1(chan[i].wave);
251 }
252 }
253 if (chan[i].std.panL.had) {
254 chan[i].pan&=0x0f;
255 chan[i].pan|=(chan[i].std.panL.val&15)<<4;
256 }
257 if (chan[i].std.panR.had) {
258 chan[i].pan&=0xf0;
259 chan[i].pan|=chan[i].std.panR.val&15;
260 }
261 if (chan[i].std.panL.had || chan[i].std.panR.had) {
262 calcAndWriteOutVol(i,chan[i].std.vol.will?chan[i].std.vol.val:15);
263 }
264 if (chan[i].std.pitch.had) {
265 if (chan[i].std.pitch.mode) {
266 chan[i].pitch2+=chan[i].std.pitch.val;
267 CLAMP_VAR(chan[i].pitch2,-32768,32767);
268 } else {
269 chan[i].pitch2=chan[i].std.pitch.val;
270 }
271 chan[i].freqChanged=true;
272 }
273 if (chan[i].active) {
274 sndCtrl|=(1<<i);
275 if (chan[i].ws.tick()) {
276 updateWave(i);
277 }
278 }
279 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
280 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);
281 if (i==1 && pcm && furnaceDac) {
282 double off=1.0;
283 if (dacSample>=0 && dacSample<parent->song.sampleLen) {
284 DivSample* s=parent->getSample(dacSample);
285 if (s->centerRate<1) {
286 off=1.0;
287 } else {
288 off=parent->getCenterRate()/(double)s->centerRate;
289 }
290 }
291 dacRate=((double)chipClock/2)/MAX(1,off*chan[i].freq);
292 if (dumpWrites) postWrite(0xffff0001,dacRate);
293 }
294 if (chan[i].freq>2048) chan[i].freq=2048;
295 if (chan[i].freq<1) chan[i].freq=1;
296 int rVal=2048-chan[i].freq;
297 rWrite(i*2,rVal&0xff);
298 rWrite(i*2+1,rVal>>8);
299 if (chan[i].keyOn) {
300 if (!chan[i].std.vol.will) {
301 calcAndWriteOutVol(i,15);
302 }
303 chan[i].keyOn=false;
304 }
305 if (chan[i].keyOff) {
306 chan[i].keyOff=false;
307 }
308 chan[i].freqChanged=false;
309 }
310 }
311 if (chan[3].std.duty.had) {
312 if (noise!=chan[3].std.duty.val) {
313 noise=chan[3].std.duty.val;
314 if (noise>0) {
315 rWrite(0x0e,((noise-1)&0x07)|0x18);
316 sndCtrl|=0x80;
317 } else {
318 sndCtrl&=~0x80;
319 }
320 }
321 }
322 if (chan[3].std.phaseReset.had) {
323 if (noise>0) {
324 rWrite(0x0e,((noise-1)&0x07)|0x18);
325 sndCtrl|=0x80;
326 } else {
327 sndCtrl&=~0x80;
328 }
329 }
330 unsigned char origSndCtrl=sndCtrl;
331 bool phaseResetHappens=false;
332 for (int i=0; i<4; i++) {
333 if (chan[i].std.phaseReset.had) {
334 phaseResetHappens=true;
335 sndCtrl&=~(1<<i);
336 }
337 }
338 if (phaseResetHappens) {
339 rWrite(0x10,sndCtrl);
340 sndCtrl=origSndCtrl;
341 }
342 rWrite(0x10,sndCtrl);
343
344 while (!postDACWrites.empty()) {
345 const DivRegWrite& w=postDACWrites.back();
346 if (dumpWrites) addWrite(w.addr,w.val);
347 postDACWrites.pop();
348 }
349 }
350
351 int DivPlatformSwan::dispatch(DivCommand c) {
352 switch (c.cmd) {
353 case DIV_CMD_NOTE_ON: {
354 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SWAN);
355 if (c.chan==1) {
356 if (ins->type==DIV_INS_AMIGA || ins->amiga.useSample) {
357 pcm=true;
358 } else if (furnaceDac) {
359 pcm=false;
360 chan[c.chan].sampleNote=DIV_NOTE_NULL;
361 chan[c.chan].sampleNoteDelta=0;
362 }
363 if (pcm) {
364 if (skipRegisterWrites) break;
365 if (setPos) {
366 setPos=false;
367 } else {
368 dacPos=0;
369 }
370 dacPeriod=0;
371 if (ins->type==DIV_INS_AMIGA || ins->amiga.useSample) {
372 if (c.value!=DIV_NOTE_NULL) {
373 dacSample=ins->amiga.getSample(c.value);
374 chan[c.chan].sampleNote=c.value;
375 c.value=ins->amiga.getFreq(c.value);
376 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
377 } else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) {
378 dacSample=ins->amiga.getSample(chan[c.chan].sampleNote);
379 c.value=ins->amiga.getFreq(chan[c.chan].sampleNote);
380 }
381 if (dacSample<0 || dacSample>=parent->song.sampleLen) {
382 dacSample=-1;
383 if (dumpWrites) postWrite(0xffff0002,0);
384 break;
385 } else {
386 if (dumpWrites) {
387 postWrite(0xffff0000,dacSample);
388 }
389 }
390 if (c.value!=DIV_NOTE_NULL) {
391 chan[1].baseFreq=NOTE_PERIODIC(c.value);
392 chan[1].freqChanged=true;
393 chan[1].note=c.value;
394 }
395 chan[1].active=true;
396 chan[1].keyOn=true;
397 chan[1].macroInit(ins);
398 furnaceDac=true;
399 } else {
400 if (c.value!=DIV_NOTE_NULL) {
401 chan[1].note=c.value;
402 }
403 dacSample=12*sampleBank+chan[1].note%12;
404 if (dacSample>=parent->song.sampleLen) {
405 dacSample=-1;
406 if (dumpWrites) postWrite(0xffff0002,0);
407 break;
408 } else {
409 if (dumpWrites) postWrite(0xffff0000,dacSample);
410 }
411 dacRate=parent->getSample(dacSample)->rate;
412 if (dumpWrites) {
413 postWrite(0xffff0001,dacRate);
414 }
415 chan[1].active=true;
416 chan[1].keyOn=true;
417 furnaceDac=false;
418 }
419 break;
420 }
421 }
422 if (c.value!=DIV_NOTE_NULL) {
423 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
424 chan[c.chan].freqChanged=true;
425 chan[c.chan].note=c.value;
426 }
427 chan[c.chan].active=true;
428 chan[c.chan].keyOn=true;
429 chan[c.chan].macroInit(ins);
430 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
431 chan[c.chan].outVol=chan[c.chan].vol;
432 }
433 if (chan[c.chan].wave<0) {
434 chan[c.chan].wave=0;
435 chan[c.chan].ws.changeWave1(chan[c.chan].wave);
436 }
437 chan[c.chan].ws.init(ins,32,15,chan[c.chan].insChanged);
438 chan[c.chan].insChanged=false;
439 break;
440 }
441 case DIV_CMD_NOTE_OFF:
442 if (c.chan==1&&pcm) {
443 dacSample=-1;
444 if (dumpWrites) postWrite(0xffff0002,0);
445 pcm=false;
446 chan[c.chan].sampleNote=DIV_NOTE_NULL;
447 chan[c.chan].sampleNoteDelta=0;
448 }
449 chan[c.chan].active=false;
450 chan[c.chan].keyOff=true;
451 chan[c.chan].macroInit(NULL);
452 break;
453 case DIV_CMD_NOTE_OFF_ENV:
454 case DIV_CMD_ENV_RELEASE:
455 chan[c.chan].std.release();
456 break;
457 case DIV_CMD_INSTRUMENT:
458 if (chan[c.chan].ins!=c.value || c.value2==1) {
459 chan[c.chan].ins=c.value;
460 chan[c.chan].insChanged=true;
461 }
462 break;
463 case DIV_CMD_VOLUME:
464 if (chan[c.chan].vol!=c.value) {
465 chan[c.chan].vol=c.value;
466 if (!chan[c.chan].std.vol.has) {
467 calcAndWriteOutVol(c.chan,15);
468 }
469 }
470 break;
471 case DIV_CMD_GET_VOLUME:
472 return chan[c.chan].vol;
473 break;
474 case DIV_CMD_PITCH:
475 chan[c.chan].pitch=c.value;
476 chan[c.chan].freqChanged=true;
477 break;
478 case DIV_CMD_WAVE:
479 chan[c.chan].wave=c.value;
480 chan[c.chan].ws.changeWave1(chan[c.chan].wave);
481 chan[c.chan].keyOn=true;
482 break;
483 case DIV_CMD_WS_SWEEP_TIME:
484 if (c.chan==2) {
485 if (c.value==0) {
486 sweep=false;
487 } else {
488 sweep=true;
489 rWrite(0x0d,(c.value-1)&0xff);
490 }
491 }
492 break;
493 case DIV_CMD_WS_SWEEP_AMOUNT:
494 if (c.chan==2) {
495 rWrite(0x0c,c.value&0xff);
496 }
497 break;
498 case DIV_CMD_WS_GLOBAL_SPEAKER_VOLUME:
499 if (c.value <= 1) {
500 rWrite(0x11,0x09|(3<<1));
501 } else if (c.value <= 3) {
502 rWrite(0x11,0x09|(2<<1));
503 } else if (c.value <= 7) {
504 rWrite(0x11,0x09|(1<<1));
505 } else {
506 rWrite(0x11,0x09|(0<<1));
507 }
508 break;
509 case DIV_CMD_NOTE_PORTA: {
510 int destFreq=NOTE_PERIODIC(c.value2+chan[c.chan].sampleNoteDelta);
511 bool return2=false;
512 if (destFreq>chan[c.chan].baseFreq) {
513 chan[c.chan].baseFreq+=c.value;
514 if (chan[c.chan].baseFreq>=destFreq) {
515 chan[c.chan].baseFreq=destFreq;
516 return2=true;
517 }
518 } else {
519 chan[c.chan].baseFreq-=c.value;
520 if (chan[c.chan].baseFreq<=destFreq) {
521 chan[c.chan].baseFreq=destFreq;
522 return2=true;
523 }
524 }
525 chan[c.chan].freqChanged=true;
526 if (return2) {
527 chan[c.chan].inPorta=false;
528 return 2;
529 }
530 break;
531 }
532 case DIV_CMD_STD_NOISE_MODE:
533 if (c.chan==3) {
534 noise=c.value&0xff;
535 if (noise>0) rWrite(0x0e,((noise-1)&0x07)|0x18);
536 }
537 break;
538 case DIV_CMD_SAMPLE_MODE:
539 if (c.chan==1) {
540 pcm=c.value;
541 if (!pcm) {
542 chan[c.chan].sampleNote=DIV_NOTE_NULL;
543 chan[c.chan].sampleNoteDelta=0;
544 }
545 }
546 break;
547 case DIV_CMD_SAMPLE_BANK:
548 sampleBank=c.value;
549 if (sampleBank>(parent->song.sample.size()/12)) {
550 sampleBank=parent->song.sample.size()/12;
551 }
552 break;
553 case DIV_CMD_SAMPLE_POS:
554 dacPos=c.value;
555 setPos=true;
556 break;
557 case DIV_CMD_PANNING: {
558 chan[c.chan].pan=(c.value&0xf0)|(c.value2>>4);
559 calcAndWriteOutVol(c.chan,chan[c.chan].std.vol.will?chan[c.chan].std.vol.val:15);
560 break;
561 }
562 case DIV_CMD_LEGATO:
563 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
564 chan[c.chan].freqChanged=true;
565 chan[c.chan].note=c.value;
566 break;
567 case DIV_CMD_PRE_PORTA:
568 if (chan[c.chan].active && c.value2) {
569 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_SWAN));
570 }
571 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);
572 chan[c.chan].inPorta=c.value;
573 break;
574 case DIV_CMD_GET_VOLMAX:
575 return 15;
576 break;
577 case DIV_CMD_MACRO_OFF:
578 chan[c.chan].std.mask(c.value,true);
579 break;
580 case DIV_CMD_MACRO_ON:
581 chan[c.chan].std.mask(c.value,false);
582 break;
583 case DIV_CMD_MACRO_RESTART:
584 chan[c.chan].std.restart(c.value);
585 break;
586 default:
587 break;
588 }
589 return 1;
590 }
591
592 void DivPlatformSwan::muteChannel(int ch, bool mute) {
593 isMuted[ch]=mute;
594 writeOutVol(ch);
595 }
596
597 void DivPlatformSwan::forceIns() {
598 noise=0;
599 for (int i=0; i<4; i++) {
600 chan[i].insChanged=true;
601 chan[i].freqChanged=true;
602 updateWave(i);
603 writeOutVol(i);
604 }
605 }
606
607 void* DivPlatformSwan::getChanState(int ch) {
608 return &chan[ch];
609 }
610
611 DivMacroInt* DivPlatformSwan::getChanMacroInt(int ch) {
612 return &chan[ch].std;
613 }
614
615 unsigned short DivPlatformSwan::getPan(int ch) {
616 return ((chan[ch].pan&0xf0)<<4)|(chan[ch].pan&15);
617 }
618
619 DivChannelModeHints DivPlatformSwan::getModeHints(int ch) {
620 DivChannelModeHints ret;
621
622 switch (ch) {
623 case 1: // PCM
624 ret.count=1;
625 ret.hint[0]=ICON_FA_VOLUME_UP;
626 ret.type[0]=pcm?4:0;
627 break;
628 case 2: // sweep
629 ret.count=1;
630 ret.hint[0]=ICON_FUR_SAW;
631 ret.type[0]=sweep?2:0;
632 break;
633 case 3: // noise
634 ret.count=1;
635 ret.hint[0]=ICON_FUR_NOISE;
636 ret.type[0]=noise?4:0;
637 break;
638 }
639
640 return ret;
641 }
642
643 DivDispatchOscBuffer* DivPlatformSwan::getOscBuffer(int ch) {
644 return oscBuf[ch];
645 }
646
647 unsigned char* DivPlatformSwan::getRegisterPool() {
648 for (int i = 0; i < 0x20; i++) {
649 regPool[i] = swan_sound_in(&ws, i | 0x80);
650 }
651 return regPool;
652 }
653
654 int DivPlatformSwan::getRegisterPoolSize() {
655 return 128;
656 }
657
658 void DivPlatformSwan::reset() {
659 while (!writes.empty()) writes.pop();
660 while (!postDACWrites.empty()) postDACWrites.pop();
661 memset(regPool,0,sizeof(regPool));
662 for (int i=0; i<4; i++) {
663 chan[i]=Channel();
664 chan[i].vol=15;
665 chan[i].pan=0xff;
666 chan[i].std.setEngine(parent);
667 chan[i].ws.setEngine(parent);
668 chan[i].ws.init(NULL,32,15,false);
669 rWrite(0x08+i,0xff);
670 }
671 if (dumpWrites) {
672 addWrite(0xffffffff,0);
673 }
674 ws_mdfn->SoundReset();
675 swan_sound_init(&ws, true);
676 pcm=false;
677 sweep=false;
678 furnaceDac=false;
679 setPos=false;
680 noise=0;
681 dacPeriod=0;
682 dacRate=0;
683 dacPos=0;
684 dacSample=-1;
685 sampleBank=0;
686 rWrite(0x0f,0x00); // wave table at 0x0000
687 rWrite(0x11,0x0f); // enable speakers, minimum headphone volume
688 }
689
690 int DivPlatformSwan::getOutputCount() {
691 return (stereo || useMdfn)?2:1;
692 }
693
694 bool DivPlatformSwan::hasAcquireDirect() {
695 return useMdfn;
696 }
697
698 void DivPlatformSwan::setUseMdfn(bool use) {
699 useMdfn=use;
700 }
701
702 void DivPlatformSwan::notifyWaveChange(int wave) {
703 for (int i=0; i<4; i++) {
704 if (chan[i].wave==wave) {
705 chan[i].ws.changeWave1(wave);
706 updateWave(i);
707 }
708 }
709 }
710
711 void DivPlatformSwan::notifyInsDeletion(void* ins) {
712 for (int i=0; i<4; i++) {
713 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
714 }
715 }
716
717 void DivPlatformSwan::poke(unsigned int addr, unsigned short val) {
718 rWrite(addr,val);
719 }
720
721 void DivPlatformSwan::poke(std::vector<DivRegWrite>& wlist) {
722 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
723 }
724
725 void DivPlatformSwan::setFlags(const DivConfig& flags) {
726 chipClock=3072000;
727 CHECK_CUSTOM_CLOCK;
728 if (useMdfn) {
729 rate=chipClock;
730 } else {
731 rate=chipClock/128;
732 }
733 stereo=flags.getBool("stereo",true);
734 for (int i=0; i<4; i++) {
735 oscBuf[i]->setRate(rate);
736 }
737 }
738
739 int DivPlatformSwan::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
740 parent=p;
741 dumpWrites=false;
742 skipRegisterWrites=false;
743 stereo=false;
744
745 for (int i=0; i<4; i++) {
746 isMuted[i]=false;
747 oscBuf[i]=new DivDispatchOscBuffer;
748 }
749
750 ws_mdfn=new WSwan();
751
752 setFlags(flags);
753 reset();
754 return 4;
755 }
756
757 void DivPlatformSwan::quit() {
758 for (int i=0; i<4; i++) {
759 delete oscBuf[i];
760 }
761 delete ws_mdfn;
762 }
763
764 DivPlatformSwan::~DivPlatformSwan() {
765 }