Added WriteUC wrapper to safewrite for unsigned char.

This commit is contained in:
ZeroByteOrg 2022-06-07 22:08:04 -05:00
parent 6a64df6c68
commit 8ed02530f6
3 changed files with 19 additions and 14 deletions

View File

@ -77,6 +77,10 @@ int SafeWriter::writeC(signed char val) {
return write(&val,1); return write(&val,1);
} }
int SafeWriter::writeUC(unsigned char val) {
return write(&val,1);
}
int SafeWriter::writeS(short val) { int SafeWriter::writeS(short val) {
return write(&val,2); return write(&val,2);
} }
@ -139,4 +143,4 @@ void SafeWriter::finish() {
delete[] buf; delete[] buf;
buf=NULL; buf=NULL;
operative=false; operative=false;
} }

View File

@ -45,6 +45,7 @@ class SafeWriter {
int write(const void* what, size_t count); int write(const void* what, size_t count);
int writeC(signed char val); int writeC(signed char val);
int writeUC(unsigned char val);
int writeS(short val); int writeS(short val);
int writeS_BE(short val); int writeS_BE(short val);
int writeI(int val); int writeI(int val);

View File

@ -36,7 +36,7 @@ void ZSM::init(unsigned int rate) {
w->init(); w->init();
// write default ZSM data header // write default ZSM data header
w->write("zm",2); // magic header w->write("zm",2); // magic header
w->writeC(ZSM_VERSION); w->writeUC(ZSM_VERSION);
// no loop offset // no loop offset
w->writeS(0); w->writeS(0);
w->writeC(0); w->writeC(0);
@ -146,7 +146,7 @@ void ZSM::setLoopPoint() {
//update the ZSM header's loop offset value //update the ZSM header's loop offset value
w->seek(0x03,SEEK_SET); w->seek(0x03,SEEK_SET);
w->writeS((short)(loopOffset&0xffff)); w->writeS((short)(loopOffset&0xffff));
w->writeC((short)((loopOffset>>16)&0xff)); w->writeUC((short)((loopOffset>>16)&0xff));
w->seek(loopOffset,SEEK_SET); w->seek(loopOffset,SEEK_SET);
// reset the PSG shadow and write cache // reset the PSG shadow and write cache
memset(&psgState,-1,sizeof(psgState)); memset(&psgState,-1,sizeof(psgState));
@ -166,10 +166,10 @@ void ZSM::setLoopPoint() {
SafeWriter* ZSM::finish() { SafeWriter* ZSM::finish() {
tick(0); // flush any pending writes / ticks tick(0); // flush any pending writes / ticks
flushTicks(); // flush ticks in case there were no writes pending flushTicks(); // flush ticks in case there were no writes pending
w->writeC(ZSM_EOF); w->writeUC(ZSM_EOF);
// update channel use masks. // update channel use masks.
w->seek(0x09,SEEK_SET); w->seek(0x09,SEEK_SET);
w->writeC((short)(ymMask & 0xff)); w->writeUC((unsigned char)(ymMask & 0xff));
w->writeS((short)(psgMask & 0xffff)); w->writeS((short)(psgMask & 0xffff));
// todo: put PCM offset/data writes here once defined in ZSM standard. // todo: put PCM offset/data writes here once defined in ZSM standard.
return w; return w;
@ -179,26 +179,26 @@ void ZSM::flushWrites() {
logD("ZSM: flushWrites.... numwrites=%d ticks=%d ymwrites=%d",numWrites,ticks,ymwrites.size()); logD("ZSM: flushWrites.... numwrites=%d ticks=%d ymwrites=%d",numWrites,ticks,ymwrites.size());
if (numWrites==0) return; if (numWrites==0) return;
flushTicks(); // only flush ticks if there are writes pending. flushTicks(); // only flush ticks if there are writes pending.
for (int i=0;i<64;i++) { for (unsigned char i=0;i<64;i++) {
if (psgState[psg_NEW][i] == psgState[psg_PREV][i]) continue; if (psgState[psg_NEW][i] == psgState[psg_PREV][i]) continue;
psgState[psg_PREV][i]=psgState[psg_NEW][i]; psgState[psg_PREV][i]=psgState[psg_NEW][i];
w->writeC(i); w->writeUC(i);
w->writeC(psgState[psg_NEW][i]); w->writeUC(psgState[psg_NEW][i]);
} }
int n=0; // n = completed YM writes. used to determine when to write the CMD byte... int n=0; // n = completed YM writes. used to determine when to write the CMD byte...
for (DivRegWrite& write: ymwrites) { for (DivRegWrite& write: ymwrites) {
if (n%ZSM_YM_MAX_WRITES == 0) { if (n%ZSM_YM_MAX_WRITES == 0) {
if(ymwrites.size()-n > ZSM_YM_MAX_WRITES) { if(ymwrites.size()-n > ZSM_YM_MAX_WRITES) {
w->writeC(ZSM_YM_CMD+ZSM_YM_MAX_WRITES); w->writeUC((unsigned char)(ZSM_YM_CMD+ZSM_YM_MAX_WRITES));
logD("ZSM: YM-write: %d (%02x) [max]",ZSM_YM_MAX_WRITES,ZSM_YM_MAX_WRITES+ZSM_YM_CMD); logD("ZSM: YM-write: %d (%02x) [max]",ZSM_YM_MAX_WRITES,ZSM_YM_MAX_WRITES+ZSM_YM_CMD);
} else { } else {
w->writeC(ZSM_YM_CMD+ymwrites.size()-n); w->writeUC((unsigned char)(ZSM_YM_CMD+ymwrites.size()-n));
logD("ZSM: YM-write: %d (%02x)",ymwrites.size()-n,ZSM_YM_CMD+ymwrites.size()-n); logD("ZSM: YM-write: %d (%02x)",ymwrites.size()-n,ZSM_YM_CMD+ymwrites.size()-n);
} }
} }
n++; n++;
w->writeC(write.addr); w->writeUC(write.addr);
w->writeC(write.val); w->writeUC(write.val);
} }
ymwrites.clear(); ymwrites.clear();
numWrites=0; numWrites=0;
@ -207,12 +207,12 @@ void ZSM::flushWrites() {
void ZSM::flushTicks() { void ZSM::flushTicks() {
while (ticks > ZSM_DELAY_MAX) { while (ticks > ZSM_DELAY_MAX) {
logD("ZSM: write delay %d (max)",ZSM_DELAY_MAX); logD("ZSM: write delay %d (max)",ZSM_DELAY_MAX);
w->writeC((signed char)(ZSM_DELAY_CMD+ZSM_DELAY_MAX)); w->writeUC((unsigned char)(ZSM_DELAY_CMD+ZSM_DELAY_MAX));
ticks -= ZSM_DELAY_MAX; ticks -= ZSM_DELAY_MAX;
} }
if (ticks>0) { if (ticks>0) {
logD("ZSM: write delay %d",ticks); logD("ZSM: write delay %d",ticks);
w->writeC(ZSM_DELAY_CMD+ticks); w->writeUC(ZSM_DELAY_CMD+ticks);
} }
ticks=0; ticks=0;
} }