Debian Codegraph @

src/engine/platform/amiga.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 #define _USE_MATH_DEFINES
21 #include "amiga.h"
22 #include "../engine.h"
23 #include "../../ta-log.h"
24 #include <math.h>
25
26 #define AMIGA_DIVIDER 8
27 #define AMIGA_VPMASK 7
28 #define CHIP_DIVIDER 16
29
30 #define chWrite(c,a,v) rWrite(((c)<<4)+0xa0+(a),(v));
31
32 const char* regCheatSheetAmiga[]={
33 "DMACON", "96",
34 "INTENA", "9A",
35 "ADKCON", "9E",
36
37 "AUD0LCH", "A0",
38 "AUD0LCL", "A2",
39 "AUD0LEN", "A4",
40 "AUD0PER", "A6",
41 "AUD0VOL", "A8",
42 "AUD0DAT", "AA",
43
44 "AUD1LCH", "B0",
45 "AUD1LCL", "B2",
46 "AUD1LEN", "B4",
47 "AUD1PER", "B6",
48 "AUD1VOL", "B8",
49 "AUD1DAT", "BA",
50
51 "AUD2LCH", "C0",
52 "AUD2LCL", "C2",
53 "AUD2LEN", "C4",
54 "AUD2PER", "C6",
55 "AUD2VOL", "C8",
56 "AUD2DAT", "CA",
57
58 "AUD3LCH", "D0",
59 "AUD3LCL", "D2",
60 "AUD3LEN", "D4",
61 "AUD3PER", "D6",
62 "AUD3VOL", "D8",
63 "AUD3DAT", "DA",
64 NULL
65 };
66
67 const char** DivPlatformAmiga::getRegisterSheet() {
68 return regCheatSheetAmiga;
69 }
70
71 #define writeAudDat(x) \
72 chan[i].audDat=x; \
73 if (i<3 && chan[i].useV) { \
74 chan[i+1].outVol=(unsigned char)chan[i].audDat^0x80; \
75 if (chan[i+1].outVol>64) chan[i+1].outVol=64; \
76 } \
77 if (i<3 && chan[i].useP) { \
78 chan[i+1].freq=(unsigned char)chan[i].audDat^0x80; \
79 if (chan[i+1].freq<AMIGA_DIVIDER) chan[i+1].freq=AMIGA_DIVIDER; \
80 }
81
82 void DivPlatformAmiga::acquire(short** buf, size_t len) {
83 }
84
85 void DivPlatformAmiga::acquireDirect(blip_buffer_t** bb, size_t len) {
86 thread_local int outL, outR, output;
87
88 for (int i=0; i<4; i++) {
89 oscBuf[i]->begin(len);
90 }
91
92 int runCount=1;
93 for (size_t h=0; h<len; h++) {
94 // skip heuristic
95 runCount=len-h;
96 //logI("FRAME START - at most %d",runCount);
97 if (delay<runCount) {
98 if (!writes.empty()) {
99 runCount=delay;
100 }
101 }
102 for (int i=0; i<4; i++) {
103 if (!amiga.mustDMA[i] && !amiga.audEn[i]) continue;
104 if (amiga.audTick[i]<runCount) {
105 runCount=amiga.audTick[i];
106 }
107 }
108 if (bypassLimits) {
109 for (int i=0; i<4; i++) {
110 if (amiga.incLoc[i]) {
111 runCount=1;
112 break;
113 }
114 }
115 } else {
116 if (228-amiga.hPos<runCount) {
117 runCount=228-amiga.hPos;
118 }
119 }
120 if (runCount>0) {
121 h+=runCount-1;
122 } else {
123 runCount=1;
124 }
125
126 delay-=runCount;
127 if (delay<0) delay=0;
128 if (!writes.empty() && delay<=0) {
129 QueuedWrite w=writes.front();
130
131 //logV("THE WRITE %x = %x",w.addr,w.val);
132
133 if (w.addr==0x96 && !(w.val&0x8000)) delay=6144;
134
135 amiga.write(w.addr,w.val);
136 writes.pop();
137 }
138
139 bool hsync=bypassLimits;
140 outL=0;
141 outR=0;
142
143 // TODO:
144 // - improve DMA overrun behavior
145 // - does V/P mod really work like that?
146 if (!bypassLimits) {
147 amiga.hPos+=runCount;
148 if (amiga.hPos>=228) {
149 amiga.hPos-=228;
150 hsync=true;
151 }
152 }
153 for (int i=0; i<4; i++) {
154 // run DMA
155 if (amiga.audEn[i]) amiga.mustDMA[i]=true;
156 if (amiga.dmaEn && amiga.mustDMA[i] && !amiga.audIr[i]) {
157 amiga.audTick[i]-=runCount;
158 if (amiga.audTick[i]<0) {
159 amiga.audTick[i]+=MAX(runCount,amiga.audPer[i]);
160 if (amiga.audByte[i]) {
161 // read next samples
162 if (!amiga.incLoc[i]) {
163 amiga.audDat[0][i]=sampleMem[(amiga.dmaLoc[i])&chipMask];
164 amiga.audDat[1][i]=sampleMem[(amiga.dmaLoc[i]+1)&chipMask];
165 amiga.incLoc[i]=true;
166 }
167
168 amiga.audWord[i]=!amiga.audWord[i];
169 }
170
171 amiga.mustDMA[i]=amiga.audEn[i];
172
173 amiga.audByte[i]=!amiga.audByte[i];
174 if (!amiga.audByte[i] && (amiga.useV[i] || amiga.useP[i])) {
175 amiga.nextOut2[i]=((unsigned char)amiga.audDat[0][i])<<8|((unsigned char)amiga.audDat[1][i]);
176 if (i<3) {
177 if (amiga.useV[i] && amiga.useP[i]) {
178 if (amiga.audWord[i]) {
179 amiga.audPer[i+1]=amiga.nextOut2[i];
180 } else {
181 amiga.audVol[i+1]=amiga.nextOut2[i];
182 }
183 } else if (amiga.useV[i]) {
184 amiga.audVol[i+1]=amiga.nextOut2[i];
185 } else {
186 amiga.audPer[i+1]=amiga.nextOut2[i];
187 }
188 }
189 } else if (!amiga.useV[i] && !amiga.useP[i]) {
190 amiga.nextOut[i]=amiga.audDat[amiga.audByte[i]][i];
191 }
192 }
193
194 if (hsync) {
195 if (amiga.incLoc[i]) {
196 amiga.incLoc[i]=false;
197 amiga.dmaLoc[i]+=2;
198 // check for length
199 if ((--amiga.dmaLen[i])==0) {
200 if (amiga.audInt[i]) {
201 amiga.audIr[i]=true;
202 irq(i);
203 }
204 amiga.dmaLoc[i]=amiga.audLoc[i];
205 amiga.dmaLen[i]=amiga.audLen[i];
206 }
207 }
208 }
209 }
210
211 // output
212 if (!isMuted[i]) {
213 if ((amiga.audVol[i]&127)>=64) {
214 output=amiga.nextOut[i]<<6;
215 } else if ((amiga.audVol[i]&127)==0) {
216 output=0;
217 } else {
218 output=amiga.nextOut[i]*amiga.audVol[i];
219 }
220 if (i==0 || i==3) {
221 outL+=(output*sep1)>>7;
222 outR+=(output*sep2)>>7;
223 } else {
224 outL+=(output*sep2)>>7;
225 outR+=(output*sep1)>>7;
226 }
227 oscBuf[i]->putSample(h,(amiga.nextOut[i]*MIN(64,amiga.audVol[i]&127))<<1);
228 } else {
229 // TODO: we can remove this!
230 oscBuf[i]->putSample(h,0);
231 }
232 }
233
234 if (outL!=oldOut[0]) {
235 blip_add_delta(bb[0],h,outL-oldOut[0]);
236 oldOut[0]=outL;
237 }
238 if (outR!=oldOut[1]) {
239 blip_add_delta(bb[1],h,outR-oldOut[1]);
240 oldOut[1]=outR;
241 }
242 }
243
244 for (int i=0; i<4; i++) {
245 oscBuf[i]->end(len);
246 }
247 }
248
249 void DivPlatformAmiga::postProcess(short* buf, int outIndex, size_t len, int sampleRate) {
250 // filtering
251 double filtFreq=100000.0;
252 if (filterOn) {
253 if (amigaModel) {
254 filtFreq=12000.0;
255 } else {
256 filtFreq=8000.0;
257 }
258 } else {
259 if (!amigaModel) filtFreq=18000.0;
260 }
261 if (filtFreq>=((double)sampleRate/2)) return;
262 filtConst=sin(M_PI*filtFreq/((double)sampleRate*2.0))*4096.0;
263
264 for (size_t i=0; i<len; i++) {
265 filter[outIndex][0]+=(filtConst*(buf[i]-filter[outIndex][0]))>>12;
266 filter[outIndex][1]+=(filtConst*(filter[outIndex][0]-filter[outIndex][1]))>>12;
267 buf[i]=filter[outIndex][1];
268 }
269 }
270
271 void DivPlatformAmiga::irq(int ch) {
272 // disable interrupt
273 rWrite(0x9a,128<<ch);
274
275 if (chan[ch].irLocL==0x400 && chan[ch].irLocH==0 && chan[ch].irLen==1) {
276 // turn off DMA
277 rWrite(0x96,1<<ch);
278 }
279
280 // acknowledge interrupt
281 rWrite(0x9c,128<<ch);
282 }
283
284 #define UPDATE_DMA(x) \
285 dmaLen[x]=audLen[x]; \
286 dmaLoc[x]=audLoc[x]; \
287 audByte[x]=true; \
288 audTick[x]=0;
289
290 void DivPlatformAmiga::Amiga::write(unsigned short addr, unsigned short val) {
291 if (addr&1) return;
292
293 switch (addr&0x1fe) {
294 case 0x96: { // DMACON
295 if (val&32768) {
296 if (val&1) audEn[0]=true;
297 if (val&2) audEn[1]=true;
298 if (val&4) audEn[2]=true;
299 if (val&8) audEn[3]=true;
300 if (val&512) dmaEn=true;
301 } else {
302 if (val&1) {
303 audEn[0]=false;
304 }
305 if (val&2) {
306 audEn[1]=false;
307 }
308 if (val&4) {
309 audEn[2]=false;
310 }
311 if (val&8) {
312 audEn[3]=false;
313 }
314 if (val&512) {
315 dmaEn=false;
316 }
317 }
318 break;
319 }
320 case 0x9a: { // INTENA
321 if (val&32768) {
322 if (val&128) audInt[0]=true;
323 if (val&256) audInt[1]=true;
324 if (val&512) audInt[2]=true;
325 if (val&1024) audInt[3]=true;
326 } else {
327 if (val&128) audInt[0]=false;
328 if (val&256) audInt[1]=false;
329 if (val&512) audInt[2]=false;
330 if (val&1024) audInt[3]=false;
331 }
332 break;
333 }
334 case 0x9c: { // INTREQ
335 if (val&32768) {
336 if (val&128) {
337 audIr[0]=true;
338 }
339 if (val&256) {
340 audIr[1]=true;
341 }
342 if (val&512) {
343 audIr[2]=true;
344 }
345 if (val&1024) {
346 audIr[3]=true;
347 }
348 } else {
349 if (val&128) audIr[0]=false;
350 if (val&256) audIr[1]=false;
351 if (val&512) audIr[2]=false;
352 if (val&1024) audIr[3]=false;
353 }
354 break;
355 }
356 case 0x9e: { // ADKCON
357 if (val&32768) {
358 if (val&1) useV[0]=true;
359 if (val&2) useV[1]=true;
360 if (val&4) useV[2]=true;
361 if (val&8) useV[3]=true;
362 if (val&16) useP[0]=true;
363 if (val&32) useP[1]=true;
364 if (val&64) useP[2]=true;
365 if (val&128) useP[3]=true;
366 } else {
367 if (val&1) useV[0]=false;
368 if (val&2) useV[1]=false;
369 if (val&4) useV[2]=false;
370 if (val&8) useV[3]=false;
371 if (val&16) useP[0]=false;
372 if (val&32) useP[1]=false;
373 if (val&64) useP[2]=false;
374 if (val&128) useP[3]=false;
375 }
376 break;
377 }
378 default: { // AUDx
379 if (addr>=0xa0 && addr<0xe0) {
380 const unsigned char ch=((addr-0xa0)>>4)&3;
381 bool updateDMA=false;
382 switch (addr&15) {
383 case 0: // LCH
384 audLoc[ch]&=0xffff;
385 audLoc[ch]|=val<<16;
386 updateDMA=true;
387 break;
388 case 2: // LCL
389 audLoc[ch]&=0xffff0000;
390 audLoc[ch]|=val&0xfffe;
391 updateDMA=true;
392 break;
393 case 4: // LEN
394 audLen[ch]=val;
395 updateDMA=true;
396 break;
397 case 6: // PER
398 audPer[ch]=val;
399 break;
400 case 8: // VOL
401 audVol[ch]=val;
402 break;
403 case 10: // DAT
404 audDat[0][ch]=val&0xff;
405 audDat[1][ch]=val>>8;
406 break;
407 }
408 if (updateDMA && !mustDMA[ch]) {
409 UPDATE_DMA(ch);
410 }
411 }
412 break;
413 }
414 }
415 }
416
417 void DivPlatformAmiga::rWrite(unsigned short addr, unsigned short val) {
418 if (addr&1) return;
419
420 //logV("%.3x = %.4x",addr,val);
421 if (!skipRegisterWrites) {
422 writes.push(QueuedWrite(addr,val));
423 regPool[addr>>1]=val;
424
425 if (dumpWrites) {
426 addWrite(addr,val);
427 }
428 }
429 }
430
431 void DivPlatformAmiga::updateWave(int ch) {
432 for (int i=0; i<MIN(256,(chan[ch].audLen<<1)); i++) {
433 sampleMem[(ch<<8)|i]=chan[ch].ws.output[i]^0x80;
434 }
435 }
436
437 void DivPlatformAmiga::tick(bool sysTick) {
438 for (int i=0; i<4; i++) {
439 chan[i].std.next();
440 if (chan[i].std.vol.had) {
441 chan[i].outVol=((chan[i].vol%65)*MIN(64,chan[i].std.vol.val))>>6;
442 chan[i].writeVol=true;
443 }
444 if (NEW_ARP_STRAT) {
445 chan[i].handleArp();
446 } else if (chan[i].std.arp.had) {
447 chan[i].baseFreq=round(NOTE_PERIODIC_NOROUND(parent->calcArp(chan[i].note,chan[i].std.arp.val)));
448 chan[i].freqChanged=true;
449 }
450 if (chan[i].useWave && chan[i].std.wave.had) {
451 if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) {
452 chan[i].wave=chan[i].std.wave.val;
453 chan[i].ws.changeWave1(chan[i].wave);
454 chan[i].updateWave=true;
455 }
456 }
457 if (chan[i].useWave && chan[i].active) {
458 if (chan[i].ws.tick()) {
459 chan[i].updateWave=true;
460 }
461 }
462 if (chan[i].std.pitch.had) {
463 if (chan[i].std.pitch.mode) {
464 chan[i].pitch2+=chan[i].std.pitch.val;
465 CLAMP_VAR(chan[i].pitch2,-32768,32767);
466 } else {
467 chan[i].pitch2=chan[i].std.pitch.val;
468 }
469 chan[i].freqChanged=true;
470 }
471 if (chan[i].std.phaseReset.had) {
472 if (chan[i].std.phaseReset.val==1 && chan[i].active) {
473 chan[i].keyOn=true;
474 }
475 }
476 }
477
478 unsigned short dmaOff=0;
479 unsigned short dmaOn=0;
480 for (int i=0; i<4; i++) {
481 if (chan[i].keyOn || chan[i].keyOff) {
482 chWrite(i,6,1);
483 dmaOff|=1<<i;
484 }
485 }
486
487 if (dmaOff) rWrite(0x96,dmaOff);
488
489 for (int i=0; i<4; i++) {
490 if (chan[i].updateWave) {
491 chan[i].updateWave=false;
492 updateWave(i);
493 }
494 }
495
496 for (int i=0; i<4; i++) {
497 double off=1.0;
498 if (!chan[i].useWave && chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
499 DivSample* s=parent->getSample(chan[i].sample);
500 if (s->centerRate<1) {
501 off=1.0;
502 } else {
503 off=parent->getCenterRate()/(double)s->centerRate;
504 }
505 }
506 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
507 //DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_AMIGA);
508 chan[i].freq=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);
509 if (chan[i].freq>4095) chan[i].freq=4095;
510 if (chan[i].freq<0) chan[i].freq=0;
511
512 chWrite(i,6,chan[i].freq);
513
514 if (chan[i].keyOn) {
515 if (chan[i].useWave) {
516 rWrite(0x9a,(128<<i));
517 chWrite(i,0,0);
518 chWrite(i,2,i<<8);
519 chWrite(i,4,chan[i].audLen);
520 if (dumpWrites) {
521 addWrite(0x200+i,i<<8);
522 addWrite(0x204+i,chan[i].audLen);
523 }
524 dmaOn|=1<<i;
525 } else {
526 if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
527 DivSample* s=parent->getSample(chan[i].sample);
528 int start=chan[i].audPos&(~1);
529 if (start>s->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT)) start=s->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT);
530 int len=s->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT)-start;
531 if (len<0) len=0;
532 if (len>131070) len=131070;
533 len>>=1;
534
535 start+=sampleOff[chan[i].sample];
536
537 if (len<1) {
538 chWrite(i,0,0);
539 chWrite(i,2,0x400);
540 chWrite(i,4,1);
541 if (dumpWrites) {
542 addWrite(0x200+i,0x400);
543 addWrite(0x204+i,1);
544 }
545 } else {
546 chWrite(i,0,start>>16);
547 chWrite(i,2,start);
548 chWrite(i,4,len);
549 if (dumpWrites) {
550 addWrite(0x200+i,start);
551 addWrite(0x204+i,len);
552 }
553 }
554
555 dmaOn|=1<<i;
556 if (s->isLoopable()) {
557 int loopPos=(sampleOff[chan[i].sample]+s->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT))&(~1);
558 int loopEnd=(s->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT)-s->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT))>>1;
559 chan[i].irLocH=loopPos>>16;
560 chan[i].irLocL=loopPos;
561 chan[i].irLen=MIN(65535,loopEnd);
562 } else {
563 chan[i].irLocH=0;
564 chan[i].irLocL=0x400;
565 chan[i].irLen=1;
566 }
567 rWrite(0x9a,0x8000|(128<<i));
568 } else {
569 chWrite(i,0,0);
570 chWrite(i,2,0x400);
571 chWrite(i,4,1);
572 if (dumpWrites) {
573 addWrite(0x200+i,0x400);
574 addWrite(0x204+i,1);
575 }
576 }
577 }
578 }
579 if (chan[i].keyOn) chan[i].keyOn=false;
580 if (chan[i].keyOff) chan[i].keyOff=false;
581 chan[i].freqChanged=false;
582 }
583 }
584
585 if (dmaOn) rWrite(0x96,0x8000|dmaOn);
586
587 for (int i=0; i<4; i++) {
588 if ((dmaOn&(1<<i)) && !chan[i].useWave) {
589 // write latched loc/len
590 if (dumpWrites) {
591 addWrite(0x200+i,(chan[i].irLocH<<16)|chan[i].irLocL);
592 addWrite(0x204+i,chan[i].irLen);
593 } else {
594 chWrite(i,0,chan[i].irLocH);
595 chWrite(i,2,chan[i].irLocL);
596 chWrite(i,4,chan[i].irLen);
597 }
598 }
599 }
600
601 for (int i=0; i<4; i++) {
602 if (chan[i].writeVol) {
603 chan[i].writeVol=false;
604 chWrite(i,8,chan[i].outVol);
605 }
606 }
607
608 if (updateADKCon) {
609 updateADKCon=false;
610 rWrite(0x9e,0xff);
611 rWrite(0x9e,(
612 0x8000|
613 (chan[0].useV?1:0)|
614 (chan[1].useV?2:0)|
615 (chan[2].useV?4:0)|
616 (chan[3].useV?8:0)|
617 (chan[0].useP?16:0)|
618 (chan[1].useP?32:0)|
619 (chan[2].useP?64:0)|
620 (chan[3].useP?128:0)
621 ));
622 }
623 }
624
625 int DivPlatformAmiga::dispatch(DivCommand c) {
626 switch (c.cmd) {
627 case DIV_CMD_NOTE_ON: {
628 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA);
629 if (ins->amiga.useWave) {
630 if (!chan[c.chan].useWave) chan[c.chan].updateWave=true;
631 chan[c.chan].useWave=true;
632 chan[c.chan].audLen=(ins->amiga.waveLen+1)>>1;
633 if (chan[c.chan].insChanged) {
634 if (chan[c.chan].wave<0) {
635 chan[c.chan].wave=0;
636 chan[c.chan].ws.setWidth(chan[c.chan].audLen<<1);
637 chan[c.chan].ws.changeWave1(chan[c.chan].wave);
638 chan[c.chan].updateWave=true;
639 }
640 }
641 chan[c.chan].sampleNote=DIV_NOTE_NULL;
642 chan[c.chan].sampleNoteDelta=0;
643 } else {
644 if (c.value!=DIV_NOTE_NULL) {
645 chan[c.chan].sample=ins->amiga.getSample(c.value);
646 chan[c.chan].sampleNote=c.value;
647 c.value=ins->amiga.getFreq(c.value);
648 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
649 }
650 chan[c.chan].useWave=false;
651 }
652 if (c.value!=DIV_NOTE_NULL) {
653 chan[c.chan].baseFreq=round(NOTE_PERIODIC_NOROUND(c.value));
654 }
655 if (chan[c.chan].useWave || chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) {
656 chan[c.chan].sample=-1;
657 }
658 if (chan[c.chan].setPos) {
659 chan[c.chan].setPos=false;
660 } else {
661 chan[c.chan].audPos=0;
662 }
663 chan[c.chan].audSub=0;
664 if (c.value!=DIV_NOTE_NULL) {
665 chan[c.chan].freqChanged=true;
666 chan[c.chan].note=c.value;
667 }
668 chan[c.chan].active=true;
669 chan[c.chan].keyOn=true;
670 chan[c.chan].macroInit(ins);
671 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
672 chan[c.chan].outVol=chan[c.chan].vol;
673 chan[c.chan].writeVol=true;
674 }
675 if (chan[c.chan].useWave) {
676 chan[c.chan].ws.init(ins,chan[c.chan].audLen<<1,255,chan[c.chan].insChanged);
677 chan[c.chan].updateWave=true;
678 }
679 chan[c.chan].insChanged=false;
680 break;
681 }
682 case DIV_CMD_NOTE_OFF:
683 chan[c.chan].sample=-1;
684 chan[c.chan].active=false;
685 chan[c.chan].keyOff=true;
686 chan[c.chan].macroInit(NULL);
687 break;
688 case DIV_CMD_NOTE_OFF_ENV:
689 case DIV_CMD_ENV_RELEASE:
690 chan[c.chan].std.release();
691 break;
692 case DIV_CMD_INSTRUMENT:
693 if (chan[c.chan].ins!=c.value || c.value2==1) {
694 chan[c.chan].ins=c.value;
695 chan[c.chan].insChanged=true;
696 }
697 break;
698 case DIV_CMD_VOLUME:
699 if (chan[c.chan].vol!=c.value) {
700 chan[c.chan].vol=c.value;
701 if (!chan[c.chan].std.vol.has) {
702 chan[c.chan].outVol=c.value;
703 chan[c.chan].writeVol=true;
704 }
705 }
706 break;
707 case DIV_CMD_GET_VOLUME:
708 if (chan[c.chan].std.vol.has) {
709 return chan[c.chan].vol;
710 }
711 return chan[c.chan].outVol;
712 break;
713 case DIV_CMD_PITCH:
714 chan[c.chan].pitch=c.value;
715 chan[c.chan].freqChanged=true;
716 break;
717 case DIV_CMD_WAVE:
718 if (!chan[c.chan].useWave) break;
719 chan[c.chan].wave=c.value;
720 chan[c.chan].keyOn=true;
721 chan[c.chan].ws.changeWave1(chan[c.chan].wave);
722 chan[c.chan].updateWave=true;
723 break;
724 case DIV_CMD_NOTE_PORTA: {
725 int destFreq=round(NOTE_PERIODIC_NOROUND(c.value2+chan[c.chan].sampleNoteDelta));
726 bool return2=false;
727 if (destFreq>chan[c.chan].baseFreq) {
728 chan[c.chan].baseFreq+=c.value;
729 if (chan[c.chan].baseFreq>=destFreq) {
730 chan[c.chan].baseFreq=destFreq;
731 return2=true;
732 }
733 } else {
734 chan[c.chan].baseFreq-=c.value;
735 if (chan[c.chan].baseFreq<=destFreq) {
736 chan[c.chan].baseFreq=destFreq;
737 return2=true;
738 }
739 }
740 chan[c.chan].freqChanged=true;
741 if (return2) {
742 chan[c.chan].inPorta=false;
743 return 2;
744 }
745 break;
746 }
747 case DIV_CMD_LEGATO: {
748 chan[c.chan].baseFreq=round(NOTE_PERIODIC_NOROUND(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))));
749 chan[c.chan].freqChanged=true;
750 chan[c.chan].note=c.value;
751 break;
752 }
753 case DIV_CMD_PRE_PORTA:
754 if (chan[c.chan].active && c.value2) {
755 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA));
756 }
757 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);
758 chan[c.chan].inPorta=c.value;
759 break;
760 case DIV_CMD_SAMPLE_POS:
761 if (chan[c.chan].useWave) break;
762 chan[c.chan].audPos=c.value;
763 if (chan[c.chan].active) chan[c.chan].keyOn=true;
764 chan[c.chan].setPos=true;
765 break;
766 case DIV_CMD_AMIGA_FILTER:
767 filterOn=c.value;
768 filtConst=filterOn?filtConstOn:filtConstOff;
769 break;
770 case DIV_CMD_AMIGA_AM:
771 chan[c.chan].useV=c.value;
772 updateADKCon=true;
773 break;
774 case DIV_CMD_AMIGA_PM:
775 chan[c.chan].useP=c.value;
776 updateADKCon=true;
777 break;
778 case DIV_CMD_GET_VOLMAX:
779 return 64;
780 break;
781 case DIV_CMD_MACRO_OFF:
782 chan[c.chan].std.mask(c.value,true);
783 break;
784 case DIV_CMD_MACRO_ON:
785 chan[c.chan].std.mask(c.value,false);
786 break;
787 case DIV_CMD_MACRO_RESTART:
788 chan[c.chan].std.restart(c.value);
789 break;
790 default:
791 break;
792 }
793 return 1;
794 }
795
796 void DivPlatformAmiga::muteChannel(int ch, bool mute) {
797 isMuted[ch]=mute;
798 }
799
800 void DivPlatformAmiga::forceIns() {
801 for (int i=0; i<4; i++) {
802 chan[i].insChanged=true;
803 chan[i].freqChanged=true;
804 chan[i].writeVol=true;
805 /*chan[i].keyOn=false;
806 chan[i].keyOff=false;
807 chan[i].sample=-1;*/
808 if (!chan[i].useWave) {
809 rWrite(0x96,1<<i);
810 }
811 }
812 }
813
814 void* DivPlatformAmiga::getChanState(int ch) {
815 return &chan[ch];
816 }
817
818 DivDispatchOscBuffer* DivPlatformAmiga::getOscBuffer(int ch) {
819 return oscBuf[ch];
820 }
821
822 void DivPlatformAmiga::reset() {
823 writes.clear();
824 memset(regPool,0,256*sizeof(unsigned short));
825 for (int i=0; i<4; i++) {
826 chan[i]=DivPlatformAmiga::Channel();
827 chan[i].std.setEngine(parent);
828 chan[i].ws.setEngine(parent);
829 chan[i].ws.init(NULL,32,255);
830 filter[0][i]=0;
831 filter[1][i]=0;
832 }
833 filterOn=false;
834 filtConst=filterOn?filtConstOn:filtConstOff;
835 updateADKCon=true;
836 delay=0;
837 oldOut[0]=0;
838 oldOut[1]=0;
839
840 amiga=Amiga();
841 // enable DMA
842 rWrite(0x96,0x8200);
843 }
844
845 int DivPlatformAmiga::getOutputCount() {
846 return 2;
847 }
848
849 bool DivPlatformAmiga::keyOffAffectsArp(int ch) {
850 return true;
851 }
852
853 bool DivPlatformAmiga::hasAcquireDirect() {
854 return true;
855 }
856
857 DivMacroInt* DivPlatformAmiga::getChanMacroInt(int ch) {
858 return &chan[ch].std;
859 }
860
861 DivSamplePos DivPlatformAmiga::getSamplePos(int ch) {
862 if (ch>=4) return DivSamplePos();
863 if (chan[ch].sample<0 || chan[ch].sample>=parent->song.sampleLen) return DivSamplePos();
864 int audPer=amiga.audPer[ch];
865 if (audPer<1) audPer=1;
866 return DivSamplePos(
867 chan[ch].sample,
868 amiga.dmaLoc[ch]-sampleOff[chan[ch].sample],
869 chipClock/audPer
870 );
871 }
872
873 void DivPlatformAmiga::notifyInsChange(int ins) {
874 for (int i=0; i<4; i++) {
875 if (chan[i].ins==ins) {
876 chan[i].insChanged=true;
877 }
878 }
879 }
880
881 void DivPlatformAmiga::notifyWaveChange(int wave) {
882 for (int i=0; i<4; i++) {
883 if (chan[i].useWave && chan[i].wave==wave) {
884 chan[i].ws.changeWave1(wave);
885 chan[i].updateWave=true;
886 }
887 }
888 }
889
890 void DivPlatformAmiga::notifyInsDeletion(void* ins) {
891 for (int i=0; i<4; i++) {
892 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
893 }
894 }
895
896 void DivPlatformAmiga::setFlags(const DivConfig& flags) {
897 if (flags.getInt("clockSel",0)) {
898 chipClock=COLOR_PAL*4.0/5.0;
899 } else {
900 chipClock=COLOR_NTSC;
901 }
902 CHECK_CUSTOM_CLOCK;
903
904 rate=chipClock;
905 for (int i=0; i<4; i++) {
906 oscBuf[i]->setRate(rate);
907 }
908 int sep=flags.getInt("stereoSep",0)&127;
909 sep1=sep+127;
910 sep2=127-sep;
911 amigaModel=flags.getInt("chipType",0);
912 chipMem=flags.getInt("chipMem",21);
913 if (chipMem<18) chipMem=18;
914 if (chipMem>21) chipMem=21;
915 chipMask=(1<<chipMem)-1;
916 bypassLimits=flags.getBool("bypassLimits",false);
917 if (amigaModel) {
918 filtConstOff=4000;
919 filtConstOn=sin(M_PI*8000.0/(double)rate)*4096.0;
920 } else {
921 filtConstOff=sin(M_PI*16000.0/(double)rate)*4096.0;
922 filtConstOn=sin(M_PI*5500.0/(double)rate)*4096.0;
923 }
924 }
925
926 void DivPlatformAmiga::poke(unsigned int addr, unsigned short val) {
927 rWrite(addr,val);
928 }
929
930 void DivPlatformAmiga::poke(std::vector<DivRegWrite>& wlist) {
931 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
932 }
933
934 unsigned char* DivPlatformAmiga::getRegisterPool() {
935 // update DMACONR
936 regPool[1]=(
937 (amiga.audEn[0]?1:0)|
938 (amiga.audEn[1]?2:0)|
939 (amiga.audEn[2]?4:0)|
940 (amiga.audEn[3]?8:0)|
941 (amiga.dmaEn?512:0)
942 );
943
944 // update ADKCONR
945 regPool[0x10>>1]=(
946 (amiga.useV[0]?1:0)|
947 (amiga.useV[1]?2:0)|
948 (amiga.useV[2]?4:0)|
949 (amiga.useV[3]?8:0)|
950 (amiga.useP[0]?16:0)|
951 (amiga.useP[1]?32:0)|
952 (amiga.useP[2]?64:0)|
953 (amiga.useP[3]?128:0)
954 );
955
956 // update INTENAR
957 regPool[0x1c>>1]=(
958 (amiga.audInt[0]?128:0)|
959 (amiga.audInt[1]?256:0)|
960 (amiga.audInt[2]?512:0)|
961 (amiga.audInt[3]?1024:0)|
962 16384 // INTEN
963 );
964
965 // update INTREQR
966 regPool[0x1e>>1]=(
967 (amiga.audIr[0]?128:0)|
968 (amiga.audIr[1]?256:0)|
969 (amiga.audIr[2]?512:0)|
970 (amiga.audIr[3]?1024:0)
971 );
972
973 return (unsigned char*)regPool;
974 }
975
976 int DivPlatformAmiga::getRegisterPoolSize() {
977 return 128;
978 }
979
980 int DivPlatformAmiga::getRegisterPoolDepth() {
981 return 16;
982 }
983
984 const void* DivPlatformAmiga::getSampleMem(int index) {
985 return index == 0 ? sampleMem : NULL;
986 }
987
988 size_t DivPlatformAmiga::getSampleMemCapacity(int index) {
989 return index == 0 ? (1<<chipMem) : 0;
990 }
991
992 size_t DivPlatformAmiga::getSampleMemUsage(int index) {
993 return index == 0 ? sampleMemLen : 0;
994 }
995
996 bool DivPlatformAmiga::isSampleLoaded(int index, int sample) {
997 if (index!=0) return false;
998 if (sample<0 || sample>32767) return false;
999 return sampleLoaded[sample];
1000 }
1001
1002 const DivMemoryComposition* DivPlatformAmiga::getMemCompo(int index) {
1003 if (index!=0) return NULL;
1004 return &memCompo;
1005 }
1006
1007 void DivPlatformAmiga::renderSamples(int sysID) {
1008 memset(sampleMem,0,2097152);
1009 memset(sampleOff,0,32768*sizeof(unsigned int));
1010 memset(sampleLoaded,0,32768*sizeof(bool));
1011
1012 memCompo=DivMemoryComposition();
1013 memCompo.name="Chip Memory";
1014
1015 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_WAVE_RAM,"Wave RAM",-1,0,1024));
1016 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_RESERVED,"End of Sample",-1,1024,1026));
1017
1018 // first 1024 bytes reserved for wavetable
1019 // the next 2 bytes are reserved for end of sample
1020 size_t memPos=1026;
1021 for (int i=0; i<parent->song.sampleLen; i++) {
1022 DivSample* s=parent->song.sample[i];
1023 if (!s->renderOn[0][sysID]) {
1024 sampleOff[i]=0;
1025 continue;
1026 }
1027
1028 if (memPos>=getSampleMemCapacity()) {
1029 logW("out of Amiga memory for sample %d!",i);
1030 break;
1031 }
1032
1033 int length=s->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT);
1034 int actualLength=MIN((int)(getSampleMemCapacity()-memPos),length);
1035 if (actualLength>0) {
1036 sampleOff[i]=memPos;
1037 memcpy(&sampleMem[memPos],s->data8,actualLength);
1038 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+actualLength));
1039 memPos+=actualLength;
1040 }
1041 // align memPos to short
1042 if (memPos&1) memPos++;
1043 sampleLoaded[i]=true;
1044 }
1045 sampleMemLen=memPos;
1046
1047 memCompo.capacity=1<<chipMem;
1048 memCompo.used=sampleMemLen;
1049 }
1050
1051 int DivPlatformAmiga::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
1052 parent=p;
1053 dumpWrites=false;
1054 skipRegisterWrites=false;
1055 for (int i=0; i<4; i++) {
1056 oscBuf[i]=new DivDispatchOscBuffer;
1057 isMuted[i]=false;
1058 }
1059
1060 // Paula volume is implemented using PWM rather than a multiplication.
1061 // sources:
1062 // - https://www.youtube.com/watch?v=xyQlmsD7PAg
1063 // - https://linusakesson.net/music/paulimba/index.php
1064 memset(volTable,0,64*64);
1065 for (int i=0; i<64; i++) {
1066 for (int j=0; j<64; j++) {
1067 volTable[i][j/AMIGA_DIVIDER]+=(j<i)*(64/AMIGA_DIVIDER);
1068 }
1069 }
1070
1071 sampleMem=new unsigned char[2097152];
1072 sampleMemLen=0;
1073
1074 setFlags(flags);
1075 reset();
1076 return 6;
1077 }
1078
1079 void DivPlatformAmiga::quit() {
1080 delete[] sampleMem;
1081 for (int i=0; i<4; i++) {
1082 delete oscBuf[i];
1083 }
1084 }
1085
1086 // initialization of important arrays
1087 DivPlatformAmiga::DivPlatformAmiga() {
1088 sampleOff=new unsigned int[32768];
1089 sampleLoaded=new bool[32768];
1090 }
1091
1092 DivPlatformAmiga::~DivPlatformAmiga() {
1093 delete[] sampleOff;
1094 delete[] sampleLoaded;
1095 }