Debian Codegraph @

src/engine/platform/mmc5.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 "mmc5.h"
21 #include "sound/nes/mmc5.h"
22 #include "../engine.h"
23 #include <math.h>
24
25 #define CHIP_DIVIDER 16
26
27 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite((a),v)); if (dumpWrites) {addWrite(a,v);} }
28
29 const char* regCheatSheetMMC5[]={
30 "S0Volume", "5000",
31 "S0PeriodL", "5002",
32 "S0PeriodH", "5003",
33 "S1Volume", "5004",
34 "S1PeriodL", "5006",
35 "S1PeriodH", "5007",
36 "PCMControl", "4010",
37 "PCMWrite", "4011",
38 "APUControl", "4015",
39 NULL
40 };
41
42 const char** DivPlatformMMC5::getRegisterSheet() {
43 return regCheatSheetMMC5;
44 }
45
46 void DivPlatformMMC5::acquireDirect(blip_buffer_t** bb, size_t len) {
47 for (int i=0; i<3; i++) {
48 oscBuf[i]->begin(len);
49 mmc5->oscBuf[i]=oscBuf[i];
50 }
51
52 mmc5->bb=bb[0];
53 mmc5->timestamp=0;
54
55 while (!writes.empty()) {
56 QueuedWrite w=writes.front();
57 regPool[(w.addr)&0x7f]=w.val;
58 extcl_cpu_wr_mem_MMC5(mmc5,0,w.addr,w.val);
59 writes.pop();
60 }
61
62 // TODO: does this matter?
63 extcl_envelope_clock_MMC5(mmc5);
64 extcl_length_clock_MMC5(mmc5);
65 for (size_t i=0; i<len; i++) {
66 // heuristic
67 int pcmAdvance=1;
68 if (dacSample==-1) {
69 break;
70 } else {
71 pcmAdvance=len-i;
72 if (dacRate>0) {
73 int remainTime=(rate-dacPeriod+dacRate-1)/dacRate;
74 if (remainTime<pcmAdvance) pcmAdvance=remainTime;
75 if (remainTime<1) pcmAdvance=1;
76 }
77 }
78
79 i+=pcmAdvance-1;
80
81 if (dacSample!=-1) {
82 dacPeriod+=dacRate*pcmAdvance;
83 if (dacPeriod>=rate) {
84 DivSample* s=parent->getSample(dacSample);
85 if (s->samples>0 && dacPos<s->samples) {
86 if (!isMuted[2]) {
87 extcl_cpu_wr_mem_MMC5(mmc5,i,0x5011,((unsigned char)s->data8[dacPos]+0x80));
88 }
89 dacPos++;
90 if (s->isLoopable() && dacPos>=(unsigned int)s->loopEnd) {
91 dacPos=s->loopStart;
92 } else if (dacPos>=s->samples) {
93 dacSample=-1;
94 }
95 dacPeriod-=rate;
96 } else {
97 dacSample=-1;
98 }
99 }
100 }
101 }
102 extcl_apu_tick_MMC5(mmc5,len);
103
104 for (int i=0; i<3; i++) {
105 oscBuf[i]->end(len);
106 }
107 }
108
109 void DivPlatformMMC5::tick(bool sysTick) {
110 for (int i=0; i<2; i++) {
111 chan[i].std.next();
112 if (chan[i].std.vol.had) {
113 // ok, why are the volumes like that?
114 chan[i].outVol=VOL_SCALE_LINEAR_BROKEN(chan[i].vol&15,MIN(15,chan[i].std.vol.val),15);
115 if (chan[i].outVol<0) chan[i].outVol=0;
116 rWrite(0x5000+i*4,0x30|chan[i].outVol|((chan[i].duty&3)<<6));
117 }
118 // TODO: arp macros on NES PCM?
119 if (NEW_ARP_STRAT) {
120 chan[i].handleArp();
121 } else if (chan[i].std.arp.had) {
122 if (!chan[i].inPorta) {
123 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
124 }
125 chan[i].freqChanged=true;
126 }
127 if (chan[i].std.duty.had) {
128 chan[i].duty=chan[i].std.duty.val;
129 rWrite(0x5000+i*4,0x30|chan[i].outVol|((chan[i].duty&3)<<6));
130 }
131 if (chan[i].std.pitch.had) {
132 if (chan[i].std.pitch.mode) {
133 chan[i].pitch2+=chan[i].std.pitch.val;
134 CLAMP_VAR(chan[i].pitch2,-32768,32767);
135 } else {
136 chan[i].pitch2=chan[i].std.pitch.val;
137 }
138 chan[i].freqChanged=true;
139 }
140 if (chan[i].std.phaseReset.had) {
141 if (chan[i].std.phaseReset.val==1) {
142 chan[i].freqChanged=true;
143 chan[i].prevFreq=-1;
144 }
145 }
146 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
147 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)-1;
148 if (chan[i].freq>2047) chan[i].freq=2047;
149 if (chan[i].freq<0) chan[i].freq=0;
150 if (chan[i].keyOn) {
151 //rWrite(16+i*5+1,((chan[i].duty&3)<<6)|(63-(ins->gb.soundLen&63)));
152 //rWrite(16+i*5+2,((chan[i].vol<<4))|(ins->gb.envLen&7)|((ins->gb.envDir&1)<<3));
153 }
154 if (chan[i].keyOff) {
155 //rWrite(16+i*5+2,8);
156 rWrite(0x5000+i*4,0x30);
157 }
158 rWrite(0x5002+i*4,chan[i].freq&0xff);
159 if ((chan[i].prevFreq>>8)!=(chan[i].freq>>8)) {
160 rWrite(0x5003+i*4,0xf8|(chan[i].freq>>8));
161 }
162 if (chan[i].freq!=65535 && chan[i].freq!=0) {
163 chan[i].prevFreq=chan[i].freq;
164 }
165 if (chan[i].keyOn) chan[i].keyOn=false;
166 if (chan[i].keyOff) chan[i].keyOff=false;
167 chan[i].freqChanged=false;
168 }
169 }
170
171 // PCM
172 if (chan[2].freqChanged) {
173 chan[2].freq=parent->calcFreq(chan[2].baseFreq,chan[2].pitch,chan[2].fixedArp?chan[2].baseNoteOverride:chan[2].arpOff,chan[2].fixedArp,false,0,chan[2].pitch2,1,1);
174 if (chan[2].furnaceDac) {
175 double off=1.0;
176 if (dacSample>=0 && dacSample<parent->song.sampleLen) {
177 DivSample* s=parent->getSample(dacSample);
178 off=(double)s->centerRate/parent->getCenterRate();
179 }
180 dacRate=MIN(chan[2].freq*off,32000);
181 if (dumpWrites) addWrite(0xffff0001,dacRate);
182 }
183 chan[2].freqChanged=false;
184 }
185 }
186
187 int DivPlatformMMC5::dispatch(DivCommand c) {
188 switch (c.cmd) {
189 case DIV_CMD_NOTE_ON:
190 if (c.chan==2) { // PCM
191 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_STD);
192 if (ins->type==DIV_INS_AMIGA) {
193 if (c.value!=DIV_NOTE_NULL) {
194 dacSample=ins->amiga.getSample(c.value);
195 chan[c.chan].sampleNote=c.value;
196 c.value=ins->amiga.getFreq(c.value);
197 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
198 } else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) {
199 dacSample=ins->amiga.getSample(chan[c.chan].sampleNote);
200 c.value=ins->amiga.getFreq(chan[c.chan].sampleNote);
201 }
202 if (dacSample<0 || dacSample>=parent->song.sampleLen) {
203 dacSample=-1;
204 if (dumpWrites) addWrite(0xffff0002,0);
205 break;
206 } else {
207 if (dumpWrites) addWrite(0xffff0000,dacSample);
208 }
209 if (chan[c.chan].setPos) {
210 chan[c.chan].setPos=false;
211 } else {
212 dacPos=0;
213 }
214 dacPeriod=0;
215 if (c.value!=DIV_NOTE_NULL) {
216 chan[c.chan].baseFreq=parent->calcBaseFreq(1,1,c.value,false);
217 chan[c.chan].freqChanged=true;
218 chan[c.chan].note=c.value;
219 }
220 chan[c.chan].active=true;
221 chan[c.chan].keyOn=true;
222 chan[c.chan].furnaceDac=true;
223 } else {
224 if (c.value!=DIV_NOTE_NULL) {
225 chan[c.chan].note=c.value;
226 }
227 dacSample=12*sampleBank+chan[c.chan].note%12;
228 if (dacSample>=parent->song.sampleLen) {
229 dacSample=-1;
230 if (dumpWrites) addWrite(0xffff0002,0);
231 break;
232 } else {
233 if (dumpWrites) addWrite(0xffff0000,dacSample);
234 }
235 if (chan[c.chan].setPos) {
236 chan[c.chan].setPos=false;
237 } else {
238 dacPos=0;
239 }
240 dacPeriod=0;
241 dacRate=parent->getSample(dacSample)->rate;
242 if (dumpWrites) addWrite(0xffff0001,dacRate);
243 chan[c.chan].furnaceDac=false;
244 }
245 break;
246 } else {
247 if (c.value!=DIV_NOTE_NULL) {
248 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
249 }
250 }
251 if (c.value!=DIV_NOTE_NULL) {
252 chan[c.chan].freqChanged=true;
253 chan[c.chan].note=c.value;
254 }
255 chan[c.chan].active=true;
256 chan[c.chan].keyOn=true;
257 chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_STD));
258 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
259 chan[c.chan].outVol=chan[c.chan].vol;
260 }
261 rWrite(0x5000+c.chan*4,0x30|chan[c.chan].vol|((chan[c.chan].duty&3)<<6));
262 break;
263 case DIV_CMD_NOTE_OFF:
264 if (c.chan==2) {
265 dacSample=-1;
266 if (dumpWrites) addWrite(0xffff0002,0);
267 }
268 chan[c.chan].active=false;
269 chan[c.chan].keyOff=true;
270 chan[c.chan].macroInit(NULL);
271 break;
272 case DIV_CMD_NOTE_OFF_ENV:
273 case DIV_CMD_ENV_RELEASE:
274 chan[c.chan].std.release();
275 break;
276 case DIV_CMD_INSTRUMENT:
277 if (chan[c.chan].ins!=c.value || c.value2==1) {
278 chan[c.chan].ins=c.value;
279 }
280 break;
281 case DIV_CMD_VOLUME:
282 if (chan[c.chan].vol!=c.value) {
283 chan[c.chan].vol=c.value;
284 if (!chan[c.chan].std.vol.has) {
285 chan[c.chan].outVol=c.value;
286 }
287 if (chan[c.chan].active) {
288 rWrite(0x5000+c.chan*4,0x30|chan[c.chan].vol|((chan[c.chan].duty&3)<<6));
289 }
290 }
291 break;
292 case DIV_CMD_GET_VOLUME:
293 return chan[c.chan].vol;
294 break;
295 case DIV_CMD_PITCH:
296 chan[c.chan].pitch=c.value;
297 chan[c.chan].freqChanged=true;
298 break;
299 case DIV_CMD_NOTE_PORTA: {
300 int destFreq=(c.chan==2)?(parent->calcBaseFreq(1,1,c.value2+chan[c.chan].sampleNoteDelta,false)):(NOTE_PERIODIC(c.value2));
301 bool return2=false;
302 if (destFreq>chan[c.chan].baseFreq) {
303 chan[c.chan].baseFreq+=c.value;
304 if (chan[c.chan].baseFreq>=destFreq) {
305 chan[c.chan].baseFreq=destFreq;
306 return2=true;
307 }
308 } else {
309 chan[c.chan].baseFreq-=c.value;
310 if (chan[c.chan].baseFreq<=destFreq) {
311 chan[c.chan].baseFreq=destFreq;
312 return2=true;
313 }
314 }
315 chan[c.chan].freqChanged=true;
316 if (return2) {
317 chan[c.chan].inPorta=false;
318 return 2;
319 }
320 break;
321 }
322 case DIV_CMD_STD_NOISE_MODE:
323 chan[c.chan].duty=c.value;
324 rWrite(0x5000+c.chan*4,0x30|(chan[c.chan].active?chan[c.chan].outVol:0)|((chan[c.chan].duty&3)<<6));
325 break;
326 case DIV_CMD_SAMPLE_BANK:
327 sampleBank=c.value;
328 if (sampleBank>(parent->song.sample.size()/12)) {
329 sampleBank=parent->song.sample.size()/12;
330 }
331 break;
332 case DIV_CMD_SAMPLE_POS:
333 if (c.chan!=2) break;
334 dacPos=c.value;
335 chan[c.chan].setPos=true;
336 break;
337 case DIV_CMD_LEGATO:
338 if (c.chan==2) {
339 chan[c.chan].baseFreq=parent->calcBaseFreq(1,1,c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)),false);
340 } else {
341 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
342 }
343 chan[c.chan].freqChanged=true;
344 chan[c.chan].note=c.value;
345 break;
346 case DIV_CMD_PRE_PORTA:
347 if (chan[c.chan].active && c.value2) {
348 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_STD));
349 }
350 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);
351 chan[c.chan].inPorta=c.value;
352 break;
353 case DIV_CMD_GET_VOLMAX:
354 return 15;
355 break;
356 case DIV_CMD_MACRO_OFF:
357 chan[c.chan].std.mask(c.value,true);
358 break;
359 case DIV_CMD_MACRO_ON:
360 chan[c.chan].std.mask(c.value,false);
361 break;
362 case DIV_CMD_MACRO_RESTART:
363 chan[c.chan].std.restart(c.value);
364 break;
365 default:
366 break;
367 }
368 return 1;
369 }
370
371 void DivPlatformMMC5::muteChannel(int ch, bool mute) {
372 isMuted[ch]=mute;
373 mmc5->muted[ch]=mute;
374 }
375
376 void DivPlatformMMC5::forceIns() {
377 for (int i=0; i<3; i++) {
378 chan[i].insChanged=true;
379 chan[i].prevFreq=65535;
380 if (i<2) {
381 // TODO: implement envelope mode
382 rWrite(0x5000+i*4,(0x30)|(chan[i].active?chan[i].outVol:0)|((chan[i].duty&3)<<6));
383 }
384 }
385 }
386
387 void* DivPlatformMMC5::getChanState(int ch) {
388 return &chan[ch];
389 }
390
391 DivMacroInt* DivPlatformMMC5::getChanMacroInt(int ch) {
392 return &chan[ch].std;
393 }
394
395 DivDispatchOscBuffer* DivPlatformMMC5::getOscBuffer(int ch) {
396 return oscBuf[ch];
397 }
398
399 unsigned char* DivPlatformMMC5::getRegisterPool() {
400 return regPool;
401 }
402
403 int DivPlatformMMC5::getRegisterPoolSize() {
404 return 32;
405 }
406
407 float DivPlatformMMC5::getPostAmp() {
408 return 64.0f;
409 }
410
411 void DivPlatformMMC5::reset() {
412 while (!writes.empty()) writes.pop();
413 for (int i=0; i<3; i++) {
414 chan[i]=DivPlatformMMC5::Channel();
415 chan[i].std.setEngine(parent);
416 }
417 if (dumpWrites) {
418 addWrite(0xffffffff,0);
419 }
420
421 dacPeriod=0;
422 dacPos=0;
423 dacRate=0;
424 dacSample=-1;
425 sampleBank=0;
426
427 map_init_MMC5(mmc5);
428 memset(regPool,0,128);
429
430 mmc5->muted[0]=isMuted[0];
431 mmc5->muted[1]=isMuted[1];
432 mmc5->muted[2]=isMuted[2];
433
434 rWrite(0x5015,0x03);
435 rWrite(0x5010,0x00);
436
437 for (int i=0; i<2; i++) {
438 rWrite(0x5000+i*4,(0x30)|0|((chan[i].duty&3)<<6));
439 }
440 }
441
442 bool DivPlatformMMC5::keyOffAffectsArp(int ch) {
443 return true;
444 }
445
446 bool DivPlatformMMC5::hasAcquireDirect() {
447 return true;
448 }
449
450 void DivPlatformMMC5::setFlags(const DivConfig& flags) {
451 int clockSel=flags.getInt("clockSel",0);
452 if (clockSel==2) { // Dendy
453 chipClock=COLOR_PAL*2.0/5.0;
454 } else if (clockSel==1) { // PAL
455 chipClock=COLOR_PAL*3.0/8.0;
456 } else { // NTSC
457 chipClock=COLOR_NTSC/2.0;
458 }
459 CHECK_CUSTOM_CLOCK;
460 rate=chipClock;
461 for (int i=0; i<3; i++) {
462 oscBuf[i]->setRate(rate);
463 }
464 }
465
466 void DivPlatformMMC5::notifyInsDeletion(void* ins) {
467 for (int i=0; i<3; i++) {
468 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
469 }
470 }
471
472 void DivPlatformMMC5::poke(unsigned int addr, unsigned short val) {
473 rWrite(addr,val);
474 }
475
476 void DivPlatformMMC5::poke(std::vector<DivRegWrite>& wlist) {
477 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
478 }
479
480 int DivPlatformMMC5::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
481 parent=p;
482 dumpWrites=false;
483 skipRegisterWrites=false;
484 writeOscBuf=0;
485 mmc5=new struct _mmc5;
486 for (int i=0; i<3; i++) {
487 isMuted[i]=false;
488 oscBuf[i]=new DivDispatchOscBuffer;
489 //mmc5->muted[i]=false; // TODO
490 }
491 setFlags(flags);
492
493 init_nla_table(500,500);
494 reset();
495 return 5;
496 }
497
498 void DivPlatformMMC5::quit() {
499 for (int i=0; i<3; i++) {
500 delete oscBuf[i];
501 }
502 delete mmc5;
503 }
504
505 DivPlatformMMC5::~DivPlatformMMC5() {
506 }