From fd2c17e4f9a9101b752d6383358ce25c9484e5e0 Mon Sep 17 00:00:00 2001 From: MooingLemur Date: Tue, 15 Aug 2023 00:16:33 -0700 Subject: [PATCH] ZSM: optimize: skip PWM write on non-pulse, change delay flush logic --- src/engine/zsm.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/engine/zsm.cpp b/src/engine/zsm.cpp index 2776379a..11602a23 100644 --- a/src/engine/zsm.cpp +++ b/src/engine/zsm.cpp @@ -133,6 +133,14 @@ void DivZSM::writePSG(unsigned char a, unsigned char v) { } else if (a>=64) { return writePCM(a-64,v); } + if (optimize) { + if ((a&3)==3 && v>64) { + // Pulse width on non-pulse waves is nonsense and wasteful + // No need to preserve state here because the next write that + // selects pulse will also set the pulse width in this register + v=v&0xc0; + } + } if (psgState[psg_PREV][a]==v) { if (psgState[psg_NEW][a]!=v) { // NEW value is being reset to the same as PREV value @@ -148,7 +156,7 @@ void DivZSM::writePSG(unsigned char a, unsigned char v) { } psgState[psg_NEW][a]=v; // mark channel as used in the psgMask if volume is set>0. - if ((a%4==2) && (v&0x3f)) psgMask|=(1<<(a>>2)); + if ((a&3)==2 && (v&0x3f)) psgMask|=(1<<(a>>2)); } void DivZSM::writePCM(unsigned char a, unsigned char v) { @@ -278,7 +286,7 @@ SafeWriter* DivZSM::finish() { void DivZSM::flushWrites() { 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()); if (numWrites==0) return; - flushTicks(); // only flush ticks if there are writes pending. + bool hasFlushed=false; for (unsigned char i=0; i<64; i++) { if (psgState[psg_NEW][i]==psgState[psg_PREV][i]) continue; // if optimize=true, suppress writes to PSG voices that are not audible (volume=0 or R+L=0) @@ -287,11 +295,19 @@ void DivZSM::flushWrites() { if (optimize && (i&3)!=2 && (psgState[psg_NEW][(i&0x3c)+2]&0x3f)==0) continue; // vol if (optimize && (i&3)!=2 && (psgState[psg_NEW][(i&0x3c)+2]&0xc0)==0) continue; // R+L psgState[psg_PREV][i]=psgState[psg_NEW][i]; + if (!hasFlushed) { + flushTicks(); + hasFlushed=true; + } w->writeC(i); w->writeC(psgState[psg_NEW][i]); } int n=0; // n=completed YM writes. used to determine when to write the CMD byte... for (DivRegWrite& write: ymwrites) { + if (!hasFlushed) { + flushTicks(); + hasFlushed=true; + } if (n%ZSM_YM_MAX_WRITES==0) { if (ymwrites.size()-n>ZSM_YM_MAX_WRITES) { w->writeC((unsigned char)(ZSM_YM_CMD+ZSM_YM_MAX_WRITES));