Debian Codegraph @

src/engine/platform/msm6295.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 "msm6295.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include <string.h>
24 #include <math.h>
25
26 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
27 #define rWriteDelay(a,v,d) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v,d)); if (dumpWrites) {addWrite(a,v);} }
28
29 #define setPhrase(c) \
30 if (isBanked) { \
31 rWrite(16+(c),bankedPhrase[chan[(c)].sample].bank); \
32 rWrite(0,0x80|((c)<<5)|bankedPhrase[chan[(c)].sample].phrase); \
33 } else { \
34 rWrite(0,0x80|chan[(c)].sample); \
35 }
36
37 const char** DivPlatformMSM6295::getRegisterSheet() {
38 return NULL;
39 }
40
41 u8 DivPlatformMSM6295::read_byte(u32 address) {
42 if (adpcmMem==NULL) {
43 return 0;
44 }
45 if (isBanked) {
46 if (address<0x400) {
47 unsigned int bankedAddress=(bank[(address>>8)&0x3]<<16)|(address&0x3ff);
48 if (bankedAddress>=getSampleMemCapacity(0)) {
49 return 0;
50 }
51 return adpcmMem[bankedAddress&0xffffff];
52 }
53 unsigned int bankedAddress=(bank[(address>>16)&0x3]<<16)|(address&0xffff);
54 if (bankedAddress>=getSampleMemCapacity(0)) {
55 return 0;
56 }
57 return adpcmMem[bankedAddress&0xffffff];
58 }
59 if (address>=getSampleMemCapacity(0)) {
60 return 0;
61 }
62 return adpcmMem[address&0x3ffff];
63 }
64
65 void DivPlatformMSM6295::acquire(short** buf, size_t len) {
66 for (int i=0; i<4; i++) {
67 oscBuf[i]->begin(len);
68 }
69
70 for (size_t h=0; h<len; h++) {
71 if (delay<=0) {
72 if (!writes.empty()) {
73 QueuedWrite& w=writes.front();
74 switch (w.addr) {
75 case 0: // command
76 msm.command_w(w.val);
77 break;
78 case 8: // chip clock select (VGM)
79 case 9:
80 case 10:
81 case 11:
82 break;
83 case 12: // rate select
84 msm.ss_w(!w.val);
85 break;
86 case 14: // enable bankswitch
87 break;
88 case 15: // set bank base
89 break;
90 case 16: // switch bank
91 case 17:
92 case 18:
93 case 19:
94 bank[w.addr-16]=w.val;
95 break;
96 }
97 writes.pop();
98 delay=w.delay;
99 }
100 } else {
101 delay-=3;
102 }
103
104 msm.tick();
105 msm.tick();
106 msm.tick();
107
108 buf[0][h]=msm.out()<<4;
109
110 if (++updateOsc>=22) {
111 updateOsc=0;
112 for (int i=0; i<4; i++) {
113 oscBuf[i]->putSample(h,msm.voice_out(i)<<5);
114 }
115 }
116 }
117
118 for (int i=0; i<4; i++) {
119 oscBuf[i]->end(len);
120 }
121 }
122
123 void DivPlatformMSM6295::tick(bool sysTick) {
124 for (int i=0; i<4; i++) {
125 if (!parent->song.disableSampleMacro) {
126 chan[i].std.next();
127 if (chan[i].std.vol.had) {
128 chan[i].outVol=VOL_SCALE_LOG_BROKEN(chan[i].std.vol.val,chan[i].vol,8);
129 }
130 if (chan[i].std.duty.had) {
131 if (rateSel!=(chan[i].std.duty.val&1)) {
132 rateSel=chan[i].std.duty.val&1;
133 rWrite(12,!rateSel);
134 }
135 }
136 if (chan[i].std.phaseReset.had) {
137 if (chan[i].std.phaseReset.val && chan[i].active) {
138 chan[i].keyOn=true;
139 }
140 }
141 }
142 if (chan[i].keyOn || chan[i].keyOff) {
143 rWriteDelay(0,(8<<i),60); // turn off
144 if (chan[i].active && !chan[i].keyOff) {
145 if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
146 setPhrase(i);
147 rWrite(0,(16<<i)|(8-chan[i].outVol)); // turn on
148 } else {
149 chan[i].sample=-1;
150 }
151 } else {
152 chan[i].sample=-1;
153 }
154 chan[i].keyOn=false;
155 chan[i].keyOff=false;
156 }
157 }
158 }
159
160 int DivPlatformMSM6295::dispatch(DivCommand c) {
161 switch (c.cmd) {
162 case DIV_CMD_NOTE_ON: {
163 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_FM);
164 if (ins->type==DIV_INS_MSM6295 || ins->type==DIV_INS_AMIGA) {
165 chan[c.chan].furnacePCM=true;
166 } else {
167 chan[c.chan].furnacePCM=false;
168 }
169 if (skipRegisterWrites) break;
170 if (chan[c.chan].furnacePCM) {
171 chan[c.chan].macroInit(ins);
172 if (!chan[c.chan].std.vol.will) {
173 chan[c.chan].outVol=chan[c.chan].vol;
174 }
175 if (c.value!=DIV_NOTE_NULL) chan[c.chan].sample=ins->amiga.getSample(c.value);
176 if (chan[c.chan].sample>=0 && chan[c.chan].sample<parent->song.sampleLen) {
177 //DivSample* s=parent->getSample(chan[c.chan].sample);
178 if (c.value!=DIV_NOTE_NULL) {
179 chan[c.chan].note=c.value;
180 }
181 chan[c.chan].active=true;
182 chan[c.chan].keyOn=true;
183 rWriteDelay(0,(8<<c.chan),180); // turn off
184 setPhrase(c.chan);
185 rWrite(0,(16<<c.chan)|(8-chan[c.chan].outVol)); // turn on
186 } else {
187 break;
188 }
189 } else {
190 chan[c.chan].sample=-1;
191 chan[c.chan].macroInit(NULL);
192 chan[c.chan].outVol=chan[c.chan].vol;
193 if ((12*sampleBank+c.value%12)<0 || (12*sampleBank+c.value%12)>=parent->song.sampleLen) {
194 break;
195 }
196 //DivSample* s=parent->getSample(12*sampleBank+c.value%12);
197 chan[c.chan].sample=12*sampleBank+c.value%12;
198 rWriteDelay(0,(8<<c.chan),180); // turn off
199 setPhrase(c.chan);
200 rWrite(0,(16<<c.chan)|(8-chan[c.chan].outVol)); // turn on
201 }
202 break;
203 }
204 case DIV_CMD_NOTE_OFF:
205 chan[c.chan].keyOff=true;
206 chan[c.chan].keyOn=false;
207 chan[c.chan].active=false;
208 rWriteDelay(0,(8<<c.chan),180); // turn off
209 chan[c.chan].macroInit(NULL);
210 break;
211 case DIV_CMD_NOTE_OFF_ENV:
212 chan[c.chan].keyOff=true;
213 chan[c.chan].keyOn=false;
214 chan[c.chan].active=false;
215 rWriteDelay(0,(8<<c.chan),180); // turn off
216 chan[c.chan].std.release();
217 break;
218 case DIV_CMD_ENV_RELEASE:
219 chan[c.chan].std.release();
220 break;
221 case DIV_CMD_VOLUME: {
222 chan[c.chan].vol=MIN(8,c.value);
223 if (!chan[c.chan].std.vol.has) {
224 chan[c.chan].outVol=c.value;
225 }
226 break;
227 }
228 case DIV_CMD_GET_VOLUME: {
229 return chan[c.chan].vol;
230 break;
231 }
232 case DIV_CMD_INSTRUMENT:
233 if (chan[c.chan].ins!=c.value || c.value2==1) {
234 chan[c.chan].insChanged=true;
235 }
236 chan[c.chan].ins=c.value;
237 break;
238 case DIV_CMD_PITCH: {
239 break;
240 }
241 case DIV_CMD_NOTE_PORTA: {
242 return 2;
243 }
244 case DIV_CMD_SAMPLE_FREQ:
245 rateSel=c.value;
246 rWrite(12,!rateSel);
247 break;
248 case DIV_CMD_SAMPLE_BANK:
249 sampleBank=c.value;
250 if (sampleBank>(parent->song.sample.size()/12)) {
251 sampleBank=parent->song.sample.size()/12;
252 }
253 break;
254 case DIV_CMD_LEGATO: {
255 break;
256 }
257 case DIV_CMD_MACRO_OFF:
258 chan[c.chan].std.mask(c.value,true);
259 break;
260 case DIV_CMD_MACRO_ON:
261 chan[c.chan].std.mask(c.value,false);
262 break;
263 case DIV_CMD_MACRO_RESTART:
264 chan[c.chan].std.restart(c.value);
265 break;
266 case DIV_CMD_GET_VOLMAX:
267 return 8;
268 break;
269 case DIV_CMD_PRE_PORTA:
270 break;
271 case DIV_CMD_PRE_NOTE:
272 break;
273 default:
274 //printf("WARNING: unimplemented command %d\n",c.cmd);
275 break;
276 }
277 return 1;
278 }
279
280 void DivPlatformMSM6295::muteChannel(int ch, bool mute) {
281 isMuted[ch]=mute;
282 msm.voice_mute(ch,mute);
283 }
284
285 void DivPlatformMSM6295::forceIns() {
286 while (!writes.empty()) writes.pop();
287 for (int i=0; i<4; i++) {
288 chan[i].insChanged=true;
289 }
290 rWrite(12,!rateSel);
291 }
292
293 void* DivPlatformMSM6295::getChanState(int ch) {
294 return &chan[ch];
295 }
296
297 DivMacroInt* DivPlatformMSM6295::getChanMacroInt(int ch) {
298 return &chan[ch].std;
299 }
300
301 DivDispatchOscBuffer* DivPlatformMSM6295::getOscBuffer(int ch) {
302 return oscBuf[ch];
303 }
304
305 unsigned char* DivPlatformMSM6295::getRegisterPool() {
306 return NULL;
307 }
308
309 int DivPlatformMSM6295::getRegisterPoolSize() {
310 return 0;
311 }
312
313 void DivPlatformMSM6295::poke(unsigned int addr, unsigned short val) {
314 //immWrite(addr,val);
315 }
316
317 void DivPlatformMSM6295::poke(std::vector<DivRegWrite>& wlist) {
318 //for (DivRegWrite& i: wlist) immWrite(i.addr,i.val);
319 }
320
321 void DivPlatformMSM6295::reset() {
322 while (!writes.empty()) writes.pop();
323 msm.reset();
324 msm.ss_w(rateSelInit);
325 if (dumpWrites) {
326 addWrite(0xffffffff,0);
327 }
328 for (int i=0; i<4; i++) {
329 chan[i]=DivPlatformMSM6295::Channel();
330 chan[i].std.setEngine(parent);
331 msm.voice_mute(i,isMuted[i]);
332 }
333 for (int i=0; i<4; i++) {
334 chan[i].vol=8;
335 chan[i].outVol=8;
336 }
337
338 sampleBank=0;
339 rateSel=rateSelInit;
340 rWrite(12,!rateSelInit);
341 if (isBanked) {
342 rWrite(14,0x81);
343 }
344
345 delay=0;
346 }
347
348 bool DivPlatformMSM6295::keyOffAffectsArp(int ch) {
349 return false;
350 }
351
352 bool DivPlatformMSM6295::getLegacyAlwaysSetVolume() {
353 return false;
354 }
355
356 float DivPlatformMSM6295::getPostAmp() {
357 return 3.0f;
358 }
359
360 void DivPlatformMSM6295::notifyInsChange(int ins) {
361 for (int i=0; i<4; i++) {
362 if (chan[i].ins==ins) {
363 chan[i].insChanged=true;
364 }
365 }
366 }
367
368 void DivPlatformMSM6295::notifyInsDeletion(void* ins) {
369 for (int i=0; i<4; i++) {
370 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
371 }
372 }
373
374 const void* DivPlatformMSM6295::getSampleMem(int index) {
375 return index == 0 ? adpcmMem : NULL;
376 }
377
378 size_t DivPlatformMSM6295::getSampleMemCapacity(int index) {
379 return index == 0 ? (isBanked?16777216:262144) : 0;
380 }
381
382 size_t DivPlatformMSM6295::getSampleMemUsage(int index) {
383 return index == 0 ? adpcmMemLen : 0;
384 }
385
386 bool DivPlatformMSM6295::isSampleLoaded(int index, int sample) {
387 if (index!=0) return false;
388 if (sample<0 || sample>32767) return false;
389 return sampleLoaded[sample];
390 }
391
392 const DivMemoryComposition* DivPlatformMSM6295::getMemCompo(int index) {
393 if (index!=0) return NULL;
394 return &memCompo;
395 }
396
397 void DivPlatformMSM6295::renderSamples(int sysID) {
398 unsigned int* sampleOffVOX=new unsigned int[32768];
399
400 memset(adpcmMem,0,16777216);
401 memset(sampleOffVOX,0,32768*sizeof(unsigned int));
402 memset(sampleLoaded,0,32768*sizeof(bool));
403 for (int i=0; i<32768; i++) {
404 bankedPhrase[i].bank=0;
405 bankedPhrase[i].phrase=0;
406 }
407
408 memCompo=DivMemoryComposition();
409 memCompo.name="Sample ROM";
410
411 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_RESERVED,"Phrase Book",-1,0,128*8));
412
413 // sample data
414 size_t memPos=128*8;
415 int sampleCount=parent->song.sampleLen;
416 if (isBanked) {
417 if (sampleCount>8191) {
418 // mark the rest as unavailable
419 for (int i=8191; i<sampleCount; i++) {
420 sampleLoaded[i]=false;
421 }
422 sampleCount=8191;
423 }
424 int bankInd=0;
425 int phraseInd=0;
426 for (int i=0; i<sampleCount; i++) {
427 DivSample* s=parent->song.sample[i];
428 if (!s->renderOn[0][sysID]) {
429 sampleOffVOX[i]=0;
430 continue;
431 }
432
433 int paddedLen=s->lengthVOX;
434 // fit to single bank size
435 if (paddedLen>65536-0x400) {
436 paddedLen=65536-0x400;
437 }
438 // 32 phrase per bank
439 if ((phraseInd>=32)||((memPos&0xff0000)!=((memPos+paddedLen)&0xff0000))) {
440 memPos=((memPos+0xffff)&0xff0000)+0x400;
441 bankInd++;
442 phraseInd=0;
443 }
444 if (memPos>=getSampleMemCapacity(0)) {
445 logW("out of ADPCM memory for sample %d!",i);
446 break;
447 }
448 if (memPos+paddedLen>=getSampleMemCapacity(0)) {
449 memcpy(adpcmMem+memPos,s->dataVOX,getSampleMemCapacity(0)-memPos);
450 logW("out of ADPCM memory for sample %d!",i);
451 } else {
452 memcpy(adpcmMem+memPos,s->dataVOX,paddedLen);
453 sampleLoaded[i]=true;
454 }
455 sampleOffVOX[i]=memPos;
456 bankedPhrase[i].bank=bankInd;
457 bankedPhrase[i].phrase=phraseInd;
458 bankedPhrase[i].length=paddedLen;
459 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+paddedLen));
460 memPos+=paddedLen;
461 phraseInd++;
462 }
463 adpcmMemLen=memPos+256;
464
465 // phrase book
466 for (int i=0; i<sampleCount; i++) {
467 int endPos=sampleOffVOX[i]+bankedPhrase[i].length;
468 for (int b=0; b<4; b++) {
469 unsigned int bankedAddr=((unsigned int)bankedPhrase[i].bank<<16)+(b<<8)+(bankedPhrase[i].phrase*8);
470 adpcmMem[bankedAddr]=b;
471 adpcmMem[bankedAddr+1]=(sampleOffVOX[i]>>8)&0xff;
472 adpcmMem[bankedAddr+2]=(sampleOffVOX[i])&0xff;
473 adpcmMem[bankedAddr+3]=b;
474 adpcmMem[bankedAddr+4]=(endPos>>8)&0xff;
475 adpcmMem[bankedAddr+5]=(endPos)&0xff;
476 }
477 }
478 } else {
479 if (sampleCount>127) {
480 // mark the rest as unavailable
481 for (int i=127; i<sampleCount; i++) {
482 sampleLoaded[i]=false;
483 }
484 sampleCount=127;
485 }
486 for (int i=0; i<sampleCount; i++) {
487 DivSample* s=parent->song.sample[i];
488 if (!s->renderOn[0][sysID]) {
489 sampleOffVOX[i]=0;
490 continue;
491 }
492
493 int paddedLen=s->lengthVOX;
494 if (memPos>=getSampleMemCapacity(0)) {
495 logW("out of ADPCM memory for sample %d!",i);
496 break;
497 }
498 if (memPos+paddedLen>=getSampleMemCapacity(0)) {
499 memcpy(adpcmMem+memPos,s->dataVOX,getSampleMemCapacity(0)-memPos);
500 logW("out of ADPCM memory for sample %d!",i);
501 } else {
502 memcpy(adpcmMem+memPos,s->dataVOX,paddedLen);
503 sampleLoaded[i]=true;
504 }
505 sampleOffVOX[i]=memPos;
506 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+paddedLen));
507 memPos+=paddedLen;
508 }
509 adpcmMemLen=memPos+256;
510
511 // phrase book
512 for (int i=0; i<sampleCount; i++) {
513 DivSample* s=parent->song.sample[i];
514 int endPos=sampleOffVOX[i]+s->lengthVOX;
515 adpcmMem[i*8]=(sampleOffVOX[i]>>16)&0xff;
516 adpcmMem[1+i*8]=(sampleOffVOX[i]>>8)&0xff;
517 adpcmMem[2+i*8]=(sampleOffVOX[i])&0xff;
518 adpcmMem[3+i*8]=(endPos>>16)&0xff;
519 adpcmMem[4+i*8]=(endPos>>8)&0xff;
520 adpcmMem[5+i*8]=(endPos)&0xff;
521 }
522 }
523
524 memCompo.capacity=getSampleMemCapacity(0);
525 memCompo.used=adpcmMemLen;
526
527 delete[] sampleOffVOX;
528 }
529
530 void DivPlatformMSM6295::setFlags(const DivConfig& flags) {
531 rateSelInit=flags.getBool("rateSel",false);
532 isBanked=flags.getBool("isBanked",false);
533 switch (flags.getInt("clockSel",0)) {
534 case 1:
535 chipClock=4224000/4;
536 break;
537 case 2:
538 chipClock=4000000;
539 break;
540 case 3:
541 chipClock=4224000;
542 break;
543 case 4:
544 chipClock=COLOR_NTSC;
545 break;
546 case 5:
547 chipClock=COLOR_NTSC/2.0;
548 break;
549 case 6:
550 chipClock=COLOR_NTSC*2.0/7.0;
551 break;
552 case 7:
553 chipClock=COLOR_NTSC/4.0;
554 break;
555 case 8:
556 chipClock=4000000/2;
557 break;
558 case 9:
559 chipClock=4224000/2;
560 break;
561 case 10:
562 chipClock=875000;
563 break;
564 case 11:
565 chipClock=937500;
566 break;
567 case 12:
568 chipClock=1500000;
569 break;
570 case 13:
571 chipClock=3000000;
572 break;
573 case 14:
574 chipClock=COLOR_NTSC/3.0;
575 break;
576 case 15:
577 chipClock=3200000;
578 break;
579 default:
580 chipClock=4000000/4;
581 break;
582 }
583 CHECK_CUSTOM_CLOCK;
584 rate=chipClock/3;
585 for (int i=0; i<4; i++) {
586 oscBuf[i]->setRate(rate);;
587 }
588 if (rateSel!=rateSelInit) {
589 rWrite(12,!rateSelInit);
590 rateSel=rateSelInit;
591 }
592 rWrite(14,isBanked?0x81:0);
593 }
594
595 int DivPlatformMSM6295::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
596 parent=p;
597 adpcmMem=new unsigned char[16777216];
598 adpcmMemLen=0;
599 dumpWrites=false;
600 skipRegisterWrites=false;
601 updateOsc=0;
602 for (int i=0; i<4; i++) {
603 isMuted[i]=false;
604 oscBuf[i]=new DivDispatchOscBuffer;
605 }
606 setFlags(flags);
607 reset();
608 return 4;
609 }
610
611 void DivPlatformMSM6295::quit() {
612 for (int i=0; i<4; i++) {
613 delete oscBuf[i];
614 }
615 delete[] adpcmMem;
616 }
617
618 // initialization of important arrays
619 DivPlatformMSM6295::DivPlatformMSM6295():
620 DivDispatch(),
621 vgsound_emu_mem_intf(),
622 msm(*this) {
623 bankedPhrase=new BankedPhrase[32768];
624 sampleLoaded=new bool[32768];
625 }
626
627 DivPlatformMSM6295::~DivPlatformMSM6295() {
628 delete[] bankedPhrase;
629 delete[] sampleLoaded;
630 }