Debian Codegraph @

src/engine/platform/qsound.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 "qsound.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include <math.h>
24
25 #define CHIP_DIVIDER (1248*2)
26 #define QS_NOTE_FREQUENCY(x) parent->calcBaseFreq(440,4096,(x)-3,false)
27
28 #define rWrite(a,v) {if(!skipRegisterWrites) {qsound_write_data(&chip,a,v); if(dumpWrites) addWrite(a,v); }}
29 #define immWrite(a,v) {qsound_write_data(&chip,a,v); if(dumpWrites) addWrite(a,v);}
30
31 const char* regCheatSheetQSound[]={
32 "Ch15_Bank", "00",
33 "Ch00_Start", "01",
34 "Ch00_Freq", "02",
35 "Ch00_Phase", "03",
36 "Ch00_Loop", "04",
37 "Ch00_End", "05",
38 "Ch00_Volume", "06",
39 "Ch00_Bank", "08",
40 "Ch01_Start", "09",
41 "Ch01_Freq", "0A",
42 "Ch01_Phase", "0B",
43 "Ch01_Loop", "0C",
44 "Ch01_End", "0D",
45 "Ch01_Volume", "0E",
46 "Ch01_Bank", "10",
47 "Ch02_Start", "11",
48 "Ch02_Freq", "12",
49 "Ch02_Phase", "13",
50 "Ch02_Loop", "14",
51 "Ch02_End", "15",
52 "Ch02_Volume", "16",
53 "Ch02_Bank", "18",
54 "Ch03_Start", "19",
55 "Ch03_Freq", "1A",
56 "Ch03_Phase", "1B",
57 "Ch03_Loop", "1C",
58 "Ch03_End", "1D",
59 "Ch03_Volume", "1E",
60 "Ch03_Bank", "20",
61 "Ch04_Start", "21",
62 "Ch04_Freq", "22",
63 "Ch04_Phase", "23",
64 "Ch04_Loop", "24",
65 "Ch04_End", "25",
66 "Ch04_Volume", "26",
67 "Ch04_Bank", "28",
68 "Ch05_Start", "29",
69 "Ch05_Freq", "2A",
70 "Ch05_Phase", "2B",
71 "Ch05_Loop", "2C",
72 "Ch05_End", "2D",
73 "Ch05_Volume", "2E",
74 "Ch05_Bank", "30",
75 "Ch06_Start", "31",
76 "Ch06_Freq", "32",
77 "Ch06_Phase", "33",
78 "Ch06_Loop", "34",
79 "Ch06_End", "35",
80 "Ch06_Volume", "36",
81 "Ch06_Bank", "38",
82 "Ch07_Start", "39",
83 "Ch07_Freq", "3A",
84 "Ch07_Phase", "3B",
85 "Ch07_Loop", "3C",
86 "Ch07_End", "3D",
87 "Ch07_Volume", "3E",
88 "Ch07_Bank", "40",
89 "Ch08_Start", "41",
90 "Ch08_Freq", "42",
91 "Ch08_Phase", "43",
92 "Ch08_Loop", "44",
93 "Ch08_End", "45",
94 "Ch08_Volume", "46",
95 "Ch08_Bank", "48",
96 "Ch09_Start", "49",
97 "Ch09_Freq", "4A",
98 "Ch09_Phase", "4B",
99 "Ch09_Loop", "4C",
100 "Ch09_End", "4D",
101 "Ch09_Volume", "4E",
102 "Ch09_Bank", "50",
103 "Ch10_Start", "51",
104 "Ch10_Freq", "52",
105 "Ch10_Phase", "53",
106 "Ch10_Loop", "54",
107 "Ch10_End", "55",
108 "Ch10_Volume", "56",
109 "Ch10_Bank", "58",
110 "Ch11_Start", "59",
111 "Ch11_Freq", "5A",
112 "Ch11_Phase", "5B",
113 "Ch11_Loop", "5C",
114 "Ch11_End", "5D",
115 "Ch11_Volume", "5E",
116 "Ch11_Bank", "60",
117 "Ch12_Start", "61",
118 "Ch12_Freq", "62",
119 "Ch12_Phase", "63",
120 "Ch12_Loop", "64",
121 "Ch12_End", "65",
122 "Ch12_Volume", "66",
123 "Ch12_Bank", "68",
124 "Ch13_Start", "69",
125 "Ch13_Freq", "6A",
126 "Ch13_Phase", "6B",
127 "Ch13_Loop", "6C",
128 "Ch13_End", "6D",
129 "Ch13_Volume", "6E",
130 "Ch13_Bank", "70",
131 "Ch14_Start", "71",
132 "Ch14_Freq", "72",
133 "Ch14_Phase", "73",
134 "Ch14_Loop", "74",
135 "Ch14_End", "75",
136 "Ch14_Volume", "76",
137 "Ch14_Bank", "78",
138 "Ch15_Start", "79",
139 "Ch15_Freq", "7A",
140 "Ch15_Phase", "7B",
141 "Ch15_Loop", "7C",
142 "Ch15_End", "7D",
143 "Ch15_Volume", "7E",
144 "Ch00_Panning", "80",
145 "Ch01_Panning", "81",
146 "Ch02_Panning", "82",
147 "Ch03_Panning", "83",
148 "Ch04_Panning", "84",
149 "Ch05_Panning", "85",
150 "Ch06_Panning", "86",
151 "Ch07_Panning", "87",
152 "Ch08_Panning", "88",
153 "Ch09_Panning", "89",
154 "Ch10_Panning", "8A",
155 "Ch11_Panning", "8B",
156 "Ch12_Panning", "8C",
157 "Ch13_Panning", "8D",
158 "Ch14_Panning", "8E",
159 "Ch15_Panning", "8F",
160 "Adpcm0_Panning","90",
161 "Adpcm1_Panning","91",
162 "Adpcm2_Panning","92",
163 "Echo_Feedback","93",
164 "Ch00_Echo", "BA",
165 "Ch01_Echo", "BB",
166 "Ch02_Echo", "BC",
167 "Ch03_Echo", "BD",
168 "Ch04_Echo", "BE",
169 "Ch05_Echo", "BF",
170 "Ch06_Echo", "C0",
171 "Ch07_Echo", "C1",
172 "Ch08_Echo", "C2",
173 "Ch09_Echo", "C3",
174 "Ch10_Echo", "C4",
175 "Ch11_Echo", "C5",
176 "Ch12_Echo", "C6",
177 "Ch13_Echo", "C7",
178 "Ch14_Echo", "C8",
179 "Ch15_Echo", "C9",
180 "Adpcm0_Start", "CA",
181 "Adpcm0_End", "CB",
182 "Adpcm0_Bank", "CC",
183 "Adpcm0_Volume","CD",
184 "Adpcm1_Start", "CE",
185 "Adpcm1_End", "CF",
186 "Adpcm1_Bank", "D0",
187 "Adpcm1_Volume","D1",
188 "Adpcm2_Start", "D2",
189 "Adpcm2_End", "D3",
190 "Adpcm2_Bank", "D4",
191 "Adpcm2_Volume","D5",
192 "Adpcm0_KeyOn", "D6",
193 "Adpcm1_KeyOn", "D7",
194 "Adpcm2_KeyOn", "D8",
195 "Echo_Delay", "D9",
196 "L_Wet_Filter", "DA",
197 "L_Dry_Filter", "DB",
198 "R_Wet_Filter", "DC",
199 "R_Dry_Filter", "DD",
200 "L_Wet_Delay", "DE",
201 "L_Dry_Delay", "DF",
202 "R_Wet_Delay", "E0",
203 "R_Dry_Delay", "E1",
204 "Delay_Flag", "E2",
205 "Mode_Select", "E3", //valid: 0000,0288,0039,061A,004F
206 "L_Wet_Volume", "E4",
207 "L_Dry_Volume", "E5",
208 "R_Wet_Volume", "E6",
209 "R_Dry_Volume", "E7",
210 NULL
211 };
212 enum q1_register_name {
213 Q1V_BANK = 0,
214 Q1V_START = 1,
215 Q1V_FREQ = 2,
216 Q1V_PHASE = 3,
217 Q1V_LOOP = 4,
218 Q1V_END = 5,
219 Q1V_VOL = 6,
220 Q1V_REG_COUNT = 7,
221
222 Q1_PAN = 0x80,
223 Q1_ECHO = 0xba,
224
225 Q1A_PAN = 0x90,
226 Q1A_START = 0xca,
227 Q1A_END = 0xcb,
228 Q1A_BANK = 0xcc,
229 Q1A_VOL = 0xcd,
230
231 Q1A_KEYON = 0xd6,
232
233 Q1_ECHO_FEEDBACK = 0x93,
234 Q1_ECHO_LENGTH = 0xd9,
235 };
236
237 const unsigned char q1_reg_map[Q1V_REG_COUNT][16] = {
238 {0x78,0x00,0x08,0x10,0x18,0x20,0x28,0x30,0x38,0x40,0x48,0x50,0x58,0x60,0x68,0x70},
239 {0x01,0x09,0x11,0x19,0x21,0x29,0x31,0x39,0x41,0x49,0x51,0x59,0x61,0x69,0x71,0x79},
240 {0x02,0x0a,0x12,0x1a,0x22,0x2a,0x32,0x3a,0x42,0x4a,0x52,0x5a,0x62,0x6a,0x72,0x7a},
241 {0x03,0x0b,0x13,0x1b,0x23,0x2b,0x33,0x3b,0x43,0x4b,0x53,0x5b,0x63,0x6b,0x73,0x7b},
242 {0x04,0x0c,0x14,0x1c,0x24,0x2c,0x34,0x3c,0x44,0x4c,0x54,0x5c,0x64,0x6c,0x74,0x7c},
243 {0x05,0x0d,0x15,0x1d,0x25,0x2d,0x35,0x3d,0x45,0x4d,0x55,0x5d,0x65,0x6d,0x75,0x7d},
244 {0x06,0x0e,0x16,0x1e,0x26,0x2e,0x36,0x3e,0x46,0x4e,0x56,0x5e,0x66,0x6e,0x76,0x7e},
245 };
246
247 const unsigned char q1a_start_map[3]={
248 0xca, 0xce, 0xd2
249 };
250
251 const unsigned char q1a_end_map[3]={
252 0xcb, 0xcf, 0xd3
253 };
254
255 const unsigned char q1a_bank_map[3]={
256 0xcc, 0xd0, 0xd4
257 };
258
259 const unsigned char q1a_vol_map[3]={
260 0xcd, 0xd1, 0xd5
261 };
262
263 const char** DivPlatformQSound::getRegisterSheet() {
264 return regCheatSheetQSound;
265 }
266
267 void DivPlatformQSound::acquire(short** buf, size_t len) {
268 for (int i=0; i<19; i++) {
269 oscBuf[i]->begin(len);
270 }
271
272 for (size_t h=0; h<len; h++) {
273 qsound_update(&chip);
274 buf[0][h]=chip.out[0];
275 buf[1][h]=chip.out[1];
276
277 for (int i=0; i<19; i++) {
278 int data=chip.voice_output[i]<<1;
279 if (data<-32768) data=-32768;
280 if (data>32767) data=32767;
281 oscBuf[i]->putSample(h,data);
282 }
283 }
284
285 for (int i=0; i<19; i++) {
286 oscBuf[i]->end(len);
287 }
288 }
289
290 void DivPlatformQSound::tick(bool sysTick) {
291 for (int i=0; i<19; i++) {
292 chan[i].std.next();
293 if (chan[i].std.vol.had) {
294 if (chan[i].isNewQSound) {
295 chan[i].outVol=((chan[i].vol&0xff)*MIN(16383,chan[i].std.vol.val))/16383;
296 chan[i].resVol=((chan[i].vol&0xff)*MIN(16383,chan[i].std.vol.val))/255;
297 } else {
298 chan[i].outVol=((chan[i].vol&0xff)*chan[i].std.vol.val)>>6;
299 chan[i].resVol=chan[i].outVol<<4;
300 }
301 // Check if enabled and write volume
302 if (chan[i].active) {
303 if (i<16) {
304 rWrite(q1_reg_map[Q1V_VOL][i],chan[i].resVol);
305 } else {
306 rWrite(q1a_vol_map[i-16],chan[i].resVol);
307 }
308 }
309 }
310 uint16_t qsoundBank=0;
311 uint16_t qsoundAddr=0;
312 uint16_t qsoundLoop=0;
313 uint16_t qsoundEnd=0;
314 if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
315 DivSample* s=parent->getSample(chan[i].sample);
316 if (i<16) {
317 qsoundBank=0x8000|(offPCM[chan[i].sample]>>16);
318 qsoundAddr=offPCM[chan[i].sample]&0xffff;
319 } else {
320 qsoundBank=0x8000|(offBS[chan[i].sample]>>16);
321 qsoundAddr=offBS[chan[i].sample]&0xffff;
322 }
323
324 int loopStart=s->loopStart;
325 int length=s->isLoopable()?s->loopEnd:s->samples;
326 if (i<16) {
327 if (length>65536-16) {
328 length=65536-16;
329 }
330 } else {
331 // ADPCM address is byte aligned
332 length>>=1;
333 if (length>65535) {
334 length=65535;
335 }
336 }
337 if (i<16) {
338 if (!s->isLoopable()) {
339 qsoundEnd=offPCM[chan[i].sample]+length+15;
340 qsoundLoop=15;
341 } else {
342 qsoundEnd=offPCM[chan[i].sample]+length;
343 qsoundLoop=length-loopStart;
344 }
345 } else {
346 // ADPCM can't loop
347 qsoundEnd=offBS[chan[i].sample]+length;
348 }
349 }
350 if (NEW_ARP_STRAT) {
351 chan[i].handleArp();
352 } else if (chan[i].std.arp.had) {
353 if (!chan[i].inPorta) {
354 chan[i].baseFreq=QS_NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
355 }
356 chan[i].freqChanged=true;
357 }
358 if (chan[i].isNewQSound && chan[i].std.duty.had) {
359 chan[i].echo=CLAMP(chan[i].std.duty.val,0,32767);
360 if (i<16) {
361 immWrite(Q1_ECHO+i,chan[i].echo&0x7fff);
362 }
363 }
364 if (chan[i].isNewQSound && chan[i].std.ex1.had) {
365 immWrite(Q1_ECHO_FEEDBACK,chan[i].std.ex1.val&0x3fff);
366 }
367 if (chan[i].isNewQSound && chan[i].std.ex2.had) {
368 immWrite(Q1_ECHO_LENGTH,0xfff-(2725-CLAMP(chan[i].std.ex2.val&0xfff,0,2725)));
369 }
370 if (chan[i].std.pitch.had) {
371 if (chan[i].std.pitch.mode) {
372 chan[i].pitch2+=chan[i].std.pitch.val;
373 CLAMP_VAR(chan[i].pitch2,-32768,32767);
374 } else {
375 chan[i].pitch2=chan[i].std.pitch.val;
376 }
377 chan[i].freqChanged=true;
378 }
379 if (chan[i].std.panL.had) { // panning
380 chan[i].panning=chan[i].std.panL.val+16;
381 }
382 if (chan[i].std.panR.had) { // surround
383 chan[i].surround=chan[i].std.panR.val;
384 }
385 if (chan[i].std.panL.had || chan[i].std.panR.had) {
386 immWrite(Q1_PAN+i,chan[i].panning+0x110+(chan[i].surround?0:0x30));
387 }
388 if (chan[i].std.phaseReset.had) {
389 if (chan[i].std.phaseReset.val==1 && chan[i].active && (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen)) {
390 chan[i].keyOn=true;
391 }
392 }
393 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
394 //DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_AMIGA);
395 double off=1.0;
396 if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
397 DivSample* s=parent->getSample(chan[i].sample);
398 if (s->centerRate<1) {
399 off=1.0;
400 } else {
401 off=(double)s->centerRate/24038.0/16.0;
402 }
403 }
404 chan[i].freq=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,440.0,4096.0);
405 if (chan[i].freq>0xefff) chan[i].freq=0xefff;
406 if (chan[i].keyOn) {
407 if (chan[i].setPos) {
408 chan[i].setPos=false;
409 } else {
410 chan[i].audPos=0;
411 }
412
413 if (i<16) {
414 rWrite(q1_reg_map[Q1V_BANK][i], qsoundBank);
415 rWrite(q1_reg_map[Q1V_END][i], qsoundEnd);
416 rWrite(q1_reg_map[Q1V_LOOP][i], qsoundLoop);
417 rWrite(q1_reg_map[Q1V_START][i], qsoundAddr+chan[i].audPos);
418 rWrite(q1_reg_map[Q1V_PHASE][i], 0x8000);
419 } else {
420 rWrite(Q1A_KEYON+(i-16),0);
421 rWrite(q1a_bank_map[i-16], qsoundBank);
422 rWrite(q1a_end_map[i-16], qsoundEnd);
423 rWrite(q1a_start_map[i-16], qsoundAddr+chan[i].audPos);
424 rWrite(Q1A_KEYON+(i-16),1);
425 }
426 //logV("ch %d bank=%04x, addr=%04x, end=%04x, loop=%04x!",i,qsoundBank,qsoundAddr,qsoundEnd,qsoundLoop);
427 // Write sample address. Enable volume
428 if (!chan[i].std.vol.had) {
429 if (chan[i].isNewQSound) {
430 chan[i].resVol=(chan[i].vol*16383)/255;
431 } else {
432 chan[i].resVol=chan[i].vol<<4;
433 }
434 if (i<16) {
435 rWrite(q1_reg_map[Q1V_VOL][i],chan[i].resVol);
436 } else {
437 rWrite(q1a_vol_map[i-16],chan[i].resVol);
438 }
439 }
440 }
441 if (chan[i].keyOff) {
442 // Disable volume
443 if (i<16) {
444 rWrite(q1_reg_map[Q1V_VOL][i],0);
445 rWrite(q1_reg_map[Q1V_FREQ][i],0);
446 } else {
447 rWrite(q1a_vol_map[i-16],0);
448 rWrite(Q1A_KEYON+(i-16),0);
449 rWrite(q1a_end_map[i-16], 1);
450 rWrite(q1a_start_map[i-16], 0);
451 rWrite(Q1A_KEYON+(i-16),1);
452 }
453 } else if (chan[i].active) {
454 //logV("ch %d frequency set to %04x, off=%f, note=%d, %04x!",i,chan[i].freq,off,chan[i].note,QS_NOTE_FREQUENCY(chan[i].note));
455 if (i<16) {
456 rWrite(q1_reg_map[Q1V_FREQ][i],chan[i].freq);
457 }
458 }
459 if (chan[i].keyOn) chan[i].keyOn=false;
460 if (chan[i].keyOff) chan[i].keyOff=false;
461 chan[i].freqChanged=false;
462 }
463 }
464 }
465
466 int DivPlatformQSound::dispatch(DivCommand c) {
467 switch (c.cmd) {
468 case DIV_CMD_NOTE_ON: {
469 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA);
470 chan[c.chan].isNewQSound=(ins->type==DIV_INS_QSOUND);
471 if (c.value!=DIV_NOTE_NULL) {
472 chan[c.chan].sample=ins->amiga.getSample(c.value);
473 chan[c.chan].sampleNote=c.value;
474 c.value=ins->amiga.getFreq(c.value);
475 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
476 }
477 if (c.value!=DIV_NOTE_NULL) {
478 chan[c.chan].baseFreq=QS_NOTE_FREQUENCY(c.value);
479 }
480 if (chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) {
481 chan[c.chan].sample=-1;
482 }
483 if (c.value!=DIV_NOTE_NULL) {
484 chan[c.chan].freqChanged=true;
485 chan[c.chan].note=c.value;
486 }
487 chan[c.chan].active=true;
488 chan[c.chan].keyOn=true;
489 chan[c.chan].keyOff=false;
490 chan[c.chan].macroInit(ins);
491 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
492 chan[c.chan].outVol=chan[c.chan].vol;
493 if (chan[c.chan].isNewQSound) {
494 chan[c.chan].resVol=(chan[c.chan].outVol*16383)/255;
495 } else {
496 chan[c.chan].resVol=chan[c.chan].outVol<<4;
497 }
498 }
499 break;
500 }
501 case DIV_CMD_NOTE_OFF:
502 chan[c.chan].sample=-1;
503 chan[c.chan].active=false;
504 chan[c.chan].keyOff=true;
505 chan[c.chan].macroInit(NULL);
506 break;
507 case DIV_CMD_NOTE_OFF_ENV:
508 case DIV_CMD_ENV_RELEASE:
509 chan[c.chan].std.release();
510 break;
511 case DIV_CMD_INSTRUMENT:
512 if (chan[c.chan].ins!=c.value || c.value2==1) {
513 chan[c.chan].ins=c.value;
514 }
515 break;
516 case DIV_CMD_VOLUME:
517 if (chan[c.chan].vol!=c.value) {
518 chan[c.chan].vol=c.value;
519 if (!chan[c.chan].std.vol.has) {
520 // Check if enabled and write volume
521 chan[c.chan].outVol=c.value;
522 if (chan[c.chan].isNewQSound) {
523 chan[c.chan].resVol=(chan[c.chan].outVol*16383)/255;
524 } else {
525 chan[c.chan].resVol=chan[c.chan].outVol<<4;
526 }
527 if (chan[c.chan].active) {
528 if (c.chan<16) {
529 rWrite(q1_reg_map[Q1V_VOL][c.chan],chan[c.chan].resVol);
530 } else {
531 rWrite(q1a_vol_map[c.chan-16],chan[c.chan].resVol);
532 }
533 }
534 }
535 }
536 break;
537 case DIV_CMD_GET_VOLUME:
538 if (chan[c.chan].std.vol.has) {
539 return chan[c.chan].vol;
540 }
541 return chan[c.chan].outVol;
542 break;
543 case DIV_CMD_PANNING:
544 chan[c.chan].panning=parent->convertPanSplitToLinearLR(c.value,c.value2,32);
545 immWrite(Q1_PAN+c.chan,chan[c.chan].panning+0x110+(chan[c.chan].surround?0:0x30));
546 break;
547 case DIV_CMD_QSOUND_ECHO_LEVEL:
548 chan[c.chan].echo=c.value<<7;
549 if (c.chan>=16) break;
550 immWrite(Q1_ECHO+c.chan,chan[c.chan].echo&0x7fff);
551 break;
552 case DIV_CMD_QSOUND_ECHO_FEEDBACK:
553 immWrite(Q1_ECHO_FEEDBACK, c.value << 6);
554 break;
555 case DIV_CMD_QSOUND_ECHO_DELAY:
556 immWrite(Q1_ECHO_LENGTH, (c.value > 2725 ? 0xfff : 0xfff - (2725 - c.value)));
557 break;
558 case DIV_CMD_QSOUND_SURROUND:
559 chan[c.chan].surround=c.value;
560 immWrite(Q1_PAN+c.chan,chan[c.chan].panning+0x110+(chan[c.chan].surround?0:0x30));
561 break;
562 case DIV_CMD_PITCH:
563 chan[c.chan].pitch=c.value;
564 chan[c.chan].freqChanged=true;
565 break;
566 case DIV_CMD_NOTE_PORTA: {
567 int destFreq=QS_NOTE_FREQUENCY(c.value2+chan[c.chan].sampleNoteDelta);
568 bool return2=false;
569 if (destFreq>chan[c.chan].baseFreq) {
570 chan[c.chan].baseFreq+=c.value;
571 if (chan[c.chan].baseFreq>=destFreq) {
572 chan[c.chan].baseFreq=destFreq;
573 return2=true;
574 }
575 } else {
576 chan[c.chan].baseFreq-=c.value;
577 if (chan[c.chan].baseFreq<=destFreq) {
578 chan[c.chan].baseFreq=destFreq;
579 return2=true;
580 }
581 }
582 chan[c.chan].freqChanged=true;
583 if (return2) {
584 chan[c.chan].inPorta=false;
585 return 2;
586 }
587 break;
588 }
589 case DIV_CMD_LEGATO: {
590 chan[c.chan].baseFreq=QS_NOTE_FREQUENCY(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(0)));
591 chan[c.chan].freqChanged=true;
592 chan[c.chan].note=c.value;
593 break;
594 }
595 case DIV_CMD_PRE_PORTA:
596 if (chan[c.chan].active && c.value2) {
597 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA));
598 }
599 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=QS_NOTE_FREQUENCY(chan[c.chan].note);
600 chan[c.chan].inPorta=c.value;
601 break;
602 case DIV_CMD_SAMPLE_POS:
603 chan[c.chan].audPos=c.value;
604 chan[c.chan].setPos=true;
605 if (chan[c.chan].active) {
606 chan[c.chan].keyOn=true;
607 }
608 break;
609 case DIV_CMD_GET_VOLMAX:
610 return 255;
611 break;
612 case DIV_CMD_MACRO_OFF:
613 chan[c.chan].std.mask(c.value,true);
614 break;
615 case DIV_CMD_MACRO_ON:
616 chan[c.chan].std.mask(c.value,false);
617 break;
618 case DIV_CMD_MACRO_RESTART:
619 chan[c.chan].std.restart(c.value);
620 break;
621 default:
622 break;
623 }
624 return 1;
625 }
626
627 void DivPlatformQSound::muteChannel(int ch, bool mute) {
628 if (mute) {
629 chip.mute_mask|=(1<<ch);
630 } else {
631 chip.mute_mask&=~(1<<ch);
632 }
633 }
634
635 void DivPlatformQSound::forceIns() {
636 // TODO: what?
637 for (int i=0; i<19; i++) {
638 chan[i].insChanged=true;
639 chan[i].freqChanged=true;
640 chan[i].keyOff=true;
641 //chan[i].sample=-1;
642 }
643 }
644
645 void* DivPlatformQSound::getChanState(int ch) {
646 return &chan[ch];
647 }
648
649 DivMacroInt* DivPlatformQSound::getChanMacroInt(int ch) {
650 return &chan[ch].std;
651 }
652
653 unsigned short DivPlatformQSound::getPan(int ch) {
654 return parent->convertPanLinearToSplit(chan[ch].panning,8,32);
655 }
656
657 DivDispatchOscBuffer* DivPlatformQSound::getOscBuffer(int ch) {
658 return oscBuf[ch];
659 }
660
661 void DivPlatformQSound::reset() {
662 for (int i=0; i<19; i++) {
663 chan[i]=DivPlatformQSound::Channel();
664 chan[i].std.setEngine(parent);
665 }
666 qsound_reset(&chip);
667 while(!chip.ready_flag) {
668 qsound_update(&chip);
669 }
670
671 immWrite(Q1_ECHO_LENGTH, 0xfff - (2725 - echoDelay));
672 immWrite(Q1_ECHO_FEEDBACK, echoFeedback << 6);
673 }
674
675 int DivPlatformQSound::getOutputCount() {
676 return 2;
677 }
678
679 bool DivPlatformQSound::keyOffAffectsArp(int ch) {
680 return true;
681 }
682
683 void DivPlatformQSound::notifyInsChange(int ins) {
684 for (int i=0; i<19; i++) {
685 if (chan[i].ins==ins) {
686 chan[i].insChanged=true;
687 }
688 }
689 }
690
691 void DivPlatformQSound::notifyWaveChange(int wave) {
692 // yeah won't add wavetables
693 }
694
695 void DivPlatformQSound::notifyInsDeletion(void* ins) {
696 for (int i=0; i<19; i++) {
697 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
698 }
699 }
700
701 void DivPlatformQSound::setFlags(const DivConfig& flags) {
702 echoDelay = 2725 - flags.getInt("echoDelay",0);
703 echoFeedback = flags.getInt("echoFeedback",0) & 255;
704
705 if (echoDelay < 0) {
706 echoDelay = 0;
707 }
708 if (echoDelay > 2725) {
709 echoDelay = 2725;
710 }
711 //rate=chipClock/CHIP_DIVIDER;
712 }
713
714 void DivPlatformQSound::poke(unsigned int addr, unsigned short val) {
715 immWrite(addr, val);
716 immWrite(addr, val);
717 }
718
719 void DivPlatformQSound::poke(std::vector<DivRegWrite>& wlist) {
720 for (DivRegWrite& i: wlist) immWrite(i.addr,i.val);
721 }
722
723 unsigned char* DivPlatformQSound::getRegisterPool() {
724 unsigned short* regPoolPtr = regPool;
725 for(int i=0; i<256; i++)
726 {
727 uint16_t data = qsound_read_data(&chip, i);
728 *regPoolPtr++ = data;
729 }
730 return (unsigned char*)regPool;
731 }
732
733 int DivPlatformQSound::getRegisterPoolSize() {
734 return 256;
735 }
736
737 int DivPlatformQSound::getRegisterPoolDepth() {
738 return 16;
739 }
740
741 const void* DivPlatformQSound::getSampleMem(int index) {
742 return (index == 0 || index == 1) ? sampleMem : NULL;
743 }
744
745 size_t DivPlatformQSound::getSampleMemCapacity(int index) {
746 return index == 0 ? 16777216 : index == 1 ? (sampleMemUsage>=16777216?1:(16777216 - sampleMemUsage)) : 0;
747 }
748
749 size_t DivPlatformQSound::getSampleMemUsage(int index) {
750 return index == 0 ? sampleMemLen : index == 1 ? ((sampleMemUsage>=sampleMemLenBS)?0:(sampleMemLenBS - sampleMemUsage)) : 0;
751 }
752
753 bool DivPlatformQSound::isSampleLoaded(int index, int sample) {
754 if (index<0 || index>1) return false;
755 if (sample<0 || sample>32767) return false;
756 if (index==1) return sampleLoadedBS[sample];
757 return sampleLoaded[sample];
758 }
759
760 const char* DivPlatformQSound::getSampleMemName(int index) {
761 return index == 0 ? "PCM" : index == 1 ? "ADPCM" : NULL;
762 }
763
764 const DivMemoryComposition* DivPlatformQSound::getMemCompo(int index) {
765 if (index!=0) return NULL;
766 return &memCompo;
767 }
768
769 void DivPlatformQSound::renderSamples(int sysID) {
770 memset(sampleMem,0,getSampleMemCapacity());
771 memset(offPCM,0,32768*sizeof(unsigned int));
772 memset(offBS,0,32768*sizeof(unsigned int));
773 memset(sampleLoaded,0,32768*sizeof(bool));
774 memset(sampleLoadedBS,0,32768*sizeof(bool));
775
776 memCompo=DivMemoryComposition();
777 memCompo.name="Sample ROM";
778
779 size_t memPos=0;
780 for (int i=0; i<parent->song.sampleLen; i++) {
781 DivSample* s=parent->song.sample[i];
782 if (!s->renderOn[0][sysID]) {
783 offPCM[i]=0;
784 continue;
785 }
786
787 int length=s->length8;
788 if (length>65536-16) {
789 length=65536-16;
790 }
791 if ((memPos&0xff0000)!=((memPos+length)&0xff0000)) {
792 memPos=(memPos+0xffff)&0xff0000;
793 }
794 if (memPos>=getSampleMemCapacity()) {
795 logW("out of QSound PCM memory for sample %d!",i);
796 break;
797 }
798 if (memPos+length>=getSampleMemCapacity()) {
799 for (unsigned int i=0; i<getSampleMemCapacity()-(memPos+length); i++) {
800 sampleMem[(memPos+i)^0x8000]=s->data8[i];
801 }
802 logW("out of QSound PCM memory for sample %d!",i);
803 } else {
804 for (int i=0; i<length; i++) {
805 sampleMem[(memPos+i)^0x8000]=s->data8[i];
806 }
807 sampleLoaded[i]=true;
808 }
809 offPCM[i]=memPos^0x8000;
810 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"PCM",i,memPos,memPos+length));
811 memPos+=length+16;
812 }
813 sampleMemLen=memPos+256;
814
815 memPos=(memPos+0xffff)&0xff0000;
816 sampleMemUsage=memPos;
817
818 for (int i=0; i<parent->song.sampleLen; i++) {
819 DivSample* s=parent->song.sample[i];
820 if (!s->renderOn[1][sysID]) {
821 offBS[i]=0;
822 continue;
823 }
824
825 int length=s->lengthQSoundA;
826 if (length>65536) {
827 length=65536;
828 }
829 if ((memPos&0xff0000)!=((memPos+length)&0xff0000)) {
830 memPos=(memPos+0xffff)&0xff0000;
831 }
832 if (memPos>=getSampleMemCapacity()) {
833 logW("out of QSound ADPCM memory for sample %d!",i);
834 break;
835 }
836 if (memPos+length>=getSampleMemCapacity()) {
837 for (unsigned int i=0; i<getSampleMemCapacity()-(memPos+length); i++) {
838 sampleMem[(memPos+i)]=s->dataQSoundA[i];
839 }
840 logW("out of QSound ADPCM memory for sample %d!",i);
841 } else {
842 for (int i=0; i<length; i++) {
843 sampleMem[(memPos+i)]=s->dataQSoundA[i];
844 }
845 sampleLoadedBS[i]=true;
846 }
847 offBS[i]=memPos;
848 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE_ALT1,"ADPCM",i,memPos,memPos+length));
849 memPos+=length;
850 }
851 sampleMemLenBS=memPos+256;
852
853 memCompo.used=sampleMemLenBS;
854 memCompo.capacity=getSampleMemCapacity(0);
855 }
856
857 int DivPlatformQSound::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
858 parent=p;
859 dumpWrites=false;
860 skipRegisterWrites=false;
861
862 for (int i=0; i<19; i++) {
863 oscBuf[i]=new DivDispatchOscBuffer;
864 //isMuted[i]=false;
865 }
866 setFlags(flags);
867
868 chipClock=60000000;
869 rate = qsound_start(&chip, chipClock);
870 sampleMem=new unsigned char[getSampleMemCapacity()];
871 sampleMemLen=0;
872 sampleMemLenBS=0;
873 sampleMemUsage=0;
874 chip.rom_data=sampleMem;
875 chip.rom_mask=0xffffff;
876 reset();
877
878 for (int i=0; i<19; i++) {
879 oscBuf[i]->setRate(rate);
880 }
881 return 19;
882 }
883
884 void DivPlatformQSound::quit() {
885 delete[] sampleMem;
886 for (int i=0; i<19; i++) {
887 delete oscBuf[i];
888 }
889 }
890
891 // initialization of important arrays
892 DivPlatformQSound::DivPlatformQSound() {
893 offPCM=new unsigned int[32768];
894 offBS=new unsigned int[32768];
895 sampleLoaded=new bool[32768];
896 sampleLoadedBS=new bool[32768];
897 }
898
899 DivPlatformQSound::~DivPlatformQSound() {
900 delete[] offPCM;
901 delete[] offBS;
902 delete[] sampleLoaded;
903 delete[] sampleLoadedBS;
904 }