Debian Codegraph @

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