Debian Codegraph @

src/engine/platform/vera.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 "vera.h"
21 #include "../engine.h"
22 #include <string.h>
23 #include <math.h>
24
25 extern "C" {
26 #include "sound/vera_psg.h"
27 #include "sound/vera_pcm.h"
28 }
29
30 //if (dumpWrites) {addWrite(((c)*4+(a)),(d));}
31 //#define rWrite(c,a,d) {regPool[(c)*4+(a)]=(d); psg_writereg(psg,((c)*4+(a)),(d));}
32 #define rWrite(c,a,d) {regPool[(c)*4+(a)]=(d); psg_writereg(psg,((c)*4+(a)),(d));if (dumpWrites) {addWrite(((c)*4+(a)),(d));}}
33 #define rWriteLo(c,a,d) rWrite(c,a,(regPool[(c)*4+(a)]&(~0x3f))|((d)&0x3f))
34 #define rWriteHi(c,a,d) rWrite(c,a,(regPool[(c)*4+(a)]&(~0xc0))|(((d)<<6)&0xc0))
35 #define rWritePCMCtrl(d) {regPool[64]=(d); pcm_write_ctrl(pcm,d);if (dumpWrites) addWrite(64,(d));}
36 #define rWritePCMRate(d) {regPool[65]=(d); pcm_write_rate(pcm,d);if (dumpWrites) addWrite(65,(d));}
37 #define rWritePCMData(d) {regPool[66]=(d); pcm_write_fifo(pcm,d);}
38 #define rWritePCMVol(d) rWritePCMCtrl((regPool[64]&(~0x8f))|((d)&15))
39 #define rWriteZSMSync(d) {if (dumpWrites) addWrite(68,(d));}
40
41 const char* regCheatSheetVERA[]={
42 "CHxFreq", "00+x*4",
43 "CHxVol", "02+x*4",
44 "CHxWave", "03+x*4",
45
46 "AUDIO_CTRL", "40",
47 "AUDIO_RATE", "41",
48 "AUDIO_DATA", "42",
49 "ZSM_PCM_LOOP_POINT", "43",
50 "ZSM_SYNC", "44",
51
52 NULL
53 };
54
55 const char** DivPlatformVERA::getRegisterSheet() {
56 return regCheatSheetVERA;
57 }
58
59 // TODO: possible sample offset latency...
60 void DivPlatformVERA::acquire(short** buf, size_t len) {
61 for (int i=0; i<17; i++) {
62 oscBuf[i]->begin(len);
63 }
64
65 // both PSG part and PCM part output a full 16-bit range, putting bufL/R
66 // argument right into both could cause an overflow
67 short whyCallItBuf[4][128];
68 size_t pos=0;
69 size_t lenCopy=len;
70 DivSample* s=parent->getSample(chan[16].pcm.sample);
71 while (lenCopy>0) {
72 if (s->samples>0 && chan[16].pcm.pos<s->samples) {
73 while (pcm_is_fifo_almost_empty(pcm)) {
74 short tmp_l=0;
75 short tmp_r=0;
76 if (!isMuted[16]) {
77 // TODO stereo samples once DivSample has a support for it
78 if (chan[16].pcm.depth16) {
79 if (chan[16].pcm.pos<s->samples) {
80 tmp_l=s->data16[chan[16].pcm.pos];
81 } else {
82 tmp_l=0;
83 }
84 tmp_r=tmp_l;
85 } else {
86 if (chan[16].pcm.pos<s->samples) {
87 tmp_l=s->data8[chan[16].pcm.pos];
88 } else {
89 tmp_l=0;
90 }
91 tmp_r=tmp_l;
92 }
93 if (!(chan[16].pan&1)) tmp_l=0;
94 if (!(chan[16].pan&2)) tmp_r=0;
95 }
96 if (chan[16].pcm.depth16) {
97 rWritePCMData(tmp_l&0xff);
98 rWritePCMData((tmp_l>>8)&0xff);
99 rWritePCMData(tmp_r&0xff);
100 rWritePCMData((tmp_r>>8)&0xff);
101 } else {
102 rWritePCMData(tmp_l&0xff);
103 rWritePCMData(tmp_r&0xff);
104 }
105 chan[16].pcm.pos++;
106 if (s->isLoopable() && chan[16].pcm.pos>=(unsigned int)s->loopEnd) {
107 chan[16].pcm.pos=s->loopStart;
108 } else if (chan[16].pcm.pos>=s->samples) {
109 chan[16].pcm.sample=-1;
110 break;
111 }
112 }
113 } else {
114 // just let the buffer run out
115 chan[16].pcm.sample=-1;
116 }
117 int curLen=MIN(lenCopy,128);
118 memset(whyCallItBuf,0,sizeof(whyCallItBuf));
119 pcm_render(pcm,whyCallItBuf[2],whyCallItBuf[3],curLen);
120 for (int i=0; i<curLen; i++) {
121 psg_render(psg,&whyCallItBuf[0][i],&whyCallItBuf[1][i],1);
122 buf[0][pos]=(short)(((int)whyCallItBuf[0][i]+whyCallItBuf[2][i])/2);
123 buf[1][pos]=(short)(((int)whyCallItBuf[1][i]+whyCallItBuf[3][i])/2);
124
125 for (int j=0; j<16; j++) {
126 oscBuf[j]->putSample(pos,psg->channels[j].lastOut);
127 }
128
129 int pcmOut=(whyCallItBuf[2][i]+whyCallItBuf[3][i])>>1;
130 if (pcmOut<-32768) pcmOut=-32768;
131 if (pcmOut>32767) pcmOut=32767;
132 oscBuf[16]->putSample(pos,pcmOut);
133
134 pos++;
135 }
136 lenCopy-=curLen;
137 }
138
139 for (int i=0; i<17; i++) {
140 oscBuf[i]->end(len);
141 }
142 }
143
144 void DivPlatformVERA::reset() {
145 for (int i=0; i<17; i++) {
146 chan[i]=Channel();
147 chan[i].std.setEngine(parent);
148 }
149 psg_reset(psg);
150 pcm_reset(pcm);
151 memset(regPool,0,67);
152 for (int i=0; i<16; i++) {
153 chan[i].vol=63;
154 chan[i].pan=3;
155 rWriteHi(i,2,isMuted[i]?0:3);
156 }
157 chan[16].vol=15;
158 chan[16].pan=3;
159 lastCenterRate=-1;
160 }
161
162 int DivPlatformVERA::calcNoteFreq(int ch, int note) {
163 if (ch<16) {
164 return parent->calcBaseFreq(chipClock,2097152,note,false);
165 } else {
166 double off=65536.0;
167 if (chan[ch].pcm.sample>=0 && chan[ch].pcm.sample<parent->song.sampleLen) {
168 DivSample* s=parent->getSample(chan[ch].pcm.sample);
169 if (s->centerRate<1) {
170 off=65536.0;
171 } else {
172 off=65536.0*(s->centerRate/parent->getCenterRate());
173 }
174 }
175 return (int)(parent->calcBaseFreq(chipClock,off,note,false));
176 }
177 }
178
179 void DivPlatformVERA::tick(bool sysTick) {
180 for (int i=0; i<16; i++) {
181 chan[i].std.next();
182 if (chan[i].std.vol.had) {
183 chan[i].outVol=MAX(chan[i].vol+chan[i].std.vol.val-63,0);
184 rWriteLo(i,2,chan[i].outVol);
185 }
186 if (NEW_ARP_STRAT) {
187 chan[i].handleArp();
188 } else if (chan[i].std.arp.had) {
189 if (!chan[i].inPorta) {
190 chan[i].baseFreq=calcNoteFreq(0,parent->calcArp(chan[i].note,chan[i].std.arp.val));
191 }
192 chan[i].freqChanged=true;
193 }
194 if (chan[i].std.duty.had) {
195 rWriteLo(i,3,chan[i].std.duty.val);
196 }
197 if (chan[i].std.wave.had) {
198 rWriteHi(i,3,chan[i].std.wave.val);
199 }
200 if (i<16) {
201 if (chan[i].std.panL.had) {
202 chan[i].pan=chan[i].std.panL.val&3;
203 chan[i].pan=((chan[i].pan&1)<<1)|((chan[i].pan&2)>>1);
204 rWriteHi(i,2,isMuted[i]?0:chan[i].pan);
205 }
206 }
207 if (chan[i].std.pitch.had) {
208 if (chan[i].std.pitch.mode) {
209 chan[i].pitch2+=chan[i].std.pitch.val;
210 CLAMP_VAR(chan[i].pitch2,-32768,32767);
211 } else {
212 chan[i].pitch2=chan[i].std.pitch.val;
213 }
214 chan[i].freqChanged=true;
215 }
216 if (chan[i].freqChanged) {
217 chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,8,chan[i].pitch2,chipClock,2097152);
218 if (chan[i].freq>65535) chan[i].freq=65535;
219 rWrite(i,0,chan[i].freq&0xff);
220 rWrite(i,1,(chan[i].freq>>8)&0xff);
221 chan[i].freqChanged=false;
222 }
223 }
224 // PCM
225 chan[16].std.next();
226 if (chan[16].std.vol.had) {
227 chan[16].outVol=MAX(chan[16].vol+MIN(chan[16].std.vol.val/4,15)-15,0);
228 rWritePCMVol(chan[16].outVol&15);
229 }
230 if (NEW_ARP_STRAT) {
231 chan[16].handleArp();
232 } else if (chan[16].std.arp.had) {
233 if (!chan[16].inPorta) {
234 chan[16].baseFreq=calcNoteFreq(16,parent->calcArp(chan[16].note,chan[16].std.arp.val));
235 }
236 chan[16].freqChanged=true;
237 }
238 if (chan[16].freqChanged) {
239 double off=65536.0;
240 if (chan[16].pcm.sample>=0 && chan[16].pcm.sample<parent->song.sampleLen) {
241 DivSample* s=parent->getSample(chan[16].pcm.sample);
242 lastCenterRate=s->centerRate;
243 if (s->centerRate>=1) {
244 off=65536.0*(s->centerRate/parent->getCenterRate());
245 }
246 } else if (lastCenterRate>=1) {
247 off=65536.0*(lastCenterRate/parent->getCenterRate());
248 }
249 chan[16].freq=parent->calcFreq(chan[16].baseFreq,chan[16].pitch,chan[16].fixedArp?chan[16].baseNoteOverride:chan[16].arpOff,chan[16].fixedArp,false,8,chan[16].pitch2,chipClock,off);
250 if (chan[16].freq>128) chan[16].freq=128;
251 rWritePCMRate(chan[16].freq&0xff);
252 chan[16].freqChanged=false;
253 }
254
255 // For export, output the entire sample that starts on this tick
256 if (dumpWrites) {
257 DivSample* s=parent->getSample(chan[16].pcm.sample);
258 if (s->samples>0) {
259 if (s->isLoopable()) {
260 // Inform the export process of the loop point for this sample
261 int tmp_ls=(s->loopStart<<1); // for stereo
262 if (chan[16].pcm.depth16)
263 tmp_ls<<=1; // for 16 bit
264 addWrite(67,tmp_ls&0xff);
265 addWrite(67,(tmp_ls>>8)&0xff);
266 addWrite(67,(tmp_ls>>16)&0xff);
267 }
268 while (true) {
269 short tmp_l=0;
270 short tmp_r=0;
271 if (!isMuted[16]) {
272 if (chan[16].pcm.depth16) {
273 tmp_l=s->data16[chan[16].pcm.pos];
274 tmp_r=tmp_l;
275 } else {
276 tmp_l=s->data8[chan[16].pcm.pos];
277 tmp_r=tmp_l;
278 }
279 if (!(chan[16].pan&1)) tmp_l=0;
280 if (!(chan[16].pan&2)) tmp_r=0;
281 }
282 if (chan[16].pcm.depth16) {
283 addWrite(66,tmp_l&0xff);
284 addWrite(66,(tmp_l>>8)&0xff);
285 addWrite(66,tmp_r&0xff);
286 addWrite(66,(tmp_r>>8)&0xff);
287 } else {
288 addWrite(66,tmp_l&0xff);
289 addWrite(66,tmp_r&0xff);
290 }
291 chan[16].pcm.pos++;
292 if (s->isLoopable() && chan[16].pcm.pos>=(unsigned int)s->loopEnd) {
293 chan[16].pcm.sample=-1;
294 break;
295 }
296 if (chan[16].pcm.pos>=s->samples) {
297 chan[16].pcm.sample=-1;
298 break;
299 }
300 }
301 } else {
302 chan[16].pcm.sample=-1;
303 }
304 }
305 }
306
307 int DivPlatformVERA::dispatch(DivCommand c) {
308 int tmp;
309 switch (c.cmd) {
310 case DIV_CMD_NOTE_ON:
311 if (c.chan<16) {
312 rWriteLo(c.chan,2,chan[c.chan].vol);
313 } else {
314 DivInstrument* ins=parent->getIns(chan[16].ins,DIV_INS_VERA);
315 if (c.value!=DIV_NOTE_NULL) {
316 chan[16].pcm.sample=ins->amiga.getSample(c.value);
317 chan[16].sampleNote=c.value;
318 c.value=ins->amiga.getFreq(c.value);
319 chan[16].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
320 } else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) {
321 chan[16].pcm.sample=ins->amiga.getSample(chan[c.chan].sampleNote);
322 c.value=ins->amiga.getFreq(chan[c.chan].sampleNote);
323 }
324 if (chan[16].pcm.sample<0 || chan[16].pcm.sample>=parent->song.sampleLen) {
325 chan[16].pcm.sample=-1;
326 }
327 if (chan[16].pcm.setPos) {
328 chan[16].pcm.setPos=false;
329 } else {
330 chan[16].pcm.pos=0;
331 }
332 DivSample* s=parent->getSample(chan[16].pcm.sample);
333 unsigned char ctrl=0x90|chan[16].vol; // always stereo
334 if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
335 chan[16].pcm.depth16=true;
336 ctrl|=0x20;
337 } else {
338 chan[16].pcm.depth16=false;
339 if (s->depth!=DIV_SAMPLE_DEPTH_8BIT) chan[16].pcm.sample=-1;
340 }
341 rWritePCMCtrl(ctrl);
342 }
343 if (c.value!=DIV_NOTE_NULL) {
344 chan[c.chan].baseFreq=calcNoteFreq(c.chan,c.value);
345 chan[c.chan].freqChanged=true;
346 chan[c.chan].note=c.value;
347 }
348 chan[c.chan].active=true;
349 chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_VERA));
350 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
351 chan[c.chan].outVol=chan[c.chan].vol;
352 }
353 break;
354 case DIV_CMD_NOTE_OFF:
355 chan[c.chan].active=false;
356 if(c.chan<16) {
357 rWriteLo(c.chan,2,0)
358 } else {
359 chan[16].pcm.sample=-1;
360 rWritePCMCtrl(0x80);
361 rWritePCMRate(0);
362 }
363 chan[c.chan].macroInit(NULL);
364 break;
365 case DIV_CMD_NOTE_OFF_ENV:
366 case DIV_CMD_ENV_RELEASE:
367 chan[c.chan].std.release();
368 break;
369 case DIV_CMD_INSTRUMENT:
370 chan[c.chan].ins=(unsigned char)c.value;
371 break;
372 case DIV_CMD_VOLUME:
373 if (c.chan<16) {
374 tmp=c.value&0x3f;
375 chan[c.chan].vol=tmp;
376 if (chan[c.chan].active) {
377 rWriteLo(c.chan,2,tmp);
378 }
379 } else {
380 tmp=c.value&0x0f;
381 chan[c.chan].vol=tmp;
382 if (chan[c.chan].active) {
383 rWritePCMVol(tmp);
384 }
385 }
386 break;
387 case DIV_CMD_GET_VOLUME:
388 return chan[c.chan].vol;
389 break;
390 case DIV_CMD_PITCH:
391 chan[c.chan].pitch=c.value;
392 chan[c.chan].freqChanged=true;
393 break;
394 case DIV_CMD_NOTE_PORTA: {
395 int destFreq=calcNoteFreq(c.chan,c.value2+chan[c.chan].sampleNoteDelta);
396 bool return2=false;
397 if (destFreq>chan[c.chan].baseFreq) {
398 chan[c.chan].baseFreq+=c.value;
399 if (chan[c.chan].baseFreq>=destFreq) {
400 chan[c.chan].baseFreq=destFreq;
401 return2=true;
402 }
403 } else {
404 chan[c.chan].baseFreq-=c.value;
405 if (chan[c.chan].baseFreq<=destFreq) {
406 chan[c.chan].baseFreq=destFreq;
407 return2=true;
408 }
409 }
410 chan[c.chan].freqChanged=true;
411 if (return2) {
412 chan[c.chan].inPorta=false;
413 return 2;
414 }
415 break;
416 }
417 case DIV_CMD_LEGATO:
418 chan[c.chan].baseFreq=calcNoteFreq(c.chan,c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
419 chan[c.chan].freqChanged=true;
420 chan[c.chan].note=c.value;
421 break;
422 case DIV_CMD_PRE_PORTA:
423 if (chan[c.chan].active && c.value2) {
424 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_VERA));
425 }
426 if (!chan[c.chan].inPorta && c.value && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=calcNoteFreq(c.chan,chan[c.chan].note);
427 chan[c.chan].inPorta=c.value;
428 break;
429 case DIV_CMD_STD_NOISE_MODE:
430 if (c.chan<16) rWriteLo(c.chan,3,c.value);
431 break;
432 case DIV_CMD_WAVE:
433 if (c.chan<16) rWriteHi(c.chan,3,c.value);
434 break;
435 case DIV_CMD_PANNING: {
436 tmp=0;
437 tmp|=(c.value>0)?1:0;
438 tmp|=(c.value2>0)?2:0;
439 chan[c.chan].pan=tmp&3;
440 if (c.chan<16) {
441 rWriteHi(c.chan,2,isMuted[c.chan]?0:chan[c.chan].pan);
442 }
443 break;
444 }
445 case DIV_CMD_SAMPLE_POS:
446 if (c.chan!=16) break;
447 chan[c.chan].pcm.pos=c.value;
448 chan[c.chan].pcm.setPos=true;
449 break;
450 case DIV_CMD_GET_VOLMAX:
451 if (c.chan<16) {
452 return 63;
453 } else {
454 return 15;
455 }
456 break;
457 case DIV_CMD_MACRO_OFF:
458 chan[c.chan].std.mask(c.value,true);
459 break;
460 case DIV_CMD_MACRO_ON:
461 chan[c.chan].std.mask(c.value,false);
462 break;
463 case DIV_CMD_MACRO_RESTART:
464 chan[c.chan].std.restart(c.value);
465 break;
466 case DIV_CMD_EXTERNAL:
467 rWriteZSMSync(c.value);
468 break;
469 default:
470 break;
471 }
472 return 1;
473 }
474
475 void* DivPlatformVERA::getChanState(int ch) {
476 return &chan[ch];
477 }
478
479 DivMacroInt* DivPlatformVERA::getChanMacroInt(int ch) {
480 return &chan[ch].std;
481 }
482
483 unsigned short DivPlatformVERA::getPan(int ch) {
484 return ((chan[ch].pan&1)<<8)|((chan[ch].pan&2)>>1);
485 }
486
487 DivDispatchOscBuffer* DivPlatformVERA::getOscBuffer(int ch) {
488 return oscBuf[ch];
489 }
490
491 unsigned char* DivPlatformVERA::getRegisterPool() {
492 return regPool;
493 }
494
495 int DivPlatformVERA::getRegisterPoolSize() {
496 return 67;
497 }
498
499 bool DivPlatformVERA::getLegacyAlwaysSetVolume() {
500 return false;
501 }
502
503 void DivPlatformVERA::muteChannel(int ch, bool mute) {
504 isMuted[ch]=mute;
505 if (ch<16) {
506 rWriteHi(ch,2,mute?0:chan[ch].pan);
507 }
508 }
509
510 float DivPlatformVERA::getPostAmp() {
511 return 4.0f;
512 }
513
514 int DivPlatformVERA::getOutputCount() {
515 return 2;
516 }
517
518 void DivPlatformVERA::notifyInsDeletion(void* ins) {
519 for (int i=0; i<17; i++) {
520 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
521 }
522 }
523
524 void DivPlatformVERA::poke(unsigned int addr, unsigned short val) {
525 switch (addr) {
526 case 64:
527 rWritePCMCtrl((unsigned char)val);
528 break;
529 case 65:
530 rWritePCMRate((unsigned char)val);
531 break;
532 case 66:
533 rWritePCMData((unsigned char)val);
534 break;
535 default:
536 rWrite(0,addr,(unsigned char)val);
537 break;
538 }
539 }
540
541 void DivPlatformVERA::poke(std::vector<DivRegWrite>& wlist) {
542 for (auto &i: wlist) poke(i.addr,i.val);
543 }
544
545 void DivPlatformVERA::setFlags(const DivConfig& flags) {
546 psg->chipType=flags.getInt("chipType",3);
547 chipClock=25000000;
548 CHECK_CUSTOM_CLOCK;
549 rate=chipClock/512;
550 for (int i=0; i<17; i++) {
551 oscBuf[i]->setRate(rate);
552 }
553 }
554
555 int DivPlatformVERA::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
556 for (int i=0; i<17; i++) {
557 isMuted[i]=false;
558 oscBuf[i]=new DivDispatchOscBuffer;
559 }
560 parent=p;
561 psg=new struct VERA_PSG;
562 pcm=new struct VERA_PCM;
563 dumpWrites=false;
564 skipRegisterWrites=false;
565 setFlags(flags);
566 reset();
567 return 17;
568 }
569
570 void DivPlatformVERA::quit() {
571 for (int i=0; i<17; i++) {
572 delete oscBuf[i];
573 }
574 delete psg;
575 delete pcm;
576 }
577 DivPlatformVERA::~DivPlatformVERA() {
578 }