Debian Codegraph @

src/engine/platform/ga20.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 "ga20.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include <math.h>
24
25 #define rWrite(a,v) {if(!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if(dumpWrites) addWrite(a,v);}}
26
27 #define CHIP_DIVIDER 64
28
29 const char* regCheatSheetGA20[]={
30 // on-chip
31 "CHX_StartL", "X*8+0",
32 "CHX_StartH", "X*8+1",
33 "CHX_EndL", "X*8+2",
34 "CHX_EndH", "X*8+3",
35 "CHX_Freq", "X*8+4",
36 "CHX_Volume", "X*8+5",
37 "CHX_Control", "X*8+6",
38 "CHX_Status", "X*8+7",
39 NULL
40 };
41
42 const char** DivPlatformGA20::getRegisterSheet() {
43 return regCheatSheetGA20;
44 }
45
46 inline void DivPlatformGA20::chWrite(unsigned char ch, unsigned int addr, unsigned char val) {
47 if (!skipRegisterWrites) {
48 if ((ch<4) && (addr<8)) {
49 rWrite((ch<<3)+(addr&7),val);
50 }
51 }
52 }
53
54 void DivPlatformGA20::acquire(short** buf, size_t len) {
55 thread_local short ga20Buf[4];
56
57 for (int i=0; i<4; i++) {
58 oscBuf[i]->begin(len);
59 }
60
61 for (size_t h=0; h<len; h++) {
62 if (!writes.empty()) {
63 QueuedWrite& w=writes.front();
64 ga20.write(w.addr,w.val);
65 regPool[w.addr]=w.val;
66 writes.pop();
67 }
68 ga20.sound_stream_update(ga20Buf,1);
69 buf[0][h]=(signed int)(ga20Buf[0]+ga20Buf[1]+ga20Buf[2]+ga20Buf[3])>>2;
70 for (int i=0; i<4; i++) {
71 oscBuf[i]->putSample(h,ga20Buf[i]>>1);
72 }
73 }
74
75 for (int i=0; i<4; i++) {
76 oscBuf[i]->end(len);
77 }
78 }
79
80 void DivPlatformGA20::acquireDirect(blip_buffer_t** bb, size_t len) {
81 thread_local short ga20Buf[4];
82
83 for (int i=0; i<4; i++) {
84 oscBuf[i]->begin(len);
85 }
86
87 for (size_t h=0; h<len; h++) {
88 size_t advance=0;
89 if (!writes.empty()) {
90 QueuedWrite& w=writes.front();
91 ga20.write(w.addr,w.val);
92 regPool[w.addr]=w.val;
93 writes.pop();
94 } else {
95 // heuristic
96 advance=len-h-1;
97
98 for (int i=0; i<4; i++) {
99 if (!ga20.m_channel[i].play) continue;
100 if (ga20.m_channel[i].hot) {
101 advance=0;
102 break;
103 }
104 const size_t newAdvance=ga20.m_channel[i].counter-ga20.m_channel[i].rate-1;
105 if (newAdvance<advance) advance=newAdvance;
106 }
107 }
108 ga20.sound_stream_update(ga20Buf,advance+1);
109 h+=advance;
110 const int out=(signed int)(ga20Buf[0]+ga20Buf[1]+ga20Buf[2]+ga20Buf[3])>>2;
111 if (out!=oldOut) {
112 blip_add_delta(bb[0],h,out-oldOut);
113 oldOut=out;
114 }
115 for (int i=0; i<4; i++) {
116 oscBuf[i]->putSample(h,ga20Buf[i]>>1);
117 }
118 }
119
120 for (int i=0; i<4; i++) {
121 oscBuf[i]->end(len);
122 }
123 }
124
125 u8 DivPlatformGA20::read_byte(u32 address) {
126 if ((sampleMem!=NULL) && (address<0x100000)) {
127 return sampleMem[address&0xfffff];
128 }
129 return 0;
130 }
131
132 void DivPlatformGA20::tick(bool sysTick) {
133 for (int i=0; i<4; i++) {
134 chan[i].std.next();
135 if (chan[i].std.vol.had) {
136 const signed char macroVol=VOL_SCALE_LOG((chan[i].vol&0xff),(0xff*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul,0xff);
137 if (macroVol!=chan[i].outVol) {
138 chan[i].outVol=macroVol;
139 chan[i].volumeChanged=true;
140 }
141 }
142 if (NEW_ARP_STRAT) {
143 chan[i].handleArp();
144 } else if (chan[i].std.arp.had) {
145 if (!chan[i].inPorta) {
146 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
147 }
148 chan[i].freqChanged=true;
149 }
150 if (chan[i].std.pitch.had) {
151 if (chan[i].std.pitch.mode) {
152 chan[i].pitch2+=chan[i].std.pitch.val;
153 CLAMP_VAR(chan[i].pitch2,-32768,32767);
154 } else {
155 chan[i].pitch2=chan[i].std.pitch.val;
156 }
157 chan[i].freqChanged=true;
158 }
159 if (chan[i].std.phaseReset.had) {
160 if (chan[i].std.phaseReset.val==1 && chan[i].active) {
161 chan[i].audPos=0;
162 chan[i].setPos=true;
163 }
164 }
165 if (chan[i].volumeChanged) {
166 chan[i].resVol=chan[i].outVol&0xff;
167 chWrite(i,0x5,chan[i].resVol);
168 chan[i].volumeChanged=false;
169 }
170 if (chan[i].setPos) {
171 // force keyon
172 chan[i].keyOn=true;
173 chan[i].setPos=false;
174 } else {
175 chan[i].audPos=0;
176 }
177 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
178 double off=1.0;
179 int sample=chan[i].sample;
180 if (sample>=0 && sample<parent->song.sampleLen) {
181 DivSample* s=parent->getSample(sample);
182 if (s->centerRate<1) {
183 off=1.0;
184 } else {
185 off=parent->getCenterRate()/s->centerRate;
186 }
187 }
188 DivSample* s=parent->getSample(chan[i].sample);
189 chan[i].freq=0x100-(int)(off*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));
190 if (chan[i].freq>255) chan[i].freq=255;
191 if (chan[i].freq<0) chan[i].freq=0;
192 if (chan[i].keyOn) {
193 unsigned int start=0;
194 unsigned int end=0;
195 if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
196 start=sampleOffGA20[chan[i].sample]&0xfffff;
197 end=start+s->length8;
198 }
199 if (chan[i].audPos>0) {
200 start=start+MIN(chan[i].audPos,MIN(getSampleMemCapacity()-1,s->length8));
201 }
202 start=MIN(start,getSampleMemCapacity()-1);
203 end=MIN(end,getSampleMemCapacity()-1);
204 // force keyoff first
205 chWrite(i,5,0);
206 chWrite(i,6,0);
207 // keyon
208 if (chan[i].prevFreq!=chan[i].freq) {
209 chWrite(i,4,chan[i].freq&0xff);
210 chan[i].prevFreq=chan[i].freq;
211 }
212 chWrite(i,0,start>>4);
213 chWrite(i,1,start>>12);
214 chWrite(i,2,end>>4);
215 chWrite(i,3,end>>12);
216 chWrite(i,5,chan[i].resVol);
217 chWrite(i,6,2);
218 if (!chan[i].std.vol.had) {
219 chan[i].outVol=chan[i].vol;
220 chan[i].volumeChanged=true;
221 }
222 chan[i].keyOn=false;
223 }
224 if (chan[i].keyOff) {
225 chWrite(i,5,0);
226 chWrite(i,6,0);
227 chan[i].keyOff=false;
228 }
229 if (chan[i].freqChanged) {
230 if (chan[i].prevFreq!=chan[i].freq) {
231 chWrite(i,4,chan[i].freq&0xff);
232 chan[i].prevFreq=chan[i].freq;
233 }
234 chan[i].freqChanged=false;
235 }
236 }
237 }
238 }
239
240 int DivPlatformGA20::dispatch(DivCommand c) {
241 switch (c.cmd) {
242 case DIV_CMD_NOTE_ON: {
243 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA);
244 chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:255;
245 if (c.value!=DIV_NOTE_NULL) {
246 chan[c.chan].sample=ins->amiga.getSample(c.value);
247 chan[c.chan].sampleNote=c.value;
248 c.value=ins->amiga.getFreq(c.value);
249 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
250 }
251 if (c.value!=DIV_NOTE_NULL) {
252 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
253 }
254 if (chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) {
255 chan[c.chan].sample=-1;
256 }
257 if (c.value!=DIV_NOTE_NULL) {
258 chan[c.chan].freqChanged=true;
259 chan[c.chan].note=c.value;
260 }
261 chan[c.chan].active=true;
262 chan[c.chan].keyOn=true;
263 chan[c.chan].macroInit(ins);
264 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
265 chan[c.chan].outVol=chan[c.chan].vol;
266 chan[c.chan].volumeChanged=true;
267 }
268 break;
269 }
270 case DIV_CMD_NOTE_OFF:
271 chan[c.chan].sample=-1;
272 chan[c.chan].active=false;
273 chan[c.chan].keyOff=true;
274 chan[c.chan].macroInit(NULL);
275 break;
276 case DIV_CMD_NOTE_OFF_ENV:
277 case DIV_CMD_ENV_RELEASE:
278 chan[c.chan].std.release();
279 break;
280 case DIV_CMD_INSTRUMENT:
281 if (chan[c.chan].ins!=c.value || c.value2==1) {
282 chan[c.chan].ins=c.value;
283 }
284 break;
285 case DIV_CMD_VOLUME:
286 if (chan[c.chan].vol!=c.value) {
287 chan[c.chan].vol=c.value;
288 if (!chan[c.chan].std.vol.has) {
289 chan[c.chan].outVol=c.value;
290 chan[c.chan].volumeChanged=true;
291 }
292 }
293 break;
294 case DIV_CMD_GET_VOLUME:
295 if (chan[c.chan].std.vol.has) {
296 return chan[c.chan].vol;
297 }
298 return chan[c.chan].outVol;
299 break;
300 case DIV_CMD_PITCH:
301 chan[c.chan].pitch=c.value;
302 chan[c.chan].freqChanged=true;
303 break;
304 case DIV_CMD_NOTE_PORTA: {
305 const int destFreq=NOTE_PERIODIC(c.value2+chan[c.chan].sampleNoteDelta);
306 bool return2=false;
307 if (destFreq>chan[c.chan].baseFreq) {
308 chan[c.chan].baseFreq+=c.value;
309 if (chan[c.chan].baseFreq>=destFreq) {
310 chan[c.chan].baseFreq=destFreq;
311 return2=true;
312 }
313 } else {
314 chan[c.chan].baseFreq-=c.value;
315 if (chan[c.chan].baseFreq<=destFreq) {
316 chan[c.chan].baseFreq=destFreq;
317 return2=true;
318 }
319 }
320 chan[c.chan].freqChanged=true;
321 if (return2) {
322 chan[c.chan].inPorta=false;
323 return 2;
324 }
325 break;
326 }
327 case DIV_CMD_LEGATO: {
328 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(0)));
329 chan[c.chan].freqChanged=true;
330 chan[c.chan].note=c.value;
331 break;
332 }
333 case DIV_CMD_PRE_PORTA:
334 if (chan[c.chan].active && c.value2) {
335 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA));
336 }
337 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);
338 chan[c.chan].inPorta=c.value;
339 break;
340 case DIV_CMD_SAMPLE_POS:
341 chan[c.chan].audPos=c.value;
342 chan[c.chan].setPos=true;
343 break;
344 case DIV_CMD_GET_VOLMAX:
345 return 255;
346 break;
347 case DIV_CMD_MACRO_OFF:
348 chan[c.chan].std.mask(c.value,true);
349 break;
350 case DIV_CMD_MACRO_ON:
351 chan[c.chan].std.mask(c.value,false);
352 break;
353 case DIV_CMD_MACRO_RESTART:
354 chan[c.chan].std.restart(c.value);
355 break;
356 default:
357 break;
358 }
359 return 1;
360 }
361
362 void DivPlatformGA20::muteChannel(int ch, bool mute) {
363 isMuted[ch]=mute;
364 ga20.set_mute(ch,mute);
365 chan[ch].volumeChanged=true;
366 }
367
368 void DivPlatformGA20::forceIns() {
369 while (!writes.empty()) writes.pop();
370 for (int i=0; i<4; i++) {
371 chan[i].insChanged=true;
372 chan[i].volumeChanged=true;
373 chan[i].freqChanged=true;
374 chan[i].prevFreq=-1;
375 }
376 }
377
378 void* DivPlatformGA20::getChanState(int ch) {
379 return &chan[ch];
380 }
381
382 DivMacroInt* DivPlatformGA20::getChanMacroInt(int ch) {
383 return &chan[ch].std;
384 }
385
386 DivSamplePos DivPlatformGA20::getSamplePos(int ch) {
387 if (ch>=4) return DivSamplePos();
388 if (chan[ch].sample<0 || chan[ch].sample>=parent->song.sampleLen) return DivSamplePos();
389 if (!ga20.is_playing(ch)) return DivSamplePos();
390 unsigned char f=chan[ch].freq;
391 return DivSamplePos(
392 chan[ch].sample,
393 ga20.get_position(ch)-sampleOffGA20[chan[ch].sample],
394 chipClock/(4*(0x100-(int)f))
395 );
396 }
397
398 DivDispatchOscBuffer* DivPlatformGA20::getOscBuffer(int ch) {
399 return oscBuf[ch];
400 }
401
402 void DivPlatformGA20::reset() {
403 writes.clear();
404 memset(regPool,0,32);
405 ga20.device_reset();
406 oldOut=0;
407 for (int i=0; i<4; i++) {
408 chan[i]=DivPlatformGA20::Channel();
409 chan[i].std.setEngine(parent);
410 // keyoff all channels
411 chWrite(i,5,0);
412 chWrite(i,6,0);
413 if (isMuted[i]) ga20.set_mute(i,true);
414 }
415 }
416
417 bool DivPlatformGA20::hasAcquireDirect() {
418 return true;
419 }
420
421 int DivPlatformGA20::getOutputCount() {
422 return 1;
423 }
424
425 void DivPlatformGA20::notifyInsChange(int ins) {
426 for (int i=0; i<4; i++) {
427 if (chan[i].ins==ins) {
428 chan[i].insChanged=true;
429 }
430 }
431 }
432
433 void DivPlatformGA20::notifyWaveChange(int wave) {
434 }
435
436 void DivPlatformGA20::notifyInsDeletion(void* ins) {
437 for (int i=0; i<4; i++) {
438 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
439 }
440 }
441
442 void DivPlatformGA20::setFlags(const DivConfig& flags) {
443 chipClock=COLOR_NTSC;
444 CHECK_CUSTOM_CLOCK;
445 rate=chipClock/4;
446 for (int i=0; i<4; i++) {
447 oscBuf[i]->setRate(rate);
448 }
449 }
450
451 void DivPlatformGA20::poke(unsigned int addr, unsigned short val) {
452 rWrite(addr&0x1f,val);
453 }
454
455 void DivPlatformGA20::poke(std::vector<DivRegWrite>& wlist) {
456 for (DivRegWrite& i: wlist) rWrite(i.addr&0x1f,i.val);
457 }
458
459 unsigned char* DivPlatformGA20::getRegisterPool() {
460 return regPool;
461 }
462
463 int DivPlatformGA20::getRegisterPoolSize() {
464 return 32;
465 }
466
467 const void* DivPlatformGA20::getSampleMem(int index) {
468 return index == 0 ? sampleMem : NULL;
469 }
470
471 size_t DivPlatformGA20::getSampleMemCapacity(int index) {
472 return index == 0 ? 1048576 : 0;
473 }
474
475 size_t DivPlatformGA20::getSampleMemUsage(int index) {
476 return index == 0 ? sampleMemLen : 0;
477 }
478
479 bool DivPlatformGA20::isSampleLoaded(int index, int sample) {
480 if (index!=0) return false;
481 if (sample<0 || sample>32767) return false;
482 return sampleLoaded[sample];
483 }
484
485 const DivMemoryComposition* DivPlatformGA20::getMemCompo(int index) {
486 if (index!=0) return NULL;
487 return &memCompo;
488 }
489
490 void DivPlatformGA20::renderSamples(int sysID) {
491 memset(sampleMem,0x00,getSampleMemCapacity());
492 memset(sampleOffGA20,0,32768*sizeof(unsigned int));
493 memset(sampleLoaded,0,32768*sizeof(bool));
494
495 memCompo=DivMemoryComposition();
496 memCompo.name="Sample ROM";
497
498 size_t memPos=0;
499 for (int i=0; i<parent->song.sampleLen; i++) {
500 DivSample* s=parent->song.sample[i];
501 if (!s->renderOn[0][sysID]) {
502 sampleOffGA20[i]=0;
503 continue;
504 }
505
506 const int length=s->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT);
507 int actualLength=MIN((int)(getSampleMemCapacity()-memPos)-1,length);
508 if (actualLength>0) {
509 sampleOffGA20[i]=memPos;
510 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+actualLength+1));
511 for (int j=0; j<actualLength; j++) {
512 // convert to 8 bit unsigned
513 unsigned char val=((unsigned char)(s->data8[j]))^0x80;
514 sampleMem[memPos++]=CLAMP(val,0x01,0xff);
515 }
516 // write end of sample marker
517 memset(&sampleMem[memPos],0x00,1);
518 memPos+=1;
519 }
520 if ((memPos+MAX(actualLength,0))>=(getSampleMemCapacity()-1)) {
521 logW("out of GA20 PCM memory for sample %d!",i);
522 break;
523 } else {
524 sampleLoaded[i]=true;
525 }
526 // align to 16 byte
527 memPos=(memPos+0xf)&~0xf;
528 }
529 sampleMemLen=memPos;
530
531 memCompo.used=sampleMemLen;
532 memCompo.capacity=1048576;
533 }
534
535 int DivPlatformGA20::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
536 parent=p;
537 dumpWrites=false;
538 skipRegisterWrites=false;
539
540 for (int i=0; i<4; i++) {
541 isMuted[i]=false;
542 oscBuf[i]=new DivDispatchOscBuffer;
543 }
544 sampleMem=new unsigned char[getSampleMemCapacity()];
545 sampleMemLen=0;
546 setFlags(flags);
547 ga20BufLen=65536;
548 for (int i=0; i<4; i++) ga20Buf[i]=new short[ga20BufLen];
549 reset();
550
551 return 4;
552 }
553
554 void DivPlatformGA20::quit() {
555 delete[] sampleMem;
556 for (int i=0; i<4; i++) {
557 delete[] ga20Buf[i];
558 delete oscBuf[i];
559 }
560 }
561
562 // initialization of important arrays
563 DivPlatformGA20::DivPlatformGA20():
564 DivDispatch(),
565 iremga20_intf(),
566 ga20(*this) {
567 sampleOffGA20=new unsigned int[32768];
568 sampleLoaded=new bool[32768];
569 }
570
571 DivPlatformGA20::~DivPlatformGA20() {
572 delete[] sampleOffGA20;
573 delete[] sampleLoaded;
574 }