Debian Codegraph @

src/engine/platform/opl.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 "opl.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 KVSL(x,y) ((chan[x].state.op[orderedOpsL1[ops==4][y]].kvs==2 && isOutputL[ops==4][chan[x].state.alg][y]) || chan[x].state.op[orderedOpsL1[ops==4][y]].kvs==1)
31
32 #define CHIP_FREQBASE chipFreqBase
33 #define PCM_FREQBASE (402653184)
34
35 #define NOTE_PCM(x) parent->calcBaseFreq(chipClock,PCM_FREQBASE,x,false)
36
37 #define PCM_CHECK(ch) ((chipType==4) && (ch>=pcmChanOffs))
38 #define PCM_REG(ch) (ch-pcmChanOffs)
39
40 // check if PCM in RAM (and size is <= 2MB) - 4MB uses whole sample area
41 #define PCM_IN_RAM (ramSize<=0x200000)
42
43 // N = invalid
44 #define N 255
45
46 const unsigned char slotsOPL2i[4][20]={
47 {0, 1, 2, 6, 7, 8, 12, 13, 14}, // OP1
48 {3, 4, 5, 9, 10, 11, 15, 16, 17}, // OP2
49 {N, N, N, N, N, N, N, N, N},
50 {N, N, N, N, N, N, N, N, N}
51 };
52
53 const unsigned char slotsOPL2Drumsi[4][20]={
54 {0, 1, 2, 6, 7, 8, 12, 16, 14, 17, 13}, // OP1
55 {3, 4, 5, 9, 10, 11, 15, N, N, N, N}, // OP2
56 {N, N, N, N, N, N, N, N, N, N, N},
57 {N, N, N, N, N, N, N, N, N, N, N}
58 };
59
60 const unsigned short chanMapOPL2[20]={
61 0, 1, 2, 3, 4, 5, 6, 7, 8, N, N, N, N, N, N, N, N, N, N, N
62 };
63
64 const unsigned short chanMapOPL2Drums[20]={
65 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 7, N, N, N, N, N, N, N, N, N
66 };
67
68 const unsigned char outChanMapOPL2[18]={
69 0, 1, 2, 3, 4, 5, 6, 7, 8, N, N, N, N, N, N, N, N, N
70 };
71
72 const unsigned char* slotsOPL2[4]={
73 slotsOPL2i[0],
74 slotsOPL2i[1],
75 slotsOPL2i[2],
76 slotsOPL2i[3]
77 };
78
79 const unsigned char* slotsOPL2Drums[4]={
80 slotsOPL2Drumsi[0],
81 slotsOPL2Drumsi[1],
82 slotsOPL2Drumsi[2],
83 slotsOPL2Drumsi[3]
84 };
85
86 const unsigned char slotsOPL3i[4][20]={
87 {0, 6, 1, 7, 2, 8, 18, 24, 19, 25, 20, 26, 30, 31, 32, 12, 13, 14}, // OP1
88 {3, 9, 4, 10, 5, 11, 21, 27, 22, 28, 23, 29, 33, 34, 35, 15, 16, 17}, // OP2
89 {6, N, 7, N, 8, N, 24, N, 25, N, 26, N, N, N, N, N, N, N}, // OP3
90 {9, N, 10, N, 11, N, 27, N, 28, N, 29, N, N, N, N, N, N, N} // OP4
91 };
92
93 const unsigned char slotsOPL3Drumsi[4][20]={
94 {0, 6, 1, 7, 2, 8, 18, 24, 19, 25, 20, 26, 30, 31, 32, 12, 16, 14, 17, 13}, // OP1
95 {3, 9, 4, 10, 5, 11, 21, 27, 22, 28, 23, 29, 33, 34, 35, 15, N, N, N, N}, // OP2
96 {6, N, 7, N, 8, N, 24, N, 25, N, 26, N, N, N, N, N, N, N, N, N}, // OP3
97 {9, N, 10, N, 11, N, 27, N, 28, N, 29, N, N, N, N, N, N, N, N, N} // OP4
98 };
99
100 const unsigned short chanMapOPL3[20]={
101 0, 3, 1, 4, 2, 5, 0x100, 0x103, 0x101, 0x104, 0x102, 0x105, 0x106, 0x107, 0x108, 6, 7, 8, N, N
102 };
103
104 const unsigned short chanMapOPL3Drums[20]={
105 0, 3, 1, 4, 2, 5, 0x100, 0x103, 0x101, 0x104, 0x102, 0x105, 0x106, 0x107, 0x108, 6, 7, 8, 8, 7
106 };
107
108 const unsigned char outChanMapOPL3[18]={
109 0, 3, 1, 4, 2, 5, 9, 12, 10, 13, 11, 14, 15, 16, 17, 6, 7, 8
110 };
111
112 const unsigned char* slotsOPL3[4]={
113 slotsOPL3i[0],
114 slotsOPL3i[1],
115 slotsOPL3i[2],
116 slotsOPL3i[3]
117 };
118
119 const unsigned char* slotsOPL3Drums[4]={
120 slotsOPL3Drumsi[0],
121 slotsOPL3Drumsi[1],
122 slotsOPL3Drumsi[2],
123 slotsOPL3Drumsi[3]
124 };
125
126 const unsigned int slotMap[36]={
127 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
128 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
129 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
130
131 0x100, 0x101, 0x102, 0x103, 0x104, 0x105,
132 0x108, 0x109, 0x10a, 0x10b, 0x10c, 0x10d,
133 0x110, 0x111, 0x112, 0x113, 0x114, 0x115,
134 };
135
136 const bool isOutputL[2][4][4]={
137 { // 2-op
138 {false, true, false, false}, // 1 > 2
139 { true, true, false, false}, // 1 + 2
140 {false, true, false, false}, // ditto, 0
141 { true, true, false, false}, // ditto, 1
142 },
143 { // 4-op
144 {false, false, false, true}, // 1 > 2 > 3 > 4
145 { true, false, false, true}, // 1 + (2 > 3 > 4)
146 {false, true, false, true}, // (1 > 2) + (3 > 4)
147 { true, false, true, true} // 1 + (2 > 3) + 4
148 }
149 };
150
151 #undef N
152
153 const int orderedOpsL1[2][4]={
154 {0, 1, 0, 1}, // 2-op
155 {0, 2, 1, 3} // 4-op
156 };
157
158 const int orderedOpsL[4]={
159 0,2,1,3
160 };
161
162 #define ADDR_AM_VIB_SUS_KSR_MULT 0x20
163 #define ADDR_KSL_TL 0x40
164 #define ADDR_AR_DR 0x60
165 #define ADDR_SL_RR 0x80
166 #define ADDR_WS 0xe0
167
168 #define ADDR_FREQ 0xa0
169 #define ADDR_FREQH 0xb0
170 #define ADDR_LR_FB_ALG 0xc0
171
172
173 #define PCM_ADDR_WAVE_L 0x208 // Wavetable number LSB
174 #define PCM_ADDR_WAVE_H_FN_L 0x220 // Wavetable number MSB, F-number LSB
175 #define PCM_ADDR_FN_H_PR_OCT 0x238 // F-number MSB, Pseudo-reverb, Octave
176 #define PCM_ADDR_TL 0x250 // Total level, Level direct
177 #define PCM_ADDR_KEY_DAMP_LFORST_CH_PAN 0x268 // Key, Damp, LFO Reset, Channel select, Panpot
178
179 #define PCM_ADDR_LFO_VIB 0x280
180 #define PCM_ADDR_AR_D1R 0x298
181 #define PCM_ADDR_DL_D2R 0x2b0
182 #define PCM_ADDR_RC_RR 0x2c8
183 #define PCM_ADDR_AM 0x2e0
184
185 #define PCM_ADDR_MIX_FM 0x2f8
186 #define PCM_ADDR_MIX_PCM 0x2f9
187
188 void DivPlatformOPL::acquire_nuked(short** buf, size_t len) {
189 thread_local short o[8];
190 thread_local int os[6];
191 thread_local ymfm::ymfm_output<2> aOut;
192 thread_local short pcmBuf[24];
193
194 for (int i=0; i<MAX(adpcmChan+1,totalChans); i++) {
195 oscBuf[i]->begin(len);
196 }
197
198 for (size_t h=0; h<len; h++) {
199 os[0]=0; os[1]=0; os[2]=0; os[3]=0; os[4]=0; os[5]=0;
200 if (!writes.empty() && --delay<0) {
201 QueuedWrite& w=writes.front();
202 if (w.addr==0xfffffffe) {
203 delay=w.val;
204 } else {
205 delay=1;
206 if (w.addr>=0x200) {
207 pcm.writeReg(w.addr&0xff,w.val);
208 regPool[0x200|(w.addr&0xff)]=w.val;
209 } else {
210 switch (w.addr) {
211 case 8:
212 if (adpcmChan>=0) {
213 adpcmB->write(w.addr-7,(w.val&15)|0x80);
214 OPL3_WriteReg(&fm,w.addr,w.val&0xc0);
215 } else {
216 OPL3_WriteReg(&fm,w.addr,w.val);
217 }
218 break;
219 case 7: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 21: case 22: case 23:
220 if (adpcmChan>=0) {
221 adpcmB->write(w.addr-7,w.val);
222 } else {
223 OPL3_WriteReg(&fm,w.addr,w.val);
224 }
225 break;
226 default:
227 OPL3_WriteReg(&fm,w.addr,w.val);
228 break;
229 }
230 regPool[w.addr&511]=w.val;
231 }
232 }
233 writes.pop();
234 }
235
236 if (downsample) {
237 OPL3_Generate4ChResampled(&fm,o);
238 } else {
239 OPL3_Generate4Ch(&fm,o);
240 }
241 if (chipType==4) {
242 pcm.generateMix(o[0],o[1],o[4],o[5],o[6],o[7],pcmBuf);
243 os[0]+=o[4]; // FM + PCM left
244 os[1]+=o[5]; // FM + PCM right
245 os[2]+=o[2]; // FM left
246 os[3]+=o[3]; // FM right
247 os[4]+=o[6]; // PCM left
248 os[5]+=o[7]; // PCM right
249 } else {
250 os[0]+=o[0];
251 os[1]+=o[1];
252 os[2]+=o[2];
253 os[3]+=o[3];
254 }
255
256 if (adpcmChan>=0) {
257 adpcmB->clock();
258 aOut.clear();
259 adpcmB->output<2>(aOut,0);
260
261 if (!isMuted[adpcmChan]) {
262 os[0]-=aOut.data[0]>>3;
263 os[1]-=aOut.data[0]>>3;
264 oscBuf[adpcmChan]->putSample(h,aOut.data[0]>>1);
265 } else {
266 oscBuf[adpcmChan]->putSample(h,0);
267 }
268 }
269
270 if (properDrums) {
271 for (int i=0; i<melodicChans+1; i++) {
272 unsigned char ch=outChanMap[i];
273 int chOut=0;
274 if (ch==255) continue;
275 if (isMuted[i]) continue;
276 if (fm.channel[i].out[0]!=NULL) {
277 chOut+=*fm.channel[ch].out[0];
278 }
279 if (fm.channel[i].out[1]!=NULL) {
280 chOut+=*fm.channel[ch].out[1];
281 }
282 if (fm.channel[i].out[2]!=NULL) {
283 chOut+=*fm.channel[ch].out[2];
284 }
285 if (fm.channel[i].out[3]!=NULL) {
286 chOut+=*fm.channel[ch].out[3];
287 }
288 oscBuf[i]->putSample(h,CLAMP(chOut<<(i==melodicChans?1:2),-32768,32767));
289 }
290 // special
291 oscBuf[melodicChans+1]->putSample(h,fm.slot[16].out*4);
292 oscBuf[melodicChans+2]->putSample(h,fm.slot[14].out*4);
293 oscBuf[melodicChans+3]->putSample(h,fm.slot[17].out*4);
294 oscBuf[melodicChans+4]->putSample(h,fm.slot[13].out*4);
295 } else {
296 for (int i=0; i<chans; i++) {
297 unsigned char ch=outChanMap[i];
298 int chOut=0;
299 if (ch==255) continue;
300 if (isMuted[i]) continue;
301 if (fm.channel[i].out[0]!=NULL) {
302 chOut+=*fm.channel[ch].out[0];
303 }
304 if (fm.channel[i].out[1]!=NULL) {
305 chOut+=*fm.channel[ch].out[1];
306 }
307 if (fm.channel[i].out[2]!=NULL) {
308 chOut+=*fm.channel[ch].out[2];
309 }
310 if (fm.channel[i].out[3]!=NULL) {
311 chOut+=*fm.channel[ch].out[3];
312 }
313 oscBuf[i]->putSample(h,CLAMP(chOut<<2,-32768,32767));
314 }
315 }
316
317 if (chipType==4) {
318 for (int i=pcmChanOffs; i<pcmChanOffs+24; i++) {
319 oscBuf[i]->putSample(h,CLAMP(pcmBuf[i-pcmChanOffs],-32768,32767));
320 }
321 }
322
323 if (os[0]<-32768) os[0]=-32768;
324 if (os[0]>32767) os[0]=32767;
325
326 if (os[1]<-32768) os[1]=-32768;
327 if (os[1]>32767) os[1]=32767;
328
329 if (os[2]<-32768) os[2]=-32768;
330 if (os[2]>32767) os[2]=32767;
331
332 if (os[3]<-32768) os[3]=-32768;
333 if (os[3]>32767) os[3]=32767;
334
335 if (os[4]<-32768) os[4]=-32768;
336 if (os[4]>32767) os[4]=32767;
337
338 if (os[5]<-32768) os[5]=-32768;
339 if (os[5]>32767) os[5]=32767;
340
341 buf[0][h]=os[0];
342 if (totalOutputs>1) {
343 buf[1][h]=os[1];
344 }
345 if (totalOutputs>2) {
346 buf[2][h]=os[2];
347 }
348 if (totalOutputs>3) {
349 buf[3][h]=os[3];
350 }
351 if (totalOutputs==6) {
352 buf[4][h]=os[4];
353 buf[5][h]=os[5];
354 }
355 }
356
357 for (int i=0; i<MAX(adpcmChan+1,totalChans); i++) {
358 oscBuf[i]->end(len);
359 }
360 }
361
362 void DivPlatformOPL::acquire_ymfm1(short** buf, size_t len) {
363 ymfm::ymfm_output<1> out;
364
365 ymfm::ym3526::fm_engine* fme=fm_ymfm1->debug_fm_engine();
366 ymfm::fm_channel<ymfm::opl_registers_base<1>>* fmChan[9];
367
368 for (int i=0; i<9; i++) {
369 fmChan[i]=fme->debug_channel(i);
370 }
371
372 for (int i=0; i<totalChans; i++) {
373 oscBuf[i]->begin(len);
374 }
375
376 for (size_t h=0; h<len; h++) {
377 if (!writes.empty() && --delay<0) {
378 QueuedWrite& w=writes.front();
379 if (w.addr==0xfffffffe) {
380 delay=w.val;
381 } else {
382 fm_ymfm1->write(0,w.addr);
383 fm_ymfm1->write(1,w.val);
384 delay=1;
385
386 regPool[w.addr&511]=w.val;
387 }
388 writes.pop();
389 }
390
391 fm_ymfm1->generate(&out,1);
392
393 buf[0][h]=out.data[0];
394
395 if (properDrums) {
396 for (int i=0; i<7; i++) {
397 if (isMuted[i]) continue;
398 oscBuf[i]->putSample(h,CLAMP(fmChan[i]->debug_output(0)<<2,-32768,32767));
399 }
400 oscBuf[7]->putSample(h,CLAMP(fmChan[7]->debug_special1()<<2,-32768,32767));
401 oscBuf[8]->putSample(h,CLAMP(fmChan[8]->debug_special1()<<2,-32768,32767));
402 oscBuf[9]->putSample(h,CLAMP(fmChan[8]->debug_special2()<<2,-32768,32767));
403 oscBuf[10]->putSample(h,CLAMP(fmChan[7]->debug_special2()<<2,-32768,32767));
404 } else {
405 for (int i=0; i<9; i++) {
406 if (isMuted[i]) continue;
407 oscBuf[i]->putSample(h,CLAMP(fmChan[i]->debug_output(0)<<2,-32768,32767));
408 }
409 }
410 }
411
412 for (int i=0; i<totalChans; i++) {
413 oscBuf[i]->end(len);
414 }
415 }
416
417 void DivPlatformOPL::acquire_ymfm2(short** buf, size_t len) {
418 ymfm::ymfm_output<1> out;
419
420 ymfm::ym3812::fm_engine* fme=fm_ymfm2->debug_fm_engine();
421 ymfm::fm_channel<ymfm::opl_registers_base<2>>* fmChan[9];
422
423 for (int i=0; i<9; i++) {
424 fmChan[i]=fme->debug_channel(i);
425 }
426
427 for (int i=0; i<totalChans; i++) {
428 oscBuf[i]->begin(len);
429 }
430
431 for (size_t h=0; h<len; h++) {
432 if (!writes.empty() && --delay<0) {
433 QueuedWrite& w=writes.front();
434 if (w.addr==0xfffffffe) {
435 delay=w.val;
436 } else {
437 fm_ymfm2->write(0,w.addr);
438 fm_ymfm2->write(1,w.val);
439 delay=1;
440
441 regPool[w.addr&511]=w.val;
442 }
443 writes.pop();
444 }
445
446 fm_ymfm2->generate(&out,1);
447
448 buf[0][h]=out.data[0];
449
450 if (properDrums) {
451 for (int i=0; i<7; i++) {
452 if (isMuted[i]) continue;
453 oscBuf[i]->putSample(h,CLAMP(fmChan[i]->debug_output(0)<<2,-32768,32767));
454 }
455 oscBuf[7]->putSample(h,CLAMP(fmChan[7]->debug_special1()<<2,-32768,32767));
456 oscBuf[8]->putSample(h,CLAMP(fmChan[8]->debug_special1()<<2,-32768,32767));
457 oscBuf[9]->putSample(h,CLAMP(fmChan[8]->debug_special2()<<2,-32768,32767));
458 oscBuf[10]->putSample(h,CLAMP(fmChan[7]->debug_special2()<<2,-32768,32767));
459 } else {
460 for (int i=0; i<9; i++) {
461 if (isMuted[i]) continue;
462 oscBuf[i]->putSample(h,CLAMP(fmChan[i]->debug_output(0)<<2,-32768,32767));
463 }
464 }
465 }
466
467 for (int i=0; i<totalChans; i++) {
468 oscBuf[i]->end(len);
469 }
470 }
471
472 void DivPlatformOPL::acquire_ymfm8950(short** buf, size_t len) {
473 ymfm::ymfm_output<1> out;
474
475 ymfm::y8950::fm_engine* fme=fm_ymfm8950->debug_fm_engine();
476 ymfm::adpcm_b_engine* abe=fm_ymfm8950->debug_adpcm_b_engine();
477 ymfm::fm_channel<ymfm::opl_registers_base<1>>* fmChan[9];
478
479 for (int i=0; i<9; i++) {
480 fmChan[i]=fme->debug_channel(i);
481 }
482
483 for (int i=0; i<totalChans+1; i++) {
484 oscBuf[i]->begin(len);
485 }
486
487 for (size_t h=0; h<len; h++) {
488 if (!writes.empty() && --delay<0) {
489 QueuedWrite& w=writes.front();
490 if (w.addr==0xfffffffe) {
491 delay=w.val;
492 } else {
493 fm_ymfm8950->write(0,w.addr);
494 fm_ymfm8950->write(1,w.val);
495 delay=1;
496
497 regPool[w.addr&511]=w.val;
498 }
499 writes.pop();
500 }
501
502 fm_ymfm8950->generate(&out,1);
503
504 buf[0][h]=out.data[0];
505
506 if (properDrums) {
507 for (int i=0; i<7; i++) {
508 if (isMuted[i]) continue;
509 oscBuf[i]->putSample(h,CLAMP(fmChan[i]->debug_output(0)<<2,-32768,32767));
510 }
511 oscBuf[7]->putSample(h,CLAMP(fmChan[7]->debug_special1()<<2,-32768,32767));
512 oscBuf[8]->putSample(h,CLAMP(fmChan[8]->debug_special1()<<2,-32768,32767));
513 oscBuf[9]->putSample(h,CLAMP(fmChan[8]->debug_special2()<<2,-32768,32767));
514 oscBuf[10]->putSample(h,CLAMP(fmChan[7]->debug_special2()<<2,-32768,32767));
515 oscBuf[11]->putSample(h,CLAMP(abe->get_last_out(0)<<2,-32768,32767));
516 } else {
517 for (int i=0; i<9; i++) {
518 if (isMuted[i]) continue;
519 oscBuf[i]->putSample(h,CLAMP(fmChan[i]->debug_output(0)<<2,-32768,32767));
520 }
521 oscBuf[9]->putSample(h,CLAMP(abe->get_last_out(0)<<2,-32768,32767));
522 }
523 }
524
525 for (int i=0; i<totalChans+1; i++) {
526 oscBuf[i]->end(len);
527 }
528 }
529
530 void DivPlatformOPL::acquire_ymfm3(short** buf, size_t len) {
531 ymfm::ymfm_output<4> out;
532
533 ymfm::ymf262::fm_engine* fme=fm_ymfm3->debug_fm_engine();
534 ymfm::fm_channel<ymfm::opl_registers_base<3>>* fmChan[18];
535
536 for (int i=0; i<18; i++) {
537 fmChan[i]=fme->debug_channel(i);
538 }
539
540 for (int i=0; i<totalChans; i++) {
541 oscBuf[i]->begin(len);
542 }
543
544 for (size_t h=0; h<len; h++) {
545 if (!writes.empty() && --delay<0) {
546 QueuedWrite& w=writes.front();
547 if (w.addr==0xfffffffe) {
548 delay=w.val;
549 } else {
550 fm_ymfm3->write((w.addr&0x100)?2:0,w.addr);
551 fm_ymfm3->write(1,w.val);
552 delay=1;
553
554 regPool[w.addr&511]=w.val;
555 }
556 writes.pop();
557 }
558
559 fm_ymfm3->generate(&out,1);
560
561 if (downsample) {
562 // 49716-44100
563 downsamplerStep+=5616;
564 if (downsamplerStep>=44100) {
565 downsamplerStep-=44100;
566 h--;
567 continue;
568 }
569 }
570
571 buf[0][h]=out.data[0]>>1;
572 if (totalOutputs>1) {
573 buf[1][h]=out.data[1]>>1;
574 }
575 if (totalOutputs>2) {
576 buf[2][h]=out.data[2]>>1;
577 }
578 if (totalOutputs>3) {
579 buf[3][h]=out.data[3]>>1;
580 }
581 if (totalOutputs==6) {
582 // placeholder for OPL4
583 buf[4][h]=0;
584 buf[5][h]=0;
585 }
586
587 if (properDrums) {
588 for (int i=0; i<16; i++) {
589 unsigned char ch=(i<12 && chan[i&(~1)].fourOp)?outChanMap[i^1]:outChanMap[i];
590 if (ch==255) continue;
591 if (isMuted[i]) continue;
592 int chOut=fmChan[ch]->debug_output(0)+fmChan[ch]->debug_output(1);
593 if (chOut==0) {
594 chOut=fmChan[ch]->debug_output(2);
595 }
596 if (chOut==0) {
597 chOut=fmChan[ch]->debug_output(3);
598 }
599 if (i==15) {
600 oscBuf[i]->putSample(h,CLAMP(chOut>>1,-32768,32767));
601 } else {
602 oscBuf[i]->putSample(h,CLAMP(chOut,-32768,32767));
603 }
604 }
605 oscBuf[16]->putSample(h,CLAMP(fmChan[7]->debug_special2()<<1,-32768,32767));
606 oscBuf[17]->putSample(h,CLAMP(fmChan[8]->debug_special1()<<1,-32768,32767));
607 oscBuf[18]->putSample(h,CLAMP(fmChan[8]->debug_special2()<<1,-32768,32767));
608 oscBuf[19]->putSample(h,CLAMP(fmChan[7]->debug_special1()<<1,-32768,32767));
609 } else {
610 for (int i=0; i<18; i++) {
611 unsigned char ch=outChanMap[i];
612 if (ch==255) continue;
613 if (isMuted[i]) continue;
614 int chOut=fmChan[ch]->debug_output(0)+fmChan[ch]->debug_output(1);
615 if (chOut==0) {
616 chOut=fmChan[ch]->debug_output(2);
617 }
618 if (chOut==0) {
619 chOut=fmChan[ch]->debug_output(3);
620 }
621 oscBuf[i]->putSample(h,CLAMP(chOut,-32768,32767));
622 }
623 }
624 }
625
626 for (int i=0; i<totalChans; i++) {
627 oscBuf[i]->end(len);
628 }
629 }
630
631 void DivPlatformOPL::acquire_ymfm4(short** buf, size_t len) {
632 ymfm::ymfm_output<6> out;
633
634 ymfm::ymf278b::fm_engine* fme=fm_ymfm4->debug_fm_engine();
635 ymfm::pcm_engine* pcme=fm_ymfm4->debug_pcm_engine();
636 ymfm::fm_channel<ymfm::opl_registers_base<4>>* fmChan[18];
637 ymfm::pcm_channel* pcmChan[24];
638
639 for (int i=0; i<18; i++) {
640 fmChan[i]=fme->debug_channel(i);
641 }
642
643 for (int i=0; i<24; i++) {
644 pcmChan[i]=pcme->debug_channel(i);
645 }
646
647 for (int i=0; i<totalChans; i++) {
648 oscBuf[i]->begin(len);
649 }
650
651 for (size_t h=0; h<len; h++) {
652 if (!writes.empty() && --delay<0) {
653 QueuedWrite& w=writes.front();
654 if (w.addr==0xfffffffe) {
655 delay=w.val;
656 } else {
657 fm_ymfm4->write((w.addr&0x200)?4:(w.addr&0x100)?2:0,w.addr);
658 fm_ymfm4->write((w.addr&0x200)?5:1,w.val);
659 delay=1;
660
661 regPool[(w.addr&0x200)?(0x200+(w.addr&255)):(w.addr&511)]=w.val;
662 }
663 writes.pop();
664 }
665
666 fm_ymfm4->generate(&out,1);
667
668 buf[0][h]=out.data[4]>>1; // FM + PCM left
669 if (totalOutputs>1) {
670 buf[1][h]=out.data[5]>>1; // FM + PCM right
671 }
672 if (totalOutputs>2) {
673 buf[2][h]=out.data[0]>>1; // FM left
674 }
675 if (totalOutputs>3) {
676 buf[3][h]=out.data[1]>>1; // FM right
677 }
678 if (totalOutputs==6) {
679 buf[4][h]=out.data[2]>>1; // PCM left
680 buf[5][h]=out.data[3]>>1; // PCM right
681 }
682
683 if (properDrums) {
684 for (int i=0; i<16; i++) {
685 unsigned char ch=(i<12 && chan[i&(~1)].fourOp)?outChanMap[i^1]:outChanMap[i];
686 if (ch==255) continue;
687 if (isMuted[i]) continue;
688 int chOut=fmChan[ch]->debug_output(0);
689 if (chOut==0) {
690 chOut=fmChan[ch]->debug_output(1);
691 }
692 if (chOut==0) {
693 chOut=fmChan[ch]->debug_output(2);
694 }
695 if (chOut==0) {
696 chOut=fmChan[ch]->debug_output(3);
697 }
698 if (i==15) {
699 oscBuf[i]->putSample(h,CLAMP(chOut,-32768,32767));
700 } else {
701 oscBuf[i]->putSample(h,CLAMP(chOut<<1,-32768,32767));
702 }
703 }
704 oscBuf[16]->putSample(h,CLAMP(fmChan[7]->debug_special2()<<1,-32768,32767));
705 oscBuf[17]->putSample(h,CLAMP(fmChan[8]->debug_special1()<<1,-32768,32767));
706 oscBuf[18]->putSample(h,CLAMP(fmChan[8]->debug_special2()<<1,-32768,32767));
707 oscBuf[19]->putSample(h,CLAMP(fmChan[7]->debug_special1()<<1,-32768,32767));
708 } else {
709 for (int i=0; i<18; i++) {
710 unsigned char ch=outChanMap[i];
711 if (ch==255) continue;
712 if (isMuted[i]) continue;
713 int chOut=fmChan[ch]->debug_output(0);
714 if (chOut==0) {
715 chOut=fmChan[ch]->debug_output(1);
716 }
717 if (chOut==0) {
718 chOut=fmChan[ch]->debug_output(2);
719 }
720 if (chOut==0) {
721 chOut=fmChan[ch]->debug_output(3);
722 }
723 oscBuf[i]->putSample(h,CLAMP(chOut<<1,-32768,32767));
724 }
725 }
726 for (int i=0; i<24; i++) {
727 unsigned char oscOffs=i+pcmChanOffs;
728 int chOut=pcmChan[i]->debug_output(0);
729 chOut+=pcmChan[i]->debug_output(1);
730 chOut+=pcmChan[i]->debug_output(2);
731 chOut+=pcmChan[i]->debug_output(3);
732 oscBuf[oscOffs]->putSample(h,CLAMP(chOut<<1,-32768,32767));
733 }
734 }
735
736 for (int i=0; i<totalChans; i++) {
737 oscBuf[i]->end(len);
738 }
739 }
740
741 static const int cycleMap[18]={
742 6, 7, 8, 6, 7, 8, 0, 1, 2,
743 0, 1, 2, 3, 4, 5, 3, 4, 5,
744 };
745
746 static const int cycleMapDrums[18]={
747 6, 10, 8, 6, 7, 9, 0, 1, 2,
748 0, 1, 2, 3, 4, 5, 3, 4, 5,
749 };
750
751 static const int cycleMap3[36]={
752 14, 12, 13, 14, 0, 2, 4, 0, 2,
753 4, 1, 3, 5, 1, 3, 5, 15, 16,
754 17, 15, 16, 17, 6, 8, 10, 6, 8, 10, 7, 9, 11, 7, 9, 11, 12, 13
755 };
756
757 static const int cycleMap3Drums[36]={
758 14, 12, 13, 14, 0, 2, 4, 0, 2,
759 4, 1, 3, 5, 1, 3, 5, 15, 19,
760 17, 15, 16, 18, 6, 8, 10, 6, 8, 10, 7, 9, 11, 7, 9, 11, 12, 13
761 };
762
763 void DivPlatformOPL::acquire_nukedLLE2(short** buf, size_t len) {
764 int chOut[11];
765 thread_local ymfm::ymfm_output<2> aOut;
766
767 for (int i=0; i<MAX(adpcmChan+1,totalChans); i++) {
768 oscBuf[i]->begin(len);
769 }
770
771 for (size_t h=0; h<len; h++) {
772 int curCycle=0;
773 unsigned char subCycle=0;
774
775 for (int i=0; i<11; i++) {
776 chOut[i]=0;
777 }
778
779 while (true) {
780 lastSH=fm_lle2.o_sh;
781 lastSY=fm_lle2.o_sy;
782
783 // register control
784 if (waitingBusy) {
785 fm_lle2.input.cs=0;
786 fm_lle2.input.rd=0;
787 fm_lle2.input.wr=1;
788 fm_lle2.input.address=0;
789 } else {
790 if (!writes.empty()) {
791 QueuedWrite& w=writes.front();
792
793 if (w.addrOrVal) {
794 regPool[w.addr&511]=w.val;
795 fm_lle2.input.cs=0;
796 fm_lle2.input.rd=1;
797 fm_lle2.input.wr=0;
798 fm_lle2.input.address=1;
799 fm_lle2.input.data_i=w.val;
800 writes.pop();
801 delay=84;
802 } else {
803 if (chipType==8950) {
804 switch (w.addr) {
805 case 8:
806 adpcmB->write(w.addr-7,(w.val&15)|0x80);
807 fm_lle2.input.cs=0;
808 fm_lle2.input.rd=1;
809 fm_lle2.input.wr=0;
810 fm_lle2.input.address=0;
811 fm_lle2.input.data_i=w.addr;
812 w.addrOrVal=true;
813 // weird. wasn't it 12?
814 delay=24;
815 break;
816 case 7: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 21: case 22: case 23:
817 adpcmB->write(w.addr-7,w.val);
818 regPool[w.addr&511]=w.val;
819 writes.pop();
820 delay=108;
821 break;
822 default:
823 fm_lle2.input.cs=0;
824 fm_lle2.input.rd=1;
825 fm_lle2.input.wr=0;
826 fm_lle2.input.address=0;
827 fm_lle2.input.data_i=w.addr;
828 w.addrOrVal=true;
829 // weird. wasn't it 12?
830 delay=24;
831 break;
832 }
833 } else {
834 fm_lle2.input.cs=0;
835 fm_lle2.input.rd=1;
836 fm_lle2.input.wr=0;
837 fm_lle2.input.address=0;
838 fm_lle2.input.data_i=w.addr;
839 w.addrOrVal=true;
840 // weird. wasn't it 12?
841 delay=24;
842 }
843 }
844
845 waitingBusy=true;
846 }
847 }
848
849 fm_lle2.input.mclk=1;
850 FMOPL2_Clock(&fm_lle2);
851 fm_lle2.input.mclk=0;
852 FMOPL2_Clock(&fm_lle2);
853
854 if (waitingBusy) {
855 if (--delay<0) waitingBusy=false;
856 }
857
858 if (!(++subCycle&3)) {
859 if (properDrums) {
860 chOut[cycleMapDrums[curCycle]]+=fm_lle2.op_value_debug;
861 } else {
862 chOut[cycleMap[curCycle]]+=fm_lle2.op_value_debug;
863 }
864 curCycle++;
865 }
866
867 if (fm_lle2.o_sy && !lastSY) {
868 dacVal>>=1;
869 dacVal|=(fm_lle2.o_mo&1)<<17;
870 }
871
872 if (!fm_lle2.o_sh && lastSH) {
873 int e=(dacVal>>15)&7;
874 int m=(dacVal>>5)&1023;
875 m-=512;
876 dacOut=(m<<e)>>1;
877 break;
878 }
879 }
880
881 for (int i=0; i<11; i++) {
882 if (isMuted[i]) continue;
883 if (i>=6 && properDrums) {
884 chOut[i]<<=1;
885 } else {
886 chOut[i]<<=2;
887 }
888 if (chOut[i]<-32768) chOut[i]=-32768;
889 if (chOut[i]>32767) chOut[i]=32767;
890 oscBuf[i]->putSample(h,chOut[i]);
891 }
892
893 if (chipType==8950) {
894 adpcmB->clock();
895 aOut.clear();
896 adpcmB->output<2>(aOut,0);
897
898 if (!isMuted[adpcmChan]) {
899 dacOut-=aOut.data[0]>>3;
900 oscBuf[adpcmChan]->putSample(h,aOut.data[0]>>1);
901 } else {
902 oscBuf[adpcmChan]->putSample(h,0);
903 }
904 }
905
906 if (dacOut<-32768) dacOut=-32768;
907 if (dacOut>32767) dacOut=32767;
908
909 buf[0][h]=dacOut;
910 }
911
912 for (int i=0; i<MAX(adpcmChan+1,totalChans); i++) {
913 oscBuf[i]->end(len);
914 }
915 }
916
917 void DivPlatformOPL::acquire_nukedLLE3(short** buf, size_t len) {
918 int chOut[20];
919 int ch=0;
920
921 for (int i=0; i<totalChans; i++) {
922 oscBuf[i]->begin(len);
923 }
924
925 for (size_t h=0; h<len; h++) {
926 int curCycle=0;
927 unsigned char subCycle=0;
928
929 for (int i=0; i<20; i++) {
930 chOut[i]=0;
931 }
932
933 while (true) {
934 lastSH=fm_lle3.o_smpac;
935 lastSH2=fm_lle3.o_smpbd;
936 lastSY=fm_lle3.o_sy;
937
938 // register control
939 if (waitingBusy) {
940 if (delay<15) {
941 fm_lle3.input.cs=0;
942 fm_lle3.input.rd=0;
943 fm_lle3.input.wr=1;
944 fm_lle3.input.address=0;
945 }
946 } else {
947 if (!writes.empty()) {
948 QueuedWrite& w=writes.front();
949
950 if (w.addrOrVal) {
951 regPool[w.addr&511]=w.val;
952 fm_lle3.input.cs=0;
953 fm_lle3.input.rd=1;
954 fm_lle3.input.wr=0;
955 fm_lle3.input.address=(w.addr&0x100)?3:1;
956 fm_lle3.input.data_i=w.val;
957 writes.pop();
958 delay=18;
959 } else {
960 fm_lle3.input.cs=0;
961 fm_lle3.input.rd=1;
962 fm_lle3.input.wr=0;
963 fm_lle3.input.address=(w.addr&0x100)?2:0;
964 fm_lle3.input.data_i=w.addr&0xff;
965 w.addrOrVal=true;
966 // weird. wasn't it 12?
967 delay=18;
968 }
969
970 waitingBusy=true;
971 }
972 }
973
974 fm_lle3.input.mclk=1;
975 FMOPL3_Clock(&fm_lle3);
976 fm_lle3.input.mclk=0;
977 FMOPL3_Clock(&fm_lle3);
978
979 if (waitingBusy) {
980 if (--delay<0) waitingBusy=false;
981 }
982
983 if (!(++subCycle&1)) {
984 if (properDrums) {
985 ch=cycleMap3Drums[curCycle];
986 } else {
987 ch=cycleMap3[curCycle];
988 }
989
990 if (ch<12 && !(ch&1) && chan[ch&(~1)].state.alg>0) ch|=1;
991
992 if (ch>=12 || (ch&1) || !chan[ch&(~1)].fourOp) {
993 if (fm_lle3.op_value_debug&0x1000) {
994 chOut[ch]+=(fm_lle3.op_value_debug|0xfffff000)<<1;
995 } else {
996 chOut[ch]+=(fm_lle3.op_value_debug)<<1;
997 }
998 }
999 curCycle++;
1000 }
1001
1002 if (fm_lle3.o_sy && !lastSY) {
1003 dacVal>>=1;
1004 dacVal|=(fm_lle3.o_doab&1)<<17;
1005 dacVal2>>=1;
1006 dacVal2|=(fm_lle3.o_docd&1)<<17;
1007 }
1008
1009 if (!fm_lle3.o_smpbd && lastSH2) {
1010 dacOut3[0]=((dacVal>>1)&0xffff)-0x8000;
1011 dacOut3[2]=((dacVal2>>1)&0xffff)-0x8000;
1012 }
1013
1014 if (!fm_lle3.o_smpac && lastSH) {
1015 dacOut3[1]=((dacVal>>1)&0xffff)-0x8000;
1016 dacOut3[3]=((dacVal2>>1)&0xffff)-0x8000;
1017 break;
1018 }
1019 }
1020
1021 for (int i=0; i<20; i++) {
1022 if (isMuted[i]) continue;
1023 if (chOut[i]<-32768) chOut[i]=-32768;
1024 if (chOut[i]>32767) chOut[i]=32767;
1025 oscBuf[i]->putSample(h,chOut[i]);
1026 }
1027
1028 for (int i=0; i<MIN(4,totalOutputs); i++) {
1029 if (dacOut3[i]<-32768) dacOut3[i]=-32768;
1030 if (dacOut3[i]>32767) dacOut3[i]=32767;
1031
1032 buf[i][h]=dacOut3[i];
1033 }
1034 }
1035
1036 for (int i=0; i<totalChans; i++) {
1037 oscBuf[i]->end(len);
1038 }
1039 }
1040
1041 void DivPlatformOPL::acquire(short** buf, size_t len) {
1042 if (emuCore==2) { // LLE
1043 switch (chipType) {
1044 case 1: case 2: case 8950:
1045 acquire_nukedLLE2(buf,len);
1046 break;
1047 case 3: case 759:
1048 acquire_nukedLLE3(buf,len);
1049 break;
1050 }
1051 } else if (emuCore==1) { // ymfm
1052 switch (chipType) {
1053 case 1:
1054 acquire_ymfm1(buf,len);
1055 break;
1056 case 2:
1057 acquire_ymfm2(buf,len);
1058 break;
1059 case 8950:
1060 acquire_ymfm8950(buf,len);
1061 break;
1062 case 3: case 759:
1063 acquire_ymfm3(buf,len);
1064 break;
1065 case 4:
1066 acquire_ymfm4(buf,len);
1067 break;
1068 }
1069 } else { // OPL3
1070 acquire_nuked(buf,len);
1071 }
1072 }
1073
1074 double DivPlatformOPL::NOTE_ADPCMB(int note) {
1075 if (adpcmChan<0) return 0;
1076 if (chan[adpcmChan].sample>=0 && chan[adpcmChan].sample<parent->song.sampleLen) {
1077 double off=65535.0*(double)(parent->getSample(chan[adpcmChan].sample)->centerRate)/parent->getCenterRate();
1078 return parent->calcBaseFreq((double)chipClock/(compatYPitch?144:72),off,note,false);
1079 }
1080 return 0;
1081 }
1082
1083 void DivPlatformOPL::tick(bool sysTick) {
1084 for (int i=0; i<totalChans; i++) {
1085 if (PCM_CHECK(i)) { // OPL4 PCM
1086 chan[i].std.next();
1087 if (chan[i].std.vol.had) {
1088 chan[i].outVol=VOL_SCALE_LOG((chan[i].vol&0x7f),(0x7f*chan[i].std.vol.val)/chan[i].macroVolMul,0x7f);
1089 immWrite(PCM_ADDR_TL+(PCM_REG(i)),((0x7f-chan[i].outVol)<<1)|(chan[i].levelDirect?1:0));
1090 }
1091
1092 if (NEW_ARP_STRAT) {
1093 chan[i].handleArp();
1094 } else if (chan[i].std.arp.had) {
1095 if (!chan[i].inPorta) {
1096 chan[i].baseFreq=NOTE_PCM(parent->calcArp(chan[i].note,chan[i].std.arp.val));
1097 }
1098 chan[i].freqChanged=true;
1099 }
1100
1101 if (chan[i].std.pitch.had) {
1102 if (chan[i].std.pitch.mode) {
1103 chan[i].pitch2+=chan[i].std.pitch.val;
1104 CLAMP_VAR(chan[i].pitch2,-131071,131071);
1105 } else {
1106 chan[i].pitch2=chan[i].std.pitch.val;
1107 }
1108 chan[i].freqChanged=true;
1109 }
1110
1111 if (chan[i].std.phaseReset.had) {
1112 if (chan[i].std.phaseReset.val==1 && chan[i].active) {
1113 chan[i].keyOn=true;
1114 chan[i].writeCtrl=true;
1115 }
1116 }
1117
1118 if (chan[i].std.panL.had) { // panning
1119 chan[i].pan=chan[i].std.panL.val&0xf;
1120 chan[i].freqChanged=true;
1121 chan[i].writeCtrl=true;
1122 }
1123
1124 if (chan[i].std.ex1.had) {
1125 chan[i].lfo=chan[i].std.ex1.val&0x7;
1126 rWrite(PCM_ADDR_LFO_VIB+PCM_REG(i),(chan[i].lfo<<3)|(chan[i].vib));
1127 }
1128
1129 if (chan[i].std.fms.had) {
1130 chan[i].vib=chan[i].std.fms.val&0x7;
1131 rWrite(PCM_ADDR_LFO_VIB+PCM_REG(i),(chan[i].lfo<<3)|(chan[i].vib));
1132 }
1133
1134 if (chan[i].std.ams.had) {
1135 chan[i].am=chan[i].std.ams.val&0x7;
1136 rWrite(PCM_ADDR_AM+PCM_REG(i),chan[i].am);
1137 }
1138
1139 if (chan[i].std.ex2.had) {
1140 chan[i].ar=chan[i].std.ex2.val&0xf;
1141 rWrite(PCM_ADDR_AR_D1R+PCM_REG(i),(chan[i].ar<<4)|(chan[i].d1r));
1142 }
1143
1144 if (chan[i].std.ex3.had) {
1145 chan[i].d1r=chan[i].std.ex3.val&0xf;
1146 rWrite(PCM_ADDR_AR_D1R+PCM_REG(i),(chan[i].ar<<4)|(chan[i].d1r));
1147 }
1148
1149 if (chan[i].std.ex4.had) {
1150 chan[i].dl=chan[i].std.ex4.val&0xf;
1151 rWrite(PCM_ADDR_DL_D2R+PCM_REG(i),(chan[i].dl<<4)|(chan[i].d2r));
1152 }
1153
1154 if (chan[i].std.ex5.had) {
1155 chan[i].d2r=chan[i].std.ex5.val&0xf;
1156 rWrite(PCM_ADDR_DL_D2R+PCM_REG(i),(chan[i].dl<<4)|(chan[i].d2r));
1157 }
1158
1159 if (chan[i].std.ex6.had) {
1160 chan[i].rc=chan[i].std.ex6.val&0xf;
1161 rWrite(PCM_ADDR_RC_RR+PCM_REG(i),(chan[i].rc<<4)|(chan[i].rr));
1162 }
1163
1164 if (chan[i].std.ex7.had) {
1165 chan[i].rr=chan[i].std.ex7.val&0xf;
1166 rWrite(PCM_ADDR_RC_RR+PCM_REG(i),(chan[i].rc<<4)|(chan[i].rr));
1167 }
1168
1169 } else {
1170 int ops=(slots[3][i]!=255 && chan[i].state.ops==4 && oplType==3)?4:2;
1171 chan[i].std.next();
1172
1173 if (chan[i].std.vol.had) {
1174 chan[i].outVol=VOL_SCALE_LOG_BROKEN(chan[i].vol,MIN(63,chan[i].std.vol.val),63);
1175 for (int j=0; j<ops; j++) {
1176 unsigned char slot=slots[j][i];
1177 if (slot==255) continue;
1178 unsigned short baseAddr=slotMap[slot];
1179 DivInstrumentFM::Operator& op=chan[i].state.op[(ops==4)?orderedOpsL[j]:j];
1180
1181 if (isMuted[i]) {
1182 rWrite(baseAddr+ADDR_KSL_TL,63|(op.ksl<<6));
1183 } else {
1184 if (KVSL(i,j) || i>melodicChans) {
1185 rWrite(baseAddr+ADDR_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[i].outVol&0x3f,63))|(op.ksl<<6));
1186 } else {
1187 rWrite(baseAddr+ADDR_KSL_TL,op.tl|(op.ksl<<6));
1188 }
1189 }
1190 }
1191 }
1192
1193 if (NEW_ARP_STRAT) {
1194 chan[i].handleArp();
1195 } else if (chan[i].std.arp.had) {
1196 if (!chan[i].inPorta) {
1197 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
1198 }
1199 chan[i].freqChanged=true;
1200 }
1201
1202 if (oplType==3 && chan[i].std.panL.had) {
1203 chan[i].pan=((chan[i].std.panL.val&1)<<1)|((chan[i].std.panL.val&2)>>1)|((chan[i].std.panL.val&4)<<1)|((chan[i].std.panL.val&8)>>1);
1204 }
1205
1206 if (chan[i].std.pitch.had) {
1207 if (chan[i].std.pitch.mode) {
1208 chan[i].pitch2+=chan[i].std.pitch.val;
1209 CLAMP_VAR(chan[i].pitch2,-131071,131071);
1210 } else {
1211 chan[i].pitch2=chan[i].std.pitch.val;
1212 }
1213 chan[i].freqChanged=true;
1214 }
1215
1216 if (chan[i].std.phaseReset.had) {
1217 if (chan[i].std.phaseReset.val==1 && chan[i].active) {
1218 chan[i].keyOn=true;
1219 }
1220 }
1221
1222 if (chan[i].std.alg.had) {
1223 chan[i].state.alg=chan[i].std.alg.val;
1224 }
1225 if (chan[i].std.fb.had) {
1226 chan[i].state.fb=chan[i].std.fb.val;
1227 }
1228
1229 if (chan[i].std.alg.had || chan[i].std.fb.had || (oplType==3 && chan[i].std.panL.had)) {
1230 if (isMuted[i] && i<=melodicChans) {
1231 rWrite(chanMap[i]+ADDR_LR_FB_ALG,(chan[i].state.alg&1)|(chan[i].state.fb<<1));
1232 if (ops==4) {
1233 rWrite(chanMap[i+1]+ADDR_LR_FB_ALG,((chan[i].state.alg>>1)&1)|(chan[i].state.fb<<1));
1234 }
1235 } else {
1236 rWrite(chanMap[i]+ADDR_LR_FB_ALG,(chan[i].state.alg&1)|(chan[i].state.fb<<1)|((chan[i].pan&15)<<4));
1237 if (ops==4) {
1238 rWrite(chanMap[i+1]+ADDR_LR_FB_ALG,((chan[i].state.alg>>1)&1)|(chan[i].state.fb<<1)|((chan[i].pan&15)<<4));
1239 }
1240 }
1241 }
1242
1243 for (int j=0; j<ops; j++) {
1244 unsigned char slot=slots[j][i];
1245 if (slot==255) continue;
1246 unsigned short baseAddr=slotMap[slot];
1247 DivInstrumentFM::Operator& op=chan[i].state.op[(ops==4)?orderedOpsL[j]:j];
1248 DivMacroInt::IntOp& m=chan[i].std.op[(ops==4)?orderedOpsL[j]:j];
1249 if (m.am.had) {
1250 op.am=m.am.val;
1251 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
1252 }
1253 if (m.vib.had) {
1254 op.vib=m.vib.val;
1255 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
1256 }
1257 if (m.sus.had) {
1258 op.sus=m.sus.val;
1259 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
1260 }
1261 if (m.ksr.had) {
1262 op.ksr=m.ksr.val;
1263 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
1264 }
1265 if (m.mult.had) {
1266 op.mult=m.mult.val;
1267 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
1268 }
1269
1270 if (m.ar.had) {
1271 op.ar=m.ar.val;
1272 rWrite(baseAddr+ADDR_AR_DR,(op.ar<<4)|op.dr);
1273 }
1274 if (m.dr.had) {
1275 op.dr=m.dr.val;
1276 rWrite(baseAddr+ADDR_AR_DR,(op.ar<<4)|op.dr);
1277 }
1278 if (m.sl.had) {
1279 op.sl=m.sl.val;
1280 rWrite(baseAddr+ADDR_SL_RR,(op.sl<<4)|op.rr);
1281 }
1282 if (m.rr.had) {
1283 op.rr=m.rr.val;
1284 rWrite(baseAddr+ADDR_SL_RR,(op.sl<<4)|op.rr);
1285 }
1286
1287 if (oplType>1) {
1288 if (m.ws.had) {
1289 op.ws=m.ws.val;
1290 rWrite(baseAddr+ADDR_WS,op.ws&((oplType==3)?7:3));
1291 }
1292 }
1293
1294 if (m.tl.had) {
1295 op.tl=m.tl.val&63;
1296 }
1297 if (m.ksl.had) {
1298 op.ksl=m.ksl.val;
1299 }
1300 if (m.tl.had || m.ksl.had) {
1301 if (isMuted[i]) {
1302 rWrite(baseAddr+ADDR_KSL_TL,63|(op.ksl<<6));
1303 } else {
1304 if (KVSL(i,j) || i>melodicChans) {
1305 rWrite(baseAddr+ADDR_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[i].outVol&0x3f,63))|(op.ksl<<6));
1306 } else {
1307 rWrite(baseAddr+ADDR_KSL_TL,op.tl|(op.ksl<<6));
1308 }
1309 }
1310 }
1311 }
1312 }
1313 }
1314
1315 int hardResetElapsed=0;
1316 bool mustHardReset=false;
1317 bool weWillWriteRRLater[64];
1318
1319 memset(weWillWriteRRLater,0,64*sizeof(bool));
1320
1321 unsigned char opMask=(int)(chan[0].fourOp)|(chan[2].fourOp<<1)|(chan[4].fourOp<<2)|(chan[6].fourOp<<3)|(chan[8].fourOp<<4)|(chan[10].fourOp<<5);
1322
1323 // write ops which are being enabled
1324 if (update4OpMask) {
1325 if (oplType==3) {
1326 immWrite(0x104,opMask|oldOpMask);
1327 //printf("updating opMask to %.2x\n",opMask);
1328 }
1329 }
1330
1331 for (int i=0; i<melodicChans; i++) {
1332 int ops=(slots[3][i]!=255 && chan[i].state.ops==4 && oplType==3)?4:2;
1333
1334 if (chan[i].keyOn || chan[i].keyOff) {
1335 immWrite(chanMap[i]+ADDR_FREQH,0x00|(chan[i].freqH&31));
1336 chan[i].keyOff=false;
1337 }
1338 if (chan[i].hardReset && chan[i].keyOn) {
1339 mustHardReset=true;
1340 for (int j=0; j<ops; j++) {
1341 unsigned char slot=slots[j][i];
1342 if (slot==255) continue;
1343 unsigned short baseAddr=slotMap[slot];
1344 if (baseAddr>=0x100) {
1345 weWillWriteRRLater[(baseAddr&0xff)|32]=true;
1346 } else {
1347 weWillWriteRRLater[(baseAddr&0xff)]=true;
1348 }
1349 immWrite(baseAddr+ADDR_SL_RR,0x0f);
1350 hardResetElapsed++;
1351 }
1352 }
1353 }
1354
1355 // and now the ones being disabled
1356 if (update4OpMask) {
1357 update4OpMask=false;
1358 if (oplType==3) {
1359 immWrite(0x104,opMask);
1360 //printf("updating opMask to %.2x\n",opMask);
1361 }
1362 oldOpMask=opMask;
1363 }
1364
1365 // update drums
1366 if (properDrums) {
1367 bool updateDrums=false;
1368 for (int i=melodicChans; i<melodicChans+5; i++) {
1369 if (chan[i].keyOn || chan[i].keyOff) {
1370 drumState&=~(1<<(melodicChans+4-i));
1371 updateDrums=true;
1372 chan[i].keyOff=false;
1373 }
1374 }
1375
1376 if (updateDrums) {
1377 immWrite(0xbd,(dam<<7)|(dvb<<6)|(properDrums<<5)|drumState);
1378 }
1379 }
1380
1381 // ADPCM
1382 if (adpcmChan>=0) {
1383 if (chan[adpcmChan].furnacePCM) {
1384 chan[adpcmChan].std.next();
1385
1386 if (chan[adpcmChan].std.vol.had) {
1387 chan[adpcmChan].outVol=(chan[adpcmChan].vol*MIN(chan[adpcmChan].macroVolMul,chan[adpcmChan].std.vol.val))/chan[adpcmChan].macroVolMul;
1388 immWrite(18,(isMuted[adpcmChan]?0:chan[adpcmChan].outVol));
1389 }
1390
1391 if (NEW_ARP_STRAT) {
1392 chan[adpcmChan].handleArp();
1393 } else if (chan[adpcmChan].std.arp.had) {
1394 if (!chan[adpcmChan].inPorta) {
1395 chan[adpcmChan].baseFreq=NOTE_ADPCMB(parent->calcArp(chan[adpcmChan].note,chan[adpcmChan].std.arp.val));
1396 }
1397 chan[adpcmChan].freqChanged=true;
1398 }
1399 if (chan[adpcmChan].std.phaseReset.had) {
1400 if ((chan[adpcmChan].std.phaseReset.val==1) && chan[adpcmChan].active) {
1401 chan[adpcmChan].keyOn=true;
1402 }
1403 }
1404 }
1405 if (chan[adpcmChan].freqChanged || chan[adpcmChan].keyOn || chan[adpcmChan].keyOff) {
1406 if (chan[adpcmChan].sample>=0 && chan[adpcmChan].sample<parent->song.sampleLen) {
1407 double off=65535.0*(double)(parent->getSample(chan[adpcmChan].sample)->centerRate)/parent->getCenterRate();
1408 chan[adpcmChan].freq=parent->calcFreq(chan[adpcmChan].baseFreq,chan[adpcmChan].pitch,chan[adpcmChan].fixedArp?chan[adpcmChan].baseNoteOverride:chan[adpcmChan].arpOff,chan[adpcmChan].fixedArp,false,4,chan[adpcmChan].pitch2,(double)chipClock/(compatYPitch?144:72),off);
1409 } else {
1410 chan[adpcmChan].freq=0;
1411 }
1412 if (chan[adpcmChan].fixedFreq>0) chan[adpcmChan].freq=chan[adpcmChan].fixedFreq;
1413 if (pretendYMU) { // YMU759 only does 4KHz or 8KHz
1414 if (chan[adpcmChan].freq>7500) {
1415 chan[adpcmChan].freq=10922; // 8KHz
1416 } else {
1417 chan[adpcmChan].freq=5461; // 4KHz
1418 }
1419 }
1420 if (chan[adpcmChan].freq<0) chan[adpcmChan].freq=0;
1421 if (chan[adpcmChan].freq>65535) chan[adpcmChan].freq=65535;
1422 immWrite(16,chan[adpcmChan].freq&0xff);
1423 immWrite(17,(chan[adpcmChan].freq>>8)&0xff);
1424 if (chan[adpcmChan].keyOn || chan[adpcmChan].keyOff) {
1425 immWrite(7,0x01); // reset
1426 if (chan[adpcmChan].active && chan[adpcmChan].keyOn && !chan[adpcmChan].keyOff) {
1427 if (chan[adpcmChan].sample>=0 && chan[adpcmChan].sample<parent->song.sampleLen) {
1428 DivSample* s=parent->getSample(chan[adpcmChan].sample);
1429 immWrite(7,(s->isLoopable())?0xb0:0xa0); // start/repeat
1430 }
1431 }
1432 chan[adpcmChan].keyOn=false;
1433 chan[adpcmChan].keyOff=false;
1434 }
1435 chan[adpcmChan].freqChanged=false;
1436 }
1437 }
1438
1439 for (int i=0; i<768; i++) {
1440 if (pendingWrites[i]!=oldWrites[i]) {
1441 if ((i>=0x80 && i<0xa0)) {
1442 if (weWillWriteRRLater[i-0x80]) continue;
1443 } else if ((i>=0x180 && i<0x1a0)) {
1444 if (weWillWriteRRLater[32|(i-0x180)]) continue;
1445 }
1446 immWrite(i,pendingWrites[i]&0xff);
1447 oldWrites[i]=pendingWrites[i];
1448 }
1449 }
1450
1451 bool updateDrums=false;
1452 for (int i=0; i<totalChans; i++) {
1453 if (PCM_CHECK(i)) { // OPL4 PCM
1454 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
1455 DivSample* s=parent->getSample(chan[i].sample);
1456 unsigned char ctrl=0;
1457 double off=(s->centerRate>=1)?((double)s->centerRate/parent->getCenterRate()):1.0;
1458 chan[i].freq=(int)(off*parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,2,chan[i].pitch2,chipClock,(524288*768)));
1459 if (chan[i].freq<0x400) chan[i].freq=0x400;
1460 chan[i].freqH=0;
1461 if (chan[i].freq>0x3ffffff) {
1462 chan[i].freq=0x3ffffff;
1463 chan[i].freqH=15;
1464 } else if (chan[i].freq>=0x800) {
1465 chan[i].freqH=bsr32(chan[i].freq)-11;
1466 }
1467 chan[i].freqL=(chan[i].freq>>chan[i].freqH)&0x3ff;
1468 chan[i].freqH=8^chan[i].freqH;
1469 ctrl|=(chan[i].active?0x80:0)|(chan[i].damp?0x40:0)|(chan[i].lfoReset?0x20:0)|(chan[i].ch?0x10:0)|(isMuted[i]?8:(chan[i].pan&0xf));
1470 int waveNum=chan[i].sample;
1471 if (waveNum>=0) {
1472 if (PCM_IN_RAM) {
1473 waveNum=CLAMP(waveNum,0,0x7f)|0x180;
1474 }
1475 if (chan[i].keyOn) {
1476 immWrite(PCM_ADDR_KEY_DAMP_LFORST_CH_PAN+PCM_REG(i),ctrl&~0x80); // force keyoff first
1477 immWrite(PCM_ADDR_WAVE_H_FN_L+PCM_REG(i),((chan[i].freqL&0x7f)<<1)|((waveNum>>8)&1));
1478 immWrite(PCM_ADDR_WAVE_L+PCM_REG(i),waveNum&0xff);
1479 immWrite(PCM_ADDR_LFO_VIB+PCM_REG(i),(chan[i].lfo<<3)|(chan[i].vib));
1480 immWrite(PCM_ADDR_AR_D1R+PCM_REG(i),(chan[i].ar<<4)|(chan[i].d1r));
1481 immWrite(PCM_ADDR_DL_D2R+PCM_REG(i),(chan[i].dl<<4)|(chan[i].d2r));
1482 immWrite(PCM_ADDR_RC_RR+PCM_REG(i),(chan[i].rc<<4)|(chan[i].rr));
1483 immWrite(PCM_ADDR_AM+PCM_REG(i),chan[i].am);
1484 if (!chan[i].std.vol.had) {
1485 chan[i].outVol=chan[i].vol;
1486 immWrite(PCM_ADDR_TL+(PCM_REG(i)),((0x7f-chan[i].outVol)<<1)|(chan[i].levelDirect?1:0));
1487 }
1488 chan[i].writeCtrl=true;
1489 chan[i].keyOn=false;
1490 }
1491 if (chan[i].keyOff) {
1492 chan[i].writeCtrl=true;
1493 chan[i].keyOff=false;
1494 }
1495 if (chan[i].freqChanged) {
1496 immWrite(PCM_ADDR_WAVE_H_FN_L+PCM_REG(i),((chan[i].freqL&0x7f)<<1)|((waveNum>>8)&1));
1497 immWrite(PCM_ADDR_FN_H_PR_OCT+PCM_REG(i),((chan[i].freqH&0xf)<<4)|(chan[i].pseudoReverb?0x08:0x00)|((chan[i].freqL>>7)&0x7));
1498 chan[i].freqChanged=false;
1499 }
1500 if (chan[i].writeCtrl) {
1501 immWrite(PCM_ADDR_KEY_DAMP_LFORST_CH_PAN+PCM_REG(i),ctrl);
1502 chan[i].writeCtrl=false;
1503 }
1504 } else {
1505 // cut if we don't have a sample
1506 immWrite(PCM_ADDR_KEY_DAMP_LFORST_CH_PAN+PCM_REG(i),ctrl&~0x80);
1507 }
1508 }
1509 } else {
1510 if (chan[i].freqChanged) {
1511 int mul=2;
1512 int fixedBlock=chan[i].state.block;
1513 if (parent->song.linearPitch!=2) {
1514 mul=octave(chan[i].baseFreq,fixedBlock)*2;
1515 }
1516 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);
1517 if (chan[i].fixedFreq>0) chan[i].freq=chan[i].fixedFreq;
1518 if (chan[i].freq<0) chan[i].freq=0;
1519 if (chan[i].freq>131071) chan[i].freq=131071;
1520 int freqt=toFreq(chan[i].freq,fixedBlock);
1521 chan[i].freqH=freqt>>8;
1522 chan[i].freqL=freqt&0xff;
1523 immWrite(chanMap[i]+ADDR_FREQ,chan[i].freqL);
1524 }
1525 if (i<melodicChans) {
1526 if (chan[i].keyOn && !chan[i].hardReset) {
1527 immWrite(chanMap[i]+ADDR_FREQH,chan[i].freqH|(0x20));
1528 chan[i].keyOn=false;
1529 } else if (chan[i].freqChanged) {
1530 if (chan[i].keyOn && chan[i].hardReset) {
1531 immWrite(chanMap[i]+ADDR_FREQH,chan[i].freqH|0);
1532 } else {
1533 immWrite(chanMap[i]+ADDR_FREQH,chan[i].freqH|(chan[i].active<<5));
1534 }
1535 }
1536 } else {
1537 if (chan[i].keyOn) {
1538 immWrite(chanMap[i]+ADDR_FREQH,chan[i].freqH);
1539 if (!isMuted[i]) drumState|=(1<<(melodicChans+4-i));
1540 updateDrums=true;
1541 chan[i].keyOn=false;
1542 } else if (chan[i].freqChanged) {
1543 immWrite(chanMap[i]+ADDR_FREQH,chan[i].freqH);
1544 }
1545 }
1546 chan[i].freqChanged=false;
1547 }
1548 }
1549
1550 if (updateDrums) {
1551 immWrite(0xbd,(dam<<7)|(dvb<<6)|(properDrums<<5)|drumState);
1552 }
1553
1554 // hard reset handling
1555 if (mustHardReset) {
1556 immWrite(0xfffffffe,128-hardResetElapsed);
1557 for (int i=0x80; i<0xa0; i++) {
1558 if (weWillWriteRRLater[i-0x80]) {
1559 immWrite(i,pendingWrites[i]&0xff);
1560 oldWrites[i]=pendingWrites[i];
1561 }
1562 }
1563 for (int i=0x180; i<0x1a0; i++) {
1564 if (weWillWriteRRLater[32|(i-0x180)]) {
1565 immWrite(i,pendingWrites[i]&0xff);
1566 oldWrites[i]=pendingWrites[i];
1567 }
1568 }
1569 for (int i=0; i<melodicChans; i++) {
1570 if (chan[i].hardReset) {
1571 if (chan[i].keyOn) {
1572 immWrite(chanMap[i]+ADDR_FREQH,chan[i].freqH|(0x20));
1573 chan[i].keyOn=false;
1574 } else if (chan[i].freqChanged) {
1575 immWrite(chanMap[i]+ADDR_FREQH,chan[i].freqH|(chan[i].active<<5));
1576 }
1577 }
1578 }
1579 }
1580 }
1581
1582 #define OPLL_C_NUM 686
1583
1584 int DivPlatformOPL::octave(int freq, int fixedBlock) {
1585 if (fixedBlock>0) {
1586 return 1<<(fixedBlock-1);
1587 }
1588 freq/=OPLL_C_NUM;
1589 if (freq==0) return 1;
1590 return 1<<bsr(freq);
1591 }
1592
1593 int DivPlatformOPL::toFreq(int freq, int fixedBlock) {
1594 int block=0;
1595 if (fixedBlock>0) {
1596 block=fixedBlock-1;
1597 } else {
1598 block=freq/OPLL_C_NUM;
1599 if (block>0) block=bsr(block);
1600 }
1601 if (block>7) block=7;
1602 freq>>=block;
1603 if (freq>0x3ff) freq=0x3ff;
1604 return (block<<10)|freq;
1605 }
1606
1607 void DivPlatformOPL::muteChannel(int ch, bool mute) {
1608 isMuted[ch]=mute;
1609 if (PCM_CHECK(ch)) {
1610 chan[ch].freqChanged=true;
1611 chan[ch].writeCtrl=true;
1612 return;
1613 }
1614 if (ch==adpcmChan) {
1615 immWrite(18,(isMuted[adpcmChan]?0:chan[adpcmChan].outVol));
1616 return;
1617 }
1618 if (oplType<3 && ch<melodicChans) {
1619 fm.channel[outChanMap[ch]].muted=mute;
1620 }
1621 int ops=(slots[3][ch]!=255 && chan[ch].state.ops==4 && oplType==3)?4:2;
1622 if (ch&1 && ch<12) {
1623 if (chan[ch-1].fourOp) return;
1624 }
1625 chan[ch].fourOp=(ops==4);
1626 update4OpMask=true;
1627 for (int i=0; i<ops; i++) {
1628 unsigned char slot=slots[i][ch];
1629 if (slot==255) continue;
1630 unsigned short baseAddr=slotMap[slot];
1631 DivInstrumentFM::Operator& op=chan[ch].state.op[(ops==4)?orderedOpsL[i]:i];
1632
1633 if (isMuted[ch]) {
1634 rWrite(baseAddr+ADDR_KSL_TL,63|(op.ksl<<6));
1635 } else {
1636 if (KVSL(ch,i) || ch>melodicChans) {
1637 rWrite(baseAddr+ADDR_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[ch].outVol&0x3f,63))|(op.ksl<<6));
1638 } else {
1639 rWrite(baseAddr+ADDR_KSL_TL,op.tl|(op.ksl<<6));
1640 }
1641 }
1642 }
1643
1644 if (properDrums && ch>melodicChans) {
1645 return;
1646 }
1647
1648 if (isMuted[ch] && ch<=melodicChans) {
1649 rWrite(chanMap[ch]+ADDR_LR_FB_ALG,(chan[ch].state.alg&1)|(chan[ch].state.fb<<1));
1650 if (ops==4) {
1651 rWrite(chanMap[ch+1]+ADDR_LR_FB_ALG,((chan[ch].state.alg>>1)&1)|(chan[ch].state.fb<<1));
1652 }
1653 } else {
1654 rWrite(chanMap[ch]+ADDR_LR_FB_ALG,(chan[ch].state.alg&1)|(chan[ch].state.fb<<1)|((chan[ch].pan&15)<<4));
1655 if (ops==4) {
1656 rWrite(chanMap[ch+1]+ADDR_LR_FB_ALG,((chan[ch].state.alg>>1)&1)|(chan[ch].state.fb<<1)|((chan[ch].pan&15)<<4));
1657 }
1658 }
1659 }
1660
1661 void DivPlatformOPL::commitState(int ch, DivInstrument* ins) {
1662 if (PCM_CHECK(ch)) {
1663 return;
1664 }
1665 if (chan[ch].insChanged) {
1666 if (ch>melodicChans && ins->type==DIV_INS_OPL_DRUMS) {
1667 for (int i=0; i<4; i++) {
1668 chan[melodicChans+i+1].state.alg=ins->fm.alg;
1669 chan[melodicChans+i+1].state.fb=ins->fm.fb;
1670 chan[melodicChans+i+1].state.opllPreset=ins->fm.opllPreset;
1671 chan[melodicChans+i+1].state.fixedDrums=ins->fm.fixedDrums;
1672 chan[melodicChans+i+1].state.kickFreq=ins->fm.kickFreq;
1673 chan[melodicChans+i+1].state.snareHatFreq=ins->fm.snareHatFreq;
1674 chan[melodicChans+i+1].state.tomTopFreq=ins->fm.tomTopFreq;
1675 chan[melodicChans+i+1].state.op[0]=ins->fm.op[i];
1676 }
1677 } else {
1678 chan[ch].state=ins->fm;
1679 }
1680 }
1681
1682 if (chan[ch].insChanged) {
1683 if (ch>melodicChans && ins->type==DIV_INS_OPL_DRUMS) {
1684 for (int i=0; i<4; i++) {
1685 int ch=melodicChans+1+i;
1686 unsigned char slot=slots[0][ch];
1687 if (slot==255) continue;
1688 unsigned short baseAddr=slotMap[slot];
1689 DivInstrumentFM::Operator& op=chan[ch].state.op[0];
1690 chan[ch].fourOp=false;
1691
1692 if (isMuted[ch]) {
1693 rWrite(baseAddr+ADDR_KSL_TL,63|(op.ksl<<6));
1694 } else {
1695 rWrite(baseAddr+ADDR_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[ch].outVol&0x3f,63))|(op.ksl<<6));
1696 }
1697
1698 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
1699 rWrite(baseAddr+ADDR_AR_DR,(op.ar<<4)|op.dr);
1700 rWrite(baseAddr+ADDR_SL_RR,(op.sl<<4)|op.rr);
1701 if (oplType>1) {
1702 rWrite(baseAddr+ADDR_WS,op.ws&((oplType==3)?7:3));
1703 }
1704
1705 oldWrites[chanMap[ch]+ADDR_LR_FB_ALG]=-1;
1706 rWrite(chanMap[ch]+ADDR_LR_FB_ALG,(chan[ch].state.alg&1)|(chan[ch].state.fb<<1)|((chan[ch].pan&15)<<4));
1707 }
1708 } else {
1709 int ops=(slots[3][ch]!=255 && chan[ch].state.ops==4 && oplType==3)?4:2;
1710 chan[ch].fourOp=(ops==4);
1711 if (chan[ch].fourOp) {
1712 /*
1713 if (chan[ch+1].active) {
1714 chan[ch+1].keyOff=true;
1715 chan[ch+1].keyOn=false;
1716 chan[ch+1].active=false;
1717 }*/
1718 chan[ch+1].insChanged=true;
1719 chan[ch+1].macroInit(NULL);
1720 }
1721 update4OpMask=true;
1722 for (int i=0; i<ops; i++) {
1723 unsigned char slot=slots[i][ch];
1724 if (slot==255) continue;
1725 unsigned short baseAddr=slotMap[slot];
1726 DivInstrumentFM::Operator& op=chan[ch].state.op[(ops==4)?orderedOpsL[i]:i];
1727
1728 if (isMuted[ch]) {
1729 rWrite(baseAddr+ADDR_KSL_TL,63|(op.ksl<<6));
1730 } else {
1731 if (KVSL(ch,i) || ch>melodicChans) {
1732 rWrite(baseAddr+ADDR_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[ch].outVol&0x3f,63))|(op.ksl<<6));
1733 } else {
1734 rWrite(baseAddr+ADDR_KSL_TL,op.tl|(op.ksl<<6));
1735 }
1736 }
1737
1738 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
1739 rWrite(baseAddr+ADDR_AR_DR,(op.ar<<4)|op.dr);
1740 rWrite(baseAddr+ADDR_SL_RR,(op.sl<<4)|op.rr);
1741 if (oplType>1) {
1742 rWrite(baseAddr+ADDR_WS,op.ws&((oplType==3)?7:3));
1743 }
1744 }
1745
1746 if (isMuted[ch] && ch<=melodicChans) {
1747 oldWrites[chanMap[ch]+ADDR_LR_FB_ALG]=-1;
1748 rWrite(chanMap[ch]+ADDR_LR_FB_ALG,(chan[ch].state.alg&1)|(chan[ch].state.fb<<1));
1749 if (ops==4) {
1750 oldWrites[chanMap[ch+1]+ADDR_LR_FB_ALG]=-1;
1751 rWrite(chanMap[ch+1]+ADDR_LR_FB_ALG,((chan[ch].state.alg>>1)&1)|(chan[ch].state.fb<<1));
1752 }
1753 } else {
1754 oldWrites[chanMap[ch]+ADDR_LR_FB_ALG]=-1;
1755 rWrite(chanMap[ch]+ADDR_LR_FB_ALG,(chan[ch].state.alg&1)|(chan[ch].state.fb<<1)|((chan[ch].pan&15)<<4));
1756 if (ops==4) {
1757 oldWrites[chanMap[ch+1]+ADDR_LR_FB_ALG]=-1;
1758 rWrite(chanMap[ch+1]+ADDR_LR_FB_ALG,((chan[ch].state.alg>>1)&1)|(chan[ch].state.fb<<1)|((chan[ch].pan&15)<<4));
1759 }
1760 }
1761 }
1762 }
1763 }
1764
1765 int DivPlatformOPL::dispatch(DivCommand c) {
1766 if (c.chan>=totalChans && c.chan!=adpcmChan) return 0;
1767 // ineffective in 4-op mode
1768 if (oplType==3 && c.chan!=adpcmChan && c.chan<14 && (c.chan&1) && c.cmd!=DIV_CMD_GET_VOLMAX && c.cmd!=DIV_CMD_INSTRUMENT) {
1769 if (chan[c.chan-1].fourOp) return 0;
1770 }
1771 switch (c.cmd) {
1772 case DIV_CMD_NOTE_ON: {
1773 if (PCM_CHECK(c.chan)) { // OPL4 PCM
1774 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_MULTIPCM);
1775 chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:127;
1776 if (c.value!=DIV_NOTE_NULL) {
1777 chan[c.chan].sample=ins->amiga.getSample(c.value);
1778 chan[c.chan].sampleNote=c.value;
1779 c.value=ins->amiga.getFreq(c.value);
1780 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
1781 }
1782 if (c.value!=DIV_NOTE_NULL) {
1783 chan[c.chan].baseFreq=NOTE_PCM(c.value);
1784 }
1785 if (chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) {
1786 chan[c.chan].sample=-1;
1787 }
1788 if (c.value!=DIV_NOTE_NULL) {
1789 chan[c.chan].freqChanged=true;
1790 chan[c.chan].note=c.value;
1791 }
1792 if (chan[c.chan].insChanged) {
1793 if (ins->type==DIV_INS_MULTIPCM) {
1794 chan[c.chan].lfo=ins->multipcm.lfo;
1795 chan[c.chan].vib=ins->multipcm.vib;
1796 chan[c.chan].am=ins->multipcm.am;
1797 chan[c.chan].ar=ins->multipcm.ar;
1798 chan[c.chan].d1r=ins->multipcm.d1r;
1799 chan[c.chan].dl=ins->multipcm.dl;
1800 chan[c.chan].d2r=ins->multipcm.d2r;
1801 chan[c.chan].rc=ins->multipcm.rc;
1802 chan[c.chan].rr=ins->multipcm.rr;
1803 chan[c.chan].damp=ins->multipcm.damp;
1804 chan[c.chan].pseudoReverb=ins->multipcm.pseudoReverb;
1805 chan[c.chan].levelDirect=ins->multipcm.levelDirect;
1806 chan[c.chan].lfoReset=ins->multipcm.lfoReset;
1807 } else {
1808 chan[c.chan].lfo=0;
1809 chan[c.chan].vib=0;
1810 chan[c.chan].am=0;
1811 chan[c.chan].ar=15;
1812 chan[c.chan].d1r=15;
1813 chan[c.chan].dl=0;
1814 chan[c.chan].d2r=0;
1815 chan[c.chan].rc=15;
1816 chan[c.chan].rr=15;
1817 chan[c.chan].damp=false;
1818 chan[c.chan].pseudoReverb=false;
1819 chan[c.chan].levelDirect=true;
1820 chan[c.chan].lfoReset=false;
1821 }
1822 chan[c.chan].insChanged=false;
1823 }
1824 chan[c.chan].active=true;
1825 chan[c.chan].keyOn=true;
1826 chan[c.chan].macroInit(ins);
1827 if (!chan[c.chan].std.vol.will) {
1828 chan[c.chan].outVol=chan[c.chan].vol;
1829 }
1830 break;
1831 } else if (c.chan==adpcmChan) { // ADPCM
1832 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_FM);
1833 chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:255;
1834 if (ins->type==DIV_INS_AMIGA || ins->type==DIV_INS_ADPCMB) {
1835 chan[c.chan].furnacePCM=true;
1836 } else {
1837 chan[c.chan].furnacePCM=false;
1838 }
1839 if (skipRegisterWrites) break;
1840 if (chan[c.chan].furnacePCM) {
1841 chan[c.chan].macroInit(ins);
1842 chan[c.chan].fixedFreq=0;
1843 if (!chan[c.chan].std.vol.will) {
1844 chan[c.chan].outVol=chan[c.chan].vol;
1845 immWrite(18,(isMuted[adpcmChan]?0:chan[adpcmChan].outVol));
1846 }
1847 if (c.value!=DIV_NOTE_NULL) {
1848 chan[c.chan].sample=ins->amiga.getSample(c.value);
1849 c.value=ins->amiga.getFreq(c.value);
1850 }
1851 if (chan[c.chan].sample>=0 && chan[c.chan].sample<parent->song.sampleLen) {
1852 DivSample* s=parent->getSample(chan[c.chan].sample);
1853 immWrite(8,0);
1854 immWrite(9,(sampleOffB[chan[c.chan].sample]>>2)&0xff);
1855 immWrite(10,(sampleOffB[chan[c.chan].sample]>>10)&0xff);
1856 int end=sampleOffB[chan[c.chan].sample]+s->lengthB-1;
1857 immWrite(11,(end>>2)&0xff);
1858 immWrite(12,(end>>10)&0xff);
1859 if (c.value!=DIV_NOTE_NULL) {
1860 chan[c.chan].note=c.value;
1861 chan[c.chan].baseFreq=NOTE_ADPCMB(chan[c.chan].note);
1862 chan[c.chan].freqChanged=true;
1863 }
1864 chan[c.chan].active=true;
1865 chan[c.chan].keyOn=true;
1866 } else {
1867 immWrite(7,0x01); // reset
1868 immWrite(9,0);
1869 immWrite(10,0);
1870 immWrite(11,0);
1871 immWrite(12,0);
1872 break;
1873 }
1874 } else {
1875 chan[c.chan].sample=-1;
1876 chan[c.chan].macroInit(NULL);
1877 chan[c.chan].outVol=chan[c.chan].vol;
1878 if ((12*sampleBank+c.value%12)>=parent->song.sampleLen) {
1879 break;
1880 }
1881 chan[c.chan].sample=12*sampleBank+c.value%12;
1882 if (chan[c.chan].sample>=0 && chan[c.chan].sample<parent->song.sampleLen) {
1883 DivSample* s=parent->getSample(12*sampleBank+c.value%12);
1884 immWrite(8,0);
1885 immWrite(9,(sampleOffB[chan[c.chan].sample]>>2)&0xff);
1886 immWrite(10,(sampleOffB[chan[c.chan].sample]>>10)&0xff);
1887 int end=sampleOffB[chan[c.chan].sample]+s->lengthB-1;
1888 immWrite(11,(end>>2)&0xff);
1889 immWrite(12,(end>>10)&0xff);
1890 int freq=(65536.0*(double)s->rate)/(double)chipRateBase;
1891 chan[c.chan].fixedFreq=freq;
1892 immWrite(16,freq&0xff);
1893 immWrite(17,(freq>>8)&0xff);
1894 chan[c.chan].active=true;
1895 chan[c.chan].keyOn=true;
1896 } else {
1897 immWrite(7,0x01); // reset
1898 immWrite(9,0);
1899 immWrite(10,0);
1900 immWrite(11,0);
1901 immWrite(12,0);
1902 }
1903 }
1904 break;
1905 }
1906 DivInstrument* ins=parent->getIns(chan[c.chan].ins,c.chan>melodicChans?DIV_INS_OPL_DRUMS:DIV_INS_OPL);
1907
1908 chan[c.chan].macroInit(ins);
1909 if (!chan[c.chan].std.vol.will) {
1910 chan[c.chan].outVol=chan[c.chan].vol;
1911 }
1912
1913 commitState(c.chan,ins);
1914 chan[c.chan].insChanged=false;
1915
1916 if (c.value!=DIV_NOTE_NULL) {
1917 if (c.chan>melodicChans && ins->type==DIV_INS_OPL_DRUMS && chan[c.chan].state.fixedDrums) {
1918 chan[melodicChans+1].fixedFreq=(chan[melodicChans+1].state.snareHatFreq&1023)<<(chan[melodicChans+1].state.snareHatFreq>>10);
1919 chan[melodicChans+2].fixedFreq=(chan[melodicChans+2].state.tomTopFreq&1023)<<(chan[melodicChans+2].state.tomTopFreq>>10);
1920 chan[melodicChans+3].fixedFreq=chan[melodicChans+2].fixedFreq;
1921 chan[melodicChans+4].fixedFreq=chan[melodicChans+1].fixedFreq;
1922
1923 chan[melodicChans+1].freqChanged=true;
1924 chan[melodicChans+2].freqChanged=true;
1925 chan[melodicChans+3].freqChanged=true;
1926 chan[melodicChans+4].freqChanged=true;
1927 } else {
1928 if (c.chan>=melodicChans && (chan[c.chan].state.opllPreset==16 || ins->type==DIV_INS_OPL_DRUMS) && chan[c.chan].state.fixedDrums) { // drums
1929 if (c.chan==melodicChans) {
1930 chan[c.chan].fixedFreq=(chan[c.chan].state.kickFreq&1023)<<(chan[c.chan].state.kickFreq>>10);
1931 } else if (c.chan==melodicChans+1 || c.chan==melodicChans+4) {
1932 chan[c.chan].fixedFreq=(chan[c.chan].state.snareHatFreq&1023)<<(chan[c.chan].state.snareHatFreq>>10);
1933 } else if (c.chan==melodicChans+2 || c.chan==melodicChans+3) {
1934 chan[c.chan].fixedFreq=(chan[c.chan].state.tomTopFreq&1023)<<(chan[c.chan].state.tomTopFreq>>10);
1935 } else {
1936 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
1937 chan[c.chan].fixedFreq=0;
1938 }
1939 } else {
1940 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
1941 chan[c.chan].fixedFreq=0;
1942 }
1943 }
1944 chan[c.chan].note=c.value;
1945 chan[c.chan].freqChanged=true;
1946 }
1947 chan[c.chan].keyOn=true;
1948 chan[c.chan].active=true;
1949 break;
1950 }
1951 case DIV_CMD_NOTE_OFF:
1952 chan[c.chan].keyOff=true;
1953 chan[c.chan].keyOn=false;
1954 chan[c.chan].active=false;
1955 if (PCM_CHECK(c.chan)) {
1956 chan[c.chan].sample=-1;
1957 chan[c.chan].macroInit(NULL);
1958 }
1959 break;
1960 case DIV_CMD_NOTE_OFF_ENV:
1961 chan[c.chan].keyOff=true;
1962 chan[c.chan].keyOn=false;
1963 chan[c.chan].active=false;
1964 chan[c.chan].std.release();
1965 break;
1966 case DIV_CMD_ENV_RELEASE:
1967 chan[c.chan].std.release();
1968 break;
1969 case DIV_CMD_VOLUME: {
1970 if (pretendYMU && c.chan!=adpcmChan) {
1971 c.value=pow(((double)c.value/127.0),0.5)*63.0;
1972 if (c.value<0) c.value=0;
1973 if (c.value>63) c.value=63;
1974 }
1975 chan[c.chan].vol=c.value;
1976 if (!chan[c.chan].std.vol.has) {
1977 chan[c.chan].outVol=c.value;
1978 }
1979 if (PCM_CHECK(c.chan)) { // OPL4 PCM
1980 immWrite(PCM_ADDR_TL+PCM_REG(c.chan),((0x7f-chan[c.chan].outVol)<<1)|(chan[c.chan].levelDirect?1:0));
1981 break;
1982 }
1983 if (c.chan==adpcmChan) { // ADPCM-B
1984 immWrite(18,(isMuted[adpcmChan]?0:chan[adpcmChan].outVol));
1985 break;
1986 }
1987 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
1988 for (int i=0; i<ops; i++) {
1989 unsigned char slot=slots[i][c.chan];
1990 if (slot==255) continue;
1991 unsigned short baseAddr=slotMap[slot];
1992 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[i]:i];
1993
1994 if (isMuted[c.chan]) {
1995 rWrite(baseAddr+ADDR_KSL_TL,63|(op.ksl<<6));
1996 } else {
1997 if (KVSL(c.chan,i) || c.chan>melodicChans) {
1998 rWrite(baseAddr+ADDR_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[c.chan].outVol&0x3f,63))|(op.ksl<<6));
1999 } else {
2000 rWrite(baseAddr+ADDR_KSL_TL,op.tl|(op.ksl<<6));
2001 }
2002 }
2003 }
2004 break;
2005 }
2006 case DIV_CMD_GET_VOLUME: {
2007 if (pretendYMU && c.chan!=adpcmChan) {
2008 return pow(((double)chan[c.chan].vol/63.0),2.0)*127.0;
2009 }
2010 return chan[c.chan].vol;
2011 break;
2012 }
2013 case DIV_CMD_INSTRUMENT:
2014 if (chan[c.chan].ins!=c.value || c.value2==1) {
2015 chan[c.chan].insChanged=true;
2016 }
2017 chan[c.chan].ins=c.value;
2018 break;
2019 case DIV_CMD_PANNING: {
2020 if (PCM_CHECK(c.chan)) {
2021 chan[c.chan].ch=false;
2022 chan[c.chan].pan=8^MIN(parent->convertPanSplitToLinearLR(c.value,c.value2,15)+1,15);
2023 chan[c.chan].freqChanged=true;
2024 chan[c.chan].writeCtrl=true;
2025 break;
2026 }
2027 if (oplType!=3) break;
2028 if (c.chan==adpcmChan) break;
2029 chan[c.chan].pan&=~3;
2030 if (c.value==0 && c.value2==0 && ((chipType!=4) && compatPan)) {
2031 chan[c.chan].pan|=3;
2032 } else {
2033 chan[c.chan].pan|=(c.value>0)|((c.value2>0)<<1);
2034 }
2035 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2036 if (isMuted[c.chan] && c.chan<=melodicChans) {
2037 rWrite(chanMap[c.chan]+ADDR_LR_FB_ALG,(chan[c.chan].state.alg&1)|(chan[c.chan].state.fb<<1));
2038 if (ops==4) {
2039 rWrite(chanMap[c.chan+1]+ADDR_LR_FB_ALG,((chan[c.chan].state.alg>>1)&1)|(chan[c.chan].state.fb<<1));
2040 }
2041 } else {
2042 rWrite(chanMap[c.chan]+ADDR_LR_FB_ALG,(chan[c.chan].state.alg&1)|(chan[c.chan].state.fb<<1)|((chan[c.chan].pan&15)<<4));
2043 if (ops==4) {
2044 rWrite(chanMap[c.chan+1]+ADDR_LR_FB_ALG,((chan[c.chan].state.alg>>1)&1)|(chan[c.chan].state.fb<<1)|((chan[c.chan].pan&15)<<4));
2045 }
2046 }
2047 break;
2048 }
2049 case DIV_CMD_SURROUND_PANNING: {
2050 if (PCM_CHECK(c.chan)) {
2051 chan[c.chan].ch=true;
2052 chan[c.chan].freqChanged=true;
2053 chan[c.chan].writeCtrl=true;
2054 break;
2055 }
2056 if (oplType!=3) break;
2057 if (c.chan==adpcmChan) break;
2058
2059 if (c.value==2) {
2060 chan[c.chan].pan&=3;
2061 if (c.value2>0) chan[c.chan].pan|=4;
2062 } else if (c.value==3) {
2063 if (c.value2>0) chan[c.chan].pan|=8;
2064 } else {
2065 break;
2066 }
2067
2068 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2069 if (isMuted[c.chan] && c.chan<=melodicChans) {
2070 rWrite(chanMap[c.chan]+ADDR_LR_FB_ALG,(chan[c.chan].state.alg&1)|(chan[c.chan].state.fb<<1));
2071 if (ops==4) {
2072 rWrite(chanMap[c.chan+1]+ADDR_LR_FB_ALG,((chan[c.chan].state.alg>>1)&1)|(chan[c.chan].state.fb<<1));
2073 }
2074 } else {
2075 rWrite(chanMap[c.chan]+ADDR_LR_FB_ALG,(chan[c.chan].state.alg&1)|(chan[c.chan].state.fb<<1)|((chan[c.chan].pan&15)<<4));
2076 if (ops==4) {
2077 rWrite(chanMap[c.chan+1]+ADDR_LR_FB_ALG,((chan[c.chan].state.alg>>1)&1)|(chan[c.chan].state.fb<<1)|((chan[c.chan].pan&15)<<4));
2078 }
2079 }
2080 break;
2081 }
2082 case DIV_CMD_PITCH: {
2083 if (c.chan==adpcmChan) {
2084 if (!chan[c.chan].furnacePCM) break;
2085 }
2086 chan[c.chan].pitch=c.value;
2087 chan[c.chan].freqChanged=true;
2088 break;
2089 }
2090 case DIV_CMD_NOTE_PORTA: {
2091 if (PCM_CHECK(c.chan)) {
2092 int destFreq=NOTE_PCM(c.value2+chan[c.chan].sampleNoteDelta);
2093 bool return2=false;
2094 if (destFreq>chan[c.chan].baseFreq) {
2095 chan[c.chan].baseFreq+=c.value;
2096 if (chan[c.chan].baseFreq>=destFreq) {
2097 chan[c.chan].baseFreq=destFreq;
2098 return2=true;
2099 }
2100 } else {
2101 chan[c.chan].baseFreq-=c.value;
2102 if (chan[c.chan].baseFreq<=destFreq) {
2103 chan[c.chan].baseFreq=destFreq;
2104 return2=true;
2105 }
2106 }
2107 chan[c.chan].freqChanged=true;
2108 if (return2) {
2109 chan[c.chan].inPorta=false;
2110 return 2;
2111 }
2112 break;
2113 }
2114 int destFreq=(c.chan==adpcmChan)?(NOTE_ADPCMB(c.value2)):(NOTE_FREQUENCY(c.value2));
2115 int newFreq;
2116 bool return2=false;
2117 int mul=1;
2118 int fixedBlock=0;
2119 if (parent->song.linearPitch!=2) {
2120 fixedBlock=chan[c.chan].state.block;
2121 mul=octave(chan[c.chan].baseFreq,fixedBlock);
2122 }
2123 if (destFreq>chan[c.chan].baseFreq) {
2124 newFreq=chan[c.chan].baseFreq+c.value*mul;
2125 if (newFreq>=destFreq) {
2126 newFreq=destFreq;
2127 return2=true;
2128 }
2129 } else {
2130 newFreq=chan[c.chan].baseFreq-c.value*mul;
2131 if (newFreq<=destFreq) {
2132 newFreq=destFreq;
2133 return2=true;
2134 }
2135 }
2136 if (!chan[c.chan].portaPause && parent->song.linearPitch!=2) {
2137 if (mul!=octave(newFreq,fixedBlock)) {
2138 chan[c.chan].portaPause=true;
2139 break;
2140 }
2141 }
2142 chan[c.chan].baseFreq=newFreq;
2143 chan[c.chan].portaPause=false;
2144 chan[c.chan].freqChanged=true;
2145 if (return2) {
2146 chan[c.chan].inPorta=false;
2147 return 2;
2148 }
2149 break;
2150 }
2151 case DIV_CMD_SAMPLE_BANK:
2152 if (adpcmChan<0) break;
2153 sampleBank=c.value;
2154 if (sampleBank>(int)(parent->song.sample.size()/12)) {
2155 sampleBank=parent->song.sample.size()/12;
2156 }
2157 iface.sampleBank=sampleBank;
2158 break;
2159 case DIV_CMD_LEGATO: {
2160 // TODO: OPL4 PCM
2161 if (chan[c.chan].insChanged) {
2162 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_FM);
2163 commitState(c.chan,ins);
2164 chan[c.chan].insChanged=false;
2165 }
2166 chan[c.chan].baseFreq=(PCM_CHECK(c.chan))?NOTE_PCM(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(0))):
2167 (c.chan==adpcmChan)?(NOTE_ADPCMB(c.value)):(NOTE_FREQUENCY(c.value));
2168 chan[c.chan].note=c.value;
2169 chan[c.chan].freqChanged=true;
2170 break;
2171 }
2172 case DIV_CMD_FM_LFO: {
2173 if (PCM_CHECK(c.chan)) break;
2174 if (c.chan==adpcmChan) break;
2175 if (c.value&2) {
2176 dvb=c.value&1;
2177 } else {
2178 dam=c.value&1;
2179 }
2180 immWrite(0xbd,(dam<<7)|(dvb<<6)|(properDrums<<5)|drumState);
2181 break;
2182 }
2183 case DIV_CMD_FM_FB: {
2184 if (PCM_CHECK(c.chan)) break;
2185 if (c.chan==adpcmChan) break;
2186 chan[c.chan].state.fb=c.value&7;
2187 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2188 if (isMuted[c.chan] && c.chan<=melodicChans) {
2189 rWrite(chanMap[c.chan]+ADDR_LR_FB_ALG,(chan[c.chan].state.alg&1)|(chan[c.chan].state.fb<<1));
2190 if (ops==4) {
2191 rWrite(chanMap[c.chan+1]+ADDR_LR_FB_ALG,((chan[c.chan].state.alg>>1)&1)|(chan[c.chan].state.fb<<1));
2192 }
2193 } else {
2194 rWrite(chanMap[c.chan]+ADDR_LR_FB_ALG,(chan[c.chan].state.alg&1)|(chan[c.chan].state.fb<<1)|((chan[c.chan].pan&15)<<4));
2195 if (ops==4) {
2196 rWrite(chanMap[c.chan+1]+ADDR_LR_FB_ALG,((chan[c.chan].state.alg>>1)&1)|(chan[c.chan].state.fb<<1)|((chan[c.chan].pan&15)<<4));
2197 }
2198 }
2199 break;
2200 }
2201 case DIV_CMD_FM_MULT: {
2202 if (PCM_CHECK(c.chan)) break;
2203 if (c.chan==adpcmChan) break;
2204 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2205 if (c.value>=ops) break;
2206 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[c.value]:c.value];
2207 op.mult=c.value2&15;
2208 unsigned char slot=slots[c.value][c.chan];
2209 if (slot==255) break;
2210 unsigned short baseAddr=slotMap[slot];
2211 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
2212 break;
2213 }
2214 case DIV_CMD_FM_TL: {
2215 if (PCM_CHECK(c.chan)) break;
2216 if (c.chan==adpcmChan) break;
2217 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2218 if (c.value>=ops) break;
2219 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[c.value]:c.value];
2220 op.tl=c.value2&63;
2221 unsigned char slot=slots[c.value][c.chan];
2222 if (slot==255) break;
2223 unsigned short baseAddr=slotMap[slot];
2224 if (isMuted[c.chan]) {
2225 rWrite(baseAddr+ADDR_KSL_TL,63|(op.ksl<<6));
2226 } else {
2227 if (KVSL(c.chan,c.value) || c.chan>melodicChans) {
2228 rWrite(baseAddr+ADDR_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[c.chan].outVol&0x3f,63))|(op.ksl<<6));
2229 } else {
2230 rWrite(baseAddr+ADDR_KSL_TL,op.tl|(op.ksl<<6));
2231 }
2232 }
2233 break;
2234 }
2235 case DIV_CMD_FM_AR: {
2236 if (PCM_CHECK(c.chan)) break;
2237 if (c.chan==adpcmChan) break;
2238 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2239 if (c.value<0) {
2240 for (int i=0; i<ops; i++) {
2241 unsigned char slot=slots[i][c.chan];
2242 if (slot==255) continue;
2243 unsigned short baseAddr=slotMap[slot];
2244 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[i]:i];
2245 op.ar=c.value2&15;
2246 rWrite(baseAddr+ADDR_AR_DR,(op.ar<<4)|op.dr);
2247 }
2248 } else {
2249 if (c.value>=ops) break;
2250 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[c.value]:c.value];
2251 op.ar=c.value2&15;
2252 unsigned char slot=slots[c.value][c.chan];
2253 if (slot==255) break;
2254 unsigned short baseAddr=slotMap[slot];
2255 rWrite(baseAddr+ADDR_AR_DR,(op.ar<<4)|op.dr);
2256 }
2257 break;
2258 }
2259 case DIV_CMD_FM_DR: {
2260 if (PCM_CHECK(c.chan)) break;
2261 if (c.chan==adpcmChan) break;
2262 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2263 if (c.value<0) {
2264 for (int i=0; i<ops; i++) {
2265 unsigned char slot=slots[i][c.chan];
2266 if (slot==255) continue;
2267 unsigned short baseAddr=slotMap[slot];
2268 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[i]:i];
2269 op.dr=c.value2&15;
2270 rWrite(baseAddr+ADDR_AR_DR,(op.ar<<4)|op.dr);
2271 }
2272 } else {
2273 if (c.value>=ops) break;
2274 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[c.value]:c.value];
2275 op.dr=c.value2&15;
2276 unsigned char slot=slots[c.value][c.chan];
2277 if (slot==255) break;
2278 unsigned short baseAddr=slotMap[slot];
2279 rWrite(baseAddr+ADDR_AR_DR,(op.ar<<4)|op.dr);
2280 }
2281 break;
2282 }
2283 case DIV_CMD_FM_SL: {
2284 if (PCM_CHECK(c.chan)) break;
2285 if (c.chan==adpcmChan) break;
2286 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2287 if (c.value<0) {
2288 for (int i=0; i<ops; i++) {
2289 unsigned char slot=slots[i][c.chan];
2290 if (slot==255) continue;
2291 unsigned short baseAddr=slotMap[slot];
2292 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[i]:i];
2293 op.sl=c.value2&15;
2294 rWrite(baseAddr+ADDR_SL_RR,(op.sl<<4)|op.rr);
2295 }
2296 } else {
2297 if (c.value>=ops) break;
2298 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[c.value]:c.value];
2299 op.sl=c.value2&15;
2300 unsigned char slot=slots[c.value][c.chan];
2301 if (slot==255) break;
2302 unsigned short baseAddr=slotMap[slot];
2303 rWrite(baseAddr+ADDR_SL_RR,(op.sl<<4)|op.rr);
2304 }
2305 break;
2306 }
2307 case DIV_CMD_FM_RR: {
2308 if (PCM_CHECK(c.chan)) break;
2309 if (c.chan==adpcmChan) break;
2310 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2311 if (c.value<0) {
2312 for (int i=0; i<ops; i++) {
2313 unsigned char slot=slots[i][c.chan];
2314 if (slot==255) continue;
2315 unsigned short baseAddr=slotMap[slot];
2316 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[i]:i];
2317 op.rr=c.value2&15;
2318 rWrite(baseAddr+ADDR_SL_RR,(op.sl<<4)|op.rr);
2319 }
2320 } else {
2321 if (c.value>=ops) break;
2322 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[c.value]:c.value];
2323 op.rr=c.value2&15;
2324 unsigned char slot=slots[c.value][c.chan];
2325 if (slot==255) break;
2326 unsigned short baseAddr=slotMap[slot];
2327 rWrite(baseAddr+ADDR_SL_RR,(op.sl<<4)|op.rr);
2328 }
2329 break;
2330 }
2331 case DIV_CMD_FM_AM: {
2332 if (PCM_CHECK(c.chan)) break;
2333 if (c.chan==adpcmChan) break;
2334 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2335 if (c.value<0) {
2336 for (int i=0; i<ops; i++) {
2337 unsigned char slot=slots[i][c.chan];
2338 if (slot==255) continue;
2339 unsigned short baseAddr=slotMap[slot];
2340 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[i]:i];
2341 op.am=c.value2&1;
2342 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
2343 }
2344 } else {
2345 if (c.value>=ops) break;
2346 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[c.value]:c.value];
2347 op.am=c.value2&1;
2348 unsigned char slot=slots[c.value][c.chan];
2349 if (slot==255) break;
2350 unsigned short baseAddr=slotMap[slot];
2351 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
2352 }
2353 break;
2354 }
2355 case DIV_CMD_FM_VIB: {
2356 if (PCM_CHECK(c.chan)) break;
2357 if (c.chan==adpcmChan) break;
2358 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2359 if (c.value<0) {
2360 for (int i=0; i<ops; i++) {
2361 unsigned char slot=slots[i][c.chan];
2362 if (slot==255) continue;
2363 unsigned short baseAddr=slotMap[slot];
2364 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[i]:i];
2365 op.vib=c.value2&1;
2366 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
2367 }
2368 } else {
2369 if (c.value>=ops) break;
2370 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[c.value]:c.value];
2371 op.vib=c.value2&1;
2372 unsigned char slot=slots[c.value][c.chan];
2373 if (slot==255) break;
2374 unsigned short baseAddr=slotMap[slot];
2375 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
2376 }
2377 break;
2378 }
2379 case DIV_CMD_FM_SUS: {
2380 if (PCM_CHECK(c.chan)) break;
2381 if (c.chan==adpcmChan) break;
2382 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2383 if (c.value<0) {
2384 for (int i=0; i<ops; i++) {
2385 unsigned char slot=slots[i][c.chan];
2386 if (slot==255) continue;
2387 unsigned short baseAddr=slotMap[slot];
2388 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[i]:i];
2389 op.sus=c.value2&1;
2390 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
2391 }
2392 } else {
2393 if (c.value>=ops) break;
2394 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[c.value]:c.value];
2395 op.sus=c.value2&1;
2396 unsigned char slot=slots[c.value][c.chan];
2397 if (slot==255) break;
2398 unsigned short baseAddr=slotMap[slot];
2399 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
2400 }
2401 break;
2402 }
2403 case DIV_CMD_FM_KSR: {
2404 if (PCM_CHECK(c.chan)) break;
2405 if (c.chan==adpcmChan) break;
2406 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2407 if (c.value<0) {
2408 for (int i=0; i<ops; i++) {
2409 unsigned char slot=slots[i][c.chan];
2410 if (slot==255) continue;
2411 unsigned short baseAddr=slotMap[slot];
2412 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[i]:i];
2413 op.ksr=c.value2&1;
2414 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
2415 }
2416 } else {
2417 if (c.value>=ops) break;
2418 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[c.value]:c.value];
2419 op.ksr=c.value2&1;
2420 unsigned char slot=slots[c.value][c.chan];
2421 if (slot==255) break;
2422 unsigned short baseAddr=slotMap[slot];
2423 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
2424 }
2425 break;
2426 }
2427 case DIV_CMD_FM_WS: {
2428 if (PCM_CHECK(c.chan)) break;
2429 if (c.chan==adpcmChan) break;
2430 if (oplType<2) break;
2431 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2432 if (c.value<0) {
2433 for (int i=0; i<ops; i++) {
2434 unsigned char slot=slots[i][c.chan];
2435 if (slot==255) continue;
2436 unsigned short baseAddr=slotMap[slot];
2437 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[i]:i];
2438 op.ws=c.value2&7;
2439 rWrite(baseAddr+ADDR_WS,op.ws&((oplType==3)?7:3));
2440 }
2441 } else {
2442 if (c.value>=ops) break;
2443 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[c.value]:c.value];
2444 op.ws=c.value2&7;
2445 unsigned char slot=slots[c.value][c.chan];
2446 if (slot==255) break;
2447 unsigned short baseAddr=slotMap[slot];
2448 rWrite(baseAddr+ADDR_WS,op.ws&((oplType==3)?7:3));
2449 }
2450 break;
2451 }
2452 case DIV_CMD_FM_RS: {
2453 if (PCM_CHECK(c.chan)) break;
2454 if (c.chan==adpcmChan) break;
2455 if (oplType<2) break;
2456 int ops=(slots[3][c.chan]!=255 && chan[c.chan].state.ops==4 && oplType==3)?4:2;
2457 if (c.value<0) {
2458 for (int i=0; i<ops; i++) {
2459 unsigned char slot=slots[i][c.chan];
2460 if (slot==255) continue;
2461 unsigned short baseAddr=slotMap[slot];
2462 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[i]:i];
2463 op.ksl=c.value2&3;
2464 if (isMuted[c.chan]) {
2465 rWrite(baseAddr+ADDR_KSL_TL,63|(op.ksl<<6));
2466 } else {
2467 if (KVSL(c.chan,i) || c.chan>melodicChans) {
2468 rWrite(baseAddr+ADDR_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[c.chan].outVol&0x3f,63))|(op.ksl<<6));
2469 } else {
2470 rWrite(baseAddr+ADDR_KSL_TL,op.tl|(op.ksl<<6));
2471 }
2472 }
2473 }
2474 } else {
2475 if (c.value>=ops) break;
2476 DivInstrumentFM::Operator& op=chan[c.chan].state.op[(ops==4)?orderedOpsL[c.value]:c.value];
2477 op.ksl=c.value2&3;
2478 unsigned char slot=slots[c.value][c.chan];
2479 if (slot==255) break;
2480 unsigned short baseAddr=slotMap[slot];
2481 if (isMuted[c.chan]) {
2482 rWrite(baseAddr+ADDR_KSL_TL,63|(op.ksl<<6));
2483 } else {
2484 if (KVSL(c.chan,c.value) || c.chan>melodicChans) {
2485 rWrite(baseAddr+ADDR_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[c.chan].outVol&0x3f,63))|(op.ksl<<6));
2486 } else {
2487 rWrite(baseAddr+ADDR_KSL_TL,op.tl|(op.ksl<<6));
2488 }
2489 }
2490 }
2491 break;
2492 }
2493 case DIV_CMD_FM_EXTCH: {
2494 if (!properDrumsSys) break;
2495 properDrums=c.value;
2496 immWrite(0xbd,(dam<<7)|(dvb<<6)|(properDrums<<5)|drumState);
2497 slots=properDrums?slotsDrums:slotsNonDrums;
2498 if (oplType==3) {
2499 chanMap=properDrums?chanMapOPL3Drums:chanMapOPL3;
2500 melodicChans=properDrums?15:18;
2501 totalChans=properDrums?20:18;
2502 if (chipType==4) {
2503 pcmChanOffs=totalChans;
2504 totalChans+=24;
2505 }
2506 } else {
2507 chanMap=properDrums?chanMapOPL2Drums:chanMapOPL2;
2508 melodicChans=properDrums?6:9;
2509 totalChans=properDrums?11:9;
2510 }
2511 break;
2512 }
2513 case DIV_CMD_FM_HARD_RESET:
2514 if (PCM_CHECK(c.chan)) break;
2515 if (c.chan==adpcmChan) break;
2516 chan[c.chan].hardReset=c.value;
2517 break;
2518 case DIV_CMD_MULTIPCM_MIX_FM:
2519 if (chipType==4) {
2520 fmMixL=CLAMP((c.value&0x70)>>4,0,7);
2521 fmMixR=CLAMP((c.value&0x7),0,7);
2522 immWrite(PCM_ADDR_MIX_FM,((7-fmMixR)<<3)|(7-fmMixL));
2523 }
2524 break;
2525 case DIV_CMD_MULTIPCM_MIX_PCM:
2526 if (chipType==4) {
2527 pcmMixL=CLAMP((c.value&0x70)>>4,0,7);
2528 pcmMixR=CLAMP((c.value&0x7),0,7);
2529 immWrite(PCM_ADDR_MIX_PCM,((7-pcmMixR)<<3)|(7-pcmMixL));
2530 }
2531 break;
2532 case DIV_CMD_MULTIPCM_LFO:
2533 if (PCM_CHECK(c.chan)) {
2534 chan[c.chan].lfo=c.value&7;
2535 rWrite(PCM_ADDR_LFO_VIB+PCM_REG(c.chan),(chan[c.chan].lfo<<3)|(chan[c.chan].vib));
2536 }
2537 break;
2538 case DIV_CMD_MULTIPCM_VIB:
2539 if (PCM_CHECK(c.chan)) {
2540 chan[c.chan].vib=c.value&7;
2541 rWrite(PCM_ADDR_LFO_VIB+PCM_REG(c.chan),(chan[c.chan].lfo<<3)|(chan[c.chan].vib));
2542 }
2543 break;
2544 case DIV_CMD_MULTIPCM_AM:
2545 if (PCM_CHECK(c.chan)) {
2546 chan[c.chan].am=c.value&7;
2547 rWrite(PCM_ADDR_AM+PCM_REG(c.chan),chan[c.chan].am);
2548 }
2549 break;
2550 case DIV_CMD_MULTIPCM_AR:
2551 if (PCM_CHECK(c.chan)) {
2552 chan[c.chan].ar=c.value&0xf;
2553 rWrite(PCM_ADDR_AR_D1R+PCM_REG(c.chan),(chan[c.chan].ar<<4)|(chan[c.chan].d1r));
2554 }
2555 break;
2556 case DIV_CMD_MULTIPCM_D1R:
2557 if (PCM_CHECK(c.chan)) {
2558 chan[c.chan].d1r=c.value&0xf;
2559 rWrite(PCM_ADDR_AR_D1R+PCM_REG(c.chan),(chan[c.chan].ar<<4)|(chan[c.chan].d1r));
2560 }
2561 break;
2562 case DIV_CMD_MULTIPCM_DL:
2563 if (PCM_CHECK(c.chan)) {
2564 chan[c.chan].dl=c.value&0xf;
2565 rWrite(PCM_ADDR_DL_D2R+PCM_REG(c.chan),(chan[c.chan].dl<<4)|(chan[c.chan].d2r));
2566 }
2567 break;
2568 case DIV_CMD_MULTIPCM_D2R:
2569 if (PCM_CHECK(c.chan)) {
2570 chan[c.chan].d2r=c.value&0xf;
2571 rWrite(PCM_ADDR_DL_D2R+PCM_REG(c.chan),(chan[c.chan].dl<<4)|(chan[c.chan].d2r));
2572 }
2573 break;
2574 case DIV_CMD_MULTIPCM_RC:
2575 if (PCM_CHECK(c.chan)) {
2576 chan[c.chan].rc=c.value&0xf;
2577 rWrite(PCM_ADDR_RC_RR+PCM_REG(c.chan),(chan[c.chan].rc<<4)|(chan[c.chan].rr));
2578 }
2579 break;
2580 case DIV_CMD_MULTIPCM_RR:
2581 if (PCM_CHECK(c.chan)) {
2582 chan[c.chan].rr=c.value&0xf;
2583 rWrite(PCM_ADDR_RC_RR+PCM_REG(c.chan),(chan[c.chan].rc<<4)|(chan[c.chan].rr));
2584 }
2585 break;
2586 case DIV_CMD_MULTIPCM_DAMP:
2587 if (PCM_CHECK(c.chan)) {
2588 chan[c.chan].damp=c.value&1;
2589 chan[c.chan].freqChanged=true;
2590 chan[c.chan].writeCtrl=true;
2591 }
2592 break;
2593 case DIV_CMD_MULTIPCM_PSEUDO_REVERB:
2594 if (PCM_CHECK(c.chan)) {
2595 chan[c.chan].pseudoReverb=c.value&1;
2596 chan[c.chan].freqChanged=true;
2597 }
2598 break;
2599 case DIV_CMD_MULTIPCM_LFO_RESET:
2600 if (PCM_CHECK(c.chan)) {
2601 chan[c.chan].lfoReset=c.value&1;
2602 chan[c.chan].freqChanged=true;
2603 chan[c.chan].writeCtrl=true;
2604 }
2605 break;
2606 case DIV_CMD_MULTIPCM_LEVEL_DIRECT:
2607 if (PCM_CHECK(c.chan)) {
2608 immWrite(PCM_ADDR_TL+PCM_REG(c.chan),((0x7f-chan[c.chan].outVol)<<1)|(chan[c.chan].levelDirect?1:0));
2609 }
2610 break;
2611 case DIV_CMD_MACRO_OFF:
2612 chan[c.chan].std.mask(c.value,true);
2613 break;
2614 case DIV_CMD_MACRO_ON:
2615 chan[c.chan].std.mask(c.value,false);
2616 break;
2617 case DIV_CMD_MACRO_RESTART:
2618 chan[c.chan].std.restart(c.value);
2619 break;
2620 case DIV_CMD_GET_VOLMAX:
2621 if (PCM_CHECK(c.chan)) return 127;
2622 if (c.chan==adpcmChan) return 255;
2623 if (pretendYMU) return 127;
2624 return 63;
2625 break;
2626 case DIV_CMD_PRE_PORTA:
2627 if (PCM_CHECK(c.chan) && chan[c.chan].active && c.value2) {
2628 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_MULTIPCM));
2629 }
2630 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) {
2631 chan[c.chan].baseFreq=(PCM_CHECK(c.chan))?NOTE_PCM(chan[c.chan].note):
2632 ((c.chan==adpcmChan)?(NOTE_ADPCMB(chan[c.chan].note)):(NOTE_FREQUENCY(chan[c.chan].note)));
2633 }
2634 chan[c.chan].inPorta=c.value;
2635 break;
2636 case DIV_CMD_PRE_NOTE:
2637 break;
2638 default:
2639 //printf("WARNING: unimplemented command %d\n",c.cmd);
2640 break;
2641 }
2642 return 1;
2643 }
2644
2645 void DivPlatformOPL::forceIns() {
2646 int oplChans=0;
2647 if (oplType==3) {
2648 chanMap=properDrums?chanMapOPL3Drums:chanMapOPL3;
2649 melodicChans=properDrums?15:18;
2650 totalChans=properDrums?20:18;
2651 oplChans=totalChans;
2652 if (chipType==4) {
2653 pcmChanOffs=totalChans;
2654 totalChans+=24;
2655 }
2656 } else {
2657 chanMap=properDrums?chanMapOPL2Drums:chanMapOPL2;
2658 melodicChans=properDrums?6:9;
2659 totalChans=properDrums?11:9;
2660 oplChans=totalChans;
2661 }
2662 for (int i=0; i<oplChans; i++) {
2663 int ops=(slots[3][i]!=255 && chan[i].state.ops==4 && oplType==3)?4:2;
2664 chan[i].insChanged=true;
2665 chan[i].freqChanged=true;
2666 chan[i].fourOp=(ops==4);
2667 /*
2668 for (int j=0; j<ops; j++) {
2669 unsigned char slot=slots[j][i];
2670 if (slot==255) continue;
2671 unsigned short baseAddr=slotMap[slot];
2672 DivInstrumentFM::Operator& op=chan[i].state.op[(ops==4)?orderedOpsL[j]:j];
2673
2674 if (isMuted[i]) {
2675 rWrite(baseAddr+ADDR_KSL_TL,63|(op.ksl<<6));
2676 } else {
2677 if (KVSL(i,j) || i>melodicChans) {
2678 rWrite(baseAddr+ADDR_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[i].outVol&0x3f,63))|(op.ksl<<6));
2679 } else {
2680 rWrite(baseAddr+ADDR_KSL_TL,op.tl|(op.ksl<<6));
2681 }
2682 }
2683
2684 rWrite(baseAddr+ADDR_AM_VIB_SUS_KSR_MULT,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
2685 rWrite(baseAddr+ADDR_AR_DR,(op.ar<<4)|op.dr);
2686 rWrite(baseAddr+ADDR_SL_RR,(op.sl<<4)|op.rr);
2687 if (oplType>1) {
2688 rWrite(baseAddr+ADDR_WS,op.ws&((oplType==3)?7:3));
2689 }
2690 }
2691 */
2692
2693 if (isMuted[i] && i<=melodicChans) {
2694 rWrite(chanMap[i]+ADDR_LR_FB_ALG,(chan[i].state.alg&1)|(chan[i].state.fb<<1));
2695 if (ops==4) {
2696 rWrite(chanMap[i+1]+ADDR_LR_FB_ALG,((chan[i].state.alg>>1)&1)|(chan[i].state.fb<<1));
2697 }
2698 } else {
2699 rWrite(chanMap[i]+ADDR_LR_FB_ALG,(chan[i].state.alg&1)|(chan[i].state.fb<<1)|((chan[i].pan&15)<<4));
2700 if (ops==4) {
2701 rWrite(chanMap[i+1]+ADDR_LR_FB_ALG,((chan[i].state.alg>>1)&1)|(chan[i].state.fb<<1)|((chan[i].pan&15)<<4));
2702 }
2703 }
2704 }
2705 for (int i=0; i<768; i++) {
2706 oldWrites[i]=-1;
2707 }
2708 immWrite(0xbd,(dam<<7)|(dvb<<6)|(properDrums<<5)|drumState);
2709 update4OpMask=true;
2710 }
2711
2712 void DivPlatformOPL::toggleRegisterDump(bool enable) {
2713 DivDispatch::toggleRegisterDump(enable);
2714 }
2715
2716 void* DivPlatformOPL::getChanState(int ch) {
2717 return &chan[ch];
2718 }
2719
2720 DivMacroInt* DivPlatformOPL::getChanMacroInt(int ch) {
2721 return &chan[ch].std;
2722 }
2723
2724 unsigned short DivPlatformOPL::getPan(int ch) {
2725 if (totalOutputs<=1) return 0;
2726 if (PCM_CHECK(ch)) {
2727 return parent->convertPanLinearToSplit(8^chan[ch].pan,8,15);
2728 }
2729 /*if (chan[ch&(~1)].fourOp) {
2730 if (ch&1) {
2731 return ((chan[ch-1].pan&2)<<7)|(chan[ch-1].pan&1);
2732 } else {
2733 return ((chan[ch+1].pan&2)<<7)|(chan[ch+1].pan&1);
2734 }
2735 }*/
2736 return ((chan[ch].pan&1)<<8)|((chan[ch].pan&2)>>1);
2737 }
2738
2739 void DivPlatformOPL::getPaired(int ch, std::vector<DivChannelPair>& ret) {
2740 if (oplType==3 && ch<12 && !(ch&1)) {
2741 if (chan[ch].fourOp) {
2742 ret.push_back(DivChannelPair(_("4OP"),ch+1));
2743 }
2744 }
2745 }
2746
2747 DivDispatchOscBuffer* DivPlatformOPL::getOscBuffer(int ch) {
2748 if (oplType==759 || chipType==8950) {
2749 if (ch>=totalChans+1) return NULL;
2750 } else {
2751 if (ch>=totalChans) return NULL;
2752 }
2753 if (oplType==3 && ch<12) {
2754 if (chan[ch&(~1)].fourOp) {
2755 if (ch&1) {
2756 return oscBuf[ch-1];
2757 } else {
2758 return oscBuf[ch+1];
2759 }
2760 }
2761 }
2762 return oscBuf[ch];
2763 }
2764
2765 int DivPlatformOPL::mapVelocity(int ch, float vel) {
2766 if (PCM_CHECK(ch)) { // TODO: correct?
2767 // -0.375dB per step
2768 // -6: 64: 16
2769 // -12: 32: 32
2770 // -18: 16: 48
2771 // -24: 8: 64
2772 // -30: 4: 80
2773 // -36: 2: 96
2774 // -42: 1: 112
2775 if (vel==0) return 0;
2776 if (vel>=1.0) return 127;
2777 return CLAMP(round(128.0-(112.0-log2(vel*127.0)*16.0)),0,127);
2778 }
2779 if (ch==adpcmChan) return vel*255.0;
2780 // -0.75dB per step
2781 // -6: 64: 8
2782 // -12: 32: 16
2783 // -18: 16: 24
2784 // -24: 8: 32
2785 // -30: 4: 40
2786 // -36: 2: 48
2787 // -42: 1: 56
2788 if (vel==0) return 0;
2789 if (vel>=1.0) return 63;
2790 return CLAMP(round(64.0-(56.0-log2(vel*127.0)*8.0)),0,63);
2791 }
2792
2793 float DivPlatformOPL::getGain(int ch, int vol) {
2794 if (vol==0) return 0;
2795 if (PCM_CHECK(ch)) return 1.0/pow(10.0,(float)(127-vol)*0.375/20.0);
2796 if (ch==adpcmChan) return (float)vol/255.0;
2797 return 1.0/pow(10.0,(float)(63-vol)*0.75/20.0);
2798 }
2799
2800 unsigned char* DivPlatformOPL::getRegisterPool() {
2801 return regPool;
2802 }
2803
2804 int DivPlatformOPL::getRegisterPoolSize() {
2805 return (chipType==4)?768:((oplType<3)?256:512);
2806 }
2807
2808 void DivPlatformOPL::reset() {
2809 while (!writes.empty()) writes.pop();
2810 memset(regPool,0,768);
2811
2812 downsamplerStep=0;
2813 oldOpMask=0;
2814 dacVal=0;
2815 dacVal2=0;
2816 dacOut=0;
2817 dacOut3[0]=0;
2818 dacOut3[1]=0;
2819 dacOut3[2]=0;
2820 dacOut3[3]=0;
2821 lastSH=false;
2822 lastSH2=false;
2823 lastSY=false;
2824 waitingBusy=true;
2825
2826 const unsigned int downsampledRate=(unsigned int)((double)rate*round(COLOR_NTSC/72.0)/(double)chipRateBase);
2827
2828 if (emuCore==2) {
2829 if (chipType==3 || chipType==759 || chipType==4) {
2830 // reset 3
2831 memset(&fm_lle3,0,sizeof(fmopl3_t));
2832 fm_lle3.input.ic=0;
2833 for (int i=0; i<400; i++) {
2834 fm_lle3.input.mclk=1;
2835 FMOPL3_Clock(&fm_lle3);
2836 fm_lle3.input.mclk=0;
2837 FMOPL3_Clock(&fm_lle3);
2838 }
2839 fm_lle3.input.ic=1;
2840 } else {
2841 // reset 2
2842 memset(&fm_lle2,0,sizeof(fmopl2_t));
2843 fm_lle2.input.ic=0;
2844 for (int i=0; i<80; i++) {
2845 fm_lle2.input.mclk=1;
2846 FMOPL2_Clock(&fm_lle2);
2847 fm_lle2.input.mclk=0;
2848 FMOPL2_Clock(&fm_lle2);
2849 }
2850 fm_lle2.input.ic=1;
2851 }
2852 } else if (emuCore==1) {
2853 switch (chipType) {
2854 case 1:
2855 fm_ymfm1->reset();
2856 break;
2857 case 2:
2858 fm_ymfm2->reset();
2859 break;
2860 case 8950:
2861 fm_ymfm8950->reset();
2862 break;
2863 case 3: case 759:
2864 fm_ymfm3->reset();
2865 break;
2866 case 4:
2867 fm_ymfm4->reset();
2868 break;
2869 }
2870 } else {
2871 if (downsample) {
2872 OPL3_Reset(&fm,downsampledRate);
2873 } else {
2874 OPL3_Reset(&fm,rate);
2875 }
2876 }
2877 pcm.reset();
2878
2879 properDrums=properDrumsSys;
2880 if (oplType==3) {
2881 chanMap=properDrums?chanMapOPL3Drums:chanMapOPL3;
2882 outChanMap=outChanMapOPL3;
2883 melodicChans=properDrums?15:18;
2884 totalChans=properDrums?20:18;
2885 if (chipType==4) {
2886 pcmChanOffs=totalChans;
2887 totalChans+=24;
2888 }
2889 } else {
2890 chanMap=properDrums?chanMapOPL2Drums:chanMapOPL2;
2891 outChanMap=outChanMapOPL2;
2892 melodicChans=properDrums?6:9;
2893 totalChans=properDrums?11:9;
2894 }
2895
2896 for (int i=0; i<totalChans; i++) {
2897 chan[i]=DivPlatformOPL::Channel();
2898 chan[i].std.setEngine(parent);
2899 chan[i].vol=(PCM_CHECK(i))?0x7f:0x3f;
2900 chan[i].outVol=(PCM_CHECK(i))?0x7f:0x3f;
2901 chan[i].pan=(PCM_CHECK(i))?0:3;
2902 }
2903
2904 if (adpcmChan>=0) {
2905 chan[adpcmChan]=DivPlatformOPL::Channel();
2906 chan[adpcmChan].std.setEngine(parent);
2907 chan[adpcmChan].vol=0xff;
2908 chan[adpcmChan].outVol=0xff;
2909
2910 adpcmB->reset();
2911
2912 // volume
2913 immWrite(18,(isMuted[adpcmChan]?0:0xff));
2914 // ADPCM limit
2915 immWrite(20,0xff);
2916 immWrite(19,0xff);
2917 }
2918
2919 if (oplType<3) for (int i=0; i<melodicChans; i++) {
2920 fm.channel[outChanMap[i]].muted=isMuted[i];
2921 }
2922
2923 for (int i=0; i<768; i++) {
2924 oldWrites[i]=-1;
2925 pendingWrites[i]=-1;
2926 }
2927
2928 lastBusy=60;
2929 lfoValue=8;
2930 drumState=0;
2931 sampleBank=0;
2932
2933 drumVol[0]=0;
2934 drumVol[1]=0;
2935 drumVol[2]=0;
2936 drumVol[3]=0;
2937 drumVol[4]=0;
2938
2939 if (oplType==2) { // enable OPL2 waveforms
2940 immWrite(0x01,0x20);
2941 }
2942
2943 if (oplType==3) { // enable OPL3 features
2944 if (chipType==4) {
2945 immWrite(0x105,3);
2946 // Reset wavetable header
2947 immWrite(0x202,PCM_IN_RAM?0x10:0x00);
2948 // initialize mixer volume
2949 fmMixL=7;
2950 fmMixR=7;
2951 pcmMixL=7;
2952 pcmMixR=7;
2953 immWrite(PCM_ADDR_MIX_FM,((7-fmMixR)<<3)|(7-fmMixL));
2954 immWrite(PCM_ADDR_MIX_PCM,((7-pcmMixR)<<3)|(7-pcmMixL));
2955 } else {
2956 immWrite(0x105,1);
2957 }
2958 }
2959
2960 if (dumpWrites) {
2961 addWrite(0xffffffff,0);
2962 }
2963
2964 update4OpMask=true;
2965 dam=false;
2966 dvb=false;
2967 delay=0;
2968
2969 immWrite(0xbd,(dam<<7)|(dvb<<6)|(properDrums<<5)|drumState);
2970 }
2971
2972 int DivPlatformOPL::getOutputCount() {
2973 return totalOutputs;
2974 }
2975
2976 bool DivPlatformOPL::keyOffAffectsArp(int ch) {
2977 return false;
2978 }
2979
2980 bool DivPlatformOPL::keyOffAffectsPorta(int ch) {
2981 return false;
2982 }
2983
2984 bool DivPlatformOPL::getLegacyAlwaysSetVolume() {
2985 return false;
2986 }
2987
2988 void DivPlatformOPL::notifyInsChange(int ins) {
2989 for (int i=0; i<totalChans; i++) {
2990 if (chan[i].ins==ins) {
2991 chan[i].insChanged=true;
2992 }
2993 }
2994 }
2995
2996 void DivPlatformOPL::notifyInsDeletion(void* ins) {
2997 for (int i=0; i<totalChans; i++) {
2998 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
2999 }
3000 }
3001
3002 void DivPlatformOPL::poke(unsigned int addr, unsigned short val) {
3003 immWrite(addr,val);
3004 }
3005
3006 void DivPlatformOPL::poke(std::vector<DivRegWrite>& wlist) {
3007 for (DivRegWrite& i: wlist) immWrite(i.addr,i.val);
3008 }
3009
3010 int DivPlatformOPL::getPortaFloor(int ch) {
3011 return (ch>5)?12:0;
3012 }
3013
3014 void DivPlatformOPL::setCore(unsigned char which) {
3015 emuCore=which;
3016 }
3017
3018 void DivPlatformOPL::setOPLType(int type, bool drums) {
3019 pretendYMU=false;
3020 downsample=false;
3021 adpcmChan=-1;
3022 switch (type) {
3023 case 1: case 2: case 8950:
3024 slotsNonDrums=slotsOPL2;
3025 slotsDrums=slotsOPL2Drums;
3026 slots=drums?slotsDrums:slotsNonDrums;
3027 chanMap=drums?chanMapOPL2Drums:chanMapOPL2;
3028 outChanMap=outChanMapOPL2;
3029 chipFreqBase=32768*72;
3030 chans=9;
3031 melodicChans=drums?6:9;
3032 totalChans=drums?11:9;
3033 if (type==8950) {
3034 adpcmChan=drums?11:9;
3035 }
3036 totalOutputs=1;
3037 break;
3038 case 3: case 4: case 759:
3039 slotsNonDrums=slotsOPL3;
3040 slotsDrums=slotsOPL3Drums;
3041 slots=drums?slotsDrums:slotsNonDrums;
3042 chanMap=drums?chanMapOPL3Drums:chanMapOPL3;
3043 outChanMap=outChanMapOPL3;
3044 chipFreqBase=32768*288;
3045 chans=18;
3046 melodicChans=drums?15:18;
3047 totalChans=drums?20:18;
3048 if (type==759) {
3049 pretendYMU=true;
3050 adpcmChan=16;
3051 } else if (type==4) {
3052 pcmChanOffs=totalChans;
3053 totalChans+=24;
3054 chipFreqBase=32768*684;
3055 downsample=true;
3056 }
3057 totalOutputs=(type==4)?6:4;
3058 break;
3059 }
3060 chipType=type;
3061 if (type==759 || type==4) {
3062 oplType=3;
3063 } else if (type==8950) {
3064 oplType=1;
3065 } else {
3066 oplType=type;
3067 }
3068 properDrumsSys=drums;
3069 }
3070
3071 void DivPlatformOPL::setFlags(const DivConfig& flags) {
3072 /*
3073 if (flags==3) {
3074 chipClock=COLOR_NTSC*12.0/7.0;
3075 } else if (flags==2) {
3076 chipClock=8000000.0;
3077 } else if (flags==1) {
3078 chipClock=COLOR_PAL*12.0/7.0;
3079 } else {
3080 chipClock=COLOR_NTSC*15.0/7.0;
3081 }
3082 ladder=flags&0x80000000;
3083 OPN2_SetChipType(ladder?ym3438_mode_ym2612:0);
3084 if (useYMFM) {
3085 if (fm_ymfm!=NULL) delete fm_ymfm;
3086 if (ladder) {
3087 fm_ymfm=new ymfm::ym2612(iface);
3088 } else {
3089 fm_ymfm=new ymfm::ym3438(iface);
3090 }
3091 rate=chipClock/144;
3092 } else {
3093 rate=chipClock/36;
3094 }*/
3095
3096 compatPan=false;
3097
3098 switch (chipType) {
3099 default:
3100 case 1: case 2: case 8950:
3101 switch (flags.getInt("clockSel",0)) {
3102 case 0x01:
3103 chipClock=COLOR_PAL*4.0/5.0;
3104 break;
3105 case 0x02:
3106 chipClock=4000000.0;
3107 break;
3108 case 0x03:
3109 chipClock=3000000.0;
3110 break;
3111 case 0x04:
3112 chipClock=38400*13*8; // 31948800/8
3113 break;
3114 case 0x05:
3115 chipClock=3500000.0;
3116 break;
3117 default:
3118 chipClock=COLOR_NTSC;
3119 break;
3120 }
3121 CHECK_CUSTOM_CLOCK;
3122 rate=chipClock/72;
3123 chipRateBase=rate;
3124 break;
3125 case 3:
3126 switch (flags.getInt("clockSel",0)) {
3127 case 0x01:
3128 chipClock=COLOR_PAL*16.0/5.0;
3129 break;
3130 case 0x02:
3131 chipClock=14000000.0;
3132 break;
3133 case 0x03:
3134 chipClock=16000000.0;
3135 break;
3136 case 0x04:
3137 chipClock=15000000.0;
3138 break;
3139 case 0x05:
3140 chipClock=33868800.0;
3141 break;
3142 default:
3143 chipClock=COLOR_NTSC*4.0;
3144 break;
3145 }
3146 CHECK_CUSTOM_CLOCK;
3147 switch (flags.getInt("chipType",0)) {
3148 case 1: // YMF289B
3149 chipFreqBase=32768*684;
3150 if (emuCore==2) {
3151 rate=chipClock/684;
3152 } else {
3153 rate=chipClock/768;
3154 }
3155 chipRateBase=chipClock/684;
3156 downsample=true;
3157 totalOutputs=2; // Stereo output only
3158 break;
3159 default: // YMF262
3160 chipFreqBase=32768*288;
3161 rate=chipClock/288;
3162 chipRateBase=rate;
3163 downsample=false;
3164 totalOutputs=4;
3165 break;
3166 }
3167 if (emuCore!=1 && emuCore!=2) {
3168 if (downsample) {
3169 const unsigned int downsampledRate=(unsigned int)((double)rate*round(COLOR_NTSC/72.0)/(double)chipRateBase);
3170 OPL3_Resample(&fm,downsampledRate);
3171 } else {
3172 OPL3_Resample(&fm,rate);
3173 }
3174 }
3175 break;
3176 case 4:
3177 switch (flags.getInt("clockSel",0)) {
3178 case 0x01:
3179 chipClock=COLOR_NTSC*8.0;
3180 break;
3181 case 0x02:
3182 chipClock=COLOR_PAL*32.0/5.0;
3183 break;
3184 default:
3185 chipClock=33868800.0;
3186 break;
3187 }
3188 switch (flags.getInt("ramSize",0)) {
3189 case 0x01: // 2MB (512KB 512KB 512KB 512KB)
3190 ramSize=0x200000;
3191 break;
3192 case 0x02: // 1MB (512KB 512KB)
3193 ramSize=0x100000;
3194 break;
3195 case 0x03: // 640KB (512KB 128KB)
3196 ramSize=0xa0000;
3197 break;
3198 case 0x04: // 512KB
3199 ramSize=0x80000;
3200 break;
3201 case 0x05: // 256KB (128KB 128KB)
3202 ramSize=0x40000;
3203 break;
3204 case 0x06: // 128KB
3205 ramSize=0x20000;
3206 break;
3207 default:
3208 ramSize=0x400000;
3209 break;
3210 }
3211 CHECK_CUSTOM_CLOCK;
3212 pcm.setClockFrequency(chipClock);
3213 rate=chipClock/768;
3214 chipRateBase=chipClock/684;
3215 immWrite(0x202,PCM_IN_RAM?0x10:0x00);
3216 break;
3217 case 759:
3218 rate=48000;
3219 chipRateBase=rate;
3220 chipClock=rate*288;
3221 break;
3222 }
3223 compatPan=flags.getBool("compatPan",false);
3224 compatYPitch=flags.getBool("compatYPitch",false);
3225
3226 for (int i=0; i<44; i++) {
3227 oscBuf[i]->setRate(rate);
3228 }
3229 }
3230
3231 const void* DivPlatformOPL::getSampleMem(int index) {
3232 return (index==0 && pcmChanOffs>=0)?pcmMem:
3233 (index==0 && adpcmChan>=0)?adpcmBMem:NULL;
3234 }
3235
3236 size_t DivPlatformOPL::getSampleMemCapacity(int index) {
3237 return (index==0 && pcmChanOffs>=0)?
3238 (PCM_IN_RAM?0x200000+ramSize:ramSize):
3239 ((index==0 && adpcmChan>=0)?262144:0);
3240 }
3241
3242 size_t DivPlatformOPL::getSampleMemUsage(int index) {
3243 return (index==0 && pcmChanOffs>=0)?pcmMemLen:
3244 (index==0 && adpcmChan>=0)?adpcmBMemLen:0;
3245 }
3246
3247 bool DivPlatformOPL::isSampleLoaded(int index, int sample) {
3248 if (index!=0) return false;
3249 if (sample<0 || sample>32767) return false;
3250 return sampleLoaded[sample];
3251 }
3252
3253 const DivMemoryComposition* DivPlatformOPL::getMemCompo(int index) {
3254 if ((adpcmChan<0) && (pcmChanOffs<0)) return NULL;
3255 if (index!=0) return NULL;
3256 return &memCompo;
3257 }
3258
3259 void DivPlatformOPL::renderSamples(int sysID) {
3260 if (adpcmChan<0 && pcmChanOffs<0) return;
3261 if (adpcmChan>=0 && adpcmBMem!=NULL) {
3262 memset(adpcmBMem,0,262144);
3263 }
3264 if (pcmChanOffs>=0 && pcmMem!=NULL) {
3265 memset(pcmMem,0,4194304);
3266 }
3267 memset(sampleOffPCM,0,32768*sizeof(unsigned int));
3268 memset(sampleOffB,0,32768*sizeof(unsigned int));
3269 memset(sampleLoaded,0,32768*sizeof(bool));
3270
3271 memCompo=DivMemoryComposition();
3272 memCompo.name="Sample Memory";
3273
3274 if (pcmChanOffs>=0) { // OPL4 PCM
3275 size_t memPos=(PCM_IN_RAM?0x200600:0x1800);
3276 const int maxSample=PCM_IN_RAM?128:512;
3277 int sampleCount=parent->song.sampleLen;
3278 if (sampleCount>maxSample) {
3279 // mark the rest as unavailable
3280 for (int i=maxSample; i<sampleCount; i++) {
3281 sampleLoaded[i]=false;
3282 }
3283 sampleCount=maxSample;
3284 }
3285 for (int i=0; i<sampleCount; i++) {
3286 DivSample* s=parent->song.sample[i];
3287 if (!s->renderOn[0][sysID]) {
3288 sampleOffPCM[i]=0;
3289 continue;
3290 }
3291
3292 int length;
3293 int sampleLength;
3294 unsigned char* src=(unsigned char*)s->getCurBuf();
3295 switch (s->depth) {
3296 case DIV_SAMPLE_DEPTH_8BIT:
3297 sampleLength=s->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT);
3298 length=MIN(65535,sampleLength+1);
3299 break;
3300 case DIV_SAMPLE_DEPTH_12BIT:
3301 sampleLength=s->getLoopEndPosition(DIV_SAMPLE_DEPTH_12BIT);
3302 length=MIN(98303,sampleLength+3);
3303 break;
3304 case DIV_SAMPLE_DEPTH_16BIT:
3305 sampleLength=s->getLoopEndPosition(DIV_SAMPLE_DEPTH_16BIT);
3306 length=MIN(131070,sampleLength+2);
3307 break;
3308 default:
3309 sampleLength=s->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT);
3310 length=MIN(65535,sampleLength+1);
3311 src=(unsigned char*)s->data8;
3312 break;
3313 }
3314 if (sampleLength<1) length=0;
3315 int actualLength=MIN((int)(getSampleMemCapacity(0)-memPos),length);
3316 if (actualLength>0) {
3317 if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
3318 for (int i=0, j=0; i<actualLength; i++, j++) {
3319 if (j>=sampleLength) j=sampleLength-2;
3320 #ifdef TA_BIG_ENDIAN
3321 pcmMem[memPos+i]=src[j];
3322 #else
3323 pcmMem[memPos+i]=src[j^1];
3324 #endif
3325 }
3326 } else {
3327 for (int i=0, j=0; i<actualLength; i++, j++) {
3328 if (j>=sampleLength && s->depth!=DIV_SAMPLE_DEPTH_12BIT) j=sampleLength-1;
3329 pcmMem[memPos+i]=src[j];
3330 }
3331 }
3332 sampleOffPCM[i]=memPos;
3333 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+length));
3334 memPos+=length;
3335 }
3336 if (actualLength<length) {
3337 logW("out of OPL4 PCM memory for sample %d!",i);
3338 break;
3339 }
3340 sampleLoaded[i]=true;
3341 }
3342 pcmMemLen=memPos+256;
3343
3344 // instrument table
3345 for (int i=0; i<sampleCount; i++) {
3346 DivSample* s=parent->song.sample[i];
3347 unsigned int insAddr=(i*12)+(PCM_IN_RAM?0x200000:0);
3348 unsigned char bitDepth;
3349 int endPos=CLAMP(s->isLoopable()?s->loopEnd:(s->samples+1),1,0x10000);
3350 int loop=s->isLoopable()?CLAMP(s->loopStart,0,endPos-2):(endPos-2);
3351 switch (s->depth) {
3352 case DIV_SAMPLE_DEPTH_8BIT:
3353 bitDepth=0;
3354 break;
3355 case DIV_SAMPLE_DEPTH_12BIT:
3356 bitDepth=1;
3357 if (!s->isLoopable()) {
3358 endPos++;
3359 loop++;
3360 }
3361 break;
3362 case DIV_SAMPLE_DEPTH_16BIT:
3363 bitDepth=2;
3364 break;
3365 default:
3366 bitDepth=0;
3367 break;
3368 }
3369 pcmMem[insAddr]=(bitDepth<<6)|((sampleOffPCM[i]>>16)&0x3f);
3370 pcmMem[1+insAddr]=(sampleOffPCM[i]>>8)&0xff;
3371 pcmMem[2+insAddr]=(sampleOffPCM[i])&0xff;
3372 pcmMem[3+insAddr]=(loop>>8)&0xff;
3373 pcmMem[4+insAddr]=(loop)&0xff;
3374 pcmMem[5+insAddr]=((~(endPos-1))>>8)&0xff;
3375 pcmMem[6+insAddr]=(~(endPos-1))&0xff;
3376 // on MultiPCM this consists of instrument params, but on OPL4 this is not used
3377 pcmMem[7+insAddr]=0; // LFO, VIB
3378 pcmMem[8+insAddr]=(0xf<<4)|(0xf<<0); // AR, D1R
3379 pcmMem[9+insAddr]=0; // DL, D2R
3380 pcmMem[10+insAddr]=(0xf<<4)|(0xf<<0); // RC, RR
3381 pcmMem[11+insAddr]=0; // AM
3382 }
3383 if (PCM_IN_RAM) {
3384 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_RESERVED,"ROM data",0,0,0x200000));
3385 }
3386
3387 memCompo.used=pcmMemLen;
3388 } else if (adpcmChan>=0) { // ADPCM
3389 size_t memPos=0;
3390 for (int i=0; i<parent->song.sampleLen; i++) {
3391 DivSample* s=parent->song.sample[i];
3392 if (!s->renderOn[0][sysID]) {
3393 sampleOffB[i]=0;
3394 continue;
3395 }
3396
3397 int paddedLen=(s->lengthB+255)&(~0xff);
3398 if ((memPos&0xf00000)!=((memPos+paddedLen)&0xf00000)) {
3399 memPos=(memPos+0xfffff)&0xf00000;
3400 }
3401 if (memPos>=getSampleMemCapacity(0)) {
3402 logW("out of ADPCM memory for sample %d!",i);
3403 break;
3404 }
3405 if (memPos+paddedLen>=getSampleMemCapacity(0)) {
3406 memcpy(adpcmBMem+memPos,s->dataB,getSampleMemCapacity(0)-memPos);
3407 logW("out of ADPCM memory for sample %d!",i);
3408 } else {
3409 memcpy(adpcmBMem+memPos,s->dataB,paddedLen);
3410 sampleLoaded[i]=true;
3411 }
3412 sampleOffB[i]=memPos;
3413 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+paddedLen));
3414 memPos+=paddedLen;
3415 }
3416 adpcmBMemLen=memPos+256;
3417
3418 memCompo.used=adpcmBMemLen;
3419 }
3420 memCompo.capacity=getSampleMemCapacity(0);
3421 }
3422
3423 int DivPlatformOPL::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
3424 parent=p;
3425 dumpWrites=false;
3426 skipRegisterWrites=false;
3427 for (int i=0; i<44; i++) {
3428 isMuted[i]=false;
3429 }
3430 for (int i=0; i<44; i++) {
3431 oscBuf[i]=new DivDispatchOscBuffer;
3432 }
3433
3434 fm_ymfm1=NULL;
3435 fm_ymfm2=NULL;
3436 fm_ymfm8950=NULL;
3437 fm_ymfm3=NULL;
3438 fm_ymfm4=NULL;
3439
3440 if (emuCore==1) {
3441 switch (chipType) {
3442 case 1:
3443 fm_ymfm1=new ymfm::ym3526(iface);
3444 break;
3445 case 2:
3446 fm_ymfm2=new ymfm::ym3812(iface);
3447 break;
3448 case 8950:
3449 fm_ymfm8950=new ymfm::y8950(iface);
3450 break;
3451 case 3: case 759:
3452 fm_ymfm3=new ymfm::ymf262(iface);
3453 break;
3454 case 4:
3455 fm_ymfm4=new ymfm::ymf278b(iface);
3456 break;
3457 }
3458 }
3459
3460 setFlags(flags);
3461
3462 if (adpcmChan>=0) {
3463 adpcmBMem=new unsigned char[262144];
3464 adpcmBMemLen=0;
3465 iface.adpcmBMem=adpcmBMem;
3466 iface.sampleBank=0;
3467 adpcmB=new ymfm::adpcm_b_engine(iface,2);
3468 }
3469
3470 if (pcmChanOffs>=0) {
3471 pcmMem=new unsigned char[4194304];
3472 pcmMemLen=0;
3473 iface.pcmMem=pcmMem;
3474 iface.sampleBank=0;
3475 pcmMemory.memory=pcmMem;
3476 }
3477
3478 reset();
3479 return totalChans;
3480 }
3481
3482 void DivPlatformOPL::quit() {
3483 for (int i=0; i<44; i++) {
3484 delete oscBuf[i];
3485 }
3486 if (adpcmChan>=0) {
3487 delete adpcmB;
3488 delete[] adpcmBMem;
3489 }
3490 if (pcmChanOffs>=0) {
3491 delete[] pcmMem;
3492 }
3493 if (fm_ymfm1!=NULL) {
3494 delete fm_ymfm1;
3495 fm_ymfm1=NULL;
3496 }
3497 if (fm_ymfm2!=NULL) {
3498 delete fm_ymfm2;
3499 fm_ymfm2=NULL;
3500 }
3501 if (fm_ymfm8950!=NULL) {
3502 delete fm_ymfm8950;
3503 fm_ymfm8950=NULL;
3504 }
3505 if (fm_ymfm3!=NULL) {
3506 delete fm_ymfm3;
3507 fm_ymfm3=NULL;
3508 }
3509 if (fm_ymfm4!=NULL) {
3510 delete fm_ymfm4;
3511 fm_ymfm4=NULL;
3512 }
3513 }
3514
3515 // initialization of important arrays
3516 DivPlatformOPL::DivPlatformOPL():
3517 pcmMemory(0x400000),
3518 pcm(pcmMemory) {
3519 sampleOffPCM=new unsigned int[32768];
3520 sampleOffB=new unsigned int[32768];
3521 sampleLoaded=new bool[32768];
3522 }
3523
3524 DivPlatformOPL::~DivPlatformOPL() {
3525 delete[] sampleOffPCM;
3526 delete[] sampleOffB;
3527 delete[] sampleLoaded;
3528 }