| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "zsm.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include "../ta-log.h" |
| 23 |
#include <fmt/printf.h> |
| 24 |
|
| 25 |
/// DivZSM definitions |
| 26 |
|
| 27 |
#define ZSM_HEADER_SIZE 16 |
| 28 |
#define ZSM_VERSION 1 |
| 29 |
#define ZSM_YM_CMD 0x40 |
| 30 |
#define ZSM_DELAY_CMD 0x80 |
| 31 |
#define ZSM_YM_MAX_WRITES 63 |
| 32 |
#define ZSM_SYNC_MAX_WRITES 31 |
| 33 |
#define ZSM_DELAY_MAX 127 |
| 34 |
#define ZSM_EOF ZSM_DELAY_CMD |
| 35 |
|
| 36 |
#define ZSM_EXT ZSM_YM_CMD |
| 37 |
#define ZSM_EXT_PCM 0x00 |
| 38 |
#define ZSM_EXT_CHIP 0x40 |
| 39 |
#define ZSM_EXT_SYNC 0x80 |
| 40 |
#define ZSM_EXT_CUSTOM 0xC0 |
| 41 |
|
| 42 |
enum YM_STATE { ym_PREV, ym_NEW, ym_STATES }; |
| 43 |
enum PSG_STATE { psg_PREV, psg_NEW, psg_STATES }; |
| 44 |
|
| 45 |
class DivZSM { |
| 46 |
private: |
| 47 |
struct S_pcmInst { |
| 48 |
int geometry; |
| 49 |
unsigned int offset, length, loopPoint; |
| 50 |
bool isLooped; |
| 51 |
}; |
| 52 |
SafeWriter* w; |
| 53 |
int ymState[ym_STATES][256]; |
| 54 |
int psgState[psg_STATES][64]; |
| 55 |
int pcmRateCache; |
| 56 |
int pcmCtrlRVCache; |
| 57 |
int pcmCtrlDCCache; |
| 58 |
unsigned int pcmLoopPointCache; |
| 59 |
bool pcmIsLooped; |
| 60 |
std::vector<DivRegWrite> ymwrites; |
| 61 |
std::vector<DivRegWrite> pcmMeta; |
| 62 |
std::vector<unsigned char> pcmData; |
| 63 |
std::vector<unsigned char> pcmCache; |
| 64 |
std::vector<S_pcmInst> pcmInsts; |
| 65 |
std::vector<DivRegWrite> syncCache; |
| 66 |
int loopOffset; |
| 67 |
int numWrites; |
| 68 |
int ticks; |
| 69 |
int tickRate; |
| 70 |
int ymMask; |
| 71 |
int psgMask; |
| 72 |
bool optimize; |
| 73 |
public: |
| 74 |
DivZSM(); |
| 75 |
~DivZSM(); |
| 76 |
void init(unsigned int rate = 60); |
| 77 |
int getoffset(); |
| 78 |
void writeYM(unsigned char a, unsigned char v); |
| 79 |
void writePSG(unsigned char a, unsigned char v); |
| 80 |
void writePCM(unsigned char a, unsigned char v); |
| 81 |
void writeSync(unsigned char a, unsigned char v); |
| 82 |
void setOptimize(bool o); |
| 83 |
void tick(int numticks = 1); |
| 84 |
void setLoopPoint(); |
| 85 |
SafeWriter* finish(); |
| 86 |
private: |
| 87 |
void flushWrites(); |
| 88 |
void flushTicks(); |
| 89 |
}; |
| 90 |
|
| 91 |
/// DivZSM implementation |
| 92 |
|
| 93 |
DivZSM::DivZSM() { |
| 94 |
w=NULL; |
| 95 |
init(); |
| 96 |
} |
| 97 |
|
| 98 |
DivZSM::~DivZSM() { |
| 99 |
} |
| 100 |
|
| 101 |
void DivZSM::init(unsigned int rate) { |
| 102 |
if (w!=NULL) delete w; |
| 103 |
w=new SafeWriter; |
| 104 |
w->init(); |
| 105 |
// write default ZSM data header |
| 106 |
w->write("zm",2); // magic header |
| 107 |
w->writeC(ZSM_VERSION); |
| 108 |
// no loop offset |
| 109 |
w->writeS(0); |
| 110 |
w->writeC(0); |
| 111 |
// no PCM |
| 112 |
w->writeS(0x00); |
| 113 |
w->writeC(0x00); |
| 114 |
// FM channel mask |
| 115 |
w->writeC(0x00); |
| 116 |
// PSG channel mask |
| 117 |
w->writeS(0x00); |
| 118 |
w->writeS((unsigned short)rate); |
| 119 |
// 2 reserved bytes (set to zero) |
| 120 |
w->writeS(0x00); |
| 121 |
tickRate=rate; |
| 122 |
loopOffset=-1; |
| 123 |
numWrites=0; |
| 124 |
ticks=0; |
| 125 |
// Initialize YM/PSG states |
| 126 |
memset(&ymState,-1,sizeof(ymState)); |
| 127 |
memset(&psgState,-1,sizeof(psgState)); |
| 128 |
// Initialize PCM states |
| 129 |
pcmRateCache=-1; |
| 130 |
pcmCtrlRVCache=-1; |
| 131 |
pcmCtrlDCCache=-1; |
| 132 |
pcmIsLooped=false; |
| 133 |
pcmLoopPointCache=0; |
| 134 |
// Channel masks |
| 135 |
ymMask=0; |
| 136 |
psgMask=0; |
| 137 |
// Optimize writes |
| 138 |
optimize=true; |
| 139 |
} |
| 140 |
|
| 141 |
int DivZSM::getoffset() { |
| 142 |
return w->tell(); |
| 143 |
} |
| 144 |
|
| 145 |
void DivZSM::writeYM(unsigned char a, unsigned char v) { |
| 146 |
int lastMask=ymMask; |
| 147 |
if (a==0x19 && v>=0x80) a=0x1a; // AMD/PSD use same reg addr. store PMD as 0x1a |
| 148 |
if (a==0x08 && (v&0xf8)) ymMask|=(1<<(v&0x07)); // mark chan as in-use if keyDN |
| 149 |
if (a!=0x08) ymState[ym_NEW][a]=v; // cache the newly-written value |
| 150 |
bool writeit=false; // used to suppress spurious writes to unused channels |
| 151 |
if (a<0x20) { |
| 152 |
if (a==0x08) { |
| 153 |
// write keyUPDN messages if channel is active. |
| 154 |
writeit=(ymMask&(1<<(v&0x07)))>0; |
| 155 |
} else { |
| 156 |
// do not suppress global registers |
| 157 |
writeit=true; |
| 158 |
} |
| 159 |
} else { |
| 160 |
writeit=(ymMask&(1<<(a&0x07)))>0; // a&0x07 = chan ID for regs >=0x20 |
| 161 |
} |
| 162 |
if (lastMask!=ymMask) { |
| 163 |
// if the ymMask just changed, then the channel has become active. |
| 164 |
// This can only happen on a KeyDN event, so voice=v&0x07 |
| 165 |
// insert a keyUP just to be safe. |
| 166 |
ymwrites.push_back(DivRegWrite(0x08,v&0x07)); |
| 167 |
numWrites++; |
| 168 |
// flush the ym_NEW cached states for this channel into the ZSM.... |
| 169 |
for (int i=0x20+(v&0x07); i<=0xff; i+=8) { |
| 170 |
if (ymState[ym_NEW][i]!=ymState[ym_PREV][i]) { |
| 171 |
ymwrites.push_back(DivRegWrite(i,ymState[ym_NEW][i])); |
| 172 |
numWrites++; |
| 173 |
// ...and update the shadow |
| 174 |
ymState[ym_PREV][i]=ymState[ym_NEW][i]; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
// Handle the current write if channel is active |
| 179 |
if (writeit && ((ymState[ym_NEW][a]!=ymState[ym_PREV][a]) || a==0x08)) { |
| 180 |
// update YM shadow if not the KeyUPDN register. |
| 181 |
if (a!=8) ymState[ym_PREV][a]=ymState[ym_NEW][a]; |
| 182 |
// if reg=PMD, then change back to real register 0x19 |
| 183 |
if (a==0x1a) a=0x19; |
| 184 |
ymwrites.push_back(DivRegWrite(a,v)); |
| 185 |
numWrites++; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void DivZSM::writeSync(unsigned char a, unsigned char v) { |
| 190 |
return syncCache.push_back(DivRegWrite(a,v)); |
| 191 |
} |
| 192 |
|
| 193 |
void DivZSM::writePSG(unsigned char a, unsigned char v) { |
| 194 |
if (a>=69) { |
| 195 |
logD("ZSM: ignoring VERA PSG write a=%02x v=%02x",a,v); |
| 196 |
return; |
| 197 |
} else if (a==68) { |
| 198 |
// Sync event |
| 199 |
numWrites++; |
| 200 |
return writeSync(0x00,v); |
| 201 |
} else if (a>=64) { |
| 202 |
return writePCM(a-64,v); |
| 203 |
} |
| 204 |
if (psgState[psg_PREV][a]==v) { |
| 205 |
if (psgState[psg_NEW][a]!=v) { |
| 206 |
// NEW value is being reset to the same as PREV value |
| 207 |
// so it is no longer a new write. |
| 208 |
numWrites--; |
| 209 |
} |
| 210 |
} else { |
| 211 |
if (psgState[psg_PREV][a]==psgState[psg_NEW][a]) { |
| 212 |
// if this write changes the NEW cached value to something other |
| 213 |
// than the PREV value, then this is a new write. |
| 214 |
numWrites++; |
| 215 |
} |
| 216 |
} |
| 217 |
psgState[psg_NEW][a]=v; |
| 218 |
// mark channel as used in the psgMask if volume is set>0. |
| 219 |
if ((a&3)==2 && (v&0x3f)) psgMask|=(1<<(a>>2)); |
| 220 |
} |
| 221 |
|
| 222 |
void DivZSM::writePCM(unsigned char a, unsigned char v) { |
| 223 |
if (a==0) { // PCM Ctrl |
| 224 |
// cache the depth and channels but don't write it to the |
| 225 |
// register queue |
| 226 |
pcmCtrlDCCache=v&0x30; |
| 227 |
// save only the reset bit and volume (if it isn't a dupe) |
| 228 |
if (pcmCtrlRVCache!=(v&0x8f)) { |
| 229 |
pcmMeta.push_back(DivRegWrite(a,(v&0x8f))); |
| 230 |
pcmCtrlRVCache=v&0x8f; |
| 231 |
numWrites++; |
| 232 |
} |
| 233 |
} else if (a==1) { // PCM Rate |
| 234 |
if (pcmRateCache!=v) { |
| 235 |
pcmMeta.push_back(DivRegWrite(a,v)); |
| 236 |
pcmRateCache=v; |
| 237 |
numWrites++; |
| 238 |
} |
| 239 |
} else if (a==2) { // PCM data |
| 240 |
pcmCache.push_back(v); |
| 241 |
numWrites++; |
| 242 |
} else if (a==3) { // PCM loop point |
| 243 |
pcmLoopPointCache=(pcmLoopPointCache>>8)|(v<<16); |
| 244 |
pcmIsLooped=true; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
void DivZSM::tick(int numticks) { |
| 249 |
flushWrites(); |
| 250 |
ticks+=numticks; |
| 251 |
} |
| 252 |
|
| 253 |
void DivZSM::setLoopPoint() { |
| 254 |
tick(0); // flush any ticks+writes |
| 255 |
flushTicks(); // flush ticks incase no writes were pending |
| 256 |
logI("ZSM: loop at file offset %d bytes",w->tell()); |
| 257 |
loopOffset=w->tell(); |
| 258 |
// update the ZSM header's loop offset value |
| 259 |
w->seek(0x03,SEEK_SET); |
| 260 |
w->writeS((short)(loopOffset&0xffff)); |
| 261 |
w->writeC((unsigned char)((loopOffset>>16)&0xff)); |
| 262 |
w->seek(loopOffset,SEEK_SET); |
| 263 |
// reset the PSG shadow and write cache |
| 264 |
memset(&psgState,-1,sizeof(psgState)); |
| 265 |
// reset the PCM caches that would inhibit dupes |
| 266 |
pcmRateCache=-1; |
| 267 |
pcmCtrlRVCache=-1; |
| 268 |
// reset the YM shadow.... |
| 269 |
memset(&ymState[ym_PREV],-1,sizeof(ymState[ym_PREV])); |
| 270 |
// ... and cache (except for unused channels) |
| 271 |
memset(&ymState[ym_NEW],-1,0x20); |
| 272 |
for (int chan=0; chan<8; chan++) { |
| 273 |
// do not clear state for as-yet-unused channels |
| 274 |
if (!(ymMask&(1<<chan))) continue; |
| 275 |
// clear the state for channels in use so they match the unknown state |
| 276 |
// of the YM shadow. |
| 277 |
for (int i=0x20+chan; i<=0xff; i+=8) { |
| 278 |
ymState[ym_NEW][i]=-1; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
void DivZSM::setOptimize(bool o) { |
| 284 |
optimize=o; |
| 285 |
} |
| 286 |
|
| 287 |
SafeWriter* DivZSM::finish() { |
| 288 |
tick(0); // flush any pending writes / ticks |
| 289 |
flushTicks(); // flush ticks in case there were no writes pending |
| 290 |
w->writeC(ZSM_EOF); |
| 291 |
if (pcmInsts.size()>256) { |
| 292 |
logE("ZSM: more than the maximum number of PCM instruments exist. Skipping PCM export entirely."); |
| 293 |
pcmData.clear(); |
| 294 |
pcmInsts.clear(); |
| 295 |
} else if (pcmData.size()) { // if exists, write PCM instruments and blob to the end of file |
| 296 |
unsigned int pcmOff=w->tell(); |
| 297 |
w->writeC('P'); |
| 298 |
w->writeC('C'); |
| 299 |
w->writeC('M'); |
| 300 |
w->writeC((unsigned char)pcmInsts.size()-1); |
| 301 |
int i=0; |
| 302 |
for (S_pcmInst& inst: pcmInsts) { |
| 303 |
// write out the instruments |
| 304 |
// PCM playback location follows: |
| 305 |
// <instrument number> |
| 306 |
// <geometry (depth and channel)> |
| 307 |
// <l m h> of PCM data offset |
| 308 |
// <l m h> of length |
| 309 |
w->writeC((unsigned char)i&0xff); |
| 310 |
w->writeC((unsigned char)inst.geometry&0x30); |
| 311 |
w->writeC((unsigned char)inst.offset&0xff); |
| 312 |
w->writeC((unsigned char)(inst.offset>>8)&0xff); |
| 313 |
w->writeC((unsigned char)(inst.offset>>16)&0xff); |
| 314 |
w->writeC((unsigned char)inst.length&0xff); |
| 315 |
w->writeC((unsigned char)(inst.length>>8)&0xff); |
| 316 |
w->writeC((unsigned char)(inst.length>>16)&0xff); |
| 317 |
// Feature mask: Lxxxxxxx |
| 318 |
// L = Loop enabled |
| 319 |
w->writeC((unsigned char)inst.isLooped<<7); |
| 320 |
// Sample loop point <l m h> |
| 321 |
w->writeC((unsigned char)inst.loopPoint&0xff); |
| 322 |
w->writeC((unsigned char)(inst.loopPoint>>8)&0xff); |
| 323 |
w->writeC((unsigned char)(inst.loopPoint>>16)&0xff); |
| 324 |
// Reserved for future use |
| 325 |
w->writeS(0); |
| 326 |
w->writeS(0); |
| 327 |
i++; |
| 328 |
} |
| 329 |
for (unsigned char& c: pcmData) { |
| 330 |
w->writeC(c); |
| 331 |
} |
| 332 |
pcmData.clear(); |
| 333 |
// update PCM offset in file |
| 334 |
w->seek(0x06,SEEK_SET); |
| 335 |
w->writeC((unsigned char)pcmOff&0xff); |
| 336 |
w->writeC((unsigned char)(pcmOff>>8)&0xff); |
| 337 |
w->writeC((unsigned char)(pcmOff>>16)&0xff); |
| 338 |
} |
| 339 |
// update channel use masks. |
| 340 |
w->seek(0x09,SEEK_SET); |
| 341 |
w->writeC((unsigned char)(ymMask&0xff)); |
| 342 |
w->writeS((short)(psgMask&0xffff)); |
| 343 |
return w; |
| 344 |
} |
| 345 |
|
| 346 |
void DivZSM::flushWrites() { |
| 347 |
logD("ZSM: flushWrites.... numwrites=%d ticks=%d ymwrites=%d pcmMeta=%d pcmCache=%d pcmData=%d syncCache=%d",numWrites,ticks,ymwrites.size(),pcmMeta.size(),pcmCache.size(),pcmData.size(),syncCache.size()); |
| 348 |
if (numWrites==0) return; |
| 349 |
bool hasFlushed=false; |
| 350 |
for (unsigned char i=0; i<64; i++) { |
| 351 |
if (psgState[psg_NEW][i]==psgState[psg_PREV][i]) continue; |
| 352 |
// if optimize=true, suppress writes to PSG voices that are not audible (volume=0 or R+L=0) |
| 353 |
// ZSMKit has a feature that can benefit from having silent channels |
| 354 |
// updated, so this is something that can be toggled off or on for export |
| 355 |
if (optimize && (i&3)!=2 && (psgState[psg_NEW][(i&0x3c)+2]&0x3f)==0) continue; // vol |
| 356 |
if (optimize && (i&3)!=2 && (psgState[psg_NEW][(i&0x3c)+2]&0xc0)==0) continue; // R+L |
| 357 |
psgState[psg_PREV][i]=psgState[psg_NEW][i]; |
| 358 |
if (!hasFlushed) { |
| 359 |
flushTicks(); |
| 360 |
hasFlushed=true; |
| 361 |
} |
| 362 |
w->writeC(i); |
| 363 |
w->writeC(psgState[psg_NEW][i]); |
| 364 |
} |
| 365 |
int n=0; // n=completed YM writes. used to determine when to write the CMD byte... |
| 366 |
for (DivRegWrite& write: ymwrites) { |
| 367 |
if (!hasFlushed) { |
| 368 |
flushTicks(); |
| 369 |
hasFlushed=true; |
| 370 |
} |
| 371 |
if (n%ZSM_YM_MAX_WRITES==0) { |
| 372 |
if (ymwrites.size()-n>ZSM_YM_MAX_WRITES) { |
| 373 |
w->writeC((unsigned char)(ZSM_YM_CMD+ZSM_YM_MAX_WRITES)); |
| 374 |
logD("ZSM: YM-write: %d (%02x) [max]",ZSM_YM_MAX_WRITES,ZSM_YM_MAX_WRITES+ZSM_YM_CMD); |
| 375 |
} else { |
| 376 |
w->writeC((unsigned char)(ZSM_YM_CMD+ymwrites.size()-n)); |
| 377 |
logD("ZSM: YM-write: %d (%02x)",ymwrites.size()-n,ZSM_YM_CMD+ymwrites.size()-n); |
| 378 |
} |
| 379 |
} |
| 380 |
n++; |
| 381 |
w->writeC(write.addr); |
| 382 |
w->writeC(write.val); |
| 383 |
} |
| 384 |
ymwrites.clear(); |
| 385 |
unsigned int pcmInst=0; |
| 386 |
unsigned int pcmOff=0; |
| 387 |
unsigned int pcmLen=0; |
| 388 |
int extCmd0Len=pcmMeta.size()*2; |
| 389 |
if (pcmCache.size()) { |
| 390 |
// collapse stereo data to mono if both channels are fully identical |
| 391 |
// which cuts PCM data size in half for center-panned PCM events |
| 392 |
if (pcmCtrlDCCache&0x10) { // stereo bit is on |
| 393 |
unsigned int e; |
| 394 |
if (pcmCtrlDCCache&0x20) { // 16-bit |
| 395 |
// for 16-bit PCM data, the size must be a multiple of 4 |
| 396 |
if (pcmCache.size()%4==0) { |
| 397 |
// check for identical L+R channels |
| 398 |
for (e=0; e<pcmCache.size(); e+=4) { |
| 399 |
if (pcmCache[e]!=pcmCache[e+2] || pcmCache[e+1]!=pcmCache[e+3]) break; |
| 400 |
} |
| 401 |
if (e==pcmCache.size()) { // did not find a mismatch |
| 402 |
// collapse the data to mono 16-bit |
| 403 |
for (e=0; e<pcmCache.size()>>1; e+=2) { |
| 404 |
pcmCache[e]=pcmCache[e<<1]; |
| 405 |
pcmCache[e+1]=pcmCache[(e<<1)+1]; |
| 406 |
} |
| 407 |
pcmCache.resize(pcmCache.size()>>1); |
| 408 |
pcmCtrlDCCache&=(unsigned char)~0x10; // clear stereo bit |
| 409 |
pcmLoopPointCache>>=1; // halve the loop point |
| 410 |
} |
| 411 |
} |
| 412 |
} else { // 8-bit |
| 413 |
// for 8-bit PCM data, the size must be a multiple of 2 |
| 414 |
if (pcmCache.size()%2==0) { |
| 415 |
// check for identical L+R channels |
| 416 |
for (e=0; e<pcmCache.size(); e+=2) { |
| 417 |
if (pcmCache[e]!=pcmCache[e+1]) break; |
| 418 |
} |
| 419 |
if (e==pcmCache.size()) { // did not find a mismatch |
| 420 |
// collapse the data to mono 8-bit |
| 421 |
for (e=0; e<pcmCache.size()>>1; e++) { |
| 422 |
pcmCache[e]=pcmCache[e<<1]; |
| 423 |
} |
| 424 |
pcmCache.resize(pcmCache.size()>>1); |
| 425 |
pcmCtrlDCCache&=(unsigned char)~0x10; // clear stereo bit |
| 426 |
pcmLoopPointCache>>=1; // halve the loop point |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
// check to see if the most recent received blob matches any of the previous data |
| 432 |
// and reuse it if there is a match, otherwise append the cache to the rest of |
| 433 |
// the PCM data |
| 434 |
std::vector<unsigned char>::iterator it; |
| 435 |
it=std::search(pcmData.begin(),pcmData.end(),pcmCache.begin(),pcmCache.end()); |
| 436 |
pcmOff=std::distance(pcmData.begin(),it); |
| 437 |
pcmLen=pcmCache.size(); |
| 438 |
logD("ZSM: pcmOff: %d pcmLen: %d",pcmOff,pcmLen); |
| 439 |
if (it==pcmData.end()) { |
| 440 |
pcmData.insert(pcmData.end(),pcmCache.begin(),pcmCache.end()); |
| 441 |
} |
| 442 |
pcmCache.clear(); |
| 443 |
extCmd0Len+=2; |
| 444 |
// search for a matching PCM instrument definition |
| 445 |
for (S_pcmInst& inst: pcmInsts) { |
| 446 |
if (inst.offset==pcmOff && inst.length==pcmLen && inst.geometry==pcmCtrlDCCache && inst.isLooped==pcmIsLooped && inst.loopPoint==pcmLoopPointCache) |
| 447 |
break; |
| 448 |
pcmInst++; |
| 449 |
} |
| 450 |
if (pcmInst==pcmInsts.size()) { |
| 451 |
S_pcmInst inst; |
| 452 |
inst.geometry=pcmCtrlDCCache; |
| 453 |
inst.offset=pcmOff; |
| 454 |
inst.length=pcmLen; |
| 455 |
inst.loopPoint=pcmLoopPointCache; |
| 456 |
inst.isLooped=pcmIsLooped; |
| 457 |
pcmInsts.push_back(inst); |
| 458 |
} |
| 459 |
pcmIsLooped=false; |
| 460 |
pcmLoopPointCache=0; |
| 461 |
} |
| 462 |
if (extCmd0Len>63) { // this would be bad, but will almost certainly never happen |
| 463 |
logE("ZSM: extCmd 0 exceeded maximum length of 63: %d",extCmd0Len); |
| 464 |
extCmd0Len=0; |
| 465 |
pcmMeta.clear(); |
| 466 |
} |
| 467 |
if (extCmd0Len) { // we have some PCM events to write |
| 468 |
if (!hasFlushed) { |
| 469 |
flushTicks(); |
| 470 |
hasFlushed=true; |
| 471 |
} |
| 472 |
w->writeC(ZSM_EXT); |
| 473 |
w->writeC(ZSM_EXT_PCM|(unsigned char)extCmd0Len); |
| 474 |
for (DivRegWrite& write: pcmMeta) { |
| 475 |
w->writeC(write.addr); |
| 476 |
w->writeC(write.val); |
| 477 |
} |
| 478 |
pcmMeta.clear(); |
| 479 |
if (pcmLen) { |
| 480 |
w->writeC(0x02); // 0x02 = Instrument trigger |
| 481 |
w->writeC((unsigned char)pcmInst&0xff); |
| 482 |
} |
| 483 |
} |
| 484 |
n=0; |
| 485 |
for (DivRegWrite& write: syncCache) { |
| 486 |
if (!hasFlushed) { |
| 487 |
flushTicks(); |
| 488 |
hasFlushed=true; |
| 489 |
} |
| 490 |
if (n%ZSM_SYNC_MAX_WRITES==0) { |
| 491 |
w->writeC(ZSM_EXT); |
| 492 |
if (syncCache.size()-n>ZSM_SYNC_MAX_WRITES) { |
| 493 |
w->writeC((unsigned char)(ZSM_EXT_SYNC|(ZSM_SYNC_MAX_WRITES<<1))); |
| 494 |
} else { |
| 495 |
w->writeC((unsigned char)(ZSM_EXT_SYNC|((syncCache.size()-n)<<1))); |
| 496 |
} |
| 497 |
} |
| 498 |
n++; |
| 499 |
w->writeC(write.addr); |
| 500 |
w->writeC(write.val); |
| 501 |
} |
| 502 |
syncCache.clear(); |
| 503 |
numWrites=0; |
| 504 |
} |
| 505 |
|
| 506 |
void DivZSM::flushTicks() { |
| 507 |
while (ticks>ZSM_DELAY_MAX) { |
| 508 |
logD("ZSM: write delay %d (max)",ZSM_DELAY_MAX); |
| 509 |
w->writeC((unsigned char)(ZSM_DELAY_CMD+ZSM_DELAY_MAX)); |
| 510 |
ticks-=ZSM_DELAY_MAX; |
| 511 |
} |
| 512 |
if (ticks>0) { |
| 513 |
logD("ZSM: write delay %d",ticks); |
| 514 |
w->writeC(ZSM_DELAY_CMD+ticks); |
| 515 |
} |
| 516 |
ticks=0; |
| 517 |
} |
| 518 |
|
| 519 |
/// ZSM export |
| 520 |
|
| 521 |
void DivExportZSM::run() { |
| 522 |
// settings |
| 523 |
unsigned int zsmrate=conf.getInt("zsmrate",60); |
| 524 |
bool loop=conf.getBool("loop",true); |
| 525 |
bool optimize=conf.getBool("optimize",true); |
| 526 |
|
| 527 |
// system IDs |
| 528 |
int VERA=-1; |
| 529 |
int YM=-1; |
| 530 |
int IGNORED=0; |
| 531 |
|
| 532 |
// find indexes for YM and VERA. Ignore other systems. |
| 533 |
for (int i=0; i<e->song.systemLen; i++) { |
| 534 |
switch (e->song.system[i]) { |
| 535 |
case DIV_SYSTEM_VERA: |
| 536 |
if (VERA>=0) { |
| 537 |
IGNORED++; |
| 538 |
break; |
| 539 |
} |
| 540 |
VERA=i; |
| 541 |
logAppendf("VERA detected as chip id %d",i); |
| 542 |
break; |
| 543 |
case DIV_SYSTEM_YM2151: |
| 544 |
if (YM>=0) { |
| 545 |
IGNORED++; |
| 546 |
break; |
| 547 |
} |
| 548 |
YM=i; |
| 549 |
logAppendf("YM detected as chip id %d",i); |
| 550 |
break; |
| 551 |
default: |
| 552 |
IGNORED++; |
| 553 |
logAppendf("Ignoring chip %d systemID %d",i,(int)e->song.system[i]); |
| 554 |
break; |
| 555 |
} |
| 556 |
} |
| 557 |
if (VERA<0 && YM<0) { |
| 558 |
logAppend("ERROR: No supported systems for ZSM"); |
| 559 |
failed=true; |
| 560 |
running=false; |
| 561 |
return; |
| 562 |
} |
| 563 |
if (IGNORED>0) { |
| 564 |
logAppendf("ZSM export ignoring %d unsupported system%c",IGNORED,IGNORED>1?'s':' '); |
| 565 |
} |
| 566 |
|
| 567 |
DivZSM zsm; |
| 568 |
|
| 569 |
e->stop(); |
| 570 |
e->repeatPattern=false; |
| 571 |
e->setOrder(0); |
| 572 |
e->synchronizedSoft([&]() { |
| 573 |
double origRate=e->got.rate; |
| 574 |
e->got.rate=zsmrate&0xffff; |
| 575 |
|
| 576 |
// determine loop point |
| 577 |
int loopOrder=0; |
| 578 |
int loopRow=0; |
| 579 |
int loopEnd=0; |
| 580 |
e->walkSong(loopOrder,loopRow,loopEnd); |
| 581 |
logAppendf("loop point: %d %d",loopOrder,loopRow); |
| 582 |
|
| 583 |
zsm.init(zsmrate); |
| 584 |
|
| 585 |
// reset the playback state |
| 586 |
e->curOrder=0; |
| 587 |
e->freelance=false; |
| 588 |
e->playing=false; |
| 589 |
e->extValuePresent=false; |
| 590 |
e->remainingLoops=-1; |
| 591 |
|
| 592 |
// Prepare to write song data |
| 593 |
e->playSub(false); |
| 594 |
//size_t tickCount=0; |
| 595 |
bool done=false; |
| 596 |
bool loopNow=false; |
| 597 |
int loopPos=-1; |
| 598 |
if (VERA>=0) e->disCont[VERA].dispatch->toggleRegisterDump(true); |
| 599 |
if (YM>=0) { |
| 600 |
e->disCont[YM].dispatch->toggleRegisterDump(true); |
| 601 |
// emit LFO initialization commands |
| 602 |
zsm.writeYM(0x18,0); // freq=0 |
| 603 |
zsm.writeYM(0x19,0x7F); // AMD =7F |
| 604 |
zsm.writeYM(0x19,0xFF); // PMD =7F |
| 605 |
// TODO: incorporate the Furnace meta-command for init data and filter |
| 606 |
// out writes to otherwise-unused channels. |
| 607 |
} |
| 608 |
// Indicate the song's tuning as a sync meta-event |
| 609 |
// specified in terms of how many 1/256th semitones |
| 610 |
// the song is offset from standard A-440 tuning. |
| 611 |
// This is mainly to benefit visualizations in players |
| 612 |
// for non-standard tunings so that they can avoid |
| 613 |
// displaying the entire song held in pitch bend. |
| 614 |
// Tunings offsets that exceed a half semitone |
| 615 |
// will simply be represented in a different key |
| 616 |
// by nature of overflowing the signed char value |
| 617 |
signed char tuningoffset=(signed char)(round(3072*(log(e->song.tuning/440.0)/log(2))))&0xff; |
| 618 |
zsm.writeSync(0x01,tuningoffset); |
| 619 |
// Set optimize flag, which mainly buffers PSG writes |
| 620 |
// whenever the channel is silent |
| 621 |
zsm.setOptimize(optimize); |
| 622 |
|
| 623 |
while (!done) { |
| 624 |
if (loopPos==-1) { |
| 625 |
if (loopOrder==e->curOrder && loopRow==e->curRow && loop) |
| 626 |
loopNow=true; |
| 627 |
if (loopNow) { |
| 628 |
// If Virtual Tempo is in use, our exact loop point |
| 629 |
// might be skipped due to quantization error. |
| 630 |
// If this happens, the tick immediately following is our loop point. |
| 631 |
if (e->ticks==1 || !(loopOrder==e->curOrder && loopRow==e->curRow)) { |
| 632 |
loopPos=zsm.getoffset(); |
| 633 |
zsm.setLoopPoint(); |
| 634 |
loopNow=false; |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
if (e->nextTick() || !e->playing) { |
| 639 |
done=true; |
| 640 |
if (!loop) { |
| 641 |
for (int i=0; i<e->song.systemLen; i++) { |
| 642 |
e->disCont[i].dispatch->getRegisterWrites().clear(); |
| 643 |
} |
| 644 |
break; |
| 645 |
} |
| 646 |
if (!e->playing) { |
| 647 |
loopPos=-1; |
| 648 |
} |
| 649 |
} |
| 650 |
// get register dumps |
| 651 |
for (int j=0; j<2; j++) { |
| 652 |
int i=0; |
| 653 |
// dump YM writes first |
| 654 |
if (j==0) { |
| 655 |
if (YM<0) { |
| 656 |
continue; |
| 657 |
} else { |
| 658 |
i=YM; |
| 659 |
} |
| 660 |
} |
| 661 |
// dump VERA writes second |
| 662 |
if (j==1) { |
| 663 |
if (VERA<0) { |
| 664 |
continue; |
| 665 |
} else { |
| 666 |
i=VERA; |
| 667 |
} |
| 668 |
} |
| 669 |
std::vector<DivRegWrite>& writes=e->disCont[i].dispatch->getRegisterWrites(); |
| 670 |
if (writes.size()>0) |
| 671 |
logD("zsmOps: Writing %d messages to chip %d",writes.size(),i); |
| 672 |
for (DivRegWrite& write: writes) { |
| 673 |
if (i==YM) { |
| 674 |
if (done && write.addr==0x08 && (write.val&0x78)>0) continue; // don't process keydown on lookahead |
| 675 |
zsm.writeYM(write.addr&0xff,write.val); |
| 676 |
} |
| 677 |
if (i==VERA) { |
| 678 |
if (done && write.addr>=64) continue; // don't process any PCM or sync events on the loop lookahead |
| 679 |
zsm.writePSG(write.addr&0xff,write.val); |
| 680 |
} |
| 681 |
} |
| 682 |
writes.clear(); |
| 683 |
} |
| 684 |
|
| 685 |
// write wait |
| 686 |
int totalWait=e->cycles; |
| 687 |
if (totalWait>0 && !done) { |
| 688 |
zsm.tick(totalWait); |
| 689 |
//tickCount+=totalWait; |
| 690 |
} |
| 691 |
} |
| 692 |
// end of song |
| 693 |
|
| 694 |
// done - close out. |
| 695 |
e->got.rate=origRate; |
| 696 |
if (VERA>=0) e->disCont[VERA].dispatch->toggleRegisterDump(false); |
| 697 |
if (YM>=0) e->disCont[YM].dispatch->toggleRegisterDump(false); |
| 698 |
|
| 699 |
e->remainingLoops=-1; |
| 700 |
e->playing=false; |
| 701 |
e->freelance=false; |
| 702 |
e->extValuePresent=false; |
| 703 |
}); |
| 704 |
|
| 705 |
progress[0].amount=1.0f; |
| 706 |
|
| 707 |
logAppend("finished!"); |
| 708 |
|
| 709 |
output.push_back(DivROMExportOutput("out.zsm",zsm.finish())); |
| 710 |
running=false; |
| 711 |
} |
| 712 |
|
| 713 |
/// DivExpottZSM - FRONTEND |
| 714 |
|
| 715 |
bool DivExportZSM::go(DivEngine* eng) { |
| 716 |
progress[0].name="Generate"; |
| 717 |
progress[0].amount=0.0f; |
| 718 |
|
| 719 |
e=eng; |
| 720 |
running=true; |
| 721 |
failed=false; |
| 722 |
mustAbort=false; |
| 723 |
exportThread=new std::thread(&DivExportZSM::run,this); |
| 724 |
return true; |
| 725 |
} |
| 726 |
|
| 727 |
void DivExportZSM::wait() { |
| 728 |
if (exportThread!=NULL) { |
| 729 |
exportThread->join(); |
| 730 |
delete exportThread; |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
void DivExportZSM::abort() { |
| 735 |
mustAbort=true; |
| 736 |
wait(); |
| 737 |
} |
| 738 |
|
| 739 |
bool DivExportZSM::isRunning() { |
| 740 |
return running; |
| 741 |
} |
| 742 |
|
| 743 |
bool DivExportZSM::hasFailed() { |
| 744 |
return failed; |
| 745 |
} |
| 746 |
|
| 747 |
DivROMExportProgress DivExportZSM::getProgress(int index) { |
| 748 |
if (index<0 || index>1) return progress[1]; |
| 749 |
return progress[index]; |
| 750 |
} |