Debian Codegraph @

src/engine/platform/namcowsg.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 "namcowsg.h"
21 #include "../engine.h"
22 #include <math.h>
23
24 //#define rWrite(a,v) pendingWrites[a]=v;
25 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
26
27 #define CHIP_FREQBASE 4194304
28
29 const char* regCheatSheetNamcoWSG[]={
30 "WaveSel0", "05",
31 "WaveSel1", "0A",
32 "WaveSel2", "0F",
33 "FreqS0", "10",
34 "FreqL0", "11",
35 "FreqM0", "12",
36 "FreqH0", "13",
37 "FreqX0", "14",
38 "Volume0", "15",
39 "FreqL1", "16",
40 "FreqM1", "17",
41 "FreqH1", "18",
42 "FreqX1", "19",
43 "Volume1", "1A",
44 "FreqL2", "1B",
45 "FreqM2", "1C",
46 "FreqH2", "1D",
47 "FreqX2", "1E",
48 "Volume2", "1F",
49 NULL
50 };
51
52 const char* regCheatSheetNamco15XX[]={
53 "Volume0", "03",
54 "FreqL0", "04",
55 "FreqH0", "05",
56 "WaveSel0", "06",
57
58 "Volume1", "0B",
59 "FreqL1", "0C",
60 "FreqH1", "0D",
61 "WaveSel1", "0E",
62
63 "Volume2", "13",
64 "FreqL2", "14",
65 "FreqH2", "15",
66 "WaveSel2", "16",
67
68 "Volume3", "1B",
69 "FreqL3", "1C",
70 "FreqH3", "1D",
71 "WaveSel3", "1E",
72
73 "Volume4", "23",
74 "FreqL4", "24",
75 "FreqH4", "25",
76 "WaveSel4", "26",
77
78 "Volume5", "2B",
79 "FreqL5", "2C",
80 "FreqH5", "2D",
81 "WaveSel5", "2E",
82
83 "Volume6", "33",
84 "FreqL6", "34",
85 "FreqH6", "35",
86 "WaveSel6", "36",
87
88 "Volume7", "3B",
89 "FreqL7", "3C",
90 "FreqH7", "3D",
91 "WaveSel7", "3E",
92
93 NULL
94 };
95
96 const char* regCheatSheetNamcoCUS30[]={
97 "VolumeL0", "00",
98 "WaveSel0", "01",
99 "FreqH0", "02",
100 "FreqL0", "03",
101 "VolumeR0", "04",
102
103 "VolumeL1", "08",
104 "WaveSel1", "09",
105 "FreqH1", "0A",
106 "FreqL1", "0B",
107 "VolumeR1", "0C",
108
109 "VolumeL2", "10",
110 "WaveSel2", "11",
111 "FreqH2", "12",
112 "FreqL2", "13",
113 "VolumeR2", "14",
114
115 "VolumeL3", "18",
116 "WaveSel3", "19",
117 "FreqH3", "1A",
118 "FreqL3", "1B",
119 "VolumeR3", "1C",
120
121 "VolumeL4", "20",
122 "WaveSel4", "21",
123 "FreqH4", "22",
124 "FreqL4", "23",
125 "VolumeR4", "24",
126
127 "VolumeL5", "28",
128 "WaveSel5", "29",
129 "FreqH5", "2A",
130 "FreqL5", "2B",
131 "VolumeR5", "2C",
132
133 "VolumeL6", "30",
134 "WaveSel6", "31",
135 "FreqH6", "32",
136 "FreqL6", "33",
137 "VolumeR6", "34",
138
139 "VolumeL7", "38",
140 "WaveSel7", "39",
141 "FreqH7", "3A",
142 "FreqL7", "3B",
143 "VolumeR7", "3C",
144
145 NULL
146 };
147
148 const char** DivPlatformNamcoWSG::getRegisterSheet() {
149 if (devType==30) return regCheatSheetNamcoCUS30;
150 if (devType==15) return regCheatSheetNamco15XX;
151 return regCheatSheetNamcoWSG;
152 }
153
154 void DivPlatformNamcoWSG::acquire(short** buf, size_t len) {
155 while (!writes.empty()) {
156 QueuedWrite w=writes.front();
157 switch (devType) {
158 case 1:
159 ((namco_device*)namco)->pacman_sound_w(w.addr,w.val);
160 break;
161 case 2:
162 ((namco_device*)namco)->polepos_sound_w(w.addr,w.val);
163 break;
164 case 15:
165 ((namco_15xx_device*)namco)->sharedram_w(w.addr,w.val);
166 break;
167 case 30:
168 ((namco_cus30_device*)namco)->namcos1_cus30_w(w.addr,w.val);
169 break;
170 }
171 regPool[w.addr&0x3f]=w.val;
172 writes.pop();
173 }
174
175 for (int i=0; i<chans; i++) {
176 oscBuf[i]->begin(len);
177 }
178
179 for (size_t h=0; h<len; h++) {
180 short* bufC[2]={
181 buf[0]+h, buf[1]+h
182 };
183 namco->sound_stream_update(bufC,1);
184 for (int i=0; i<chans; i++) {
185 oscBuf[i]->putSample(h,(namco->m_channel_list[i].last_out*chans)>>1);
186 }
187 }
188
189 for (int i=0; i<chans; i++) {
190 oscBuf[i]->end(len);
191 }
192 }
193
194 void DivPlatformNamcoWSG::updateWave(int ch) {
195 if (romMode) return;
196 if (devType==30) {
197 for (int i=0; i<32; i++) {
198 ((namco_cus30_device*)namco)->namcos1_cus30_w(i+ch*32,chan[ch].ws.output[i]);
199 }
200 } else {
201 for (int i=0; i<32; i++) {
202 namco->update_namco_waveform(i+ch*32,chan[ch].ws.output[i]);
203 }
204 }
205 }
206
207 void DivPlatformNamcoWSG::tick(bool sysTick) {
208 for (int i=0; i<chans; i++) {
209 chan[i].std.next();
210 if (chan[i].std.vol.had) {
211 chan[i].outVol=VOL_SCALE_LINEAR(chan[i].vol,chan[i].std.vol.val,15);
212 }
213 if (chan[i].std.duty.had) {
214 chan[i].noise=chan[i].std.duty.val;
215 chan[i].freqChanged=true;
216 }
217 if (NEW_ARP_STRAT) {
218 chan[i].handleArp();
219 } else if (chan[i].std.arp.had) {
220 if (!chan[i].inPorta) {
221 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
222 }
223 chan[i].freqChanged=true;
224 }
225 if (chan[i].std.wave.had) {
226 if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) {
227 chan[i].wave=chan[i].std.wave.val;
228 chan[i].ws.changeWave1(chan[i].wave);
229 if (!chan[i].keyOff) chan[i].keyOn=true;
230 }
231 }
232 if (chan[i].std.panL.had) {
233 chan[i].pan&=0x0f;
234 chan[i].pan|=(chan[i].std.panL.val&15)<<4;
235 }
236 if (chan[i].std.panR.had) {
237 chan[i].pan&=0xf0;
238 chan[i].pan|=chan[i].std.panR.val&15;
239 }
240 if (chan[i].std.pitch.had) {
241 if (chan[i].std.pitch.mode) {
242 chan[i].pitch2+=chan[i].std.pitch.val;
243 CLAMP_VAR(chan[i].pitch2,-1048575,1048575);
244 } else {
245 chan[i].pitch2=chan[i].std.pitch.val;
246 }
247 chan[i].freqChanged=true;
248 }
249 if (chan[i].active) {
250 if (chan[i].ws.tick() || (chan[i].std.phaseReset.had && chan[i].std.phaseReset.val==1)) {
251 updateWave(i);
252 }
253 }
254 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
255 //DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_PCE);
256 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);
257 if (chan[i].freq<0) chan[i].freq=0;
258 if (chan[i].freq>1048575) chan[i].freq=1048575;
259 if (chan[i].keyOn) {
260 }
261 if (chan[i].keyOff) {
262 }
263 if (chan[i].keyOn) chan[i].keyOn=false;
264 if (chan[i].keyOff) chan[i].keyOff=false;
265 chan[i].freqChanged=false;
266 }
267 }
268
269 // update state
270 switch (devType) {
271 case 1:
272 if (chan[0].active && !isMuted[0]) {
273 rWrite(0x15,chan[0].outVol);
274 } else {
275 rWrite(0x15,0);
276 }
277 if (chan[1].active && !isMuted[1]) {
278 rWrite(0x1a,chan[1].outVol);
279 } else {
280 rWrite(0x1a,0);
281 }
282 if (chan[2].active && !isMuted[2]) {
283 rWrite(0x1f,chan[2].outVol);
284 } else {
285 rWrite(0x1f,0);
286 }
287
288 rWrite(0x10,(chan[0].freq)&15);
289 rWrite(0x11,(chan[0].freq>>4)&15);
290 rWrite(0x12,(chan[0].freq>>8)&15);
291 rWrite(0x13,(chan[0].freq>>12)&15);
292 rWrite(0x14,(chan[0].freq>>16)&15);
293
294 rWrite(0x16,(chan[1].freq>>4)&15);
295 rWrite(0x17,(chan[1].freq>>8)&15);
296 rWrite(0x18,(chan[1].freq>>12)&15);
297 rWrite(0x19,(chan[1].freq>>16)&15);
298
299 rWrite(0x1b,(chan[2].freq>>4)&15);
300 rWrite(0x1c,(chan[2].freq>>8)&15);
301 rWrite(0x1d,(chan[2].freq>>12)&15);
302 rWrite(0x1e,(chan[2].freq>>16)&15);
303
304 rWrite(0x05,romMode?(chan[0].wave&7):0);
305 rWrite(0x0a,romMode?(chan[1].wave&7):1);
306 rWrite(0x0f,romMode?(chan[2].wave&7):2);
307 break;
308 case 15:
309 for (int i=0; i<8; i++) {
310 if (chan[i].active && !isMuted[i]) {
311 rWrite((i<<3)+0x03,chan[i].outVol);
312 } else {
313 rWrite((i<<3)+0x03,0);
314 }
315 rWrite((i<<3)+0x04,chan[i].freq&0xff);
316 rWrite((i<<3)+0x05,(chan[i].freq>>8)&0xff);
317 rWrite((i<<3)+0x06,((chan[i].freq>>16)&15)|((romMode?(chan[i].wave&7):i)<<4));
318 }
319 break;
320 case 30:
321 for (int i=0; i<8; i++) {
322 if (chan[i].active && !isMuted[i]) {
323 rWrite((i<<3)+0x100,(chan[i].outVol*((chan[i].pan>>4)&15))/15);
324 rWrite((i<<3)+0x104,((chan[i].outVol*(chan[i].pan&15))/15)|(chan[(i+1)&7].noise?0x80:0));
325 } else {
326 rWrite((i<<3)+0x100,0);
327 rWrite((i<<3)+0x104,(chan[(i+1)&7].noise?0x80:0));
328 }
329 if (chan[i].noise && newNoise) {
330 int noiseFreq=chan[i].freq>>9;
331 if (noiseFreq<0) noiseFreq=0;
332 if (noiseFreq>255) noiseFreq=255;
333 rWrite((i<<3)+0x103,noiseFreq);
334 } else {
335 rWrite((i<<3)+0x103,chan[i].freq&0xff);
336 rWrite((i<<3)+0x102,(chan[i].freq>>8)&0xff);
337 rWrite((i<<3)+0x101,((chan[i].freq>>16)&15)|(i<<4));
338 }
339 }
340 break;
341 }
342 }
343
344 int DivPlatformNamcoWSG::dispatch(DivCommand c) {
345 switch (c.cmd) {
346 case DIV_CMD_NOTE_ON: {
347 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_PCE);
348 if (c.value!=DIV_NOTE_NULL) {
349 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
350 chan[c.chan].freqChanged=true;
351 chan[c.chan].note=c.value;
352 }
353 chan[c.chan].active=true;
354 chan[c.chan].keyOn=true;
355 chan[c.chan].macroInit(ins);
356 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
357 chan[c.chan].outVol=chan[c.chan].vol;
358 }
359 if (chan[c.chan].wave<0) {
360 chan[c.chan].wave=0;
361 chan[c.chan].ws.changeWave1(chan[c.chan].wave);
362 }
363 chan[c.chan].ws.init(ins,32,15,chan[c.chan].insChanged);
364 chan[c.chan].insChanged=false;
365 break;
366 }
367 case DIV_CMD_NOTE_OFF:
368 chan[c.chan].active=false;
369 chan[c.chan].keyOff=true;
370 chan[c.chan].macroInit(NULL);
371 break;
372 case DIV_CMD_NOTE_OFF_ENV:
373 case DIV_CMD_ENV_RELEASE:
374 chan[c.chan].std.release();
375 break;
376 case DIV_CMD_INSTRUMENT:
377 if (chan[c.chan].ins!=c.value || c.value2==1) {
378 chan[c.chan].ins=c.value;
379 chan[c.chan].insChanged=true;
380 }
381 break;
382 case DIV_CMD_VOLUME:
383 if (chan[c.chan].vol!=c.value) {
384 chan[c.chan].vol=c.value;
385 if (!chan[c.chan].std.vol.has) {
386 chan[c.chan].outVol=c.value;
387 }
388 }
389 break;
390 case DIV_CMD_GET_VOLUME:
391 if (chan[c.chan].std.vol.has) {
392 return chan[c.chan].vol;
393 }
394 return chan[c.chan].outVol;
395 break;
396 case DIV_CMD_PITCH:
397 chan[c.chan].pitch=c.value;
398 chan[c.chan].freqChanged=true;
399 break;
400 case DIV_CMD_WAVE:
401 chan[c.chan].wave=c.value;
402 chan[c.chan].ws.changeWave1(chan[c.chan].wave);
403 chan[c.chan].keyOn=true;
404 break;
405 case DIV_CMD_NOTE_PORTA: {
406 int destFreq=NOTE_FREQUENCY(c.value2);
407 bool return2=false;
408 if (destFreq>chan[c.chan].baseFreq) {
409 chan[c.chan].baseFreq+=c.value*((parent->song.linearPitch==2)?1:8);
410 if (chan[c.chan].baseFreq>=destFreq) {
411 chan[c.chan].baseFreq=destFreq;
412 return2=true;
413 }
414 } else {
415 chan[c.chan].baseFreq-=c.value*((parent->song.linearPitch==2)?1:8);
416 if (chan[c.chan].baseFreq<=destFreq) {
417 chan[c.chan].baseFreq=destFreq;
418 return2=true;
419 }
420 }
421 chan[c.chan].freqChanged=true;
422 if (return2) {
423 chan[c.chan].inPorta=false;
424 return 2;
425 }
426 break;
427 }
428 case DIV_CMD_STD_NOISE_MODE:
429 chan[c.chan].noise=c.value;
430 chan[c.chan].freqChanged=true;
431 break;
432 case DIV_CMD_PANNING: {
433 chan[c.chan].pan=(c.value&0xf0)|(c.value2>>4);
434 break;
435 }
436 case DIV_CMD_LEGATO:
437 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
438 chan[c.chan].freqChanged=true;
439 chan[c.chan].note=c.value;
440 break;
441 case DIV_CMD_PRE_PORTA:
442 if (chan[c.chan].active && c.value2) {
443 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_PCE));
444 }
445 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);
446 chan[c.chan].inPorta=c.value;
447 break;
448 case DIV_CMD_GET_VOLMAX:
449 return 15;
450 break;
451 case DIV_CMD_MACRO_OFF:
452 chan[c.chan].std.mask(c.value,true);
453 break;
454 case DIV_CMD_MACRO_ON:
455 chan[c.chan].std.mask(c.value,false);
456 break;
457 case DIV_CMD_MACRO_RESTART:
458 chan[c.chan].std.restart(c.value);
459 break;
460 default:
461 break;
462 }
463 return 1;
464 }
465
466 void DivPlatformNamcoWSG::muteChannel(int ch, bool mute) {
467 isMuted[ch]=mute;
468 }
469
470 void DivPlatformNamcoWSG::forceIns() {
471 for (int i=0; i<chans; i++) {
472 chan[i].insChanged=true;
473 chan[i].freqChanged=true;
474 updateWave(i);
475 }
476 }
477
478 void* DivPlatformNamcoWSG::getChanState(int ch) {
479 return &chan[ch];
480 }
481
482 DivMacroInt* DivPlatformNamcoWSG::getChanMacroInt(int ch) {
483 return &chan[ch].std;
484 }
485
486 unsigned short DivPlatformNamcoWSG::getPan(int ch) {
487 if (devType!=30) return 0;
488 return ((chan[ch].pan&0xf0)<<4)|(chan[ch].pan&15);
489 }
490
491 DivDispatchOscBuffer* DivPlatformNamcoWSG::getOscBuffer(int ch) {
492 return oscBuf[ch];
493 }
494
495 unsigned char* DivPlatformNamcoWSG::getRegisterPool() {
496 return regPool;
497 }
498
499 int DivPlatformNamcoWSG::getRegisterPoolSize() {
500 return (devType==1)?32:64;
501 }
502
503 void DivPlatformNamcoWSG::reset() {
504 while (!writes.empty()) writes.pop();
505 memset(regPool,0,128);
506 for (int i=0; i<chans; i++) {
507 chan[i]=DivPlatformNamcoWSG::Channel();
508 chan[i].std.setEngine(parent);
509 chan[i].ws.setEngine(parent);
510 chan[i].ws.init(NULL,32,15,false);
511 }
512 if (dumpWrites) {
513 addWrite(0xffffffff,0);
514 }
515 namco->set_voices(chans);
516 namco->set_stereo((devType==2 || devType==30));
517 namco->device_start(NULL);
518
519 updateROMWaves();
520 }
521
522 int DivPlatformNamcoWSG::getOutputCount() {
523 return (devType==30)?2:1;
524 }
525
526 bool DivPlatformNamcoWSG::keyOffAffectsArp(int ch) {
527 return true;
528 }
529
530 void DivPlatformNamcoWSG::updateROMWaves() {
531 if (romMode) {
532 // copy wavetables
533 for (int i=0; i<8; i++) {
534 int data=0;
535 DivWavetable* w=parent->getWave(i);
536
537 for (int j=0; j<32; j++) {
538 if (w->max<1 || w->len<1) {
539 data=0;
540 } else {
541 data=w->data[j*w->len/32]*15/w->max;
542 if (data<0) data=0;
543 if (data>15) data=15;
544 }
545 namco->update_namco_waveform(i*32+j,data);
546 }
547 }
548 }
549 }
550
551 void DivPlatformNamcoWSG::notifyWaveChange(int wave) {
552 for (int i=0; i<chans; i++) {
553 if (chan[i].wave==wave) {
554 chan[i].ws.changeWave1(wave);
555 updateWave(i);
556 }
557 }
558 updateROMWaves();
559 }
560
561 void DivPlatformNamcoWSG::notifyInsDeletion(void* ins) {
562 for (int i=0; i<chans; i++) {
563 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
564 }
565 }
566
567 void DivPlatformNamcoWSG::setDeviceType(int type) {
568 devType=type;
569 switch (type) {
570 case 15:
571 chans=8;
572 break;
573 case 30:
574 chans=8;
575 break;
576 case 1:
577 chans=3;
578 break;
579 case 2:
580 chans=8;
581 break;
582 }
583 }
584
585 void DivPlatformNamcoWSG::setFlags(const DivConfig& flags) {
586 chipClock=3072000;
587 CHECK_CUSTOM_CLOCK;
588 rate=chipClock/32;
589 namco->device_clock_changed(96000);
590 for (int i=0; i<chans; i++) {
591 oscBuf[i]->setRate(rate);
592 }
593 newNoise=flags.getBool("newNoise",true);
594 romMode=flags.getBool("romMode",false);
595 if (devType==30) romMode=false;
596 }
597
598 void DivPlatformNamcoWSG::poke(unsigned int addr, unsigned short val) {
599 rWrite(addr,val);
600 }
601
602 void DivPlatformNamcoWSG::poke(std::vector<DivRegWrite>& wlist) {
603 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
604 }
605
606 int DivPlatformNamcoWSG::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
607 parent=p;
608 dumpWrites=false;
609 skipRegisterWrites=false;
610 for (int i=0; i<chans; i++) {
611 isMuted[i]=false;
612 oscBuf[i]=new DivDispatchOscBuffer;
613 }
614 switch (devType) {
615 case 15:
616 namco=new namco_15xx_device(3072000);
617 break;
618 case 30:
619 namco=new namco_cus30_device(3072000);
620 break;
621 default:
622 namco=new namco_device(3072000);
623 break;
624 }
625 setFlags(flags);
626 reset();
627 return 6;
628 }
629
630 void DivPlatformNamcoWSG::quit() {
631 for (int i=0; i<chans; i++) {
632 delete oscBuf[i];
633 }
634 delete namco;
635 }
636
637 DivPlatformNamcoWSG::~DivPlatformNamcoWSG() {
638 }