Debian Codegraph @

src/engine/platform/es5506.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 "es5506.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include <math.h>
24
25 #define PITCH_OFFSET ((double)(16*2048*(chanMax+1)))
26 #define NOTE_ES5506(c,note) ((amigaPitch && parent->song.linearPitch!=2)?parent->calcBaseFreq(COLOR_NTSC*16,chan[c].pcm.freqOffs,note,true):parent->calcBaseFreq(chipClock,chan[c].pcm.freqOffs,note,false))
27
28 #define rWrite(a,...) {if(!skipRegisterWrites) {hostIntf32.push_back(QueuedHostIntf(4,(a),__VA_ARGS__)); }}
29 #define immWrite(a,...) {hostIntf32.push_back(QueuedHostIntf(4,(a),__VA_ARGS__));}
30 #define pageWrite(p,a,d) \
31 if (!skipRegisterWrites) { \
32 if (curPage!=(p)) { \
33 curPage=(p); \
34 rWrite(0xf,curPage) \
35 if (dumpWrites) { \
36 addWrite(0x3c,0) \
37 addWrite(0x3d,0) \
38 addWrite(0x3e,0) \
39 addWrite(0x3f,curPage) \
40 } \
41 } \
42 rWrite((a),(d)) \
43 if (dumpWrites) { \
44 addWrite(((a)<<2)|0,((d)>>24)&0xff) \
45 addWrite(((a)<<2)|1,((d)>>16)&0xff) \
46 addWrite(((a)<<2)|2,((d)>>8)&0xff) \
47 addWrite(((a)<<2)|3,((d)>>0)&0xff) \
48 } \
49 }
50
51 #define pageWriteDelayed(p,a,d,dl) \
52 if (!skipRegisterWrites) { \
53 if (curPage!=(p)) { \
54 curPage=(p); \
55 rWrite(0xf,curPage) \
56 if (dumpWrites) { \
57 addWrite(0x3c,0) \
58 addWrite(0x3d,0) \
59 addWrite(0x3e,0) \
60 addWrite(0x3f,curPage) \
61 } \
62 } \
63 rWrite((a),(d),(dl)) \
64 if (dumpWrites) { \
65 addWrite(((a)<<2)|0,((d)>>24)&0xff) \
66 addWrite(((a)<<2)|1,((d)>>16)&0xff) \
67 addWrite(((a)<<2)|2,((d)>>8)&0xff) \
68 addWrite(((a)<<2)|3,((d)>>0)&0xff) \
69 } \
70 }
71
72 #define pageWriteMask(p,pm,a,d) \
73 if (!skipRegisterWrites) { \
74 if ((curPage&(pm))!=((p)&(pm))) { \
75 curPage=(curPage&~(pm))|((p)&(pm)); \
76 rWrite(0xf,curPage) \
77 if (dumpWrites) { \
78 addWrite(0x3c,0) \
79 addWrite(0x3d,0) \
80 addWrite(0x3e,0) \
81 addWrite(0x3f,curPage) \
82 } \
83 } \
84 rWrite((a),(d)) \
85 if (dumpWrites) { \
86 addWrite(((a)<<2)|0,((d)>>24)&0xff) \
87 addWrite(((a)<<2)|1,((d)>>16)&0xff) \
88 addWrite(((a)<<2)|2,((d)>>8)&0xff) \
89 addWrite(((a)<<2)|3,((d)>>0)&0xff) \
90 } \
91 }
92
93 #define crWrite(c,d) \
94 if (!skipRegisterWrites) { \
95 if ((curPage&0x5f)!=((c)&0x5f)) { \
96 curPage=(curPage&~0x5f)|((c)&0x5f); \
97 rWrite(0xf,curPage) \
98 if (dumpWrites) { \
99 addWrite(0x3c,0) \
100 addWrite(0x3d,0) \
101 addWrite(0x3e,0) \
102 addWrite(0x3f,curPage) \
103 } \
104 } \
105 chan[c].cr=(d); \
106 rWrite(0,chan[c].cr) \
107 if (dumpWrites) { \
108 addWrite(0x0,0) \
109 addWrite(0x1,0) \
110 addWrite(0x2,(chan[c].cr>>8)&0xff) \
111 addWrite(0x3,(chan[c].cr>>0)&0xff) \
112 } \
113 }
114
115 #define crWriteMask(c,d,m) \
116 if (!skipRegisterWrites) { \
117 if ((curPage&0x5f)!=((c)&0x5f)) { \
118 curPage=(curPage&~0x5f)|((c)&0x5f); \
119 rWrite(0xf,curPage) \
120 if (dumpWrites) { \
121 addWrite(0x3c,0) \
122 addWrite(0x3d,0) \
123 addWrite(0x3e,0) \
124 addWrite(0x3f,curPage); \
125 } \
126 } \
127 chan[c].cr=(chan[c].cr&~(m))|((d)&(m)); \
128 rWrite(0,chan[c].cr) \
129 if (dumpWrites) { \
130 addWrite(0x0,0) \
131 addWrite(0x1,0) \
132 addWrite(0x2,(chan[c].cr>>8)&0xff) \
133 addWrite(0x3,(chan[c].cr>>0)&0xff) \
134 } \
135 }
136
137 #define pageReadMask(p,pm,a,st,...) \
138 if (!skipRegisterWrites) { \
139 if ((curPage&(pm))!=((p)&(pm))) { \
140 curPage=(curPage&~(pm))|((p)&(pm)); \
141 rWrite(0xf,curPage) \
142 if (dumpWrites) { \
143 addWrite(0x3c,0) \
144 addWrite(0x3d,0) \
145 addWrite(0x3e,0) \
146 addWrite(0x3f,curPage) \
147 } \
148 } \
149 rRead(st,(a),__VA_ARGS__); \
150 }
151
152
153 const char* regCheatSheetES5506[]={
154 "CR", "00|00",
155 "FC", "00|01",
156 "LVOL", "00|02",
157 "LVRAMP", "00|03",
158 "RVOL", "00|04",
159 "RVRAMP", "00|05",
160 "ECOUNT", "00|06",
161 "K2", "00|07",
162 "K2RAMP", "00|08",
163 "K1", "00|09",
164 "K1RAMP", "00|0A",
165 "ACTV", "00|0B",
166 "MODE", "00|0C",
167 "POT", "00|0D",
168 "IRQV", "00|0E",
169 "PAGE", "00|0F",
170 "CR", "20|00",
171 "START", "20|01",
172 "END", "20|02",
173 "ACCUM", "20|03",
174 "O4(n-1)", "20|04",
175 "O3(n-2)", "20|05",
176 "O3(n-1)", "20|06",
177 "O2(n-2)", "20|07",
178 "O2(n-1)", "20|08",
179 "O1(n-1)", "20|09",
180 "W_ST", "20|0A",
181 "W_END", "20|0B",
182 "LR_END", "20|0C",
183 "POT", "20|0D",
184 "IRQV", "20|0E",
185 "PAGE", "20|0F",
186 "CH0L", "40|00",
187 "CH0R", "40|01",
188 "CH1L", "40|02",
189 "CH1R", "40|03",
190 "CH2L", "40|04",
191 "CH2R", "40|05",
192 "CH3L", "40|06",
193 "CH3R", "40|07",
194 "CH4L", "40|08",
195 "CH4R", "40|09",
196 "CH5L", "40|0A",
197 "CH5R", "40|0B",
198 "POT", "40|0D",
199 "IRQV", "40|0E",
200 "PAGE", "40|0F",
201 NULL
202 };
203
204 const char** DivPlatformES5506::getRegisterSheet() {
205 return regCheatSheetES5506;
206 }
207
208 void DivPlatformES5506::acquire(short** buf, size_t len) {
209 for (int i=0; i<chanMax; i++) {
210 oscBuf[i]->begin(len);
211 }
212 for (size_t h=0; h<len; h++) {
213 // convert 32 bit access to 8 bit host interface
214 while (!hostIntf32.empty()) {
215 QueuedHostIntf w=hostIntf32.front();
216 if (w.isRead && (w.read!=NULL)) {
217 hostIntf8.push(QueuedHostIntf(w.state,0,w.addr,w.read));
218 hostIntf8.push(QueuedHostIntf(w.state,1,w.addr,w.read));
219 hostIntf8.push(QueuedHostIntf(w.state,2,w.addr,w.read));
220 hostIntf8.push(QueuedHostIntf(w.state,3,w.addr,w.read,w.delay));
221 } else {
222 hostIntf8.push(QueuedHostIntf(0,w.addr,w.val));
223 hostIntf8.push(QueuedHostIntf(1,w.addr,w.val));
224 hostIntf8.push(QueuedHostIntf(2,w.addr,w.val));
225 hostIntf8.push(QueuedHostIntf(3,w.addr,w.val,w.delay));
226 }
227 hostIntf32.pop();
228 }
229
230 es5506.tick_perf();
231 if (cycle>0) { // wait until delay
232 cycle-=2;
233 } else while (!hostIntf8.empty()) {
234 QueuedHostIntf w=hostIntf8.front();
235 unsigned char shift=24-(w.step<<3);
236 if (w.isRead) {
237 logE("READING?!");
238 hostIntf8.pop();
239 } else {
240 es5506.host_w((w.addr<<2)+w.step,(w.val>>shift)&0xff);
241 if ((w.step==3) && (w.delay>0)) {
242 cycle+=w.delay;
243 }
244 hostIntf8.pop();
245 if (cycle>0) break;
246 }
247 }
248
249 for (int o=0; o<6; o++) {
250 buf[(o<<1)|0][h]=es5506.lout(o);
251 buf[(o<<1)|1][h]=es5506.rout(o);
252 }
253 for (int i=chanMax; i>=0; i--) {
254 oscBuf[i]->putSample(h,(es5506.voice_lout(i)+es5506.voice_rout(i))>>5);
255 }
256 }
257 for (int i=0; i<chanMax; i++) {
258 oscBuf[i]->end(len);
259 }
260 }
261
262 void DivPlatformES5506::e_pin(bool state) {
263 }
264
265 void DivPlatformES5506::irqb(bool state) {
266 /*
267 rRead(0x0e,0x80,&irqv,0x9f);
268 irqTrigger=true;
269 */
270 }
271
272 // modified GUSVolumeTable from Impulse Tracker (SoundDrivers/GUS.INC)
273 static const short amigaVolTable[65]={
274 0x000, 0x8FF, 0x9FF, 0xA80,
275 0xAFF, 0xB40, 0xB80, 0xBC0,
276 0xBFF, 0xC20, 0xC40, 0xC60,
277 0xC80, 0xCA0, 0xCC0, 0xCE0,
278 0xCFF, 0xD10, 0xD20, 0xD30,
279 0xD40, 0xD50, 0xD60, 0xD70,
280 0xD80, 0xD90, 0xDA0, 0xDB0,
281 0xDC0, 0xDD0, 0xDE0, 0xDF0,
282 0xDFF, 0xE08, 0xE10, 0xE18,
283 0xE20, 0xE28, 0xE30, 0xE38,
284 0xE40, 0xE48, 0xE50, 0xE58,
285 0xE60, 0xE68, 0xE70, 0xE78,
286 0xE80, 0xE88, 0xE90, 0xE98,
287 0xEA0, 0xEA8, 0xEB0, 0xEB8,
288 0xEC0, 0xEC8, 0xED0, 0xED8,
289 0xEE0, 0xEE8, 0xEF0, 0xEF8,
290 0xEFF
291 };
292
293 // same thing
294 static const short amigaPanTable[128]={
295 0x000, 0x8FF, 0x9FF, 0xA80, 0xAFF, 0xB40, 0xB80, 0xBC0,
296 0xBFF, 0xC20, 0xC40, 0xC60, 0xC80, 0xCA0, 0xCC0, 0xCE0,
297 0xCFF, 0xD10, 0xD20, 0xD30, 0xD40, 0xD50, 0xD60, 0xD70,
298 0xD80, 0xD90, 0xDA0, 0xDB0, 0xDC0, 0xDD0, 0xDE0, 0xDF0,
299 0xDFF, 0xE08, 0xE10, 0xE18, 0xE20, 0xE28, 0xE30, 0xE38,
300 0xE40, 0xE48, 0xE50, 0xE58, 0xE60, 0xE68, 0xE70, 0xE78,
301 0xE80, 0xE88, 0xE90, 0xE98, 0xEA0, 0xEA8, 0xEB0, 0xEB8,
302 0xEC0, 0xEC8, 0xED0, 0xED8, 0xEE0, 0xEE8, 0xEF0, 0xEF8,
303 0xEFF, 0xF04, 0xF08, 0xF0C, 0xF10, 0xF14, 0xF18, 0xF1C,
304 0xF20, 0xF24, 0xF28, 0xF2C, 0xF30, 0xF34, 0xF38, 0xF3C,
305 0xF40, 0xF44, 0xF48, 0xF4C, 0xF50, 0xF54, 0xF58, 0xF5C,
306 0xF60, 0xF64, 0xF68, 0xF6C, 0xF70, 0xF74, 0xF78, 0xF7C,
307 0xF80, 0xF84, 0xF88, 0xF8C, 0xF90, 0xF94, 0xF98, 0xF9C,
308 0xFA0, 0xFA4, 0xFA8, 0xFAC, 0xFB0, 0xFB4, 0xFB8, 0xFBC,
309 0xFC0, 0xFC4, 0xFC8, 0xFCC, 0xFD0, 0xFD4, 0xFD8, 0xFDC,
310 0xFE0, 0xFE4, 0xFE8, 0xFEC, 0xFF0, 0xFF4, 0xFF8, 0xFFF
311 };
312
313 void DivPlatformES5506::updateNoteChangesAsNeeded(int ch) {
314 if (chan[ch].noteChanged.changed) { // note value changed or frequency offset is changed
315 if (chan[ch].noteChanged.offs) {
316 if (chan[ch].pcm.freqOffs!=chan[ch].pcm.nextFreqOffs) {
317 chan[ch].pcm.freqOffs=chan[ch].pcm.nextFreqOffs;
318 chan[ch].nextFreq=NOTE_ES5506(ch,chan[ch].currNote);
319 chan[ch].noteChanged.freq=1;
320 chan[ch].freqChanged=true;
321 }
322 }
323 if (chan[ch].noteChanged.note) {
324 chan[ch].currNote=chan[ch].nextNote;
325 const int nextFreq=NOTE_ES5506(ch,chan[ch].nextNote);
326 if (chan[ch].nextFreq!=nextFreq) {
327 chan[ch].nextFreq=nextFreq;
328 chan[ch].noteChanged.freq=1;
329 }
330 }
331 if (chan[ch].noteChanged.freq) {
332 if (chan[ch].baseFreq!=chan[ch].nextFreq) {
333 chan[ch].baseFreq=chan[ch].nextFreq;
334 chan[ch].freqChanged=true;
335 }
336 }
337 chan[ch].noteChanged.changed=0;
338 }
339 }
340
341 void DivPlatformES5506::tick(bool sysTick) {
342 for (int i=0; i<=chanMax; i++) {
343 if (chan[i].crDirValInit) {
344 chan[i].crDirVal=es5506.regs_r(i,0,false)&0x41;
345 chan[i].crDirValInit=false;
346 }
347 if (!chan[i].crDirValChanged) {
348 chan[i].crDirVal=es5506.regs_r(i,0,false)&0x41;
349 }
350 chan[i].std.next();
351 DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_ES5506);
352 signed int k1=chan[i].k1Prev,k2=chan[i].k2Prev;
353 // volume/panning macros
354 if (chan[i].std.vol.had) {
355 int nextVol;
356 if (amigaVol) {
357 nextVol=VOL_SCALE_LINEAR(MIN(64,chan[i].vol),MIN(64,chan[i].std.vol.val),64);
358 } else {
359 nextVol=VOL_SCALE_LOG((0xfff*chan[i].vol)/0xff,(0xfff*chan[i].std.vol.val)/chan[i].volMacroMax,0xfff);
360 }
361 if (chan[i].outVol!=nextVol) {
362 chan[i].outVol=nextVol;
363 chan[i].volChanged.lVol=1;
364 chan[i].volChanged.rVol=1;
365 }
366 }
367 if (chan[i].std.panL.had) {
368 const int nextLVol=VOL_SCALE_LOG((0xfff*chan[i].lVol)/0xff,(0xfff*chan[i].std.panL.val)/chan[i].panMacroMax,0xfff);
369 if (chan[i].outLVol!=nextLVol) {
370 chan[i].outLVol=nextLVol;
371 chan[i].volChanged.lVol=1;
372 }
373 }
374 if (chan[i].std.panR.had) {
375 const int nextRVol=VOL_SCALE_LOG((0xfff*chan[i].rVol)/0xff,(0xfff*chan[i].std.panR.val)/chan[i].panMacroMax,0xfff);
376 if (chan[i].outRVol!=nextRVol) {
377 chan[i].outRVol=nextRVol;
378 chan[i].volChanged.rVol=1;
379 }
380 }
381 // arpeggio/pitch macros, frequency related
382 if (NEW_ARP_STRAT) {
383 chan[i].handleArp();
384 } else if (chan[i].std.arp.had) {
385 if (!chan[i].inPorta) {
386 chan[i].nextNote=parent->calcArp(chan[i].note,chan[i].std.arp.val);
387 }
388 chan[i].noteChanged.note=1;
389 }
390 if (chan[i].std.pitch.had) {
391 if (chan[i].std.pitch.mode) {
392 chan[i].pitch2+=chan[i].std.pitch.val;
393 CLAMP_VAR(chan[i].pitch2,-2048,2048);
394 } else {
395 chan[i].pitch2=chan[i].std.pitch.val;
396 }
397 chan[i].freqChanged=true;
398 }
399 // phase reset macro
400 if (chan[i].std.phaseReset.had) {
401 if (chan[i].std.phaseReset.val==1) {
402 chan[i].keyOn=true;
403 }
404 }
405 // filter macros
406 if (chan[i].std.duty.had) {
407 if (chan[i].filter.mode!=DivInstrumentES5506::Filter::FilterMode(chan[i].std.duty.val&3)) {
408 chan[i].filter.mode=DivInstrumentES5506::Filter::FilterMode(chan[i].std.duty.val&3);
409 chan[i].filterChanged.mode=1;
410 }
411 }
412 if (chan[i].std.ex1.had) {
413 switch (chan[i].std.ex1.mode) {
414 case 0: // absolute
415 if (chan[i].filter.k1!=(chan[i].std.ex1.val&0xffff)) {
416 chan[i].filter.k1=chan[i].std.ex1.val&0xffff;
417 chan[i].filterChanged.k1=1;
418 }
419 break;
420 case 1: // relative
421 if (chan[i].k1Offs!=chan[i].std.ex1.val) {
422 chan[i].k1Offs=chan[i].std.ex1.val;
423 chan[i].filterChanged.k1=1;
424 }
425 break;
426 default:
427 break;
428 }
429 }
430 if (chan[i].std.ex2.had) {
431 switch (chan[i].std.ex2.mode) {
432 case 0: // absolute
433 if (chan[i].filter.k2!=(chan[i].std.ex2.val&0xffff)) {
434 chan[i].filter.k2=chan[i].std.ex2.val&0xffff;
435 chan[i].filterChanged.k2=1;
436 }
437 break;
438 case 1: // relative
439 if (chan[i].k2Offs!=chan[i].std.ex1.val) {
440 chan[i].k2Offs=chan[i].std.ex1.val;
441 chan[i].filterChanged.k2=1;
442 }
443 break;
444 default:
445 break;
446 }
447 }
448 // envelope macros
449 if (chan[i].std.ex3.had) {
450 if (chan[i].envelope.ecount!=(chan[i].std.ex3.val&0x1ff)) {
451 chan[i].envelope.ecount=chan[i].std.ex3.val&0x1ff;
452 chan[i].envChanged.ecount=1;
453 }
454 }
455 if (chan[i].std.ex4.had) {
456 if (chan[i].envelope.lVRamp!=chan[i].std.ex4.val) {
457 chan[i].envelope.lVRamp=chan[i].std.ex4.val;
458 chan[i].envChanged.lVRamp=1;
459 }
460 }
461 if (chan[i].std.ex5.had) {
462 if (chan[i].envelope.rVRamp!=chan[i].std.ex5.val) {
463 chan[i].envelope.rVRamp=chan[i].std.ex5.val;
464 chan[i].envChanged.rVRamp=1;
465 }
466 }
467 if (chan[i].std.ex6.had) {
468 if (chan[i].envelope.k1Ramp!=chan[i].std.ex6.val) {
469 chan[i].envelope.k1Ramp=chan[i].std.ex6.val;
470 chan[i].envChanged.k1Ramp=1;
471 }
472 }
473 if (chan[i].std.ex7.had) {
474 if (chan[i].envelope.k2Ramp!=chan[i].std.ex7.val) {
475 chan[i].envelope.k2Ramp=chan[i].std.ex7.val;
476 chan[i].envChanged.k2Ramp=1;
477 }
478 }
479 if (chan[i].std.ex8.had) {
480 if (chan[i].envelope.k1Slow!=(bool)(chan[i].std.ex8.val&1)) {
481 chan[i].envelope.k1Slow=chan[i].std.ex8.val&1;
482 chan[i].envChanged.k1Ramp=1;
483 }
484 if (chan[i].envelope.k2Slow!=(bool)(chan[i].std.ex8.val&2)) {
485 chan[i].envelope.k2Slow=chan[i].std.ex8.val&2;
486 chan[i].envChanged.k2Ramp=1;
487 }
488 }
489 // filter slide
490 if (!chan[i].keyOn) {
491 if (chan[i].k1Slide!=0) {
492 signed int next=CLAMP(chan[i].filter.k1+chan[i].k1Slide,0,65535);
493 if (chan[i].filter.k1!=next) {
494 chan[i].filter.k1=next;
495 chan[i].filterChanged.k1=1;
496 }
497 }
498 if (chan[i].k2Slide!=0) {
499 signed int next=CLAMP(chan[i].filter.k2+chan[i].k2Slide,0,65535);
500 if (chan[i].filter.k2!=next) {
501 chan[i].filter.k2=next;
502 chan[i].filterChanged.k2=1;
503 }
504 }
505 }
506 // channel assignment
507 if (chan[i].active && chan[i].std.fb.had) {
508 const unsigned char ca=CLAMP(chan[i].std.fb.val,0,5);
509 if (chan[i].ca!=ca) {
510 chan[i].ca=ca;
511 if (!chan[i].keyOn) {
512 chan[i].volChanged.ca=1;
513 }
514 }
515 }
516 // control macros
517 if (chan[i].active && chan[i].std.alg.had) {
518 if (chan[i].pcm.pause!=(bool)(chan[i].std.alg.val&1)) {
519 chan[i].pcm.pause=chan[i].std.alg.val&1;
520 if (!chan[i].keyOn) {
521 chan[i].crWriteVal=(chan[i].crWriteVal&~0x41)|(chan[i].crDirVal);
522 chan[i].crWriteVal=(chan[i].crWriteVal&~0x0002)|(chan[i].pcm.pause?0x0002:0x0000);
523 chan[i].crChanged=true;
524 }
525 }
526 if (chan[i].pcm.direction!=(bool)(chan[i].std.alg.val&2)) {
527 chan[i].pcm.direction=chan[i].std.alg.val&2;
528 if (!chan[i].keyOn) {
529 chan[i].crDirVal=(chan[i].crDirVal&~0x0040)|(chan[i].pcm.direction?0x0040:0x0000);
530 chan[i].crWriteVal=(chan[i].crWriteVal&~0x41)|chan[i].crDirVal;
531 chan[i].crDirValChanged=true;
532 chan[i].crChanged=true;
533 }
534 }
535 }
536 // update registers
537 if (chan[i].volChanged.changed) {
538 // calculate volume (16 bit)
539 if (chan[i].volChanged.lVol) {
540 if (amigaVol) {
541 chan[i].resLVol=VOL_SCALE_LOG(amigaVolTable[CLAMP(chan[i].outVol,0,64)],chan[i].outLVol,0xfff);
542 } else {
543 chan[i].resLVol=VOL_SCALE_LOG(chan[i].outVol,chan[i].outLVol,0xfff);
544 }
545 chan[i].resLVol-=volScale;
546 if (chan[i].resLVol<0) chan[i].resLVol=0;
547 chan[i].resLVol<<=4;
548 if (!chan[i].keyOn && chan[i].active) {
549 pageWrite(0x00|i,0x02,chan[i].resLVol);
550 }
551 }
552 if (chan[i].volChanged.rVol) {
553 if (amigaVol) {
554 chan[i].resRVol=VOL_SCALE_LOG(amigaVolTable[CLAMP(chan[i].outVol,0,64)],chan[i].outRVol,0xfff);
555 } else {
556 chan[i].resRVol=VOL_SCALE_LOG(chan[i].outVol,chan[i].outRVol,0xfff);
557 }
558 chan[i].resRVol-=volScale;
559 if (chan[i].resRVol<0) chan[i].resRVol=0;
560 chan[i].resRVol<<=4;
561 if (!chan[i].keyOn && chan[i].active) {
562 pageWrite(0x00|i,0x04,chan[i].resRVol);
563 }
564 }
565 if (chan[i].volChanged.ca) {
566 chan[i].crWriteVal=(chan[i].crWriteVal&~0x41)|chan[i].crDirVal;
567 chan[i].crWriteVal=(chan[i].crWriteVal&~0x1c00)|(chan[i].ca<<10);
568 chan[i].crChanged=true;
569 }
570 chan[i].volChanged.changed=0;
571 }
572 if (chan[i].pcmChanged.changed) {
573 if (chan[i].pcmChanged.index) {
574 const int next=chan[i].pcm.next;
575 bool sampleValid=false;
576 if (((ins->amiga.useNoteMap) && (next>=0 && next<120)) ||
577 ((!ins->amiga.useNoteMap) && (next>=0 && next<parent->song.sampleLen))) {
578 DivInstrumentAmiga::SampleMap& noteMapind=ins->amiga.noteMap[next];
579 int sample=next;
580 if (ins->amiga.useNoteMap) {
581 sample=noteMapind.map;
582 }
583 if (sample>=0 && sample<parent->song.sampleLen) {
584 const unsigned int offES5506=sampleOffES5506[sample];
585 sampleValid=true;
586 chan[i].pcm.index=sample;
587 chan[i].pcm.isNoteMap=ins->amiga.useNoteMap;
588 DivSample* s=parent->getSample(sample);
589 // get frequency offset
590 double off=1.0;
591 double center=(double)s->centerRate;
592 if (center<1) {
593 off=1.0;
594 } else {
595 off=(double)center/parent->getCenterRate();
596 }
597 if (ins->amiga.useNoteMap) {
598 //chan[i].pcm.note=next;
599 }
600 // get loop mode
601 DivSampleLoopMode loopMode=s->isLoopable()?s->loopMode:DIV_SAMPLE_LOOP_MAX;
602 const unsigned int start=offES5506<<10;
603 const unsigned int length=s->samples-1;
604 const unsigned int end=start+(length<<11);
605 const unsigned int nextBank=(offES5506>>22)&3;
606 const double nextFreqOffs=((amigaPitch && parent->song.linearPitch!=2)?16:PITCH_OFFSET)*off;
607 chan[i].pcm.loopMode=loopMode;
608 chan[i].pcm.bank=nextBank;
609 chan[i].pcm.start=start;
610 chan[i].pcm.end=end;
611 chan[i].pcm.length=length;
612 if ((chan[i].pcm.loopMode!=loopMode) || (chan[i].pcm.bank!=nextBank)) {
613 chan[i].pcm.loopMode=loopMode;
614 chan[i].pcm.bank=nextBank;
615 chan[i].pcmChanged.loopBank=1;
616 }
617 if (chan[i].pcm.nextFreqOffs!=nextFreqOffs) {
618 chan[i].pcm.nextFreqOffs=nextFreqOffs;
619 chan[i].noteChanged.offs=1;
620 }
621 }
622 }
623 if (sampleValid) {
624 if (!chan[i].keyOn) {
625 pageWrite(0x20|i,0x03,(chan[i].pcm.direction)?chan[i].pcm.end:chan[i].pcm.start);
626 }
627 chan[i].pcmChanged.slice=1;
628 }
629 chan[i].pcmChanged.index=0;
630 }
631 if (chan[i].pcmChanged.slice) {
632 if (!chan[i].keyOn) {
633 if (chan[i].pcm.index>=0 && chan[i].pcm.index<parent->song.sampleLen) {
634 // get loop mode
635 DivSample* s=parent->getSample(chan[i].pcm.index);
636 const unsigned int start=sampleOffES5506[chan[i].pcm.index]<<10;
637 const unsigned int nextLoopStart=(start+(s->loopStart<<11))&0xfffff800;
638 const unsigned int nextLoopEnd=(start+((s->loopEnd)<<11))&0xffffff80;
639 if ((chan[i].pcm.loopStart!=nextLoopStart) || (chan[i].pcm.loopEnd!=nextLoopEnd)) {
640 chan[i].pcm.loopStart=nextLoopStart;
641 chan[i].pcm.loopEnd=nextLoopEnd;
642 chan[i].pcmChanged.position=1;
643 }
644 }
645 }
646 chan[i].pcmChanged.slice=0;
647 }
648 if (chan[i].pcmChanged.position) {
649 if (!chan[i].keyOn) {
650 pageWrite(0x20|i,0x01,(chan[i].pcm.loopMode==DIV_SAMPLE_LOOP_MAX)?chan[i].pcm.start:chan[i].pcm.loopStart);
651 pageWrite(0x20|i,0x02,(chan[i].pcm.loopMode==DIV_SAMPLE_LOOP_MAX)?chan[i].pcm.end:chan[i].pcm.loopEnd);
652 }
653 chan[i].pcmChanged.position=0;
654 }
655 if (chan[i].pcmChanged.loopBank) {
656 if (!chan[i].keyOn) {
657 unsigned int loopFlag=(chan[i].pcm.bank<<14)|(chan[i].pcm.direction?0x0040:0x0000);
658 chan[i].isReverseLoop=false;
659 switch (chan[i].pcm.loopMode) {
660 case DIV_SAMPLE_LOOP_FORWARD: // Forward loop
661 loopFlag|=0x0008;
662 break;
663 /*
664 case DIV_SAMPLE_LOOP_BACKWARD: // Backward loop: IRQ enable
665 loopFlag|=0x0038;
666 chan[i].isReverseLoop=true;
667 break;
668 */
669 case DIV_SAMPLE_LOOP_PINGPONG: // Pingpong loop: Hardware support
670 loopFlag|=0x0018;
671 break;
672 case DIV_SAMPLE_LOOP_MAX: // no loop
673 default:
674 break;
675 }
676 // Set loop mode & Bank
677 chan[i].crDirVal=(chan[i].crDirVal&~0x0040)|(chan[i].pcm.direction?0x0040:0x0000);
678 chan[i].crWriteVal=(chan[i].crWriteVal&~0x41)|chan[i].crDirVal;
679 chan[i].crWriteVal=(chan[i].crWriteVal&~0xe0fd)|loopFlag;
680 chan[i].crDirValChanged=true;
681 chan[i].crChanged=true;
682 }
683 chan[i].pcmChanged.loopBank=0;
684 }
685 chan[i].pcmChanged.dummy=0;
686 }
687 if (chan[i].filterChanged.changed) {
688 if (!chan[i].keyOn) {
689 if (chan[i].filterChanged.mode) {
690 chan[i].crWriteVal=(chan[i].crWriteVal&~0x41)|chan[i].crDirVal;
691 chan[i].crWriteVal=(chan[i].crWriteVal&~0x0300)|(chan[i].filter.mode<<8);
692 chan[i].crChanged=true;
693 }
694 if (chan[i].filterChanged.k2) {
695 if (chan[i].std.ex2.mode!=0) { // Relative
696 k2=CLAMP(chan[i].filter.k2+chan[i].k2Offs,0,65535);
697 } else {
698 k2=chan[i].filter.k2;
699 }
700 }
701 if (chan[i].filterChanged.k1) {
702 if (chan[i].std.ex1.mode!=0) { // Relative
703 k1=CLAMP(chan[i].filter.k1+chan[i].k1Offs,0,65535);
704 } else {
705 k1=chan[i].filter.k1;
706 }
707 }
708 }
709 chan[i].filterChanged.changed=0;
710 }
711 if (chan[i].envChanged.changed) {
712 if (!chan[i].keyOn) {
713 if (chan[i].envChanged.lVRamp) {
714 pageWrite(0x00|i,0x03,((unsigned char)chan[i].envelope.lVRamp)<<8);
715 }
716 if (chan[i].envChanged.rVRamp) {
717 pageWrite(0x00|i,0x05,((unsigned char)chan[i].envelope.rVRamp)<<8);
718 }
719 if (chan[i].envChanged.ecount) {
720 pageWrite(0x00|i,0x06,(unsigned int)chan[i].envelope.ecount);
721 }
722 if (chan[i].envChanged.k2Ramp) {
723 pageWrite(0x00|i,0x08,(((unsigned char)chan[i].envelope.k2Ramp)<<8)|(chan[i].envelope.k2Slow?1:0));
724 }
725 if (chan[i].envChanged.k1Ramp) {
726 pageWrite(0x00|i,0x0a,(((unsigned char)chan[i].envelope.k1Ramp)<<8)|(chan[i].envelope.k1Slow?1:0));
727 }
728 }
729 chan[i].envChanged.changed=0;
730 }
731 updateNoteChangesAsNeeded(i);
732 if (chan[i].pcm.setPos) {
733 if (chan[i].active) {
734 const unsigned int start=chan[i].pcm.start;
735 const unsigned int end=chan[i].pcm.length;
736 const unsigned int pos=chan[i].pcm.direction?(end-chan[i].pcm.nextPos):chan[i].pcm.nextPos;
737 if ((chan[i].pcm.direction && pos>0) || ((!chan[i].pcm.direction) && pos<end)) {
738 pageWrite(0x20|i,0x03,start+(pos<<11));
739 }
740 } else {
741 // force keyon
742 chan[i].keyOn=true;
743 }
744 chan[i].pcm.setPos=false;
745 } else {
746 chan[i].pcm.nextPos=0;
747 }
748 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
749 if (amigaPitch && parent->song.linearPitch!=2) {
750 chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch*16,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,true,2,chan[i].pitch2*16,16*COLOR_NTSC,chan[i].pcm.freqOffs);
751 chan[i].freq=PITCH_OFFSET*(COLOR_NTSC/chan[i].freq)/(chipClock/16.0);
752 chan[i].freq=CLAMP(chan[i].freq,0,0x1ffff);
753 } else {
754 chan[i].freq=CLAMP(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,chan[i].pcm.freqOffs),0,0x1ffff);
755 }
756 if (chan[i].keyOn) {
757 if (chan[i].pcm.index>=0 && chan[i].pcm.index<parent->song.sampleLen) {
758 const int ind=chan[i].pcm.index;
759 DivSample* s=parent->getSample(ind);
760 // get frequency offset
761 double off=1.0;
762 double center=(double)s->centerRate;
763 if (center<1) {
764 off=1.0;
765 } else {
766 off=(double)center/parent->getCenterRate();
767 }
768 chan[i].pcm.loopStart=(chan[i].pcm.start+(s->loopStart<<11))&0xfffff800;
769 chan[i].pcm.loopEnd=(chan[i].pcm.start+((s->loopEnd)<<11))&0xffffff80;
770 chan[i].pcm.freqOffs=((amigaPitch && parent->song.linearPitch!=2)?16:PITCH_OFFSET)*off;
771 unsigned int startPos=chan[i].pcm.direction?chan[i].pcm.end:chan[i].pcm.start;
772 if (chan[i].pcm.nextPos) {
773 const unsigned int start=chan[i].pcm.start;
774 const unsigned int end=chan[i].pcm.length;
775 startPos=start+((chan[i].pcm.direction?(end-chan[i].pcm.nextPos):(chan[i].pcm.nextPos))<<11);
776 chan[i].pcm.nextPos=0;
777 }
778 chan[i].k1Prev=0xffff;
779 chan[i].k2Prev=0xffff;
780 crWrite(0x00|i,0x0303); // Wipeout CR
781 pageWrite(0x00|i,0x06,0); // Clear ECOUNT
782 pageWrite(0x20|i,0x03,startPos); // Set ACCUM to start address
783 pageWrite(0x00|i,0x07,0xffff); // Set K1 and K2 to 0xffff
784 pageWriteDelayed(0x00|i,0x09,0xffff,(chanMax+1)*4*2); // needs to 4 sample period delay
785 pageWrite(0x00|i,0x01,chan[i].freq);
786 pageWrite(0x20|i,0x01,(chan[i].pcm.loopMode==DIV_SAMPLE_LOOP_MAX)?chan[i].pcm.start:chan[i].pcm.loopStart);
787 pageWrite(0x20|i,0x02,(chan[i].pcm.loopMode==DIV_SAMPLE_LOOP_MAX)?chan[i].pcm.end:chan[i].pcm.loopEnd);
788 // initialize overwrite
789 if (chan[i].overwrite.state.overwrited) {
790 // Filter
791 if (chan[i].overwrite.state.mode) {
792 chan[i].filter.mode=chan[i].overwrite.filter.mode;
793 }
794 if (chan[i].overwrite.state.k1) {
795 chan[i].filter.k1=chan[i].overwrite.filter.k1;
796 }
797 if (chan[i].overwrite.state.k2) {
798 chan[i].filter.k2=chan[i].overwrite.filter.k2;
799 }
800 // Envelope
801 if (chan[i].overwrite.state.ecount) {
802 chan[i].envelope.ecount=chan[i].overwrite.envelope.ecount;
803 }
804 if (chan[i].overwrite.state.lVRamp) {
805 chan[i].envelope.lVRamp=chan[i].overwrite.envelope.lVRamp;
806 }
807 if (chan[i].overwrite.state.rVRamp) {
808 chan[i].envelope.rVRamp=chan[i].overwrite.envelope.rVRamp;
809 }
810 if (chan[i].overwrite.state.k1Ramp) {
811 chan[i].envelope.k1Ramp=chan[i].overwrite.envelope.k1Ramp;
812 chan[i].envelope.k1Slow=chan[i].overwrite.envelope.k1Slow;
813 }
814 if (chan[i].overwrite.state.k2Ramp) {
815 chan[i].envelope.k2Ramp=chan[i].overwrite.envelope.k2Ramp;
816 chan[i].envelope.k2Slow=chan[i].overwrite.envelope.k2Slow;
817 }
818 chan[i].overwrite.state.overwrited=0;
819 }
820 // initialize envelope
821 pageWrite(0x00|i,0x03,(unsigned char)(chan[i].envelope.lVRamp)<<8);
822 pageWrite(0x00|i,0x05,(unsigned char)(chan[i].envelope.rVRamp)<<8);
823 pageWrite(0x00|i,0x0a,((unsigned char)(chan[i].envelope.k1Ramp)<<8)|(chan[i].envelope.k1Slow?1:0));
824 pageWrite(0x00|i,0x08,((unsigned char)(chan[i].envelope.k2Ramp)<<8)|(chan[i].envelope.k2Slow?1:0));
825 // initialize filter
826 chan[i].crWriteVal=(chan[i].crWriteVal&~0xc300)|((chan[i].pcm.bank<<14)|(chan[i].filter.mode<<8));
827 chan[i].crChanged=true;
828 if ((chan[i].std.ex2.mode!=0) && (chan[i].std.ex2.had)) {
829 k2=CLAMP(chan[i].filter.k2+chan[i].k2Offs,0,65535);
830 } else {
831 k2=chan[i].filter.k2;
832 }
833 pageWrite(0x00|i,0x07,k2);
834 chan[i].k2Prev=k2;
835 if ((chan[i].std.ex1.mode!=0) && (chan[i].std.ex1.had)) {
836 k1=CLAMP(chan[i].filter.k1+chan[i].k1Offs,0,65535);
837 } else {
838 k1=chan[i].filter.k1;
839 }
840 pageWrite(0x00|i,0x09,k1);
841 chan[i].k1Prev=k1;
842 pageWrite(0x00|i,0x02,chan[i].resLVol);
843 pageWrite(0x00|i,0x04,chan[i].resRVol);
844 unsigned int loopFlag=(chan[i].ca<<10)|(chan[i].pcm.direction?0x0040:0x0000);
845 chan[i].isReverseLoop=false;
846 switch (chan[i].pcm.loopMode) {
847 case DIV_SAMPLE_LOOP_FORWARD: // Forward loop
848 loopFlag|=0x0008;
849 break;
850 /*
851 case DIV_SAMPLE_LOOP_BACKWARD: // Backward loop: IRQ enable
852 loopFlag|=0x0038;
853 chan[i].isReverseLoop=true;
854 break;
855 */
856 case DIV_SAMPLE_LOOP_PINGPONG: // Pingpong loop: Hardware support
857 loopFlag|=0x0018;
858 break;
859 case DIV_SAMPLE_LOOP_MAX: // no loop
860 default:
861 break;
862 }
863 if (chan[i].pcm.pause) {
864 loopFlag|=0x0002;
865 }
866 // Run sample
867 chan[i].crDirVal=(chan[i].crDirVal&~0x0040)|(chan[i].pcm.direction?0x0040:0x0000);
868 chan[i].crWriteVal=(chan[i].crWriteVal&~0x41)|chan[i].crDirVal;
869 chan[i].crWriteVal=(chan[i].crWriteVal&~0x3cff)|loopFlag;
870 chan[i].crDirValChanged=true;
871 chan[i].crChanged=true;
872 pageWrite(0x00|i,0x06,(unsigned int)chan[i].envelope.ecount); // Clear ECOUNT
873 }
874 }
875 if (chan[i].keyOff) {
876 chan[i].crWriteVal=0x0303;
877 chan[i].crChanged=true;
878 crWrite(0x00|i,0x0303); // Wipeout CR
879 } else if (!chan[i].keyOn && chan[i].active) {
880 pageWrite(0x00|i,0x01,chan[i].freq);
881 }
882 if (chan[i].keyOn) chan[i].keyOn=false;
883 if (chan[i].keyOff) chan[i].keyOff=false;
884 chan[i].freqChanged=false;
885 }
886 if (!chan[i].keyOn && chan[i].active) {
887 if (chan[i].k2Prev!=k2) {
888 pageWrite(0x00|i,0x07,k2);
889 chan[i].k2Prev=k2;
890 }
891 if (chan[i].k1Prev!=k1) {
892 pageWrite(0x00|i,0x09,k1);
893 chan[i].k1Prev=k1;
894 }
895 }
896 if (chan[i].crChanged) {
897 crWrite(0x00|i,chan[i].crWriteVal);
898 chan[i].crChanged=false;
899 if (chan[i].crDirValChanged) {
900 chan[i].crDirValInit=true;
901 chan[i].crDirValChanged=false;
902 }
903 }
904 }
905 }
906
907 // man this code
908 // part of the reason why it's so messy is because the chip is
909 // overly complex and because when this pull request was made,
910 // Furnace still was in an early state with no support for sample
911 // maps or whatever...
912 // one day I'll come back and clean this up
913 int DivPlatformES5506::dispatch(DivCommand c) {
914 switch (c.cmd) {
915 case DIV_CMD_NOTE_ON: {
916 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_ES5506);
917 bool sampleValid=false;
918 if (c.value!=DIV_NOTE_NULL) {
919 int sample=ins->amiga.getSample(c.value);
920 chan[c.chan].sampleNote=c.value;
921 if (sample>=0 && sample<parent->song.sampleLen) {
922 sampleValid=true;
923 chan[c.chan].volMacroMax=(ins->type==DIV_INS_AMIGA || amigaVol)?64:0xfff;
924 chan[c.chan].panMacroMax=ins->type==DIV_INS_AMIGA?127:0xfff;
925 chan[c.chan].pcm.next=ins->amiga.useNoteMap?c.value:sample;
926 c.value=ins->amiga.getFreq(c.value);
927 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
928 chan[c.chan].pcm.note=c.value;
929 chan[c.chan].filter=ins->es5506.filter;
930 chan[c.chan].envelope=ins->es5506.envelope;
931 } else {
932 chan[c.chan].sampleNoteDelta=0;
933 }
934 } else {
935 int sample=ins->amiga.getSample(chan[c.chan].sampleNote);
936 if (sample>=0 && sample<parent->song.sampleLen) {
937 sampleValid=true;
938 chan[c.chan].volMacroMax=(ins->type==DIV_INS_AMIGA || amigaVol)?64:0xfff;
939 chan[c.chan].panMacroMax=ins->type==DIV_INS_AMIGA?127:0xfff;
940 chan[c.chan].pcm.next=ins->amiga.useNoteMap?chan[c.chan].sampleNote:sample;
941 c.value=ins->amiga.getFreq(chan[c.chan].sampleNote);
942 chan[c.chan].pcm.note=c.value;
943 chan[c.chan].filter=ins->es5506.filter;
944 chan[c.chan].envelope=ins->es5506.envelope;
945 } else {
946 chan[c.chan].sampleNoteDelta=0;
947 }
948 }
949 if (!sampleValid) {
950 chan[c.chan].pcm.index=chan[c.chan].pcm.next=-1;
951 chan[c.chan].sampleNoteDelta=0;
952 chan[c.chan].filter=DivInstrumentES5506::Filter();
953 chan[c.chan].envelope=DivInstrumentES5506::Envelope();
954 }
955 if (c.value!=DIV_NOTE_NULL) {
956 chan[c.chan].note=c.value;
957 chan[c.chan].nextNote=chan[c.chan].note;
958 chan[c.chan].pcm.nextFreqOffs=chan[c.chan].pcm.freqOffs;
959 chan[c.chan].freqChanged=true;
960 chan[c.chan].pcmChanged.changed=0xff;
961 chan[c.chan].noteChanged.changed=0xff;
962 chan[c.chan].volChanged.changed=0xff;
963 updateNoteChangesAsNeeded(c.chan);
964 }
965 if (!chan[c.chan].std.vol.will) {
966 if (amigaVol) {
967 chan[c.chan].outVol=chan[c.chan].vol;
968 } else {
969 chan[c.chan].outVol=(0xfff*chan[c.chan].vol)/0xff;
970 }
971 }
972 if (amigaVol) {
973 if (!chan[c.chan].std.panL.will) {
974 chan[c.chan].outLVol=amigaPanTable[(chan[c.chan].lVol>>1)&127];
975 }
976 if (!chan[c.chan].std.panR.will) {
977 chan[c.chan].outRVol=amigaPanTable[(chan[c.chan].rVol>>1)&127];
978 }
979 } else {
980 if (!chan[c.chan].std.panL.will) {
981 chan[c.chan].outLVol=(0xfff*chan[c.chan].lVol)/0xff;
982 }
983 if (!chan[c.chan].std.panR.will) {
984 chan[c.chan].outRVol=(0xfff*chan[c.chan].rVol)/0xff;
985 }
986 }
987 chan[c.chan].active=true;
988 chan[c.chan].keyOn=true;
989 chan[c.chan].macroInit(ins);
990 break;
991 }
992 case DIV_CMD_NOTE_OFF:
993 chan[c.chan].filter=DivInstrumentES5506::Filter();
994 chan[c.chan].envelope=DivInstrumentES5506::Envelope();
995 chan[c.chan].active=false;
996 chan[c.chan].keyOff=true;
997 chan[c.chan].macroInit(NULL);
998 break;
999 case DIV_CMD_NOTE_OFF_ENV:
1000 case DIV_CMD_ENV_RELEASE:
1001 chan[c.chan].std.release();
1002 break;
1003 case DIV_CMD_INSTRUMENT:
1004 if (chan[c.chan].ins!=c.value || c.value2==1) {
1005 chan[c.chan].ins=c.value;
1006 }
1007 break;
1008 case DIV_CMD_VOLUME:
1009 if (chan[c.chan].vol!=c.value) {
1010 chan[c.chan].vol=c.value;
1011 if (!chan[c.chan].std.vol.has) {
1012 if (amigaVol) {
1013 chan[c.chan].outVol=c.value;
1014 } else {
1015 chan[c.chan].outVol=(0xfff*c.value)/0xff;
1016 }
1017 chan[c.chan].volChanged.changed=0xff;
1018 }
1019 }
1020 break;
1021 case DIV_CMD_GET_VOLUME:
1022 return chan[c.chan].vol;
1023 break;
1024 case DIV_CMD_PANNING: {
1025 if (chan[c.chan].ca!=0) {
1026 chan[c.chan].ca=0;
1027 chan[c.chan].volChanged.ca=1;
1028 }
1029 if (amigaVol) {
1030 // Left volume
1031 if (chan[c.chan].lVol!=c.value) {
1032 chan[c.chan].lVol=c.value;
1033 if (!chan[c.chan].std.panL.has) {
1034 chan[c.chan].outLVol=amigaPanTable[(c.value>>1)&127];
1035 chan[c.chan].volChanged.lVol=1;
1036 }
1037 }
1038 // Right volume
1039 if (chan[c.chan].rVol!=c.value2) {
1040 chan[c.chan].rVol=c.value2;
1041 if (!chan[c.chan].std.panR.has) {
1042 chan[c.chan].outRVol=amigaPanTable[(c.value2>>1)&127];
1043 chan[c.chan].volChanged.rVol=1;
1044 }
1045 }
1046 } else {
1047 // Left volume
1048 if (chan[c.chan].lVol!=c.value) {
1049 chan[c.chan].lVol=c.value;
1050 if (!chan[c.chan].std.panL.has) {
1051 chan[c.chan].outLVol=(0xfff*c.value)/0xff;
1052 chan[c.chan].volChanged.lVol=1;
1053 }
1054 }
1055 // Right volume
1056 if (chan[c.chan].rVol!=c.value2) {
1057 chan[c.chan].rVol=c.value2;
1058 if (!chan[c.chan].std.panR.has) {
1059 chan[c.chan].outRVol=(0xfff*c.value2)/0xff;
1060 chan[c.chan].volChanged.rVol=1;
1061 }
1062 }
1063 }
1064 break;
1065 }
1066 case DIV_CMD_SURROUND_PANNING: {
1067 unsigned char ca=CLAMP(c.value>>1,0,5);
1068 if (chan[c.chan].ca!=ca) {
1069 chan[c.chan].ca=ca;
1070 chan[c.chan].volChanged.ca=1;
1071 }
1072 if ((c.value&1)==0) {
1073 // Left volume
1074 if (chan[c.chan].lVol!=c.value2) {
1075 chan[c.chan].lVol=c.value2;
1076 if (!chan[c.chan].std.panL.has) {
1077 chan[c.chan].outLVol=(0xfff*c.value2)/0xff;
1078 chan[c.chan].volChanged.lVol=1;
1079 }
1080 }
1081 }
1082 else if ((c.value&1)==1) {
1083 // Right volume
1084 if (chan[c.chan].rVol!=c.value2) {
1085 chan[c.chan].rVol=c.value2;
1086 if (!chan[c.chan].std.panR.has) {
1087 chan[c.chan].outRVol=(0xfff*c.value2)/0xff;
1088 chan[c.chan].volChanged.rVol=1;
1089 }
1090 }
1091 }
1092 break;
1093 }
1094 case DIV_CMD_PITCH:
1095 chan[c.chan].pitch=c.value;
1096 chan[c.chan].freqChanged=true;
1097 break;
1098 // Filter commands
1099 case DIV_CMD_ES5506_FILTER_MODE:
1100 if (!chan[c.chan].active) {
1101 if (!chan[c.chan].overwrite.state.mode) {
1102 chan[c.chan].overwrite.filter.mode=chan[c.chan].filter.mode;
1103 chan[c.chan].overwrite.state.mode=1;
1104 }
1105 chan[c.chan].overwrite.filter.mode=DivInstrumentES5506::Filter::FilterMode(c.value&3);
1106 }
1107 chan[c.chan].filter.mode=DivInstrumentES5506::Filter::FilterMode(c.value&3);
1108 chan[c.chan].filterChanged.mode=1;
1109 break;
1110 case DIV_CMD_ES5506_FILTER_K1:
1111 if (!chan[c.chan].active) {
1112 if (!chan[c.chan].overwrite.state.k1) {
1113 chan[c.chan].overwrite.filter.k1=chan[c.chan].filter.k1;
1114 chan[c.chan].overwrite.state.k1=1;
1115 }
1116 chan[c.chan].overwrite.filter.k1=(chan[c.chan].overwrite.filter.k1&~c.value2)|(c.value&c.value2);
1117 }
1118 chan[c.chan].filter.k1=(chan[c.chan].filter.k1&~c.value2)|(c.value&c.value2);
1119 chan[c.chan].filterChanged.k1=1;
1120 break;
1121 case DIV_CMD_ES5506_FILTER_K2:
1122 if (!chan[c.chan].active) {
1123 if (!chan[c.chan].overwrite.state.k2) {
1124 chan[c.chan].overwrite.filter.k2=chan[c.chan].filter.k2;
1125 chan[c.chan].overwrite.state.k2=1;
1126 }
1127 chan[c.chan].overwrite.filter.k2=(chan[c.chan].overwrite.filter.k2&~c.value2)|(c.value&c.value2);
1128 }
1129 chan[c.chan].filter.k2=(chan[c.chan].filter.k2&~c.value2)|(c.value&c.value2);
1130 chan[c.chan].filterChanged.k2=1;
1131 break;
1132 case DIV_CMD_ES5506_FILTER_K1_SLIDE:
1133 chan[c.chan].k1Slide=c.value2?(-c.value):c.value;
1134 break;
1135 case DIV_CMD_ES5506_FILTER_K2_SLIDE:
1136 chan[c.chan].k2Slide=c.value2?(-c.value):c.value;
1137 break;
1138 // Envelope commands
1139 case DIV_CMD_ES5506_ENVELOPE_COUNT:
1140 if (!chan[c.chan].active) {
1141 if (!chan[c.chan].overwrite.state.ecount) {
1142 chan[c.chan].overwrite.envelope.ecount=chan[c.chan].envelope.ecount;
1143 chan[c.chan].overwrite.state.ecount=1;
1144 }
1145 chan[c.chan].overwrite.envelope.ecount=c.value&0x1ff;
1146 }
1147 chan[c.chan].envelope.ecount=c.value&0x1ff;
1148 chan[c.chan].envChanged.ecount=1;
1149 break;
1150 case DIV_CMD_ES5506_ENVELOPE_LVRAMP:
1151 if (!chan[c.chan].active) {
1152 if (!chan[c.chan].overwrite.state.lVRamp) {
1153 chan[c.chan].overwrite.envelope.lVRamp=chan[c.chan].envelope.lVRamp;
1154 chan[c.chan].overwrite.state.lVRamp=1;
1155 }
1156 chan[c.chan].overwrite.envelope.lVRamp=(signed char)(c.value&0xff);
1157 }
1158 chan[c.chan].envelope.lVRamp=(signed char)(c.value&0xff);
1159 chan[c.chan].envChanged.lVRamp=1;
1160 break;
1161 case DIV_CMD_ES5506_ENVELOPE_RVRAMP:
1162 if (!chan[c.chan].active) {
1163 if (!chan[c.chan].overwrite.state.rVRamp) {
1164 chan[c.chan].overwrite.envelope.rVRamp=chan[c.chan].envelope.rVRamp;
1165 chan[c.chan].overwrite.state.rVRamp=1;
1166 }
1167 chan[c.chan].overwrite.envelope.rVRamp=(signed char)(c.value&0xff);
1168 }
1169 chan[c.chan].envelope.rVRamp=(signed char)(c.value&0xff);
1170 chan[c.chan].envChanged.rVRamp=1;
1171 break;
1172 case DIV_CMD_ES5506_ENVELOPE_K1RAMP:
1173 if (!chan[c.chan].active) {
1174 if (!chan[c.chan].overwrite.state.k1Ramp) {
1175 chan[c.chan].overwrite.envelope.k1Ramp=chan[c.chan].envelope.k1Ramp;
1176 chan[c.chan].overwrite.envelope.k1Slow=chan[c.chan].envelope.k1Slow;
1177 chan[c.chan].overwrite.state.k1Ramp=1;
1178 }
1179 chan[c.chan].overwrite.envelope.k1Ramp=(signed char)(c.value&0xff);
1180 chan[c.chan].overwrite.envelope.k1Slow=c.value2&1;
1181 }
1182 chan[c.chan].envelope.k1Ramp=(signed char)(c.value&0xff);
1183 chan[c.chan].envelope.k1Slow=c.value2&1;
1184 chan[c.chan].envChanged.k1Ramp=1;
1185 break;
1186 case DIV_CMD_ES5506_ENVELOPE_K2RAMP:
1187 if (!chan[c.chan].active) {
1188 if (!chan[c.chan].overwrite.state.k2Ramp) {
1189 chan[c.chan].overwrite.envelope.k2Ramp=chan[c.chan].envelope.k2Ramp;
1190 chan[c.chan].overwrite.envelope.k2Slow=chan[c.chan].envelope.k2Slow;
1191 chan[c.chan].overwrite.state.k2Ramp=1;
1192 }
1193 chan[c.chan].overwrite.envelope.k2Ramp=(signed char)(c.value&0xff);
1194 chan[c.chan].overwrite.envelope.k2Slow=c.value2&1;
1195 }
1196 chan[c.chan].envelope.k2Ramp=(signed char)(c.value&0xff);
1197 chan[c.chan].envelope.k2Slow=c.value2&1;
1198 chan[c.chan].envChanged.k2Ramp=1;
1199 break;
1200 // controls
1201 case DIV_CMD_ES5506_PAUSE:
1202 if (chan[c.chan].active) {
1203 if (chan[c.chan].pcm.pause!=(bool)(c.value&1)) {
1204 chan[c.chan].pcm.pause=c.value&1;
1205 chan[c.chan].crWriteVal=(chan[c.chan].crWriteVal&~0x41)|chan[c.chan].crDirVal;
1206 chan[c.chan].crWriteVal=(chan[c.chan].crWriteVal&~0x0002)|(chan[c.chan].pcm.pause?0x0002:0x0000);
1207 chan[c.chan].crChanged=true;
1208 }
1209 }
1210 break;
1211 case DIV_CMD_NOTE_PORTA: {
1212 int nextFreq=chan[c.chan].baseFreq;
1213 int destFreq=NOTE_ES5506(c.chan,c.value2+chan[c.chan].sampleNoteDelta);
1214 bool return2=false;
1215 if (amigaPitch && parent->song.linearPitch!=2) {
1216 c.value*=16;
1217 }
1218 if (destFreq>nextFreq) {
1219 nextFreq+=c.value;
1220 if (nextFreq>=destFreq) {
1221 nextFreq=destFreq;
1222 return2=true;
1223 }
1224 } else {
1225 nextFreq-=c.value;
1226 if (nextFreq<=destFreq) {
1227 nextFreq=destFreq;
1228 return2=true;
1229 }
1230 }
1231 chan[c.chan].nextFreq=nextFreq;
1232 chan[c.chan].noteChanged.freq=1;
1233 if (return2) {
1234 chan[c.chan].inPorta=false;
1235 return 2;
1236 }
1237 break;
1238 }
1239 case DIV_CMD_LEGATO: {
1240 chan[c.chan].note=c.value;
1241 chan[c.chan].nextNote=chan[c.chan].note+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(0));
1242 chan[c.chan].noteChanged.note=1;
1243 break;
1244 }
1245 case DIV_CMD_PRE_PORTA:
1246 if (chan[c.chan].active && c.value2) {
1247 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_ES5506));
1248 }
1249 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) {
1250 chan[c.chan].nextNote=chan[c.chan].note;
1251 chan[c.chan].noteChanged.note=1;
1252 }
1253 chan[c.chan].inPorta=c.value;
1254 break;
1255 case DIV_CMD_SAMPLE_POS: {
1256 chan[c.chan].pcm.nextPos=c.value;
1257 chan[c.chan].pcm.setPos=true;
1258 break;
1259 }
1260 case DIV_CMD_SAMPLE_DIR: {
1261 if (chan[c.chan].pcm.direction!=(bool)(c.value&1)) {
1262 chan[c.chan].pcm.direction=c.value&1;
1263 chan[c.chan].crDirVal=(chan[c.chan].crDirVal&~0x0040)|(chan[c.chan].pcm.direction?0x0040:0x0000);
1264 chan[c.chan].crWriteVal=(chan[c.chan].crWriteVal&~0x41)|chan[c.chan].crDirVal;
1265 chan[c.chan].crDirValChanged=true;
1266 chan[c.chan].crChanged=true;
1267 }
1268 break;
1269 }
1270 case DIV_CMD_GET_VOLMAX:
1271 return amigaVol?64:255;
1272 break;
1273 case DIV_CMD_MACRO_OFF:
1274 chan[c.chan].std.mask(c.value,true);
1275 break;
1276 case DIV_CMD_MACRO_ON:
1277 chan[c.chan].std.mask(c.value,false);
1278 break;
1279 case DIV_CMD_MACRO_RESTART:
1280 chan[c.chan].std.restart(c.value);
1281 break;
1282 default:
1283 break;
1284 }
1285 return 1;
1286 }
1287
1288 void DivPlatformES5506::muteChannel(int ch, bool mute) {
1289 isMuted[ch]=mute;
1290 es5506.set_mute(ch,mute);
1291 }
1292
1293 void DivPlatformES5506::forceIns() {
1294 for (int i=0; i<=chanMax; i++) {
1295 chan[i].insChanged=true;
1296 chan[i].freqChanged=true;
1297 chan[i].noteChanged.changed=0xff;
1298 chan[i].volChanged.changed=0xff;
1299 chan[i].filterChanged.changed=0xff;
1300 chan[i].envChanged.changed=0xff;
1301 chan[i].pcmChanged.changed=0xff;
1302 }
1303 }
1304
1305 void* DivPlatformES5506::getChanState(int ch) {
1306 return &chan[ch];
1307 }
1308
1309 DivMacroInt* DivPlatformES5506::getChanMacroInt(int ch) {
1310 return &chan[ch].std;
1311 }
1312
1313 unsigned short DivPlatformES5506::getPan(int ch) {
1314 float expL=255.0f*pow(((float)(chan[ch].resLVol>>4))/4095.0f,4.0f);
1315 float expR=255.0f*pow(((float)(chan[ch].resRVol>>4))/4095.0f,4.0f);
1316 if (expL>255.0f) expL=255.0f;
1317 if (expR>255.0f) expR=255.0f;
1318 return (((unsigned int)expL)<<8)|((unsigned int)expR);
1319 }
1320
1321 void DivPlatformES5506::reset() {
1322 while (!hostIntf32.empty()) hostIntf32.pop();
1323 while (!hostIntf8.empty()) hostIntf8.pop();
1324 for (int i=0; i<32; i++) {
1325 chan[i]=DivPlatformES5506::Channel();
1326 chan[i].vol=amigaVol?64:255;
1327 chan[i].outVol=amigaVol?64:255;
1328 chan[i].std.setEngine(parent);
1329 }
1330 es5506.reset();
1331 for (int i=0; i<32; i++) {
1332 es5506.set_mute(i,isMuted[i]);
1333 }
1334
1335 cycle=0;
1336 curPage=0;
1337 irqv=0x80;
1338 irqTrigger=false;
1339 chanMax=initChanMax;
1340
1341 if (dumpWrites) {
1342 addWrite(0xffffffff,0);
1343 }
1344
1345 pageWriteMask(0x00,0x60,0x0b,(unsigned int)chanMax);
1346 pageWriteMask(0x00,0x60,0x0b,0x1f);
1347 // set serial output to I2S-ish, 16 bit
1348 pageWriteMask(0x20,0x60,0x0a,0x01);
1349 pageWriteMask(0x20,0x60,0x0b,0x11);
1350 pageWriteMask(0x20,0x60,0x0c,0x20);
1351 pageWriteMask(0x00,0x60,0x0c,0x08); // Reset serial output
1352 }
1353
1354 int DivPlatformES5506::getOutputCount() {
1355 return 12;
1356 }
1357
1358 bool DivPlatformES5506::keyOffAffectsArp(int ch) {
1359 return true;
1360 }
1361
1362 void DivPlatformES5506::notifyInsChange(int ins) {
1363 for (int i=0; i<32; i++) {
1364 if (chan[i].ins==ins) {
1365 chan[i].insChanged=true;
1366 }
1367 }
1368 }
1369
1370 void DivPlatformES5506::notifyWaveChange(int wave) {
1371 }
1372
1373 void DivPlatformES5506::notifyInsDeletion(void* ins) {
1374 for (int i=0; i<32; i++) {
1375 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
1376 }
1377 }
1378
1379 void DivPlatformES5506::setFlags(const DivConfig& flags) {
1380 chipClock=16000000;
1381 CHECK_CUSTOM_CLOCK;
1382
1383 initChanMax=MAX(4,flags.getInt("channels",0x1f)&0x1f);
1384 volScale=4095-flags.getInt("volScale",4095);
1385 amigaVol=flags.getBool("amigaVol",false);
1386 amigaPitch=flags.getBool("amigaPitch",false);
1387 chanMax=initChanMax;
1388 pageWriteMask(0x00,0x60,0x0b,(unsigned int)chanMax);
1389
1390 rate=chipClock/(16*(initChanMax+1)); // 2 E clock tick (16 CLKIN tick) per voice / 4
1391 for (int i=0; i<32; i++) {
1392 oscBuf[i]->setRate(rate);
1393 }
1394 }
1395
1396 void DivPlatformES5506::poke(unsigned int addr, unsigned short val) {
1397 immWrite(addr, val);
1398 }
1399
1400 void DivPlatformES5506::poke(std::vector<DivRegWrite>& wlist) {
1401 for (DivRegWrite& i: wlist) immWrite(i.addr,i.val);
1402 }
1403
1404 DivDispatchOscBuffer* DivPlatformES5506::getOscBuffer(int ch) {
1405 return oscBuf[ch];
1406 }
1407
1408 unsigned char* DivPlatformES5506::getRegisterPool() {
1409 unsigned char* regPoolPtr = regPool;
1410 for (unsigned char p=0; p<128; p++) {
1411 for (unsigned char r=0; r<16; r++) {
1412 unsigned int reg=es5506.regs_r(p,r,false);
1413 for (int b=0; b<4; b++) {
1414 *regPoolPtr++ = reg>>(24-(b<<3));
1415 }
1416 }
1417 }
1418 return regPool;
1419 }
1420
1421 int DivPlatformES5506::getRegisterPoolSize() {
1422 return 4*16*128; // 7 bit page x 16 registers per page x 32 bit per registers
1423 }
1424 const void* DivPlatformES5506::getSampleMem(int index) {
1425 return index == 0 ? sampleMem : NULL;
1426 }
1427
1428 size_t DivPlatformES5506::getSampleMemCapacity(int index) {
1429 return index == 0 ? 16777216 : 0; // 2Mword x 16bit * 4 banks
1430 }
1431
1432 size_t DivPlatformES5506::getSampleMemUsage(int index) {
1433 return index == 0 ? sampleMemLen : 0;
1434 }
1435
1436 bool DivPlatformES5506::isSampleLoaded(int index, int sample) {
1437 if (index!=0) return false;
1438 if (sample<0 || sample>32767) return false;
1439 return sampleLoaded[sample];
1440 }
1441
1442 const DivMemoryComposition* DivPlatformES5506::getMemCompo(int index) {
1443 if (index!=0) return NULL;
1444 return &memCompo;
1445 }
1446
1447 void DivPlatformES5506::renderSamples(int sysID) {
1448 memset(sampleMem,0,getSampleMemCapacity());
1449 memset(sampleOffES5506,0,32768*sizeof(unsigned int));
1450 memset(sampleLoaded,0,32768*sizeof(bool));
1451
1452 memCompo=DivMemoryComposition();
1453 memCompo.name="Sample Memory";
1454
1455 size_t memPos=128; // add silent at begin and end of each bank for reverse playback and add 1 for loop
1456 for (int i=0; i<parent->song.sampleLen; i++) {
1457 DivSample* s=parent->song.sample[i];
1458 if (!s->renderOn[0][sysID]) {
1459 sampleOffES5506[i]=0;
1460 continue;
1461 }
1462
1463 unsigned int length=s->length16;
1464 // fit sample size to single bank size
1465 if (length>(4194304-128)) {
1466 length=4194304-128;
1467 }
1468 if ((memPos&0xc00000)!=((memPos+length+128)&0xc00000)) {
1469 memPos=((memPos+0x3fffff)&0xffc00000)+128;
1470 }
1471 if (memPos>=(getSampleMemCapacity()-128)) {
1472 logW("out of ES5506 memory for sample %d!",i);
1473 break;
1474 }
1475 if (memPos+length>=(getSampleMemCapacity()-128)) {
1476 memcpy(sampleMem+(memPos/sizeof(short)),s->data16,(getSampleMemCapacity()-128)-memPos);
1477 logW("out of ES5506 memory for sample %d!",i);
1478 } else {
1479 memcpy(sampleMem+(memPos/sizeof(short)),s->data16,length);
1480 // inject loop sample
1481 if (s->loop && s->loopEnd>=0 && s->loopEnd<=(int)s->samples && s->loopStart>=0 && s->loopStart<(int)s->samples) {
1482 sampleMem[(memPos/sizeof(short))+s->loopEnd]=s->data16[s->loopStart];
1483 if (s->loopEnd>=(int)s->samples) length+=2;
1484 }
1485 }
1486 sampleOffES5506[i]=memPos;
1487 sampleLoaded[i]=true;
1488 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+length));
1489 memPos+=length;
1490 }
1491 sampleMemLen=memPos+256;
1492
1493 memCompo.used=sampleMemLen;
1494 memCompo.capacity=16777216;
1495 }
1496
1497 int DivPlatformES5506::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
1498 sampleMem=new signed short[getSampleMemCapacity()/sizeof(short)];
1499 sampleMemLen=0;
1500 parent=p;
1501 dumpWrites=false;
1502 skipRegisterWrites=false;
1503 volScale=0;
1504 curPage=0;
1505
1506 for (int i=0; i<32; i++) {
1507 isMuted[i]=false;
1508 oscBuf[i]=new DivDispatchOscBuffer;
1509 }
1510
1511 setFlags(flags);
1512
1513 reset();
1514 return 32;
1515 }
1516
1517 void DivPlatformES5506::quit() {
1518 delete[] sampleMem;
1519 for (int i=0; i<32; i++) {
1520 delete oscBuf[i];
1521 }
1522 }
1523
1524 // initialization of important arrays
1525 DivPlatformES5506::DivPlatformES5506():
1526 DivDispatch(),
1527 es550x_intf(),
1528 es5506(*this) {
1529 sampleOffES5506=new unsigned int[32768];
1530 sampleLoaded=new bool[32768];
1531 }
1532
1533 DivPlatformES5506::~DivPlatformES5506() {
1534 delete[] sampleOffES5506;
1535 delete[] sampleLoaded;
1536 }