Debian Codegraph @

src/engine/platform/tia.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 "tia.h"
21 #include "../engine.h"
22 #include <string.h>
23 #include <math.h>
24
25 #define rWrite(a,v) if (!skipRegisterWrites) {tia.write(a,v); regPool[((a)-0x15)&0x0f]=v; if (dumpWrites) {addWrite(a,v);} }
26
27 const char* regCheatSheetTIA[]={
28 "AUDC0", "15",
29 "AUDC1", "16",
30 "AUDF0", "17",
31 "AUDF1", "18",
32 "AUDV0", "19",
33 "AUDV1", "1A",
34 NULL
35 };
36
37 const char** DivPlatformTIA::getRegisterSheet() {
38 return regCheatSheetTIA;
39 }
40
41 void DivPlatformTIA::acquireDirect(blip_buffer_t** bb, size_t len) {
42 thread_local int out[2];
43 for (int i=0; i<2; i++) {
44 oscBuf[i]->begin(len);
45 }
46
47 for (size_t h=0; h<len; h++) {
48 int advance=len-h;
49
50 if (tia.myCounter<advance) advance=tia.myCounter;
51 if (softwarePitch) {
52 if (tuneCounter>=228) {
53 if (456-tuneCounter<advance) {
54 advance=456-tuneCounter;
55 }
56 } else {
57 if (228-tuneCounter<advance) {
58 advance=228-tuneCounter;
59 }
60 }
61 }
62 if (advance<1) advance=1;
63
64 if (softwarePitch) {
65 int i=-1;
66 tuneCounter+=advance;
67 if (tuneCounter==228) {
68 i=0;
69 }
70 if (tuneCounter>=456) {
71 i=1;
72 tuneCounter=0;
73 }
74 if (i>=0) {
75 if (chan[i].tuneCtr++>=chan[i].curFreq) {
76 int freq=chan[i].freq;
77 chan[i].tuneAcc+=chan[i].tuneFreq;
78 if (chan[i].tuneAcc>=256) {
79 freq++;
80 chan[i].tuneAcc-=256;
81 }
82 chan[i].curFreq=freq;
83 chan[i].tuneCtr=0;
84 rWrite(0x17+i,freq);
85 }
86 }
87 }
88 tia.tick(advance);
89
90 h+=advance-1;
91
92 if (mixingType==2) {
93 out[0]=tia.myCurrentSample[0];
94 out[1]=tia.myCurrentSample[1];
95 } else if (mixingType==1) {
96 out[0]=(tia.myCurrentSample[0]+tia.myCurrentSample[1])>>1;
97 } else {
98 out[0]=tia.myCurrentSample[0];
99 }
100
101 if (out[0]!=prevSample[0]) {
102 blip_add_delta(bb[0],h,out[0]-prevSample[0]);
103 prevSample[0]=out[0];
104 }
105 if (mixingType==2) {
106 blip_add_delta(bb[1],h,out[1]-prevSample[1]);
107 prevSample[1]=out[1];
108 }
109 oscBuf[0]->putSample(h,tia.myChannelOut[0]);
110 oscBuf[1]->putSample(h,tia.myChannelOut[1]);
111 }
112
113 for (int i=0; i<2; i++) {
114 oscBuf[i]->end(len);
115 }
116 }
117
118 unsigned char DivPlatformTIA::dealWithFreq(unsigned char shape, int base, int pitch) {
119 int bp=base+pitch;
120 double mult=0.25*(parent->song.tuning*0.0625)*pow(2.0,double(768+bp)/(256.0*12.0));
121 if (mult<0.5) mult=0.5;
122 int ret=0;
123 switch (shape) {
124 case 1: // buzzy
125 ret=ceil(31400/(30.6*mult))-1;
126 break;
127 case 2: // low buzzy
128 ret=ceil(31400/(480*mult))-1;
129 break;
130 case 3: // flangy
131 ret=ceil(31400/(60*mult))-1;
132 break;
133 case 4: case 5: // square
134 ret=ceil(31400/(4.05*mult))-1;
135 break;
136 case 6: case 7: case 8: case 9: case 10: // pure buzzy/reedy/noise
137 ret=ceil(31400/(63*mult))-1;
138 break;
139 case 12: case 13: // low square
140 ret=round(31400/(4.0*3*mult))-1;
141 break;
142 case 14: case 15: // low pure buzzy/reedy
143 ret=ceil(31400/(3*63*mult))-1;
144 break;
145 }
146
147 if (ret<0) ret=0;
148 if (ret>31) ret=31;
149
150 return ret;
151 }
152
153 int DivPlatformTIA::dealWithFreqNew(int shape, int bp) {
154 double mult=(parent->song.tuning*0.0625)*pow(2.0,double(768+bp)/(256.0*12.0));
155 double clock=chipClock/28.5;
156 switch (shape) {
157 case 1: // buzzy
158 mult*=30;
159 break;
160 case 2: // low buzzy
161 mult*=465;
162 break;
163 case 3: // flangy
164 mult*=58.125;
165 break;
166 case 4: case 5: // square
167 mult*=4;
168 break;
169 case 6: case 7: case 9: case 10: // pure buzzy/reedy
170 mult*=62;
171 break;
172 case 8: // noise
173 mult*=63.875;
174 break;
175 case 12: case 13: // low square
176 mult*=12;
177 break;
178 case 14: case 15: // low pure buzzy/reedy
179 mult*=186;
180 break;
181 }
182 if (mult<1) mult=1;
183 if (clock<mult) {
184 return 0;
185 }
186 double ret=floor(clock/mult);
187 int fract=((clock/mult)-ret)*256;
188 return ret*256+fract-256;
189 }
190
191 void DivPlatformTIA::tick(bool sysTick) {
192 for (int i=0; i<2; i++) {
193 chan[i].std.next();
194 if (chan[i].std.vol.had) {
195 chan[i].outVol=VOL_SCALE_LINEAR_BROKEN(chan[i].vol&15,MIN(15,chan[i].std.vol.val),15);
196 if (chan[i].outVol<0) chan[i].outVol=0;
197 if (isMuted[i]) {
198 rWrite(0x19+i,0);
199 } else {
200 rWrite(0x19+i,chan[i].outVol&15);
201 }
202 }
203 chan[i].handleArp();
204 if (chan[i].std.wave.had) {
205 chan[i].shape=chan[i].std.wave.val&15;
206 rWrite(0x15+i,chan[i].shape);
207 chan[i].freqChanged=true;
208 }
209 if (chan[i].std.pitch.had) {
210 if (chan[i].std.pitch.mode) {
211 chan[i].pitch2+=chan[i].std.pitch.val;
212 CLAMP_VAR(chan[i].pitch2,-32768,32767);
213 } else {
214 chan[i].pitch2=chan[i].std.pitch.val;
215 }
216 chan[i].freqChanged=true;
217 }
218 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
219 if (chan[i].fixedArp) {
220 chan[i].freq=chan[i].baseNoteOverride&31;
221 chan[i].tuneFreq=0;
222 if (!skipRegisterWrites && dumpWrites) {
223 addWrite(0xfffe0000+i,chan[i].freq*256);
224 }
225 } else if (oldPitch) {
226 int bf=chan[i].baseFreq;
227 if (!chan[i].fixedArp) {
228 bf+=chan[i].arpOff<<8;
229 }
230 chan[i].freq=dealWithFreq(chan[i].shape,bf,chan[i].pitch+chan[i].pitch2);
231 if (chan[i].shape==4 || chan[i].shape==5) {
232 if (bf<39*256) {
233 rWrite(0x15+i,6);
234 chan[i].freq=dealWithFreq(6,bf,chan[i].pitch+chan[i].pitch2);
235 } else if (bf<59*256) {
236 rWrite(0x15+i,12);
237 chan[i].freq=dealWithFreq(12,bf,chan[i].pitch+chan[i].pitch2);
238 } else {
239 rWrite(0x15+i,chan[i].shape);
240 }
241 }
242 if (chan[i].freq>31) chan[i].freq=31;
243 chan[i].tuneFreq=0;
244 } else {
245 int bf=chan[i].baseFreq+(chan[i].arpOff<<8);
246 int shape=chan[i].shape;
247 if (shape==4 || shape==5) {
248 if (bf<40*256) {
249 shape=6;
250 rWrite(0x15+i,6);
251 } else if (bf<59*256) {
252 shape=12;
253 rWrite(0x15+i,12);
254 } else {
255 rWrite(0x15+i,4);
256 }
257 }
258 bf+=chan[i].pitch+chan[i].pitch2;
259 int freq=dealWithFreqNew(shape,bf);
260 if (freq>=31*256) freq=31*256;
261 if (softwarePitch && !skipRegisterWrites && dumpWrites) {
262 addWrite(0xfffe0000+i,freq);
263 }
264 chan[i].freq=freq>>8;
265 chan[i].tuneFreq=freq&255;
266 }
267
268 if (chan[i].keyOff) {
269 rWrite(0x19+i,0);
270 }
271 if (!softwarePitch) {
272 if (chan[i].tuneFreq>=128) chan[i].freq++;
273 rWrite(0x17+i,chan[i].freq);
274 if (!skipRegisterWrites && dumpWrites) {
275 addWrite(0xfffe0000+i,chan[i].freq<<8);
276 }
277 }
278 if (chan[i].keyOn) chan[i].keyOn=false;
279 if (chan[i].keyOff) chan[i].keyOff=false;
280 chan[i].freqChanged=false;
281 }
282 }
283 }
284
285 int DivPlatformTIA::dispatch(DivCommand c) {
286 switch (c.cmd) {
287 case DIV_CMD_NOTE_ON: {
288 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_TIA);
289 if (c.value!=DIV_NOTE_NULL) {
290 chan[c.chan].baseFreq=c.value<<8;
291 chan[c.chan].freqChanged=true;
292 chan[c.chan].note=c.value;
293 }
294 chan[c.chan].active=true;
295 chan[c.chan].keyOn=true;
296 rWrite(0x15+c.chan,chan[c.chan].shape);
297 chan[c.chan].macroInit(ins);
298 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
299 chan[c.chan].outVol=chan[c.chan].vol;
300 }
301 if (chan[c.chan].insChanged) {
302 if (!chan[c.chan].std.wave.will) {
303 chan[c.chan].shape=4;
304 rWrite(0x15+c.chan,chan[c.chan].shape);
305 }
306 chan[c.chan].insChanged=false;
307 }
308 if (isMuted[c.chan]) {
309 rWrite(0x19+c.chan,0);
310 } else {
311 rWrite(0x19+c.chan,chan[c.chan].vol&15);
312 }
313 break;
314 }
315 case DIV_CMD_NOTE_OFF:
316 chan[c.chan].keyOff=true;
317 chan[c.chan].active=false;
318 chan[c.chan].macroInit(NULL);
319 break;
320 case DIV_CMD_NOTE_OFF_ENV:
321 case DIV_CMD_ENV_RELEASE:
322 chan[c.chan].std.release();
323 break;
324 case DIV_CMD_VOLUME: {
325 chan[c.chan].vol=c.value;
326 if (!chan[c.chan].std.vol.has) {
327 chan[c.chan].outVol=c.value;
328 }
329 if (isMuted[c.chan] || !chan[c.chan].active) {
330 rWrite(0x19+c.chan,0);
331 } else {
332 rWrite(0x19+c.chan,chan[c.chan].outVol&15);
333 }
334 break;
335 }
336 case DIV_CMD_GET_VOLUME: {
337 return chan[c.chan].vol;
338 break;
339 }
340 case DIV_CMD_INSTRUMENT:
341 if (chan[c.chan].ins!=c.value || c.value2==1) {
342 chan[c.chan].insChanged=true;
343 }
344 chan[c.chan].ins=c.value;
345 break;
346 case DIV_CMD_PITCH: {
347 chan[c.chan].pitch=c.value;
348 chan[c.chan].freqChanged=true;
349 break;
350 }
351 case DIV_CMD_NOTE_PORTA: {
352 int destFreq=c.value2<<8;
353 bool return2=false;
354 if (destFreq>chan[c.chan].baseFreq) {
355 chan[c.chan].baseFreq+=c.value;
356 if (chan[c.chan].baseFreq>=destFreq) {
357 chan[c.chan].baseFreq=destFreq;
358 return2=true;
359 }
360 } else {
361 chan[c.chan].baseFreq-=c.value;
362 if (chan[c.chan].baseFreq<=destFreq) {
363 chan[c.chan].baseFreq=destFreq;
364 return2=true;
365 }
366 }
367 chan[c.chan].freqChanged=true;
368 if (return2) {
369 chan[c.chan].inPorta=false;
370 return 2;
371 }
372 break;
373 }
374 case DIV_CMD_LEGATO: {
375 chan[c.chan].baseFreq=c.value<<8;
376 chan[c.chan].freqChanged=true;
377 break;
378 }
379 case DIV_CMD_WAVE:
380 chan[c.chan].shape=c.value&15;
381 rWrite(0x15+c.chan,chan[c.chan].shape);
382 chan[c.chan].freqChanged=true;
383 break;
384 case DIV_CMD_MACRO_OFF:
385 chan[c.chan].std.mask(c.value,true);
386 break;
387 case DIV_CMD_MACRO_ON:
388 chan[c.chan].std.mask(c.value,false);
389 break;
390 case DIV_CMD_MACRO_RESTART:
391 chan[c.chan].std.restart(c.value);
392 break;
393 case DIV_CMD_GET_VOLMAX:
394 return 15;
395 break;
396 case DIV_CMD_PRE_PORTA:
397 if (chan[c.chan].active && c.value2) {
398 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_TIA));
399 }
400 chan[c.chan].inPorta=c.value;
401 break;
402 case DIV_CMD_PRE_NOTE:
403 break;
404 case DIV_CMD_EXTERNAL:
405 if (!skipRegisterWrites && dumpWrites) {
406 addWrite(0xfffe0002,c.value);
407 }
408 break;
409 default:
410 //printf("WARNING: unimplemented command %d\n",c.cmd);
411 break;
412 }
413 return 1;
414 }
415
416 void DivPlatformTIA::muteChannel(int ch, bool mute) {
417 isMuted[ch]=mute;
418 if (isMuted[ch]) {
419 rWrite(0x19+ch,0);
420 } else {
421 if (chan[ch].active) rWrite(0x19+ch,chan[ch].outVol&15);
422 }
423 }
424
425 void DivPlatformTIA::forceIns() {
426 for (int i=0; i<2; i++) {
427 chan[i].insChanged=true;
428 if (chan[i].active) {
429 chan[i].freqChanged=true;
430 if (!isMuted[i]) rWrite(0x19+i,chan[i].outVol&15);
431 }
432 }
433 }
434
435 void* DivPlatformTIA::getChanState(int ch) {
436 return &chan[ch];
437 }
438
439 DivMacroInt* DivPlatformTIA::getChanMacroInt(int ch) {
440 return &chan[ch].std;
441 }
442
443 DivDispatchOscBuffer* DivPlatformTIA::getOscBuffer(int ch) {
444 return oscBuf[ch];
445 }
446
447 unsigned char* DivPlatformTIA::getRegisterPool() {
448 return regPool;
449 }
450
451 int DivPlatformTIA::getRegisterPoolSize() {
452 return 6;
453 }
454
455 void DivPlatformTIA::reset() {
456 tuneCounter=0;
457 prevSample[0]=0;
458 prevSample[1]=0;
459 tia.reset(mixingType);
460 memset(regPool,0,16);
461 for (int i=0; i<2; i++) {
462 chan[i]=DivPlatformTIA::Channel();
463 chan[i].std.setEngine(parent);
464 chan[i].vol=0x0f;
465 }
466 }
467
468 float DivPlatformTIA::getPostAmp() {
469 return 0.5f;
470 }
471
472 int DivPlatformTIA::getOutputCount() {
473 return (mixingType==2)?2:1;
474 }
475
476 bool DivPlatformTIA::keyOffAffectsArp(int ch) {
477 return true;
478 }
479
480 bool DivPlatformTIA::hasAcquireDirect() {
481 return true;
482 }
483
484 bool DivPlatformTIA::getLegacyAlwaysSetVolume() {
485 return false;
486 }
487
488 void DivPlatformTIA::notifyInsDeletion(void* ins) {
489 for (int i=0; i<2; i++) {
490 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
491 }
492 }
493
494 void DivPlatformTIA::poke(unsigned int addr, unsigned short val) {
495 rWrite(addr,val);
496 }
497
498 void DivPlatformTIA::poke(std::vector<DivRegWrite>& wlist) {
499 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
500 }
501
502 void DivPlatformTIA::setFlags(const DivConfig& flags) {
503 if (flags.getInt("clockSel",0)) {
504 chipClock=COLOR_PAL*4.0/5.0;
505 } else {
506 chipClock=COLOR_NTSC;
507 }
508 CHECK_CUSTOM_CLOCK;
509 rate=chipClock;
510 mixingType=flags.getInt("mixingType",0)&3;
511 softwarePitch=flags.getBool("softwarePitch",false);
512 oldPitch=flags.getBool("oldPitch",false);
513 for (int i=0; i<2; i++) {
514 oscBuf[i]->setRate(rate);
515 }
516 tia.reset(mixingType);
517 }
518
519 int DivPlatformTIA::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
520 parent=p;
521 dumpWrites=false;
522 skipRegisterWrites=false;
523 mixingType=0;
524 for (int i=0; i<2; i++) {
525 isMuted[i]=false;
526 oscBuf[i]=new DivDispatchOscBuffer;
527 }
528 setFlags(flags);
529 reset();
530 return 2;
531 }
532
533 void DivPlatformTIA::quit() {
534 }