Debian Codegraph @

src/engine/platform/opll.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 "opll.h"
21 #include "../engine.h"
22 #include "../bsr.h"
23 #include "../../ta-log.h"
24 #include <string.h>
25 #include <math.h>
26
27 #define rWrite(a,v) if (!skipRegisterWrites) {pendingWrites[a]=v;}
28 #define immWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
29
30 #define CHIP_FREQBASE 1180068
31
32 #define DRUM_VOL(_x) (drumActivated[_x]?drumVol[_x]:15)
33
34 const unsigned char cycleMapOPLL[18]={
35 8, 7, 6, 7, 8, 7, 8, 6, 0, 1, 2, 7, 8, 9, 3, 4, 5, 9
36 };
37
38 const unsigned char drumSlot[11]={
39 0, 0, 0, 0, 0, 0, 6, 7, 8, 8, 7
40 };
41
42 const unsigned char visMapOPLL[9]={
43 6, 7, 8, 3, 4, 5, 0, 1, 2
44 };
45
46 void DivPlatformOPLL::acquire_nuked(short** buf, size_t len) {
47 thread_local int o[2];
48 thread_local int os;
49
50 for (int i=0; i<11; i++) {
51 oscBuf[i]->begin(len);
52 }
53
54 for (size_t h=0; h<len; h++) {
55 os=0;
56 for (int i=0; i<9; i++) {
57 if (!writes.empty() && --delay<0) {
58 // 84 is safe value
59 QueuedWrite& w=writes.front();
60 if (w.addrOrVal) {
61 OPLL_Write(&fm,1,w.val);
62 //logV("write: %x = %.2x",w.addr,w.val);
63 regPool[w.addr&0xff]=w.val;
64 writes.pop();
65 delay=21;
66 } else {
67 //printf("busycounter: %d\n",lastBusy);
68 OPLL_Write(&fm,0,w.addr);
69 w.addrOrVal=true;
70 delay=3;
71 }
72 }
73
74 OPLL_Clock(&fm,o);
75 unsigned char nextOut=cycleMapOPLL[fm.cycles];
76 if ((nextOut>=6 && properDrums) || !isMuted[nextOut]) {
77 os+=(o[0]+o[1]);
78 if (vrc7 || (fm.rm_enable&0x20)) oscBuf[nextOut]->putSample(h,(o[0]+o[1])<<6);
79 } else {
80 if (vrc7 || (fm.rm_enable&0x20)) oscBuf[nextOut]->putSample(h,0);
81 }
82 }
83 if (!(vrc7 || (fm.rm_enable&0x20))) for (int i=0; i<9; i++) {
84 unsigned char ch=visMapOPLL[i];
85 if ((i>=6 && properDrums) || !isMuted[ch]) {
86 oscBuf[ch]->putSample(h,(fm.output_ch[i])<<6);
87 } else {
88 oscBuf[ch]->putSample(h,0);
89 }
90 }
91 os*=50;
92 if (os<-32768) os=-32768;
93 if (os>32767) os=32767;
94 buf[0][h]=os;
95 }
96
97 for (int i=0; i<11; i++) {
98 oscBuf[i]->end(len);
99 }
100 }
101
102 void DivPlatformOPLL::acquire_ymfm(short** buf, size_t len) {
103 }
104
105 static const unsigned char freakingDrumMap[5]={
106 9, 11, 12, 13, 10
107 };
108
109 void DivPlatformOPLL::acquire_emu(short** buf, size_t len) {
110 thread_local int os;
111
112 for (int i=0; i<11; i++) {
113 oscBuf[i]->begin(len);
114 }
115
116 for (size_t h=0; h<len; h++) {
117 if (!writes.empty()) {
118 QueuedWrite& w=writes.front();
119 OPLL_writeReg(fm_emu,w.addr,w.val);
120 writes.pop();
121 }
122 os=-OPLL_calc(fm_emu);
123 os=os+(os<<1);
124 if (os<-32768) os=-32768;
125 if (os>32767) os=32767;
126
127 buf[0][h]=os;
128
129 for (int i=0; i<11; i++) {
130 if (i>=6 && properDrums) {
131 oscBuf[i]->putSample(h,(-fm_emu->ch_out[freakingDrumMap[i-6]])<<3);
132 } else {
133 oscBuf[i]->putSample(h,(-fm_emu->ch_out[i])<<3);
134 }
135 }
136 }
137
138 for (int i=0; i<11; i++) {
139 oscBuf[i]->end(len);
140 }
141 }
142
143 void DivPlatformOPLL::acquire(short** buf, size_t len) {
144 switch (selCore) {
145 case 0:
146 acquire_nuked(buf,len);
147 break;
148 case 1:
149 acquire_emu(buf,len);
150 break;
151 }
152 }
153
154 void DivPlatformOPLL::tick(bool sysTick) {
155 for (int i=0; i<11; i++) {
156 chan[i].std.next();
157
158 if (chan[i].std.vol.had) {
159 chan[i].outVol=VOL_SCALE_LOG_BROKEN(chan[i].vol,MIN(15,chan[i].std.vol.val),15);
160
161 if (i>=6 && properDrums) {
162 drumVol[i-6]=15-chan[i].outVol;
163 rWrite(0x36,DRUM_VOL(0));
164 rWrite(0x37,DRUM_VOL(1)|(DRUM_VOL(4)<<4));
165 rWrite(0x38,DRUM_VOL(3)|(DRUM_VOL(2)<<4));
166 } else if (i<6 || !crapDrums) {
167 if (i<9) {
168 rWrite(0x30+i,((15-VOL_SCALE_LOG_BROKEN(chan[i].outVol,15-chan[i].state.op[1].tl,15))&15)|(chan[i].state.opllPreset<<4));
169 }
170 }
171 }
172
173 if (NEW_ARP_STRAT) {
174 chan[i].handleArp();
175 } else if (chan[i].std.arp.had) {
176 if (!chan[i].inPorta) {
177 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
178 }
179 chan[i].freqChanged=true;
180 }
181
182 if (chan[i].std.wave.had && chan[i].state.opllPreset!=16) {
183 chan[i].state.opllPreset=chan[i].std.wave.val;
184 if (i<9) {
185 rWrite(0x30+i,((15-VOL_SCALE_LOG_BROKEN(chan[i].outVol,15-chan[i].state.op[1].tl,15))&15)|(chan[i].state.opllPreset<<4));
186 }
187 }
188
189 if (chan[i].std.pitch.had) {
190 if (chan[i].std.pitch.mode) {
191 chan[i].pitch2+=chan[i].std.pitch.val;
192 CLAMP_VAR(chan[i].pitch2,-65535,65535);
193 } else {
194 chan[i].pitch2=chan[i].std.pitch.val;
195 }
196 chan[i].freqChanged=true;
197 }
198
199 if (chan[i].std.phaseReset.had) {
200 if (chan[i].std.phaseReset.val==1 && chan[i].active) {
201 chan[i].keyOn=true;
202 }
203 }
204
205 if (chan[i].std.alg.had) { // SUS
206 chan[i].state.alg=chan[i].std.alg.val;
207 if (chan[i].state.opllPreset==0) {
208 chan[i].freqChanged=true;
209 }
210 }
211 if (chan[i].std.fb.had) {
212 chan[i].state.fb=chan[i].std.fb.val;
213 if (chan[i].state.opllPreset==0) {
214 rWrite(0x03,(chan[i].state.op[1].ksl<<6)|((chan[i].state.fms&1)<<4)|((chan[i].state.ams&1)<<3)|chan[i].state.fb);
215 }
216 }
217 if (chan[i].std.fms.had) {
218 chan[i].state.fms=chan[i].std.fms.val;
219 if (chan[i].state.opllPreset==0) {
220 rWrite(0x03,(chan[i].state.op[1].ksl<<6)|((chan[i].state.fms&1)<<4)|((chan[i].state.ams&1)<<3)|chan[i].state.fb);
221 }
222 }
223 if (chan[i].std.ams.had) {
224 chan[i].state.ams=chan[i].std.ams.val;
225 if (chan[i].state.opllPreset==0) {
226 rWrite(0x03,(chan[i].state.op[1].ksl<<6)|((chan[i].state.fms&1)<<4)|((chan[i].state.ams&1)<<3)|chan[i].state.fb);
227 }
228 }
229
230 for (int j=0; j<2; j++) {
231 DivInstrumentFM::Operator& op=chan[i].state.op[j];
232 DivMacroInt::IntOp& m=chan[i].std.op[j];
233
234 if (m.am.had) {
235 op.am=m.am.val;
236 if (chan[i].state.opllPreset==0) {
237 rWrite(0x00+j,(op.am<<7)|(op.vib<<6)|((op.ssgEnv&8)<<2)|(op.ksr<<4)|(op.mult));
238 }
239 }
240 if (m.ar.had) {
241 op.ar=m.ar.val;
242 if (chan[i].state.opllPreset==0) {
243 rWrite(0x04+j,(op.ar<<4)|(op.dr));
244 }
245 }
246 if (m.dr.had) {
247 op.dr=m.dr.val;
248 if (chan[i].state.opllPreset==0) {
249 rWrite(0x04+j,(op.ar<<4)|(op.dr));
250 }
251 }
252 if (m.mult.had) {
253 op.mult=m.mult.val;
254 if (chan[i].state.opllPreset==0) {
255 rWrite(0x00+j,(op.am<<7)|(op.vib<<6)|((op.ssgEnv&8)<<2)|(op.ksr<<4)|(op.mult));
256 }
257 }
258 if (m.rr.had) {
259 op.rr=m.rr.val;
260 if (chan[i].state.opllPreset==0) {
261 rWrite(0x06+j,(op.sl<<4)|(op.rr));
262 }
263 }
264 if (m.sl.had) {
265 op.sl=m.sl.val;
266 if (chan[i].state.opllPreset==0) {
267 rWrite(0x06+j,(op.sl<<4)|(op.rr));
268 }
269 }
270 if (m.tl.had) {
271 op.tl=m.tl.val&((j==1)?15:63);
272 if (j==1) {
273 if (i<9) {
274 rWrite(0x30+i,((15-VOL_SCALE_LOG_BROKEN(chan[i].outVol,15-chan[i].state.op[1].tl,15))&15)|(chan[i].state.opllPreset<<4));
275 }
276 } else {
277 if (chan[i].state.opllPreset==0) {
278 rWrite(0x02,(chan[i].state.op[0].ksl<<6)|(op.tl&63));
279 }
280 }
281 }
282
283 if (m.egt.had) {
284 op.ssgEnv=(m.egt.val&1)?8:0;
285 if (chan[i].state.opllPreset==0) {
286 rWrite(0x00+j,(op.am<<7)|(op.vib<<6)|((op.ssgEnv&8)<<2)|(op.ksr<<4)|(op.mult));
287 }
288 }
289 if (m.ksl.had) {
290 op.ksl=m.ksl.val;
291 if (chan[i].state.opllPreset==0) {
292 if (j==1) {
293 rWrite(0x02,(chan[i].state.op[0].ksl<<6)|(chan[i].state.op[0].tl&63));
294 } else {
295 rWrite(0x03,(chan[i].state.op[1].ksl<<6)|((chan[i].state.fms&1)<<4)|((chan[i].state.ams&1)<<3)|chan[i].state.fb);
296 }
297 }
298 }
299 if (m.ksr.had) {
300 op.ksr=m.ksr.val;
301 if (chan[i].state.opllPreset==0) {
302 rWrite(0x00+j,(op.am<<7)|(op.vib<<6)|((op.ssgEnv&8)<<2)|(op.ksr<<4)|(op.mult));
303 }
304 }
305 if (m.vib.had) {
306 op.vib=m.vib.val;
307 if (chan[i].state.opllPreset==0) {
308 rWrite(0x00+j,(op.am<<7)|(op.vib<<6)|((op.ssgEnv&8)<<2)|(op.ksr<<4)|(op.mult));
309 }
310 }
311 }
312
313 if (chan[i].keyOn || chan[i].keyOff) {
314 if (i>=6 && properDrums) {
315 drumState&=~(0x10>>(i-6));
316 immWrite(0x0e,0x20|drumState);
317 //logV("properDrums %d",i);
318 } else if (i>=6 && crapDrums) {
319 drumState&=~(0x10>>(chan[i].note%12));
320 immWrite(0x0e,0x20|drumState);
321 //logV("drums %d",i);
322 } else {
323 if (i<9) {
324 immWrite(0x20+i,(chan[i].freqH)|(chan[i].state.alg?0x20:0));
325 }
326 //logV("normal %d",i);
327 }
328 //chan[i].keyOn=false;
329 chan[i].keyOff=false;
330 }
331 }
332
333 for (int i=0; i<256; i++) {
334 if (pendingWrites[i]!=oldWrites[i]) {
335 immWrite(i,pendingWrites[i]&0xff);
336 oldWrites[i]=pendingWrites[i];
337 }
338 }
339
340 for (int i=0; i<11; i++) {
341 if (chan[i].freqChanged) {
342 int mul=2;
343 int fixedBlock=chan[i].state.block;
344 if (parent->song.linearPitch!=2) {
345 mul=octave(chan[i].baseFreq,fixedBlock)*2;
346 }
347 chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,mul,chan[i].pitch2,chipClock,CHIP_FREQBASE);
348 if (chan[i].fixedFreq>0 && properDrums) chan[i].freq=chan[i].fixedFreq;
349 if (chan[i].freq<0) chan[i].freq=0;
350 if (chan[i].freq>65535) chan[i].freq=65535;
351 int freqt=toFreq(chan[i].freq,fixedBlock);
352 if (freqt>4095) freqt=4095;
353 chan[i].freqL=freqt&0xff;
354 if (i>=6 && properDrums && (i<9 || !noTopHatFreq)) {
355 immWrite(0x10+drumSlot[i],freqt&0xff);
356 immWrite(0x20+drumSlot[i],freqt>>8);
357 switch (i) {
358 case 7:
359 lastFreqSH=0;
360 break;
361 case 8:
362 lastFreqTT=0;
363 break;
364 case 9:
365 lastFreqTT=1;
366 break;
367 case 19:
368 lastFreqSH=1;
369 break;
370 }
371 } else if (i<6 || !crapDrums) {
372 if (i<9) {
373 immWrite(0x10+i,freqt&0xff);
374 }
375 }
376 chan[i].freqH=(freqt>>8)&15;
377 }
378 if (chan[i].keyOn && i>=6 && properDrums) {
379 if (!isMuted[i]) {
380 drumState|=(0x10>>(i-6));
381 immWrite(0x0e,0x20|drumState);
382 }
383 chan[i].keyOn=false;
384 } else if (chan[i].keyOn && i>=6 && crapDrums) {
385 //printf("%d\n",chan[i].note%12);
386 drumState|=(0x10>>(chan[i].note%12));
387 immWrite(0x0e,0x20|drumState);
388 chan[i].keyOn=false;
389 } else if ((chan[i].keyOn || chan[i].freqChanged) && i<9) {
390 //immWrite(0x28,0xf0|konOffs[i]);
391 if (!(i>=6 && properDrums)) {
392 if (i<9) {
393 immWrite(0x20+i,(chan[i].freqH)|(chan[i].active<<4)|(chan[i].state.alg?0x20:0));
394 }
395 }
396 chan[i].keyOn=false;
397 }
398 chan[i].freqChanged=false;
399 }
400 }
401
402 #define OPLL_C_NUM 343
403
404 int DivPlatformOPLL::octave(int freq, int fixedBlock) {
405 if (fixedBlock>0) {
406 return 1<<(fixedBlock-1);
407 }
408 freq/=OPLL_C_NUM;
409 if (freq==0) return 1;
410 return 1<<bsr(freq);
411 }
412
413 int DivPlatformOPLL::toFreq(int freq, int fixedBlock) {
414 int block=0;
415 if (fixedBlock>0) {
416 block=fixedBlock-1;
417 } else {
418 block=freq/OPLL_C_NUM;
419 if (block>0) block=bsr(block);
420 }
421 if (block>7) block=7;
422 freq>>=block;
423 if (freq>0x1ff) freq=0x1ff;
424 return (block<<9)|freq;
425 }
426
427 void DivPlatformOPLL::muteChannel(int ch, bool mute) {
428 isMuted[ch]=mute;
429 if (selCore==1) {
430 OPLL_setMask(fm_emu,
431 (isMuted[0]?1:0)|
432 (isMuted[1]?2:0)|
433 (isMuted[2]?4:0)|
434 (isMuted[3]?8:0)|
435 (isMuted[4]?16:0)|
436 (isMuted[5]?32:0)|
437 (isMuted[6]?64:0)|
438 (isMuted[7]?128:0)|
439 (isMuted[8]?256:0)
440 );
441 }
442 }
443
444 void DivPlatformOPLL::commitState(int ch, DivInstrument* ins) {
445 if (chan[ch].insChanged) {
446 chan[ch].state=ins->fm;
447 }
448
449 if (chan[ch].insChanged) {
450 // update custom preset
451 if (chan[ch].state.opllPreset==0) {
452 DivInstrumentFM::Operator& mod=chan[ch].state.op[0];
453 DivInstrumentFM::Operator& car=chan[ch].state.op[1];
454 rWrite(0x00,(mod.am<<7)|(mod.vib<<6)|((mod.ssgEnv&8)<<2)|(mod.ksr<<4)|(mod.mult));
455 rWrite(0x01,(car.am<<7)|(car.vib<<6)|((car.ssgEnv&8)<<2)|(car.ksr<<4)|(car.mult));
456 rWrite(0x02,(mod.ksl<<6)|(mod.tl&63));
457 rWrite(0x03,(car.ksl<<6)|((chan[ch].state.fms&1)<<4)|((chan[ch].state.ams&1)<<3)|chan[ch].state.fb);
458 rWrite(0x04,(mod.ar<<4)|(mod.dr));
459 rWrite(0x05,(car.ar<<4)|(car.dr));
460 rWrite(0x06,(mod.sl<<4)|(mod.rr));
461 rWrite(0x07,(car.sl<<4)|(car.rr));
462 lastCustomMemory=ch;
463 }
464 if (chan[ch].state.opllPreset==16) { // compatible drums mode
465 if (ch>=6) {
466 if (!properDrumsSys) {
467 crapDrums=true;
468 immWrite(0x16,0x20);
469 immWrite(0x26,0x05);
470 immWrite(0x16,0x20);
471 immWrite(0x26,0x05);
472 immWrite(0x17,0x50);
473 immWrite(0x27,0x05);
474 immWrite(0x17,0x50);
475 immWrite(0x27,0x05);
476 immWrite(0x18,0xC0);
477 immWrite(0x28,0x01);
478 }
479 }
480 } else {
481 if (ch>=6) {
482 if (crapDrums) {
483 crapDrums=false;
484 immWrite(0x0e,0);
485 drumState=0;
486 }
487 }
488 if (ch<9) {
489 rWrite(0x30+ch,((15-VOL_SCALE_LOG_BROKEN(chan[ch].outVol,15-chan[ch].state.op[1].tl,15))&15)|(chan[ch].state.opllPreset<<4));
490 }
491 }
492 }
493 }
494
495 void DivPlatformOPLL::switchMode(bool mode) {
496 if (mode==properDrums) return;
497 if (mode) {
498 //logV("mode switch to DRUMS");
499 for (int i=0; i<5; i++) {
500 drumActivated[i]=chan[6+i].keyOn;
501 }
502
503 immWrite(0x26,0);
504 immWrite(0x27,0);
505 immWrite(0x28,0);
506 immWrite(0x16,0);
507 immWrite(0x17,0);
508 immWrite(0x18,0);
509 immWrite(0x0e,0x20);
510 rWrite(0x36,DRUM_VOL(0));
511 rWrite(0x37,DRUM_VOL(1)|(DRUM_VOL(4)<<4));
512 rWrite(0x38,DRUM_VOL(3)|(DRUM_VOL(2)<<4));
513 oldWrites[0x36]=-1;
514 oldWrites[0x37]=-1;
515 oldWrites[0x38]=-1;
516 } else {
517 //logV("mode switch to NORMAL");
518 immWrite(0x0e,0x20);
519 immWrite(0x0e,0x00);
520 for (int i=6; i<9; i++) {
521 if (chan[i].active) {
522 chan[i].freqChanged=true;
523 chan[i].keyOff=false;
524 chan[i].keyOn=true;
525 oldWrites[0x30+i]=-1;
526 }
527 chan[i].insChanged=true;
528 }
529 }
530 properDrums=mode;
531 drumState=0;
532 }
533
534 int DivPlatformOPLL::dispatch(DivCommand c) {
535 switch (c.cmd) {
536 case DIV_CMD_NOTE_ON: {
537 if (c.chan>=9 && !properDrums) return 0;
538 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_OPLL);
539 if (chan[c.chan].insChanged) {
540 chan[c.chan].state=ins->fm;
541 }
542
543 chan[c.chan].macroInit(ins);
544 if (!chan[c.chan].std.vol.will) {
545 chan[c.chan].outVol=chan[c.chan].vol;
546 }
547
548 if (c.chan>=6 && properDrums) { // drums mode
549 chan[c.chan].insChanged=false;
550 drumActivated[c.chan-6]=true;
551 if (c.value!=DIV_NOTE_NULL) {
552 if (chan[c.chan].state.opllPreset==16 && chan[c.chan].state.fixedDrums) {
553 if (fixedAll) {
554 chan[6].fixedFreq=(chan[c.chan].state.kickFreq&511)<<(chan[c.chan].state.kickFreq>>9);
555 chan[7].fixedFreq=(chan[c.chan].state.snareHatFreq&511)<<(chan[c.chan].state.snareHatFreq>>9);
556 chan[8].fixedFreq=(chan[c.chan].state.tomTopFreq&511)<<(chan[c.chan].state.tomTopFreq>>9);
557 chan[9].fixedFreq=(chan[c.chan].state.tomTopFreq&511)<<(chan[c.chan].state.tomTopFreq>>9);
558 chan[10].fixedFreq=(chan[c.chan].state.snareHatFreq&511)<<(chan[c.chan].state.snareHatFreq>>9);
559
560 chan[7].freqChanged=true;
561 chan[8].freqChanged=true;
562 chan[9].freqChanged=true;
563 } else {
564 switch (c.chan) {
565 case 6:
566 chan[c.chan].fixedFreq=(chan[c.chan].state.kickFreq&511)<<(chan[c.chan].state.kickFreq>>9);
567 break;
568 case 7: case 10:
569 chan[c.chan].fixedFreq=(chan[c.chan].state.snareHatFreq&511)<<(chan[c.chan].state.snareHatFreq>>9);
570 break;
571 case 8: case 9:
572 chan[c.chan].fixedFreq=(chan[c.chan].state.tomTopFreq&511)<<(chan[c.chan].state.tomTopFreq>>9);
573 break;
574 }
575 }
576 } else {
577 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
578 }
579 chan[c.chan].note=c.value;
580 chan[c.chan].freqChanged=true;
581 }
582 chan[c.chan].keyOn=true;
583 chan[c.chan].active=true;
584
585 rWrite(0x36,DRUM_VOL(0));
586 rWrite(0x37,DRUM_VOL(1)|(DRUM_VOL(4)<<4));
587 rWrite(0x38,DRUM_VOL(3)|(DRUM_VOL(2)<<4));
588 break;
589 }
590
591 commitState(c.chan,ins);
592 chan[c.chan].insChanged=false;
593
594 if (c.value!=DIV_NOTE_NULL) {
595 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
596 chan[c.chan].note=c.value;
597
598 if (c.chan>=6 && crapDrums) {
599 switch (chan[c.chan].note%12) {
600 case 0: // kick
601 drumVol[0]=(15-(chan[c.chan].outVol*(15-chan[c.chan].state.op[1].tl))/15);
602 break;
603 case 1: // snare
604 drumVol[1]=(15-(chan[c.chan].outVol*(15-chan[c.chan].state.op[1].tl))/15);
605 break;
606 case 2: // tom
607 drumVol[2]=(15-(chan[c.chan].outVol*(15-chan[c.chan].state.op[1].tl))/15);
608 break;
609 case 3: // top
610 drumVol[3]=(15-(chan[c.chan].outVol*(15-chan[c.chan].state.op[1].tl))/15);
611 break;
612 default: // hi-hat
613 drumVol[4]=(15-(chan[c.chan].outVol*(15-chan[c.chan].state.op[1].tl))/15);
614 break;
615 }
616 rWrite(0x36,DRUM_VOL(0));
617 rWrite(0x37,DRUM_VOL(1)|(DRUM_VOL(4)<<4));
618 rWrite(0x38,DRUM_VOL(3)|(DRUM_VOL(2)<<4));
619 }
620 chan[c.chan].freqChanged=true;
621 }
622 chan[c.chan].keyOn=true;
623 chan[c.chan].active=true;
624 break;
625 }
626 case DIV_CMD_NOTE_OFF:
627 if (c.chan>=9 && !properDrums) return 0;
628 chan[c.chan].keyOff=true;
629 chan[c.chan].keyOn=false;
630 chan[c.chan].freqChanged=true;
631 chan[c.chan].active=false;
632 break;
633 case DIV_CMD_NOTE_OFF_ENV:
634 if (c.chan>=9 && !properDrums) return 0;
635 chan[c.chan].keyOff=true;
636 chan[c.chan].keyOn=false;
637 chan[c.chan].freqChanged=true;
638 chan[c.chan].active=false;
639 chan[c.chan].std.release();
640 break;
641 case DIV_CMD_ENV_RELEASE:
642 if (c.chan>=9 && !properDrums) return 0;
643 chan[c.chan].std.release();
644 break;
645 case DIV_CMD_VOLUME: {
646 if (c.chan>=9 && !properDrums) return 0;
647 chan[c.chan].vol=c.value;
648 if (!chan[c.chan].std.vol.has) {
649 chan[c.chan].outVol=c.value;
650 }
651 if (c.chan>=6 && properDrums) {
652 drumVol[c.chan-6]=15-chan[c.chan].outVol;
653 rWrite(0x36,DRUM_VOL(0));
654 rWrite(0x37,DRUM_VOL(1)|(DRUM_VOL(4)<<4));
655 rWrite(0x38,DRUM_VOL(3)|(DRUM_VOL(2)<<4));
656 break;
657 } else if (c.chan<6 || !crapDrums) {
658 if (c.chan<9) {
659 rWrite(0x30+c.chan,((15-VOL_SCALE_LOG_BROKEN(chan[c.chan].outVol,15-chan[c.chan].state.op[1].tl,15))&15)|(chan[c.chan].state.opllPreset<<4));
660 }
661 }
662 break;
663 }
664 case DIV_CMD_GET_VOLUME: {
665 return chan[c.chan].vol;
666 break;
667 }
668 case DIV_CMD_INSTRUMENT:
669 if (chan[c.chan].ins!=c.value || c.value2==1) {
670 chan[c.chan].insChanged=true;
671 }
672 chan[c.chan].ins=c.value;
673 break;
674 case DIV_CMD_PITCH: {
675 if (c.chan>=9 && !properDrums) return 0;
676 chan[c.chan].pitch=c.value;
677 chan[c.chan].freqChanged=true;
678 break;
679 }
680 case DIV_CMD_NOTE_PORTA: {
681 if (c.chan>=9 && !properDrums) return 0;
682 int destFreq=NOTE_FREQUENCY(c.value2);
683 int newFreq;
684 bool return2=false;
685 int mul=1;
686 int fixedBlock=0;
687 if (parent->song.linearPitch!=2) {
688 fixedBlock=chan[c.chan].state.block;
689 mul=octave(chan[c.chan].baseFreq,fixedBlock);
690 }
691 if (destFreq>chan[c.chan].baseFreq) {
692 newFreq=chan[c.chan].baseFreq+c.value*mul;
693 if (newFreq>=destFreq) {
694 newFreq=destFreq;
695 return2=true;
696 }
697 } else {
698 newFreq=chan[c.chan].baseFreq-c.value*mul;
699 if (newFreq<=destFreq) {
700 newFreq=destFreq;
701 return2=true;
702 }
703 }
704 /*if (!chan[c.chan].portaPause) {
705 if (mul!=octave(newFreq,fixedBlock)) {
706 chan[c.chan].portaPause=true;
707 break;
708 }
709 }*/
710 chan[c.chan].baseFreq=newFreq;
711 chan[c.chan].portaPause=false;
712 chan[c.chan].freqChanged=true;
713 if (return2) {
714 chan[c.chan].inPorta=false;
715 return 2;
716 }
717 break;
718 }
719 case DIV_CMD_LEGATO: {
720 if (c.chan>=9 && !properDrums) return 0;
721 if (c.chan<6 || (!crapDrums && !properDrums)) {
722 if (chan[c.chan].insChanged) {
723 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_OPLL);
724 commitState(c.chan,ins);
725 chan[c.chan].insChanged=false;
726 }
727 }
728 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
729 chan[c.chan].note=c.value;
730 chan[c.chan].freqChanged=true;
731 break;
732 }
733 case DIV_CMD_WAVE: {
734 if (c.chan>=6 && (crapDrums || properDrums)) break;
735 if (c.chan>=9) break;
736 chan[c.chan].state.opllPreset=c.value&15;
737 rWrite(0x30+c.chan,((15-VOL_SCALE_LOG_BROKEN(chan[c.chan].outVol,15-chan[c.chan].state.op[1].tl,15))&15)|(chan[c.chan].state.opllPreset<<4));
738 break;
739 }
740 case DIV_CMD_FM_FB: {
741 if (c.chan>=9 && !properDrums) return 0;
742 //DivInstrumentFM::Operator& mod=chan[c.chan].state.op[0];
743 DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
744 chan[c.chan].state.fb=c.value&7;
745 rWrite(0x03,(car.ksl<<6)|((chan[c.chan].state.fms&1)<<4)|((chan[c.chan].state.ams&1)<<3)|chan[c.chan].state.fb);
746 break;
747 }
748
749 case DIV_CMD_FM_MULT: {
750 if (c.chan>=9 && !properDrums) return 0;
751 if (c.value==0) {
752 DivInstrumentFM::Operator& mod=chan[c.chan].state.op[0];
753 mod.mult=c.value2&15;
754 rWrite(0x00,(mod.am<<7)|(mod.vib<<6)|((mod.ssgEnv&8)<<2)|(mod.ksr<<4)|(mod.mult));
755 } else {
756 DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
757 car.mult=c.value2&15;
758 rWrite(0x01,(car.am<<7)|(car.vib<<6)|((car.ssgEnv&8)<<2)|(car.ksr<<4)|(car.mult));
759 }
760 break;
761 }
762 case DIV_CMD_FM_TL: {
763 if (c.chan>=9 && !properDrums) return 0;
764 if (c.value==0) {
765 DivInstrumentFM::Operator& mod=chan[c.chan].state.op[0];
766 //DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
767 mod.tl=c.value2&63;
768 rWrite(0x02,(mod.ksl<<6)|(mod.tl&63));
769 } else {
770 DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
771 car.tl=c.value2&15;
772 if (c.chan<9) {
773 rWrite(0x30+c.chan,((15-VOL_SCALE_LOG_BROKEN(chan[c.chan].outVol,15-chan[c.chan].state.op[1].tl,15))&15)|(chan[c.chan].state.opllPreset<<4));
774 }
775 }
776 break;
777 }
778 case DIV_CMD_FM_AR: {
779 if (c.chan>=9 && !properDrums) return 0;
780 DivInstrumentFM::Operator& mod=chan[c.chan].state.op[0];
781 DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
782 if (c.value<0) {
783 mod.ar=c.value2&15;
784 car.ar=c.value2&15;
785 } else {
786 if (c.value==0) {
787 mod.ar=c.value2&15;
788 } else {
789 car.ar=c.value2&15;
790 }
791 }
792 rWrite(0x04,(mod.ar<<4)|(mod.dr));
793 rWrite(0x05,(car.ar<<4)|(car.dr));
794 break;
795 }
796 case DIV_CMD_FM_DR: {
797 if (c.chan>=9 && !properDrums) return 0;
798 DivInstrumentFM::Operator& mod=chan[c.chan].state.op[0];
799 DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
800 if (c.value<0) {
801 mod.dr=c.value2&15;
802 car.dr=c.value2&15;
803 } else {
804 if (c.value==0) {
805 mod.dr=c.value2&15;
806 } else {
807 car.dr=c.value2&15;
808 }
809 }
810 rWrite(0x04,(mod.ar<<4)|(mod.dr));
811 rWrite(0x05,(car.ar<<4)|(car.dr));
812 break;
813 }
814 case DIV_CMD_FM_SL: {
815 if (c.chan>=9 && !properDrums) return 0;
816 DivInstrumentFM::Operator& mod=chan[c.chan].state.op[0];
817 DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
818 if (c.value<0) {
819 mod.sl=c.value2&15;
820 car.sl=c.value2&15;
821 } else {
822 if (c.value==0) {
823 mod.sl=c.value2&15;
824 } else {
825 car.sl=c.value2&15;
826 }
827 }
828 rWrite(0x06,(mod.sl<<4)|(mod.rr));
829 rWrite(0x07,(car.sl<<4)|(car.rr));
830 break;
831 }
832 case DIV_CMD_FM_RR: {
833 if (c.chan>=9 && !properDrums) return 0;
834 DivInstrumentFM::Operator& mod=chan[c.chan].state.op[0];
835 DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
836 if (c.value<0) {
837 mod.rr=c.value2&15;
838 car.rr=c.value2&15;
839 } else {
840 if (c.value==0) {
841 mod.rr=c.value2&15;
842 } else {
843 car.rr=c.value2&15;
844 }
845 }
846 rWrite(0x06,(mod.sl<<4)|(mod.rr));
847 rWrite(0x07,(car.sl<<4)|(car.rr));
848 break;
849 }
850 case DIV_CMD_FM_AM: {
851 if (c.chan>=9 && !properDrums) return 0;
852 DivInstrumentFM::Operator& mod=chan[c.chan].state.op[0];
853 DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
854 if (c.value<0) {
855 mod.am=c.value2&1;
856 car.am=c.value2&1;
857 } else {
858 if (c.value==0) {
859 mod.am=c.value2&1;
860 } else {
861 car.am=c.value2&1;
862 }
863 }
864 rWrite(0x00,(mod.am<<7)|(mod.vib<<6)|((mod.ssgEnv&8)<<2)|(mod.ksr<<4)|(mod.mult));
865 rWrite(0x01,(car.am<<7)|(car.vib<<6)|((car.ssgEnv&8)<<2)|(car.ksr<<4)|(car.mult));
866 break;
867 }
868 case DIV_CMD_FM_VIB: {
869 if (c.chan>=9 && !properDrums) return 0;
870 DivInstrumentFM::Operator& mod=chan[c.chan].state.op[0];
871 DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
872 if (c.value<0) {
873 mod.vib=c.value2&1;
874 car.vib=c.value2&1;
875 } else {
876 if (c.value==0) {
877 mod.vib=c.value2&1;
878 } else {
879 car.vib=c.value2&1;
880 }
881 }
882 rWrite(0x00,(mod.am<<7)|(mod.vib<<6)|((mod.ssgEnv&8)<<2)|(mod.ksr<<4)|(mod.mult));
883 rWrite(0x01,(car.am<<7)|(car.vib<<6)|((car.ssgEnv&8)<<2)|(car.ksr<<4)|(car.mult));
884 break;
885 }
886 case DIV_CMD_FM_KSR: {
887 if (c.chan>=9 && !properDrums) return 0;
888 DivInstrumentFM::Operator& mod=chan[c.chan].state.op[0];
889 DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
890 if (c.value<0) {
891 mod.ksr=c.value2&1;
892 car.ksr=c.value2&1;
893 } else {
894 if (c.value==0) {
895 mod.ksr=c.value2&1;
896 } else {
897 car.ksr=c.value2&1;
898 }
899 }
900 rWrite(0x00,(mod.am<<7)|(mod.vib<<6)|((mod.ssgEnv&8)<<2)|(mod.ksr<<4)|(mod.mult));
901 rWrite(0x01,(car.am<<7)|(car.vib<<6)|((car.ssgEnv&8)<<2)|(car.ksr<<4)|(car.mult));
902 break;
903 }
904 case DIV_CMD_FM_SUS: {
905 if (c.chan>=9 && !properDrums) return 0;
906 DivInstrumentFM::Operator& mod=chan[c.chan].state.op[0];
907 DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
908 if (c.value<0) {
909 mod.ssgEnv=c.value2?8:0;
910 car.ssgEnv=c.value2?8:0;
911 } else {
912 if (c.value==0) {
913 mod.ssgEnv=c.value2?8:0;
914 } else {
915 car.ssgEnv=c.value2?8:0;
916 }
917 }
918 rWrite(0x00,(mod.am<<7)|(mod.vib<<6)|((mod.ssgEnv&8)<<2)|(mod.ksr<<4)|(mod.mult));
919 rWrite(0x01,(car.am<<7)|(car.vib<<6)|((car.ssgEnv&8)<<2)|(car.ksr<<4)|(car.mult));
920 break;
921 }
922 case DIV_CMD_FM_RS: {
923 if (c.chan>=9 && !properDrums) return 0;
924 DivInstrumentFM::Operator& mod=chan[c.chan].state.op[0];
925 DivInstrumentFM::Operator& car=chan[c.chan].state.op[1];
926 if (c.value<0) {
927 mod.ksl=c.value2&3;
928 car.ksl=c.value2&3;
929 } else {
930 if (c.value==0) {
931 mod.ksl=c.value2&3;
932 } else {
933 car.ksl=c.value2&3;
934 }
935 }
936 rWrite(0x02,(mod.ksl<<6)|(mod.tl&63));
937 rWrite(0x03,(car.ksl<<6)|((chan[c.chan].state.fms&1)<<4)|((chan[c.chan].state.ams&1)<<3)|chan[c.chan].state.fb);
938 break;
939 }
940 case DIV_CMD_FM_EXTCH:
941 if (!properDrumsSys) break;
942 if ((int)properDrums==c.value) break;
943 switchMode(c.value);
944 break;
945 case DIV_CMD_MACRO_OFF:
946 chan[c.chan].std.mask(c.value,true);
947 break;
948 case DIV_CMD_MACRO_ON:
949 chan[c.chan].std.mask(c.value,false);
950 break;
951 case DIV_CMD_MACRO_RESTART:
952 chan[c.chan].std.restart(c.value);
953 break;
954 case DIV_CMD_GET_VOLMAX:
955 return 15;
956 break;
957 case DIV_CMD_PRE_PORTA:
958 if (c.chan>=9 && !properDrums) return 0;
959 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_FREQUENCY(chan[c.chan].note);
960 chan[c.chan].inPorta=c.value;
961 break;
962 case DIV_CMD_PRE_NOTE:
963 break;
964 default:
965 //printf("WARNING: unimplemented command %d\n",c.cmd);
966 break;
967 }
968 return 1;
969 }
970
971 void DivPlatformOPLL::forceIns() {
972 for (int i=0; i<9; i++) {
973 if (i>=6 && properDrums) continue;
974 // update custom preset
975 if (chan[i].state.opllPreset==0 && i==lastCustomMemory) {
976 DivInstrumentFM::Operator& mod=chan[i].state.op[0];
977 DivInstrumentFM::Operator& car=chan[i].state.op[1];
978 rWrite(0x00,(mod.am<<7)|(mod.vib<<6)|((mod.ssgEnv&8)<<2)|(mod.ksr<<4)|(mod.mult));
979 rWrite(0x01,(car.am<<7)|(car.vib<<6)|((car.ssgEnv&8)<<2)|(car.ksr<<4)|(car.mult));
980 rWrite(0x02,(mod.ksl<<6)|(mod.tl&63));
981 rWrite(0x03,(car.ksl<<6)|((chan[i].state.fms&1)<<4)|((chan[i].state.ams&1)<<3)|chan[i].state.fb);
982 rWrite(0x04,(mod.ar<<4)|(mod.dr));
983 rWrite(0x05,(car.ar<<4)|(car.dr));
984 rWrite(0x06,(mod.sl<<4)|(mod.rr));
985 rWrite(0x07,(car.sl<<4)|(car.rr));
986 }
987 if (i<9) {
988 rWrite(0x30+i,((15-VOL_SCALE_LOG_BROKEN(chan[i].outVol,15-chan[i].state.op[1].tl,15))&15)|(chan[i].state.opllPreset<<4));
989 }
990 if (!(i>=6 && properDrums)) {
991 if (chan[i].active) {
992 chan[i].keyOn=true;
993 chan[i].freqChanged=true;
994 chan[i].insChanged=true;
995 }
996 }
997 }
998 if (crapDrums) { // WHAT?! FIX THIS!
999 immWrite(0x16,0x20);
1000 immWrite(0x26,0x05);
1001 immWrite(0x16,0x20);
1002 immWrite(0x26,0x05);
1003 immWrite(0x17,0x50);
1004 immWrite(0x27,0x05);
1005 immWrite(0x17,0x50);
1006 immWrite(0x27,0x05);
1007 immWrite(0x18,0xC0);
1008 immWrite(0x28,0x01);
1009 }
1010 // restore drum volumes and state
1011 if (properDrums) {
1012 rWrite(0x36,DRUM_VOL(0));
1013 rWrite(0x37,DRUM_VOL(1)|(DRUM_VOL(4)<<4));
1014 rWrite(0x38,DRUM_VOL(3)|(DRUM_VOL(2)<<4));
1015
1016 if (lastFreqSH==0) {
1017 chan[7].freqChanged=true;
1018 } else if (lastFreqSH==1) {
1019 chan[10].freqChanged=true;
1020 }
1021
1022 if (lastFreqTT==0) {
1023 chan[8].freqChanged=true;
1024 } else if (lastFreqTT==1) {
1025 chan[9].freqChanged=true;
1026 }
1027
1028 chan[6].freqChanged=true;
1029 }
1030 drumState=0;
1031 }
1032
1033 void DivPlatformOPLL::toggleRegisterDump(bool enable) {
1034 DivDispatch::toggleRegisterDump(enable);
1035 }
1036
1037 void DivPlatformOPLL::setVRC7(bool vrc) {
1038 vrc7=vrc;
1039 }
1040
1041 void DivPlatformOPLL::setProperDrums(bool pd) {
1042 properDrums=pd;
1043 properDrumsSys=pd;
1044 }
1045
1046
1047 void* DivPlatformOPLL::getChanState(int ch) {
1048 return &chan[ch];
1049 }
1050
1051 DivMacroInt* DivPlatformOPLL::getChanMacroInt(int ch) {
1052 return &chan[ch].std;
1053 }
1054
1055 DivDispatchOscBuffer* DivPlatformOPLL::getOscBuffer(int ch) {
1056 if (ch>=9 && selCore==0) return NULL;
1057 return oscBuf[ch];
1058 }
1059
1060 int DivPlatformOPLL::mapVelocity(int ch, float vel) {
1061 // -3dB per step
1062 if (vel==0) return 0;
1063 if (vel>=1.0) return 15;
1064 return CLAMP(round(16.0-(14.0-log2(vel*127.0)*2.0)),0,15);
1065 }
1066
1067 float DivPlatformOPLL::getGain(int ch, int vol) {
1068 if (vol==0) return 0;
1069 return 1.0/pow(10.0,(float)(15-vol)*3.0/20.0);
1070 }
1071
1072 unsigned char* DivPlatformOPLL::getRegisterPool() {
1073 return regPool;
1074 }
1075
1076 int DivPlatformOPLL::getRegisterPoolSize() {
1077 return 64;
1078 }
1079
1080 static const unsigned char nukedToEmuPatch[4]={
1081 0, 2, 3, 1
1082 };
1083
1084 void DivPlatformOPLL::reset() {
1085 while (!writes.empty()) writes.pop();
1086 memset(regPool,0,256);
1087 if (vrc7) {
1088 OPLL_Reset(&fm,opll_type_ds1001);
1089 } else {
1090 OPLL_Reset(&fm,opll_type_ym2413);
1091 switch (patchSet) {
1092 case 0:
1093 fm.patchrom=OPLL_GetPatchROM(opll_type_ym2413);
1094 break;
1095 case 1:
1096 fm.patchrom=OPLL_GetPatchROM(opll_type_ymf281);
1097 break;
1098 case 2:
1099 fm.patchrom=OPLL_GetPatchROM(opll_type_ym2423);
1100 break;
1101 case 3:
1102 fm.patchrom=OPLL_GetPatchROM(opll_type_ds1001);
1103 break;
1104 }
1105 }
1106 if (dumpWrites) {
1107 addWrite(0xffffffff,0);
1108 }
1109 if (selCore==1) {
1110 OPLL_reset(fm_emu);
1111 OPLL_setChipType(fm_emu,vrc7?1:0);
1112 OPLL_resetPatch(fm_emu,vrc7?1:nukedToEmuPatch[patchSet&3]);
1113 OPLL_setMask(fm_emu,
1114 (isMuted[0]?1:0)|
1115 (isMuted[1]?2:0)|
1116 (isMuted[2]?4:0)|
1117 (isMuted[3]?8:0)|
1118 (isMuted[4]?16:0)|
1119 (isMuted[5]?32:0)|
1120 (isMuted[6]?64:0)|
1121 (isMuted[7]?128:0)|
1122 (isMuted[8]?256:0)
1123 );
1124 }
1125 for (int i=0; i<11; i++) {
1126 chan[i]=DivPlatformOPLL::Channel();
1127 chan[i].std.setEngine(parent);
1128 chan[i].vol=15;
1129 chan[i].outVol=15;
1130 }
1131
1132 for (int i=0; i<256; i++) {
1133 oldWrites[i]=-1;
1134 pendingWrites[i]=-1;
1135 }
1136
1137 lastBusy=60;
1138 drumState=0;
1139 lastCustomMemory=-1;
1140
1141 for (int i=0; i<5; i++) {
1142 drumVol[i]=0;
1143 drumActivated[i]=true;
1144 }
1145
1146 delay=0;
1147 crapDrums=false;
1148 properDrums=properDrumsSys;
1149
1150 lastFreqSH=-1;
1151 lastFreqTT=-1;
1152
1153 if (properDrums) {
1154 immWrite(0x0e,0x20);
1155 }
1156 }
1157
1158 bool DivPlatformOPLL::keyOffAffectsArp(int ch) {
1159 return false;
1160 }
1161
1162 bool DivPlatformOPLL::keyOffAffectsPorta(int ch) {
1163 return false;
1164 }
1165
1166 bool DivPlatformOPLL::getLegacyAlwaysSetVolume() {
1167 return false;
1168 }
1169
1170 void DivPlatformOPLL::notifyInsChange(int ins) {
1171 for (int i=0; i<11; i++) {
1172 if (chan[i].ins==ins) {
1173 chan[i].insChanged=true;
1174 }
1175 }
1176 }
1177
1178 void DivPlatformOPLL::notifyInsDeletion(void* ins) {
1179 for (int i=0; i<11; i++) {
1180 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
1181 }
1182 }
1183
1184 void DivPlatformOPLL::poke(unsigned int addr, unsigned short val) {
1185 immWrite(addr,val);
1186 }
1187
1188 void DivPlatformOPLL::poke(std::vector<DivRegWrite>& wlist) {
1189 for (DivRegWrite& i: wlist) immWrite(i.addr,i.val);
1190 }
1191
1192 int DivPlatformOPLL::getPortaFloor(int ch) {
1193 return (ch>5)?12:0;
1194 }
1195
1196 void DivPlatformOPLL::setCore(unsigned char which) {
1197 selCore=which;
1198 }
1199
1200 float DivPlatformOPLL::getPostAmp() {
1201 return 1.5f;
1202 }
1203
1204 void DivPlatformOPLL::setFlags(const DivConfig& flags) {
1205 int clockSel=flags.getInt("clockSel",0);
1206 if (clockSel==3) {
1207 chipClock=COLOR_NTSC/2.0;
1208 } else if (clockSel==2) {
1209 chipClock=4000000.0;
1210 } else if (clockSel==1) {
1211 chipClock=COLOR_PAL*4.0/5.0;
1212 } else {
1213 chipClock=COLOR_NTSC;
1214 }
1215 CHECK_CUSTOM_CLOCK;
1216 patchSet=flags.getInt("patchSet",0);
1217 if (selCore==1) {
1218 rate=chipClock/72;
1219 } else {
1220 rate=chipClock/36;
1221 }
1222 for (int i=0; i<11; i++) {
1223 oscBuf[i]->setRate(rate);
1224 }
1225 noTopHatFreq=flags.getBool("noTopHatFreq",false);
1226 fixedAll=flags.getBool("fixedAll",true);
1227 }
1228
1229 int DivPlatformOPLL::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
1230 parent=p;
1231 dumpWrites=false;
1232 skipRegisterWrites=false;
1233 patchSet=0;
1234 fm_emu=NULL;
1235 if (selCore==1) {
1236 fm_emu=OPLL_new(72,1);
1237 OPLL_setChipType(fm_emu,vrc7?1:0);
1238 }
1239
1240 for (int i=0; i<11; i++) {
1241 isMuted[i]=false;
1242 oscBuf[i]=new DivDispatchOscBuffer;
1243 }
1244 setFlags(flags);
1245
1246 reset();
1247 return 11;
1248 }
1249
1250 void DivPlatformOPLL::quit() {
1251 for (int i=0; i<11; i++) {
1252 delete oscBuf[i];
1253 }
1254 if (fm_emu!=NULL) {
1255 OPLL_delete(fm_emu);
1256 fm_emu=NULL;
1257 }
1258 }
1259
1260 DivPlatformOPLL::~DivPlatformOPLL() {
1261 }