Debian Codegraph @

src/engine/platform/genesisext.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 "genesisext.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include <math.h>
24
25 #define CHIP_FREQBASE fmFreqBase
26 #define CHIP_DIVIDER fmDivBase
27
28 #define IS_REALLY_MUTED(x) (isMuted[x] && (x<5 || !softPCM || (isMuted[5] && isMuted[6])))
29
30 void DivPlatformGenesisExt::commitStateExt(int ch, DivInstrument* ins) {
31 int ordch=orderedOps[ch];
32
33 if (opChan[ch].insChanged) {
34 chan[extChanOffs].state.alg=ins->fm.alg;
35 if (ch==0 || fbAllOps) {
36 chan[extChanOffs].state.fb=ins->fm.fb;
37 }
38 chan[extChanOffs].state.fms=ins->fm.fms;
39 chan[extChanOffs].state.ams=ins->fm.ams;
40 chan[extChanOffs].state.block=ins->fm.block;
41 chan[extChanOffs].state.op[ordch]=ins->fm.op[ordch];
42 }
43
44 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[ordch];
45 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[ordch];
46 // TODO: how does this work?!
47 if (isOpMuted[ch] || !op.enable) {
48 rWrite(baseAddr+0x40,127);
49 } else {
50 if (opChan[ch].insChanged) {
51 rWrite(baseAddr+0x40,127-VOL_SCALE_LOG_BROKEN(127-op.tl,opChan[ch].outVol&0x7f,127));
52 }
53 }
54 if (opChan[ch].insChanged) {
55 rWrite(baseAddr+0x30,(op.mult&15)|(dtTable[op.dt&7]<<4));
56 rWrite(baseAddr+0x50,(op.ar&31)|(op.rs<<6));
57 rWrite(baseAddr+0x60,(op.dr&31)|(op.am<<7));
58 rWrite(baseAddr+0x70,op.d2r&31);
59 rWrite(baseAddr+0x80,(op.rr&15)|(op.sl<<4));
60 rWrite(baseAddr+0x90,op.ssgEnv&15);
61 opChan[ch].mask=op.enable;
62 }
63 if (opChan[ch].insChanged) { // TODO how does this work?
64 rWrite(chanOffs[extChanOffs]+0xb0,(chan[extChanOffs].state.alg&7)|(chan[extChanOffs].state.fb<<3));
65 rWrite(chanOffs[extChanOffs]+0xb4,(IS_EXTCH_MUTED?0:(opChan[ch].pan<<6))|(chan[extChanOffs].state.fms&7)|((chan[extChanOffs].state.ams&3)<<4));
66 }
67 }
68
69 int DivPlatformGenesisExt::dispatch(DivCommand c) {
70 if (c.chan<2) {
71 return DivPlatformGenesis::dispatch(c);
72 }
73 if (c.chan>5) {
74 c.chan-=3;
75 return DivPlatformGenesis::dispatch(c);
76 }
77 int ch=c.chan-extChanOffs;
78 int ordch=orderedOps[ch];
79 if (!extMode) {
80 c.chan=2;
81 return DivPlatformGenesis::dispatch(c);
82 }
83 switch (c.cmd) {
84 case DIV_CMD_NOTE_ON: {
85 DivInstrument* ins=parent->getIns(opChan[ch].ins,DIV_INS_FM);
86
87 if (noExtMacros) {
88 opChan[ch].macroInit(NULL);
89 } else {
90 opChan[ch].macroInit(ins);
91 }
92 if (!opChan[ch].std.vol.will) {
93 opChan[ch].outVol=opChan[ch].vol;
94 }
95
96 commitStateExt(ch,ins);
97 opChan[ch].insChanged=false;
98
99 if (c.value!=DIV_NOTE_NULL) {
100 opChan[ch].baseFreq=NOTE_FNUM_BLOCK(c.value,11,chan[extChanOffs].state.block);
101 opChan[ch].portaPause=false;
102 opChan[ch].note=c.value;
103 opChan[ch].freqChanged=true;
104 }
105 opChan[ch].keyOn=true;
106 opChan[ch].active=true;
107 break;
108 }
109 case DIV_CMD_NOTE_OFF:
110 opChan[ch].keyOff=true;
111 opChan[ch].keyOn=false;
112 opChan[ch].active=false;
113 break;
114 case DIV_CMD_NOTE_OFF_ENV:
115 if (noExtMacros) break;
116 opChan[ch].keyOff=true;
117 opChan[ch].keyOn=false;
118 opChan[ch].active=false;
119 opChan[ch].std.release();
120 break;
121 case DIV_CMD_ENV_RELEASE:
122 if (noExtMacros) break;
123 opChan[ch].std.release();
124 break;
125 case DIV_CMD_VOLUME: {
126 opChan[ch].vol=c.value;
127 if (!opChan[ch].std.vol.has) {
128 opChan[ch].outVol=c.value;
129 }
130 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[ordch];
131 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[ordch];
132 if (isOpMuted[ch] || !op.enable) {
133 rWrite(baseAddr+0x40,127);
134 } else {
135 rWrite(baseAddr+0x40,127-VOL_SCALE_LOG_BROKEN(127-op.tl,opChan[ch].outVol&0x7f,127));
136 }
137 break;
138 }
139 case DIV_CMD_GET_VOLUME: {
140 return opChan[ch].vol;
141 break;
142 }
143 case DIV_CMD_INSTRUMENT:
144 if (opChan[ch].ins!=c.value || c.value2==1) {
145 opChan[ch].insChanged=true;
146 }
147 opChan[ch].ins=c.value;
148 chan[extChanOffs].ins=opChan[ch].ins;
149 break;
150 case DIV_CMD_PANNING: {
151 if (c.value==0 && c.value2==0) {
152 opChan[ch].pan=3;
153 } else {
154 opChan[ch].pan=(c.value2>0)|((c.value>0)<<1);
155 }
156 if (parent->song.sharedExtStat) {
157 for (int i=0; i<4; i++) {
158 if (ch==i) continue;
159 opChan[i].pan=opChan[ch].pan;
160 }
161 }
162 rWrite(chanOffs[extChanOffs]+0xb4,(IS_EXTCH_MUTED?0:(opChan[ch].pan<<6))|(chan[extChanOffs].state.fms&7)|((chan[extChanOffs].state.ams&3)<<4));
163 lastExtChPan=opChan[ch].pan;
164 break;
165 }
166 case DIV_CMD_PITCH: {
167 opChan[ch].pitch=c.value;
168 opChan[ch].freqChanged=true;
169 break;
170 }
171 case DIV_CMD_NOTE_PORTA: {
172 if (parent->song.linearPitch==2) {
173 int destFreq=NOTE_FREQUENCY(c.value2);
174 bool return2=false;
175 if (destFreq>opChan[ch].baseFreq) {
176 opChan[ch].baseFreq+=c.value;
177 if (opChan[ch].baseFreq>=destFreq) {
178 opChan[ch].baseFreq=destFreq;
179 return2=true;
180 }
181 } else {
182 opChan[ch].baseFreq-=c.value;
183 if (opChan[ch].baseFreq<=destFreq) {
184 opChan[ch].baseFreq=destFreq;
185 return2=true;
186 }
187 }
188 opChan[ch].freqChanged=true;
189 if (return2) {
190 //opChan[ch].inPorta=false;
191 return 2;
192 }
193 break;
194 }
195 PLEASE_HELP_ME(opChan[ch],chan[extChanOffs].state.block);
196 break;
197 }
198 case DIV_CMD_SAMPLE_MODE: {
199 // not ignored actually!
200 if (!parent->song.ignoreDACModeOutsideIntendedChannel) {
201 chan[5].dacMode=c.value;
202 rWrite(0x2b,c.value<<7);
203 }
204 break;
205 }
206 case DIV_CMD_SAMPLE_BANK:
207 if (!parent->song.ignoreDACModeOutsideIntendedChannel) {
208 chan[5].sampleBank=c.value;
209 if (chan[5].sampleBank>(parent->song.sample.size()/12)) {
210 chan[5].sampleBank=parent->song.sample.size()/12;
211 }
212 }
213 break;
214 case DIV_CMD_LEGATO: {
215 if (opChan[ch].insChanged) {
216 DivInstrument* ins=parent->getIns(opChan[ch].ins,DIV_INS_FM);
217 commitStateExt(ch,ins);
218 opChan[ch].insChanged=false;
219 }
220 opChan[ch].baseFreq=NOTE_FNUM_BLOCK(c.value,11,chan[extChanOffs].state.block);
221 opChan[ch].freqChanged=true;
222 break;
223 }
224 case DIV_CMD_FM_EXTCH: {
225 if (extMode==(bool)c.value) break;
226 extMode=c.value;
227 immWrite(0x27,extMode?0x40:0);
228 if (!extMode) {
229 for (int i=0; i<4; i++) {
230 opChan[i].insChanged=true;
231 }
232 chan[extChanOffs].insChanged=true;
233 }
234 break;
235 }
236 case DIV_CMD_FM_LFO: {
237 lfoValue=(c.value&7)|((c.value>>4)<<3);
238 rWrite(0x22,lfoValue);
239 break;
240 }
241 case DIV_CMD_FM_ALG: {
242 chan[extChanOffs].state.alg=c.value&7;
243 // TODO: TL compensation?
244 rWrite(ADDR_FB_ALG+chanOffs[extChanOffs],(chan[extChanOffs].state.alg&7)|(chan[extChanOffs].state.fb<<3));
245 break;
246 }
247 case DIV_CMD_FM_FB: {
248 chan[extChanOffs].state.fb=c.value&7;
249 rWrite(chanOffs[extChanOffs]+ADDR_FB_ALG,(chan[extChanOffs].state.alg&7)|(chan[extChanOffs].state.fb<<3));
250 break;
251 }
252 case DIV_CMD_FM_FMS: {
253 chan[extChanOffs].state.fms=c.value&7;
254 rWrite(chanOffs[extChanOffs]+0xb4,(IS_EXTCH_MUTED?0:(opChan[ch].pan<<6))|(chan[extChanOffs].state.fms&7)|((chan[extChanOffs].state.ams&3)<<4));
255 break;
256 }
257 case DIV_CMD_FM_AMS: {
258 chan[extChanOffs].state.ams=c.value&3;
259 rWrite(chanOffs[extChanOffs]+0xb4,(IS_EXTCH_MUTED?0:(opChan[ch].pan<<6))|(chan[extChanOffs].state.fms&7)|((chan[extChanOffs].state.ams&3)<<4));
260 break;
261 }
262 case DIV_CMD_FM_MULT: {
263 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[c.value]];
264 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[c.value]];
265 op.mult=c.value2&15;
266 rWrite(baseAddr+0x30,(op.mult&15)|(dtTable[op.dt&7]<<4));
267 break;
268 }
269 case DIV_CMD_FM_TL: {
270 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[c.value]];
271 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[c.value]];
272 op.tl=c.value2;
273 if (isOpMuted[ch] || !op.enable) {
274 rWrite(baseAddr+0x40,127);
275 } else if (KVS(2,c.value)) {
276 rWrite(baseAddr+0x40,127-VOL_SCALE_LOG_BROKEN(127-op.tl,opChan[ch].outVol&0x7f,127));
277 } else {
278 rWrite(baseAddr+0x40,op.tl);
279 }
280 break;
281 }
282 case DIV_CMD_FM_AR: {
283 if (c.value<0) {
284 for (int i=0; i<4; i++) {
285 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[i];
286 op.ar=c.value2&31;
287 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[i];
288 rWrite(baseAddr+0x50,(op.ar&31)|(op.rs<<6));
289 }
290 } else {
291 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[c.value]];
292 op.ar=c.value2&31;
293 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[c.value]];
294 rWrite(baseAddr+0x50,(op.ar&31)|(op.rs<<6));
295 }
296 break;
297 }
298 case DIV_CMD_FM_RS: {
299 if (c.value<0) {
300 for (int i=0; i<4; i++) {
301 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[i];
302 op.rs=c.value2&3;
303 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[i];
304 rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6));
305 }
306 } else if (c.value<4) {
307 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[c.value]];
308 op.rs=c.value2&3;
309 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[c.value]];
310 rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6));
311 }
312 break;
313 }
314 case DIV_CMD_FM_AM: {
315 if (c.value<0) {
316 for (int i=0; i<4; i++) {
317 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[i];
318 op.am=c.value2&1;
319 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[i];
320 rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7));
321 }
322 } else if (c.value<4) {
323 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[c.value]];
324 op.am=c.value2&1;
325 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[c.value]];
326 rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7));
327 }
328 break;
329 }
330 case DIV_CMD_FM_DR: {
331 if (c.value<0) {
332 for (int i=0; i<4; i++) {
333 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[i];
334 op.dr=c.value2&31;
335 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[i];
336 rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7));
337 }
338 } else if (c.value<4) {
339 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[c.value]];
340 op.dr=c.value2&31;
341 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[c.value]];
342 rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7));
343 }
344 break;
345 }
346 case DIV_CMD_FM_SL: {
347 if (c.value<0) {
348 for (int i=0; i<4; i++) {
349 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[i];
350 op.sl=c.value2&15;
351 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[i];
352 rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4));
353 }
354 } else if (c.value<4) {
355 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[c.value]];
356 op.sl=c.value2&15;
357 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[c.value]];
358 rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4));
359 }
360 break;
361 }
362 case DIV_CMD_FM_RR: {
363 if (c.value<0) {
364 for (int i=0; i<4; i++) {
365 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[i];
366 op.rr=c.value2&15;
367 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[i];
368 rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4));
369 }
370 } else if (c.value<4) {
371 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[c.value]];
372 op.rr=c.value2&15;
373 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[c.value]];
374 rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4));
375 }
376 break;
377 }
378 case DIV_CMD_FM_D2R: {
379 if (c.value<0) {
380 for (int i=0; i<4; i++) {
381 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[i];
382 op.d2r=c.value2&31;
383 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[i];
384 rWrite(baseAddr+ADDR_DT2_D2R,op.d2r&31);
385 }
386 } else if (c.value<4) {
387 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[c.value]];
388 op.d2r=c.value2&31;
389 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[c.value]];
390 rWrite(baseAddr+ADDR_DT2_D2R,op.d2r&31);
391 }
392 break;
393 }
394 case DIV_CMD_FM_DT: {
395 if (c.value<0) {
396 for (int i=0; i<4; i++) {
397 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[i];
398 op.dt=c.value&7;
399 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[i];
400 rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4));
401 }
402 } else if (c.value<4) {
403 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[c.value]];
404 op.dt=c.value2&7;
405 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[c.value]];
406 rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4));
407 }
408 break;
409 }
410 case DIV_CMD_FM_SSG: {
411 if (c.value<0) {
412 for (int i=0; i<4; i++) {
413 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[i];
414 op.ssgEnv=8^(c.value2&15);
415 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[i];
416 rWrite(baseAddr+ADDR_SSG,op.ssgEnv&15);
417 }
418 } else if (c.value<4) {
419 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[c.value]];
420 op.ssgEnv=8^(c.value2&15);
421 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[c.value]];
422 rWrite(baseAddr+ADDR_SSG,op.ssgEnv&15);
423 }
424 break;
425 }
426 case DIV_CMD_FM_HARD_RESET:
427 opChan[ch].hardReset=c.value;
428 break;
429 case DIV_CMD_GET_VOLMAX:
430 return 127;
431 break;
432 case DIV_CMD_MACRO_OFF:
433 opChan[ch].std.mask(c.value,true);
434 break;
435 case DIV_CMD_MACRO_ON:
436 opChan[ch].std.mask(c.value,false);
437 break;
438 case DIV_CMD_PRE_PORTA:
439 break;
440 default:
441 //printf("WARNING: unimplemented command %d\n",c.cmd);
442 break;
443 }
444 return 1;
445 }
446
447 void DivPlatformGenesisExt::muteChannel(int ch, bool mute) {
448 if (ch<2) {
449 DivPlatformGenesis::muteChannel(ch,mute);
450 return;
451 }
452 if (ch>5) {
453 DivPlatformGenesis::muteChannel(ch-3,mute);
454 return;
455 }
456 isOpMuted[ch-2]=mute;
457
458 DivPlatformGenesis::muteChannel(extChanOffs,IS_EXTCH_MUTED);
459
460 if (extMode) {
461 for (int i=0; i<4; i++) {
462 int ordch=orderedOps[i];
463 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[ordch];
464 DivInstrumentFM::Operator op=chan[extChanOffs].state.op[ordch];
465 if (isOpMuted[i] || !op.enable) {
466 rWrite(baseAddr+0x40,127);
467 } else {
468 rWrite(baseAddr+0x40,127-VOL_SCALE_LOG_BROKEN(127-op.tl,opChan[i].outVol&0x7f,127));
469 }
470 }
471
472 rWrite(chanOffs[extChanOffs]+0xb4,(IS_EXTCH_MUTED?0:(opChan[ch-2].pan<<6))|(chan[extChanOffs].state.fms&7)|((chan[extChanOffs].state.ams&3)<<4));
473 }
474 }
475
476 static int opChanOffsL[4]={
477 0xa9, 0xaa, 0xa8, 0xa2
478 };
479
480 static int opChanOffsH[4]={
481 0xad, 0xae, 0xac, 0xa6
482 };
483
484 void DivPlatformGenesisExt::tick(bool sysTick) {
485 int hardResetElapsed=0;
486 bool mustHardReset=false;
487
488 if (extMode && !noExtMacros) for (int i=0; i<4; i++) {
489 opChan[i].std.next();
490
491 if (opChan[i].std.phaseReset.had) {
492 if (opChan[i].std.phaseReset.val==1 && opChan[i].active) {
493 opChan[i].keyOn=true;
494 }
495 }
496 }
497
498 if (extMode) {
499 bool writeSomething=false;
500 unsigned char writeMask=2;
501 for (int i=0; i<4; i++) {
502 writeMask|=(unsigned char)(opChan[i].mask && opChan[i].active)<<(4+i);
503 if (opChan[i].keyOn || opChan[i].keyOff) {
504 writeSomething=true;
505 writeMask&=~(1<<(4+i));
506 opChan[i].keyOff=false;
507 }
508 if (opChan[i].hardReset && opChan[i].keyOn) {
509 mustHardReset=true;
510 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[i];
511 immWrite(baseAddr+ADDR_SL_RR,0x0f);
512 hardResetElapsed++;
513 }
514 }
515 if (writeSomething) {
516 if (chan[csmChan].active) { // CSM
517 writeMask^=0xf0;
518 }
519 immWrite(0x28,writeMask);
520 }
521 }
522
523 if (extMode && !noExtMacros) for (int i=0; i<4; i++) {
524 if (opChan[i].std.vol.had) {
525 opChan[i].outVol=VOL_SCALE_LOG_BROKEN(opChan[i].vol,MIN(127,opChan[i].std.vol.val),127);
526 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[i]];
527 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[i]];
528 if (isOpMuted[i]) {
529 rWrite(baseAddr+ADDR_TL,127);
530 } else {
531 rWrite(baseAddr+ADDR_TL,127-VOL_SCALE_LOG_BROKEN(127-op.tl,opChan[i].outVol&0x7f,127));
532 }
533 }
534
535 if (NEW_ARP_STRAT) {
536 opChan[i].handleArp();
537 } else if (opChan[i].std.arp.had) {
538 if (!opChan[i].inPorta) {
539 opChan[i].baseFreq=NOTE_FNUM_BLOCK(parent->calcArp(opChan[i].note,opChan[i].std.arp.val),11,chan[extChanOffs].state.block);
540 }
541 opChan[i].freqChanged=true;
542 }
543
544 if (opChan[i].std.pitch.had) {
545 if (opChan[i].std.pitch.mode) {
546 opChan[i].pitch2+=opChan[i].std.pitch.val;
547 CLAMP_VAR(opChan[i].pitch2,-1048576,1048575);
548 } else {
549 opChan[i].pitch2=opChan[i].std.pitch.val;
550 }
551 opChan[i].freqChanged=true;
552 }
553
554 // channel macros
555 if (opChan[i].std.alg.had) {
556 chan[extChanOffs].state.alg=opChan[i].std.alg.val;
557 rWrite(chanOffs[extChanOffs]+ADDR_FB_ALG,(chan[extChanOffs].state.alg&7)|(chan[extChanOffs].state.fb<<3));
558 if (!parent->song.algMacroBehavior) for (int j=0; j<4; j++) {
559 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[j];
560 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[j];
561 if (isOpMuted[j] || !op.enable) {
562 rWrite(baseAddr+0x40,127);
563 } else {
564 rWrite(baseAddr+0x40,127-VOL_SCALE_LOG_BROKEN(127-op.tl,opChan[j].outVol&0x7f,127));
565 }
566 }
567 }
568 if (i==0 || fbAllOps) {
569 if (opChan[i].std.fb.had) {
570 chan[extChanOffs].state.fb=opChan[i].std.fb.val;
571 rWrite(chanOffs[extChanOffs]+ADDR_FB_ALG,(chan[extChanOffs].state.alg&7)|(chan[extChanOffs].state.fb<<3));
572 }
573 }
574 if (opChan[i].std.fms.had) {
575 chan[extChanOffs].state.fms=opChan[i].std.fms.val;
576 rWrite(chanOffs[extChanOffs]+ADDR_LRAF,(IS_EXTCH_MUTED?0:(opChan[i].pan<<6))|(chan[extChanOffs].state.fms&7)|((chan[extChanOffs].state.ams&3)<<4));
577 }
578 if (opChan[i].std.ams.had) {
579 chan[extChanOffs].state.ams=opChan[i].std.ams.val;
580 rWrite(chanOffs[extChanOffs]+ADDR_LRAF,(IS_EXTCH_MUTED?0:(opChan[i].pan<<6))|(chan[extChanOffs].state.fms&7)|((chan[extChanOffs].state.ams&3)<<4));
581 }
582 if (opChan[i].std.ex3.had) {
583 lfoValue=(opChan[i].std.ex3.val>7)?0:(8|(opChan[i].std.ex3.val&7));
584 rWrite(0x22,lfoValue);
585 }
586
587 if (opChan[i].std.panL.had) {
588 opChan[i].pan=opChan[i].std.panL.val&3;
589 if (parent->song.sharedExtStat) {
590 for (int j=0; j<4; j++) {
591 if (i==j) continue;
592 opChan[j].pan=opChan[i].pan;
593 }
594 }
595 rWrite(chanOffs[extChanOffs]+ADDR_LRAF,(IS_EXTCH_MUTED?0:(opChan[i].pan<<6))|(chan[extChanOffs].state.fms&7)|((chan[extChanOffs].state.ams&3)<<4));
596 }
597
598 // param macros
599 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[orderedOps[i]];
600 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[orderedOps[i]];
601 DivMacroInt::IntOp& m=opChan[i].std.op[orderedOps[i]];
602 if (m.am.had) {
603 op.am=m.am.val;
604 rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7));
605 }
606 if (m.ar.had) {
607 op.ar=m.ar.val;
608 rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6));
609 }
610 if (m.dr.had) {
611 op.dr=m.dr.val;
612 rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7));
613 }
614 if (m.mult.had) {
615 op.mult=m.mult.val;
616 rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4));
617 }
618 if (m.rr.had) {
619 op.rr=m.rr.val;
620 rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4));
621 }
622 if (m.sl.had) {
623 op.sl=m.sl.val;
624 rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4));
625 }
626 if (m.tl.had) {
627 op.tl=m.tl.val;
628 if (isOpMuted[i]) {
629 rWrite(baseAddr+ADDR_TL,127);
630 } else {
631 rWrite(baseAddr+ADDR_TL,127-VOL_SCALE_LOG_BROKEN(127-op.tl,opChan[i].outVol&0x7f,127));
632 }
633 }
634 if (m.rs.had) {
635 op.rs=m.rs.val;
636 rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6));
637 }
638 if (m.dt.had) {
639 op.dt=m.dt.val;
640 rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4));
641 }
642 if (m.d2r.had) {
643 op.d2r=m.d2r.val;
644 rWrite(baseAddr+ADDR_DT2_D2R,op.d2r&31);
645 }
646 if (m.ssg.had) {
647 op.ssgEnv=m.ssg.val;
648 rWrite(baseAddr+ADDR_SSG,op.ssgEnv&15);
649 }
650 }
651
652 DivPlatformGenesis::tick(sysTick);
653
654 bool writeNoteOn=false;
655 unsigned char writeMask=2;
656 unsigned char hardResetMask=0;
657 if (extMode) for (int i=0; i<4; i++) {
658 if (opChan[i].freqChanged) {
659 if (parent->song.linearPitch==2) {
660 opChan[i].freq=parent->calcFreq(opChan[i].baseFreq,opChan[i].pitch,opChan[i].fixedArp?opChan[i].baseNoteOverride:opChan[i].arpOff,opChan[i].fixedArp,false,2,opChan[i].pitch2,chipClock,CHIP_FREQBASE,11,chan[extChanOffs].state.block);
661 } else {
662 int fNum=parent->calcFreq(opChan[i].baseFreq&0x7ff,opChan[i].pitch,opChan[i].fixedArp?opChan[i].baseNoteOverride:opChan[i].arpOff,opChan[i].fixedArp,false,2,opChan[i].pitch2);
663 int block=(opChan[i].baseFreq&0xf800)>>11;
664 if (fNum<0) fNum=0;
665 if (fNum>2047) {
666 while (block<7) {
667 fNum>>=1;
668 block++;
669 }
670 if (fNum>2047) fNum=2047;
671 }
672 opChan[i].freq=(block<<11)|fNum;
673 }
674 if (opChan[i].freq>0x3fff) opChan[i].freq=0x3fff;
675 immWrite(opChanOffsH[i],opChan[i].freq>>8);
676 immWrite(opChanOffsL[i],opChan[i].freq&0xff);
677 opChan[i].freqChanged=false;
678 }
679 writeMask|=(unsigned char)(opChan[i].mask && opChan[i].active)<<(4+i);
680 if (opChan[i].keyOn) {
681 writeNoteOn=true;
682 if (opChan[i].mask) {
683 writeMask|=1<<(4+i);
684 if (opChan[i].hardReset) {
685 hardResetMask|=1<<(4+i);
686 }
687 }
688 if (!opChan[i].hardReset) {
689 opChan[i].keyOn=false;
690 }
691 }
692 }
693
694 if (extMode) {
695 if (chan[csmChan].freqChanged) {
696 chan[csmChan].freq=parent->calcFreq(chan[csmChan].baseFreq,chan[csmChan].pitch,chan[csmChan].fixedArp?chan[csmChan].baseNoteOverride:chan[csmChan].arpOff,chan[csmChan].fixedArp,true,0,chan[csmChan].pitch2,chipClock,CHIP_DIVIDER);
697 if (chan[csmChan].freq<1) chan[csmChan].freq=1;
698 if (chan[csmChan].freq>1024) chan[csmChan].freq=1024;
699 int wf=0x400-chan[csmChan].freq;
700 immWrite(0x24,wf>>2);
701 immWrite(0x25,wf&3);
702 chan[csmChan].freqChanged=false;
703 }
704
705 if (chan[csmChan].keyOff || chan[csmChan].keyOn) {
706 writeNoteOn=true;
707 for (int i=0; i<4; i++) {
708 writeMask|=opChan[i].active<<(4+i);
709 }
710 }
711 }
712
713 if (writeNoteOn) {
714 if (chan[csmChan].active) { // CSM
715 writeMask^=0xf0;
716 }
717 writeMask^=hardResetMask;
718 immWrite(0x28,writeMask);
719 writeMask^=hardResetMask;
720
721 // hard reset handling
722 if (mustHardReset) {
723 immWrite(0xfffffffe,hardResetCycles-hardResetElapsed);
724 for (int i=0; i<4; i++) {
725 if (opChan[i].keyOn && opChan[i].hardReset) {
726 // restore SL/RR
727 unsigned short baseAddr=chanOffs[extChanOffs]|opOffs[i];
728 DivInstrumentFM::Operator& op=chan[extChanOffs].state.op[i];
729 immWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4));
730 opChan[i].keyOn=false;
731 }
732 }
733 immWrite(0x28,writeMask);
734 }
735 }
736
737 if (extMode) {
738 if (chan[csmChan].keyOn) {
739 immWrite(0x27,0x81);
740 chan[csmChan].keyOn=false;
741 }
742 if (chan[csmChan].keyOff) {
743 immWrite(0x27,0x40);
744 chan[csmChan].keyOff=false;
745 }
746 }
747 }
748
749 void DivPlatformGenesisExt::forceIns() {
750 for (int i=0; i<6; i++) {
751 for (int j=0; j<4; j++) {
752 unsigned short baseAddr=chanOffs[i]|opOffs[j];
753 DivInstrumentFM::Operator& op=chan[i].state.op[j];
754 if (i==2 && extMode) { // extended channel
755 if (isOpMuted[orderedOps[j]] || !op.enable) {
756 rWrite(baseAddr+0x40,127);
757 } else {
758 rWrite(baseAddr+0x40,127-VOL_SCALE_LOG_BROKEN(127-op.tl,opChan[orderedOps[j]].outVol&0x7f,127));
759 }
760 } else {
761 if (isMuted[i]) {
762 rWrite(baseAddr+ADDR_TL,127);
763 } else {
764 if (KVS(i,j)) {
765 rWrite(baseAddr+ADDR_TL,127-VOL_SCALE_LOG_BROKEN(127-op.tl,chan[i].outVol&0x7f,127));
766 } else {
767 rWrite(baseAddr+ADDR_TL,op.tl);
768 }
769 }
770 }
771 rWrite(baseAddr+ADDR_MULT_DT,(op.mult&15)|(dtTable[op.dt&7]<<4));
772 rWrite(baseAddr+ADDR_RS_AR,(op.ar&31)|(op.rs<<6));
773 rWrite(baseAddr+ADDR_AM_DR,(op.dr&31)|(op.am<<7));
774 rWrite(baseAddr+ADDR_DT2_D2R,op.d2r&31);
775 rWrite(baseAddr+ADDR_SL_RR,(op.rr&15)|(op.sl<<4));
776 rWrite(baseAddr+ADDR_SSG,op.ssgEnv&15);
777 }
778 rWrite(chanOffs[i]+ADDR_FB_ALG,(chan[i].state.alg&7)|(chan[i].state.fb<<3));
779 if (i==2) {
780 rWrite(chanOffs[i]+ADDR_LRAF,(IS_EXTCH_MUTED?0:(lastExtChPan<<6))|(chan[i].state.fms&7)|((chan[i].state.ams&3)<<4));
781 } else {
782 rWrite(chanOffs[i]+ADDR_LRAF,(IS_REALLY_MUTED(i)?0:(chan[i].pan<<6))|(chan[i].state.fms&7)|((chan[i].state.ams&3)<<4));
783 }
784 if (chan[i].active) {
785 chan[i].keyOn=true;
786 chan[i].freqChanged=true;
787 }
788 }
789 if (chan[5].dacMode) {
790 chan[5].dacSample=-1;
791 chan[6].dacSample=-1;
792 rWrite(0x2b,0x80);
793 }
794 immWrite(0x22,lfoValue);
795 for (int i=0; i<4; i++) {
796 opChan[i].insChanged=true;
797 if (opChan[i].active) {
798 opChan[i].keyOn=true;
799 opChan[i].freqChanged=true;
800 }
801 }
802 if (extMode && chan[csmChan].active) { // CSM
803 chan[csmChan].insChanged=true;
804 chan[csmChan].freqChanged=true;
805 chan[csmChan].keyOn=true;
806 }
807 if (!extMode) {
808 immWrite(0x27,0x00);
809 }
810 }
811
812 void* DivPlatformGenesisExt::getChanState(int ch) {
813 if (ch>=6) return &chan[ch-3];
814 if (ch>=2) return &opChan[ch-2];
815 return &chan[ch];
816 }
817
818 DivMacroInt* DivPlatformGenesisExt::getChanMacroInt(int ch) {
819 if (ch>=6) return &chan[ch-3].std;
820 if (ch>=2) return &opChan[ch-2].std;
821 return &chan[ch].std;
822 }
823
824 unsigned short DivPlatformGenesisExt::getPan(int ch) {
825 if (ch==4+csmChan) return 0;
826 if (ch>=4+extChanOffs) return DivPlatformGenesis::getPan(ch-3);
827 if (ch>=extChanOffs) {
828 if (extMode) {
829 return ((lastExtChPan&2)<<7)|(lastExtChPan&1);
830 } else {
831 return DivPlatformGenesis::getPan(extChanOffs);
832 }
833 }
834 return DivPlatformGenesis::getPan(ch);
835 }
836
837 DivDispatchOscBuffer* DivPlatformGenesisExt::getOscBuffer(int ch) {
838 if (ch>=6) return oscBuf[ch-3];
839 if (ch<3) return oscBuf[ch];
840 return NULL;
841 }
842
843 int DivPlatformGenesisExt::mapVelocity(int ch, float vel) {
844 if (ch>=extChanOffs+4) return DivPlatformGenesis::mapVelocity(ch-3,vel);
845 if (ch>=extChanOffs) return DivPlatformGenesis::mapVelocity(extChanOffs,vel);
846 return DivPlatformGenesis::mapVelocity(ch,vel);
847 }
848
849 void DivPlatformGenesisExt::reset() {
850 DivPlatformGenesis::reset();
851
852 for (int i=0; i<4; i++) {
853 opChan[i]=DivPlatformOPN::OPNOpChannelStereo();
854 opChan[i].std.setEngine(parent);
855 opChan[i].vol=127;
856 opChan[i].outVol=127;
857 }
858
859 lastExtChPan=3;
860
861 // channel 3 mode
862 immWrite(0x27,0x40);
863 extMode=true;
864 }
865
866 bool DivPlatformGenesisExt::keyOffAffectsArp(int ch) {
867 return (ch>8);
868 }
869
870 bool DivPlatformGenesisExt::keyOffAffectsPorta(int ch) {
871 return (ch>8);
872 }
873
874 void DivPlatformGenesisExt::notifyInsChange(int ins) {
875 DivPlatformGenesis::notifyInsChange(ins);
876 for (int i=0; i<4; i++) {
877 if (opChan[i].ins==ins) {
878 opChan[i].insChanged=true;
879 }
880 }
881 }
882
883 void DivPlatformGenesisExt::notifyInsDeletion(void* ins) {
884 DivPlatformGenesis::notifyInsDeletion(ins);
885 for (int i=0; i<4; i++) {
886 opChan[i].std.notifyInsDeletion((DivInstrument*)ins);
887 }
888 }
889
890 int DivPlatformGenesisExt::getPortaFloor(int ch) {
891 return (ch>8)?12:0;
892 }
893
894 void DivPlatformGenesisExt::setCSMChannel(unsigned char ch) {
895 csmChan=ch;
896 }
897
898 int DivPlatformGenesisExt::init(DivEngine* parent, int channels, int sugRate, const DivConfig& flags) {
899 DivPlatformGenesis::init(parent,channels,sugRate,flags);
900 for (int i=0; i<4; i++) {
901 isOpMuted[i]=false;
902 }
903 extSys=true;
904
905 reset();
906 return 13;
907 }
908
909 void DivPlatformGenesisExt::quit() {
910 DivPlatformGenesis::quit();
911 }
912
913 DivPlatformGenesisExt::~DivPlatformGenesisExt() {
914 }