Debian Codegraph @

src/engine/vgmOps.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 "engine.h"
21 #include "../ta-log.h"
22 #include "../utfutils.h"
23 #include "song.h"
24
25 // this function is so long
26 // may as well make it something else
27 void DivEngine::performVGMWrite(SafeWriter* w, DivSystem sys, DivRegWrite& write, int streamOff, double* loopTimer, double* loopFreq, int* loopSample, bool* sampleDir, bool isSecond, int* pendingFreq, int* playingSample, int* setPos, unsigned int* sampleOff8, unsigned int* sampleLen8, size_t bankOffset, bool directStream, bool* sampleStoppable, bool dpcm07, DivDispatch** writeNES, int rateCorrection) {
28 unsigned char baseAddr1=isSecond?0xa0:0x50;
29 unsigned char baseAddr2=isSecond?0x80:0;
30 unsigned short baseAddr2S=isSecond?0x8000:0;
31 unsigned char smsAddr=isSecond?0x30:0x50;
32 unsigned char ggAddr=isSecond?0x3f:0x4f;
33 unsigned char rf5c68Addr=isSecond?0xb1:0xb0;
34 if (write.addr==0xffffffff) { // Furnace fake reset
35 switch (sys) {
36 case DIV_SYSTEM_YM2612:
37 case DIV_SYSTEM_YM2612_EXT:
38 case DIV_SYSTEM_YM2612_DUALPCM:
39 case DIV_SYSTEM_YM2612_DUALPCM_EXT:
40 case DIV_SYSTEM_YM2612_CSM:
41 for (int i=0; i<3; i++) { // set SL and RR to highest
42 w->writeC(2|baseAddr1);
43 w->writeC(0x80+i);
44 w->writeC(0xff);
45 w->writeC(2|baseAddr1);
46 w->writeC(0x84+i);
47 w->writeC(0xff);
48 w->writeC(2|baseAddr1);
49 w->writeC(0x88+i);
50 w->writeC(0xff);
51 w->writeC(2|baseAddr1);
52 w->writeC(0x8c+i);
53 w->writeC(0xff);
54
55 w->writeC(3|baseAddr1);
56 w->writeC(0x80+i);
57 w->writeC(0xff);
58 w->writeC(3|baseAddr1);
59 w->writeC(0x84+i);
60 w->writeC(0xff);
61 w->writeC(3|baseAddr1);
62 w->writeC(0x88+i);
63 w->writeC(0xff);
64 w->writeC(3|baseAddr1);
65 w->writeC(0x8c+i);
66 w->writeC(0xff);
67 }
68 for (int i=0; i<3; i++) { // note off
69 w->writeC(2|baseAddr1);
70 w->writeC(0x28);
71 w->writeC(i);
72 w->writeC(2|baseAddr1);
73 w->writeC(0x28);
74 w->writeC(4+i);
75 }
76 w->writeC(2|baseAddr1); // disable DAC
77 w->writeC(0x2b);
78 w->writeC(0);
79 break;
80 case DIV_SYSTEM_SMS:
81 for (int i=0; i<4; i++) {
82 w->writeC(smsAddr);
83 w->writeC(0x90|(i<<5)|15);
84 }
85 break;
86 case DIV_SYSTEM_T6W28:
87 for (int i=0; i<4; i++) {
88 w->writeC(0x30);
89 w->writeC(0x90|(i<<5)|15);
90 w->writeC(0x50);
91 w->writeC(0x90|(i<<5)|15);
92 }
93 break;
94 case DIV_SYSTEM_GB:
95 // square 1
96 w->writeC(0xb3);
97 w->writeC(2|baseAddr2);
98 w->writeC(0);
99 w->writeC(0xb3);
100 w->writeC(4|baseAddr2);
101 w->writeC(0x80);
102
103 // square 2
104 w->writeC(0xb3);
105 w->writeC(7|baseAddr2);
106 w->writeC(0);
107 w->writeC(0xb3);
108 w->writeC(9|baseAddr2);
109 w->writeC(0x80);
110
111 // wave
112 w->writeC(0xb3);
113 w->writeC(0x0c|baseAddr2);
114 w->writeC(0);
115 w->writeC(0xb3);
116 w->writeC(0x0e|baseAddr2);
117 w->writeC(0x80);
118
119 // noise
120 w->writeC(0xb3);
121 w->writeC(0x11|baseAddr2);
122 w->writeC(0);
123 w->writeC(0xb3);
124 w->writeC(0x13|baseAddr2);
125 w->writeC(0x80);
126 break;
127 case DIV_SYSTEM_PCE:
128 for (int i=0; i<6; i++) {
129 w->writeC(0xb9);
130 w->writeC(0|baseAddr2);
131 w->writeC(i);
132 w->writeC(0xb9);
133 w->writeC(4|baseAddr2);
134 w->writeC(0x5f);
135 w->writeC(0xb9);
136 w->writeC(4|baseAddr2);
137 w->writeC(0x1f);
138 for (int j=0; j<32; j++) {
139 w->writeC(0xb9);
140 w->writeC(6|baseAddr2);
141 w->writeC(0);
142 }
143 }
144 break;
145 case DIV_SYSTEM_NES:
146 w->writeC(0xb4);
147 w->writeC(0x15|baseAddr2);
148 w->writeC(0);
149 break;
150 case DIV_SYSTEM_YM2151:
151 for (int i=0; i<8; i++) {
152 w->writeC(4|baseAddr1);
153 w->writeC(0xe0+i);
154 w->writeC(0xff);
155 w->writeC(4|baseAddr1);
156 w->writeC(0xe8+i);
157 w->writeC(0xff);
158 w->writeC(4|baseAddr1);
159 w->writeC(0xf0+i);
160 w->writeC(0xff);
161 w->writeC(4|baseAddr1);
162 w->writeC(0xf8+i);
163 w->writeC(0xff);
164
165 w->writeC(4|baseAddr1);
166 w->writeC(0x08);
167 w->writeC(i);
168 }
169 break;
170 case DIV_SYSTEM_SEGAPCM:
171 case DIV_SYSTEM_SEGAPCM_COMPAT:
172 for (int i=0; i<16; i++) {
173 w->writeC(0xc0);
174 w->writeS((0x86|baseAddr2S)+(i<<3));
175 w->writeC(3);
176 }
177 break;
178 case DIV_SYSTEM_X1_010:
179 for (int i=0; i<16; i++) {
180 w->writeC(0xc8);
181 w->writeS_BE(baseAddr2S+(i<<3));
182 w->writeC(0);
183 }
184 break;
185 case DIV_SYSTEM_YM2610:
186 case DIV_SYSTEM_YM2610_FULL:
187 case DIV_SYSTEM_YM2610B:
188 case DIV_SYSTEM_YM2610_EXT:
189 case DIV_SYSTEM_YM2610_FULL_EXT:
190 case DIV_SYSTEM_YM2610B_EXT:
191 case DIV_SYSTEM_YM2610_CSM:
192 case DIV_SYSTEM_YM2610B_CSM:
193 // TODO: YM2610B channels 1 and 4 and ADPCM-B
194 for (int i=0; i<2; i++) { // set SL and RR to highest
195 w->writeC(8|baseAddr1);
196 w->writeC(0x81+i);
197 w->writeC(0xff);
198 w->writeC(8|baseAddr1);
199 w->writeC(0x85+i);
200 w->writeC(0xff);
201 w->writeC(8|baseAddr1);
202 w->writeC(0x89+i);
203 w->writeC(0xff);
204 w->writeC(8|baseAddr1);
205 w->writeC(0x8d+i);
206 w->writeC(0xff);
207
208 w->writeC(9|baseAddr1);
209 w->writeC(0x81+i);
210 w->writeC(0xff);
211 w->writeC(9|baseAddr1);
212 w->writeC(0x85+i);
213 w->writeC(0xff);
214 w->writeC(9|baseAddr1);
215 w->writeC(0x89+i);
216 w->writeC(0xff);
217 w->writeC(9|baseAddr1);
218 w->writeC(0x8d+i);
219 w->writeC(0xff);
220 }
221 for (int i=0; i<2; i++) { // note off
222 w->writeC(8|baseAddr1);
223 w->writeC(0x28);
224 w->writeC(1+i);
225 w->writeC(8|baseAddr1);
226 w->writeC(0x28);
227 w->writeC(5+i);
228 }
229
230 // reset AY
231 w->writeC(8|baseAddr1);
232 w->writeC(7);
233 w->writeC(0x3f);
234
235 w->writeC(8|baseAddr1);
236 w->writeC(8);
237 w->writeC(0);
238
239 w->writeC(8|baseAddr1);
240 w->writeC(9);
241 w->writeC(0);
242
243 w->writeC(8|baseAddr1);
244 w->writeC(10);
245 w->writeC(0);
246
247 // reset sample
248 w->writeC(9|baseAddr1);
249 w->writeC(0);
250 w->writeC(0xbf);
251 break;
252 case DIV_SYSTEM_OPLL:
253 case DIV_SYSTEM_OPLL_DRUMS:
254 case DIV_SYSTEM_VRC7:
255 for (int i=0; i<9; i++) {
256 w->writeC(1|baseAddr1);
257 w->writeC(0x20+i);
258 w->writeC(0);
259 w->writeC(1|baseAddr1);
260 w->writeC(0x30+i);
261 w->writeC(0);
262 w->writeC(1|baseAddr1);
263 w->writeC(0x10+i);
264 w->writeC(0);
265 }
266 break;
267 case DIV_SYSTEM_YM2203:
268 case DIV_SYSTEM_YM2203_EXT:
269 case DIV_SYSTEM_YM2203_CSM:
270 for (int i=0; i<3; i++) { // set SL and RR to highest
271 w->writeC(5|baseAddr1);
272 w->writeC(0x80+i);
273 w->writeC(0xff);
274 w->writeC(5|baseAddr1);
275 w->writeC(0x84+i);
276 w->writeC(0xff);
277 w->writeC(5|baseAddr1);
278 w->writeC(0x88+i);
279 w->writeC(0xff);
280 w->writeC(5|baseAddr1);
281 w->writeC(0x8c+i);
282 w->writeC(0xff);
283 }
284 for (int i=0; i<3; i++) { // note off
285 w->writeC(5|baseAddr1);
286 w->writeC(0x28);
287 w->writeC(i);
288 }
289
290 // SSG
291 w->writeC(5|baseAddr1);
292 w->writeC(7);
293 w->writeC(0x3f);
294
295 w->writeC(5|baseAddr1);
296 w->writeC(8);
297 w->writeC(0);
298
299 w->writeC(5|baseAddr1);
300 w->writeC(9);
301 w->writeC(0);
302
303 w->writeC(5|baseAddr1);
304 w->writeC(10);
305 w->writeC(0);
306 break;
307 case DIV_SYSTEM_AY8910:
308 w->writeC(0xa0);
309 w->writeC(7|baseAddr2);
310 w->writeC(0x3f);
311
312 w->writeC(0xa0);
313 w->writeC(8|baseAddr2);
314 w->writeC(0);
315
316 w->writeC(0xa0);
317 w->writeC(9|baseAddr2);
318 w->writeC(0);
319
320 w->writeC(0xa0);
321 w->writeC(10|baseAddr2);
322 w->writeC(0);
323 break;
324 case DIV_SYSTEM_AY8930:
325 w->writeC(0xa0);
326 w->writeC(0x0d|baseAddr2);
327 w->writeC(0);
328 w->writeC(0xa0);
329 w->writeC(0x0d|baseAddr2);
330 w->writeC(0xa0);
331 break;
332 case DIV_SYSTEM_SAA1099:
333 w->writeC(0xbd);
334 w->writeC(0x1c|baseAddr2);
335 w->writeC(0x02);
336 w->writeC(0xbd);
337 w->writeC(0x14|baseAddr2);
338 w->writeC(0);
339 w->writeC(0xbd);
340 w->writeC(0x15|baseAddr2);
341 w->writeC(0);
342
343 for (int i=0; i<6; i++) {
344 w->writeC(0xbd);
345 w->writeC((0|baseAddr2)+i);
346 w->writeC(0);
347 }
348 break;
349 case DIV_SYSTEM_POKEY:
350 for (int i=0; i<9; i++) {
351 w->writeC(0xbb);
352 w->writeC(i|baseAddr2);
353 w->writeC(0);
354 }
355 break;
356 case DIV_SYSTEM_LYNX:
357 w->writeC(0x40);
358 w->writeC(0x44);
359 w->writeC(0xff); //stereo attenuation select
360 w->writeC(0x40);
361 w->writeC(0x50);
362 w->writeC(0x00); //stereo channel disable
363 for (int i=0; i<4; i++) { //stereo attenuation value
364 w->writeC(0x40);
365 w->writeC(0x40+i);
366 w->writeC(0xff);
367 }
368 break;
369 case DIV_SYSTEM_QSOUND:
370 for (int i=0; i<16; i++) {
371 w->writeC(0xc4);
372 w->writeC(0);
373 w->writeC(0);
374 w->writeC(2+(i*8));
375 w->writeC(0xc4);
376 w->writeC(0);
377 w->writeC(0);
378 w->writeC(6+(i*8));
379 }
380 for (int i=0; i<3; i++) {
381 w->writeC(0xc4);
382 w->writeC(0);
383 w->writeC(0);
384 w->writeC(0xcd+(i*4));
385 w->writeC(0xc4);
386 w->writeC(0x00);
387 w->writeC(0x01);
388 w->writeC(0xd6+i);
389 }
390 break;
391 case DIV_SYSTEM_ES5506:
392 for (int i=0; i<32; i++) {
393 for (int b=0; b<4; b++) {
394 w->writeC(0xbe);
395 w->writeC(baseAddr2|((0xf<<2)+b));
396 w->writeC((b==3)?i:0);
397 }
398 unsigned int init_cr=0x0303;
399 for (int b=0; b<4; b++) {
400 w->writeC(0xbe);
401 w->writeC(baseAddr2|b);
402 w->writeC(init_cr>>(24-(b<<3)));
403 }
404 for (int r=1; r<11; r++) {
405 for (int b=0; b<4; b++) {
406 w->writeC(0xbe);
407 w->writeC(baseAddr2|((r<<2)+b));
408 w->writeC(((r==7 || r==9) && b&2)?0xff:0);
409 }
410 }
411 for (int b=0; b<4; b++) {
412 w->writeC(0xbe);
413 w->writeC(baseAddr2|((0xf<<2)+b));
414 w->writeC(0x20|i);
415 }
416 for (int r=1; r<10; r++) {
417 for (int b=0; b<4; b++) {
418 w->writeC(0xbe);
419 w->writeC(baseAddr2|((r<<2)+b));
420 w->writeC(0);
421 }
422 }
423 }
424 break;
425 case DIV_SYSTEM_OPL:
426 case DIV_SYSTEM_OPL_DRUMS:
427 // disable envelope
428 for (int i=0; i<6; i++) {
429 w->writeC(0x0b|baseAddr1);
430 w->writeC(0x80+i);
431 w->writeC(0x0f);
432 w->writeC(0x0b|baseAddr1);
433 w->writeC(0x88+i);
434 w->writeC(0x0f);
435 w->writeC(0x0b|baseAddr1);
436 w->writeC(0x90+i);
437 w->writeC(0x0f);
438 }
439 // key off + freq reset
440 for (int i=0; i<9; i++) {
441 w->writeC(0x0b|baseAddr1);
442 w->writeC(0xa0+i);
443 w->writeC(0);
444 w->writeC(0x0b|baseAddr1);
445 w->writeC(0xb0+i);
446 w->writeC(0);
447 }
448 break;
449 case DIV_SYSTEM_Y8950:
450 case DIV_SYSTEM_Y8950_DRUMS:
451 // disable envelope
452 for (int i=0; i<6; i++) {
453 w->writeC(0x0c|baseAddr1);
454 w->writeC(0x80+i);
455 w->writeC(0x0f);
456 w->writeC(0x0c|baseAddr1);
457 w->writeC(0x88+i);
458 w->writeC(0x0f);
459 w->writeC(0x0c|baseAddr1);
460 w->writeC(0x90+i);
461 w->writeC(0x0f);
462 }
463 // key off + freq reset
464 for (int i=0; i<9; i++) {
465 w->writeC(0x0c|baseAddr1);
466 w->writeC(0xa0+i);
467 w->writeC(0);
468 w->writeC(0x0c|baseAddr1);
469 w->writeC(0xb0+i);
470 w->writeC(0);
471 }
472 // TODO: ADPCM
473 break;
474 case DIV_SYSTEM_OPL2:
475 case DIV_SYSTEM_OPL2_DRUMS:
476 // disable envelope
477 for (int i=0; i<6; i++) {
478 w->writeC(0x0a|baseAddr1);
479 w->writeC(0x80+i);
480 w->writeC(0x0f);
481 w->writeC(0x0a|baseAddr1);
482 w->writeC(0x88+i);
483 w->writeC(0x0f);
484 w->writeC(0x0a|baseAddr1);
485 w->writeC(0x90+i);
486 w->writeC(0x0f);
487 }
488 // key off + freq reset
489 for (int i=0; i<9; i++) {
490 w->writeC(0x0a|baseAddr1);
491 w->writeC(0xa0+i);
492 w->writeC(0);
493 w->writeC(0x0a|baseAddr1);
494 w->writeC(0xb0+i);
495 w->writeC(0);
496 }
497 break;
498 case DIV_SYSTEM_OPL3:
499 case DIV_SYSTEM_OPL3_DRUMS:
500 // disable envelope
501 for (int i=0; i<6; i++) {
502 w->writeC(0x0e|baseAddr1);
503 w->writeC(0x80+i);
504 w->writeC(0x0f);
505 w->writeC(0x0e|baseAddr1);
506 w->writeC(0x88+i);
507 w->writeC(0x0f);
508 w->writeC(0x0e|baseAddr1);
509 w->writeC(0x90+i);
510 w->writeC(0x0f);
511 w->writeC(0x0f|baseAddr1);
512 w->writeC(0x80+i);
513 w->writeC(0x0f);
514 w->writeC(0x0f|baseAddr1);
515 w->writeC(0x88+i);
516 w->writeC(0x0f);
517 w->writeC(0x0f|baseAddr1);
518 w->writeC(0x90+i);
519 w->writeC(0x0f);
520 }
521 // key off + freq reset
522 for (int i=0; i<9; i++) {
523 w->writeC(0x0e|baseAddr1);
524 w->writeC(0xa0+i);
525 w->writeC(0);
526 w->writeC(0x0e|baseAddr1);
527 w->writeC(0xb0+i);
528 w->writeC(0);
529 w->writeC(0x0f|baseAddr1);
530 w->writeC(0xa0+i);
531 w->writeC(0);
532 w->writeC(0x0f|baseAddr1);
533 w->writeC(0xb0+i);
534 w->writeC(0);
535 }
536 // reset 4-op
537 w->writeC(0x0f|baseAddr1);
538 w->writeC(0x04);
539 w->writeC(0x00);
540 break;
541 case DIV_SYSTEM_SCC:
542 case DIV_SYSTEM_SCC_PLUS:
543 w->writeC(0xd2);
544 w->writeC(baseAddr2|3);
545 w->writeC(0);
546 w->writeC(0);
547 break;
548 case DIV_SYSTEM_RF5C68:
549 w->writeC(rf5c68Addr);
550 w->writeC(7);
551 w->writeC(0);
552 w->writeC(rf5c68Addr);
553 w->writeC(8);
554 w->writeC(0xff);
555 break;
556 case DIV_SYSTEM_MSM6258:
557 w->writeC(0xb7); // stop
558 w->writeC(baseAddr2|0);
559 w->writeC(1);
560 break;
561 case DIV_SYSTEM_MSM6295:
562 w->writeC(0xb8); // disable all channels
563 w->writeC(baseAddr2|0);
564 w->writeC(0x78);
565 w->writeC(0xb8); // select rate
566 w->writeC(baseAddr2|12);
567 w->writeC(1);
568 break;
569 case DIV_SYSTEM_VBOY:
570 // isn't it amazing when a chip has a built-in reset command?
571 w->writeC(0xc7);
572 w->writeS_BE(baseAddr2S|(0x580>>2));
573 w->writeC(0xff);
574 break;
575 case DIV_SYSTEM_GA20:
576 for (int i=0; i<4; i++) {
577 w->writeC(0xbf); // mute
578 w->writeC((baseAddr2|5)+(i*8));
579 w->writeC(0);
580 w->writeC(0xbf); // keyoff
581 w->writeC((baseAddr2|6)+(i*8));
582 w->writeC(0);
583 }
584 break;
585 case DIV_SYSTEM_K053260:
586 for (int i=0; i<4; i++) {
587 w->writeC(0xba); // mute
588 w->writeC(baseAddr2|0x2f);
589 w->writeC(0);
590 w->writeC(0xba); // keyoff
591 w->writeC(baseAddr2|0x28);
592 w->writeC(0);
593 }
594 break;
595 case DIV_SYSTEM_C140:
596 for (int i=0; i<24; i++) {
597 w->writeC(0xd4); // mute
598 w->writeS_BE(baseAddr2S|(i<<4)|0);
599 w->writeC(0);
600 w->writeC(0xd4);
601 w->writeS_BE(baseAddr2S|(i<<4)|1);
602 w->writeC(0);
603 w->writeC(0xd4); // keyoff
604 w->writeS_BE(baseAddr2S|(i<<4)|5);
605 w->writeC(0);
606 }
607 break;
608 case DIV_SYSTEM_C219:
609 for (int i=0; i<16; i++) {
610 w->writeC(0xd4); // mute
611 w->writeS_BE(baseAddr2S|(i<<4)|0);
612 w->writeC(0);
613 w->writeC(0xd4);
614 w->writeS_BE(baseAddr2S|(i<<4)|1);
615 w->writeC(0);
616 w->writeC(0xd4); // keyoff
617 w->writeS_BE(baseAddr2S|(i<<4)|5);
618 w->writeC(0);
619 }
620 break;
621 case DIV_SYSTEM_OPL4:
622 case DIV_SYSTEM_OPL4_DRUMS:
623 // disable envelope
624 for (int i=0; i<6; i++) {
625 w->writeC(0xd0);
626 w->writeC(0x00|baseAddr2);
627 w->writeC(0x80+i);
628 w->writeC(0x0f);
629 w->writeC(0xd0);
630 w->writeC(0x00|baseAddr2);
631 w->writeC(0x88+i);
632 w->writeC(0x0f);
633 w->writeC(0xd0);
634 w->writeC(0x00|baseAddr2);
635 w->writeC(0x90+i);
636 w->writeC(0x0f);
637 w->writeC(0xd0);
638 w->writeC(0x01|baseAddr2);
639 w->writeC(0x80+i);
640 w->writeC(0x0f);
641 w->writeC(0xd0);
642 w->writeC(0x01|baseAddr2);
643 w->writeC(0x88+i);
644 w->writeC(0x0f);
645 w->writeC(0xd0);
646 w->writeC(0x01|baseAddr2);
647 w->writeC(0x90+i);
648 w->writeC(0x0f);
649 }
650 for (int i=0; i<24; i++) {
651 w->writeC(0xd0);
652 w->writeC(0x02|baseAddr2);
653 w->writeC(0x80+i);
654 w->writeC(0x00);
655 w->writeC(0xd0);
656 w->writeC(0x02|baseAddr2);
657 w->writeC(0x98+i);
658 w->writeC(0x00);
659 w->writeC(0xd0);
660 w->writeC(0x02|baseAddr2);
661 w->writeC(0xb0+i);
662 w->writeC(0x00);
663 w->writeC(0xd0);
664 w->writeC(0x02|baseAddr2);
665 w->writeC(0xc8+i);
666 w->writeC(0x00);
667 w->writeC(0xd0);
668 w->writeC(0x02|baseAddr2);
669 w->writeC(0xe0+i);
670 w->writeC(0x00);
671 }
672 // key off + freq reset
673 for (int i=0; i<9; i++) {
674 w->writeC(0xd0);
675 w->writeC(0x00|baseAddr2);
676 w->writeC(0xa0+i);
677 w->writeC(0);
678 w->writeC(0xd0);
679 w->writeC(0x00|baseAddr2);
680 w->writeC(0xb0+i);
681 w->writeC(0);
682 w->writeC(0xd0);
683 w->writeC(0x01|baseAddr2);
684 w->writeC(0xa0+i);
685 w->writeC(0);
686 w->writeC(0xd0);
687 w->writeC(0x01|baseAddr2);
688 w->writeC(0xb0+i);
689 w->writeC(0);
690 }
691 for (int i=0; i<24; i++) {
692 w->writeC(0xd0);
693 w->writeC(0x02|baseAddr2);
694 w->writeC(0x20+i);
695 w->writeC(0);
696 w->writeC(0xd0);
697 w->writeC(0x02|baseAddr2);
698 w->writeC(0x38+i);
699 w->writeC(0);
700 w->writeC(0xd0);
701 w->writeC(0x02|baseAddr2);
702 w->writeC(0x68+i);
703 w->writeC(8);
704 }
705 // reset 4-op
706 w->writeC(0xd0);
707 w->writeC(0x01|baseAddr2);
708 w->writeC(0x04);
709 w->writeC(0x00);
710 break;
711 default:
712 break;
713 }
714 }
715 if (write.addr==0xffff0004) { // switch sample bank
716 switch (sys) {
717 case DIV_SYSTEM_NES: {
718 if (dpcm07) {
719 unsigned int bankAddr=bankOffset+(write.val<<14);
720 w->writeC(0x68);
721 w->writeC(0x66);
722 w->writeC(0x07|(isSecond?0x80:0x00));
723 w->writeC(bankAddr&0xff);
724 w->writeC((bankAddr>>8)&0xff);
725 w->writeC((bankAddr>>16)&0xff);
726 w->writeC(0x00);
727 w->writeC(0xc0);
728 w->writeC(0x00);
729 w->writeC(0x00);
730 w->writeC(0x40);
731 w->writeC(0x00);
732 } else {
733 // write the whole damn bank.
734 // this code looks like a mess because it is a hack.
735 // don't blame me if your VGM ends up being over a gigabyte!
736 size_t howMuchWillBeWritten=writeNES[isSecond?1:0]->getSampleMemUsage();
737 // refuse to switch if we're going out of bounds
738 if ((write.val<<14)>=howMuchWillBeWritten) break;
739 howMuchWillBeWritten-=(write.val<<14);
740 if (howMuchWillBeWritten>16384) howMuchWillBeWritten=16384;
741 w->writeC(0x67);
742 w->writeC(0x66);
743 w->writeC(0xc2);
744 w->writeI((isSecond?0x80000000:0)|(howMuchWillBeWritten+2));
745 // data
746 w->writeS(0xc000);
747 w->write(&(((unsigned char*)writeNES[isSecond?1:0]->getSampleMem())[write.val<<14]),howMuchWillBeWritten);
748 }
749 break;
750 }
751 default:
752 break;
753 }
754 }
755 if (write.addr>=0xffff0000) { // Furnace special command
756 if (!directStream) {
757 unsigned char streamID=streamOff+((write.addr&0xff00)>>8);
758 logD("writing stream command %x:%x with stream ID %d",write.addr,write.val,streamID);
759 switch (write.addr&0xff) {
760 case 0: // play sample
761 sampleStoppable[streamID]=true;
762 if (write.val<(unsigned int)song.sampleLen) {
763 if (playingSample[streamID]!=(int)write.val) {
764 pendingFreq[streamID]=write.val;
765 } else {
766 DivSample* sample=song.sample[write.val];
767 int pos=sampleOff8[write.val&0x7fff]+setPos[streamID];
768 int len=(int)sampleLen8[write.val&0x7fff]-setPos[streamID];
769
770 if (len<0) len=0;
771
772 if (setPos[streamID]!=0) {
773 if (len<=0) {
774 w->writeC(0x94);
775 w->writeC(streamID);
776 } else {
777 w->writeC(0x93);
778 w->writeC(streamID);
779 w->writeI(pos);
780 w->writeC(1|((sample->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT)==0 && sample->isLoopable())?0x80:0)|(sampleDir[streamID]?0x10:0)); // flags
781 w->writeI(len);
782 }
783 } else {
784 w->writeC(0x95);
785 w->writeC(streamID);
786 w->writeS(write.val); // sample number
787 w->writeC((sample->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT)==0 && sample->isLoopable())|(sampleDir[streamID]?0x10:0)); // flags
788 }
789
790 if (sample->isLoopable() && !sampleDir[streamID]) {
791 loopTimer[streamID]=len;
792 loopSample[streamID]=write.val;
793 }
794 playingSample[streamID]=write.val;
795 setPos[streamID]=0;
796 }
797 }
798 break;
799 case 1: { // set sample freq
800 sampleStoppable[streamID]=true;
801 int realFreq=(write.val*44100)/rateCorrection;
802 if (realFreq<0) realFreq=0;
803 if (realFreq>44100) realFreq=44100;
804 w->writeC(0x92);
805 w->writeC(streamID);
806 w->writeI(realFreq);
807 loopFreq[streamID]=realFreq;
808 if (pendingFreq[streamID]!=-1) {
809 DivSample* sample=song.sample[pendingFreq[streamID]];
810 int pos=sampleOff8[pendingFreq[streamID]&0x7fff]+setPos[streamID];
811 int len=(int)sampleLen8[pendingFreq[streamID]&0x7fff]-setPos[streamID];
812
813 if (len<0) len=0;
814
815 if (setPos[streamID]!=0) {
816 if (len<=0) {
817 w->writeC(0x94);
818 w->writeC(streamID);
819 } else {
820 w->writeC(0x93);
821 w->writeC(streamID);
822 w->writeI(pos);
823 w->writeC(1|((sample->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT)==0 && sample->isLoopable())?0x80:0)|(sampleDir[streamID]?0x10:0)); // flags
824 w->writeI(len);
825 }
826 } else {
827 w->writeC(0x95);
828 w->writeC(streamID);
829 w->writeS(pendingFreq[streamID]); // sample number
830 w->writeC((sample->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT)==0 && sample->isLoopable())|(sampleDir[streamID]?0x10:0)); // flags
831 }
832
833 if (sample->isLoopable() && !sampleDir[streamID]) {
834 loopTimer[streamID]=len;
835 loopSample[streamID]=pendingFreq[streamID];
836 }
837 playingSample[streamID]=pendingFreq[streamID];
838 pendingFreq[streamID]=-1;
839 setPos[streamID]=0;
840 }
841 break;
842 }
843 case 2: // stop sample
844 if (sampleStoppable[streamID]) {
845 w->writeC(0x94);
846 w->writeC(streamID);
847 loopSample[streamID]=-1;
848 playingSample[streamID]=-1;
849 pendingFreq[streamID]=-1;
850 sampleStoppable[streamID]=false;
851 }
852 break;
853 case 3: // set sample direction
854 sampleDir[streamID]=write.val;
855 break;
856 case 5: // set sample pos
857 setPos[streamID]=write.val;
858
859 if (playingSample[streamID]!=-1 && pendingFreq[streamID]==-1) {
860 // play the sample again
861 DivSample* sample=song.sample[playingSample[streamID]];
862 int pos=sampleOff8[playingSample[streamID]&0x7fff]+setPos[streamID];
863 int len=(int)sampleLen8[playingSample[streamID]&0x7fff]-setPos[streamID];
864
865 if (len<0) len=0;
866
867 if (setPos[streamID]!=0) {
868 if (len<=0) {
869 w->writeC(0x94);
870 w->writeC(streamID);
871 } else {
872 w->writeC(0x93);
873 w->writeC(streamID);
874 w->writeI(pos);
875 w->writeC(1|((sample->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT)==0 && sample->isLoopable())?0x80:0)|(sampleDir[streamID]?0x10:0)); // flags
876 w->writeI(len);
877 }
878 } else {
879 w->writeC(0x95);
880 w->writeC(streamID);
881 w->writeS(playingSample[streamID]); // sample number
882 w->writeC((sample->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT)==0 && sample->isLoopable())|(sampleDir[streamID]?0x10:0)); // flags
883 }
884
885 if (sample->isLoopable() && !sampleDir[streamID]) {
886 loopTimer[streamID]=len;
887 loopSample[streamID]=playingSample[streamID];
888 }
889 }
890 break;
891 }
892 }
893 return;
894 }
895 switch (sys) {
896 case DIV_SYSTEM_YM2612:
897 case DIV_SYSTEM_YM2612_EXT:
898 case DIV_SYSTEM_YM2612_DUALPCM:
899 case DIV_SYSTEM_YM2612_DUALPCM_EXT:
900 case DIV_SYSTEM_YM2612_CSM:
901 switch (write.addr>>8) {
902 case 0: // port 0
903 w->writeC(2|baseAddr1);
904 w->writeC(write.addr&0xff);
905 w->writeC(write.val);
906 break;
907 case 1: // port 1
908 w->writeC(3|baseAddr1);
909 w->writeC(write.addr&0xff);
910 w->writeC(write.val);
911 break;
912 case 2: // PSG
913 w->writeC(smsAddr);
914 w->writeC(write.val);
915 break;
916 }
917 break;
918 case DIV_SYSTEM_SMS:
919 if (write.addr==1) {
920 w->writeC(ggAddr);
921 } else {
922 w->writeC(smsAddr);
923 }
924 w->writeC(write.val);
925 break;
926 case DIV_SYSTEM_T6W28:
927 if (write.addr) {
928 w->writeC(0x30);
929 } else {
930 w->writeC(0x50);
931 }
932 w->writeC(write.val);
933 break;
934 case DIV_SYSTEM_GB:
935 w->writeC(0xb3);
936 w->writeC(baseAddr2|((write.addr-16)&0xff));
937 w->writeC(write.val);
938 break;
939 case DIV_SYSTEM_PCE:
940 w->writeC(0xb9);
941 w->writeC(baseAddr2|(write.addr&0xff));
942 w->writeC(write.val);
943 break;
944 case DIV_SYSTEM_NES:
945 w->writeC(0xb4);
946 w->writeC(baseAddr2|(write.addr&0xff));
947 w->writeC(write.val);
948 break;
949 case DIV_SYSTEM_FDS: // yeah
950 w->writeC(0xb4);
951 if ((write.addr&0xff)==0x23) {
952 w->writeC(baseAddr2|0x3f);
953 } else if ((write.addr&0xff)>=0x80) {
954 w->writeC(baseAddr2|(0x20+(write.addr&0x7f)));
955 } else {
956 w->writeC(baseAddr2|(write.addr&0xff));
957 }
958
959 w->writeC(write.val);
960 break;
961 case DIV_SYSTEM_YM2151:
962 w->writeC(4|baseAddr1);
963 w->writeC(write.addr&0xff);
964 w->writeC(write.val);
965 break;
966 case DIV_SYSTEM_SEGAPCM:
967 case DIV_SYSTEM_SEGAPCM_COMPAT:
968 w->writeC(0xc0);
969 w->writeS(baseAddr2S|(write.addr&0xffff));
970 w->writeC(write.val);
971 break;
972 case DIV_SYSTEM_X1_010:
973 w->writeC(0xc8);
974 w->writeS_BE(baseAddr2S|(write.addr&0x1fff));
975 w->writeC(write.val);
976 break;
977 case DIV_SYSTEM_YM2610:
978 case DIV_SYSTEM_YM2610_FULL:
979 case DIV_SYSTEM_YM2610B:
980 case DIV_SYSTEM_YM2610_EXT:
981 case DIV_SYSTEM_YM2610_FULL_EXT:
982 case DIV_SYSTEM_YM2610B_EXT:
983 case DIV_SYSTEM_YM2610_CSM:
984 case DIV_SYSTEM_YM2610B_CSM:
985 switch (write.addr>>8) {
986 case 0: // port 0
987 w->writeC(8|baseAddr1);
988 w->writeC(write.addr&0xff);
989 w->writeC(write.val);
990 break;
991 case 1: // port 1
992 w->writeC(9|baseAddr1);
993 w->writeC(write.addr&0xff);
994 w->writeC(write.val);
995 break;
996 }
997 break;
998 case DIV_SYSTEM_YM2203:
999 case DIV_SYSTEM_YM2203_EXT:
1000 case DIV_SYSTEM_YM2203_CSM:
1001 w->writeC(5|baseAddr1);
1002 w->writeC(write.addr&0xff);
1003 w->writeC(write.val);
1004 break;
1005 case DIV_SYSTEM_YM2608:
1006 case DIV_SYSTEM_YM2608_EXT:
1007 case DIV_SYSTEM_YM2608_CSM:
1008 switch (write.addr>>8) {
1009 case 0: // port 0
1010 w->writeC(6|baseAddr1);
1011 w->writeC(write.addr&0xff);
1012 w->writeC(write.val);
1013 break;
1014 case 1: // port 1
1015 w->writeC(7|baseAddr1);
1016 w->writeC(write.addr&0xff);
1017 w->writeC(write.val);
1018 break;
1019 }
1020 break;
1021 case DIV_SYSTEM_OPLL:
1022 case DIV_SYSTEM_OPLL_DRUMS:
1023 case DIV_SYSTEM_VRC7:
1024 w->writeC(1|baseAddr1);
1025 w->writeC(write.addr&0xff);
1026 w->writeC(write.val);
1027 break;
1028 case DIV_SYSTEM_AY8910:
1029 case DIV_SYSTEM_AY8930:
1030 w->writeC(0xa0);
1031 w->writeC(baseAddr2|(write.addr&0xff));
1032 w->writeC(write.val);
1033 break;
1034 case DIV_SYSTEM_SAA1099:
1035 w->writeC(0xbd);
1036 w->writeC(baseAddr2|(write.addr&0xff));
1037 w->writeC(write.val);
1038 break;
1039 case DIV_SYSTEM_POKEY:
1040 w->writeC(0xbb);
1041 w->writeC(baseAddr2|(write.addr&0x0f));
1042 w->writeC(write.val&0xff);
1043 break;
1044 case DIV_SYSTEM_LYNX:
1045 w->writeC(0x40);
1046 w->writeC(write.addr&0xff);
1047 w->writeC(write.val&0xff);
1048 break;
1049 case DIV_SYSTEM_QSOUND:
1050 w->writeC(0xc4);
1051 w->writeC((write.val>>8)&0xff);
1052 w->writeC(write.val&0xff);
1053 w->writeC(write.addr&0xff);
1054 break;
1055 case DIV_SYSTEM_SWAN:
1056 if ((write.addr&0x7f)<0x40) {
1057 w->writeC(0xbc);
1058 w->writeC(baseAddr2|(write.addr&0x3f));
1059 w->writeC(write.val&0xff);
1060 } else {
1061 // (Wave) RAM write
1062 w->writeC(0xc6);
1063 w->writeS_BE(baseAddr2S|(write.addr&0x3f));
1064 w->writeC(write.val&0xff);
1065 }
1066 break;
1067 case DIV_SYSTEM_ES5506:
1068 w->writeC(0xbe);
1069 w->writeC(baseAddr2|(write.addr&0x7f));
1070 w->writeC(write.val&0xff);
1071 break;
1072 case DIV_SYSTEM_VBOY:
1073 w->writeC(0xc7);
1074 w->writeS_BE(baseAddr2S|(write.addr>>2));
1075 w->writeC(write.val&0xff);
1076 break;
1077 case DIV_SYSTEM_OPL:
1078 case DIV_SYSTEM_OPL_DRUMS:
1079 w->writeC(0x0b|baseAddr1);
1080 w->writeC(write.addr&0xff);
1081 w->writeC(write.val);
1082 break;
1083 case DIV_SYSTEM_Y8950:
1084 case DIV_SYSTEM_Y8950_DRUMS:
1085 w->writeC(0x0c|baseAddr1);
1086 w->writeC(write.addr&0xff);
1087 w->writeC(write.val);
1088 break;
1089 case DIV_SYSTEM_OPL2:
1090 case DIV_SYSTEM_OPL2_DRUMS:
1091 w->writeC(0x0a|baseAddr1);
1092 w->writeC(write.addr&0xff);
1093 w->writeC(write.val);
1094 break;
1095 case DIV_SYSTEM_OPL3:
1096 case DIV_SYSTEM_OPL3_DRUMS:
1097 switch (write.addr>>8) {
1098 case 0: // port 0
1099 w->writeC(0x0e|baseAddr1);
1100 w->writeC(write.addr&0xff);
1101 w->writeC(write.val);
1102 break;
1103 case 1: // port 1
1104 w->writeC(0x0f|baseAddr1);
1105 w->writeC(write.addr&0xff);
1106 w->writeC(write.val);
1107 break;
1108 }
1109 break;
1110 case DIV_SYSTEM_SCC:
1111 if (write.addr<0x80) {
1112 w->writeC(0xd2);
1113 w->writeC(baseAddr2|0);
1114 w->writeC(write.addr&0x7f);
1115 w->writeC(write.val&0xff);
1116 } else if (write.addr<0x8a) {
1117 w->writeC(0xd2);
1118 w->writeC(baseAddr2|1);
1119 w->writeC((write.addr-0x80)&0x7f);
1120 w->writeC(write.val&0xff);
1121 } else if (write.addr<0x8f) {
1122 w->writeC(0xd2);
1123 w->writeC(baseAddr2|2);
1124 w->writeC((write.addr-0x8a)&0x7f);
1125 w->writeC(write.val&0xff);
1126 } else if (write.addr<0x90) {
1127 w->writeC(0xd2);
1128 w->writeC(baseAddr2|3);
1129 w->writeC((write.addr-0x8f)&0x7f);
1130 w->writeC(write.val&0xff);
1131 } else if (write.addr>=0xe0) {
1132 w->writeC(0xd2);
1133 w->writeC(baseAddr2|5);
1134 w->writeC((write.addr-0xe0)&0x7f);
1135 w->writeC(write.val&0xff);
1136 } else {
1137 logW("SCC: writing to unmapped address %.2x!",write.addr);
1138 }
1139 break;
1140 case DIV_SYSTEM_SCC_PLUS:
1141 if (write.addr<0x80) {
1142 w->writeC(0xd2);
1143 w->writeC(baseAddr2|0);
1144 w->writeC(write.addr&0x7f);
1145 w->writeC(write.val&0xff);
1146 } else if (write.addr<0xa0) {
1147 w->writeC(0xd2);
1148 w->writeC(baseAddr2|4);
1149 w->writeC(write.addr);
1150 w->writeC(write.val&0xff);
1151 } else if (write.addr<0xaa) {
1152 w->writeC(0xd2);
1153 w->writeC(baseAddr2|1);
1154 w->writeC((write.addr-0xa0)&0x7f);
1155 w->writeC(write.val&0xff);
1156 } else if (write.addr<0xaf) {
1157 w->writeC(0xd2);
1158 w->writeC(baseAddr2|2);
1159 w->writeC((write.addr-0xaa)&0x7f);
1160 w->writeC(write.val&0xff);
1161 } else if (write.addr<0xb0) {
1162 w->writeC(0xd2);
1163 w->writeC(baseAddr2|3);
1164 w->writeC((write.addr-0xaf)&0x7f);
1165 w->writeC(write.val&0xff);
1166 } else if (write.addr>=0xe0) {
1167 w->writeC(0xd2);
1168 w->writeC(baseAddr2|5);
1169 w->writeC((write.addr-0xe0)&0x7f);
1170 w->writeC(write.val&0xff);
1171 } else {
1172 logW("SCC+: writing to unmapped address %.2x!",write.addr);
1173 }
1174 break;
1175 case DIV_SYSTEM_YMZ280B:
1176 w->writeC(0x0d|baseAddr1);
1177 w->writeC(write.addr&0xff);
1178 w->writeC(write.val&0xff);
1179 break;
1180 case DIV_SYSTEM_RF5C68:
1181 w->writeC(rf5c68Addr);
1182 w->writeC(write.addr&0xff);
1183 w->writeC(write.val);
1184 break;
1185 case DIV_SYSTEM_MSM6258:
1186 w->writeC(0xb7);
1187 w->writeC(baseAddr2|(write.addr&0x7f));
1188 w->writeC(write.val);
1189 logV("MSM write to %.2x %.2x",write.addr,write.val);
1190 break;
1191 case DIV_SYSTEM_MSM6295:
1192 w->writeC(0xb8);
1193 w->writeC(baseAddr2|(write.addr&0x7f));
1194 w->writeC(write.val);
1195 break;
1196 case DIV_SYSTEM_GA20:
1197 w->writeC(0xbf);
1198 w->writeC(baseAddr2|(write.addr&0x7f));
1199 w->writeC(write.val);
1200 break;
1201 case DIV_SYSTEM_K053260:
1202 w->writeC(0xba);
1203 w->writeC(baseAddr2|(write.addr&0x3f));
1204 w->writeC(write.val&0xff);
1205 break;
1206 case DIV_SYSTEM_C140:
1207 case DIV_SYSTEM_C219:
1208 w->writeC(0xd4);
1209 w->writeS_BE(baseAddr2S|(write.addr&0x1ff));
1210 w->writeC(write.val&0xff);
1211 break;
1212 case DIV_SYSTEM_OPL4:
1213 case DIV_SYSTEM_OPL4_DRUMS:
1214 w->writeC(0xd0);
1215 w->writeC(((write.addr>>8)&0x7f)|baseAddr2);
1216 w->writeC(write.addr&0xff);
1217 w->writeC(write.val);
1218 break;
1219 default:
1220 logW("write not handled!");
1221 break;
1222 }
1223 }
1224
1225 #define CHIP_VOL(_id,_mult) { \
1226 double _vol=fabs((float)song.systemVol[i])*256.0*_mult; \
1227 if (_vol<0.0) _vol=0.0; \
1228 if (_vol>32767.0) _vol=32767.0; \
1229 chipVolSum+=(unsigned int)(_vol/_mult); \
1230 chipAccounting++; \
1231 chipVol.push_back((_id)|(0x80000000)|(((unsigned int)_vol)<<16)); \
1232 }
1233
1234 #define CHIP_VOL_SECOND(_id,_mult) { \
1235 double _vol=fabs((float)song.systemVol[i])*256.0*_mult; \
1236 if (_vol<0.0) _vol=0.0; \
1237 if (_vol>32767.0) _vol=32767.0; \
1238 chipVolSum+=(unsigned int)(_vol/_mult); \
1239 chipAccounting++; \
1240 chipVol.push_back((_id)|(0x80000100)|(((unsigned int)_vol)<<16)); \
1241 }
1242
1243 SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop, int version, bool patternHints, bool directStream, int trailingTicks, bool dpcm07, int correctedRate) {
1244 if (version<0x150) {
1245 lastError="VGM version is too low";
1246 return NULL;
1247 }
1248 stop();
1249 repeatPattern=false;
1250 setOrder(0);
1251 BUSY_BEGIN_SOFT;
1252 double origRate=got.rate;
1253 got.rate=correctedRate;
1254 // determine loop point
1255 int loopOrder=0;
1256 int loopRow=0;
1257 int loopEnd=0;
1258 walkSong(loopOrder,loopRow,loopEnd);
1259 logI("loop point: %d %d",loopOrder,loopRow);
1260 warnings="";
1261
1262 curOrder=0;
1263 freelance=false;
1264 playing=false;
1265 extValuePresent=false;
1266 remainingLoops=-1;
1267
1268 // play the song ourselves
1269 bool done=false;
1270 int writeCount=0;
1271
1272 int gd3Off=0;
1273
1274 int hasSN=0;
1275 int snNoiseConfig=9;
1276 int snNoiseSize=16;
1277 int snFlags=0;
1278 int hasOPLL=0;
1279 int hasOPN2=0;
1280 int hasOPM=0;
1281 int hasSegaPCM=0;
1282 int segaPCMOffset=0xf8000d;
1283 int hasRFC=0;
1284 int hasOPN=0;
1285 int hasOPNA=0;
1286 int hasOPNB=0;
1287 int hasOPL2=0;
1288 int hasOPL=0;
1289 int hasY8950=0;
1290 int hasOPL3=0;
1291 int hasOPL4=0;
1292 int hasOPX=0;
1293 int hasZ280=0;
1294 int hasRFC1=0;
1295 int hasPWM=0;
1296 int hasAY=0;
1297 int ayConfig=0;
1298 int ayFlags=0;
1299 int hasGB=0;
1300 int hasNES=0;
1301 int hasMultiPCM=0;
1302 int hasuPD7759=0;
1303 int hasOKIM6258=0;
1304 int hasK054539=0;
1305 int hasOKIM6295=0;
1306 int hasK051649=0;
1307 int hasPCE=0;
1308 int hasC140=0;
1309 int c140Type=0;
1310 int hasK053260=0;
1311 int hasPOKEY=0;
1312 int hasQSound=0;
1313 int hasSCSP=0;
1314 int hasSwan=0;
1315 int hasVSU=0;
1316 int hasSAA=0;
1317 int hasES5503=0;
1318 int hasES5505=0;
1319 int hasX1=0;
1320 int hasC352=0;
1321 int hasGA20=0;
1322 int hasLynx=0;
1323
1324 int howManyChips=0;
1325 int chipVolSum=0;
1326 int chipAccounting=0;
1327
1328 int loopPos=-1;
1329 int loopTickSong=-1;
1330 int songTick=0;
1331
1332 unsigned int* sampleOff8=new unsigned int[32768];
1333 unsigned int* sampleLen8=new unsigned int[32768];
1334 unsigned int* sampleOffSegaPCM=new unsigned int[32768];
1335
1336 SafeWriter* w=new SafeWriter;
1337 w->init();
1338
1339 // write header
1340 w->write("Vgm ",4);
1341 w->writeI(0); // will be written later
1342 w->writeI(version);
1343
1344 bool willExport[DIV_MAX_CHIPS];
1345 bool isSecond[DIV_MAX_CHIPS];
1346 int streamIDs[DIV_MAX_CHIPS];
1347 size_t bankOffset[DIV_MAX_CHIPS];
1348 double loopTimer[DIV_MAX_CHANS];
1349 double loopFreq[DIV_MAX_CHANS];
1350 int loopSample[DIV_MAX_CHANS];
1351 bool sampleDir[DIV_MAX_CHANS];
1352 int pendingFreq[DIV_MAX_CHANS];
1353 int playingSample[DIV_MAX_CHANS];
1354 bool sampleStoppable[DIV_MAX_CHANS];
1355 int setPos[DIV_MAX_CHANS];
1356 std::vector<unsigned int> chipVol;
1357 std::vector<DivDelayedWrite> delayedWrites[DIV_MAX_CHIPS];
1358 std::vector<std::pair<int,DivDelayedWrite>> sortedWrites;
1359 std::vector<size_t> tickPos;
1360 std::vector<int> tickSample;
1361
1362 bool trailing=false;
1363 bool beenOneLoopAlready=false;
1364 bool mayWriteRate=(fmod(curSubSong->hz,1.0)<0.00001 || fmod(curSubSong->hz,1.0)>0.99999);
1365 int countDown=MAX(0,trailingTicks)+1;
1366
1367 memset(bankOffset,0,DIV_MAX_CHIPS*sizeof(size_t));
1368
1369 for (int i=0; i<DIV_MAX_CHANS; i++) {
1370 loopTimer[i]=0;
1371 loopFreq[i]=0;
1372 loopSample[i]=-1;
1373 pendingFreq[i]=-1;
1374 playingSample[i]=-1;
1375 setPos[i]=0;
1376 sampleDir[i]=false;
1377 sampleStoppable[i]=true;
1378 }
1379
1380 bool writeDACSamples=false;
1381 bool writeNESSamples=false;
1382 bool writePCESamples=false;
1383 bool writeVOXSamples=false;
1384 bool writeLynxSamples=false;
1385 DivDispatch* writeADPCM_OPNA[2]={NULL,NULL};
1386 DivDispatch* writeADPCM_OPNB[2]={NULL,NULL};
1387 DivDispatch* writeADPCM_Y8950[2]={NULL,NULL};
1388 DivDispatch* writeSegaPCM[2]={NULL,NULL};
1389 DivDispatch* writeX1010[2]={NULL,NULL};
1390 DivDispatch* writeQSound[2]={NULL,NULL};
1391 DivDispatch* writeES5506[2]={NULL,NULL};
1392 DivDispatch* writeZ280[2]={NULL,NULL};
1393 DivDispatch* writeRF5C68[2]={NULL,NULL};
1394 DivDispatch* writeMSM6295[2]={NULL,NULL};
1395 DivDispatch* writeGA20[2]={NULL,NULL};
1396 DivDispatch* writeK053260[2]={NULL,NULL};
1397 DivDispatch* writeC140[2]={NULL,NULL};
1398 DivDispatch* writeC219[2]={NULL,NULL};
1399 DivDispatch* writeNES[2]={NULL,NULL};
1400 DivDispatch* writePCM_OPL4[2]={NULL,NULL};
1401
1402 int writeNESIndex[2]={0,0};
1403
1404 size_t bankOffsetNESCurrent=0;
1405 size_t bankOffsetNES[2]={0,0};
1406
1407 for (int i=0; i<song.systemLen; i++) {
1408 willExport[i]=false;
1409 isSecond[i]=false;
1410 streamIDs[i]=0;
1411 if (sysToExport!=NULL) {
1412 if (!sysToExport[i]) continue;
1413 }
1414 if (minVGMVersion(song.system[i])>version) continue;
1415 switch (song.system[i]) {
1416 case DIV_SYSTEM_SMS:
1417 if (!hasSN) {
1418 hasSN=disCont[i].dispatch->chipClock;
1419 CHIP_VOL(0,4.0);
1420 willExport[i]=true;
1421 switch (song.systemFlags[i].getInt("chipType",0)) {
1422 case 1: // real SN
1423 snNoiseConfig=3;
1424 snNoiseSize=15;
1425 break;
1426 case 2: // real SN atari bass (seemingly unsupported)
1427 snNoiseConfig=3;
1428 snNoiseSize=15;
1429 break;
1430 default: // Sega VDP
1431 snNoiseConfig=9;
1432 snNoiseSize=16;
1433 break;
1434 }
1435 } else if (!(hasSN&0x40000000)) {
1436 isSecond[i]=true;
1437 willExport[i]=true;
1438 CHIP_VOL_SECOND(0,4.0);
1439 hasSN|=0x40000000;
1440 howManyChips++;
1441 }
1442 break;
1443 case DIV_SYSTEM_GB:
1444 if (!hasGB) {
1445 hasGB=disCont[i].dispatch->chipClock;
1446 CHIP_VOL(19,0.75);
1447 willExport[i]=true;
1448 } else if (!(hasGB&0x40000000)) {
1449 isSecond[i]=true;
1450 CHIP_VOL_SECOND(19,0.75);
1451 willExport[i]=true;
1452 hasGB|=0x40000000;
1453 howManyChips++;
1454 }
1455 break;
1456 case DIV_SYSTEM_PCE:
1457 if (!hasPCE) {
1458 // the clock is halved in VGM...
1459 hasPCE=disCont[i].dispatch->chipClock/2;
1460 CHIP_VOL(27,0.98);
1461 willExport[i]=true;
1462 writePCESamples=true;
1463 } else if (!(hasPCE&0x40000000)) {
1464 isSecond[i]=true;
1465 CHIP_VOL(27,0.98);
1466 willExport[i]=true;
1467 hasPCE|=0x40000000;
1468 howManyChips++;
1469 }
1470 break;
1471 case DIV_SYSTEM_NES:
1472 if (!hasNES) {
1473 hasNES=disCont[i].dispatch->chipClock;
1474 CHIP_VOL(20,1.7);
1475 willExport[i]=true;
1476 writeNESSamples=true;
1477 writeNES[0]=disCont[i].dispatch;
1478 writeNESIndex[0]=i;
1479 } else if (!(hasNES&0x40000000)) {
1480 isSecond[i]=true;
1481 CHIP_VOL_SECOND(20,1.7);
1482 willExport[i]=true;
1483 hasNES|=0x40000000;
1484 writeNES[1]=disCont[i].dispatch;
1485 writeNESIndex[1]=i;
1486 howManyChips++;
1487 }
1488 break;
1489 case DIV_SYSTEM_SEGAPCM:
1490 case DIV_SYSTEM_SEGAPCM_COMPAT:
1491 if (!hasSegaPCM) {
1492 hasSegaPCM=4000000;
1493 CHIP_VOL(4,0.67);
1494 willExport[i]=true;
1495 writeSegaPCM[0]=disCont[i].dispatch;
1496 } else if (!(hasSegaPCM&0x40000000)) {
1497 isSecond[i]=true;
1498 CHIP_VOL_SECOND(4,0.67);
1499 willExport[i]=true;
1500 writeSegaPCM[1]=disCont[i].dispatch;
1501 hasSegaPCM|=0x40000000;
1502 howManyChips++;
1503 }
1504 break;
1505 case DIV_SYSTEM_X1_010:
1506 if (!hasX1) {
1507 hasX1=disCont[i].dispatch->chipClock;
1508 CHIP_VOL(38,2.0);
1509 willExport[i]=true;
1510 writeX1010[0]=disCont[i].dispatch;
1511 } else if (!(hasX1&0x40000000)) {
1512 isSecond[i]=true;
1513 CHIP_VOL_SECOND(38,2.0);
1514 willExport[i]=true;
1515 writeX1010[1]=disCont[i].dispatch;
1516 hasX1|=0x40000000;
1517 howManyChips++;
1518 }
1519 break;
1520 case DIV_SYSTEM_YM2610:
1521 case DIV_SYSTEM_YM2610_FULL:
1522 case DIV_SYSTEM_YM2610B:
1523 case DIV_SYSTEM_YM2610_EXT:
1524 case DIV_SYSTEM_YM2610_FULL_EXT:
1525 case DIV_SYSTEM_YM2610B_EXT:
1526 case DIV_SYSTEM_YM2610_CSM:
1527 case DIV_SYSTEM_YM2610B_CSM:
1528 if (!hasOPNB) {
1529 hasOPNB=disCont[i].dispatch->chipClock;
1530 CHIP_VOL(8,1.0);
1531 CHIP_VOL(0x88,1.25);
1532 willExport[i]=true;
1533 writeADPCM_OPNB[0]=disCont[i].dispatch;
1534 } else if (!(hasOPNB&0x40000000)) {
1535 isSecond[i]=true;
1536 CHIP_VOL_SECOND(8,1.0);
1537 CHIP_VOL_SECOND(0x88,1.25);
1538 willExport[i]=true;
1539 writeADPCM_OPNB[1]=disCont[i].dispatch;
1540 hasOPNB|=0x40000000;
1541 howManyChips++;
1542 }
1543 if (((song.system[i]==DIV_SYSTEM_YM2610B) || (song.system[i]==DIV_SYSTEM_YM2610B_EXT) || (song.system[i]==DIV_SYSTEM_YM2610B_CSM)) && (!(hasOPNB&0x80000000))) { // YM2610B flag
1544 hasOPNB|=0x80000000;
1545 }
1546 break;
1547 case DIV_SYSTEM_AY8910:
1548 case DIV_SYSTEM_AY8930: {
1549 if (!hasAY) {
1550 bool hasClockDivider=false; // Configurable clock divider
1551 bool hasStereo=true; // Stereo
1552 hasAY=disCont[i].dispatch->chipClock;
1553 ayFlags=1;
1554 if (song.system[i]==DIV_SYSTEM_AY8930) { // AY8930
1555 ayConfig=0x03;
1556 hasClockDivider=true;
1557 } else {
1558 switch (song.systemFlags[i].getInt("chipType",0)) {
1559 case 1: // YM2149
1560 ayConfig=0x10;
1561 hasClockDivider=true;
1562 break;
1563 case 2: // Sunsoft 5B
1564 ayConfig=0x10;
1565 ayFlags|=0x12; // Clock internally divided, Single sound output
1566 hasStereo=false; // due to above, can't be per-channel stereo configurable
1567 break;
1568 case 3: // AY8914
1569 ayConfig=0x04;
1570 break;
1571 default: // AY8910
1572 ayConfig=0x00;
1573 break;
1574 }
1575 }
1576 if (hasClockDivider && song.systemFlags[i].getBool("halfClock",false)) {
1577 ayFlags|=0x10;
1578 }
1579 if (hasStereo && song.systemFlags[i].getBool("stereo",false)) {
1580 ayFlags|=0x80;
1581 }
1582 CHIP_VOL(18,1.0);
1583 willExport[i]=true;
1584 } else if (!(hasAY&0x40000000)) {
1585 isSecond[i]=true;
1586 CHIP_VOL_SECOND(18,1.0);
1587 willExport[i]=true;
1588 hasAY|=0x40000000;
1589 howManyChips++;
1590 }
1591 break;
1592 }
1593 case DIV_SYSTEM_SAA1099:
1594 if (!hasSAA) {
1595 hasSAA=disCont[i].dispatch->chipClock;
1596 CHIP_VOL(35,1.0);
1597 willExport[i]=true;
1598 } else if (!(hasSAA&0x40000000)) {
1599 isSecond[i]=true;
1600 CHIP_VOL_SECOND(35,1.0);
1601 willExport[i]=true;
1602 hasSAA|=0x40000000;
1603 howManyChips++;
1604 }
1605 break;
1606 case DIV_SYSTEM_YM2612:
1607 case DIV_SYSTEM_YM2612_EXT:
1608 case DIV_SYSTEM_YM2612_DUALPCM:
1609 case DIV_SYSTEM_YM2612_DUALPCM_EXT:
1610 case DIV_SYSTEM_YM2612_CSM:
1611 if (!hasOPN2) {
1612 hasOPN2=disCont[i].dispatch->chipClock;
1613 CHIP_VOL(2,1.6);
1614 willExport[i]=true;
1615 writeDACSamples=true;
1616 } else if (!(hasOPN2&0x40000000)) {
1617 isSecond[i]=true;
1618 CHIP_VOL_SECOND(2,1.6);
1619 willExport[i]=true;
1620 hasOPN2|=0x40000000;
1621 howManyChips++;
1622 }
1623 break;
1624 case DIV_SYSTEM_YM2151:
1625 if (!hasOPM) {
1626 hasOPM=disCont[i].dispatch->chipClock;
1627 CHIP_VOL(3,1.0);
1628 willExport[i]=true;
1629 } else if (!(hasOPM&0x40000000)) {
1630 isSecond[i]=true;
1631 CHIP_VOL_SECOND(3,1.0);
1632 willExport[i]=true;
1633 hasOPM|=0x40000000;
1634 howManyChips++;
1635 }
1636 break;
1637 case DIV_SYSTEM_YM2203:
1638 case DIV_SYSTEM_YM2203_EXT:
1639 case DIV_SYSTEM_YM2203_CSM:
1640 if (!hasOPN) {
1641 hasOPN=disCont[i].dispatch->chipClock;
1642 willExport[i]=true;
1643 CHIP_VOL(6,1.0);
1644 CHIP_VOL(0x86,1.7);
1645 } else if (!(hasOPN&0x40000000)) {
1646 isSecond[i]=true;
1647 willExport[i]=true;
1648 CHIP_VOL_SECOND(6,1.0);
1649 CHIP_VOL_SECOND(0x86,1.7);
1650 hasOPN|=0x40000000;
1651 howManyChips++;
1652 }
1653 break;
1654 case DIV_SYSTEM_YM2608:
1655 case DIV_SYSTEM_YM2608_EXT:
1656 case DIV_SYSTEM_YM2608_CSM:
1657 if (!hasOPNA) {
1658 hasOPNA=disCont[i].dispatch->chipClock;
1659 CHIP_VOL(7,1.0);
1660 CHIP_VOL(0x87,1.3);
1661 willExport[i]=true;
1662 writeADPCM_OPNA[0]=disCont[i].dispatch;
1663 } else if (!(hasOPNA&0x40000000)) {
1664 isSecond[i]=true;
1665 CHIP_VOL_SECOND(7,1.0);
1666 CHIP_VOL_SECOND(0x87,1.3);
1667 willExport[i]=true;
1668 writeADPCM_OPNA[1]=disCont[i].dispatch;
1669 hasOPNA|=0x40000000;
1670 howManyChips++;
1671 }
1672 break;
1673 case DIV_SYSTEM_OPLL:
1674 case DIV_SYSTEM_OPLL_DRUMS:
1675 case DIV_SYSTEM_VRC7:
1676 if (!hasOPLL) {
1677 hasOPLL=disCont[i].dispatch->chipClock;
1678 CHIP_VOL(1,3.2);
1679 willExport[i]=true;
1680 } else if (!(hasOPLL&0x40000000)) {
1681 isSecond[i]=true;
1682 CHIP_VOL_SECOND(1,3.2);
1683 willExport[i]=true;
1684 hasOPLL|=0x40000000;
1685 howManyChips++;
1686 }
1687 break;
1688 case DIV_SYSTEM_FDS:
1689 if (!hasNES) {
1690 hasNES=0x80000000|disCont[i].dispatch->chipClock;
1691 willExport[i]=true;
1692 } else if (!(hasNES&0x80000000)) {
1693 hasNES|=0x80000000;
1694 willExport[i]=true;
1695 } else if (!(hasNES&0x40000000)) {
1696 isSecond[i]=true;
1697 willExport[i]=true;
1698 hasNES|=0xc0000000;
1699 howManyChips++;
1700 }
1701 break;
1702 case DIV_SYSTEM_POKEY:
1703 if (!hasPOKEY) {
1704 hasPOKEY=disCont[i].dispatch->chipClock;
1705 CHIP_VOL(30,0.8);
1706 willExport[i]=true;
1707 } else if (!(hasPOKEY&0x40000000)) {
1708 isSecond[i]=true;
1709 CHIP_VOL_SECOND(30,0.8);
1710 willExport[i]=true;
1711 hasPOKEY|=0x40000000;
1712 howManyChips++;
1713 }
1714 break;
1715 case DIV_SYSTEM_LYNX:
1716 if (!hasLynx) {
1717 hasLynx=disCont[i].dispatch->chipClock;
1718 willExport[i]=true;
1719 writeLynxSamples=true;
1720 } else if (!(hasLynx&0x40000000)) {
1721 isSecond[i]=true;
1722 willExport[i]=true;
1723 hasLynx|=0x40000000;
1724 howManyChips++;
1725 }
1726 break;
1727 case DIV_SYSTEM_QSOUND:
1728 if (!hasQSound) {
1729 // could set chipClock to 4000000 here for compatibility
1730 // However I think it it not necessary because old VGM players will still
1731 // not be able to handle the 64kb sample bank trick
1732 hasQSound=disCont[i].dispatch->chipClock;
1733 CHIP_VOL(31,1.0);
1734 willExport[i]=true;
1735 writeQSound[0]=disCont[i].dispatch;
1736 } else if (!(hasQSound&0x40000000)) {
1737 isSecond[i]=true;
1738 CHIP_VOL_SECOND(31,1.0);
1739 willExport[i]=false;
1740 writeQSound[1]=disCont[i].dispatch;
1741 addWarning("dual QSound is not supported by the VGM format");
1742 }
1743 break;
1744 case DIV_SYSTEM_SWAN:
1745 if (!hasSwan) {
1746 hasSwan=disCont[i].dispatch->chipClock;
1747 CHIP_VOL(33,1.0);
1748 willExport[i]=true;
1749 // funny enough, VGM doesn't have support for WSC's sound DMA by design
1750 // so DAC stream it goes
1751 // since WS has the same PCM format as YM2612 DAC, I can just reuse this flag
1752 writeDACSamples=true;
1753 } else if (!(hasSwan&0x40000000)) {
1754 isSecond[i]=true;
1755 CHIP_VOL_SECOND(33,1.0);
1756 willExport[i]=true;
1757 hasSwan|=0x40000000;
1758 howManyChips++;
1759 }
1760 break;
1761 case DIV_SYSTEM_ES5506:
1762 if (!hasES5505) {
1763 // VGM identifies ES5506 if highest bit sets, otherwise ES5505
1764 hasES5505=0x80000000|disCont[i].dispatch->chipClock;
1765 willExport[i]=true;
1766 writeES5506[0]=disCont[i].dispatch;
1767 } else if (!(hasES5505&0x40000000)) {
1768 isSecond[i]=true;
1769 willExport[i]=false;
1770 hasES5505|=0xc0000000;
1771 writeES5506[1]=disCont[i].dispatch;
1772 howManyChips++;
1773 }
1774 break;
1775 case DIV_SYSTEM_VBOY:
1776 if (!hasVSU) {
1777 hasVSU=disCont[i].dispatch->chipClock;
1778 CHIP_VOL(34,0.72);
1779 willExport[i]=true;
1780 } else if (!(hasVSU&0x40000000)) {
1781 isSecond[i]=true;
1782 CHIP_VOL_SECOND(34,0.72);
1783 willExport[i]=true;
1784 hasVSU|=0x40000000;
1785 howManyChips++;
1786 }
1787 break;
1788 case DIV_SYSTEM_OPL:
1789 case DIV_SYSTEM_OPL_DRUMS:
1790 if (!hasOPL) {
1791 hasOPL=disCont[i].dispatch->chipClock;
1792 CHIP_VOL(9,1.0);
1793 willExport[i]=true;
1794 } else if (!(hasOPL&0x40000000)) {
1795 isSecond[i]=true;
1796 CHIP_VOL_SECOND(9,1.0);
1797 willExport[i]=true;
1798 hasOPL|=0x40000000;
1799 howManyChips++;
1800 }
1801 break;
1802 case DIV_SYSTEM_Y8950:
1803 case DIV_SYSTEM_Y8950_DRUMS:
1804 if (!hasY8950) {
1805 hasY8950=disCont[i].dispatch->chipClock;
1806 CHIP_VOL(11,1.0);
1807 willExport[i]=true;
1808 writeADPCM_Y8950[0]=disCont[i].dispatch;
1809 } else if (!(hasY8950&0x40000000)) {
1810 isSecond[i]=true;
1811 CHIP_VOL_SECOND(11,1.0);
1812 willExport[i]=true;
1813 writeADPCM_Y8950[1]=disCont[i].dispatch;
1814 hasY8950|=0x40000000;
1815 howManyChips++;
1816 }
1817 break;
1818 case DIV_SYSTEM_OPL2:
1819 case DIV_SYSTEM_OPL2_DRUMS:
1820 if (!hasOPL2) {
1821 hasOPL2=disCont[i].dispatch->chipClock;
1822 CHIP_VOL(10,1.0);
1823 willExport[i]=true;
1824 } else if (!(hasOPL2&0x40000000)) {
1825 isSecond[i]=true;
1826 CHIP_VOL_SECOND(10,1.0);
1827 willExport[i]=true;
1828 hasOPL2|=0x40000000;
1829 howManyChips++;
1830 }
1831 break;
1832 case DIV_SYSTEM_OPL3:
1833 case DIV_SYSTEM_OPL3_DRUMS:
1834 if (!hasOPL3) {
1835 hasOPL3=disCont[i].dispatch->chipClock;
1836 CHIP_VOL(12,1.0);
1837 willExport[i]=true;
1838 } else if (!(hasOPL3&0x40000000)) {
1839 isSecond[i]=true;
1840 CHIP_VOL_SECOND(12,1.0);
1841 willExport[i]=true;
1842 hasOPL3|=0x40000000;
1843 howManyChips++;
1844 }
1845 break;
1846 case DIV_SYSTEM_SCC:
1847 case DIV_SYSTEM_SCC_PLUS:
1848 if (!hasK051649) {
1849 hasK051649=disCont[i].dispatch->chipClock;
1850 if (song.system[i]==DIV_SYSTEM_SCC_PLUS) {
1851 hasK051649|=0x80000000;
1852 }
1853 CHIP_VOL(25,1.0);
1854 willExport[i]=true;
1855 } else if (!(hasK051649&0x40000000)) {
1856 isSecond[i]=true;
1857 CHIP_VOL_SECOND(25,1.0);
1858 willExport[i]=true;
1859 hasK051649|=0x40000000;
1860 if (song.system[i]==DIV_SYSTEM_SCC_PLUS) {
1861 hasK051649|=0x80000000;
1862 }
1863 howManyChips++;
1864 }
1865 break;
1866 case DIV_SYSTEM_YMZ280B:
1867 if (!hasZ280) {
1868 hasZ280=disCont[i].dispatch->chipClock;
1869 CHIP_VOL(15,0.72);
1870 willExport[i]=true;
1871 writeZ280[0]=disCont[i].dispatch;
1872 } else if (!(hasZ280&0x40000000)) {
1873 isSecond[i]=true;
1874 CHIP_VOL_SECOND(15,0.72);
1875 willExport[i]=true;
1876 writeZ280[1]=disCont[i].dispatch;
1877 hasZ280|=0x40000000;
1878 howManyChips++;
1879 }
1880 break;
1881 case DIV_SYSTEM_RF5C68:
1882 // here's the dumb part: VGM thinks RF5C68 and RF5C164 are different
1883 // chips even though the only difference is the output resolution
1884 // these system types are currently handled by reusing isSecond flag
1885 // also this system is not dual-able
1886 if (song.systemFlags[i].getInt("chipType",0)==1) {
1887 if (!hasRFC1) {
1888 hasRFC1=disCont[i].dispatch->chipClock;
1889 isSecond[i]=true;
1890 CHIP_VOL(16,0.8);
1891 willExport[i]=true;
1892 writeRF5C68[1]=disCont[i].dispatch;
1893 }
1894 } else if (!hasRFC) {
1895 hasRFC=disCont[i].dispatch->chipClock;
1896 CHIP_VOL(5,1.1);
1897 willExport[i]=true;
1898 writeRF5C68[0]=disCont[i].dispatch;
1899 }
1900 break;
1901 case DIV_SYSTEM_MSM6258:
1902 if (!hasOKIM6258) {
1903 hasOKIM6258=disCont[i].dispatch->chipClock;
1904 CHIP_VOL(23,0.65);
1905 willExport[i]=true;
1906 writeVOXSamples=true;
1907 } else if (!(hasOKIM6258&0x40000000)) {
1908 isSecond[i]=true;
1909 CHIP_VOL_SECOND(23,0.65);
1910 willExport[i]=true;
1911 writeVOXSamples=true;
1912 hasOKIM6258|=0x40000000;
1913 howManyChips++;
1914 }
1915 break;
1916 case DIV_SYSTEM_MSM6295:
1917 if (!hasOKIM6295) {
1918 hasOKIM6295=disCont[i].dispatch->chipClock;
1919 CHIP_VOL(24,1.0);
1920 willExport[i]=true;
1921 writeMSM6295[0]=disCont[i].dispatch;
1922 } else if (!(hasOKIM6295&0x40000000)) {
1923 isSecond[i]=true;
1924 CHIP_VOL_SECOND(24,1.0);
1925 willExport[i]=true;
1926 writeMSM6295[1]=disCont[i].dispatch;
1927 hasOKIM6295|=0x40000000;
1928 howManyChips++;
1929 }
1930 break;
1931 case DIV_SYSTEM_GA20:
1932 if (!hasGA20) {
1933 hasGA20=disCont[i].dispatch->chipClock;
1934 CHIP_VOL(40,0.4);
1935 willExport[i]=true;
1936 writeGA20[0]=disCont[i].dispatch;
1937 } else if (!(hasGA20&0x40000000)) {
1938 isSecond[i]=true;
1939 CHIP_VOL_SECOND(40,0.4);
1940 willExport[i]=true;
1941 writeGA20[1]=disCont[i].dispatch;
1942 hasGA20|=0x40000000;
1943 howManyChips++;
1944 }
1945 break;
1946 case DIV_SYSTEM_K053260:
1947 if (!hasK053260) {
1948 hasK053260=disCont[i].dispatch->chipClock;
1949 CHIP_VOL(29,0.4);
1950 willExport[i]=true;
1951 writeK053260[0]=disCont[i].dispatch;
1952 } else if (!(hasK053260&0x40000000)) {
1953 isSecond[i]=true;
1954 CHIP_VOL_SECOND(29,0.4);
1955 willExport[i]=true;
1956 writeK053260[1]=disCont[i].dispatch;
1957 hasK053260|=0x40000000;
1958 howManyChips++;
1959 }
1960 break;
1961 case DIV_SYSTEM_T6W28:
1962 if (!hasSN) {
1963 hasSN=0xc0000000|disCont[i].dispatch->chipClock;
1964 CHIP_VOL(0,2.0);
1965 snNoiseConfig=3;
1966 snNoiseSize=15;
1967 willExport[i]=true;
1968 }
1969 break;
1970 case DIV_SYSTEM_C140:
1971 if (!hasC140) {
1972 // ?!?!?!
1973 hasC140=disCont[i].dispatch->rate/2;
1974 CHIP_VOL(40,1.0);
1975 willExport[i]=true;
1976 writeC140[0]=disCont[i].dispatch;
1977 c140Type=(song.systemFlags[i].getInt("bankType",0)==1)?1:0;
1978 } else if (!(hasC140&0x40000000)) {
1979 isSecond[i]=true;
1980 CHIP_VOL_SECOND(40,1.0);
1981 willExport[i]=true;
1982 writeC140[1]=disCont[i].dispatch;
1983 hasC140|=0x40000000;
1984 howManyChips++;
1985 }
1986 break;
1987 case DIV_SYSTEM_C219:
1988 if (!hasC140) {
1989 // ?!?!?!
1990 hasC140=disCont[i].dispatch->rate/2;
1991 CHIP_VOL(40,1.0);
1992 willExport[i]=true;
1993 writeC219[0]=disCont[i].dispatch;
1994 c140Type=2;
1995 } else if (!(hasC140&0x40000000)) {
1996 isSecond[i]=true;
1997 CHIP_VOL_SECOND(40,1.0);
1998 willExport[i]=true;
1999 writeC219[1]=disCont[i].dispatch;
2000 hasC140|=0x40000000;
2001 c140Type=2;
2002 howManyChips++;
2003 }
2004 break;
2005 case DIV_SYSTEM_OPL4:
2006 case DIV_SYSTEM_OPL4_DRUMS:
2007 if (!hasOPL4) {
2008 hasOPL4=disCont[i].dispatch->chipClock;
2009 CHIP_VOL(13,1.0);
2010 willExport[i]=true;
2011 writePCM_OPL4[0]=disCont[i].dispatch;
2012 } else if (!(hasOPL4&0x40000000)) {
2013 isSecond[i]=true;
2014 CHIP_VOL_SECOND(13,1.0);
2015 willExport[i]=true;
2016 writePCM_OPL4[1]=disCont[i].dispatch;
2017 hasOPL4|=0x40000000;
2018 howManyChips++;
2019 }
2020 break;
2021 default:
2022 break;
2023 }
2024 if (willExport[i]) {
2025 disCont[i].dispatch->toggleRegisterDump(true);
2026 }
2027 }
2028
2029 // variable set but not used?
2030 logV("howManyChips: %d",howManyChips);
2031
2032 // write chips and stuff
2033 w->writeI(hasSN);
2034 w->writeI(hasOPLL);
2035 w->writeI(0);
2036 w->writeI(0); // length. will be written later
2037 w->writeI(0); // loop. will be written later
2038 w->writeI(0); // loop length. why is this necessary?
2039 w->writeI(0); // tick rate
2040 w->writeS(snNoiseConfig);
2041 w->writeC(snNoiseSize);
2042 if (version>=0x151) {
2043 w->writeC(snFlags);
2044 } else {
2045 w->writeC(0);
2046 }
2047 w->writeI(hasOPN2);
2048 w->writeI(hasOPM);
2049 w->writeI(0); // data pointer. will be written later
2050 if (version>=0x151) {
2051 w->writeI(hasSegaPCM);
2052 w->writeI(segaPCMOffset);
2053 w->writeI(hasRFC);
2054 w->writeI(hasOPN);
2055 w->writeI(hasOPNA);
2056 w->writeI(hasOPNB);
2057 w->writeI(hasOPL2);
2058 w->writeI(hasOPL);
2059 w->writeI(hasY8950);
2060 w->writeI(hasOPL3);
2061 w->writeI(hasOPL4);
2062 w->writeI(hasOPX);
2063 w->writeI(hasZ280);
2064 w->writeI(hasRFC1);
2065 w->writeI(hasPWM);
2066 w->writeI(hasAY);
2067 w->writeC(ayConfig);
2068 w->writeC(ayFlags);
2069 w->writeC(ayFlags); // OPN
2070 w->writeC(ayFlags); // OPNA
2071 } else {
2072 w->writeI(0);
2073 w->writeI(0);
2074 w->writeI(0);
2075 w->writeI(0);
2076 w->writeI(0);
2077 w->writeI(0);
2078 w->writeI(0);
2079 w->writeI(0);
2080 w->writeI(0);
2081 w->writeI(0);
2082 w->writeI(0);
2083 w->writeI(0);
2084 w->writeI(0);
2085 w->writeI(0);
2086 w->writeI(0);
2087 w->writeI(0);
2088 w->writeC(0);
2089 w->writeC(0);
2090 w->writeC(0); // OPN
2091 w->writeC(0); // OPNA
2092 }
2093 if (version>=0x160) { // global volume
2094 double abnormalVol=song.masterVol*(double)chipVolSum/(256.0*MAX(1,chipAccounting));
2095 int calcVolume=32.0*(log(abnormalVol)/log(2.0));
2096 if (calcVolume<-63) calcVolume=-63;
2097 if (calcVolume>192) calcVolume=192;
2098 w->writeC(calcVolume&0xff); // volume
2099 } else {
2100 w->writeC(0); // volume
2101 }
2102 // currently not used but is part of 1.60
2103 w->writeC(0); // reserved
2104 w->writeC(0); // loop count
2105 // 1.51
2106 w->writeC(0); // loop modifier
2107
2108 if (version>=0x161) {
2109 w->writeI(hasGB);
2110 w->writeI(hasNES);
2111 w->writeI(hasMultiPCM);
2112 w->writeI(hasuPD7759);
2113 w->writeI(hasOKIM6258);
2114 w->writeC(hasOKIM6258?10:0); // flags
2115 w->writeC(0); // K flags
2116 w->writeC(c140Type); // C140 chip type
2117 w->writeC(0); // reserved
2118 w->writeI(hasOKIM6295);
2119 w->writeI(hasK051649);
2120 w->writeI(hasK054539);
2121 w->writeI(hasPCE);
2122 w->writeI(hasC140);
2123 w->writeI(hasK053260);
2124 w->writeI(hasPOKEY);
2125 w->writeI(hasQSound);
2126 } else {
2127 w->writeI(0);
2128 w->writeI(0);
2129 w->writeI(0);
2130 w->writeI(0);
2131 w->writeI(0);
2132 w->writeC(0); // flags
2133 w->writeC(0); // K flags
2134 w->writeC(0); // C140 chip type
2135 w->writeC(0); // reserved
2136 w->writeI(0);
2137 w->writeI(0);
2138 w->writeI(0);
2139 w->writeI(0);
2140 w->writeI(0);
2141 w->writeI(0);
2142 w->writeI(0);
2143 w->writeI(0);
2144 }
2145 if (version>=0x171) {
2146 w->writeI(hasSCSP);
2147 } else {
2148 w->writeI(0);
2149 }
2150 // 1.70
2151 w->writeI(0); // extra header
2152 // 1.71
2153 if (version>=0x171) {
2154 w->writeI(hasSwan);
2155 w->writeI(hasVSU);
2156 w->writeI(hasSAA);
2157 w->writeI(hasES5503);
2158 w->writeI(hasES5505);
2159 w->writeC(0); // 5503 chans
2160 w->writeC(hasES5505?1:0); // 5505 chans
2161 w->writeC(0); // C352 clock divider
2162 w->writeC(0); // reserved
2163 w->writeI(hasX1);
2164 w->writeI(hasC352);
2165 w->writeI(hasGA20);
2166 w->writeI(version>=0x172?hasLynx:0); //Mikey introduced in 1.72
2167 } else {
2168 w->writeI(0);
2169 w->writeI(0);
2170 w->writeI(0);
2171 w->writeI(0);
2172 w->writeI(0);
2173 w->writeC(0); // 5503 chans
2174 w->writeC(0); // 5505 chans
2175 w->writeC(0); // C352 clock divider
2176 w->writeC(0); // reserved
2177 w->writeI(0);
2178 w->writeI(0);
2179 w->writeI(0);
2180 w->writeI(0);
2181 }
2182 for (int i=0; i<6; i++) { // reserved
2183 w->writeI(0);
2184 }
2185
2186 unsigned int exHeaderOff=w->tell();
2187 if (version>=0x170) {
2188 logD("writing extended header...");
2189 w->writeI(12);
2190 w->writeI(0);
2191 w->writeI(4);
2192
2193 // write chip volumes
2194 logD("writing chip volumes (%ld)...",chipVol.size());
2195 w->writeC(chipVol.size());
2196 for (unsigned int& i: chipVol) {
2197 logV("- %.8x",i);
2198 w->writeI(i);
2199 }
2200 }
2201
2202 unsigned int songOff=w->tell();
2203
2204 // initialize sample offsets
2205 memset(sampleOff8,0,32768*sizeof(unsigned int));
2206 memset(sampleLen8,0,32768*sizeof(unsigned int));
2207 memset(sampleOffSegaPCM,0,32768*sizeof(unsigned int));
2208
2209 // write samples
2210 unsigned int sampleSeek=0;
2211 for (int i=0; i<song.sampleLen; i++) {
2212 DivSample* sample=song.sample[i];
2213 logI("setting seek to %d",sampleSeek);
2214 sampleOff8[i]=sampleSeek;
2215 sampleLen8[i]=sample->length8;
2216 sampleSeek+=sample->length8;
2217 }
2218
2219 if (writeDACSamples && !directStream) for (int i=0; i<song.sampleLen; i++) {
2220 DivSample* sample=song.sample[i];
2221 w->writeC(0x67);
2222 w->writeC(0x66);
2223 w->writeC(0);
2224 w->writeI(sample->length8);
2225 for (unsigned int j=0; j<sample->length8; j++) {
2226 w->writeC((unsigned char)sample->data8[j]^0x80);
2227 }
2228 }
2229
2230 if (writeNESSamples && !directStream) for (int i=0; i<song.sampleLen; i++) {
2231 DivSample* sample=song.sample[i];
2232 w->writeC(0x67);
2233 w->writeC(0x66);
2234 w->writeC(7);
2235 w->writeI(sample->length8);
2236 for (unsigned int j=0; j<sample->length8; j++) {
2237 w->writeC(((unsigned char)sample->data8[j]^0x80)>>1);
2238 }
2239 bankOffsetNESCurrent+=sample->length8;
2240 }
2241
2242 if (writePCESamples && !directStream) for (int i=0; i<song.sampleLen; i++) {
2243 DivSample* sample=song.sample[i];
2244 w->writeC(0x67);
2245 w->writeC(0x66);
2246 w->writeC(5);
2247 w->writeI(sample->length8);
2248 for (unsigned int j=0; j<sample->length8; j++) {
2249 w->writeC(((unsigned char)sample->data8[j]^0x80)>>3);
2250 }
2251 }
2252
2253 if (writeVOXSamples && !directStream) for (int i=0; i<song.sampleLen; i++) {
2254 DivSample* sample=song.sample[i];
2255 w->writeC(0x67);
2256 w->writeC(0x66);
2257 w->writeC(4);
2258 w->writeI(sample->lengthVOX);
2259 for (unsigned int j=0; j<sample->lengthVOX; j++) {
2260 unsigned char actualData=(sample->dataVOX[j]>>4)|(sample->dataVOX[j]<<4);
2261 w->writeC(actualData);
2262 }
2263 }
2264
2265 if (writeLynxSamples && !directStream) for (int i=0; i<song.sampleLen; i++) {
2266 DivSample* sample=song.sample[i];
2267 w->writeC(0x67);
2268 w->writeC(0x66);
2269 w->writeC(8);
2270 w->writeI(sample->length8);
2271 for (unsigned int j=0; j<sample->length8; j++) {
2272 w->writeC(sample->data8[j]);
2273 }
2274 }
2275
2276 for (int i=0; i<2; i++) {
2277 // SegaPCM
2278 if (writeSegaPCM[i]!=NULL && writeSegaPCM[i]->getSampleMemUsage(0)>0) {
2279 w->writeC(0x67);
2280 w->writeC(0x66);
2281 w->writeC(0x80);
2282 w->writeI((writeSegaPCM[i]->getSampleMemUsage(0)+8)|(i*0x80000000));
2283 w->writeI(writeSegaPCM[i]->getSampleMemCapacity(0));
2284 w->writeI(0);
2285 w->write(writeSegaPCM[i]->getSampleMem(0),writeSegaPCM[i]->getSampleMemUsage(0));
2286 }
2287 // ADPCM (OPNA)
2288 if (writeADPCM_OPNA[i]!=NULL && writeADPCM_OPNA[i]->getSampleMemUsage(0)>0) {
2289 w->writeC(0x67);
2290 w->writeC(0x66);
2291 w->writeC(0x81);
2292 w->writeI((writeADPCM_OPNA[i]->getSampleMemUsage(0)+8)|(i*0x80000000));
2293 w->writeI(writeADPCM_OPNA[i]->getSampleMemCapacity(0));
2294 w->writeI(0);
2295 w->write(writeADPCM_OPNA[i]->getSampleMem(0),writeADPCM_OPNA[i]->getSampleMemUsage(0));
2296 }
2297 // ADPCM-A (OPNB)
2298 if (writeADPCM_OPNB[i]!=NULL && writeADPCM_OPNB[i]->getSampleMemUsage(0)>0) {
2299 w->writeC(0x67);
2300 w->writeC(0x66);
2301 w->writeC(0x82);
2302 w->writeI((writeADPCM_OPNB[i]->getSampleMemUsage(0)+8)|(i*0x80000000));
2303 w->writeI(writeADPCM_OPNB[i]->getSampleMemCapacity(0));
2304 w->writeI(0);
2305 w->write(writeADPCM_OPNB[i]->getSampleMem(0),writeADPCM_OPNB[i]->getSampleMemUsage(0));
2306 }
2307 // ADPCM-B (OPNB)
2308 if (writeADPCM_OPNB[i]!=NULL && writeADPCM_OPNB[i]->getSampleMemUsage(1)>0) {
2309 w->writeC(0x67);
2310 w->writeC(0x66);
2311 w->writeC(0x83);
2312 w->writeI((writeADPCM_OPNB[i]->getSampleMemUsage(1)+8)|(i*0x80000000));
2313 w->writeI(writeADPCM_OPNB[i]->getSampleMemCapacity(1));
2314 w->writeI(0);
2315 w->write(writeADPCM_OPNB[i]->getSampleMem(1),writeADPCM_OPNB[i]->getSampleMemUsage(1));
2316 }
2317 // ADPCM (Y8950)
2318 if (writeADPCM_Y8950[i]!=NULL && writeADPCM_Y8950[i]->getSampleMemUsage(0)>0) {
2319 w->writeC(0x67);
2320 w->writeC(0x66);
2321 w->writeC(0x88);
2322 w->writeI((writeADPCM_Y8950[i]->getSampleMemUsage(0)+8)|(i*0x80000000));
2323 w->writeI(writeADPCM_Y8950[i]->getSampleMemCapacity(0));
2324 w->writeI(0);
2325 w->write(writeADPCM_Y8950[i]->getSampleMem(0),writeADPCM_Y8950[i]->getSampleMemUsage(0));
2326 }
2327 // QSound has two sample types sharing the same memory.
2328 // however, they are prepresented as separate memories in Furnace.
2329 // we find the largest one to see how much memory is being used in total.
2330 if (writeQSound[i]!=NULL && (writeQSound[i]->getSampleMemUsage(0)>0 || writeQSound[i]->getSampleMemUsage(1)>0)) {
2331 unsigned int blockSize=(writeQSound[i]->getSampleMemUsage(0)+writeQSound[i]->getSampleMemUsage(1)+0xffff)&(~0xffff);
2332 if (blockSize > 0x1000000) {
2333 blockSize = 0x1000000;
2334 }
2335 w->writeC(0x67);
2336 w->writeC(0x66);
2337 w->writeC(0x8F);
2338 w->writeI((blockSize+8)|(i*0x80000000));
2339 w->writeI(writeQSound[i]->getSampleMemCapacity(0));
2340 w->writeI(0);
2341 w->write(writeQSound[i]->getSampleMem(),blockSize);
2342 }
2343 if (writeX1010[i]!=NULL && writeX1010[i]->getSampleMemUsage()>0) {
2344 w->writeC(0x67);
2345 w->writeC(0x66);
2346 w->writeC(0x91);
2347 w->writeI((writeX1010[i]->getSampleMemUsage()+8)|(i*0x80000000));
2348 w->writeI(writeX1010[i]->getSampleMemCapacity());
2349 w->writeI(0);
2350 w->write(writeX1010[i]->getSampleMem(),writeX1010[i]->getSampleMemUsage());
2351 }
2352 if (writeZ280[i]!=NULL && writeZ280[i]->getSampleMemUsage()>0) {
2353 // In VGM, YMZ280B's 16-bit PCM has an endianness swapped
2354 // which have been fixed in the upstream MAME since 2013
2355 // in order to get Konami FireBeat working
2356 // The reason given for VGM not applying this change was
2357 // "It matches OPL4 and MAME probably did an endianness optimization"
2358 size_t sampleMemLen=writeZ280[i]->getSampleMemUsage();
2359 unsigned char* sampleMem=new unsigned char[sampleMemLen];
2360 memcpy(sampleMem,writeZ280[i]->getSampleMem(),sampleMemLen);
2361 w->writeC(0x67);
2362 w->writeC(0x66);
2363 w->writeC(0x86);
2364 w->writeI((writeZ280[i]->getSampleMemUsage()+8)|(i*0x80000000));
2365 w->writeI(writeZ280[i]->getSampleMemCapacity());
2366 w->writeI(0);
2367 w->write(sampleMem,sampleMemLen);
2368 delete[] sampleMem;
2369 }
2370 // PCM (OPL4)
2371 if (writePCM_OPL4[i]!=NULL && writePCM_OPL4[i]->getSampleMemUsage(0)>0) {
2372 w->writeC(0x67);
2373 w->writeC(0x66);
2374 w->writeC(0x84);
2375 w->writeI((writePCM_OPL4[i]->getSampleMemUsage(0)+8)|(i*0x80000000));
2376 w->writeI(writePCM_OPL4[i]->getSampleMemCapacity(0));
2377 w->writeI(0);
2378 w->write(writePCM_OPL4[i]->getSampleMem(0),writePCM_OPL4[i]->getSampleMemUsage(0));
2379 }
2380 }
2381
2382 for (int i=0; i<2; i++) {
2383 if (writeRF5C68[i]!=NULL && writeRF5C68[i]->getSampleMemUsage()>0) {
2384 w->writeC(0x67);
2385 w->writeC(0x66);
2386 w->writeC(0xc0+i);
2387 w->writeI(writeRF5C68[i]->getSampleMemUsage()+2);
2388 w->writeS(0);
2389 w->write(writeRF5C68[i]->getSampleMem(),writeRF5C68[i]->getSampleMemUsage());
2390 }
2391 if (writeMSM6295[i]!=NULL && writeMSM6295[i]->getSampleMemUsage()>0) {
2392 w->writeC(0x67);
2393 w->writeC(0x66);
2394 w->writeC(0x8b);
2395 w->writeI((writeMSM6295[i]->getSampleMemUsage()+8)|(i*0x80000000));
2396 w->writeI(writeMSM6295[i]->getSampleMemCapacity());
2397 w->writeI(0);
2398 w->write(writeMSM6295[i]->getSampleMem(),writeMSM6295[i]->getSampleMemUsage());
2399 }
2400 if (writeGA20[i]!=NULL && writeGA20[i]->getSampleMemUsage()>0) {
2401 w->writeC(0x67);
2402 w->writeC(0x66);
2403 w->writeC(0x93);
2404 w->writeI((writeGA20[i]->getSampleMemUsage()+8)|(i*0x80000000));
2405 w->writeI(writeGA20[i]->getSampleMemCapacity());
2406 w->writeI(0);
2407 w->write(writeGA20[i]->getSampleMem(),writeGA20[i]->getSampleMemUsage());
2408 }
2409 if (writeK053260[i]!=NULL && writeK053260[i]->getSampleMemUsage()>0) {
2410 w->writeC(0x67);
2411 w->writeC(0x66);
2412 w->writeC(0x8e);
2413 w->writeI((writeK053260[i]->getSampleMemUsage()+8)|(i*0x80000000));
2414 w->writeI(writeK053260[i]->getSampleMemCapacity());
2415 w->writeI(0);
2416 w->write(writeK053260[i]->getSampleMem(),writeK053260[i]->getSampleMemUsage());
2417 }
2418 if (writeNES[i]!=NULL && writeNES[i]->getSampleMemUsage()>0) {
2419 if (dpcm07) {
2420 size_t howMuchWillBeWritten=writeNES[i]->getSampleMemUsage();
2421 w->writeC(0x67);
2422 w->writeC(0x66);
2423 w->writeC(7);
2424 w->writeI(howMuchWillBeWritten);
2425 w->write(writeNES[i]->getSampleMem(),howMuchWillBeWritten);
2426 bankOffsetNES[i]=bankOffsetNESCurrent;
2427 bankOffset[writeNESIndex[i]]=bankOffsetNES[i];
2428 bankOffsetNESCurrent+=howMuchWillBeWritten;
2429 // force the first bank
2430 w->writeC(0x68);
2431 w->writeC(0x6c);
2432 w->writeC(0x07|(i?0x80:0x00));
2433 w->writeC(bankOffsetNES[i]&0xff);
2434 w->writeC((bankOffsetNES[i]>>8)&0xff);
2435 w->writeC((bankOffsetNES[i]>>16)&0xff);
2436 w->writeC(0x00);
2437 w->writeC(0xc0);
2438 w->writeC(0x00);
2439 w->writeC(0x00);
2440 w->writeC(0x40);
2441 w->writeC(0x00);
2442 } else {
2443 // write the first bank
2444 size_t howMuchWillBeWritten=writeNES[i]->getSampleMemUsage();
2445 if (howMuchWillBeWritten>16384) howMuchWillBeWritten=16384;
2446 w->writeC(0x67);
2447 w->writeC(0x66);
2448 w->writeC(0xc2);
2449 w->writeI((i?0x80000000:0)|(howMuchWillBeWritten+2));
2450 // data
2451 w->writeS(0xc000);
2452 w->write(writeNES[i]->getSampleMem(),howMuchWillBeWritten);
2453 }
2454 }
2455 }
2456
2457 // TODO
2458 for (int i=0; i<2; i++) {
2459 if (writeES5506[i]!=NULL && writeES5506[i]->getSampleMemUsage()>0) {
2460 // split sample data into 4 areas
2461 unsigned short* mem=(unsigned short*)writeES5506[i]->getSampleMem();
2462 for (int b=0; b<4; b++) {
2463 int offs=b<<22;
2464 int memLen=CLAMP((int)writeES5506[i]->getSampleMemUsage()-offs,0,0x400000);
2465 if (memLen>0) {
2466 w->writeC(0x67);
2467 w->writeC(0x66);
2468 w->writeC(0x90);
2469 w->writeI((memLen+8)|(i*0x80000000));
2470 w->writeI(MIN(writeES5506[i]->getSampleMemCapacity(),0x400000));
2471 w->writeI(b<<28);
2472 for (int i=0; i<(memLen>>1); i++) {
2473 w->writeS(mem[(offs>>1)+i]);
2474 }
2475 }
2476 }
2477 }
2478 if (writeC140[i]!=NULL && writeC140[i]->getSampleMemUsage()>0) {
2479 w->writeC(0x67);
2480 w->writeC(0x66);
2481 w->writeC(0x8d);
2482 unsigned short* mem=(unsigned short*)writeC140[i]->getSampleMem();
2483 size_t memLen=writeC140[i]->getSampleMemUsage()>>1;
2484 w->writeI((memLen+8)|(i*0x80000000));
2485 w->writeI(writeC140[i]->getSampleMemCapacity());
2486 w->writeI(0);
2487 for (size_t i=0; i<memLen; i++) {
2488 w->writeC(mem[i]>>8);
2489 }
2490 }
2491 if (writeC219[i]!=NULL && writeC219[i]->getSampleMemUsage()>0) {
2492 w->writeC(0x67);
2493 w->writeC(0x66);
2494 w->writeC(0x8d);
2495 unsigned char* mem=(unsigned char*)writeC219[i]->getSampleMem();
2496 size_t memLen=writeC219[i]->getSampleMemUsage();
2497 w->writeI((memLen+8)|(i*0x80000000));
2498 w->writeI(writeC219[i]->getSampleMemCapacity());
2499 w->writeI(0);
2500 for (size_t i=0; i<memLen; i++) {
2501 w->writeC(mem[i]);
2502 }
2503 }
2504 }
2505
2506 // initialize streams
2507 int streamID=0;
2508 if (!directStream) {
2509 for (int i=0; i<song.systemLen; i++) {
2510 if (!willExport[i]) continue;
2511 streamIDs[i]=streamID;
2512 switch (song.system[i]) {
2513 case DIV_SYSTEM_YM2612:
2514 case DIV_SYSTEM_YM2612_EXT:
2515 case DIV_SYSTEM_YM2612_DUALPCM:
2516 case DIV_SYSTEM_YM2612_DUALPCM_EXT:
2517 case DIV_SYSTEM_YM2612_CSM:
2518 w->writeC(0x90);
2519 w->writeC(streamID);
2520 w->writeC(0x02);
2521 w->writeC(0); // port
2522 w->writeC(0x2a); // DAC
2523
2524 w->writeC(0x91);
2525 w->writeC(streamID);
2526 w->writeC(0);
2527 w->writeC(1);
2528 w->writeC(0);
2529
2530 w->writeC(0x92);
2531 w->writeC(streamID);
2532 w->writeI(32000); // default
2533 streamID++;
2534 break;
2535 case DIV_SYSTEM_NES:
2536 w->writeC(0x90);
2537 w->writeC(streamID);
2538 w->writeC(20);
2539 w->writeC(0); // port
2540 w->writeC(0x11); // DAC
2541
2542 w->writeC(0x91);
2543 w->writeC(streamID);
2544 w->writeC(7);
2545 w->writeC(1);
2546 w->writeC(0);
2547
2548 w->writeC(0x92);
2549 w->writeC(streamID);
2550 w->writeI(32000); // default
2551 streamID++;
2552 break;
2553 case DIV_SYSTEM_PCE:
2554 for (int j=0; j<6; j++) {
2555 w->writeC(0x90);
2556 w->writeC(streamID);
2557 w->writeC(27);
2558 w->writeC(j); // port
2559 w->writeC(0x06); // select+DAC
2560
2561 w->writeC(0x91);
2562 w->writeC(streamID);
2563 w->writeC(5);
2564 w->writeC(1);
2565 w->writeC(0);
2566
2567 w->writeC(0x92);
2568 w->writeC(streamID);
2569 w->writeI(16000); // default
2570 streamID++;
2571 }
2572 break;
2573 case DIV_SYSTEM_SWAN:
2574 w->writeC(0x90);
2575 w->writeC(streamID);
2576 w->writeC(isSecond[i]?0xa1:0x21);
2577 w->writeC(0); // port
2578 w->writeC(0x09); // DAC
2579
2580 w->writeC(0x91);
2581 w->writeC(streamID);
2582 w->writeC(0);
2583 w->writeC(1);
2584 w->writeC(0);
2585
2586 w->writeC(0x92);
2587 w->writeC(streamID);
2588 w->writeI(24000); // default
2589 streamID++;
2590 break;
2591 case DIV_SYSTEM_MSM6258:
2592 w->writeC(0x90);
2593 w->writeC(streamID);
2594 w->writeC(isSecond[i]?0x97:0x17);
2595 w->writeC(0); // port
2596 w->writeC(1); // data input
2597
2598 w->writeC(0x91);
2599 w->writeC(streamID);
2600 w->writeC(4);
2601 w->writeC(1);
2602 w->writeC(0);
2603 streamID++;
2604 break;
2605 case DIV_SYSTEM_LYNX:
2606 for (int j=0; j<4; j++) {
2607 w->writeC(0x90);
2608 w->writeC(streamID);
2609 w->writeC(isSecond[i]?0xa9:0x29);
2610 w->writeC(0); // port
2611 w->writeC(0x22+(j<<3)); // output write
2612
2613 w->writeC(0x91);
2614 w->writeC(streamID);
2615 w->writeC(8);
2616 w->writeC(1);
2617 w->writeC(0);
2618
2619 w->writeC(0x92);
2620 w->writeC(streamID);
2621 w->writeI(16000); // default
2622 streamID++;
2623 }
2624 break;
2625 default:
2626 break;
2627 }
2628 }
2629 }
2630
2631 // write song data
2632 playSub(false);
2633 size_t tickCount=0;
2634 bool writeLoop=false;
2635 bool alreadyWroteLoop=false;
2636 int ord=-1;
2637 int exportChans=0;
2638 for (int i=0; i<chans; i++) {
2639 if (!willExport[dispatchOfChan[i]]) continue;
2640 exportChans++;
2641 chan[i].wentThroughNote=false;
2642 chan[i].goneThroughNote=false;
2643 }
2644 while (!done) {
2645 if (loopPos==-1) {
2646 if (loopOrder==curOrder && loopRow==curRow) {
2647 if ((ticks-((tempoAccum+virtualTempoN)/virtualTempoD))<=0) {
2648 writeLoop=true;
2649 }
2650 }
2651 }
2652 songTick++;
2653 tickPos.push_back(w->tell());
2654 tickSample.push_back(tickCount);
2655 if (nextTick(false,true)) {
2656 if (trailing) beenOneLoopAlready=true;
2657 trailing=true;
2658 if (!loop) countDown=0;
2659 for (int i=0; i<chans; i++) {
2660 if (!willExport[dispatchOfChan[i]]) continue;
2661 chan[i].wentThroughNote=false;
2662 }
2663 }
2664 if (trailing) {
2665 switch (trailingTicks) {
2666 case -1: { // automatic
2667 bool stillHaveTo=false;
2668 for (int i=0; i<chans; i++) {
2669 if (!willExport[dispatchOfChan[i]]) continue;
2670 if (!chan[i].goneThroughNote) continue;
2671 if (!chan[i].wentThroughNote) {
2672 stillHaveTo=true;
2673 break;
2674 }
2675 }
2676 if (!stillHaveTo) countDown=0;
2677 break;
2678 }
2679 case -2: // one loop
2680 break;
2681 default: // custom
2682 countDown--;
2683 break;
2684 }
2685 if (song.loopModality!=2) countDown=0;
2686
2687 if (countDown>0 && !beenOneLoopAlready) {
2688 loopTickSong++;
2689 }
2690 }
2691 if (countDown<=0 || !playing || beenOneLoopAlready) {
2692 done=true;
2693 if (!loop) {
2694 for (int i=0; i<song.systemLen; i++) {
2695 disCont[i].dispatch->getRegisterWrites().clear();
2696 }
2697 break;
2698 }
2699 // stop all streams
2700 if (!directStream) {
2701 for (int i=0; i<streamID; i++) {
2702 w->writeC(0x94);
2703 w->writeC(i);
2704 loopSample[i]=-1;
2705 }
2706 }
2707
2708 if (!playing) {
2709 writeLoop=false;
2710 loopPos=-1;
2711 }
2712 } else {
2713 // check for pattern change
2714 if (prevOrder!=ord) {
2715 logI("registering order change %d on %d",prevOrder, prevRow);
2716 ord=prevOrder;
2717
2718 if (patternHints) {
2719 w->writeC(0x67);
2720 w->writeC(0x66);
2721 w->writeC(0xfe);
2722 w->writeI(3+exportChans);
2723 w->writeC(0x01);
2724 w->writeC(prevOrder);
2725 w->writeC(prevRow);
2726 for (int i=0; i<chans; i++) {
2727 if (!willExport[dispatchOfChan[i]]) continue;
2728 w->writeC(curSubSong->orders.ord[i][prevOrder]);
2729 }
2730 }
2731 }
2732 }
2733
2734 auto runStreams=[&](int runTime, int& wtAccum) -> int {
2735 if (!directStream) {
2736 for (int i=0; i<streamID; i++) {
2737 if (loopSample[i]>=0) {
2738 loopTimer[i]-=(loopFreq[i]/44100.0)*(double)runTime;
2739 }
2740 }
2741 bool haveNegatives=false;
2742 for (int i=0; i<streamID; i++) {
2743 if (loopSample[i]>=0) {
2744 if (loopTimer[i]<0) {
2745 haveNegatives=true;
2746 }
2747 }
2748 }
2749 while (haveNegatives) {
2750 // finish all negatives
2751 int nextToTouch=-1;
2752 for (int i=0; i<streamID; i++) {
2753 if (loopSample[i]>=0) {
2754 if (loopTimer[i]<0) {
2755 if (nextToTouch>=0) {
2756 if (loopTimer[nextToTouch]>loopTimer[i]) nextToTouch=i;
2757 } else {
2758 nextToTouch=i;
2759 }
2760 }
2761 }
2762 }
2763 if (nextToTouch>=0) {
2764 double waitTime=runTime+(loopTimer[nextToTouch]*(44100.0/MAX(1,loopFreq[nextToTouch])));
2765 if (waitTime>0) {
2766 w->writeC(0x61);
2767 w->writeS(waitTime);
2768 logV("wait is: %f",waitTime);
2769 runTime-=waitTime;
2770 wtAccum+=waitTime;
2771 }
2772 if (loopSample[nextToTouch]<song.sampleLen) {
2773 DivSample* sample=song.sample[loopSample[nextToTouch]];
2774 // insert loop
2775 if (sample->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT)<sample->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT)) {
2776 w->writeC(0x93);
2777 w->writeC(nextToTouch);
2778 w->writeI(sampleOff8[loopSample[nextToTouch]]+sample->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT));
2779 w->writeC(0x81);
2780 w->writeI(sample->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT)-sample->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT));
2781 }
2782 }
2783 loopSample[nextToTouch]=-1;
2784 } else {
2785 haveNegatives=false;
2786 }
2787 }
2788 }
2789
2790 return runTime;
2791 };
2792
2793 // calculate number of samples in this tick
2794 int totalWait=cycles;
2795
2796 // get register dumps and put them into delayed writes
2797 int writeNum=0;
2798 for (int i=0; i<song.systemLen; i++) {
2799 int curDelay=0;
2800 std::vector<DivRegWrite>& writes=disCont[i].dispatch->getRegisterWrites();
2801 for (DivRegWrite& j: writes) {
2802 if (j.addr==0xfffffffe) { // delay
2803 curDelay+=(double)j.val*(44100.0/(double)disCont[i].dispatch->rate);
2804 if (curDelay>totalWait) curDelay=totalWait-1;
2805 } else {
2806 sortedWrites.push_back(std::pair<int,DivDelayedWrite>(i,DivDelayedWrite(curDelay,writeNum++,j.addr,j.val)));
2807 }
2808 }
2809 writes.clear();
2810 }
2811
2812 // handle direct stream writes
2813 if (directStream) {
2814 // render stream of all chips
2815 for (int i=0; i<song.systemLen; i++) {
2816 disCont[i].dispatch->fillStream(delayedWrites[i],44100,totalWait);
2817 for (DivDelayedWrite& j: delayedWrites[i]) {
2818 sortedWrites.push_back(std::pair<int,DivDelayedWrite>(i,j));
2819 }
2820 delayedWrites[i].clear();
2821 }
2822 }
2823
2824 // put writes
2825 if (!sortedWrites.empty()) {
2826 // sort writes
2827 std::sort(sortedWrites.begin(),sortedWrites.end(),[](const std::pair<int,DivDelayedWrite>& a, const std::pair<int,DivDelayedWrite>& b) -> bool {
2828 if (a.second.time==b.second.time) {
2829 return a.second.order<b.second.order;
2830 }
2831 return a.second.time<b.second.time;
2832 });
2833
2834 // write it out
2835 int lastOne=0;
2836 for (std::pair<int,DivDelayedWrite>& i: sortedWrites) {
2837 if (i.second.time>lastOne) {
2838 // write delay
2839 int delay=i.second.time-lastOne;
2840 // handle streams
2841 int wtAccum1=0;
2842 delay=runStreams(delay,wtAccum1);
2843 // ????
2844
2845 if (delay>16) {
2846 w->writeC(0x61);
2847 w->writeS(delay);
2848 } else if (delay>0) {
2849 w->writeC(0x70+delay-1);
2850 }
2851 lastOne=i.second.time;
2852 }
2853 // write write
2854 performVGMWrite(w,song.system[i.first],i.second.write,streamIDs[i.first],loopTimer,loopFreq,loopSample,sampleDir,isSecond[i.first],pendingFreq,playingSample,setPos,sampleOff8,sampleLen8,bankOffset[i.first],directStream,sampleStoppable,dpcm07,writeNES,correctedRate);
2855 writeCount++;
2856 }
2857 sortedWrites.clear();
2858 totalWait-=lastOne;
2859 tickCount+=lastOne;
2860 }
2861
2862 // handle streams
2863 int wtAccum=0;
2864 totalWait=runStreams(totalWait,wtAccum);
2865 tickCount+=wtAccum;
2866
2867 // write wait
2868 if (totalWait>0) {
2869 if (totalWait==735) {
2870 w->writeC(0x62);
2871 } else if (totalWait==882) {
2872 w->writeC(0x63);
2873 } else {
2874 w->writeC(0x61);
2875 w->writeS(totalWait);
2876 }
2877 tickCount+=totalWait;
2878 }
2879 if (writeLoop && !alreadyWroteLoop) {
2880 writeLoop=false;
2881 alreadyWroteLoop=true;
2882 loopPos=w->tell();
2883 loopTickSong=songTick;
2884 }
2885 }
2886 // end of song
2887 w->writeC(0x66);
2888
2889 got.rate=origRate;
2890
2891 for (int i=0; i<song.systemLen; i++) {
2892 disCont[i].dispatch->toggleRegisterDump(false);
2893 }
2894
2895 // write GD3 tag
2896 gd3Off=w->tell();
2897 w->write("Gd3 ",4);
2898 w->writeI(0x100);
2899 w->writeI(0); // length. will be written later
2900
2901 WString ws;
2902 ws=utf8To16(song.name.c_str());
2903 w->writeWString(ws,false); // name
2904 ws=utf8To16(song.nameJ.c_str());
2905 w->writeWString(ws,false); // japanese name
2906 ws=utf8To16(song.category.c_str());
2907 w->writeWString(ws,false); // game name
2908 ws=utf8To16(song.categoryJ.c_str());
2909 w->writeWString(ws,false); // japanese game name
2910 ws=utf8To16(song.systemName.c_str());
2911 w->writeWString(ws,false); // system name
2912 ws=utf8To16(song.systemNameJ.c_str());
2913 w->writeWString(ws,false); // japanese system name
2914 ws=utf8To16(song.author.c_str());
2915 w->writeWString(ws,false); // author name
2916 ws=utf8To16(song.authorJ.c_str());
2917 w->writeWString(ws,false); // japanese author name
2918 w->writeS(0); // date
2919 w->writeWString(L"Furnace (chiptune tracker)",false); // ripper
2920 ws=utf8To16(song.notes.c_str());
2921 w->writeWString(ws,false); // notes
2922
2923 int gd3Len=w->tell()-gd3Off-12;
2924
2925 w->seek(gd3Off+8,SEEK_SET);
2926 w->writeI(gd3Len);
2927
2928 // finish file
2929 size_t len=w->size()-4;
2930 w->seek(4,SEEK_SET);
2931 w->writeI(len);
2932 w->seek(0x14,SEEK_SET);
2933 w->writeI(gd3Off-0x14);
2934 w->writeI(tickCount);
2935 if (loop) {
2936 if (loopPos==-1) {
2937 w->writeI(0);
2938 w->writeI(0);
2939 } else if (loopTickSong<0 || loopTickSong>(int)tickPos.size()) {
2940 logW("loopTickSong out of range! %d>%d",loopTickSong,(int)tickPos.size());
2941 w->writeI(0);
2942 w->writeI(0);
2943 } else {
2944 int realLoopTick=tickSample[loopTickSong];
2945 int realLoopPos=tickPos[loopTickSong];
2946 logI("tickCount-realLoopTick: %d. realLoopPos: %d",tickCount-realLoopTick,realLoopPos);
2947 w->writeI(realLoopPos-0x1c);
2948 w->writeI(tickCount-realLoopTick);
2949 }
2950 } else {
2951 w->writeI(0);
2952 w->writeI(0);
2953 }
2954 if (mayWriteRate) {
2955 w->writeI(round(curSubSong->hz));
2956 }
2957 w->seek(0x34,SEEK_SET);
2958 w->writeI(songOff-0x34);
2959 if (version>=0x170) {
2960 w->seek(0xbc,SEEK_SET);
2961 w->writeI(exHeaderOff-0xbc);
2962 }
2963
2964 remainingLoops=-1;
2965 playing=false;
2966 freelance=false;
2967 extValuePresent=false;
2968
2969 logI("%d register writes total.",writeCount);
2970
2971 delete[] sampleOff8;
2972 delete[] sampleLen8;
2973 delete[] sampleOffSegaPCM;
2974
2975 BUSY_END;
2976 return w;
2977 }