2023-08-21 14:49:31 -05:00
|
|
|
/**
|
|
|
|
|
* Furnace Tracker - multi-system chiptune tracker
|
2025-01-28 18:49:19 -05:00
|
|
|
* Copyright (C) 2021-2025 tildearrow and contributors
|
2023-08-21 14:49:31 -05:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "engine.h"
|
|
|
|
|
#include "../ta-log.h"
|
2025-04-21 03:26:05 -05:00
|
|
|
#include <stack>
|
2025-04-03 17:04:34 -05:00
|
|
|
#include <unordered_map>
|
2023-08-21 14:49:31 -05:00
|
|
|
|
2025-04-05 18:19:41 -05:00
|
|
|
int DivCS::getCmdLength(unsigned char ext) {
|
|
|
|
|
switch (ext) {
|
|
|
|
|
case DIV_CMD_SAMPLE_MODE:
|
|
|
|
|
case DIV_CMD_SAMPLE_FREQ:
|
|
|
|
|
case DIV_CMD_SAMPLE_BANK:
|
|
|
|
|
case DIV_CMD_SAMPLE_DIR:
|
|
|
|
|
case DIV_CMD_FM_HARD_RESET:
|
|
|
|
|
case DIV_CMD_FM_LFO:
|
|
|
|
|
case DIV_CMD_FM_LFO_WAVE:
|
|
|
|
|
case DIV_CMD_FM_LFO2:
|
|
|
|
|
case DIV_CMD_FM_LFO2_WAVE:
|
|
|
|
|
case DIV_CMD_FM_FB:
|
|
|
|
|
case DIV_CMD_FM_EXTCH:
|
|
|
|
|
case DIV_CMD_FM_AM_DEPTH:
|
|
|
|
|
case DIV_CMD_FM_PM_DEPTH:
|
|
|
|
|
case DIV_CMD_STD_NOISE_FREQ:
|
|
|
|
|
case DIV_CMD_STD_NOISE_MODE:
|
|
|
|
|
case DIV_CMD_WAVE:
|
|
|
|
|
case DIV_CMD_GB_SWEEP_TIME:
|
|
|
|
|
case DIV_CMD_GB_SWEEP_DIR:
|
|
|
|
|
case DIV_CMD_PCE_LFO_MODE:
|
|
|
|
|
case DIV_CMD_PCE_LFO_SPEED:
|
|
|
|
|
case DIV_CMD_NES_DMC:
|
|
|
|
|
case DIV_CMD_C64_CUTOFF:
|
|
|
|
|
case DIV_CMD_C64_RESONANCE:
|
|
|
|
|
case DIV_CMD_C64_FILTER_MODE:
|
|
|
|
|
case DIV_CMD_C64_RESET_TIME:
|
|
|
|
|
case DIV_CMD_C64_RESET_MASK:
|
|
|
|
|
case DIV_CMD_C64_FILTER_RESET:
|
|
|
|
|
case DIV_CMD_C64_DUTY_RESET:
|
|
|
|
|
case DIV_CMD_C64_EXTENDED:
|
|
|
|
|
case DIV_CMD_AY_ENVELOPE_SET:
|
|
|
|
|
case DIV_CMD_AY_ENVELOPE_LOW:
|
|
|
|
|
case DIV_CMD_AY_ENVELOPE_HIGH:
|
|
|
|
|
case DIV_CMD_AY_ENVELOPE_SLIDE:
|
|
|
|
|
case DIV_CMD_AY_NOISE_MASK_AND:
|
|
|
|
|
case DIV_CMD_AY_NOISE_MASK_OR:
|
|
|
|
|
case DIV_CMD_AY_AUTO_ENVELOPE:
|
|
|
|
|
case DIV_CMD_FDS_MOD_DEPTH:
|
|
|
|
|
case DIV_CMD_FDS_MOD_HIGH:
|
|
|
|
|
case DIV_CMD_FDS_MOD_LOW:
|
|
|
|
|
case DIV_CMD_FDS_MOD_POS:
|
|
|
|
|
case DIV_CMD_FDS_MOD_WAVE:
|
|
|
|
|
case DIV_CMD_SAA_ENVELOPE:
|
|
|
|
|
case DIV_CMD_AMIGA_FILTER:
|
|
|
|
|
case DIV_CMD_AMIGA_AM:
|
|
|
|
|
case DIV_CMD_AMIGA_PM:
|
|
|
|
|
case DIV_CMD_MACRO_OFF:
|
|
|
|
|
case DIV_CMD_MACRO_ON:
|
|
|
|
|
case DIV_CMD_MACRO_RESTART:
|
|
|
|
|
case DIV_CMD_QSOUND_ECHO_FEEDBACK:
|
|
|
|
|
case DIV_CMD_QSOUND_ECHO_LEVEL:
|
|
|
|
|
case DIV_CMD_QSOUND_SURROUND:
|
|
|
|
|
case DIV_CMD_X1_010_ENVELOPE_SHAPE:
|
|
|
|
|
case DIV_CMD_X1_010_ENVELOPE_ENABLE:
|
|
|
|
|
case DIV_CMD_X1_010_ENVELOPE_MODE:
|
|
|
|
|
case DIV_CMD_X1_010_ENVELOPE_PERIOD:
|
|
|
|
|
case DIV_CMD_X1_010_ENVELOPE_SLIDE:
|
|
|
|
|
case DIV_CMD_X1_010_AUTO_ENVELOPE:
|
|
|
|
|
case DIV_CMD_X1_010_SAMPLE_BANK_SLOT:
|
|
|
|
|
case DIV_CMD_WS_SWEEP_TIME:
|
|
|
|
|
case DIV_CMD_WS_SWEEP_AMOUNT:
|
|
|
|
|
case DIV_CMD_N163_WAVE_POSITION:
|
|
|
|
|
case DIV_CMD_N163_WAVE_LENGTH:
|
|
|
|
|
case DIV_CMD_N163_WAVE_UNUSED1:
|
|
|
|
|
case DIV_CMD_N163_WAVE_UNUSED2:
|
|
|
|
|
case DIV_CMD_N163_WAVE_LOADPOS:
|
|
|
|
|
case DIV_CMD_N163_WAVE_LOADLEN:
|
|
|
|
|
case DIV_CMD_N163_WAVE_UNUSED3:
|
|
|
|
|
case DIV_CMD_N163_CHANNEL_LIMIT:
|
|
|
|
|
case DIV_CMD_N163_GLOBAL_WAVE_LOAD:
|
|
|
|
|
case DIV_CMD_N163_GLOBAL_WAVE_LOADPOS:
|
|
|
|
|
case DIV_CMD_N163_UNUSED4:
|
|
|
|
|
case DIV_CMD_N163_UNUSED5:
|
|
|
|
|
case DIV_CMD_SU_SYNC_PERIOD_LOW:
|
|
|
|
|
case DIV_CMD_SU_SYNC_PERIOD_HIGH:
|
|
|
|
|
case DIV_CMD_ADPCMA_GLOBAL_VOLUME:
|
|
|
|
|
case DIV_CMD_SNES_ECHO:
|
|
|
|
|
case DIV_CMD_SNES_PITCH_MOD:
|
|
|
|
|
case DIV_CMD_SNES_INVERT:
|
|
|
|
|
case DIV_CMD_SNES_GAIN_MODE:
|
|
|
|
|
case DIV_CMD_SNES_GAIN:
|
|
|
|
|
case DIV_CMD_SNES_ECHO_ENABLE:
|
|
|
|
|
case DIV_CMD_SNES_ECHO_DELAY:
|
|
|
|
|
case DIV_CMD_SNES_ECHO_VOL_LEFT:
|
|
|
|
|
case DIV_CMD_SNES_ECHO_VOL_RIGHT:
|
|
|
|
|
case DIV_CMD_SNES_ECHO_FEEDBACK:
|
|
|
|
|
case DIV_CMD_NES_ENV_MODE:
|
|
|
|
|
case DIV_CMD_NES_LENGTH:
|
|
|
|
|
case DIV_CMD_NES_COUNT_MODE:
|
|
|
|
|
case DIV_CMD_FM_AM2_DEPTH:
|
|
|
|
|
case DIV_CMD_FM_PM2_DEPTH:
|
|
|
|
|
case DIV_CMD_ES5506_ENVELOPE_LVRAMP:
|
|
|
|
|
case DIV_CMD_ES5506_ENVELOPE_RVRAMP:
|
|
|
|
|
case DIV_CMD_ES5506_PAUSE:
|
|
|
|
|
case DIV_CMD_ES5506_FILTER_MODE:
|
|
|
|
|
case DIV_CMD_SNES_GLOBAL_VOL_LEFT:
|
|
|
|
|
case DIV_CMD_SNES_GLOBAL_VOL_RIGHT:
|
|
|
|
|
case DIV_CMD_NES_LINEAR_LENGTH:
|
|
|
|
|
case DIV_CMD_EXTERNAL:
|
|
|
|
|
case DIV_CMD_C64_AD:
|
|
|
|
|
case DIV_CMD_C64_SR:
|
|
|
|
|
case DIV_CMD_DAVE_HIGH_PASS:
|
|
|
|
|
case DIV_CMD_DAVE_RING_MOD:
|
|
|
|
|
case DIV_CMD_DAVE_SWAP_COUNTERS:
|
|
|
|
|
case DIV_CMD_DAVE_LOW_PASS:
|
|
|
|
|
case DIV_CMD_DAVE_CLOCK_DIV:
|
|
|
|
|
case DIV_CMD_MINMOD_ECHO:
|
|
|
|
|
case DIV_CMD_FDS_MOD_AUTO:
|
|
|
|
|
case DIV_CMD_FM_OPMASK:
|
|
|
|
|
case DIV_CMD_MULTIPCM_MIX_FM:
|
|
|
|
|
case DIV_CMD_MULTIPCM_MIX_PCM:
|
|
|
|
|
case DIV_CMD_MULTIPCM_LFO:
|
|
|
|
|
case DIV_CMD_MULTIPCM_VIB:
|
|
|
|
|
case DIV_CMD_MULTIPCM_AM:
|
|
|
|
|
case DIV_CMD_MULTIPCM_AR:
|
|
|
|
|
case DIV_CMD_MULTIPCM_D1R:
|
|
|
|
|
case DIV_CMD_MULTIPCM_DL:
|
|
|
|
|
case DIV_CMD_MULTIPCM_D2R:
|
|
|
|
|
case DIV_CMD_MULTIPCM_RC:
|
|
|
|
|
case DIV_CMD_MULTIPCM_RR:
|
|
|
|
|
case DIV_CMD_MULTIPCM_DAMP:
|
|
|
|
|
case DIV_CMD_MULTIPCM_PSEUDO_REVERB:
|
|
|
|
|
case DIV_CMD_MULTIPCM_LFO_RESET:
|
|
|
|
|
case DIV_CMD_MULTIPCM_LEVEL_DIRECT:
|
|
|
|
|
case DIV_CMD_SID3_SPECIAL_WAVE:
|
|
|
|
|
case DIV_CMD_SID3_RING_MOD_SRC:
|
|
|
|
|
case DIV_CMD_SID3_HARD_SYNC_SRC:
|
|
|
|
|
case DIV_CMD_SID3_PHASE_MOD_SRC:
|
|
|
|
|
case DIV_CMD_SID3_WAVE_MIX:
|
|
|
|
|
case DIV_CMD_SID3_1_BIT_NOISE:
|
|
|
|
|
case DIV_CMD_SID3_CHANNEL_INVERSION:
|
|
|
|
|
case DIV_CMD_SID3_FILTER_CONNECTION:
|
|
|
|
|
case DIV_CMD_SID3_FILTER_MATRIX:
|
|
|
|
|
case DIV_CMD_SID3_FILTER_ENABLE:
|
|
|
|
|
case DIV_CMD_SID3_PHASE_RESET:
|
|
|
|
|
case DIV_CMD_SID3_NOISE_PHASE_RESET:
|
|
|
|
|
case DIV_CMD_SID3_ENVELOPE_RESET:
|
|
|
|
|
case DIV_CMD_SID3_CUTOFF_SCALING:
|
|
|
|
|
case DIV_CMD_SID3_RESONANCE_SCALING:
|
|
|
|
|
case DIV_CMD_WS_GLOBAL_SPEAKER_VOLUME:
|
|
|
|
|
return 1;
|
|
|
|
|
case DIV_CMD_FM_TL:
|
|
|
|
|
case DIV_CMD_FM_AM:
|
|
|
|
|
case DIV_CMD_FM_AR:
|
|
|
|
|
case DIV_CMD_FM_DR:
|
|
|
|
|
case DIV_CMD_FM_SL:
|
|
|
|
|
case DIV_CMD_FM_D2R:
|
|
|
|
|
case DIV_CMD_FM_RR:
|
|
|
|
|
case DIV_CMD_FM_DT:
|
|
|
|
|
case DIV_CMD_FM_DT2:
|
|
|
|
|
case DIV_CMD_FM_RS:
|
|
|
|
|
case DIV_CMD_FM_KSR:
|
|
|
|
|
case DIV_CMD_FM_VIB:
|
|
|
|
|
case DIV_CMD_FM_SUS:
|
|
|
|
|
case DIV_CMD_FM_WS:
|
|
|
|
|
case DIV_CMD_FM_SSG:
|
|
|
|
|
case DIV_CMD_FM_REV:
|
|
|
|
|
case DIV_CMD_FM_EG_SHIFT:
|
|
|
|
|
case DIV_CMD_FM_MULT:
|
|
|
|
|
case DIV_CMD_FM_FINE:
|
|
|
|
|
case DIV_CMD_AY_IO_WRITE:
|
|
|
|
|
case DIV_CMD_AY_AUTO_PWM:
|
|
|
|
|
case DIV_CMD_SURROUND_PANNING:
|
|
|
|
|
case DIV_CMD_SU_SWEEP_PERIOD_LOW:
|
|
|
|
|
case DIV_CMD_SU_SWEEP_PERIOD_HIGH:
|
|
|
|
|
case DIV_CMD_SU_SWEEP_BOUND:
|
|
|
|
|
case DIV_CMD_SU_SWEEP_ENABLE:
|
|
|
|
|
case DIV_CMD_SNES_ECHO_FIR:
|
|
|
|
|
case DIV_CMD_ES5506_FILTER_K1_SLIDE:
|
|
|
|
|
case DIV_CMD_ES5506_FILTER_K2_SLIDE:
|
|
|
|
|
case DIV_CMD_ES5506_ENVELOPE_K1RAMP:
|
|
|
|
|
case DIV_CMD_ES5506_ENVELOPE_K2RAMP:
|
|
|
|
|
case DIV_CMD_ESFM_OP_PANNING:
|
|
|
|
|
case DIV_CMD_ESFM_OUTLVL:
|
|
|
|
|
case DIV_CMD_ESFM_MODIN:
|
|
|
|
|
case DIV_CMD_ESFM_ENV_DELAY:
|
|
|
|
|
case DIV_CMD_POWERNOISE_COUNTER_LOAD:
|
|
|
|
|
case DIV_CMD_POWERNOISE_IO_WRITE:
|
|
|
|
|
case DIV_CMD_BIFURCATOR_STATE_LOAD:
|
|
|
|
|
case DIV_CMD_BIFURCATOR_PARAMETER:
|
|
|
|
|
case DIV_CMD_SID3_LFSR_FEEDBACK_BITS:
|
|
|
|
|
case DIV_CMD_SID3_FILTER_DISTORTION:
|
|
|
|
|
case DIV_CMD_SID3_FILTER_OUTPUT_VOLUME:
|
|
|
|
|
case DIV_CMD_C64_PW_SLIDE:
|
|
|
|
|
case DIV_CMD_C64_CUTOFF_SLIDE:
|
|
|
|
|
return 2;
|
|
|
|
|
case DIV_CMD_C64_FINE_DUTY:
|
|
|
|
|
case DIV_CMD_C64_FINE_CUTOFF:
|
|
|
|
|
case DIV_CMD_LYNX_LFSR_LOAD:
|
|
|
|
|
case DIV_CMD_QSOUND_ECHO_DELAY:
|
|
|
|
|
case DIV_CMD_ES5506_ENVELOPE_COUNT:
|
|
|
|
|
return 2;
|
|
|
|
|
case DIV_CMD_ES5506_FILTER_K1:
|
|
|
|
|
case DIV_CMD_ES5506_FILTER_K2:
|
|
|
|
|
return 4;
|
|
|
|
|
case DIV_CMD_FM_FIXFREQ:
|
|
|
|
|
return 2;
|
|
|
|
|
case DIV_CMD_NES_SWEEP:
|
|
|
|
|
return 2;
|
|
|
|
|
case DIV_CMD_SAMPLE_POS:
|
|
|
|
|
return 4;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DivCS::getInsLength(unsigned char ins, unsigned char ext, unsigned char* speedDial) {
|
2025-04-03 06:26:25 -05:00
|
|
|
switch (ins) {
|
|
|
|
|
case 0xb8: // ins
|
|
|
|
|
case 0xc0: // pre porta
|
2025-04-15 18:35:50 -05:00
|
|
|
case 0xc1: // arp time
|
2025-04-03 06:26:25 -05:00
|
|
|
case 0xc3: // vib range
|
|
|
|
|
case 0xc4: // vib shape
|
|
|
|
|
case 0xc5: // pitch
|
2025-04-08 04:00:55 -05:00
|
|
|
case 0xc6: // arpeggio
|
2025-04-03 06:26:25 -05:00
|
|
|
case 0xc7: // volume
|
|
|
|
|
case 0xca: // legato
|
2025-04-15 19:06:51 -05:00
|
|
|
case 0xcc: // tremolo
|
|
|
|
|
case 0xcd: // panbrello
|
|
|
|
|
case 0xce: // pan slide
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xdd: // waitc
|
2025-04-17 18:58:11 -05:00
|
|
|
case 0xc2: // vibrato
|
2025-04-03 06:26:25 -05:00
|
|
|
return 2;
|
2025-04-05 04:33:46 -05:00
|
|
|
case 0xcf: // pan
|
2025-04-03 06:26:25 -05:00
|
|
|
case 0xc8: // vol slide
|
|
|
|
|
case 0xc9: // porta
|
|
|
|
|
return 3;
|
|
|
|
|
// speed dial commands
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xe0: case 0xe1: case 0xe2: case 0xe3:
|
|
|
|
|
case 0xe4: case 0xe5: case 0xe6: case 0xe7:
|
|
|
|
|
case 0xe8: case 0xe9: case 0xea: case 0xeb:
|
|
|
|
|
case 0xec: case 0xed: case 0xee: case 0xef:
|
2025-04-05 18:19:41 -05:00
|
|
|
if (speedDial==NULL) return 0;
|
|
|
|
|
return 1+getCmdLength(speedDial[ins&15]);
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd0: // opt
|
2025-04-03 06:26:25 -05:00
|
|
|
return 4;
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd7: // cmd
|
2025-04-05 03:19:44 -05:00
|
|
|
// determine length from secondary
|
|
|
|
|
if (ext==0) return 0;
|
2025-04-05 18:19:41 -05:00
|
|
|
return 2+getCmdLength(ext);
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd8: // call
|
|
|
|
|
case 0xdc: // waits
|
2025-04-03 06:26:25 -05:00
|
|
|
return 3;
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd4: // callsym
|
|
|
|
|
case 0xd5: // calli
|
|
|
|
|
case 0xda: // jmp
|
|
|
|
|
case 0xdb: // rate
|
2025-04-05 04:33:46 -05:00
|
|
|
case 0xcb: // volporta
|
2025-04-03 06:26:25 -05:00
|
|
|
return 5;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-13 20:42:15 -05:00
|
|
|
void writeCommandValues(SafeWriter* w, const DivCommand& c, bool bigEndian) {
|
2023-08-21 14:49:31 -05:00
|
|
|
switch (c.cmd) {
|
|
|
|
|
case DIV_CMD_NOTE_ON:
|
|
|
|
|
if (c.value==DIV_NOTE_NULL) {
|
|
|
|
|
w->writeC(0xb4);
|
|
|
|
|
} else {
|
|
|
|
|
w->writeC(CLAMP(c.value+60,0,0xb3));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case DIV_CMD_NOTE_OFF:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xb5);
|
|
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_NOTE_OFF_ENV:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xb6);
|
|
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_ENV_RELEASE:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xb7);
|
|
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_INSTRUMENT:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xb8);
|
|
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_PRE_PORTA:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xc0);
|
|
|
|
|
break;
|
|
|
|
|
case DIV_CMD_HINT_ARP_TIME:
|
|
|
|
|
w->writeC(0xc1);
|
|
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_HINT_VIBRATO:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xc2);
|
|
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_HINT_VIBRATO_RANGE:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xc3);
|
|
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_HINT_VIBRATO_SHAPE:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xc4);
|
|
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_HINT_PITCH:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xc5);
|
|
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_HINT_ARPEGGIO:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xc6);
|
|
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_HINT_VOLUME:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xc7);
|
|
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_HINT_VOL_SLIDE:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xc8);
|
|
|
|
|
break;
|
|
|
|
|
case DIV_CMD_HINT_PORTA:
|
|
|
|
|
w->writeC(0xc9);
|
|
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_HINT_LEGATO:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xca);
|
|
|
|
|
break;
|
|
|
|
|
case DIV_CMD_HINT_VOL_SLIDE_TARGET:
|
|
|
|
|
w->writeC(0xcb);
|
|
|
|
|
break;
|
2025-04-05 04:33:46 -05:00
|
|
|
case DIV_CMD_HINT_TREMOLO:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xcc);
|
|
|
|
|
break;
|
2025-04-05 04:33:46 -05:00
|
|
|
case DIV_CMD_HINT_PANBRELLO:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xcd);
|
|
|
|
|
break;
|
2025-04-05 04:33:46 -05:00
|
|
|
case DIV_CMD_HINT_PAN_SLIDE:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xce);
|
|
|
|
|
break;
|
2025-04-05 04:33:46 -05:00
|
|
|
case DIV_CMD_HINT_PANNING:
|
2025-04-15 18:35:50 -05:00
|
|
|
w->writeC(0xcf);
|
2023-08-21 14:49:31 -05:00
|
|
|
break;
|
|
|
|
|
default:
|
2025-04-15 19:58:55 -05:00
|
|
|
w->writeC(0xd7);
|
2023-08-21 14:49:31 -05:00
|
|
|
w->writeC(c.cmd);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
switch (c.cmd) {
|
|
|
|
|
case DIV_CMD_HINT_LEGATO:
|
|
|
|
|
if (c.value==DIV_NOTE_NULL) {
|
|
|
|
|
w->writeC(0xff);
|
|
|
|
|
} else {
|
|
|
|
|
w->writeC(c.value+60);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case DIV_CMD_NOTE_ON:
|
|
|
|
|
case DIV_CMD_NOTE_OFF:
|
|
|
|
|
case DIV_CMD_NOTE_OFF_ENV:
|
|
|
|
|
case DIV_CMD_ENV_RELEASE:
|
|
|
|
|
break;
|
|
|
|
|
case DIV_CMD_INSTRUMENT:
|
|
|
|
|
case DIV_CMD_HINT_VIBRATO_RANGE:
|
|
|
|
|
case DIV_CMD_HINT_VIBRATO_SHAPE:
|
|
|
|
|
case DIV_CMD_HINT_PITCH:
|
|
|
|
|
case DIV_CMD_HINT_VOLUME:
|
2025-04-05 04:33:46 -05:00
|
|
|
case DIV_CMD_HINT_TREMOLO:
|
|
|
|
|
case DIV_CMD_HINT_PANBRELLO:
|
|
|
|
|
case DIV_CMD_HINT_PAN_SLIDE:
|
2025-04-08 04:00:55 -05:00
|
|
|
case DIV_CMD_HINT_ARPEGGIO:
|
2025-04-15 18:35:50 -05:00
|
|
|
case DIV_CMD_HINT_ARP_TIME:
|
2025-04-17 18:58:11 -05:00
|
|
|
case DIV_CMD_HINT_VIBRATO:
|
2023-08-21 14:49:31 -05:00
|
|
|
w->writeC(c.value);
|
|
|
|
|
break;
|
2025-04-05 04:33:46 -05:00
|
|
|
case DIV_CMD_HINT_PANNING:
|
2023-08-21 14:49:31 -05:00
|
|
|
w->writeC(c.value);
|
|
|
|
|
w->writeC(c.value2);
|
|
|
|
|
break;
|
2025-04-25 02:27:43 -05:00
|
|
|
case DIV_CMD_HINT_PORTA: {
|
|
|
|
|
unsigned char val=CLAMP(c.value+60,0,255);
|
|
|
|
|
w->writeC(val);
|
|
|
|
|
w->writeC(c.value2);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_PRE_PORTA:
|
|
|
|
|
w->writeC((c.value?0x80:0)|(c.value2?0x40:0));
|
|
|
|
|
break;
|
|
|
|
|
case DIV_CMD_HINT_VOL_SLIDE:
|
2025-04-13 20:42:15 -05:00
|
|
|
if (bigEndian) {
|
|
|
|
|
w->writeS_BE(c.value);
|
|
|
|
|
} else {
|
|
|
|
|
w->writeS(c.value);
|
|
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
break;
|
2024-08-23 10:39:14 -07:00
|
|
|
case DIV_CMD_HINT_VOL_SLIDE_TARGET:
|
2025-04-13 20:42:15 -05:00
|
|
|
if (bigEndian) {
|
|
|
|
|
w->writeS_BE(c.value);
|
|
|
|
|
w->writeS_BE(c.value2);
|
|
|
|
|
} else {
|
|
|
|
|
w->writeS(c.value);
|
|
|
|
|
w->writeS(c.value2);
|
|
|
|
|
}
|
2024-08-23 10:39:14 -07:00
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_SAMPLE_MODE:
|
|
|
|
|
case DIV_CMD_SAMPLE_FREQ:
|
|
|
|
|
case DIV_CMD_SAMPLE_BANK:
|
|
|
|
|
case DIV_CMD_SAMPLE_DIR:
|
|
|
|
|
case DIV_CMD_FM_HARD_RESET:
|
|
|
|
|
case DIV_CMD_FM_LFO:
|
|
|
|
|
case DIV_CMD_FM_LFO_WAVE:
|
2025-04-05 18:19:41 -05:00
|
|
|
case DIV_CMD_FM_LFO2:
|
|
|
|
|
case DIV_CMD_FM_LFO2_WAVE:
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_FM_FB:
|
|
|
|
|
case DIV_CMD_FM_EXTCH:
|
|
|
|
|
case DIV_CMD_FM_AM_DEPTH:
|
|
|
|
|
case DIV_CMD_FM_PM_DEPTH:
|
|
|
|
|
case DIV_CMD_STD_NOISE_FREQ:
|
|
|
|
|
case DIV_CMD_STD_NOISE_MODE:
|
|
|
|
|
case DIV_CMD_WAVE:
|
|
|
|
|
case DIV_CMD_GB_SWEEP_TIME:
|
|
|
|
|
case DIV_CMD_GB_SWEEP_DIR:
|
|
|
|
|
case DIV_CMD_PCE_LFO_MODE:
|
|
|
|
|
case DIV_CMD_PCE_LFO_SPEED:
|
|
|
|
|
case DIV_CMD_NES_DMC:
|
|
|
|
|
case DIV_CMD_C64_CUTOFF:
|
|
|
|
|
case DIV_CMD_C64_RESONANCE:
|
|
|
|
|
case DIV_CMD_C64_FILTER_MODE:
|
|
|
|
|
case DIV_CMD_C64_RESET_TIME:
|
|
|
|
|
case DIV_CMD_C64_RESET_MASK:
|
|
|
|
|
case DIV_CMD_C64_FILTER_RESET:
|
|
|
|
|
case DIV_CMD_C64_DUTY_RESET:
|
|
|
|
|
case DIV_CMD_C64_EXTENDED:
|
|
|
|
|
case DIV_CMD_AY_ENVELOPE_SET:
|
|
|
|
|
case DIV_CMD_AY_ENVELOPE_LOW:
|
|
|
|
|
case DIV_CMD_AY_ENVELOPE_HIGH:
|
|
|
|
|
case DIV_CMD_AY_ENVELOPE_SLIDE:
|
|
|
|
|
case DIV_CMD_AY_NOISE_MASK_AND:
|
|
|
|
|
case DIV_CMD_AY_NOISE_MASK_OR:
|
|
|
|
|
case DIV_CMD_AY_AUTO_ENVELOPE:
|
|
|
|
|
case DIV_CMD_FDS_MOD_DEPTH:
|
|
|
|
|
case DIV_CMD_FDS_MOD_HIGH:
|
|
|
|
|
case DIV_CMD_FDS_MOD_LOW:
|
|
|
|
|
case DIV_CMD_FDS_MOD_POS:
|
|
|
|
|
case DIV_CMD_FDS_MOD_WAVE:
|
|
|
|
|
case DIV_CMD_SAA_ENVELOPE:
|
|
|
|
|
case DIV_CMD_AMIGA_FILTER:
|
|
|
|
|
case DIV_CMD_AMIGA_AM:
|
|
|
|
|
case DIV_CMD_AMIGA_PM:
|
|
|
|
|
case DIV_CMD_MACRO_OFF:
|
|
|
|
|
case DIV_CMD_MACRO_ON:
|
2024-01-17 14:48:47 -05:00
|
|
|
case DIV_CMD_MACRO_RESTART:
|
2025-04-05 18:19:41 -05:00
|
|
|
case DIV_CMD_QSOUND_ECHO_FEEDBACK:
|
|
|
|
|
case DIV_CMD_QSOUND_ECHO_LEVEL:
|
|
|
|
|
case DIV_CMD_QSOUND_SURROUND:
|
|
|
|
|
case DIV_CMD_X1_010_ENVELOPE_SHAPE:
|
|
|
|
|
case DIV_CMD_X1_010_ENVELOPE_ENABLE:
|
|
|
|
|
case DIV_CMD_X1_010_ENVELOPE_MODE:
|
|
|
|
|
case DIV_CMD_X1_010_ENVELOPE_PERIOD:
|
|
|
|
|
case DIV_CMD_X1_010_ENVELOPE_SLIDE:
|
|
|
|
|
case DIV_CMD_X1_010_AUTO_ENVELOPE:
|
|
|
|
|
case DIV_CMD_X1_010_SAMPLE_BANK_SLOT:
|
|
|
|
|
case DIV_CMD_WS_SWEEP_TIME:
|
|
|
|
|
case DIV_CMD_WS_SWEEP_AMOUNT:
|
|
|
|
|
case DIV_CMD_N163_WAVE_POSITION:
|
|
|
|
|
case DIV_CMD_N163_WAVE_LENGTH:
|
|
|
|
|
case DIV_CMD_N163_WAVE_UNUSED1:
|
|
|
|
|
case DIV_CMD_N163_WAVE_UNUSED2:
|
|
|
|
|
case DIV_CMD_N163_WAVE_LOADPOS:
|
|
|
|
|
case DIV_CMD_N163_WAVE_LOADLEN:
|
|
|
|
|
case DIV_CMD_N163_WAVE_UNUSED3:
|
|
|
|
|
case DIV_CMD_N163_CHANNEL_LIMIT:
|
|
|
|
|
case DIV_CMD_N163_GLOBAL_WAVE_LOAD:
|
|
|
|
|
case DIV_CMD_N163_GLOBAL_WAVE_LOADPOS:
|
|
|
|
|
case DIV_CMD_N163_UNUSED4:
|
|
|
|
|
case DIV_CMD_N163_UNUSED5:
|
|
|
|
|
case DIV_CMD_SU_SYNC_PERIOD_LOW:
|
|
|
|
|
case DIV_CMD_SU_SYNC_PERIOD_HIGH:
|
|
|
|
|
case DIV_CMD_ADPCMA_GLOBAL_VOLUME:
|
|
|
|
|
case DIV_CMD_SNES_ECHO:
|
|
|
|
|
case DIV_CMD_SNES_PITCH_MOD:
|
|
|
|
|
case DIV_CMD_SNES_INVERT:
|
|
|
|
|
case DIV_CMD_SNES_GAIN_MODE:
|
|
|
|
|
case DIV_CMD_SNES_GAIN:
|
|
|
|
|
case DIV_CMD_SNES_ECHO_ENABLE:
|
|
|
|
|
case DIV_CMD_SNES_ECHO_DELAY:
|
|
|
|
|
case DIV_CMD_SNES_ECHO_VOL_LEFT:
|
|
|
|
|
case DIV_CMD_SNES_ECHO_VOL_RIGHT:
|
|
|
|
|
case DIV_CMD_SNES_ECHO_FEEDBACK:
|
|
|
|
|
case DIV_CMD_NES_ENV_MODE:
|
|
|
|
|
case DIV_CMD_NES_LENGTH:
|
|
|
|
|
case DIV_CMD_NES_COUNT_MODE:
|
|
|
|
|
case DIV_CMD_FM_AM2_DEPTH:
|
|
|
|
|
case DIV_CMD_FM_PM2_DEPTH:
|
|
|
|
|
case DIV_CMD_ES5506_ENVELOPE_LVRAMP:
|
|
|
|
|
case DIV_CMD_ES5506_ENVELOPE_RVRAMP:
|
|
|
|
|
case DIV_CMD_ES5506_PAUSE:
|
|
|
|
|
case DIV_CMD_ES5506_FILTER_MODE:
|
|
|
|
|
case DIV_CMD_SNES_GLOBAL_VOL_LEFT:
|
|
|
|
|
case DIV_CMD_SNES_GLOBAL_VOL_RIGHT:
|
|
|
|
|
case DIV_CMD_NES_LINEAR_LENGTH:
|
|
|
|
|
case DIV_CMD_EXTERNAL:
|
|
|
|
|
case DIV_CMD_C64_AD:
|
|
|
|
|
case DIV_CMD_C64_SR:
|
|
|
|
|
case DIV_CMD_DAVE_HIGH_PASS:
|
|
|
|
|
case DIV_CMD_DAVE_RING_MOD:
|
|
|
|
|
case DIV_CMD_DAVE_SWAP_COUNTERS:
|
|
|
|
|
case DIV_CMD_DAVE_LOW_PASS:
|
|
|
|
|
case DIV_CMD_DAVE_CLOCK_DIV:
|
|
|
|
|
case DIV_CMD_MINMOD_ECHO:
|
|
|
|
|
case DIV_CMD_FDS_MOD_AUTO:
|
|
|
|
|
case DIV_CMD_FM_OPMASK:
|
|
|
|
|
case DIV_CMD_MULTIPCM_MIX_FM:
|
|
|
|
|
case DIV_CMD_MULTIPCM_MIX_PCM:
|
|
|
|
|
case DIV_CMD_MULTIPCM_LFO:
|
|
|
|
|
case DIV_CMD_MULTIPCM_VIB:
|
|
|
|
|
case DIV_CMD_MULTIPCM_AM:
|
|
|
|
|
case DIV_CMD_MULTIPCM_AR:
|
|
|
|
|
case DIV_CMD_MULTIPCM_D1R:
|
|
|
|
|
case DIV_CMD_MULTIPCM_DL:
|
|
|
|
|
case DIV_CMD_MULTIPCM_D2R:
|
|
|
|
|
case DIV_CMD_MULTIPCM_RC:
|
|
|
|
|
case DIV_CMD_MULTIPCM_RR:
|
|
|
|
|
case DIV_CMD_MULTIPCM_DAMP:
|
|
|
|
|
case DIV_CMD_MULTIPCM_PSEUDO_REVERB:
|
|
|
|
|
case DIV_CMD_MULTIPCM_LFO_RESET:
|
|
|
|
|
case DIV_CMD_MULTIPCM_LEVEL_DIRECT:
|
|
|
|
|
case DIV_CMD_SID3_SPECIAL_WAVE:
|
|
|
|
|
case DIV_CMD_SID3_RING_MOD_SRC:
|
|
|
|
|
case DIV_CMD_SID3_HARD_SYNC_SRC:
|
|
|
|
|
case DIV_CMD_SID3_PHASE_MOD_SRC:
|
|
|
|
|
case DIV_CMD_SID3_WAVE_MIX:
|
|
|
|
|
case DIV_CMD_SID3_1_BIT_NOISE:
|
|
|
|
|
case DIV_CMD_SID3_CHANNEL_INVERSION:
|
|
|
|
|
case DIV_CMD_SID3_FILTER_CONNECTION:
|
|
|
|
|
case DIV_CMD_SID3_FILTER_MATRIX:
|
|
|
|
|
case DIV_CMD_SID3_FILTER_ENABLE:
|
|
|
|
|
case DIV_CMD_SID3_PHASE_RESET:
|
|
|
|
|
case DIV_CMD_SID3_NOISE_PHASE_RESET:
|
|
|
|
|
case DIV_CMD_SID3_ENVELOPE_RESET:
|
|
|
|
|
case DIV_CMD_SID3_CUTOFF_SCALING:
|
|
|
|
|
case DIV_CMD_SID3_RESONANCE_SCALING:
|
|
|
|
|
case DIV_CMD_WS_GLOBAL_SPEAKER_VOLUME:
|
2023-08-21 14:49:31 -05:00
|
|
|
w->writeC(c.value);
|
|
|
|
|
break;
|
|
|
|
|
case DIV_CMD_FM_TL:
|
|
|
|
|
case DIV_CMD_FM_AM:
|
|
|
|
|
case DIV_CMD_FM_AR:
|
|
|
|
|
case DIV_CMD_FM_DR:
|
|
|
|
|
case DIV_CMD_FM_SL:
|
|
|
|
|
case DIV_CMD_FM_D2R:
|
|
|
|
|
case DIV_CMD_FM_RR:
|
|
|
|
|
case DIV_CMD_FM_DT:
|
|
|
|
|
case DIV_CMD_FM_DT2:
|
|
|
|
|
case DIV_CMD_FM_RS:
|
|
|
|
|
case DIV_CMD_FM_KSR:
|
|
|
|
|
case DIV_CMD_FM_VIB:
|
|
|
|
|
case DIV_CMD_FM_SUS:
|
|
|
|
|
case DIV_CMD_FM_WS:
|
|
|
|
|
case DIV_CMD_FM_SSG:
|
|
|
|
|
case DIV_CMD_FM_REV:
|
|
|
|
|
case DIV_CMD_FM_EG_SHIFT:
|
|
|
|
|
case DIV_CMD_FM_MULT:
|
|
|
|
|
case DIV_CMD_FM_FINE:
|
|
|
|
|
case DIV_CMD_AY_IO_WRITE:
|
|
|
|
|
case DIV_CMD_AY_AUTO_PWM:
|
|
|
|
|
case DIV_CMD_SURROUND_PANNING:
|
2025-04-05 18:19:41 -05:00
|
|
|
case DIV_CMD_SU_SWEEP_PERIOD_LOW:
|
|
|
|
|
case DIV_CMD_SU_SWEEP_PERIOD_HIGH:
|
|
|
|
|
case DIV_CMD_SU_SWEEP_BOUND:
|
|
|
|
|
case DIV_CMD_SU_SWEEP_ENABLE:
|
|
|
|
|
case DIV_CMD_SNES_ECHO_FIR:
|
|
|
|
|
case DIV_CMD_ES5506_FILTER_K1_SLIDE:
|
|
|
|
|
case DIV_CMD_ES5506_FILTER_K2_SLIDE:
|
|
|
|
|
case DIV_CMD_ES5506_ENVELOPE_K1RAMP:
|
|
|
|
|
case DIV_CMD_ES5506_ENVELOPE_K2RAMP:
|
|
|
|
|
case DIV_CMD_ESFM_OP_PANNING:
|
|
|
|
|
case DIV_CMD_ESFM_OUTLVL:
|
|
|
|
|
case DIV_CMD_ESFM_MODIN:
|
|
|
|
|
case DIV_CMD_ESFM_ENV_DELAY:
|
|
|
|
|
case DIV_CMD_POWERNOISE_COUNTER_LOAD:
|
|
|
|
|
case DIV_CMD_POWERNOISE_IO_WRITE:
|
|
|
|
|
case DIV_CMD_BIFURCATOR_STATE_LOAD:
|
|
|
|
|
case DIV_CMD_BIFURCATOR_PARAMETER:
|
|
|
|
|
case DIV_CMD_SID3_LFSR_FEEDBACK_BITS:
|
|
|
|
|
case DIV_CMD_SID3_FILTER_DISTORTION:
|
|
|
|
|
case DIV_CMD_SID3_FILTER_OUTPUT_VOLUME:
|
|
|
|
|
case DIV_CMD_C64_PW_SLIDE:
|
|
|
|
|
case DIV_CMD_C64_CUTOFF_SLIDE:
|
2023-08-21 14:49:31 -05:00
|
|
|
w->writeC(c.value);
|
|
|
|
|
w->writeC(c.value2);
|
|
|
|
|
break;
|
|
|
|
|
case DIV_CMD_C64_FINE_DUTY:
|
|
|
|
|
case DIV_CMD_C64_FINE_CUTOFF:
|
|
|
|
|
case DIV_CMD_LYNX_LFSR_LOAD:
|
2025-04-05 18:19:41 -05:00
|
|
|
case DIV_CMD_QSOUND_ECHO_DELAY:
|
|
|
|
|
case DIV_CMD_ES5506_ENVELOPE_COUNT:
|
2025-04-13 20:42:15 -05:00
|
|
|
if (bigEndian) {
|
|
|
|
|
w->writeS_BE(c.value);
|
|
|
|
|
} else {
|
|
|
|
|
w->writeS(c.value);
|
|
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
break;
|
2025-04-05 18:19:41 -05:00
|
|
|
case DIV_CMD_ES5506_FILTER_K1:
|
|
|
|
|
case DIV_CMD_ES5506_FILTER_K2:
|
2025-04-13 20:42:15 -05:00
|
|
|
if (bigEndian) {
|
|
|
|
|
w->writeS_BE(c.value);
|
|
|
|
|
w->writeS_BE(c.value2);
|
|
|
|
|
} else {
|
|
|
|
|
w->writeS(c.value);
|
|
|
|
|
w->writeS(c.value2);
|
|
|
|
|
}
|
2025-04-05 18:19:41 -05:00
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_FM_FIXFREQ:
|
2025-04-13 20:42:15 -05:00
|
|
|
if (bigEndian) {
|
|
|
|
|
w->writeS_BE((c.value<<12)|(c.value2&0x7ff));
|
|
|
|
|
} else {
|
|
|
|
|
w->writeS((c.value<<12)|(c.value2&0x7ff));
|
|
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
break;
|
|
|
|
|
case DIV_CMD_NES_SWEEP:
|
|
|
|
|
w->writeC((c.value?8:0)|(c.value2&0x77));
|
|
|
|
|
break;
|
2025-04-05 18:19:41 -05:00
|
|
|
case DIV_CMD_SAMPLE_POS:
|
2025-04-13 20:42:15 -05:00
|
|
|
if (bigEndian) {
|
|
|
|
|
w->writeI_BE(c.value);
|
|
|
|
|
} else {
|
|
|
|
|
w->writeI(c.value);
|
|
|
|
|
}
|
2025-04-05 18:19:41 -05:00
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
default:
|
|
|
|
|
logW("unimplemented command %s!",cmdName[c.cmd]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-04-07 04:33:28 -05:00
|
|
|
// padding (TODO: optimize)
|
|
|
|
|
while (w->tell()&7) w->writeC(0);
|
2023-08-21 14:49:31 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-05 18:19:41 -05:00
|
|
|
#define _EXT(b,x,l) (((size_t)((x)+1)<(size_t)(l))?(b[(x)+1]):0)
|
|
|
|
|
|
2025-04-05 03:19:44 -05:00
|
|
|
using namespace DivCS;
|
|
|
|
|
|
2025-04-07 18:35:50 -05:00
|
|
|
int estimateBlockSize(unsigned char* buf, size_t len, unsigned char* speedDial) {
|
|
|
|
|
int ret=0;
|
|
|
|
|
for (size_t i=0; i<len; i+=8) {
|
|
|
|
|
ret+=getInsLength(buf[i],buf[i+1],speedDial);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-08 19:52:26 -05:00
|
|
|
void reloc8(unsigned char* buf, size_t len, unsigned int sourceAddr, unsigned int destAddr) {
|
|
|
|
|
unsigned int delta=destAddr-sourceAddr;
|
|
|
|
|
for (size_t i=0; i<len; i+=8) {
|
|
|
|
|
switch (buf[i]) {
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd5: // calli
|
|
|
|
|
case 0xda: { // jmp
|
2025-04-08 19:52:26 -05:00
|
|
|
unsigned int addr=buf[i+1]|(buf[i+2]<<8)|(buf[i+3]<<16)|(buf[i+4]<<24);
|
|
|
|
|
addr+=delta;
|
|
|
|
|
buf[i+1]=addr&0xff;
|
|
|
|
|
buf[i+2]=(addr>>8)&0xff;
|
|
|
|
|
buf[i+3]=(addr>>16)&0xff;
|
|
|
|
|
buf[i+4]=(addr>>24)&0xff;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd8: { // call
|
2025-04-08 19:52:26 -05:00
|
|
|
unsigned int addr=buf[i+1]|(buf[i+2]<<8);
|
|
|
|
|
addr+=delta;
|
|
|
|
|
if (addr>0xffff) {
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[i]=0xd5;
|
2025-04-08 19:52:26 -05:00
|
|
|
buf[i+1]=addr&0xff;
|
|
|
|
|
buf[i+2]=(addr>>8)&0xff;
|
|
|
|
|
buf[i+3]=(addr>>16)&0xff;
|
|
|
|
|
buf[i+4]=(addr>>24)&0xff;
|
|
|
|
|
} else {
|
|
|
|
|
buf[i+1]=addr&0xff;
|
|
|
|
|
buf[i+2]=(addr>>8)&0xff;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-07 18:35:50 -05:00
|
|
|
|
2025-04-13 20:42:15 -05:00
|
|
|
void reloc(unsigned char* buf, size_t len, unsigned int sourceAddr, unsigned int destAddr, unsigned char* speedDial, bool bigEndian) {
|
2025-04-03 06:26:25 -05:00
|
|
|
unsigned int delta=destAddr-sourceAddr;
|
|
|
|
|
for (size_t i=0; i<len;) {
|
2025-04-05 18:19:41 -05:00
|
|
|
int insLen=getInsLength(buf[i],_EXT(buf,i,len),speedDial);
|
2025-04-03 06:26:25 -05:00
|
|
|
if (insLen<1) {
|
|
|
|
|
logE("INS %x NOT IMPLEMENTED...",buf[i]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
switch (buf[i]) {
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd5: // calli
|
|
|
|
|
case 0xda: { // jmp
|
2025-04-06 04:59:01 -05:00
|
|
|
unsigned int addr=buf[i+1]|(buf[i+2]<<8)|(buf[i+3]<<16)|(buf[i+4]<<24);
|
2025-04-03 06:26:25 -05:00
|
|
|
addr+=delta;
|
2025-04-13 20:42:15 -05:00
|
|
|
if (bigEndian) {
|
|
|
|
|
buf[i+1]=(addr>>24)&0xff;
|
|
|
|
|
buf[i+2]=(addr>>16)&0xff;
|
|
|
|
|
buf[i+3]=(addr>>8)&0xff;
|
|
|
|
|
buf[i+4]=addr&0xff;
|
|
|
|
|
} else {
|
|
|
|
|
buf[i+1]=addr&0xff;
|
|
|
|
|
buf[i+2]=(addr>>8)&0xff;
|
|
|
|
|
buf[i+3]=(addr>>16)&0xff;
|
|
|
|
|
buf[i+4]=(addr>>24)&0xff;
|
|
|
|
|
}
|
2025-04-03 06:26:25 -05:00
|
|
|
break;
|
|
|
|
|
}
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd8: { // call
|
2025-04-06 04:59:01 -05:00
|
|
|
unsigned short addr=buf[i+1]|(buf[i+2]<<8);
|
|
|
|
|
addr+=delta;
|
2025-04-13 20:42:15 -05:00
|
|
|
if (bigEndian) {
|
|
|
|
|
buf[i+1]=(addr>>8)&0xff;
|
|
|
|
|
buf[i+2]=addr&0xff;
|
|
|
|
|
} else {
|
|
|
|
|
buf[i+1]=addr&0xff;
|
|
|
|
|
buf[i+2]=(addr>>8)&0xff;
|
|
|
|
|
}
|
2025-04-06 04:59:01 -05:00
|
|
|
break;
|
|
|
|
|
}
|
2025-04-03 06:26:25 -05:00
|
|
|
}
|
|
|
|
|
i+=insLen;
|
|
|
|
|
}
|
2025-04-03 05:09:40 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-07 13:17:27 -05:00
|
|
|
SafeWriter* stripNops(SafeWriter* s) {
|
2025-04-04 05:01:49 -05:00
|
|
|
std::unordered_map<unsigned int,unsigned int> addrTable;
|
|
|
|
|
SafeWriter* oldStream=s;
|
|
|
|
|
unsigned char* buf=oldStream->getFinalBuf();
|
|
|
|
|
s=new SafeWriter;
|
|
|
|
|
s->init();
|
|
|
|
|
|
|
|
|
|
// prepare address map
|
|
|
|
|
size_t addr=0;
|
2025-04-07 04:33:28 -05:00
|
|
|
for (size_t i=0; i<oldStream->size(); i+=8) {
|
2025-04-04 05:01:49 -05:00
|
|
|
addrTable[i]=addr;
|
2025-04-15 19:58:55 -05:00
|
|
|
if (buf[i]!=0xd1) addr+=8;
|
2025-04-04 05:01:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// translate addresses
|
2025-04-07 04:33:28 -05:00
|
|
|
for (size_t i=0; i<oldStream->size(); i+=8) {
|
2025-04-04 05:01:49 -05:00
|
|
|
switch (buf[i]) {
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd5: // calli
|
|
|
|
|
case 0xda: { // jmp
|
2025-04-06 04:59:01 -05:00
|
|
|
unsigned int addr=buf[i+1]|(buf[i+2]<<8)|(buf[i+3]<<16)|(buf[i+4]<<24);
|
2025-04-11 20:21:46 -05:00
|
|
|
assert(!(addr&7));
|
2025-04-12 20:15:17 -05:00
|
|
|
if (addr>=oldStream->size()) {
|
|
|
|
|
logE("OUT OF BOUNDS!");
|
2025-04-12 04:55:40 -05:00
|
|
|
abort();
|
|
|
|
|
}
|
2025-04-15 19:58:55 -05:00
|
|
|
if (buf[addr]==0xd1) {
|
2025-04-12 20:15:17 -05:00
|
|
|
logE("POINTS TO NOP");
|
2025-04-12 04:55:40 -05:00
|
|
|
abort();
|
|
|
|
|
}
|
2025-04-04 05:01:49 -05:00
|
|
|
try {
|
|
|
|
|
addr=addrTable[addr];
|
|
|
|
|
buf[i+1]=addr&0xff;
|
|
|
|
|
buf[i+2]=(addr>>8)&0xff;
|
|
|
|
|
buf[i+3]=(addr>>16)&0xff;
|
|
|
|
|
buf[i+4]=(addr>>24)&0xff;
|
|
|
|
|
} catch (std::out_of_range& e) {
|
|
|
|
|
logW("address %x is not mappable!",addr);
|
2025-04-12 04:55:40 -05:00
|
|
|
abort();
|
2025-04-04 05:01:49 -05:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd8: { // call
|
2025-04-06 04:59:01 -05:00
|
|
|
unsigned int addr=buf[i+1]|(buf[i+2]<<8);
|
|
|
|
|
try {
|
|
|
|
|
addr=addrTable[addr];
|
|
|
|
|
buf[i+1]=addr&0xff;
|
|
|
|
|
buf[i+2]=(addr>>8)&0xff;
|
|
|
|
|
} catch (std::out_of_range& e) {
|
|
|
|
|
logW("address %x is not mappable!",addr);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-04-04 05:01:49 -05:00
|
|
|
}
|
2025-04-15 19:58:55 -05:00
|
|
|
if (buf[i]!=0xd1) {
|
2025-04-07 04:33:28 -05:00
|
|
|
s->write(&buf[i],8);
|
2025-04-04 05:01:49 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
oldStream->finish();
|
|
|
|
|
delete oldStream;
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-19 18:56:19 -05:00
|
|
|
SafeWriter* stripNopsPacked(SafeWriter* s, unsigned char* speedDial, unsigned int* chanStreamOff) {
|
2025-04-09 03:53:17 -05:00
|
|
|
std::unordered_map<unsigned int,unsigned int> addrTable;
|
|
|
|
|
SafeWriter* oldStream=s;
|
|
|
|
|
unsigned char* buf=oldStream->getFinalBuf();
|
|
|
|
|
s=new SafeWriter;
|
|
|
|
|
s->init();
|
|
|
|
|
|
|
|
|
|
// prepare address map
|
|
|
|
|
size_t addr=0;
|
|
|
|
|
for (size_t i=0; i<oldStream->size();) {
|
|
|
|
|
int insLen=getInsLength(buf[i],_EXT(buf,i,oldStream->size()),speedDial);
|
|
|
|
|
if (insLen<1) {
|
|
|
|
|
logE("INS %x NOT IMPLEMENTED...",buf[i]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
addrTable[i]=addr;
|
2025-04-19 18:56:19 -05:00
|
|
|
if (buf[i]!=0xd1 && buf[i]!=0xd0) addr+=insLen;
|
2025-04-09 03:53:17 -05:00
|
|
|
i+=insLen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// translate addresses
|
|
|
|
|
for (size_t i=0; i<oldStream->size();) {
|
|
|
|
|
int insLen=getInsLength(buf[i],_EXT(buf,i,oldStream->size()),speedDial);
|
|
|
|
|
if (insLen<1) {
|
|
|
|
|
logE("INS %x NOT IMPLEMENTED...",buf[i]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
switch (buf[i]) {
|
2025-04-19 18:56:19 -05:00
|
|
|
case 0xd0: // ext (for channel offsets)
|
|
|
|
|
if (buf[i+3]==0) {
|
|
|
|
|
int ch=buf[i+1];
|
|
|
|
|
if (ch>=0 && ch<DIV_MAX_CHANS) {
|
|
|
|
|
chanStreamOff[ch]=addrTable[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd5: // calli
|
|
|
|
|
case 0xda: { // jmp
|
2025-04-09 03:53:17 -05:00
|
|
|
unsigned int addr=buf[i+1]|(buf[i+2]<<8)|(buf[i+3]<<8)|(buf[i+4]<<24);
|
|
|
|
|
try {
|
|
|
|
|
addr=addrTable[addr];
|
|
|
|
|
buf[i+1]=addr&0xff;
|
|
|
|
|
buf[i+2]=(addr>>8)&0xff;
|
|
|
|
|
buf[i+3]=(addr>>16)&0xff;
|
|
|
|
|
buf[i+4]=(addr>>24)&0xff;
|
|
|
|
|
} catch (std::out_of_range& e) {
|
|
|
|
|
logW("address %x is not mappable!",addr);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd8: { // call
|
2025-04-09 03:53:17 -05:00
|
|
|
unsigned int addr=buf[i+1]|(buf[i+2]<<8);
|
|
|
|
|
try {
|
|
|
|
|
addr=addrTable[addr];
|
|
|
|
|
buf[i+1]=addr&0xff;
|
|
|
|
|
buf[i+2]=(addr>>8)&0xff;
|
|
|
|
|
if (addr>0xffff) { // this may never happen but it's here just in case
|
|
|
|
|
logW("address %x is out of range for 16-bit call!",addr);
|
|
|
|
|
}
|
|
|
|
|
} catch (std::out_of_range& e) {
|
|
|
|
|
logW("address %x is not mappable!",addr);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-19 18:56:19 -05:00
|
|
|
if (buf[i]!=0xd1 && buf[i]!=0xd0) {
|
2025-04-09 03:53:17 -05:00
|
|
|
s->write(&buf[i],insLen);
|
|
|
|
|
}
|
|
|
|
|
i+=insLen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
oldStream->finish();
|
|
|
|
|
delete oldStream;
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-07 12:19:27 -05:00
|
|
|
struct BlockMatch {
|
|
|
|
|
size_t orig, block;
|
|
|
|
|
unsigned int len;
|
|
|
|
|
bool done;
|
|
|
|
|
BlockMatch(size_t o, size_t b, unsigned int l):
|
|
|
|
|
orig(o), block(b), len(l), done(false) {}
|
|
|
|
|
BlockMatch():
|
|
|
|
|
orig(0), block(0), len(0), done(false) {}
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-12 04:55:40 -05:00
|
|
|
struct MatchBenefit {
|
|
|
|
|
size_t index;
|
|
|
|
|
int benefit;
|
|
|
|
|
unsigned int len;
|
|
|
|
|
MatchBenefit(size_t i, int b, unsigned int l):
|
|
|
|
|
index(i), benefit(b), len(l) {}
|
|
|
|
|
MatchBenefit():
|
|
|
|
|
index(0), benefit(0), len(0) {}
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-07 12:19:27 -05:00
|
|
|
#define OVERLAPS(a1,a2,b1,b2) ((b1)<(a2) && (b2)>(a1))
|
|
|
|
|
|
2025-04-11 15:04:59 -05:00
|
|
|
#define MIN_MATCH_SIZE 32
|
2025-04-07 19:05:17 -05:00
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
SafeWriter* findSubBlocks(SafeWriter* stream, std::vector<SafeWriter*>& subBlocks, unsigned char* speedDial, DivCSProgress* progress) {
|
2025-04-04 19:28:29 -05:00
|
|
|
unsigned char* buf=stream->getFinalBuf();
|
2025-04-07 19:05:17 -05:00
|
|
|
size_t matchSize=MIN_MATCH_SIZE;
|
2025-04-07 12:19:27 -05:00
|
|
|
std::vector<BlockMatch> matches;
|
2025-04-12 15:10:42 -05:00
|
|
|
std::vector<BlockMatch> workMatches;
|
2025-04-12 04:55:40 -05:00
|
|
|
std::vector<size_t> origs;
|
2025-04-12 15:10:42 -05:00
|
|
|
MatchBenefit bestBenefit;
|
2025-04-07 12:19:27 -05:00
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
matches.clear();
|
|
|
|
|
|
2025-04-14 14:42:15 -05:00
|
|
|
if (progress!=NULL) {
|
|
|
|
|
progress->findTotal=stream->size();
|
|
|
|
|
progress->optStage=0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
// fast match algorithm
|
|
|
|
|
// search for small matches, and then find bigger ones
|
|
|
|
|
logD("finding possible matches");
|
|
|
|
|
for (size_t i=0; i<stream->size(); i+=8) {
|
2025-04-14 14:42:15 -05:00
|
|
|
if (!(i&2047)) {
|
|
|
|
|
if (progress!=NULL) progress->findCurrent=i;
|
|
|
|
|
}
|
2025-04-12 04:55:40 -05:00
|
|
|
bool storedOrig=false;
|
2025-04-11 20:21:46 -05:00
|
|
|
for (size_t j=i+matchSize; j<stream->size(); j+=8) {
|
|
|
|
|
if (memcmp(&buf[i],&buf[j],matchSize)==0) {
|
2025-04-12 04:55:40 -05:00
|
|
|
if (!storedOrig) {
|
|
|
|
|
// store index to the first match somewhere else for the sake of speed
|
|
|
|
|
origs.push_back(matches.size());
|
|
|
|
|
storedOrig=true;
|
|
|
|
|
}
|
2025-04-11 20:21:46 -05:00
|
|
|
// store this match for later
|
|
|
|
|
matches.push_back(BlockMatch(i,j,matchSize));
|
2025-04-07 12:19:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-11 20:21:46 -05:00
|
|
|
}
|
2025-04-07 12:19:27 -05:00
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
logD("%d candidates",(int)matches.size());
|
2025-04-12 04:55:40 -05:00
|
|
|
logD("%d origs",(int)origs.size());
|
2025-04-07 12:19:27 -05:00
|
|
|
|
2025-04-14 13:42:15 -05:00
|
|
|
if (progress!=NULL) {
|
|
|
|
|
if ((int)matches.size()>progress->optTotal) progress->optTotal=matches.size();
|
|
|
|
|
progress->optCurrent=matches.size();
|
2025-04-14 14:42:15 -05:00
|
|
|
progress->origCount=origs.size();
|
|
|
|
|
progress->findCurrent=stream->size();
|
|
|
|
|
progress->optStage=1;
|
2025-04-14 13:42:15 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
// quit if there isn't anything
|
|
|
|
|
if (matches.empty()) return stream;
|
2025-04-09 06:21:08 -05:00
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
// search for bigger matches
|
|
|
|
|
for (size_t i=0; i<matches.size(); i++) {
|
2025-04-14 14:42:15 -05:00
|
|
|
if ((i&8191)==0) {
|
|
|
|
|
logV("match %d of %d",i,(int)matches.size());
|
|
|
|
|
}
|
|
|
|
|
if ((i&1023)==0) {
|
|
|
|
|
if (progress!=NULL) progress->expandCurrent=i;
|
|
|
|
|
}
|
2025-04-11 20:21:46 -05:00
|
|
|
BlockMatch& b=matches[i];
|
2025-04-09 06:21:08 -05:00
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
size_t finalLen=b.len;
|
|
|
|
|
size_t origPos=b.orig+b.len;
|
|
|
|
|
size_t blockPos=b.block+b.len;
|
|
|
|
|
while (true) {
|
2025-04-14 19:25:25 -05:00
|
|
|
// origPos is guaranteed to be before blockPos
|
|
|
|
|
if (blockPos>=stream->size()) {
|
2025-04-11 20:21:46 -05:00
|
|
|
break;
|
2025-04-09 06:21:08 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
if (buf[origPos]!=buf[blockPos]) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
origPos++;
|
|
|
|
|
blockPos++;
|
|
|
|
|
finalLen++;
|
2025-04-09 06:21:08 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
finalLen&=~7;
|
|
|
|
|
b.len=finalLen;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 14:42:15 -05:00
|
|
|
if (progress!=NULL) {
|
|
|
|
|
progress->expandCurrent=matches.size();
|
|
|
|
|
progress->optStage=2;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-12 04:55:40 -05:00
|
|
|
// new code MAN... WHY...
|
|
|
|
|
// basically the workflow should be:
|
|
|
|
|
// - test every block position
|
|
|
|
|
// - test every length from MIN_MATCH_SIZE to largest length
|
|
|
|
|
// - check for overlap, bad matches and all of that
|
|
|
|
|
// - for bad matches, fortunately we can use length for a speed-up... but first make it right
|
|
|
|
|
// - add weighted benefit to a list (DEBUG..... remove once it's stable)
|
|
|
|
|
// - pick largest benefit from list
|
|
|
|
|
// - make sub-blocks!!!
|
2025-04-14 05:09:20 -05:00
|
|
|
logD("testing %d match groups for benefit",(int)origs.size());
|
2025-04-14 14:42:15 -05:00
|
|
|
size_t origIndex=0;
|
2025-04-14 17:48:16 -05:00
|
|
|
for (size_t i=0; i<origs.size(); i++) {
|
|
|
|
|
size_t begin=origs[i];
|
|
|
|
|
size_t end=(i+1<origs.size())?origs[i+1]:matches.size();
|
2025-04-12 04:55:40 -05:00
|
|
|
size_t minSize=MIN_MATCH_SIZE;
|
|
|
|
|
std::vector<BlockMatch> testLenMatches;
|
|
|
|
|
|
2025-04-14 14:42:15 -05:00
|
|
|
if (progress!=NULL) progress->origCurrent=origIndex;
|
|
|
|
|
|
|
|
|
|
origIndex++;
|
|
|
|
|
|
2025-04-14 17:48:16 -05:00
|
|
|
if (!(i&255)) logV("orig %d of %d",(int)i,(int)origs.size());
|
2025-04-12 04:55:40 -05:00
|
|
|
|
|
|
|
|
// test all lengths
|
2025-04-14 17:48:16 -05:00
|
|
|
for (size_t len=minSize; true; len+=8) {
|
2025-04-12 04:55:40 -05:00
|
|
|
testLenMatches.clear();
|
|
|
|
|
// filter matches
|
2025-04-14 17:48:16 -05:00
|
|
|
for (size_t _k=begin; _k<end; _k++) {
|
|
|
|
|
BlockMatch& k=matches[_k];
|
2025-04-12 04:55:40 -05:00
|
|
|
// match length shall be greater than or equal to current length
|
|
|
|
|
if (len>k.len) continue;
|
|
|
|
|
|
|
|
|
|
// check for bad matches, which include:
|
|
|
|
|
// - match overlapping with itself
|
|
|
|
|
// - block only consisting of calls
|
|
|
|
|
// - block containing a ret, jmp or stop
|
|
|
|
|
|
|
|
|
|
// 1. self-overlapping
|
|
|
|
|
if (OVERLAPS(k.orig,k.orig+len,k.block,k.block+len)) continue;
|
|
|
|
|
|
|
|
|
|
// 2. only calls and jmp/ret/stop
|
2025-04-12 20:15:17 -05:00
|
|
|
bool metCriteria=true;
|
2025-04-12 04:55:40 -05:00
|
|
|
for (size_t l=k.orig; l<k.orig+len; l+=8) {
|
2025-04-15 19:58:55 -05:00
|
|
|
if (buf[l]==0xd4 || buf[l]==0xd5) {
|
2025-04-12 20:15:17 -05:00
|
|
|
metCriteria=false;
|
2025-04-12 04:55:40 -05:00
|
|
|
break;
|
|
|
|
|
}
|
2025-04-11 20:21:46 -05:00
|
|
|
}
|
2025-04-12 04:55:40 -05:00
|
|
|
if (!metCriteria) continue;
|
|
|
|
|
|
|
|
|
|
// 3. jmp/ret/stop
|
|
|
|
|
for (size_t l=k.orig; l<k.orig+len; l+=8) {
|
2025-04-15 19:58:55 -05:00
|
|
|
if (buf[l]==0xd9 || buf[l]==0xda || buf[l]==0xdf) {
|
2025-04-12 04:55:40 -05:00
|
|
|
metCriteria=false;
|
2025-04-07 13:17:27 -05:00
|
|
|
break;
|
|
|
|
|
}
|
2025-04-07 12:19:27 -05:00
|
|
|
}
|
2025-04-12 04:55:40 -05:00
|
|
|
if (!metCriteria) continue;
|
|
|
|
|
|
|
|
|
|
// all criteria met
|
|
|
|
|
testLenMatches.push_back(k);
|
2025-04-11 20:21:46 -05:00
|
|
|
}
|
2025-04-07 12:19:27 -05:00
|
|
|
|
2025-04-14 16:29:57 -05:00
|
|
|
// get out if no further matches (trying with bigger sizes is guaranteed to fail)
|
|
|
|
|
if (testLenMatches.empty()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-04-11 20:21:46 -05:00
|
|
|
|
2025-04-12 15:10:42 -05:00
|
|
|
// check for overlapping matches
|
|
|
|
|
size_t overlapPos=testLenMatches[0].orig;
|
|
|
|
|
size_t validCount=0;
|
|
|
|
|
for (BlockMatch& k: testLenMatches) {
|
|
|
|
|
//logV("test %d with %d",(int)overlapPos,(int)k.block);
|
|
|
|
|
if (OVERLAPS(overlapPos,overlapPos+len,k.block,k.block+len)) {
|
|
|
|
|
k.done=true;
|
|
|
|
|
//logW("overlap");
|
|
|
|
|
} else {
|
|
|
|
|
validCount++;
|
|
|
|
|
}
|
|
|
|
|
overlapPos=k.block;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-12 04:55:40 -05:00
|
|
|
// calculate (weighted) benefit
|
|
|
|
|
const int blockSize=estimateBlockSize(&buf[testLenMatches[0].orig],len,speedDial);
|
2025-04-12 15:10:42 -05:00
|
|
|
const int gains=((blockSize-3)*validCount)-4;
|
2025-04-12 04:55:40 -05:00
|
|
|
int finalBenefit=gains*2+len*3;
|
|
|
|
|
if (gains<1) finalBenefit=-1;
|
2025-04-11 20:21:46 -05:00
|
|
|
|
2025-04-12 15:10:42 -05:00
|
|
|
// check whether this set of matches has greater benefit
|
|
|
|
|
if (finalBenefit>bestBenefit.benefit) {
|
|
|
|
|
//logD("- %x (%d): %d = %d",(int)i,(int)len,(int)testLenMatches.size(),finalBenefit);
|
2025-04-14 17:48:16 -05:00
|
|
|
bestBenefit=MatchBenefit(begin,finalBenefit,len);
|
2025-04-12 15:10:42 -05:00
|
|
|
// copy matches so we don't have to select them later
|
|
|
|
|
workMatches=testLenMatches;
|
2025-04-11 20:21:46 -05:00
|
|
|
}
|
2025-04-07 20:44:25 -05:00
|
|
|
}
|
2025-04-11 20:21:46 -05:00
|
|
|
}
|
2025-04-11 15:04:59 -05:00
|
|
|
|
2025-04-12 15:10:42 -05:00
|
|
|
// quit if there isn't benefit
|
|
|
|
|
if (bestBenefit.benefit<1) return stream;
|
2025-04-12 04:55:40 -05:00
|
|
|
|
2025-04-12 15:10:42 -05:00
|
|
|
// quit if there's nothing to work on
|
|
|
|
|
if (workMatches.empty()) return stream;
|
2025-04-11 15:04:59 -05:00
|
|
|
|
2025-04-12 15:10:42 -05:00
|
|
|
// pick best benefit
|
2025-04-12 04:55:40 -05:00
|
|
|
logI("BEST BENEFIT: %d in %x with size %u",bestBenefit.benefit,(int)bestBenefit.index,bestBenefit.len);
|
2025-04-11 15:04:59 -05:00
|
|
|
|
2025-04-12 04:55:40 -05:00
|
|
|
// work on matches with this benefit
|
2025-04-12 15:10:42 -05:00
|
|
|
size_t bestOrig=matches[bestBenefit.index].orig;
|
2025-04-11 20:21:46 -05:00
|
|
|
logI("match count %d",(int)workMatches.size());
|
|
|
|
|
|
2025-04-14 14:42:15 -05:00
|
|
|
if (progress!=NULL) {
|
|
|
|
|
progress->optStage=3;
|
|
|
|
|
progress->origCurrent=origs.size();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-12 15:10:42 -05:00
|
|
|
// make sub-block
|
|
|
|
|
size_t subBlockID=subBlocks.size();
|
|
|
|
|
logV("new sub-block %d",(int)subBlockID);
|
|
|
|
|
|
2025-04-12 20:15:17 -05:00
|
|
|
assert(!(bestOrig&7));
|
|
|
|
|
|
2025-04-12 15:10:42 -05:00
|
|
|
// isolate this sub-block
|
|
|
|
|
SafeWriter* newBlock=new SafeWriter;
|
|
|
|
|
newBlock->init();
|
|
|
|
|
newBlock->write(&buf[bestOrig],bestBenefit.len);
|
2025-04-15 19:58:55 -05:00
|
|
|
newBlock->writeC(0xd9); // ret
|
2025-04-12 15:10:42 -05:00
|
|
|
// padding
|
|
|
|
|
newBlock->writeC(0);
|
|
|
|
|
newBlock->writeC(0);
|
|
|
|
|
newBlock->writeC(0);
|
|
|
|
|
newBlock->writeC(0);
|
|
|
|
|
newBlock->writeC(0);
|
|
|
|
|
newBlock->writeC(0);
|
|
|
|
|
newBlock->writeC(0);
|
|
|
|
|
subBlocks.push_back(newBlock);
|
|
|
|
|
|
|
|
|
|
// insert call on the original block
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[bestOrig]=0xd4;
|
2025-04-12 15:10:42 -05:00
|
|
|
buf[bestOrig+1]=subBlockID&0xff;
|
|
|
|
|
buf[bestOrig+2]=(subBlockID>>8)&0xff;
|
|
|
|
|
buf[bestOrig+3]=(subBlockID>>16)&0xff;
|
|
|
|
|
buf[bestOrig+4]=(subBlockID>>24)&0xff;
|
|
|
|
|
buf[bestOrig+5]=0;
|
|
|
|
|
buf[bestOrig+6]=0;
|
|
|
|
|
buf[bestOrig+7]=0;
|
|
|
|
|
|
|
|
|
|
// replace the rest with nop
|
|
|
|
|
for (size_t j=bestOrig+8; j<bestOrig+bestBenefit.len; j++) {
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[j]=0xd1;
|
2025-04-12 04:55:40 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-12 15:10:42 -05:00
|
|
|
// set matches to this sub-block
|
2025-04-11 20:21:46 -05:00
|
|
|
for (BlockMatch& i: workMatches) {
|
2025-04-12 15:10:42 -05:00
|
|
|
// skip invalid matches
|
2025-04-11 20:21:46 -05:00
|
|
|
if (i.done) continue;
|
|
|
|
|
|
2025-04-12 20:15:17 -05:00
|
|
|
assert(!(i.block&7));
|
|
|
|
|
|
2025-04-12 15:10:42 -05:00
|
|
|
// set match to this sub-block
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[i.block]=0xd4;
|
2025-04-11 20:21:46 -05:00
|
|
|
buf[i.block+1]=subBlockID&0xff;
|
|
|
|
|
buf[i.block+2]=(subBlockID>>8)&0xff;
|
|
|
|
|
buf[i.block+3]=(subBlockID>>16)&0xff;
|
|
|
|
|
buf[i.block+4]=(subBlockID>>24)&0xff;
|
|
|
|
|
buf[i.block+5]=0;
|
|
|
|
|
buf[i.block+6]=0;
|
|
|
|
|
buf[i.block+7]=0;
|
|
|
|
|
|
|
|
|
|
// replace the rest with nop
|
2025-04-12 20:15:17 -05:00
|
|
|
for (size_t j=i.block+8; j<i.block+bestBenefit.len; j++) {
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[j]=0xd1;
|
2025-04-07 20:44:25 -05:00
|
|
|
}
|
2025-04-11 20:21:46 -05:00
|
|
|
}
|
2025-04-07 13:17:27 -05:00
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
logV("done!");
|
2025-04-07 13:17:27 -05:00
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
// remove nop's
|
|
|
|
|
stream=stripNops(stream);
|
|
|
|
|
buf=stream->getFinalBuf();
|
2025-04-07 13:17:27 -05:00
|
|
|
|
2025-04-04 19:28:29 -05:00
|
|
|
return stream;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-07 04:33:28 -05:00
|
|
|
SafeWriter* packStream(SafeWriter* s, unsigned char* speedDial) {
|
|
|
|
|
std::unordered_map<unsigned int,unsigned int> addrTable;
|
|
|
|
|
SafeWriter* oldStream=s;
|
|
|
|
|
unsigned char* buf=oldStream->getFinalBuf();
|
|
|
|
|
s=new SafeWriter;
|
|
|
|
|
s->init();
|
|
|
|
|
|
|
|
|
|
// prepare address map
|
|
|
|
|
size_t addr=0;
|
|
|
|
|
for (size_t i=0; i<oldStream->size(); i+=8) {
|
|
|
|
|
addrTable[i]=addr;
|
|
|
|
|
addr+=getInsLength(buf[i],_EXT(buf,i,oldStream->size()),speedDial);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// translate addresses and write stream
|
|
|
|
|
for (size_t i=0; i<oldStream->size(); i+=8) {
|
|
|
|
|
int insLen=getInsLength(buf[i],_EXT(buf,i,oldStream->size()),speedDial);
|
|
|
|
|
if (insLen<1) {
|
|
|
|
|
logE("INS %x NOT IMPLEMENTED...",buf[i]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
switch (buf[i]) {
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd5: { // calli
|
2025-04-07 04:33:28 -05:00
|
|
|
unsigned int addr=buf[i+1]|(buf[i+2]<<8)|(buf[i+3]<<16)|(buf[i+4]<<24);
|
|
|
|
|
try {
|
|
|
|
|
addr=addrTable[addr];
|
2025-04-09 03:53:17 -05:00
|
|
|
// check whether we have sufficient room to turn this into a 16-bit call
|
|
|
|
|
if (addr<0xff00) {
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[i]=0xd8;
|
2025-04-09 03:53:17 -05:00
|
|
|
buf[i+1]=addr&0xff;
|
|
|
|
|
buf[i+2]=(addr>>8)&0xff;
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[i+3]=0xd1;
|
|
|
|
|
buf[i+4]=0xd1;
|
2025-04-12 04:55:40 -05:00
|
|
|
} else {
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[i]=0xd5;
|
2025-04-09 03:53:17 -05:00
|
|
|
buf[i+1]=addr&0xff;
|
|
|
|
|
buf[i+2]=(addr>>8)&0xff;
|
|
|
|
|
buf[i+3]=(addr>>16)&0xff;
|
|
|
|
|
buf[i+4]=(addr>>24)&0xff;
|
2025-04-12 04:55:40 -05:00
|
|
|
}
|
2025-04-07 04:33:28 -05:00
|
|
|
} catch (std::out_of_range& e) {
|
|
|
|
|
logW("address %x is not mappable!",addr);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xda: { // jmp
|
2025-04-09 03:53:17 -05:00
|
|
|
unsigned int addr=buf[i+1]|(buf[i+2]<<8)|(buf[i+3]<<16)|(buf[i+4]<<24);
|
2025-04-07 04:33:28 -05:00
|
|
|
try {
|
|
|
|
|
addr=addrTable[addr];
|
|
|
|
|
buf[i+1]=addr&0xff;
|
|
|
|
|
buf[i+2]=(addr>>8)&0xff;
|
2025-04-09 03:53:17 -05:00
|
|
|
buf[i+3]=(addr>>16)&0xff;
|
|
|
|
|
buf[i+4]=(addr>>24)&0xff;
|
2025-04-07 04:33:28 -05:00
|
|
|
} catch (std::out_of_range& e) {
|
|
|
|
|
logW("address %x is not mappable!",addr);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-04-15 19:58:55 -05:00
|
|
|
case 0xd8: { // call
|
2025-04-09 03:53:17 -05:00
|
|
|
logW("16-bit call should NEVER be generated. aborting!");
|
|
|
|
|
abort();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-04-07 04:33:28 -05:00
|
|
|
}
|
|
|
|
|
s->write(&buf[i],insLen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
oldStream->finish();
|
|
|
|
|
delete oldStream;
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-13 20:42:15 -05:00
|
|
|
SafeWriter* DivEngine::saveCommand(DivCSProgress* progress, DivCSOptions options) {
|
2023-08-21 14:49:31 -05:00
|
|
|
stop();
|
|
|
|
|
repeatPattern=false;
|
|
|
|
|
shallStop=false;
|
|
|
|
|
setOrder(0);
|
|
|
|
|
BUSY_BEGIN_SOFT;
|
|
|
|
|
// determine loop point
|
|
|
|
|
int loopOrder=0;
|
|
|
|
|
int loopRow=0;
|
|
|
|
|
int loopEnd=0;
|
|
|
|
|
walkSong(loopOrder,loopRow,loopEnd);
|
|
|
|
|
logI("loop point: %d %d",loopOrder,loopRow);
|
|
|
|
|
|
|
|
|
|
int cmdPopularity[256];
|
|
|
|
|
int delayPopularity[256];
|
|
|
|
|
|
|
|
|
|
int sortedCmdPopularity[16];
|
|
|
|
|
int sortedDelayPopularity[16];
|
|
|
|
|
unsigned char sortedCmd[16];
|
|
|
|
|
unsigned char sortedDelay[16];
|
|
|
|
|
|
2025-04-08 19:52:26 -05:00
|
|
|
SafeWriter* globalStream;
|
2023-08-21 14:49:31 -05:00
|
|
|
SafeWriter* chanStream[DIV_MAX_CHANS];
|
|
|
|
|
unsigned int chanStreamOff[DIV_MAX_CHANS];
|
2025-04-21 03:26:05 -05:00
|
|
|
unsigned int chanStackSize[DIV_MAX_CHANS];
|
2025-04-03 05:09:40 -05:00
|
|
|
std::vector<size_t> tickPos[DIV_MAX_CHANS];
|
|
|
|
|
int loopTick=-1;
|
2023-08-21 14:49:31 -05:00
|
|
|
|
|
|
|
|
memset(cmdPopularity,0,256*sizeof(int));
|
|
|
|
|
memset(delayPopularity,0,256*sizeof(int));
|
|
|
|
|
memset(chanStream,0,DIV_MAX_CHANS*sizeof(void*));
|
|
|
|
|
memset(chanStreamOff,0,DIV_MAX_CHANS*sizeof(unsigned int));
|
2025-04-21 03:26:05 -05:00
|
|
|
memset(chanStackSize,0,DIV_MAX_CHANS*sizeof(unsigned int));
|
2023-08-21 14:49:31 -05:00
|
|
|
memset(sortedCmdPopularity,0,16*sizeof(int));
|
|
|
|
|
memset(sortedDelayPopularity,0,16*sizeof(int));
|
|
|
|
|
memset(sortedCmd,0,16);
|
|
|
|
|
memset(sortedDelay,0,16);
|
|
|
|
|
|
|
|
|
|
SafeWriter* w=new SafeWriter;
|
|
|
|
|
w->init();
|
|
|
|
|
|
2025-04-08 19:52:26 -05:00
|
|
|
globalStream=new SafeWriter;
|
|
|
|
|
globalStream->init();
|
|
|
|
|
|
2023-08-21 14:49:31 -05:00
|
|
|
// write header
|
2024-03-09 18:20:17 -05:00
|
|
|
w->write("FCS",4);
|
2025-04-13 20:42:15 -05:00
|
|
|
w->writeS(chans);
|
|
|
|
|
// flags
|
|
|
|
|
w->writeC((options.longPointers?1:0)|(options.bigEndian?2:0));
|
|
|
|
|
// reserved
|
|
|
|
|
w->writeC(0);
|
|
|
|
|
// preset delays and speed dial
|
|
|
|
|
for (int i=0; i<32; i++) {
|
|
|
|
|
w->writeC(0);
|
|
|
|
|
}
|
2024-03-09 18:20:17 -05:00
|
|
|
// offsets
|
|
|
|
|
for (int i=0; i<chans; i++) {
|
|
|
|
|
chanStream[i]=new SafeWriter;
|
|
|
|
|
chanStream[i]->init();
|
2025-04-13 20:42:15 -05:00
|
|
|
if (options.longPointers) {
|
|
|
|
|
w->writeI(0);
|
|
|
|
|
} else {
|
|
|
|
|
w->writeS(0);
|
|
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
}
|
2025-04-21 03:26:05 -05:00
|
|
|
// max stack sizes
|
|
|
|
|
for (int i=0; i<chans; i++) {
|
|
|
|
|
w->writeC(0);
|
|
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
|
|
|
|
|
// play the song ourselves
|
|
|
|
|
bool done=false;
|
|
|
|
|
playSub(false);
|
|
|
|
|
|
|
|
|
|
int tick=0;
|
|
|
|
|
bool oldCmdStreamEnabled=cmdStreamEnabled;
|
|
|
|
|
cmdStreamEnabled=true;
|
|
|
|
|
double curDivider=divider;
|
|
|
|
|
|
2025-04-03 17:04:34 -05:00
|
|
|
// PASS 0: play the song and log channel command streams
|
2025-04-08 19:52:26 -05:00
|
|
|
// song beginning marker
|
|
|
|
|
for (int i=0; i<chans; i++) {
|
2025-04-15 19:58:55 -05:00
|
|
|
chanStream[i]->writeC(0xd0);
|
2025-04-09 02:52:11 -05:00
|
|
|
chanStream[i]->writeC(i);
|
2025-04-08 19:52:26 -05:00
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
// padding
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
while (!done) {
|
2025-04-03 05:09:40 -05:00
|
|
|
for (int i=0; i<chans; i++) {
|
|
|
|
|
tickPos[i].push_back(chanStream[i]->tell());
|
|
|
|
|
}
|
|
|
|
|
if (loopTick==-1) {
|
|
|
|
|
if (loopOrder==curOrder && loopRow==curRow) {
|
|
|
|
|
if ((ticks-((tempoAccum+virtualTempoN)/virtualTempoD))<=0) {
|
|
|
|
|
logI("loop is on tick %d",tick);
|
|
|
|
|
loopTick=tick;
|
2025-04-08 19:52:26 -05:00
|
|
|
// loop marker
|
2025-04-03 17:04:34 -05:00
|
|
|
for (int i=0; i<chans; i++) {
|
2025-04-15 19:58:55 -05:00
|
|
|
chanStream[i]->writeC(0xd0);
|
2025-04-08 19:52:26 -05:00
|
|
|
chanStream[i]->writeC(i);
|
2025-04-09 02:52:11 -05:00
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x01);
|
2025-04-07 04:33:28 -05:00
|
|
|
// padding
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
2025-04-03 17:04:34 -05:00
|
|
|
}
|
2025-04-03 05:09:40 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
if (nextTick(false,true) || !playing) {
|
|
|
|
|
done=true;
|
2024-03-08 14:13:50 -05:00
|
|
|
break;
|
2023-08-21 14:49:31 -05:00
|
|
|
}
|
|
|
|
|
// get command stream
|
|
|
|
|
if (curDivider!=divider) {
|
|
|
|
|
curDivider=divider;
|
2025-04-15 19:58:55 -05:00
|
|
|
chanStream[0]->writeC(0xdb);
|
2024-03-09 18:20:17 -05:00
|
|
|
chanStream[0]->writeI((int)(curDivider*65536));
|
2025-04-07 04:33:28 -05:00
|
|
|
// padding
|
|
|
|
|
chanStream[0]->writeC(0x00);
|
|
|
|
|
chanStream[0]->writeC(0x00);
|
|
|
|
|
chanStream[0]->writeC(0x00);
|
2023-08-21 14:49:31 -05:00
|
|
|
}
|
|
|
|
|
for (DivCommand& i: cmdStream) {
|
|
|
|
|
switch (i.cmd) {
|
|
|
|
|
// strip away hinted/useless commands
|
|
|
|
|
case DIV_CMD_GET_VOLUME:
|
|
|
|
|
case DIV_CMD_VOLUME:
|
2025-04-05 04:33:46 -05:00
|
|
|
case DIV_CMD_PANNING:
|
2023-08-21 14:49:31 -05:00
|
|
|
case DIV_CMD_NOTE_PORTA:
|
|
|
|
|
case DIV_CMD_LEGATO:
|
|
|
|
|
case DIV_CMD_PITCH:
|
|
|
|
|
case DIV_CMD_PRE_NOTE:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2024-03-09 18:20:17 -05:00
|
|
|
cmdPopularity[i.cmd]++;
|
2025-04-13 20:42:15 -05:00
|
|
|
writeCommandValues(chanStream[i.chan],i,options.bigEndian);
|
2023-08-21 14:49:31 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cmdStream.clear();
|
2025-04-01 17:26:39 -05:00
|
|
|
for (int i=0; i<chans; i++) {
|
2025-04-15 19:58:55 -05:00
|
|
|
chanStream[i]->writeC(0xde);
|
2025-04-07 04:33:28 -05:00
|
|
|
// padding
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
2025-04-01 17:26:39 -05:00
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
tick++;
|
|
|
|
|
}
|
2025-04-03 05:09:40 -05:00
|
|
|
if (!playing || loopTick<0) {
|
|
|
|
|
for (int i=0; i<chans; i++) {
|
2025-04-15 19:58:55 -05:00
|
|
|
chanStream[i]->writeC(0xdf);
|
2025-04-07 04:33:28 -05:00
|
|
|
// padding
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
2025-04-03 05:09:40 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (int i=0; i<chans; i++) {
|
|
|
|
|
if ((int)tickPos[i].size()>loopTick) {
|
2025-04-15 19:58:55 -05:00
|
|
|
chanStream[i]->writeC(0xda);
|
2025-04-03 05:09:40 -05:00
|
|
|
chanStream[i]->writeI(tickPos[i][loopTick]);
|
|
|
|
|
logD("chan %d loop addr: %x",i,tickPos[i][loopTick]);
|
2025-04-07 04:33:28 -05:00
|
|
|
// padding
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
2025-04-03 05:09:40 -05:00
|
|
|
} else {
|
|
|
|
|
logW("chan %d unable to find loop addr!",i);
|
2025-04-15 19:58:55 -05:00
|
|
|
chanStream[i]->writeC(0xdf);
|
2025-04-07 04:33:28 -05:00
|
|
|
// padding
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
|
|
|
|
chanStream[i]->writeC(0x00);
|
2025-04-03 05:09:40 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-01 20:53:56 -05:00
|
|
|
logV("%d",tick);
|
2023-08-21 14:49:31 -05:00
|
|
|
cmdStreamEnabled=oldCmdStreamEnabled;
|
|
|
|
|
|
2025-04-05 03:19:44 -05:00
|
|
|
remainingLoops=-1;
|
|
|
|
|
playing=false;
|
|
|
|
|
freelance=false;
|
|
|
|
|
extValuePresent=false;
|
|
|
|
|
BUSY_END;
|
|
|
|
|
|
2025-04-05 19:27:44 -05:00
|
|
|
// PASS 1: optimize command calls
|
2025-04-13 20:42:15 -05:00
|
|
|
if (!options.noCmdCallOpt) {
|
2025-04-07 00:20:48 -05:00
|
|
|
// calculate command usage
|
|
|
|
|
int sortCand=-1;
|
|
|
|
|
int sortPos=0;
|
|
|
|
|
while (sortPos<16) {
|
|
|
|
|
sortCand=-1;
|
|
|
|
|
for (int i=DIV_CMD_SAMPLE_MODE; i<256; i++) {
|
|
|
|
|
if (cmdPopularity[i]) {
|
|
|
|
|
if (sortCand==-1) {
|
|
|
|
|
sortCand=i;
|
|
|
|
|
} else if (cmdPopularity[sortCand]<cmdPopularity[i]) {
|
|
|
|
|
sortCand=i;
|
|
|
|
|
}
|
2025-04-05 19:27:44 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-07 00:20:48 -05:00
|
|
|
if (sortCand==-1) break;
|
2025-04-05 19:27:44 -05:00
|
|
|
|
2025-04-07 00:20:48 -05:00
|
|
|
sortedCmdPopularity[sortPos]=cmdPopularity[sortCand];
|
|
|
|
|
sortedCmd[sortPos]=sortCand;
|
|
|
|
|
cmdPopularity[sortCand]=0;
|
|
|
|
|
sortPos++;
|
|
|
|
|
}
|
2025-04-05 19:27:44 -05:00
|
|
|
|
2025-04-07 00:20:48 -05:00
|
|
|
// set speed dial commands
|
|
|
|
|
for (int h=0; h<chans; h++) {
|
|
|
|
|
unsigned char* buf=chanStream[h]->getFinalBuf();
|
2025-04-07 04:33:28 -05:00
|
|
|
for (size_t i=0; i<chanStream[h]->size(); i+=8) {
|
2025-04-15 19:58:55 -05:00
|
|
|
if (buf[i]==0xd7) {
|
2025-04-07 00:20:48 -05:00
|
|
|
// find whether this command is in speed dial
|
|
|
|
|
for (int j=0; j<16; j++) {
|
|
|
|
|
if (buf[i+1]==sortedCmd[j]) {
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[i]=0xe0+j;
|
2025-04-07 00:20:48 -05:00
|
|
|
// move everything to the left
|
2025-04-07 04:33:28 -05:00
|
|
|
for (int k=i+2; k<(int)i+8; k++) {
|
2025-04-07 00:20:48 -05:00
|
|
|
buf[k-1]=buf[k];
|
|
|
|
|
}
|
|
|
|
|
break;
|
2025-04-06 04:24:17 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-05 19:27:44 -05:00
|
|
|
|
|
|
|
|
// PASS 2: condense delays
|
2025-04-13 20:42:15 -05:00
|
|
|
if (!options.noDelayCondense) {
|
2025-04-07 00:20:48 -05:00
|
|
|
// calculate delay usage
|
|
|
|
|
for (int h=0; h<chans; h++) {
|
|
|
|
|
unsigned char* buf=chanStream[h]->getFinalBuf();
|
|
|
|
|
int delayCount=0;
|
2025-04-07 04:33:28 -05:00
|
|
|
for (size_t i=0; i<chanStream[h]->size(); i+=8) {
|
2025-04-15 19:58:55 -05:00
|
|
|
if (buf[i]==0xde) {
|
2025-04-07 00:20:48 -05:00
|
|
|
delayCount++;
|
|
|
|
|
} else {
|
|
|
|
|
if (delayCount>1 && delayCount<=255) {
|
|
|
|
|
delayPopularity[delayCount]++;
|
|
|
|
|
}
|
|
|
|
|
delayCount=0;
|
2025-04-03 17:04:34 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
|
2025-04-07 00:20:48 -05:00
|
|
|
// preset delays
|
|
|
|
|
int sortCand=-1;
|
|
|
|
|
int sortPos=0;
|
|
|
|
|
while (sortPos<16) {
|
|
|
|
|
sortCand=-1;
|
|
|
|
|
for (int i=0; i<256; i++) {
|
|
|
|
|
if (delayPopularity[i]) {
|
|
|
|
|
if (sortCand==-1) {
|
|
|
|
|
sortCand=i;
|
|
|
|
|
} else if (delayPopularity[sortCand]<delayPopularity[i]) {
|
|
|
|
|
sortCand=i;
|
|
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-07 00:20:48 -05:00
|
|
|
if (sortCand==-1) break;
|
2023-08-21 14:49:31 -05:00
|
|
|
|
2025-04-07 00:20:48 -05:00
|
|
|
sortedDelayPopularity[sortPos]=delayPopularity[sortCand];
|
|
|
|
|
sortedDelay[sortPos]=sortCand;
|
|
|
|
|
delayPopularity[sortCand]=0;
|
|
|
|
|
sortPos++;
|
|
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
|
2025-04-07 00:20:48 -05:00
|
|
|
// condense delays
|
|
|
|
|
for (int h=0; h<chans; h++) {
|
|
|
|
|
unsigned char* buf=chanStream[h]->getFinalBuf();
|
|
|
|
|
int delayPos=-1;
|
|
|
|
|
int delayCount=0;
|
|
|
|
|
int delayLast=0;
|
2025-04-07 04:33:28 -05:00
|
|
|
for (size_t i=0; i<chanStream[h]->size(); i+=8) {
|
2025-04-15 19:58:55 -05:00
|
|
|
if (buf[i]==0xde) {
|
2025-04-07 00:20:48 -05:00
|
|
|
if (delayPos==-1) delayPos=i;
|
|
|
|
|
delayCount++;
|
|
|
|
|
delayLast=i;
|
|
|
|
|
} else {
|
|
|
|
|
// finish the last delay if any
|
|
|
|
|
if (delayPos!=-1) {
|
|
|
|
|
if (delayCount>1) {
|
|
|
|
|
if (delayLast<delayPos) {
|
|
|
|
|
logE("delayLast<delayPos! %d<%d",delayLast,delayPos);
|
2025-04-03 17:04:34 -05:00
|
|
|
} else {
|
2025-04-07 00:20:48 -05:00
|
|
|
// write condensed delay and fill the rest with nop
|
|
|
|
|
if (delayCount>255) {
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[delayPos++]=0xdc;
|
2025-04-07 00:20:48 -05:00
|
|
|
buf[delayPos++]=delayCount&0xff;
|
|
|
|
|
buf[delayPos++]=(delayCount>>8)&0xff;
|
|
|
|
|
} else {
|
|
|
|
|
bool foundShort=false;
|
|
|
|
|
for (int j=0; j<16; j++) {
|
|
|
|
|
if (sortedDelay[j]==delayCount) {
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[delayPos++]=0xf0+j;
|
2025-04-07 00:20:48 -05:00
|
|
|
foundShort=true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!foundShort) {
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[delayPos++]=0xdd;
|
2025-04-07 00:20:48 -05:00
|
|
|
buf[delayPos++]=delayCount;
|
2025-04-03 17:04:34 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-07 04:48:13 -05:00
|
|
|
// padding
|
|
|
|
|
while (delayPos&7) buf[delayPos++]=0;
|
2025-04-07 00:20:48 -05:00
|
|
|
// fill with nop
|
|
|
|
|
for (int j=delayPos; j<=delayLast; j++) {
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[j]=0xd1;
|
2025-04-03 17:04:34 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-07 00:20:48 -05:00
|
|
|
delayPos=-1;
|
|
|
|
|
delayCount=0;
|
2025-04-03 17:04:34 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 13:42:15 -05:00
|
|
|
// PASS 3: note off + one-tick wait
|
|
|
|
|
// optimize one-tick gaps sometimes used in songs
|
|
|
|
|
for (int h=0; h<chans; h++) {
|
|
|
|
|
unsigned char* buf=chanStream[h]->getFinalBuf();
|
|
|
|
|
if (chanStream[h]->size()<8) continue;
|
|
|
|
|
for (size_t i=0; i<chanStream[h]->size()-8; i+=8) {
|
|
|
|
|
// find note off
|
|
|
|
|
if (buf[i]==0xb5) {
|
|
|
|
|
// check for contiguous wait 1
|
2025-04-15 19:58:55 -05:00
|
|
|
if (buf[i+8]==0xde) {
|
2025-04-14 13:42:15 -05:00
|
|
|
// turn it into 0xf6 (note off + wait 1) and change the next one to nop
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[i]=0xd6;
|
|
|
|
|
buf[i+8]=0xd1;
|
2025-04-14 13:42:15 -05:00
|
|
|
|
|
|
|
|
// skip the next instruction
|
|
|
|
|
i+=8;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PASS 4: remove nop's
|
2025-04-03 17:04:34 -05:00
|
|
|
// this includes modifying call addresses to compensate
|
|
|
|
|
for (int h=0; h<chans; h++) {
|
2025-04-07 13:17:27 -05:00
|
|
|
chanStream[h]=stripNops(chanStream[h]);
|
2025-04-04 05:01:49 -05:00
|
|
|
}
|
2025-04-03 17:04:34 -05:00
|
|
|
|
2025-04-14 13:42:15 -05:00
|
|
|
// PASS 5: put all channels together
|
2025-04-08 19:52:26 -05:00
|
|
|
for (int i=0; i<chans; i++) {
|
2025-04-09 02:52:11 -05:00
|
|
|
chanStreamOff[i]=globalStream->tell();
|
2025-04-08 19:52:26 -05:00
|
|
|
logI("- %d: off %x size %ld",i,chanStreamOff[i],chanStream[i]->size());
|
2025-04-09 02:52:11 -05:00
|
|
|
reloc8(chanStream[i]->getFinalBuf(),chanStream[i]->size(),0,globalStream->tell());
|
2025-04-08 19:52:26 -05:00
|
|
|
globalStream->write(chanStream[i]->getFinalBuf(),chanStream[i]->size());
|
|
|
|
|
chanStream[i]->finish();
|
|
|
|
|
delete chanStream[i];
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 13:42:15 -05:00
|
|
|
// PASS 6: find sub-blocks and isolate them
|
2025-04-13 20:42:15 -05:00
|
|
|
if (!options.noSubBlock) {
|
2025-04-08 19:52:26 -05:00
|
|
|
std::vector<SafeWriter*> subBlocks;
|
|
|
|
|
size_t beforeSize=globalStream->size();
|
|
|
|
|
|
|
|
|
|
// 6 is the minimum size that can be reliably optimized
|
|
|
|
|
logI("finding sub-blocks");
|
2025-04-04 05:01:49 -05:00
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
bool haveBlocks=false;
|
|
|
|
|
subBlocks.clear();
|
|
|
|
|
// repeat until no more sub-blocks are produced
|
|
|
|
|
do {
|
|
|
|
|
logD("iteration...");
|
|
|
|
|
globalStream=findSubBlocks(globalStream,subBlocks,sortedCmd,progress);
|
|
|
|
|
|
|
|
|
|
haveBlocks=!subBlocks.empty();
|
|
|
|
|
// insert sub-blocks and resolve symbols
|
|
|
|
|
logI("%d sub-blocks total",(int)subBlocks.size());
|
|
|
|
|
std::vector<size_t> blockOff;
|
2025-04-12 04:55:40 -05:00
|
|
|
blockOff.clear();
|
2025-04-11 20:21:46 -05:00
|
|
|
globalStream->seek(0,SEEK_END);
|
|
|
|
|
for (size_t i=0; i<subBlocks.size(); i++) {
|
|
|
|
|
SafeWriter* block=subBlocks[i];
|
|
|
|
|
|
2025-04-12 20:15:17 -05:00
|
|
|
// write sub-block
|
|
|
|
|
blockOff.push_back(globalStream->tell());
|
|
|
|
|
logV("block size: %d",(int)block->size());
|
|
|
|
|
assert(!(block->size()&7));
|
|
|
|
|
globalStream->write(block->getFinalBuf(),block->size());
|
2025-04-04 19:28:29 -05:00
|
|
|
}
|
2025-04-07 00:20:48 -05:00
|
|
|
|
2025-04-11 20:21:46 -05:00
|
|
|
for (SafeWriter* block: subBlocks) {
|
|
|
|
|
block->finish();
|
|
|
|
|
delete block;
|
|
|
|
|
}
|
|
|
|
|
subBlocks.clear();
|
|
|
|
|
|
|
|
|
|
// resolve symbols
|
|
|
|
|
unsigned char* buf=globalStream->getFinalBuf();
|
|
|
|
|
for (size_t j=0; j<globalStream->size(); j+=8) {
|
2025-04-15 19:58:55 -05:00
|
|
|
if (buf[j]==0xd4) { // callsym
|
2025-04-11 20:21:46 -05:00
|
|
|
unsigned int addr=buf[j+1]|(buf[j+2]<<8)|(buf[j+3]<<16)|(buf[j+4]<<24);
|
|
|
|
|
if (addr<blockOff.size()) {
|
|
|
|
|
// turn it into call
|
|
|
|
|
addr=blockOff[addr];
|
2025-04-15 19:58:55 -05:00
|
|
|
buf[j]=0xd5;
|
2025-04-11 20:21:46 -05:00
|
|
|
buf[j+1]=addr&0xff;
|
|
|
|
|
buf[j+2]=(addr>>8)&0xff;
|
|
|
|
|
buf[j+3]=(addr>>16)&0xff;
|
|
|
|
|
buf[j+4]=(addr>>24)&0xff;
|
|
|
|
|
} else {
|
|
|
|
|
logE("requested symbol %d is out of bounds!",addr);
|
2025-04-12 20:15:17 -05:00
|
|
|
abort();
|
2025-04-11 20:21:46 -05:00
|
|
|
}
|
2025-04-04 05:01:49 -05:00
|
|
|
}
|
2025-04-03 17:04:34 -05:00
|
|
|
}
|
2025-04-11 20:21:46 -05:00
|
|
|
} while (haveBlocks);
|
2025-04-04 19:28:29 -05:00
|
|
|
|
2025-04-08 19:52:26 -05:00
|
|
|
size_t afterSize=globalStream->size();
|
|
|
|
|
logI("(before: %d - after: %d)",(int)beforeSize,(int)afterSize);
|
|
|
|
|
assert(!(globalStream->size()&7));
|
2025-04-03 17:04:34 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-14 13:42:15 -05:00
|
|
|
// PASS 7: pack stream
|
2025-04-08 19:52:26 -05:00
|
|
|
globalStream=packStream(globalStream,sortedCmd);
|
2025-04-07 04:33:28 -05:00
|
|
|
|
2025-04-14 13:42:15 -05:00
|
|
|
// PASS 8: remove nop's which may be produced by 32-bit call conversion
|
2025-04-19 18:56:19 -05:00
|
|
|
// also find new offsets
|
|
|
|
|
globalStream=stripNopsPacked(globalStream,sortedCmd,chanStreamOff);
|
2025-04-09 02:52:11 -05:00
|
|
|
|
2025-04-19 18:56:19 -05:00
|
|
|
for (int h=0; h<chans; h++) {
|
|
|
|
|
chanStreamOff[h]+=w->tell();
|
2025-04-09 02:52:11 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-13 20:42:15 -05:00
|
|
|
// write results (convert addresses to big-endian if necessary)
|
|
|
|
|
reloc(globalStream->getFinalBuf(),globalStream->size(),0,w->tell(),sortedCmd,options.bigEndian);
|
2025-04-08 19:52:26 -05:00
|
|
|
w->write(globalStream->getFinalBuf(),globalStream->size());
|
2023-08-21 14:49:31 -05:00
|
|
|
|
2025-04-21 03:26:05 -05:00
|
|
|
// calculate max stack sizes
|
|
|
|
|
for (int h=0; h<chans; h++) {
|
|
|
|
|
std::stack<unsigned int> callStack;
|
|
|
|
|
unsigned int maxStackSize=0;
|
|
|
|
|
unsigned char* buf=w->getFinalBuf();
|
|
|
|
|
bool done=false;
|
|
|
|
|
for (size_t i=chanStreamOff[h]; i<w->size();) {
|
|
|
|
|
int insLen=getInsLength(buf[i],_EXT(buf,i,w->size()),sortedCmd);
|
|
|
|
|
if (insLen<1) {
|
|
|
|
|
logE("%d: INS %x NOT IMPLEMENTED...",h,buf[i]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
switch (buf[i]) {
|
|
|
|
|
case 0xd5: { // calli
|
|
|
|
|
unsigned int addr=buf[i+1]|(buf[i+2]<<8)|(buf[i+3]<<16)|(buf[i+4]<<24);
|
|
|
|
|
callStack.push(i+insLen);
|
|
|
|
|
if (callStack.size()>maxStackSize) maxStackSize=callStack.size();
|
|
|
|
|
i=addr;
|
|
|
|
|
insLen=0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0xd8: { // call
|
|
|
|
|
unsigned short addr=buf[i+1]|(buf[i+2]<<8);
|
|
|
|
|
callStack.push(i+insLen);
|
|
|
|
|
if (callStack.size()>maxStackSize) maxStackSize=callStack.size();
|
|
|
|
|
i=addr;
|
|
|
|
|
insLen=0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0xd9: { // ret
|
|
|
|
|
if (callStack.empty()) {
|
|
|
|
|
logE("%d: trying to ret with empty stack!",h);
|
|
|
|
|
done=true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i=callStack.top();
|
|
|
|
|
insLen=0;
|
|
|
|
|
callStack.pop();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0xda: // jmp
|
|
|
|
|
case 0xdf: // stop
|
|
|
|
|
done=true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (maxStackSize>255) {
|
|
|
|
|
logE("%d: stack overflow!",h);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (done) break;
|
|
|
|
|
i+=insLen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chanStackSize[h]=maxStackSize;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-15 05:01:37 -05:00
|
|
|
globalStream->finish();
|
|
|
|
|
delete globalStream;
|
|
|
|
|
|
2025-04-13 20:42:15 -05:00
|
|
|
w->seek(40,SEEK_SET);
|
2024-03-09 18:20:17 -05:00
|
|
|
for (int i=0; i<chans; i++) {
|
2025-04-13 20:42:15 -05:00
|
|
|
if (options.longPointers) {
|
|
|
|
|
if (options.bigEndian) {
|
|
|
|
|
w->writeI_BE(chanStreamOff[i]);
|
|
|
|
|
} else {
|
|
|
|
|
w->writeI(chanStreamOff[i]);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (options.bigEndian) {
|
|
|
|
|
w->writeS_BE(chanStreamOff[i]);
|
|
|
|
|
} else {
|
|
|
|
|
w->writeS(chanStreamOff[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-09 18:20:17 -05:00
|
|
|
}
|
2023-08-21 14:49:31 -05:00
|
|
|
|
2025-04-21 03:26:05 -05:00
|
|
|
logD("maximum stack sizes:");
|
|
|
|
|
unsigned int cumulativeStackSize=0;
|
|
|
|
|
for (int i=0; i<chans; i++) {
|
|
|
|
|
w->writeC(chanStackSize[i]);
|
|
|
|
|
logD("- %d: %d",i,chanStackSize[i]);
|
|
|
|
|
cumulativeStackSize+=chanStackSize[i];
|
|
|
|
|
}
|
|
|
|
|
logD("(total stack size: %d)",cumulativeStackSize);
|
|
|
|
|
|
2024-03-09 18:20:17 -05:00
|
|
|
logD("delay popularity:");
|
2025-04-13 20:42:15 -05:00
|
|
|
w->seek(8,SEEK_SET);
|
2024-03-09 18:20:17 -05:00
|
|
|
for (int i=0; i<16; i++) {
|
|
|
|
|
w->writeC(sortedDelay[i]);
|
|
|
|
|
if (sortedDelayPopularity[i]) logD("- %d: %d",sortedDelay[i],sortedDelayPopularity[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logD("command popularity:");
|
|
|
|
|
for (int i=0; i<16; i++) {
|
|
|
|
|
w->writeC(sortedCmd[i]);
|
2025-04-06 04:24:17 -05:00
|
|
|
if (sortedCmdPopularity[i]) logD("- %s ($%.2x): %d",cmdName[sortedCmd[i]],sortedCmd[i],sortedCmdPopularity[i]);
|
2023-08-21 14:49:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return w;
|
|
|
|
|
}
|