Debian Codegraph @

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