Debian Codegraph @

src/engine/platform/lynx.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 "lynx.h"
21 #include "../engine.h"
22 #include "../bsr.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 WRITE_VOLUME(ch,v) rWrite(0x20+(ch<<3),(v))
28 #define WRITE_FEEDBACK(ch,v) rWrite(0x21+(ch<<3),(v))
29 #define WRITE_OUTPUT(ch,v) rWrite(0x22+(ch<<3),(v))
30 #define WRITE_LFSR(ch,v) rWrite(0x23+(ch<<3),(v))
31 #define WRITE_BACKUP(ch,v) rWrite(0x24+(ch<<3),(v))
32 #define WRITE_CONTROL(ch,v) rWrite(0x25+(ch<<3),(v))
33 #define WRITE_OTHER(ch,v) rWrite(0x27+(ch<<3),(v))
34 #define WRITE_ATTEN(ch,v) rWrite((0x40+ch),(v))
35 #define WRITE_STEREO(v) rWrite(0x50,(v))
36
37 #define CHIP_FREQBASE 16000000
38
39 static int32_t clamp(int32_t v, int32_t lo, int32_t hi)
40 {
41 return v<lo?lo:(v>hi?hi:v);
42 }
43
44 const int DUTY_DIVIDERS[]={
45 1, 2, 4, 3, 6, 7, 7, 4, 8, 15, 6, 14, 15, 12, 14, 5,
46 10, 21, 31, 28, 31, 30, 12, 31, 21, 8, 30, 31, 28, 31, 31, 6,
47 12, 63, 14, 62, 9, 28, 62, 15, 14, 42, 8, 21, 62, 63, 15, 60,
48 63, 20, 42, 63, 28, 12, 63, 62, 62, 63, 21, 24, 15, 62, 60, 7,
49 16, 63, 30, 254, 217, 84, 186, 217, 12, 254, 28, 15, 254, 51, 255, 84,
50 217, 120, 210, 63, 60, 255, 255, 254, 254, 217, 63, 252, 17, 42, 124, 85,
51 30, 254, 24, 217, 210, 21, 255, 252, 28, 217, 10, 186, 63, 124, 254, 255,
52 186, 255, 255, 56, 255, 70, 36, 30, 255, 60, 254, 85, 124, 85, 21, 254,
53 22,1533,2047, 868,1953,2046, 420, 651,1533,2044,2046,2047,1016,1785, 105, 126,
54 595, 630, 204,1533,1302,2047,2047,2044,2044, 119, 372,1778,1953, 120, 682,1905,
55 595, 868, 510,2047,2044, 762, 279, 682, 210,1953, 595,1524,1533, 210, 248, 635,
56 60,2047,2047, 62,1905,2044,2046, 42,2047, 510, 252,1905,1778,2047,1533,1016,
57 1953,1860,1778, 219, 48,1905,2047,1778, 682,2047, 465,1020,1785, 126,2044, 434,
58 2044, 63,2047,2046,2047, 280, 30,1953, 210, 682, 868, 89,2046,2047,2047,1524,
59 1302,1785,2047,1860,2047,2046,1016, 372,2047, 84, 630, 70, 252,2047,1533, 682,
60 1905,2046, 372,1905, 90,1533, 217, 168, 340,2047,2047,1302,1533, 28, 186,2047,
61 24,3255, 126,1190, 45,4092, 178, 315, 28, 438, 124, 315,3570, 255,1023,2044,
62 819,1016, 930,3937,1260,1302, 511,4094,3810, 819, 195,2604,1023,4094, 84,1365,
63 18,3810, 56,1023, 42,3937, 819,4092, 124,4095, 30,4094, 85,1524,3906, 63,
64 3906,1023,3255, 120,4095,3570,1020,3937,2667,3556,4094, 195,2044,4095,4095, 762,
65 28,1302, 84,4095,3906,1023, 255, 292, 16,1085, 42,3066, 315,3556,4094,4095,
66 3066,4095,3937, 372, 511,4094, 620,1302, 273, 504,1190, 819,4092,4095,1023, 210,
67 124,1023, 126,3570,3937, 252, 438,4095, 30,4094, 120,4095,4094, 255, 63,1020,
68 4095,1364, 558,2667, 420,4095, 105,1270,1190,3255, 93,1016, 91, 372,4092,3937,
69 3255, 44,3066,1365,1736, 510, 91,4094,1302, 511,3937, 420, 105,3906,4092, 819,
70 252, 585, 255, 210,3937,1016,3570,1023, 455,3066,2044,1365,4094, 126,2667,4092,
71 3810, 93, 63,1364,1365,3906, 120, 28,1023,2044, 238,4095,3556, 255,4095, 372,
72 511,1190,1260,1023,3066, 255, 819, 408,2044, 255,1302,4094, 455,2604,4094, 585,
73 438, 511, 455, 292, 455, 434,1364, 102,1085, 204,3570, 255, 240,4095, 93,4094,
74 3937,4094, 84, 63,4094, 315, 819, 140,1260,3937,4095,3066,2667, 680,4094, 511,
75 4095,2044, 178,1023,1020, 819, 21,3810,4094, 20,1023,3556,3937, 210,2040, 273,
76 252,2667,4095,3906, 63, 124, 930, 455, 510,4094,4092, 511,3570,4095, 30, 744
77 };
78
79 const char* regCheatSheetLynx[]={
80 "AUDIO0_VOLCNTRL", "20",
81 "AUDIO0_FEEDBACK", "21",
82 "AUDIO0_OUTPUT", "22",
83 "AUDIO0_SHIFT", "23",
84 "AUDIO0_BACKUP", "24",
85 "AUDIO0_CONTROL", "25",
86 "AUDIO0_COUNTER", "26",
87 "AUDIO0_OTHER", "27",
88 "AUDIO1_VOLCNTRL", "28",
89 "AUDIO1_FEEDBACK", "29",
90 "AUDIO1_OUTPUT", "2a",
91 "AUDIO1_SHIFT", "2b",
92 "AUDIO1_BACKUP", "2c",
93 "AUDIO1_CONTROL", "2d",
94 "AUDIO1_COUNTER", "2e",
95 "AUDIO1_OTHER", "2f",
96 "AUDIO2_VOLCNTRL", "30",
97 "AUDIO2_FEEDBACK", "31",
98 "AUDIO2_OUTPUT", "32",
99 "AUDIO2_SHIFT", "33",
100 "AUDIO2_BACKUP", "34",
101 "AUDIO2_CONTROL", "35",
102 "AUDIO2_COUNTER", "36",
103 "AUDIO2_OTHER", "37",
104 "AUDIO3_VOLCNTRL", "38",
105 "AUDIO3_FEEDBACK", "39",
106 "AUDIO3_OUTPUT", "3a",
107 "AUDIO3_SHIFT", "3b",
108 "AUDIO3_BACKUP", "3c",
109 "AUDIO3_CONTROL", "3d",
110 "AUDIO3_COUNTER", "3e",
111 "AUDIO3_OTHER", "3f",
112 "ATTENREG0", "40",
113 "ATTENREG1", "41",
114 "ATTENREG2", "42",
115 "ATTENREG3", "43",
116 "MPAN", "44",
117 "MSTEREO", "50",
118 NULL
119 };
120
121
122 const char** DivPlatformLynx::getRegisterSheet() {
123 return regCheatSheetLynx;
124 }
125
126 void DivPlatformLynx::processDAC(int sRate) {
127 for (int i=0; i<4; i++) {
128 if (chan[i].pcm && chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
129 chan[i].sampleAccum-=chan[i].sampleFreq;
130 while (chan[i].sampleAccum<0) {
131 chan[i].sampleAccum+=sRate;
132 DivSample* s=parent->getSample(chan[i].sample);
133 if (s!=NULL) {
134 if (isMuted[i]) {
135 WRITE_OUTPUT(i,0);
136 } else {
137 if (chan[i].samplePos<0 || chan[i].samplePos>=(int)s->samples) {
138 WRITE_OUTPUT(i,0);
139 } else {
140 WRITE_OUTPUT(i,CLAMP((s->data8[chan[i].samplePos]*chan[i].outVol)>>7,-128,127));
141 }
142 }
143 chan[i].samplePos++;
144
145 if (s->isLoopable() && chan[i].samplePos>=s->loopEnd) {
146 chan[i].samplePos=s->loopStart;
147 } else if (chan[i].samplePos>=(int)s->samples) {
148 chan[i].sample=-1;
149 }
150 }
151 }
152 }
153 }
154 }
155
156 void DivPlatformLynx::acquire(short** buf, size_t len) {
157 thread_local int chanBuf[4];
158
159 for (int i=0; i<4; i++) {
160 oscBuf[i]->begin(len);
161 }
162
163 for (size_t h=0; h<len; h++) {
164 processDAC(rate);
165
166 while (!writes.empty()) {
167 QueuedWrite& w=writes.front();
168 mikey->write(w.addr,w.val);
169 writes.pop_front();
170 }
171
172 mikey->sampleAudio(buf[0]+h,buf[1]+h,1,chanBuf);
173
174 for (int i=0; i<4; i++) {
175 oscBuf[i]->putSample(h,chanBuf[i]);
176 }
177 }
178
179 for (int i=0; i<4; i++) {
180 oscBuf[i]->end(len);
181 }
182 }
183
184 void DivPlatformLynx::fillStream(std::vector<DivDelayedWrite>& stream, int sRate, size_t len) {
185 writes.clear();
186 for (size_t i=0; i<len; i++) {
187 processDAC(sRate);
188
189 while (!writes.empty()) {
190 QueuedWrite& w=writes.front();
191 stream.push_back(DivDelayedWrite(i,w.addr,w.val));
192 writes.pop_front();
193 }
194 }
195 regWrites.clear();
196 }
197
198 void DivPlatformLynx::tick(bool sysTick) {
199 for (int i=0; i<4; i++) {
200 chan[i].std.next();
201 if (chan[i].std.vol.had) {
202 if (chan[i].pcm) {
203 chan[i].outVol=((chan[i].vol&127)*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul;
204 } else {
205 chan[i].outVol=((chan[i].vol&127)*MIN(127,chan[i].std.vol.val))>>7;
206 }
207 WRITE_VOLUME(i,(isMuted[i]?0:(chan[i].outVol&127)));
208 }
209 if (NEW_ARP_STRAT) {
210 chan[i].handleArp();
211 } else if (chan[i].std.arp.had) {
212 if (!chan[i].inPorta) {
213 double CHIP_DIVIDER=tuned?DUTY_DIVIDERS[chan[i].duty.val&0x1ff]*8:64;
214 chan[i].actualNote=parent->calcArp(chan[i].note,chan[i].std.arp.val);
215 chan[i].baseFreq=NOTE_PERIODIC(chan[i].actualNote);
216 if (chan[i].pcm) chan[i].sampleBaseFreq=NOTE_FREQUENCY(chan[i].actualNote);
217 chan[i].freqChanged=true;
218 }
219 }
220
221 if (chan[i].std.panL.had) {
222 chan[i].pan&=0x0f;
223 chan[i].pan|=(chan[i].std.panL.val&15)<<4;
224 }
225
226 if (chan[i].std.panR.had) {
227 chan[i].pan&=0xf0;
228 chan[i].pan|=chan[i].std.panR.val&15;
229 }
230
231 if (chan[i].std.panL.had || chan[i].std.panR.had) {
232 WRITE_ATTEN(i,chan[i].pan);
233 }
234
235 if (chan[i].std.pitch.had) {
236 if (chan[i].std.pitch.mode) {
237 chan[i].pitch2+=chan[i].std.pitch.val;
238 CLAMP_VAR(chan[i].pitch2,-32768,32767);
239 } else {
240 chan[i].pitch2=chan[i].std.pitch.val;
241 }
242 chan[i].freqChanged=true;
243 }
244
245 if (chan[i].std.ex1.had) {
246 chan[i].lfsr=chan[i].std.ex1.val&0xfff;
247 chan[i].freqChanged=true;
248 chan[i].updateLFSR=true;
249 }
250
251 if (chan[i].std.phaseReset.had) {
252 if (chan[i].std.phaseReset.val==1) {
253 if (chan[i].pcm && chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
254 chan[i].sampleAccum=0;
255 if (chan[i].setPos) {
256 chan[i].setPos=false;
257 } else {
258 chan[i].samplePos=0;
259 }
260 }
261 chan[i].freqChanged=true;
262 chan[i].updateLFSR=true;
263 }
264 }
265
266 if (chan[i].freqChanged) {
267 if (chan[i].pcm) {
268 double off=1.0;
269 if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
270 DivSample* s=parent->getSample(chan[i].sample);
271 if (s->centerRate<1) {
272 off=1.0;
273 } else {
274 off=(double)s->centerRate/parent->getCenterRate();
275 }
276 }
277 chan[i].sampleFreq=off*parent->calcFreq(chan[i].sampleBaseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,2,chan[i].pitch2,chipClock,CHIP_FREQBASE);
278 if (dumpWrites) addWrite(0xffff0001+(i<<8),chan[i].sampleFreq);
279 } else {
280 if (chan[i].updateLFSR) {
281 WRITE_LFSR(i, (chan[i].lfsr&0xff));
282 WRITE_OTHER(i, ((chan[i].lfsr&0xf00)>>4));
283 chan[i].updateLFSR=false;
284 }
285 if (chan[i].std.duty.had) {
286 chan[i].duty=chan[i].std.duty.val;
287 if (!chan[i].pcm) {
288 WRITE_FEEDBACK(i, chan[i].duty.feedback);
289 }
290 }
291 double divider=tuned?DUTY_DIVIDERS[chan[i].duty.val&0x1ff]*8:64;
292 chan[i].fd=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,divider);
293 WRITE_CONTROL(i, (chan[i].fd.clockDivider|0x18|chan[i].duty.int_feedback7));
294 WRITE_BACKUP( i, chan[i].fd.backup );
295 }
296 chan[i].freqChanged=false;
297 } else if (chan[i].std.duty.had) {
298 chan[i].duty=chan[i].std.duty.val;
299 if (!chan[i].pcm) {
300 if (tuned) {
301 double divider=DUTY_DIVIDERS[chan[i].duty.val&0x1ff]*8;
302 chan[i].fd=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,divider);
303 }
304 WRITE_FEEDBACK(i, chan[i].duty.feedback);
305 WRITE_CONTROL(i, (chan[i].fd.clockDivider|0x18|chan[i].duty.int_feedback7));
306 if (tuned) WRITE_BACKUP( i, chan[i].fd.backup );
307 }
308 }
309 }
310 }
311
312 int DivPlatformLynx::dispatch(DivCommand c) {
313 double CHIP_DIVIDER=tuned?DUTY_DIVIDERS[chan[c.chan].duty.val&0x1ff]*8:64;
314 switch (c.cmd) {
315 case DIV_CMD_NOTE_ON: {
316 bool prevPCM=chan[c.chan].pcm;
317 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_MIKEY);
318 chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:127;
319 chan[c.chan].pcm=(ins->type==DIV_INS_AMIGA || ins->amiga.useSample);
320 if (chan[c.chan].pcm!=prevPCM) {
321 if (chan[c.chan].pcm) {
322 WRITE_FEEDBACK(c.chan,0);
323 WRITE_CONTROL(c.chan,0x18);
324 WRITE_BACKUP(c.chan,0);
325 } else {
326 chan[c.chan].sampleNote=DIV_NOTE_NULL;
327 chan[c.chan].sampleNoteDelta=0;
328 WRITE_FEEDBACK(c.chan,chan[c.chan].duty.feedback);
329 WRITE_CONTROL(c.chan,(chan[c.chan].fd.clockDivider|0x18|chan[c.chan].duty.int_feedback7));
330 WRITE_BACKUP(c.chan,chan[c.chan].fd.backup);
331 }
332 }
333 if (chan[c.chan].pcm) {
334 if (c.value!=DIV_NOTE_NULL) {
335 chan[c.chan].sample=ins->amiga.getSample(c.value);
336 chan[c.chan].sampleNote=c.value;
337 c.value=ins->amiga.getFreq(c.value);
338 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
339 chan[c.chan].sampleBaseFreq=NOTE_FREQUENCY(c.value);
340 } else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) {
341 chan[c.chan].sample=ins->amiga.getSample(chan[c.chan].sampleNote);
342 c.value=ins->amiga.getFreq(chan[c.chan].sampleNote);
343 }
344 chan[c.chan].sampleAccum=0;
345 if (chan[c.chan].setPos) {
346 chan[c.chan].setPos=false;
347 } else {
348 chan[c.chan].samplePos=0;
349 }
350 if (dumpWrites) {
351 addWrite(0xffff0000+(c.chan<<8),chan[c.chan].sample);
352 }
353 }
354 if (c.value!=DIV_NOTE_NULL) {
355 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
356 chan[c.chan].freqChanged=true;
357 chan[c.chan].note=c.value;
358 chan[c.chan].actualNote=c.value;
359 chan[c.chan].updateLFSR=true;
360 }
361 chan[c.chan].active=true;
362 WRITE_VOLUME(c.chan,(isMuted[c.chan]?0:(chan[c.chan].vol&127)));
363 chan[c.chan].macroInit(ins);
364 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
365 chan[c.chan].outVol=chan[c.chan].vol;
366 }
367 break;
368 }
369 case DIV_CMD_NOTE_OFF:
370 chan[c.chan].active=false;
371 WRITE_VOLUME(c.chan,0);
372 WRITE_CONTROL(c.chan,0);
373 chan[c.chan].macroInit(NULL);
374 if (chan[c.chan].pcm) {
375 chan[c.chan].pcm=false;
376 if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
377 }
378 break;
379 case DIV_CMD_LYNX_LFSR_LOAD:
380 chan[c.chan].freqChanged=true;
381 chan[c.chan].lfsr=c.value;
382 chan[c.chan].updateLFSR=true;
383 break;
384 case DIV_CMD_NOTE_OFF_ENV:
385 case DIV_CMD_ENV_RELEASE:
386 chan[c.chan].std.release();
387 break;
388 case DIV_CMD_INSTRUMENT:
389 chan[c.chan].ins=c.value;
390 //chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_MIKEY));
391 break;
392 case DIV_CMD_VOLUME:
393 if (chan[c.chan].vol!=c.value) {
394 chan[c.chan].vol=c.value;
395 if (!chan[c.chan].std.vol.has) {
396 chan[c.chan].outVol=c.value;
397 }
398 if (chan[c.chan].active && !chan[c.chan].pcm) WRITE_VOLUME(c.chan,(isMuted[c.chan]?0:(chan[c.chan].vol&127)));
399 }
400 break;
401 case DIV_CMD_PANNING:
402 chan[c.chan].pan=(c.value&0xf0)|(c.value2>>4);
403 WRITE_ATTEN(c.chan,chan[c.chan].pan);
404 break;
405 case DIV_CMD_GET_VOLUME:
406 if (chan[c.chan].std.vol.has) {
407 return chan[c.chan].vol;
408 }
409 return chan[c.chan].outVol;
410 break;
411 case DIV_CMD_PITCH:
412 chan[c.chan].pitch=c.value;
413 chan[c.chan].freqChanged=true;
414 break;
415 case DIV_CMD_NOTE_PORTA: {
416 int destFreq=NOTE_PERIODIC(c.value2+chan[c.chan].sampleNoteDelta);
417 bool return2=false;
418 if (destFreq>chan[c.chan].baseFreq) {
419 chan[c.chan].baseFreq+=c.value;
420 if (chan[c.chan].baseFreq>=destFreq) {
421 chan[c.chan].baseFreq=destFreq;
422 return2=true;
423 }
424 } else {
425 chan[c.chan].baseFreq-=c.value;
426 if (chan[c.chan].baseFreq<=destFreq) {
427 chan[c.chan].baseFreq=destFreq;
428 return2=true;
429 }
430 }
431 chan[c.chan].freqChanged=true;
432 if (chan[c.chan].pcm && parent->song.linearPitch==2) {
433 chan[c.chan].sampleBaseFreq=chan[c.chan].baseFreq;
434 }
435 if (return2) {
436 chan[c.chan].inPorta=false;
437 return 2;
438 }
439 break;
440 }
441 case DIV_CMD_LEGATO: {
442 int whatAMess=c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0));
443 chan[c.chan].baseFreq=NOTE_PERIODIC(whatAMess);
444 if (chan[c.chan].pcm) {
445 chan[c.chan].sampleBaseFreq=NOTE_FREQUENCY(whatAMess);
446 }
447 chan[c.chan].freqChanged=true;
448 chan[c.chan].note=c.value;
449 chan[c.chan].actualNote=c.value;
450 break;
451 }
452 case DIV_CMD_PRE_PORTA:
453 if (chan[c.chan].active && c.value2) {
454 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_MIKEY));
455 }
456 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);
457 chan[c.chan].inPorta=c.value;
458 break;
459 case DIV_CMD_SAMPLE_POS:
460 chan[c.chan].samplePos=c.value;
461 chan[c.chan].setPos=true;
462 break;
463 case DIV_CMD_GET_VOLMAX:
464 return 127;
465 break;
466 case DIV_CMD_MACRO_OFF:
467 chan[c.chan].std.mask(c.value,true);
468 break;
469 case DIV_CMD_MACRO_ON:
470 chan[c.chan].std.mask(c.value,false);
471 break;
472 case DIV_CMD_MACRO_RESTART:
473 chan[c.chan].std.restart(c.value);
474 break;
475 default:
476 break;
477 }
478 return 1;
479 }
480
481 void DivPlatformLynx::muteChannel(int ch, bool mute) {
482 isMuted[ch]=mute;
483 if (chan[ch].active) WRITE_VOLUME(ch,(isMuted[ch]?0:(chan[ch].outVol&127)));
484 }
485
486 int DivPlatformLynx::getOutputCount() {
487 return 2;
488 }
489
490 void DivPlatformLynx::forceIns() {
491 for (int i=0; i<4; i++) {
492 if (chan[i].active) {
493 chan[i].insChanged=true;
494 chan[i].freqChanged=true;
495 if (!chan[i].pcm) {
496 WRITE_FEEDBACK(i,chan[i].duty.feedback);
497 }
498 }
499 WRITE_ATTEN(i,chan[i].pan);
500 }
501 }
502
503 void* DivPlatformLynx::getChanState(int ch) {
504 return &chan[ch];
505 }
506
507 DivMacroInt* DivPlatformLynx::getChanMacroInt(int ch) {
508 return &chan[ch].std;
509 }
510
511 unsigned short DivPlatformLynx::getPan(int ch) {
512 return ((chan[ch].pan&0xf0)<<4)|(chan[ch].pan&15);
513 }
514
515 DivSamplePos DivPlatformLynx::getSamplePos(int ch) {
516 if (ch>=4) return DivSamplePos();
517 if (!chan[ch].pcm) return DivSamplePos();
518 return DivSamplePos(
519 chan[ch].sample,
520 chan[ch].samplePos,
521 chan[ch].sampleFreq
522 );
523 }
524
525 DivDispatchOscBuffer* DivPlatformLynx::getOscBuffer(int ch) {
526 return oscBuf[ch];
527 }
528
529 unsigned char* DivPlatformLynx::getRegisterPool()
530 {
531 return const_cast<unsigned char*>( mikey->getRegisterPool() );
532 }
533
534 int DivPlatformLynx::getRegisterPoolSize()
535 {
536 return 4*8+4;
537 }
538
539 void DivPlatformLynx::reset() {
540 mikey=std::make_unique<Lynx::Mikey>(rate);
541
542 for (int i=0; i<4; i++) {
543 chan[i]=DivPlatformLynx::Channel();
544 chan[i].std.setEngine(parent);
545 }
546 writes.clear();
547 if (dumpWrites) {
548 addWrite(0xffffffff,0);
549 }
550 WRITE_STEREO(0);
551 }
552
553 bool DivPlatformLynx::keyOffAffectsArp(int ch) {
554 return true;
555 }
556
557 bool DivPlatformLynx::keyOffAffectsPorta(int ch) {
558 return true;
559 }
560
561 bool DivPlatformLynx::getLegacyAlwaysSetVolume() {
562 return false;
563 }
564
565 //int DivPlatformLynx::getPortaFloor(int ch) {
566 // return 12;
567 //}
568
569 void DivPlatformLynx::setFlags(const DivConfig& flags) {
570 tuned=flags.getBool("tuned",false);
571 chipClock=16000000;
572 CHECK_CUSTOM_CLOCK;
573 rate=chipClock/128;
574 for (int i=0; i<4; i++) {
575 oscBuf[i]->setRate(rate);
576 }
577 }
578
579 void DivPlatformLynx::notifyInsDeletion(void* ins) {
580 for (int i=0; i<4; i++) {
581 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
582 }
583 }
584
585 void DivPlatformLynx::poke(unsigned int addr, unsigned short val) {
586 rWrite(addr,val);
587 }
588
589 void DivPlatformLynx::poke(std::vector<DivRegWrite>& wlist) {
590 for (DivRegWrite& i: wlist) rWrite(i.addr, i.val);
591 }
592
593 int DivPlatformLynx::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
594 parent=p;
595 dumpWrites=false;
596 skipRegisterWrites=false;
597
598 for (int i=0; i<4; i++) {
599 isMuted[i]=false;
600 oscBuf[i]=new DivDispatchOscBuffer;
601 }
602 setFlags(flags);
603
604 reset();
605 return 4;
606 }
607
608 void DivPlatformLynx::quit() {
609 for (int i=0; i<4; i++) {
610 delete oscBuf[i];
611 }
612 mikey.reset();
613 }
614
615 DivPlatformLynx::~DivPlatformLynx() {
616 }
617
618 DivPlatformLynx::MikeyFreqDiv::MikeyFreqDiv(int frequency) {
619
620 int clamped=clamp(frequency, 36, 16383);
621
622 auto top=bsr(clamped);
623
624 if (top>7)
625 {
626 clockDivider=top-7;
627 backup=clamped>>(top-7);
628 }
629 else
630 {
631 clockDivider=0;
632 backup=clamped;
633 }
634 }
635
636 DivPlatformLynx::MikeyDuty::MikeyDuty(int duty) {
637
638 //duty:
639 //9: int
640 //8: f11
641 //7: f10
642 //6: f7
643 //5: f5
644 //4: f4
645 //3: f3
646 //2: f2
647 //1: f1
648 //0: f0
649
650 val=duty;
651 //f7 moved to bit 7 and int moved to bit 5
652 int_feedback7=((duty&0x40)<<1)|((duty&0x200)>>4);
653 //f11 and f10 moved to bits 7 & 6
654 feedback=(duty&0x3f)|((duty&0x180)>>1);
655 }