Debian Codegraph @

src/engine/dispatch.h

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 #ifndef _DISPATCH_H
21 #define _DISPATCH_H
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include "../pch.h"
26 #include "blip_buf.h"
27 #include "config.h"
28 #include "chipUtils.h"
29 #include "defines.h"
30
31 #define ONE_SEMITONE 2200
32
33 #define addWrite(a,v) regWrites.push_back(DivRegWrite(a,v));
34
35 // HOW TO ADD A NEW COMMAND:
36 // add it to this enum. then see playback.cpp.
37 // there is a const char* cmdName[] array, which contains the command
38 // names as strings for the commands (and other debug stuff).
39 //
40 // if you miss it, the program will crash or misbehave at some point.
41 //
42 // the comments are: (arg1, arg2) -> val
43 // not all commands have a return value
44 enum DivDispatchCmds {
45 DIV_CMD_NOTE_ON=0, // (note)
46 DIV_CMD_NOTE_OFF,
47 DIV_CMD_NOTE_OFF_ENV,
48 DIV_CMD_ENV_RELEASE,
49 DIV_CMD_INSTRUMENT, // (ins, force)
50 DIV_CMD_VOLUME, // (vol)
51 // TODO: think of possibly moving this
52 DIV_CMD_GET_VOLUME, // () -> vol
53 // TODO: move. shouldn't be a command.
54 DIV_CMD_GET_VOLMAX, // () -> volMax
55 DIV_CMD_NOTE_PORTA, // (target, speed) -> 2 if target reached
56 DIV_CMD_PITCH, // (pitch)
57 DIV_CMD_PANNING, // (left, right)
58 DIV_CMD_LEGATO, // (note)
59 DIV_CMD_PRE_PORTA, // (inPorta, isPortaOrSlide)
60 DIV_CMD_PRE_NOTE, // used in C64 (note)
61
62 // these will be used in ROM export.
63 // do NOT implement!
64 DIV_CMD_HINT_VIBRATO, // (speed, depth)
65 DIV_CMD_HINT_VIBRATO_RANGE, // (range)
66 DIV_CMD_HINT_VIBRATO_SHAPE, // (shape)
67 DIV_CMD_HINT_PITCH, // (pitch)
68 DIV_CMD_HINT_ARPEGGIO, // (note1, note2)
69 DIV_CMD_HINT_VOLUME, // (vol)
70 DIV_CMD_HINT_VOL_SLIDE, // (amount, oneTick)
71 DIV_CMD_HINT_PORTA, // (target, speed)
72 DIV_CMD_HINT_LEGATO, // (note)
73 DIV_CMD_HINT_VOL_SLIDE_TARGET, // (amount, target)
74 DIV_CMD_HINT_TREMOLO, // (speed/depth as a byte)
75 DIV_CMD_HINT_PANBRELLO, // (speed/depth as a byte)
76 DIV_CMD_HINT_PAN_SLIDE, // (speed)
77 DIV_CMD_HINT_PANNING, // (left, right)
78
79 DIV_CMD_SAMPLE_MODE, // (enabled)
80 DIV_CMD_SAMPLE_FREQ, // (frequency)
81 DIV_CMD_SAMPLE_BANK, // (bank)
82 DIV_CMD_SAMPLE_POS, // (pos)
83 DIV_CMD_SAMPLE_DIR, // (direction)
84
85 DIV_CMD_FM_HARD_RESET, // (enabled)
86 DIV_CMD_FM_LFO, // (speed)
87 DIV_CMD_FM_LFO_WAVE, // (waveform)
88 DIV_CMD_FM_TL, // (op, value)
89 DIV_CMD_FM_AM, // (op, value)
90 DIV_CMD_FM_AR, // (op, value)
91 DIV_CMD_FM_DR, // (op, value)
92 DIV_CMD_FM_SL, // (op, value)
93 DIV_CMD_FM_D2R, // (op, value)
94 DIV_CMD_FM_RR, // (op, value)
95 DIV_CMD_FM_DT, // (op, value)
96 DIV_CMD_FM_DT2, // (op, value)
97 DIV_CMD_FM_RS, // (op, value)
98 DIV_CMD_FM_KSR, // (op, value)
99 DIV_CMD_FM_VIB, // (op, value)
100 DIV_CMD_FM_SUS, // (op, value)
101 DIV_CMD_FM_WS, // (op, value)
102 DIV_CMD_FM_SSG, // (op, value)
103 DIV_CMD_FM_REV, // (op, value)
104 DIV_CMD_FM_EG_SHIFT, // (op, value)
105 DIV_CMD_FM_FB, // (value)
106 DIV_CMD_FM_MULT, // (op, value)
107 DIV_CMD_FM_FINE, // (op, value)
108 DIV_CMD_FM_FIXFREQ, // (op, value)
109 DIV_CMD_FM_EXTCH, // (enabled)
110 DIV_CMD_FM_AM_DEPTH, // (depth)
111 DIV_CMD_FM_PM_DEPTH, // (depth)
112
113 DIV_CMD_FM_LFO2, // (speed)
114 DIV_CMD_FM_LFO2_WAVE, // (waveform)
115
116 DIV_CMD_STD_NOISE_FREQ, // (freq)
117 DIV_CMD_STD_NOISE_MODE, // (mode)
118
119 DIV_CMD_WAVE, // (waveform)
120
121 DIV_CMD_GB_SWEEP_TIME, // (time)
122 DIV_CMD_GB_SWEEP_DIR, // (direction)
123
124 DIV_CMD_PCE_LFO_MODE, // (mode)
125 DIV_CMD_PCE_LFO_SPEED, // (speed)
126
127 DIV_CMD_NES_SWEEP, // (direction, value)
128 DIV_CMD_NES_DMC, // (value)
129
130 DIV_CMD_C64_CUTOFF, // (value)
131 DIV_CMD_C64_RESONANCE, // (value)
132 DIV_CMD_C64_FILTER_MODE, // (value)
133 DIV_CMD_C64_RESET_TIME, // (value)
134 DIV_CMD_C64_RESET_MASK, // (mask)
135 DIV_CMD_C64_FILTER_RESET, // (value)
136 DIV_CMD_C64_DUTY_RESET, // (value)
137 DIV_CMD_C64_EXTENDED, // (value)
138 DIV_CMD_C64_FINE_DUTY, // (value)
139 DIV_CMD_C64_FINE_CUTOFF, // (value)
140
141 DIV_CMD_AY_ENVELOPE_SET,
142 DIV_CMD_AY_ENVELOPE_LOW,
143 DIV_CMD_AY_ENVELOPE_HIGH,
144 DIV_CMD_AY_ENVELOPE_SLIDE,
145 DIV_CMD_AY_NOISE_MASK_AND, // (value)
146 DIV_CMD_AY_NOISE_MASK_OR, // (value)
147 DIV_CMD_AY_AUTO_ENVELOPE, // (value)
148 DIV_CMD_AY_IO_WRITE, // (port, value)
149 DIV_CMD_AY_AUTO_PWM,
150
151 DIV_CMD_FDS_MOD_DEPTH,
152 DIV_CMD_FDS_MOD_HIGH,
153 DIV_CMD_FDS_MOD_LOW,
154 DIV_CMD_FDS_MOD_POS,
155 DIV_CMD_FDS_MOD_WAVE,
156
157 DIV_CMD_SAA_ENVELOPE, // (value)
158
159 DIV_CMD_AMIGA_FILTER, // (enabled)
160 DIV_CMD_AMIGA_AM, // (enabled)
161 DIV_CMD_AMIGA_PM, // (enabled)
162
163 DIV_CMD_LYNX_LFSR_LOAD, // (value)
164
165 DIV_CMD_QSOUND_ECHO_FEEDBACK,
166 DIV_CMD_QSOUND_ECHO_DELAY,
167 DIV_CMD_QSOUND_ECHO_LEVEL,
168 DIV_CMD_QSOUND_SURROUND,
169
170 DIV_CMD_X1_010_ENVELOPE_SHAPE,
171 DIV_CMD_X1_010_ENVELOPE_ENABLE,
172 DIV_CMD_X1_010_ENVELOPE_MODE,
173 DIV_CMD_X1_010_ENVELOPE_PERIOD,
174 DIV_CMD_X1_010_ENVELOPE_SLIDE,
175 DIV_CMD_X1_010_AUTO_ENVELOPE,
176 DIV_CMD_X1_010_SAMPLE_BANK_SLOT,
177
178 DIV_CMD_WS_SWEEP_TIME, // (time)
179 DIV_CMD_WS_SWEEP_AMOUNT, // (value)
180
181 DIV_CMD_N163_WAVE_POSITION,
182 DIV_CMD_N163_WAVE_LENGTH,
183 DIV_CMD_N163_WAVE_UNUSED1,
184 DIV_CMD_N163_WAVE_UNUSED2,
185 DIV_CMD_N163_WAVE_LOADPOS,
186 DIV_CMD_N163_WAVE_LOADLEN,
187 DIV_CMD_N163_WAVE_UNUSED3,
188 DIV_CMD_N163_CHANNEL_LIMIT,
189 DIV_CMD_N163_GLOBAL_WAVE_LOAD,
190 DIV_CMD_N163_GLOBAL_WAVE_LOADPOS,
191 DIV_CMD_N163_UNUSED4,
192 DIV_CMD_N163_UNUSED5,
193
194 DIV_CMD_SU_SWEEP_PERIOD_LOW, // (which, val)
195 DIV_CMD_SU_SWEEP_PERIOD_HIGH, // (which, val)
196 DIV_CMD_SU_SWEEP_BOUND, // (which, val)
197 DIV_CMD_SU_SWEEP_ENABLE, // (which, val)
198 DIV_CMD_SU_SYNC_PERIOD_LOW,
199 DIV_CMD_SU_SYNC_PERIOD_HIGH,
200
201 DIV_CMD_ADPCMA_GLOBAL_VOLUME,
202
203 DIV_CMD_SNES_ECHO,
204 DIV_CMD_SNES_PITCH_MOD,
205 DIV_CMD_SNES_INVERT,
206 DIV_CMD_SNES_GAIN_MODE,
207 DIV_CMD_SNES_GAIN,
208 DIV_CMD_SNES_ECHO_ENABLE,
209 DIV_CMD_SNES_ECHO_DELAY,
210 DIV_CMD_SNES_ECHO_VOL_LEFT,
211 DIV_CMD_SNES_ECHO_VOL_RIGHT,
212 DIV_CMD_SNES_ECHO_FEEDBACK,
213 DIV_CMD_SNES_ECHO_FIR,
214
215 DIV_CMD_NES_ENV_MODE,
216 DIV_CMD_NES_LENGTH,
217 DIV_CMD_NES_COUNT_MODE,
218
219 DIV_CMD_MACRO_OFF, // (which)
220 DIV_CMD_MACRO_ON, // (which)
221
222 DIV_CMD_SURROUND_PANNING, // (out, val)
223
224 DIV_CMD_FM_AM2_DEPTH, // (depth)
225 DIV_CMD_FM_PM2_DEPTH, // (depth)
226
227 DIV_CMD_ES5506_FILTER_MODE, // (value)
228 DIV_CMD_ES5506_FILTER_K1, // (value, mask)
229 DIV_CMD_ES5506_FILTER_K2, // (value, mask)
230 DIV_CMD_ES5506_FILTER_K1_SLIDE, // (value, negative)
231 DIV_CMD_ES5506_FILTER_K2_SLIDE, // (value, negative)
232 DIV_CMD_ES5506_ENVELOPE_COUNT, // (count)
233 DIV_CMD_ES5506_ENVELOPE_LVRAMP, // (ramp)
234 DIV_CMD_ES5506_ENVELOPE_RVRAMP, // (ramp)
235 DIV_CMD_ES5506_ENVELOPE_K1RAMP, // (ramp, slowdown)
236 DIV_CMD_ES5506_ENVELOPE_K2RAMP, // (ramp, slowdown)
237 DIV_CMD_ES5506_PAUSE, // (value)
238
239 DIV_CMD_HINT_ARP_TIME, // (value)
240
241 DIV_CMD_SNES_GLOBAL_VOL_LEFT,
242 DIV_CMD_SNES_GLOBAL_VOL_RIGHT,
243
244 DIV_CMD_NES_LINEAR_LENGTH,
245
246 DIV_CMD_EXTERNAL, // (value)
247
248 DIV_CMD_C64_AD, // (value)
249 DIV_CMD_C64_SR, // (value)
250
251 DIV_CMD_ESFM_OP_PANNING, // (op, value)
252 DIV_CMD_ESFM_OUTLVL, // (op, value)
253 DIV_CMD_ESFM_MODIN, // (op, value)
254 DIV_CMD_ESFM_ENV_DELAY, // (op, value)
255
256 DIV_CMD_MACRO_RESTART, // (which)
257
258 DIV_CMD_POWERNOISE_COUNTER_LOAD, // (which, val)
259 DIV_CMD_POWERNOISE_IO_WRITE, // (port, value)
260
261 DIV_CMD_DAVE_HIGH_PASS,
262 DIV_CMD_DAVE_RING_MOD,
263 DIV_CMD_DAVE_SWAP_COUNTERS,
264 DIV_CMD_DAVE_LOW_PASS,
265 DIV_CMD_DAVE_CLOCK_DIV,
266
267 DIV_CMD_MINMOD_ECHO,
268
269 DIV_CMD_BIFURCATOR_STATE_LOAD,
270 DIV_CMD_BIFURCATOR_PARAMETER,
271
272 DIV_CMD_FDS_MOD_AUTO,
273
274 DIV_CMD_FM_OPMASK, // (mask)
275
276 DIV_CMD_MULTIPCM_MIX_FM, // (value)
277 DIV_CMD_MULTIPCM_MIX_PCM, // (value)
278 DIV_CMD_MULTIPCM_LFO, // (value)
279 DIV_CMD_MULTIPCM_VIB, // (value)
280 DIV_CMD_MULTIPCM_AM, // (value)
281 DIV_CMD_MULTIPCM_AR, // (value)
282 DIV_CMD_MULTIPCM_D1R, // (value)
283 DIV_CMD_MULTIPCM_DL, // (value)
284 DIV_CMD_MULTIPCM_D2R, // (value)
285 DIV_CMD_MULTIPCM_RC, // (value)
286 DIV_CMD_MULTIPCM_RR, // (value)
287 DIV_CMD_MULTIPCM_DAMP, // (value)
288 DIV_CMD_MULTIPCM_PSEUDO_REVERB, // (value)
289 DIV_CMD_MULTIPCM_LFO_RESET, // (value)
290 DIV_CMD_MULTIPCM_LEVEL_DIRECT, // (value)
291
292 DIV_CMD_SID3_SPECIAL_WAVE,
293 DIV_CMD_SID3_RING_MOD_SRC,
294 DIV_CMD_SID3_HARD_SYNC_SRC,
295 DIV_CMD_SID3_PHASE_MOD_SRC,
296 DIV_CMD_SID3_WAVE_MIX,
297 DIV_CMD_SID3_LFSR_FEEDBACK_BITS,
298 DIV_CMD_SID3_1_BIT_NOISE,
299 DIV_CMD_SID3_FILTER_DISTORTION,
300 DIV_CMD_SID3_FILTER_OUTPUT_VOLUME,
301 DIV_CMD_SID3_CHANNEL_INVERSION,
302 DIV_CMD_SID3_FILTER_CONNECTION,
303 DIV_CMD_SID3_FILTER_MATRIX,
304 DIV_CMD_SID3_FILTER_ENABLE,
305
306 DIV_CMD_C64_PW_SLIDE,
307 DIV_CMD_C64_CUTOFF_SLIDE,
308
309 DIV_CMD_SID3_PHASE_RESET,
310 DIV_CMD_SID3_NOISE_PHASE_RESET,
311 DIV_CMD_SID3_ENVELOPE_RESET,
312
313 DIV_CMD_SID3_CUTOFF_SCALING,
314 DIV_CMD_SID3_RESONANCE_SCALING,
315
316 DIV_CMD_WS_GLOBAL_SPEAKER_VOLUME, // (multiplier)
317
318 DIV_CMD_FM_ALG,
319 DIV_CMD_FM_FMS,
320 DIV_CMD_FM_AMS,
321 DIV_CMD_FM_FMS2,
322 DIV_CMD_FM_AMS2,
323
324 DIV_CMD_MAX
325 };
326
327 struct DivCommand {
328 DivDispatchCmds cmd;
329 unsigned char chan, dis;
330 int value, value2;
331 DivCommand(DivDispatchCmds c, unsigned char ch, int val, int val2):
332 cmd(c),
333 chan(ch),
334 dis(ch),
335 value(val),
336 value2(val2) {}
337 DivCommand(DivDispatchCmds c, unsigned char ch, int val):
338 cmd(c),
339 chan(ch),
340 dis(ch),
341 value(val),
342 value2(0) {}
343 DivCommand(DivDispatchCmds c, unsigned char ch):
344 cmd(c),
345 chan(ch),
346 dis(ch),
347 value(0),
348 value2(0) {}
349 };
350
351 struct DivPitchTable {
352 int pitch[(12*128)+1];
353 unsigned char linearity, blockBits;
354 bool period;
355
356 // get pitch
357 int get(int base, int pitch, int pitch2);
358
359 // linear: note
360 // non-linear: get(note,0,0)
361 int getBase(int note);
362
363 // calculate pitch table
364 void init(float tuning, double clock, double divider, int octave, unsigned char linear, bool isPeriod, unsigned char block=0);
365
366 DivPitchTable():
367 linearity(2),
368 blockBits(0),
369 period(false) {
370 memset(pitch,0,sizeof(pitch));
371 }
372 };
373
374 struct DivDelayedCommand {
375 int ticks;
376 DivCommand cmd;
377 };
378
379 struct DivRegWrite {
380 /**
381 * an address of 0xffffxx00 indicates a Furnace specific command.
382 * the following addresses are available:
383 * - 0xffffxx00: start sample playback
384 * - xx is the instance ID
385 * - data is the sample number
386 * - 0xffffxx01: set sample rate
387 * - xx is the instance ID
388 * - data is the sample rate
389 * - 0xffffxx02: stop sample playback
390 * - xx is the instance ID
391 * - 0xffffxx03: set sample playback direction
392 * - x is the instance ID
393 * - 0xffffxx04: switch sample bank
394 * - for use in VGM export
395 * - 0xffffxx05: set sample position
396 * - xx is the instance ID
397 * - data is the sample position
398 * - 0xffffffff: reset
399 * - 0xfffffffe: add delay
400 * - data is the delay
401 */
402 unsigned int addr;
403 unsigned int val;
404 DivRegWrite():
405 addr(0), val(0) {}
406 DivRegWrite(unsigned int a, unsigned int v):
407 addr(a), val(v) {}
408 };
409
410 struct DivDelayedWrite {
411 int time;
412 // this variable is internal.
413 // it is used by VGM export to make sure these writes are in order.
414 // do not change.
415 int order;
416 DivRegWrite write;
417 DivDelayedWrite(int t, int o, unsigned int a, unsigned int v):
418 time(t),
419 order(o),
420 write(a,v) {}
421 DivDelayedWrite(int t, unsigned int a, unsigned int v):
422 time(t),
423 order(0),
424 write(a,v) {}
425 };
426
427 struct DivSamplePos {
428 int sample, pos, freq;
429 DivSamplePos(int s, int p, int f):
430 sample(s),
431 pos(p),
432 freq(f) {}
433 DivSamplePos():
434 sample(-1),
435 pos(0),
436 freq(0) {}
437 };
438
439 constexpr size_t OSCBUF_PREC=(sizeof(size_t)>=8)?16:16;
440 constexpr size_t OSCBUF_MASK=(UINTMAX_C(1)<<OSCBUF_PREC)-1;
441
442 #define putSampleIKnowWhatIAmDoing(_ob,_pos,_val) \
443 _ob->data[_pos]=_val;
444
445 // the actual output of all DivDispatchOscBuffer instanced runs at 65536Hz.
446 struct DivDispatchOscBuffer {
447 size_t rate;
448 size_t rateMul;
449 unsigned int needle;
450 unsigned short readNeedle;
451 //unsigned short lastSample;
452 bool follow, mustNotKillNeedle;
453 short data[65536];
454
455 inline void putSample(const size_t pos, const short val) {
456 unsigned short realPos=((needle+pos*rateMul)>>OSCBUF_PREC);
457 if (val==-1) {
458 data[realPos]=0xfffe;
459 return;
460 }
461 //lastSample=val;
462 data[realPos]=val;
463 }
464 /*
465 inline void putSampleIKnowWhatIAmDoing(const unsigned short pos, const short val) {
466 //unsigned short realPos=((needle+pos*rateMul)>>OSCBUF_PREC);
467 if (val==-1) {
468 data[pos]=0xfffe;
469 return;
470 }
471 //lastSample=val;
472 data[pos]=val;
473 }*/
474 inline void begin(size_t len) {
475 size_t calc=(len*rateMul);
476 unsigned short start=needle>>16;
477 unsigned short end=(needle+calc)>>16;
478
479 if (mustNotKillNeedle && start!=end) {
480 start++;
481 end++;
482 }
483
484 //logD("C %d %d %d",len,calc,rate);
485
486 if (end<start) {
487 //logE("ELS %d %d %d",end,start,calc);
488 memset(&data[start],-1,(0x10000-start)*sizeof(short));
489 memset(data,-1,end*sizeof(short));
490 //data[needle>>16]=lastSample;
491 return;
492 }
493 memset(&data[start],-1,(end-start)*sizeof(short));
494 //data[needle>>16]=lastSample;
495 }
496 inline void end(size_t len) {
497 size_t calc=len*rateMul;
498 needle+=calc;
499 mustNotKillNeedle=needle&0xffff;//(data[needle>>16]!=-1);
500 //data[needle>>16]=lastSample;
501 }
502 void reset() {
503 memset(data,-1,65536*sizeof(short));
504 needle=0;
505 readNeedle=0;
506 mustNotKillNeedle=false;
507 //lastSample=0;
508 }
509 void setRate(unsigned int r) {
510 double rateMulD=65536.0/(double)r;
511 rateMulD*=(double)(UINTMAX_C(1)<<OSCBUF_PREC);
512 rate=r;
513 rateMul=(size_t)rateMulD;
514 }
515 DivDispatchOscBuffer():
516 rate(65536),
517 rateMul(UINTMAX_C(1)<<OSCBUF_PREC),
518 needle(0),
519 readNeedle(0),
520 //lastSample(0),
521 follow(true),
522 mustNotKillNeedle(false) {
523 memset(data,-1,65536*sizeof(short));
524 }
525 };
526
527 struct DivChannelPair {
528 const char* label;
529 // -1: none
530 signed char pairs[8];
531
532 DivChannelPair(const char* l, signed char p0, signed char p1, signed char p2, signed char p3, signed char p4, signed char p5, signed char p6, signed char p7):
533 label(l),
534 pairs{p0,p1,p2,p3,p4,p5,p6,p7} {}
535 DivChannelPair(const char* l, signed char p):
536 label(l),
537 pairs{p,-1,-1,-1,-1,-1,-1,-1} {}
538 DivChannelPair():
539 label(NULL),
540 pairs{-1,-1,-1,-1,-1,-1,-1,-1} {}
541 };
542
543 struct DivChannelModeHints {
544 const char* hint[4];
545 // valid types:
546 // - 0: disabled
547 // - 1: volume
548 // - 2: pitch
549 // - 3: panning
550 // - 4: chip primary
551 // - 5: chip secondary
552 // - 6: mixing
553 // - 7: DSP
554 // - 8: note
555 // - 9: misc 1
556 // - 10: misc 2
557 // - 11: misc 3
558 // - 12: attack
559 // - 13: decay
560 // - 14: sustain
561 // - 15: release
562 // - 16: dec linear
563 // - 17: dec exp
564 // - 18: inc linear
565 // - 19: inc bent
566 // - 20: direct
567 // - 21: warning
568 // - 22: error
569 unsigned char type[4];
570 // up to 4
571 unsigned char count;
572
573 DivChannelModeHints():
574 hint{NULL,NULL,NULL,NULL},
575 type{0,0,0,0},
576 count(0) {}
577 };
578
579 enum DivMemoryEntryType {
580 DIV_MEMORY_FREE=0, // shouldn't be used
581 DIV_MEMORY_PADDING,
582 DIV_MEMORY_RESERVED,
583 DIV_MEMORY_SAMPLE,
584 DIV_MEMORY_SAMPLE_ALT1,
585 DIV_MEMORY_SAMPLE_ALT2,
586 DIV_MEMORY_SAMPLE_ALT3,
587 DIV_MEMORY_WAVE_RAM,
588 DIV_MEMORY_WAVE_STATIC,
589 DIV_MEMORY_ECHO,
590 DIV_MEMORY_N163_LOAD,
591 DIV_MEMORY_N163_PLAY,
592 DIV_MEMORY_BANK0,
593 DIV_MEMORY_BANK1,
594 DIV_MEMORY_BANK2,
595 DIV_MEMORY_BANK3,
596 DIV_MEMORY_BANK4,
597 DIV_MEMORY_BANK5,
598 DIV_MEMORY_BANK6,
599 DIV_MEMORY_BANK7,
600 };
601
602 struct DivMemoryEntry {
603 DivMemoryEntryType type;
604 String name;
605 int asset;
606 size_t begin, end;
607 DivMemoryEntry(DivMemoryEntryType t, String n, int a, size_t b, size_t e):
608 type(t),
609 name(n),
610 asset(a),
611 begin(b),
612 end(e) {}
613 DivMemoryEntry():
614 type(DIV_MEMORY_FREE),
615 name(""),
616 asset(-1),
617 begin(0),
618 end(0) {}
619 };
620
621 enum DivMemoryWaveView: unsigned char {
622 DIV_MEMORY_WAVE_NONE=0,
623 DIV_MEMORY_WAVE_4BIT, // Namco 163
624 DIV_MEMORY_WAVE_6BIT, // Virtual Boy
625 DIV_MEMORY_WAVE_8BIT_SIGNED, // SCC
626 };
627
628 struct DivMemoryComposition {
629 std::vector<DivMemoryEntry> entries;
630 String name;
631 size_t capacity;
632 size_t used;
633 const unsigned char* memory;
634 DivMemoryWaveView waveformView;
635 DivMemoryComposition():
636 name(""),
637 capacity(0),
638 used(0),
639 memory(NULL),
640 waveformView(DIV_MEMORY_WAVE_NONE) {}
641 };
642
643 class DivEngine;
644 class DivMacroInt;
645
646 class DivDispatch {
647 protected:
648 DivEngine* parent;
649 std::vector<DivRegWrite> regWrites;
650 /**
651 * please honor these variables if needed.
652 */
653 bool skipRegisterWrites, dumpWrites;
654 public:
655 /**
656 * the rate the samples are provided.
657 * the engine shall resample to the output rate.
658 * you have to initialize this one during init() or setFlags().
659 */
660 int rate;
661
662 /**
663 * the actual chip's clock.
664 * you have to initialize this one during init() or setFlags().
665 */
666 int chipClock;
667
668 /**
669 * fill a buffer with sound data.
670 * @param buf pointers to output buffers.
671 * @param len the amount of samples to fill.
672 */
673 virtual void acquire(short** buf, size_t len);
674
675 /**
676 * fill a buffer with sound data (direct access to blip_buf).
677 * @param bb pointers to blip_buf instances.
678 * @param len the amount of samples to fill.
679 */
680 virtual void acquireDirect(blip_buffer_t** bb, size_t len);
681
682 /**
683 * post-process a rendered sound buffer.
684 * @param buf pointers to output buffers.
685 * @param outIndex the output index.
686 * @param len the number of samples in the buffer.
687 * @param sampleRate the current audio output rate (usually 44100 or 48000).
688 */
689 virtual void postProcess(short* buf, int outIndex, size_t len, int sampleRate);
690
691 /**
692 * fill a write stream with data (e.g. for software-mixed PCM).
693 * @param stream the write stream.
694 * @param rate stream rate (e.g. 44100 for VGM).
695 * @param len number of samples.
696 */
697 virtual void fillStream(std::vector<DivDelayedWrite>& stream, int sRate, size_t len);
698
699 /**
700 * send a command to this dispatch.
701 * @param c a DivCommand.
702 * @return a return value which varies depending on the command.
703 */
704 virtual int dispatch(DivCommand c);
705
706 /**
707 * reset the state of this dispatch.
708 */
709 virtual void reset();
710
711 /**
712 * ticks this dispatch.
713 * @param sysTick whether the engine has ticked (if not then this may be a sub-tick used in low-latency mode).
714 */
715 virtual void tick(bool sysTick=true);
716
717 /**
718 * get the state of a channel.
719 * @param chan the channel.
720 * @return a pointer, or NULL.
721 */
722 virtual void* getChanState(int chan);
723
724 /**
725 * get the DivMacroInt of a channel.
726 * @param chan the channel.
727 * @return a pointer, or NULL.
728 */
729 virtual DivMacroInt* getChanMacroInt(int chan);
730
731 /**
732 * get the stereo panning of a channel.
733 * @param chan the channel.
734 * @return a 16-bit number. left in top 8 bits and right in bottom 8 bits.
735 */
736 virtual unsigned short getPan(int chan);
737
738 /**
739 * get "paired" channels.
740 * @param ch the channel to query.
741 * @param ret the DivChannelPair vector of pairs.
742 */
743 virtual void getPaired(int ch, std::vector<DivChannelPair>& ret);
744
745 /**
746 * get channel mode hints.
747 * @param chan the channel to query.
748 * @return a DivChannelModeHints.
749 */
750 virtual DivChannelModeHints getModeHints(int chan);
751
752
753 /**
754 * get currently playing sample (and its position).
755 * @param chan the channel.
756 * @return a DivSamplePos. if sample is -1 then nothing is playing or the
757 * channel doesn't play samples.
758 */
759 virtual DivSamplePos getSamplePos(int chan);
760
761 /**
762 * get an oscilloscope buffer for a channel.
763 * @param chan the channel.
764 * @return a pointer to a DivDispatchOscBuffer, or NULL if not supported.
765 */
766 virtual DivDispatchOscBuffer* getOscBuffer(int chan);
767
768 /**
769 * get the register pool of this dispatch.
770 * @return a pointer, or NULL.
771 */
772 virtual unsigned char* getRegisterPool();
773
774 /**
775 * get the size of the register pool of this dispatch.
776 * @return the size.
777 */
778 virtual int getRegisterPoolSize();
779
780 /**
781 * get the bit depth of the register pool of this dispatch.
782 * If the result is 16, it should be casted to unsigned short.
783 * @return the depth. Default value is 8
784 */
785 virtual int getRegisterPoolDepth();
786
787 /**
788 * get this dispatch's state. DO NOT IMPLEMENT YET.
789 * @return a pointer to the dispatch's state. must be deallocated manually!
790 */
791 virtual void* getState();
792
793 /**
794 * set this dispatch's state. DO NOT IMPLEMENT YET.
795 * @param state a pointer to a state pertaining to this dispatch,
796 * or NULL if this dispatch does not support state saves.
797 */
798 virtual void setState(void* state);
799
800 /**
801 * mute a channel.
802 * @param ch the channel to mute.
803 * @param mute whether to mute or unmute.
804 */
805 virtual void muteChannel(int ch, bool mute);
806
807 /**
808 * get the number of outputs this dispatch provides.
809 * @return number of outputs (usually 1 or 2 but may be more). SHALL NOT be less than one.
810 */
811 virtual int getOutputCount();
812
813 /**
814 * test whether sending a key off command to a channel should reset arp too.
815 * @param ch the channel in question.
816 * @return whether it does.
817 */
818 virtual bool keyOffAffectsArp(int ch);
819
820 /**
821 * test whether sending a key off command to a channel should reset slides too.
822 * @param ch the channel in question.
823 * @return whether it does.
824 */
825 virtual bool keyOffAffectsPorta(int ch);
826
827 /**
828 * test whether volume is global.
829 * @return whether it is.
830 */
831 virtual bool isVolGlobal();
832
833 /**
834 * map MIDI velocity (from 0 to 127) to chip volume.
835 * @param ch the chip channel. -1 means N/A.
836 * @param vel input velocity, from 0.0 to 1.0.
837 * @return output volume.
838 */
839 virtual int mapVelocity(int ch, float vel);
840
841 /**
842 * map chip volume to gain.
843 * @param ch the chip channel. -1 means N/A.
844 * @param vol input volume.
845 * @return output gain fron 0.0 to 1.0.
846 */
847 virtual float getGain(int ch, int vol);
848
849 /**
850 * get the lowest note in a portamento.
851 * @param ch the channel in question.
852 * @return the lowest note.
853 */
854 virtual int getPortaFloor(int ch);
855
856 /**
857 * check whether to always set volume on volume change (even when same volume).
858 * only for compatibility purposes!
859 * @return truth.
860 */
861 virtual bool getLegacyAlwaysSetVolume();
862
863 /**
864 * get the required amplification level of this dispatch's output.
865 * @return the amplification level.
866 */
867 virtual float getPostAmp();
868
869 /**
870 * check whether DC offset correction is required.
871 * @return truth.
872 */
873 virtual bool getDCOffRequired();
874
875 /**
876 * check whether PRE_NOTE command is desired.
877 * @return truth.
878 */
879 virtual bool getWantPreNote();
880
881 /**
882 * check whether acquireDirect is available.
883 * @return whether it is.
884 */
885 virtual bool hasAcquireDirect();
886
887 /**
888 * get minimum chip clock.
889 * @return clock in Hz, or 0 if custom clocks are not supported.
890 */
891 virtual int getClockRangeMin();
892
893 /**
894 * get maximum chip clock.
895 * @return clock in Hz, or 0 if custom clocks are not supported.
896 */
897 virtual int getClockRangeMax();
898
899 /**
900 * set the chip flags.
901 * @param flags a DivConfig containing chip flags.
902 */
903 virtual void setFlags(const DivConfig& flags);
904
905 /**
906 * set skip reg writes.
907 */
908 virtual void setSkipRegisterWrites(bool value);
909
910 /**
911 * notify instrument change.
912 */
913 virtual void notifyInsChange(int ins);
914
915 /**
916 * notify wavetable change.
917 */
918 virtual void notifyWaveChange(int wave);
919
920 /**
921 * notify deletion of an instrument.
922 */
923 virtual void notifyInsDeletion(void* ins);
924
925 /**
926 * notify that playback stopped.
927 */
928 virtual void notifyPlaybackStop();
929
930 /**
931 * force-retrigger instruments.
932 */
933 virtual void forceIns();
934
935 /**
936 * enable register dumping.
937 */
938 virtual void toggleRegisterDump(bool enable);
939
940 /**
941 * get register writes.
942 */
943 std::vector<DivRegWrite>& getRegisterWrites();
944
945 /**
946 * poke a register.
947 * @param addr address.
948 * @param val value.
949 */
950 virtual void poke(unsigned int addr, unsigned short val);
951
952 /**
953 * poke a register.
954 * @param wlist a vector containing DivRegWrites.
955 */
956 virtual void poke(std::vector<DivRegWrite>& wlist);
957
958 /**
959 * get available registers.
960 * @return an array of C strings, terminated by NULL; or NULL if none available.
961 */
962 virtual const char** getRegisterSheet();
963
964 /**
965 * Get sample memory buffer.
966 * @param index the memory index.
967 * @return a pointer to sample memory, or NULL.
968 */
969 virtual const void* getSampleMem(int index = 0);
970
971 /**
972 * Get sample memory capacity.
973 * @param index the memory index.
974 * @return memory capacity in bytes, or 0 if memory doesn't exist.
975 */
976 virtual size_t getSampleMemCapacity(int index = 0);
977
978 /**
979 * get sample memory name.
980 * @param index the memory index.
981 * @return a name, or NULL if it doesn't have any name in particular.
982 */
983 virtual const char* getSampleMemName(int index=0);
984
985 /**
986 * Get sample memory usage.
987 * @param index the memory index.
988 * @return memory usage in bytes.
989 */
990 virtual size_t getSampleMemUsage(int index = 0);
991
992 /**
993 * check whether sample has been loaded in memory.
994 * @param index index.
995 * @param sample the sample in question.
996 * @return whether it did.
997 */
998 virtual bool isSampleLoaded(int index, int sample);
999
1000 /**
1001 * get memory composition.
1002 * @param index the memory index.
1003 * @return a pointer to DivMemoryComposition, or NULL.
1004 */
1005 virtual const DivMemoryComposition* getMemCompo(int index);
1006
1007 /**
1008 * Render samples into sample memory.
1009 * @param sysID the chip's index in the chip list.
1010 */
1011 virtual void renderSamples(int sysID);
1012
1013 /**
1014 * tell this DivDispatch that the tuning and/or pitch linearity has changed, and therefore the pitch table must be regenerated.
1015 */
1016 virtual void notifyPitchTable();
1017
1018 /**
1019 * initialize this DivDispatch.
1020 * @param parent the parent DivEngine.
1021 * @param channels the number of channels to acquire.
1022 * @param sugRate the suggested rate. this may change, so don't rely on it.
1023 * @param flags a DivConfig containing chip flags.
1024 * @return the number of channels allocated.
1025 */
1026 virtual int init(DivEngine* parent, int channels, int sugRate, const DivConfig& flags);
1027
1028 /**
1029 * quit the DivDispatch.
1030 */
1031 virtual void quit();
1032
1033 virtual ~DivDispatch();
1034 };
1035
1036 // custom chip clock helper define. put in setFlags, but before rate is set.
1037 #define CHECK_CUSTOM_CLOCK \
1038 if (flags.getInt("customClock",0)>0) { \
1039 chipClock=flags.getInt("customClock",getClockRangeMin()); \
1040 if (chipClock>getClockRangeMax()) chipClock=getClockRangeMax(); \
1041 if (chipClock<getClockRangeMin()) chipClock=getClockRangeMin(); \
1042 }
1043
1044 // NOTE: these definitions may be deprecated in the future. see DivPitchTable.
1045 // pitch calculation:
1046 // - a DivDispatch usually contains four variables per channel:
1047 // - baseFreq: this changes on new notes, legato, arpeggio and slides.
1048 // - pitch: this changes with DIV_CMD_PITCH (E5xx/04xy).
1049 // - freq: this is the result of combining baseFreq and pitch using DivEngine::calcFreq().
1050 // - freqChanged: whether baseFreq and/or pitch have changed, and a frequency recalculation is required on the next tick.
1051 // - the following definitions will help you calculate baseFreq.
1052 // - to use them, define CHIP_DIVIDER and/or CHIP_FREQBASE in your code (not in the header though!).
1053 #define NOTE_PERIODIC(x) round(parent->calcBaseFreq(chipClock,CHIP_DIVIDER,x,true))
1054 #define NOTE_PERIODIC_NOROUND(x) parent->calcBaseFreq(chipClock,CHIP_DIVIDER,x,true)
1055 #define NOTE_FREQUENCY(x) parent->calcBaseFreq(chipClock,CHIP_FREQBASE,x,false)
1056
1057 // this is a special case definition. only use it for f-num/block-based chips.
1058 #define NOTE_FNUM_BLOCK(x,bits,blk) parent->calcBaseFreqFNumBlock(chipClock,CHIP_FREQBASE,x,bits,blk)
1059
1060 // this is for volume scaling calculation.
1061 #define VOL_SCALE_LINEAR(x,y,range) ((parent->song.ceilVolumeScaling)?((((x)*(y))+(range-1))/(range)):(((x)*(y))/(range)))
1062 #define VOL_SCALE_LOG(x,y,range) (CLAMP(((x)+(y))-(range),0,(range)))
1063 #define VOL_SCALE_LINEAR_BROKEN(x,y,range) ((parent->song.newVolumeScaling)?(VOL_SCALE_LINEAR(x,y,range)):(VOL_SCALE_LOG(x,y,range)))
1064 #define VOL_SCALE_LOG_BROKEN(x,y,range) ((parent->song.newVolumeScaling)?(VOL_SCALE_LOG(x,y,range)):(VOL_SCALE_LINEAR(x,y,range)))
1065
1066 // these are here for convenience.
1067 // it is encouraged to use these, since you get an exact value this way.
1068 // - NTSC colorburst: 3.58MHz
1069 // - PAL colorburst: 4.43MHz
1070 #define COLOR_NTSC (315000000.0/88.0)
1071 #define COLOR_PAL (283.75*15625.0+25.0)
1072
1073 #define CLAMP_VAR(x,xMin,xMax) \
1074 if ((x)<(xMin)) (x)=(xMin); \
1075 if ((x)>(xMax)) (x)=(xMax);
1076
1077 #define NEW_ARP_STRAT (parent->song.linearPitch==2 && !parent->song.oldArpStrategy)
1078 #define HACKY_LEGATO_MESS chan[c.chan].std.arp.will && !chan[c.chan].std.arp.mode && !NEW_ARP_STRAT
1079
1080 #endif