Debian Codegraph @

src/engine/platform/pokey.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 "pokey.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23
24 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
25
26 #define CHIP_DIVIDER 1
27
28 const char* regCheatSheetPOKEY[]={
29 "AUDF1", "0",
30 "AUDC1", "1",
31 "AUDF2", "2",
32 "AUDC2", "3",
33 "AUDF3", "4",
34 "AUDC3", "5",
35 "AUDF4", "6",
36 "AUDC4", "7",
37 "AUDCTL", "8",
38 "SKCTL", "F",
39 NULL
40 };
41
42 // LLsLSsLLsSLsLLn
43 const unsigned char snapPeriodLong[15]={
44 0, 1, 1, 3, 3, 6, 6, 7, 7, 10, 10, 12, 12, 13, 13
45 };
46
47 const unsigned char snapPeriodShort[15]={
48 2, 2, 2, 2, 5, 5, 5, 8, 8, 11, 11, 11, 11, 17, 17
49 };
50
51 // LsSLsLLnLLsLSsL
52 const unsigned char snapPeriodLong16[15]={
53 0, 0, 3, 3, 3, 5, 6, 6, 8, 9, 9, 11, 11, 14, 14
54 };
55
56 const unsigned char snapPeriodShort16[15]={
57 1, 1, 1, 4, 4, 4, 4, 4, 10, 10, 10, 10, 13, 13, 13
58 };
59
60 const unsigned char waveMap[8]={
61 0, 1, 2, 3, 4, 5, 6, 6
62 };
63
64 const char** DivPlatformPOKEY::getRegisterSheet() {
65 return regCheatSheetPOKEY;
66 }
67
68 void DivPlatformPOKEY::acquire(short** buf, size_t len) {
69 if (useAltASAP) {
70 acquireASAP(buf[0],len);
71 } else {
72 acquireMZ(buf[0],len);
73 }
74 }
75
76 void DivPlatformPOKEY::acquireMZ(short* buf, size_t len) {
77 for (int i=0; i<4; i++) {
78 oscBuf[i]->begin(len);
79 }
80
81 for (size_t h=0; h<len; h++) {
82 while (!writes.empty()) {
83 QueuedWrite w=writes.front();
84 Update_pokey_sound_mz(&pokey,w.addr,w.val,0);
85 regPool[w.addr&0x0f]=w.val;
86 writes.pop();
87 }
88
89 mzpokeysnd_process_16(&pokey,&buf[h],1);
90
91 if (++oscBufDelay>=14) {
92 oscBufDelay=0;
93 oscBuf[0]->putSample(h,pokey.outvol_0<<10);
94 oscBuf[1]->putSample(h,pokey.outvol_1<<10);
95 oscBuf[2]->putSample(h,pokey.outvol_2<<10);
96 oscBuf[3]->putSample(h,pokey.outvol_3<<10);
97 }
98 }
99
100 for (int i=0; i<4; i++) {
101 oscBuf[i]->end(len);
102 }
103 }
104
105 void DivPlatformPOKEY::acquireASAP(short* buf, size_t len) {
106 thread_local short oscB[4];
107
108 while (!writes.empty()) {
109 QueuedWrite w=writes.front();
110 altASAP.write(w.addr, w.val);
111 writes.pop();
112 }
113
114 for (int i=0; i<4; i++) {
115 oscBuf[i]->begin(len);
116 }
117
118 for (size_t h=0; h<len; h++) {
119 if (++oscBufDelay>=2) {
120 oscBufDelay=0;
121 buf[h]=altASAP.sampleAudio(oscB);
122
123 for (int i=0; i<4; i++) {
124 oscBuf[i]->putSample(h,oscB[i]);
125 }
126 } else {
127 buf[h]=altASAP.sampleAudio();
128 }
129 }
130
131 for (int i=0; i<4; i++) {
132 oscBuf[i]->end(len);
133 }
134 }
135
136 void DivPlatformPOKEY::tick(bool sysTick) {
137 for (int i=0; i<4; i++) {
138 chan[i].std.next();
139 if (chan[i].std.vol.had) {
140 chan[i].outVol=VOL_SCALE_LINEAR(chan[i].vol&15,MIN(15,chan[i].std.vol.val),15);
141 chan[i].ctlChanged=true;
142 }
143 if (NEW_ARP_STRAT) {
144 chan[i].handleArp();
145 } else if (chan[i].std.arp.had) {
146 if (!chan[i].inPorta) {
147 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
148 }
149 chan[i].freqChanged=true;
150 }
151 if (chan[i].std.duty.had) {
152 audctl=chan[i].std.duty.val;
153 audctlChanged=true;
154 }
155 if (chan[i].std.wave.had) {
156 chan[i].wave=chan[i].std.wave.val;
157 chan[i].ctlChanged=true;
158 chan[i].freqChanged=true;
159 }
160 if (chan[i].std.pitch.had) {
161 if (chan[i].std.pitch.mode) {
162 chan[i].pitch2+=chan[i].std.pitch.val;
163 CLAMP_VAR(chan[i].pitch2,-32768,32767);
164 } else {
165 chan[i].pitch2=chan[i].std.pitch.val;
166 }
167 chan[i].freqChanged=true;
168 }
169 }
170
171 if (audctlChanged) {
172 audctlChanged=false;
173 rWrite(8,audctl);
174 for (int i=0; i<4; i++) {
175 chan[i].freqChanged=true;
176 chan[i].ctlChanged=true;
177 }
178 }
179
180 if (skctlChanged) {
181 skctlChanged=false;
182 rWrite(15,skctl);
183 }
184
185 for (int i=0; i<4; i++) {
186 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
187 chan[i].freq=parent->calcFreq(chan[i].baseFreq,parent->song.linearPitch?chan[i].pitch:0,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,true,0,parent->song.linearPitch?chan[i].pitch2:0,chipClock,CHIP_DIVIDER);
188
189 if ((i==0 && !(audctl&64)) || (i==2 && !(audctl&32)) || i==1 || i==3) {
190 chan[i].freq/=7;
191 switch (chan[i].wave) {
192 case 6:
193 chan[i].freq/=5;
194 chan[i].freq>>=1;
195 break;
196 case 7:
197 if (audctl&1) {
198 chan[i].freq/=5;
199 } else {
200 chan[i].freq/=15;
201 }
202 chan[i].freq>>=1;
203 break;
204 default:
205 chan[i].freq>>=2;
206 break;
207 }
208 } else if ((i==0 && audctl&64) || (i==2 && audctl&32)) {
209 switch (chan[i].wave) {
210 case 6:
211 chan[i].freq<<=1;
212 chan[i].freq/=5;
213 break;
214 case 7:
215 chan[i].freq<<=1;
216 chan[i].freq/=15;
217 break;
218 }
219 }
220
221 if (audctl&1 && !((i==0 && audctl&64) || (i==2 && audctl&32))) {
222 chan[i].freq>>=2;
223 }
224
225 // non-linear pitch
226 if (parent->song.linearPitch==0) {
227 chan[i].freq-=chan[i].pitch;
228 }
229
230 if (--chan[i].freq<0) chan[i].freq=0;
231
232 // snap buzz periods
233 int minFreq8=255;
234 if (chan[i].wave==7) {
235 if ((i==0 && audctl&64) || (i==2 && audctl&32)) {
236 chan[i].freq=15*(chan[i].freq/15)+snapPeriodLong16[(chan[i].freq%15)]+1;
237 } else {
238 if (!(audctl&1)) chan[i].freq=15*(chan[i].freq/15)+snapPeriodLong[(chan[i].freq%15)];
239 }
240 } else if (chan[i].wave==6) {
241 if ((i==0 && audctl&64) || (i==2 && audctl&32)) {
242 chan[i].freq=15*(chan[i].freq/15)+snapPeriodShort16[(chan[i].freq%15)]+1;
243 } else {
244 if (!(audctl&1)) chan[i].freq=15*(chan[i].freq/15)+snapPeriodShort[(chan[i].freq%15)];
245 }
246 minFreq8=251;
247 }
248
249 if ((i==0 && audctl&16) || (i==2 && audctl&8)) {
250 if (chan[i].freq>65535) chan[i].freq=65535;
251 } else {
252 if (chan[i].freq>minFreq8) chan[i].freq=minFreq8;
253 }
254
255 // write frequency
256 if ((i==1 && audctl&16) || (i==3 && audctl&8)) {
257 // ignore - channel is paired
258 } else {
259 rWrite(i<<1,chan[i].freq&0xff);
260 if ((i==0 && audctl&16) || (i==2 && audctl&8)) {
261 rWrite((1+i)<<1,chan[i].freq>>8);
262 }
263 }
264
265 if (chan[i].keyOff) {
266 chan[i].ctlChanged=true;
267 }
268 if (chan[i].keyOn) chan[i].keyOn=false;
269 if (chan[i].keyOff) chan[i].keyOff=false;
270 chan[i].freqChanged=false;
271 }
272 if (chan[i].ctlChanged) {
273 unsigned char val=((chan[i].active && !isMuted[i])?(chan[i].outVol&15):0)|(waveMap[chan[i].wave&7]<<5);
274 chan[i].ctlChanged=false;
275 if ((i==1 && audctl&16) || (i==3 && audctl&8)) {
276 // ignore - channel is paired
277 } else if ((i==0 && audctl&16) || (i==2 && audctl&8)) {
278 rWrite(1+(i<<1),0);
279 rWrite(3+(i<<1),val);
280 } else {
281
282 rWrite(1+(i<<1),val);
283 }
284 }
285 }
286 }
287
288 int DivPlatformPOKEY::dispatch(DivCommand c) {
289 switch (c.cmd) {
290 case DIV_CMD_NOTE_ON: {
291 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_POKEY);
292 if (c.value!=DIV_NOTE_NULL) {
293 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
294 chan[c.chan].freqChanged=true;
295 chan[c.chan].note=c.value;
296 }
297 chan[c.chan].active=true;
298 chan[c.chan].keyOn=true;
299 chan[c.chan].macroInit(ins);
300 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
301 chan[c.chan].outVol=chan[c.chan].vol;
302 chan[c.chan].ctlChanged=true;
303 }
304 chan[c.chan].insChanged=false;
305 break;
306 }
307 case DIV_CMD_NOTE_OFF:
308 chan[c.chan].active=false;
309 chan[c.chan].keyOff=true;
310 chan[c.chan].macroInit(NULL);
311 break;
312 case DIV_CMD_NOTE_OFF_ENV:
313 case DIV_CMD_ENV_RELEASE:
314 chan[c.chan].std.release();
315 break;
316 case DIV_CMD_INSTRUMENT:
317 if (chan[c.chan].ins!=c.value || c.value2==1) {
318 chan[c.chan].ins=c.value;
319 chan[c.chan].insChanged=true;
320 }
321 break;
322 case DIV_CMD_VOLUME:
323 if (chan[c.chan].vol!=c.value) {
324 chan[c.chan].vol=c.value;
325 if (!chan[c.chan].std.vol.has) {
326 chan[c.chan].outVol=c.value;
327 if (chan[c.chan].active) {
328 chan[c.chan].ctlChanged=true;
329 }
330 }
331 }
332 break;
333 case DIV_CMD_GET_VOLUME:
334 if (chan[c.chan].std.vol.has) {
335 return chan[c.chan].vol;
336 }
337 return chan[c.chan].outVol;
338 break;
339 case DIV_CMD_PITCH:
340 chan[c.chan].pitch=c.value;
341 chan[c.chan].freqChanged=true;
342 break;
343 case DIV_CMD_WAVE:
344 chan[c.chan].wave=c.value;
345 chan[c.chan].ctlChanged=true;
346 chan[c.chan].freqChanged=true; //round freq for specific waves
347 break;
348 case DIV_CMD_STD_NOISE_MODE:
349 audctl=c.value&0xff;
350 audctlChanged=true;
351 break;
352 case DIV_CMD_STD_NOISE_FREQ:
353 skctl=c.value?0x8b:0x03;
354 skctlChanged=true;
355 break;
356 case DIV_CMD_NOTE_PORTA: {
357 int destFreq=NOTE_PERIODIC(c.value2);
358 bool return2=false;
359 if (destFreq>chan[c.chan].baseFreq) {
360 chan[c.chan].baseFreq+=c.value;
361 if (chan[c.chan].baseFreq>=destFreq) {
362 chan[c.chan].baseFreq=destFreq;
363 return2=true;
364 }
365 } else {
366 chan[c.chan].baseFreq-=c.value;
367 if (chan[c.chan].baseFreq<=destFreq) {
368 chan[c.chan].baseFreq=destFreq;
369 return2=true;
370 }
371 }
372 chan[c.chan].freqChanged=true;
373 if (return2) {
374 chan[c.chan].inPorta=false;
375 return 2;
376 }
377 break;
378 }
379 case DIV_CMD_LEGATO:
380 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
381 chan[c.chan].freqChanged=true;
382 chan[c.chan].note=c.value;
383 break;
384 case DIV_CMD_PRE_PORTA:
385 if (chan[c.chan].active && c.value2) {
386 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_POKEY));
387 }
388 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);
389 chan[c.chan].inPorta=c.value;
390 break;
391 case DIV_CMD_GET_VOLMAX:
392 return 15;
393 break;
394 case DIV_CMD_MACRO_OFF:
395 chan[c.chan].std.mask(c.value,true);
396 break;
397 case DIV_CMD_MACRO_ON:
398 chan[c.chan].std.mask(c.value,false);
399 break;
400 case DIV_CMD_MACRO_RESTART:
401 chan[c.chan].std.restart(c.value);
402 break;
403 default:
404 break;
405 }
406 return 1;
407 }
408
409 void DivPlatformPOKEY::muteChannel(int ch, bool mute) {
410 isMuted[ch]=mute;
411 chan[ch].ctlChanged=true;
412 }
413
414 void DivPlatformPOKEY::forceIns() {
415 for (int i=0; i<4; i++) {
416 chan[i].insChanged=true;
417 chan[i].ctlChanged=true;
418 chan[i].freqChanged=true;
419 }
420 audctlChanged=true;
421 skctlChanged=true;
422 }
423
424 void* DivPlatformPOKEY::getChanState(int ch) {
425 return &chan[ch];
426 }
427
428 DivMacroInt* DivPlatformPOKEY::getChanMacroInt(int ch) {
429 return &chan[ch].std;
430 }
431
432 void DivPlatformPOKEY::getPaired(int ch, std::vector<DivChannelPair>& ret) {
433 switch (ch) {
434 case 0:
435 if (audctl&4) ret.push_back(DivChannelPair(_("filter"),2));
436 break;
437 case 1:
438 if (audctl&16) ret.push_back(DivChannelPair(_("16-bit"),0));
439 break;
440 case 2:
441 if (audctl&8) ret.push_back(DivChannelPair(_("16-bit"),3));
442 break;
443 case 3:
444 if (audctl&2) ret.push_back(DivChannelPair(_("filter"),1));
445 break;
446 }
447 }
448
449 DivDispatchOscBuffer* DivPlatformPOKEY::getOscBuffer(int ch) {
450 return oscBuf[ch];
451 }
452
453 unsigned char* DivPlatformPOKEY::getRegisterPool() {
454 if (useAltASAP) {
455 return const_cast<unsigned char*>(altASAP.getRegisterPool());
456 } else {
457 return regPool;
458 }
459 }
460
461 int DivPlatformPOKEY::getRegisterPoolSize() {
462 return 16;
463 }
464
465 void DivPlatformPOKEY::reset() {
466 while (!writes.empty()) writes.pop();
467 memset(regPool,0,16);
468 for (int i=0; i<4; i++) {
469 chan[i]=DivPlatformPOKEY::Channel();
470 chan[i].std.setEngine(parent);
471 }
472 if (dumpWrites) {
473 addWrite(0xffffffff,0);
474 }
475
476 if (useAltASAP) {
477 altASAP.reset();
478 } else {
479 ResetPokeyState(&pokey);
480 }
481
482 audctl=0;
483 audctlChanged=true;
484 skctl=3;
485 skctlChanged=true;
486 }
487
488 bool DivPlatformPOKEY::keyOffAffectsArp(int ch) {
489 return true;
490 }
491
492 float DivPlatformPOKEY::getPostAmp() {
493 return 2.0f;
494 }
495
496 void DivPlatformPOKEY::notifyInsDeletion(void* ins) {
497 for (int i=0; i<4; i++) {
498 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
499 }
500 }
501
502 void DivPlatformPOKEY::setFlags(const DivConfig& flags) {
503 if (flags.getInt("clockSel",0)) {
504 chipClock=COLOR_PAL*2.0/5.0;
505 } else {
506 chipClock=COLOR_NTSC/2.0;
507 }
508 CHECK_CUSTOM_CLOCK;
509
510 if (useAltASAP) {
511 rate=chipClock/7;
512 for (int i=0; i<4; i++) {
513 oscBuf[i]->setRate(rate);
514 }
515 altASAP.init(chipClock,rate);
516 altASAP.reset();
517 } else {
518 rate=chipClock;
519 for (int i=0; i<4; i++) {
520 oscBuf[i]->setRate(rate);
521 }
522 }
523 }
524
525 void DivPlatformPOKEY::poke(unsigned int addr, unsigned short val) {
526 rWrite(addr,val);
527 }
528
529 void DivPlatformPOKEY::poke(std::vector<DivRegWrite>& wlist) {
530 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
531 }
532
533 int DivPlatformPOKEY::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
534 parent=p;
535 dumpWrites=false;
536 skipRegisterWrites=false;
537 oscBufDelay=0;
538 for (int i=0; i<4; i++) {
539 isMuted[i]=false;
540 oscBuf[i]=new DivDispatchOscBuffer;
541 }
542
543 if (!useAltASAP) {
544 MZPOKEYSND_Init(&pokey);
545 }
546
547 setFlags(flags);
548 reset();
549 return 6;
550 }
551
552 void DivPlatformPOKEY::quit() {
553 for (int i=0; i<4; i++) {
554 delete oscBuf[i];
555 }
556 }
557
558 void DivPlatformPOKEY::setAltASAP(bool value) {
559 useAltASAP=value;
560 }
561
562 DivPlatformPOKEY::~DivPlatformPOKEY() {
563 }