Debian Codegraph @

src/engine/platform/rf5c68.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 "rf5c68.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include <math.h>
24
25 #define rWrite(a,v) {if(!skipRegisterWrites) {rf5c68.rf5c68_w(a,v); regPool[a]=v; if(dumpWrites) addWrite(a,v);}}
26
27 #define CHIP_FREQBASE 786432
28
29 const char* regCheatSheetRF5C68[]={
30 "Volume", "0",
31 "Panning", "1",
32 "FreqL", "2",
33 "FreqH", "3",
34 "LoopStartL", "4",
35 "LoopStartH", "5",
36 "StartH", "6",
37 "Control", "7",
38 "Disable", "8",
39 NULL
40 };
41
42 const char** DivPlatformRF5C68::getRegisterSheet() {
43 return regCheatSheetRF5C68;
44 }
45
46 void DivPlatformRF5C68::chWrite(unsigned char ch, unsigned int addr, unsigned char val) {
47 if (!skipRegisterWrites) {
48 if (curChan!=ch) {
49 curChan=ch;
50 rWrite(7,curChan|0xc0);
51 }
52 regPool[16+((ch)<<4)+((addr)&0x0f)]=val;
53 rWrite(addr,val);
54 }
55 }
56
57 // TODO: this code is weird
58 // make sure newDispatch didn't break it up
59 void DivPlatformRF5C68::acquire(short** buf, size_t len) {
60 short bufC[16][256];
61 short* chBufPtrs[16]={
62 bufC[0],bufC[1],bufC[2],bufC[3],bufC[4],bufC[5],bufC[6],bufC[7],
63 bufC[8],bufC[9],bufC[10],bufC[11],bufC[12],bufC[13],bufC[14],bufC[15]
64 };
65 size_t pos=0;
66
67 for (int i=0; i<16; i++) {
68 memset(bufC[i],0,256*sizeof(short));
69 }
70
71 for (int i=0; i<8; i++) {
72 oscBuf[i]->begin(len);
73 }
74
75 size_t lenCopy=len;
76 while (lenCopy > 0) {
77 size_t blockLen=MIN(lenCopy,256);
78 short* bufPtrs[2]={&buf[0][pos],&buf[1][pos]};
79 rf5c68.sound_stream_update(bufPtrs,chBufPtrs,blockLen);
80 for (int i=0; i<8; i++) {
81 for (size_t j=0; j<blockLen; j++) {
82 oscBuf[i]->putSample(pos+j,(bufC[i*2][j]+bufC[i*2+1][j])>>1);
83 }
84 }
85 pos+=blockLen;
86 lenCopy-=blockLen;
87 }
88
89 for (int i=0; i<8; i++) {
90 oscBuf[i]->end(len);
91 }
92 }
93
94 void DivPlatformRF5C68::tick(bool sysTick) {
95 for (int i=0; i<8; i++) {
96 chan[i].std.next();
97 if (chan[i].std.vol.had) {
98 chan[i].outVol=((chan[i].vol&0xff)*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul;
99 chWrite(i,0,chan[i].outVol);
100 }
101 if (NEW_ARP_STRAT) {
102 chan[i].handleArp();
103 } else if (chan[i].std.arp.had) {
104 if (!chan[i].inPorta) {
105 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
106 }
107 chan[i].freqChanged=true;
108 }
109 if (chan[i].std.pitch.had) {
110 if (chan[i].std.pitch.mode) {
111 chan[i].pitch2+=chan[i].std.pitch.val;
112 CLAMP_VAR(chan[i].pitch2,-32768,32767);
113 } else {
114 chan[i].pitch2=chan[i].std.pitch.val;
115 }
116 chan[i].freqChanged=true;
117 }
118 // panning registers are reversed in this chip
119 if (chan[i].std.panL.had) {
120 chan[i].panning&=0xf0;
121 chan[i].panning|=chan[i].std.panL.val&15;
122 }
123 if (chan[i].std.panR.had) {
124 chan[i].panning&=0x0f;
125 chan[i].panning|=(chan[i].std.panR.val&15)<<4;
126 }
127 if (chan[i].std.panL.had || chan[i].std.panR.had) {
128 chWrite(i,1,isMuted[i]?0:chan[i].panning);
129 }
130 if (chan[i].std.phaseReset.had) {
131 if (chan[i].std.phaseReset.val==1 && chan[i].active) {
132 chan[i].audPos=0;
133 chan[i].setPos=true;
134 }
135 }
136 if (chan[i].setPos) {
137 // force keyon
138 chan[i].keyOn=true;
139 chan[i].setPos=false;
140 } else {
141 chan[i].audPos=0;
142 }
143 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
144 unsigned char keyon=regPool[8]&~(1<<i);
145 unsigned char keyoff=keyon|(1<<i);
146 DivSample* s=parent->getSample(chan[i].sample);
147 double off=(s->centerRate>=1)?((double)s->centerRate/parent->getCenterRate()):1.0;
148 chan[i].freq=(int)(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));
149 if (chan[i].freq>65535) chan[i].freq=65535;
150 if (chan[i].keyOn) {
151 unsigned int start=0;
152 unsigned int loop=0;
153 if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
154 start=sampleOffRFC[chan[i].sample];
155 loop=start+s->length8;
156 }
157 if (chan[i].audPos>0) {
158 start=start+MIN(chan[i].audPos,s->length8);
159 }
160 if (s->isLoopable()) {
161 loop=start+s->loopStart;
162 }
163 start=MIN(start,getSampleMemCapacity()-32);
164 loop=MIN(loop,getSampleMemCapacity()-32);
165 rWrite(8,keyoff); // force keyoff first
166 chWrite(i,6,start>>8);
167 chWrite(i,4,loop&0xff);
168 chWrite(i,5,loop>>8);
169 if (!chan[i].std.vol.had) {
170 chan[i].outVol=chan[i].vol;
171 chWrite(i,0,chan[i].outVol);
172 }
173 rWrite(8,keyon);
174 chan[i].keyOn=false;
175 }
176 if (chan[i].keyOff) {
177 rWrite(8,keyoff);
178 chan[i].keyOff=false;
179 }
180 if (chan[i].freqChanged) {
181 chWrite(i,2,chan[i].freq&0xff);
182 chWrite(i,3,chan[i].freq>>8);
183 chan[i].freqChanged=false;
184 }
185 }
186 }
187 }
188
189 int DivPlatformRF5C68::dispatch(DivCommand c) {
190 switch (c.cmd) {
191 case DIV_CMD_NOTE_ON: {
192 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA);
193 chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:255;
194 if (c.value!=DIV_NOTE_NULL) {
195 chan[c.chan].sample=ins->amiga.getSample(c.value);
196 chan[c.chan].sampleNote=c.value;
197 c.value=ins->amiga.getFreq(c.value);
198 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
199 }
200 if (c.value!=DIV_NOTE_NULL) {
201 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
202 }
203 if (chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) {
204 chan[c.chan].sample=-1;
205 }
206 if (c.value!=DIV_NOTE_NULL) {
207 chan[c.chan].freqChanged=true;
208 chan[c.chan].note=c.value;
209 }
210 chan[c.chan].active=true;
211 chan[c.chan].keyOn=true;
212 chan[c.chan].macroInit(ins);
213 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
214 chan[c.chan].outVol=chan[c.chan].vol;
215 }
216 break;
217 }
218 case DIV_CMD_NOTE_OFF:
219 chan[c.chan].sample=-1;
220 chan[c.chan].active=false;
221 chan[c.chan].keyOff=true;
222 chan[c.chan].macroInit(NULL);
223 break;
224 case DIV_CMD_NOTE_OFF_ENV:
225 case DIV_CMD_ENV_RELEASE:
226 chan[c.chan].std.release();
227 break;
228 case DIV_CMD_INSTRUMENT:
229 if (chan[c.chan].ins!=c.value || c.value2==1) {
230 chan[c.chan].ins=c.value;
231 }
232 break;
233 case DIV_CMD_VOLUME:
234 if (chan[c.chan].vol!=c.value) {
235 chan[c.chan].vol=c.value;
236 if (!chan[c.chan].std.vol.has) {
237 chan[c.chan].outVol=c.value;
238 chWrite(c.chan,0,chan[c.chan].outVol);
239 }
240 }
241 break;
242 case DIV_CMD_GET_VOLUME:
243 if (chan[c.chan].std.vol.has) {
244 return chan[c.chan].vol;
245 }
246 return chan[c.chan].outVol;
247 break;
248 case DIV_CMD_PANNING:
249 chan[c.chan].panning=(c.value>>4)|(c.value2&0xf0);
250 chWrite(c.chan,1,isMuted[c.chan]?0:chan[c.chan].panning);
251 break;
252 case DIV_CMD_PITCH:
253 chan[c.chan].pitch=c.value;
254 chan[c.chan].freqChanged=true;
255 break;
256 case DIV_CMD_NOTE_PORTA: {
257 int destFreq=NOTE_FREQUENCY(c.value2+chan[c.chan].sampleNoteDelta);
258 bool return2=false;
259 if (destFreq>chan[c.chan].baseFreq) {
260 chan[c.chan].baseFreq+=c.value;
261 if (chan[c.chan].baseFreq>=destFreq) {
262 chan[c.chan].baseFreq=destFreq;
263 return2=true;
264 }
265 } else {
266 chan[c.chan].baseFreq-=c.value;
267 if (chan[c.chan].baseFreq<=destFreq) {
268 chan[c.chan].baseFreq=destFreq;
269 return2=true;
270 }
271 }
272 chan[c.chan].freqChanged=true;
273 if (return2) {
274 chan[c.chan].inPorta=false;
275 return 2;
276 }
277 break;
278 }
279 case DIV_CMD_LEGATO: {
280 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(0)));
281 chan[c.chan].freqChanged=true;
282 chan[c.chan].note=c.value;
283 break;
284 }
285 case DIV_CMD_PRE_PORTA:
286 if (chan[c.chan].active && c.value2) {
287 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA));
288 }
289 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);
290 chan[c.chan].inPorta=c.value;
291 break;
292 case DIV_CMD_SAMPLE_POS:
293 chan[c.chan].audPos=c.value;
294 chan[c.chan].setPos=true;
295 break;
296 case DIV_CMD_GET_VOLMAX:
297 return 255;
298 break;
299 case DIV_CMD_MACRO_OFF:
300 chan[c.chan].std.mask(c.value,true);
301 break;
302 case DIV_CMD_MACRO_ON:
303 chan[c.chan].std.mask(c.value,false);
304 break;
305 case DIV_CMD_MACRO_RESTART:
306 chan[c.chan].std.restart(c.value);
307 break;
308 default:
309 break;
310 }
311 return 1;
312 }
313
314 void DivPlatformRF5C68::muteChannel(int ch, bool mute) {
315 isMuted[ch]=mute;
316 chWrite(ch,1,mute?0:chan[ch].panning);
317 }
318
319 void DivPlatformRF5C68::forceIns() {
320 for (int i=0; i<8; i++) {
321 chan[i].insChanged=true;
322 chan[i].freqChanged=true;
323 chan[i].sample=-1;
324 chWrite(i,1,isMuted[i]?0:chan[i].panning);
325 }
326 }
327
328 void* DivPlatformRF5C68::getChanState(int ch) {
329 return &chan[ch];
330 }
331
332 DivMacroInt* DivPlatformRF5C68::getChanMacroInt(int ch) {
333 return &chan[ch].std;
334 }
335
336 unsigned short DivPlatformRF5C68::getPan(int ch) {
337 return ((chan[ch].panning&15)<<8)|((chan[ch].panning&0xf0)>>4);
338 }
339
340 DivDispatchOscBuffer* DivPlatformRF5C68::getOscBuffer(int ch) {
341 return oscBuf[ch];
342 }
343
344 void DivPlatformRF5C68::reset() {
345 memset(regPool,0,144);
346 rf5c68.device_reset();
347 rWrite(0x08,0xff); // keyoff all channels
348 for (int i=0; i<8; i++) {
349 chan[i]=DivPlatformRF5C68::Channel();
350 chan[i].std.setEngine(parent);
351 chWrite(i,0,255);
352 chWrite(i,1,isMuted[i]?0:255);
353 }
354 }
355
356 int DivPlatformRF5C68::getOutputCount() {
357 return 2;
358 }
359
360 void DivPlatformRF5C68::notifyInsChange(int ins) {
361 for (int i=0; i<8; i++) {
362 if (chan[i].ins==ins) {
363 chan[i].insChanged=true;
364 }
365 }
366 }
367
368 void DivPlatformRF5C68::notifyWaveChange(int wave) {
369 }
370
371 void DivPlatformRF5C68::notifyInsDeletion(void* ins) {
372 for (int i=0; i<8; i++) {
373 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
374 }
375 }
376
377 void DivPlatformRF5C68::setFlags(const DivConfig& flags) {
378 switch (flags.getInt("clockSel",0)) {
379 case 1: chipClock=10000000; break;
380 case 2: chipClock=12500000; break;
381 default: chipClock=8000000; break;
382 }
383 CHECK_CUSTOM_CLOCK;
384 chipType=flags.getInt("chipType",0);
385 rate=chipClock/384;
386 for (int i=0; i<8; i++) {
387 oscBuf[i]->setRate(rate);
388 }
389 rf5c68=(chipType==1)?rf5c164_device():rf5c68_device();
390 rf5c68.device_start(sampleMem);
391 }
392
393 void DivPlatformRF5C68::poke(unsigned int addr, unsigned short val) {
394 rWrite(addr&0x0f,val);
395 }
396
397 void DivPlatformRF5C68::poke(std::vector<DivRegWrite>& wlist) {
398 for (DivRegWrite& i: wlist) rWrite(i.addr&0x0f,i.val);
399 }
400
401 unsigned char* DivPlatformRF5C68::getRegisterPool() {
402 return regPool;
403 }
404
405 int DivPlatformRF5C68::getRegisterPoolSize() {
406 return 144;
407 }
408
409 const void* DivPlatformRF5C68::getSampleMem(int index) {
410 return index == 0 ? sampleMem : NULL;
411 }
412
413 size_t DivPlatformRF5C68::getSampleMemCapacity(int index) {
414 return index == 0 ? 65536 : 0;
415 }
416
417 size_t DivPlatformRF5C68::getSampleMemUsage(int index) {
418 return index == 0 ? sampleMemLen : 0;
419 }
420
421 bool DivPlatformRF5C68::isSampleLoaded(int index, int sample) {
422 if (index!=0) return false;
423 if (sample<0 || sample>32767) return false;
424 return sampleLoaded[sample];
425 }
426
427 const DivMemoryComposition* DivPlatformRF5C68::getMemCompo(int index) {
428 if (index!=0) return NULL;
429 return &memCompo;
430 }
431
432 void DivPlatformRF5C68::renderSamples(int sysID) {
433 memset(sampleMem,0,getSampleMemCapacity());
434 memset(sampleOffRFC,0,32768*sizeof(unsigned int));
435 memset(sampleLoaded,0,32768*sizeof(bool));
436
437 memCompo=DivMemoryComposition();
438 memCompo.name="Sample Memory";
439
440 size_t memPos=0;
441 for (int i=0; i<parent->song.sampleLen; i++) {
442 DivSample* s=parent->song.sample[i];
443 if (!s->renderOn[0][sysID]) {
444 sampleOffRFC[i]=0;
445 continue;
446 }
447
448 int length=s->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT);
449 int actualLength=MIN((int)(getSampleMemCapacity()-memPos)-32,length);
450 if (actualLength>0) {
451 sampleOffRFC[i]=memPos;
452 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+actualLength+32));
453 for (int j=0; j<actualLength; j++) {
454 // convert to signed magnitude
455 signed char val=s->data8[j];
456 CLAMP_VAR(val,-127,126);
457 sampleMem[memPos++]=(val>0)?(val|0x80):(0-val);
458 }
459 // write end of sample marker
460 memset(&sampleMem[memPos],0xff,32);
461 memPos+=32;
462 }
463 if (actualLength<length) {
464 logW("out of RF5C68 PCM memory for sample %d!",i);
465 break;
466 }
467 // align memPos to 256-byte boundary
468 memPos=(memPos+0xff)&~0xff;
469 sampleLoaded[i]=true;
470 }
471 sampleMemLen=memPos;
472
473 memCompo.used=sampleMemLen;
474 memCompo.capacity=getSampleMemCapacity(0);
475 }
476
477 int DivPlatformRF5C68::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
478 parent=p;
479 dumpWrites=false;
480 skipRegisterWrites=false;
481
482 for (int i=0; i<8; i++) {
483 isMuted[i]=false;
484 oscBuf[i]=new DivDispatchOscBuffer;
485 }
486 sampleMem=new unsigned char[getSampleMemCapacity()];
487 sampleMemLen=0;
488 setFlags(flags);
489 reset();
490
491 return 8;
492 }
493
494 void DivPlatformRF5C68::quit() {
495 delete[] sampleMem;
496 for (int i=0; i<8; i++) {
497 delete oscBuf[i];
498 }
499 }
500
501 // initialization of important arrays
502 DivPlatformRF5C68::DivPlatformRF5C68() {
503 sampleOffRFC=new unsigned int[32768];
504 sampleLoaded=new bool[32768];
505 }
506
507 DivPlatformRF5C68::~DivPlatformRF5C68() {
508 delete[] sampleOffRFC;
509 delete[] sampleLoaded;
510 }