Debian Codegraph @

src/engine/platform/ymz280b.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 "ymz280b.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include <math.h>
24
25 #define CHIP_FREQBASE 25165824
26
27 #define rWrite(a,v) {if(!skipRegisterWrites) {ymz280b.write(0,a); ymz280b.write(1,v); regPool[a]=v; if(dumpWrites) addWrite(a,v); }}
28
29 const char* regCheatSheetYMZ280B[]={
30 "CHx_Freq", "00+x*4",
31 "CHx_Control", "01+x*4",
32 "CHx_Volume", "02+x*4",
33 "CHx_Panning", "03+x*4",
34 "CHx_StartH", "20+x*4",
35 "CHx_LoopStartH", "21+x*4",
36 "CHx_LoopEndH", "22+x*4",
37 "CHx_EndH", "23+x*4",
38 "CHx_StartM", "40+x*4",
39 "CHx_LoopStartM", "41+x*4",
40 "CHx_LoopEndM", "42+x*4",
41 "CHx_EndM", "43+x*4",
42 "CHx_StartL", "60+x*4",
43 "CHx_LoopStartL", "61+x*4",
44 "CHx_LoopEndL", "62+x*4",
45 "CHx_EndL", "63+x*4",
46 "DSP_Channel", "80",
47 "DSP_Enable", "81",
48 "DSP_Data", "82",
49 "RAM_AddrH", "84",
50 "RAM_AddrM", "85",
51 "RAM_AddrL", "86",
52 "RAM_Data", "87",
53 "IRQ_Enable", "E0",
54 "Enable", "FF",
55 NULL
56 };
57
58 const char** DivPlatformYMZ280B::getRegisterSheet() {
59 return regCheatSheetYMZ280B;
60 }
61
62 // not this crap again... I hate while loops!
63 void DivPlatformYMZ280B::acquire(short** buf, size_t len) {
64 short why[16][256];
65 short *bufPtrs[16]={
66 why[0],why[1],why[2],why[3],why[4],why[5],why[6],why[7],
67 why[8],why[9],why[10],why[11],why[12],why[13],why[14],why[15]
68 };
69
70 for (int i=0; i<8; i++) {
71 oscBuf[i]->begin(len);
72 }
73
74 size_t lenCopy=len;
75 size_t pos=0;
76 while (lenCopy > 0) {
77 size_t blockLen = MIN(lenCopy, 256);
78 ymz280b.sound_stream_update(bufPtrs, blockLen);
79 for (size_t i=0; i<blockLen; i++) {
80 int dataL=0;
81 int dataR=0;
82 for (int j=0; j<8; j++) {
83 dataL+=why[j*2][i];
84 dataR+=why[j*2+1][i];
85 oscBuf[j]->putSample(pos,(short)(((int)why[j*2][i]+why[j*2+1][i])/4));
86 }
87 buf[0][pos]=(short)(dataL/8);
88 buf[1][pos]=(short)(dataR/8);
89 pos++;
90 }
91 lenCopy-=blockLen;
92 }
93
94 for (int i=0; i<8; i++) {
95 oscBuf[i]->end(len);
96 }
97 }
98
99 void DivPlatformYMZ280B::tick(bool sysTick) {
100 for (int i=0; i<8; i++) {
101 chan[i].std.next();
102 if (chan[i].std.vol.had) {
103 chan[i].outVol=((chan[i].vol&0xff)*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul;
104 writeOutVol(i);
105 }
106 if (NEW_ARP_STRAT) {
107 chan[i].handleArp();
108 } else if (chan[i].std.arp.had) {
109 if (!chan[i].inPorta) {
110 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
111 }
112 chan[i].freqChanged=true;
113 }
114 if (chan[i].std.pitch.had) {
115 if (chan[i].std.pitch.mode) {
116 chan[i].pitch2+=chan[i].std.pitch.val;
117 CLAMP_VAR(chan[i].pitch2,-32768,32767);
118 } else {
119 chan[i].pitch2=chan[i].std.pitch.val;
120 }
121 chan[i].freqChanged=true;
122 }
123 if (chan[i].std.panL.had) { // panning
124 if (chan[i].isNewYMZ) {
125 chan[i].panning=8+chan[i].std.panL.val;
126 } else {
127 chan[i].panning=MIN((chan[i].std.panL.val*15/16+15)/2+1,15);
128 }
129 rWrite(0x03+i*4,chan[i].panning);
130 }
131 if (chan[i].std.phaseReset.had) {
132 if ((chan[i].std.phaseReset.val==1) && chan[i].active) {
133 chan[i].audPos=0;
134 chan[i].setPos=true;
135 }
136 }
137 if (chan[i].setPos) {
138 // force keyon
139 chan[i].keyOn=true;
140 chan[i].setPos=false;
141 } else {
142 chan[i].audPos=0;
143 }
144 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
145 DivSample* s=parent->getSample(chan[i].sample);
146 unsigned char ctrl;
147 switch (s->depth) {
148 case DIV_SAMPLE_DEPTH_YMZ_ADPCM: ctrl=0x20; break;
149 case DIV_SAMPLE_DEPTH_8BIT: ctrl=0x40; break;
150 case DIV_SAMPLE_DEPTH_16BIT: ctrl=0x60; break;
151 default: ctrl=0;
152 }
153 double off=(s->centerRate>=1)?((double)s->centerRate/parent->getCenterRate()):1.0;
154 chan[i].freq=(int)round(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)/256.0)-1;
155 if (chan[i].freq<0) chan[i].freq=0;
156 if (chan[i].freq>511) chan[i].freq=511;
157 // ADPCM has half the range
158 if (s->depth==DIV_SAMPLE_DEPTH_YMZ_ADPCM && chan[i].freq>255) chan[i].freq=255;
159 ctrl|=(chan[i].active?0x80:0)|((s->isLoopable())?0x10:0)|(chan[i].freq>>8);
160 if (chan[i].keyOn) {
161 unsigned int start=0;
162 unsigned int loopStart=0;
163 unsigned int loopEnd=0;
164 unsigned int end=0;
165 if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
166 start=sampleOff[chan[i].sample];
167 end=MIN(start+s->getCurBufLen(),getSampleMemCapacity()-1);
168 }
169 if (s->isLoopable()) {
170 switch (s->depth) {
171 case DIV_SAMPLE_DEPTH_YMZ_ADPCM: loopStart=start+s->loopStart/2; loopEnd=start+s->loopEnd/2; break;
172 case DIV_SAMPLE_DEPTH_8BIT: loopStart=start+s->loopStart; loopEnd=start+s->loopEnd; break;
173 case DIV_SAMPLE_DEPTH_16BIT: loopStart=start+s->loopStart*2; loopEnd=start+s->loopEnd*2; break;
174 default: break;
175 }
176 loopEnd=MIN(loopEnd,end);
177 loopStart=MIN(loopStart,loopEnd);
178 }
179 if (chan[i].audPos>0) {
180 switch (s->depth) {
181 case DIV_SAMPLE_DEPTH_YMZ_ADPCM: start+=chan[i].audPos/2; break;
182 case DIV_SAMPLE_DEPTH_8BIT: start+=chan[i].audPos; break;
183 case DIV_SAMPLE_DEPTH_16BIT: start+=chan[i].audPos*2; break;
184 default: break;
185 }
186 start=MIN(start,end);
187 }
188 rWrite(0x01+i*4,ctrl&~0x80); // force keyoff first
189 rWrite(0x20+i*4,(start>>16)&0xff);
190 rWrite(0x21+i*4,(loopStart>>16)&0xff);
191 rWrite(0x22+i*4,(loopEnd>>16)&0xff);
192 rWrite(0x23+i*4,(end>>16)&0xff);
193 rWrite(0x40+i*4,(start>>8)&0xff);
194 rWrite(0x41+i*4,(loopStart>>8)&0xff);
195 rWrite(0x42+i*4,(loopEnd>>8)&0xff);
196 rWrite(0x43+i*4,(end>>8)&0xff);
197 rWrite(0x60+i*4,start&0xff);
198 rWrite(0x61+i*4,loopStart&0xff);
199 rWrite(0x62+i*4,loopEnd&0xff);
200 rWrite(0x63+i*4,end&0xff);
201 if (!chan[i].std.vol.had) {
202 chan[i].outVol=chan[i].vol;
203 writeOutVol(i);
204 }
205 chan[i].keyOn=false;
206 }
207 if (chan[i].keyOff) {
208 chan[i].keyOff=false;
209 }
210 if (chan[i].freqChanged) {
211 rWrite(0x00+i*4,chan[i].freq&0xff);
212 chan[i].freqChanged=false;
213 }
214 rWrite(0x01+i*4,ctrl);
215 }
216 }
217 }
218
219 int DivPlatformYMZ280B::dispatch(DivCommand c) {
220 switch (c.cmd) {
221 case DIV_CMD_NOTE_ON: {
222 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA);
223 chan[c.chan].isNewYMZ=ins->type==DIV_INS_YMZ280B;
224 chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:255;
225 if (c.value!=DIV_NOTE_NULL) {
226 chan[c.chan].sample=ins->amiga.getSample(c.value);
227 chan[c.chan].sampleNote=c.value;
228 c.value=ins->amiga.getFreq(c.value);
229 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
230 }
231 if (c.value!=DIV_NOTE_NULL) {
232 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
233 }
234 if (chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) {
235 chan[c.chan].sample=-1;
236 }
237 if (c.value!=DIV_NOTE_NULL) {
238 chan[c.chan].freqChanged=true;
239 chan[c.chan].note=c.value;
240 }
241 chan[c.chan].active=true;
242 chan[c.chan].keyOn=true;
243 chan[c.chan].macroInit(ins);
244 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
245 chan[c.chan].outVol=chan[c.chan].vol;
246 }
247 break;
248 }
249 case DIV_CMD_NOTE_OFF:
250 chan[c.chan].sample=-1;
251 chan[c.chan].active=false;
252 chan[c.chan].keyOff=true;
253 chan[c.chan].macroInit(NULL);
254 break;
255 case DIV_CMD_NOTE_OFF_ENV:
256 case DIV_CMD_ENV_RELEASE:
257 chan[c.chan].std.release();
258 break;
259 case DIV_CMD_INSTRUMENT:
260 if (chan[c.chan].ins!=c.value || c.value2==1) {
261 chan[c.chan].ins=c.value;
262 }
263 break;
264 case DIV_CMD_VOLUME:
265 if (chan[c.chan].vol!=c.value) {
266 chan[c.chan].vol=c.value;
267 if (!chan[c.chan].std.vol.has) {
268 chan[c.chan].outVol=c.value;
269 writeOutVol(c.chan);
270 }
271 }
272 break;
273 case DIV_CMD_GET_VOLUME:
274 if (chan[c.chan].std.vol.has) {
275 return chan[c.chan].vol;
276 }
277 return chan[c.chan].outVol;
278 break;
279 case DIV_CMD_PANNING:
280 chan[c.chan].panning=MIN(parent->convertPanSplitToLinearLR(c.value,c.value2,15)+1,15);
281 rWrite(0x03+c.chan*4,chan[c.chan].panning);
282 break;
283 case DIV_CMD_PITCH:
284 chan[c.chan].pitch=c.value;
285 chan[c.chan].freqChanged=true;
286 break;
287 case DIV_CMD_NOTE_PORTA: {
288 int destFreq=NOTE_FREQUENCY(c.value2+chan[c.chan].sampleNoteDelta);
289 bool return2=false;
290 int multiplier=(parent->song.linearPitch==2)?1:256;
291 if (destFreq>chan[c.chan].baseFreq) {
292 chan[c.chan].baseFreq+=c.value*multiplier;
293 if (chan[c.chan].baseFreq>=destFreq) {
294 chan[c.chan].baseFreq=destFreq;
295 return2=true;
296 }
297 } else {
298 chan[c.chan].baseFreq-=c.value*multiplier;
299 if (chan[c.chan].baseFreq<=destFreq) {
300 chan[c.chan].baseFreq=destFreq;
301 return2=true;
302 }
303 }
304 chan[c.chan].freqChanged=true;
305 if (return2) {
306 chan[c.chan].inPorta=false;
307 return 2;
308 }
309 break;
310 }
311 case DIV_CMD_LEGATO: {
312 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(0)));
313 chan[c.chan].freqChanged=true;
314 chan[c.chan].note=c.value;
315 break;
316 }
317 case DIV_CMD_PRE_PORTA:
318 if (chan[c.chan].active && c.value2) {
319 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA));
320 }
321 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);
322 chan[c.chan].inPorta=c.value;
323 break;
324 case DIV_CMD_SAMPLE_POS:
325 chan[c.chan].audPos=c.value;
326 chan[c.chan].setPos=true;
327 break;
328 case DIV_CMD_GET_VOLMAX:
329 return 255;
330 break;
331 case DIV_CMD_MACRO_OFF:
332 chan[c.chan].std.mask(c.value,true);
333 break;
334 case DIV_CMD_MACRO_ON:
335 chan[c.chan].std.mask(c.value,false);
336 break;
337 case DIV_CMD_MACRO_RESTART:
338 chan[c.chan].std.restart(c.value);
339 break;
340 default:
341 break;
342 }
343 return 1;
344 }
345
346 void DivPlatformYMZ280B::writeOutVol(int ch) {
347 unsigned char val=isMuted[ch]?0:chan[ch].outVol;
348 rWrite(0x02+ch*4,val);
349 }
350
351 void DivPlatformYMZ280B::muteChannel(int ch, bool mute) {
352 isMuted[ch]=mute;
353 writeOutVol(ch);
354 }
355
356 void DivPlatformYMZ280B::forceIns() {
357 for (int i=0; i<8; i++) {
358 chan[i].insChanged=true;
359 chan[i].freqChanged=true;
360 chan[i].sample=-1;
361
362 rWrite(0x03+i*4,chan[i].panning);
363 }
364 }
365
366 void* DivPlatformYMZ280B::getChanState(int ch) {
367 return &chan[ch];
368 }
369
370 DivMacroInt* DivPlatformYMZ280B::getChanMacroInt(int ch) {
371 return &chan[ch].std;
372 }
373
374 unsigned short DivPlatformYMZ280B::getPan(int ch) {
375 return parent->convertPanLinearToSplit(chan[ch].panning,8,15);
376 }
377
378 DivDispatchOscBuffer* DivPlatformYMZ280B::getOscBuffer(int ch) {
379 return oscBuf[ch];
380 }
381
382 void DivPlatformYMZ280B::reset() {
383 memset(regPool,0,256);
384 ymz280b.device_reset();
385 rWrite(0xff,0x80); // enable keyon
386 for (int i=0; i<8; i++) {
387 chan[i]=DivPlatformYMZ280B::Channel();
388 chan[i].std.setEngine(parent);
389 rWrite(0x02+i*4,255);
390 rWrite(0x03+i*4,8);
391 }
392 }
393
394 int DivPlatformYMZ280B::getOutputCount() {
395 return 2;
396 }
397
398 void DivPlatformYMZ280B::notifyInsChange(int ins) {
399 for (int i=0; i<8; i++) {
400 if (chan[i].ins==ins) {
401 chan[i].insChanged=true;
402 }
403 }
404 }
405
406 void DivPlatformYMZ280B::notifyWaveChange(int wave) {
407 // TODO when wavetables are added
408 // TODO they probably won't be added unless the samples reside in RAM
409 }
410
411 void DivPlatformYMZ280B::notifyInsDeletion(void* ins) {
412 for (int i=0; i<8; i++) {
413 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
414 }
415 }
416
417 void DivPlatformYMZ280B::poke(unsigned int addr, unsigned short val) {
418 rWrite(addr,val);
419 }
420
421 void DivPlatformYMZ280B::poke(std::vector<DivRegWrite>& wlist) {
422 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
423 }
424
425 unsigned char* DivPlatformYMZ280B::getRegisterPool() {
426 return regPool;
427 }
428
429 int DivPlatformYMZ280B::getRegisterPoolSize() {
430 return 256;
431 }
432
433 float DivPlatformYMZ280B::getPostAmp() {
434 // according to MAME core's mixing
435 return 4.0f;
436 }
437
438 const void* DivPlatformYMZ280B::getSampleMem(int index) {
439 return index == 0 ? sampleMem : NULL;
440 }
441
442 size_t DivPlatformYMZ280B::getSampleMemCapacity(int index) {
443 return index == 0 ? 16777216 : 0;
444 }
445
446 size_t DivPlatformYMZ280B::getSampleMemUsage(int index) {
447 return index == 0 ? sampleMemLen : 0;
448 }
449
450 bool DivPlatformYMZ280B::isSampleLoaded(int index, int sample) {
451 if (index!=0) return false;
452 if (sample<0 || sample>32767) return false;
453 return sampleLoaded[sample];
454 }
455
456 const DivMemoryComposition* DivPlatformYMZ280B::getMemCompo(int index) {
457 if (index!=0) return NULL;
458 return &memCompo;
459 }
460
461 void DivPlatformYMZ280B::renderSamples(int sysID) {
462 memset(sampleMem,0,getSampleMemCapacity());
463 memset(sampleOff,0,32768*sizeof(unsigned int));
464 memset(sampleLoaded,0,32768*sizeof(bool));
465
466 memCompo=DivMemoryComposition();
467 memCompo.name="Sample ROM";
468
469 size_t memPos=0;
470 for (int i=0; i<parent->song.sampleLen; i++) {
471 DivSample* s=parent->song.sample[i];
472 if (!s->renderOn[0][sysID]) {
473 sampleOff[i]=0;
474 continue;
475 }
476
477 int length=s->getCurBufLen();
478 unsigned char* src=(unsigned char*)s->getCurBuf();
479 int actualLength=MIN((int)(getSampleMemCapacity()-memPos),length);
480 if (actualLength>0) {
481 #ifdef TA_BIG_ENDIAN
482 memcpy(&sampleMem[memPos],src,actualLength);
483 #else
484 if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
485 for (int i=0; i<actualLength; i++) {
486 sampleMem[memPos+i]=src[i^1];
487 }
488 } else {
489 memcpy(&sampleMem[memPos],src,actualLength);
490 }
491 #endif
492 sampleOff[i]=memPos;
493 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+length));
494 memPos+=length;
495 }
496 if (actualLength<length) {
497 logW("out of YMZ280B PCM memory for sample %d!",i);
498 break;
499 }
500 sampleLoaded[i]=true;
501 }
502 sampleMemLen=memPos;
503
504 memCompo.used=sampleMemLen;
505 memCompo.capacity=getSampleMemCapacity(0);
506 }
507
508 void DivPlatformYMZ280B::setChipModel(int type) {
509 chipType=type;
510 }
511
512 void DivPlatformYMZ280B::setFlags(const DivConfig& flags) {
513 switch (chipType) {
514 default:
515 case 280:
516 switch (flags.getInt("clockSel",0)) {
517 case 0x01:
518 chipClock=COLOR_NTSC*4.0;
519 break;
520 case 0x02:
521 chipClock=COLOR_PAL*16.0/5.0;
522 break;
523 case 0x03:
524 chipClock=16000000;
525 break;
526 case 0x04:
527 chipClock=50000000.0/3.0;
528 break;
529 case 0x05:
530 chipClock=14000000;
531 break;
532 default:
533 chipClock=16934400;
534 break;
535 }
536 CHECK_CUSTOM_CLOCK;
537 rate=chipClock/384;
538 break;
539 case 759:
540 rate=32000;
541 chipClock=rate*384;
542 break;
543 }
544 for (int i=0; i<8; i++) {
545 oscBuf[i]->setRate(rate);
546 }
547 }
548
549 int DivPlatformYMZ280B::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
550 parent=p;
551 dumpWrites=false;
552 skipRegisterWrites=false;
553
554 for (int i=0; i<8; i++) {
555 isMuted[i]=false;
556 oscBuf[i]=new DivDispatchOscBuffer;
557 }
558 sampleMem=new unsigned char[getSampleMemCapacity()];
559 sampleMemLen=0;
560 ymz280b.device_start(sampleMem);
561 setFlags(flags);
562 reset();
563
564 return 8;
565 }
566
567 void DivPlatformYMZ280B::quit() {
568 delete[] sampleMem;
569 for (int i=0; i<8; i++) {
570 delete oscBuf[i];
571 }
572 }
573
574 // initialization of important arrays
575 DivPlatformYMZ280B::DivPlatformYMZ280B() {
576 sampleOff=new unsigned int[32768];
577 sampleLoaded=new bool[32768];
578 }
579
580 DivPlatformYMZ280B::~DivPlatformYMZ280B() {
581 delete[] sampleOff;
582 delete[] sampleLoaded;
583 }