From c34233cf4febfdf8e745a043fcbac6552f985889 Mon Sep 17 00:00:00 2001 From: cam900 Date: Tue, 8 Aug 2023 21:27:12 +0900 Subject: [PATCH 01/22] Prepare for C140 --- CMakeLists.txt | 3 + src/engine/platform/c140.cpp | 481 +++++++++++++++++++++++++++++++ src/engine/platform/c140.h | 102 +++++++ src/engine/platform/sound/c140.c | 183 ++++++++++++ src/engine/platform/sound/c140.h | 91 ++++++ src/gui/about.cpp | 10 +- src/main.cpp | 3 + 7 files changed, 867 insertions(+), 6 deletions(-) create mode 100644 src/engine/platform/c140.cpp create mode 100644 src/engine/platform/c140.h create mode 100644 src/engine/platform/sound/c140.c create mode 100644 src/engine/platform/sound/c140.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1bf22a333..592cd5e10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -507,6 +507,8 @@ src/engine/platform/sound/d65modified.c src/engine/platform/sound/ted-sound.c +src/engine/platform/sound/c140.c + src/engine/platform/oplAInterface.cpp src/engine/platform/ym2608Interface.cpp src/engine/platform/ym2610Interface.cpp @@ -599,6 +601,7 @@ src/engine/platform/sm8521.cpp src/engine/platform/pv1000.cpp src/engine/platform/k053260.cpp src/engine/platform/ted.cpp +src/engine/platform/c140.cpp src/engine/platform/pcmdac.cpp src/engine/platform/dummy.cpp diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp new file mode 100644 index 000000000..62067baea --- /dev/null +++ b/src/engine/platform/c140.cpp @@ -0,0 +1,481 @@ +/** + * Furnace Tracker - multi-system chiptune tracker + * Copyright (C) 2021-2023 tildearrow and contributors + * + * 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 "c140.h" +#include "../engine.h" +#include "../../ta-log.h" +#include +#include + +#define CHIP_FREQBASE 12582912 + +#define rWrite(a,v) {if(!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if(dumpWrites) addWrite(a,v); }} + +const char* regCheatSheetC140[]={ + "CHx_RVol", "00+x*10", + "CHx_LVol", "01+x*10", + "CHx_FreqH", "02+x*10", + "CHx_FreqL", "03+x*10", + "CHx_Bank", "04+x*10", + "CHx_Ctrl", "05+x*10", + "CHx_StartH", "06+x*10", + "CHx_StartL", "07+x*10", + "CHx_EndH", "08+x*10", + "CHx_EndL", "09+x*10", + "CHx_LoopH", "0A+x*10", + "CHx_LoopL", "0B+x*10", + "Timer", "1FA", + "IRQ", "1FE", + NULL +}; + +const char** DivPlatformC140::getRegisterSheet() { + return regCheatSheetC140; +} + +void DivPlatformC140::acquire(short** buf, size_t len) { + for (size_t h=0; h32767) c140.lout=32767; + + if (c140.rout<-32768) c140.rout=-32768; + if (c140.rout>32767) c140.rout=32767; + + buf[0][h]=c140.lout; + buf[1][h]=c140.rout; + + for (int i=0; i<24; i++) { + oscBuf[i]->data[oscBuf[i]->needle++]=(c140.voice[i].lout+c140.voice[i].rout)>>9; + } + } +} + +void DivPlatformC140::tick(bool sysTick) { + for (int i=0; i<24; i++) { + chan[i].std.next(); + if (chan[i].std.vol.had) { + chan[i].outVol=(chan[i].vol*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul; + chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255; + chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255; + rWrite(1+(i<<4),chan[i].chVolL); + rWrite(0+(i<<4),chan[i].chVolR); + } + if (NEW_ARP_STRAT) { + chan[i].handleArp(); + } else if (chan[i].std.arp.had) { + if (!chan[i].inPorta) { + chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val)); + } + chan[i].freqChanged=true; + } + if (chan[i].std.pitch.had) { + if (chan[i].std.pitch.mode) { + chan[i].pitch2+=chan[i].std.pitch.val; + CLAMP_VAR(chan[i].pitch2,-32768,32767); + } else { + chan[i].pitch2=chan[i].std.pitch.val; + } + chan[i].freqChanged=true; + } + if (chan[i].std.panL.had) { + chan[i].chPanL=chan[i].std.panL.val&255; + chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255; + rWrite(1+(i<<4),chan[i].chVolL); + } + + if (chan[i].std.panR.had) { + chan[i].chPanR=chan[i].std.panR.val&255; + chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255; + rWrite(0+(i<<4),chan[i].chVolR); + } + + if (chan[i].std.phaseReset.had) { + if ((chan[i].std.phaseReset.val==1) && chan[i].active) { + chan[i].audPos=0; + chan[i].setPos=true; + } + } + if (chan[i].setPos) { + // force keyon + chan[i].keyOn=true; + chan[i].setPos=false; + } else { + chan[i].audPos=0; + } + if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { + DivSample* s=parent->getSample(chan[i].sample); + unsigned char ctrl=0; + double off=(s->centerRate>=1)?((double)s->centerRate/8363.0):1.0; + chan[i].freq=(int)(off*parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,2,chan[i].pitch2,chipClock,CHIP_FREQBASE)); + if (chan[i].freq<0) chan[i].freq=0; + if (chan[i].freq>65535) chan[i].freq=65535; + ctrl|=(chan[i].active?0x80:0)|((s->isLoopable())?0x10:0); + if (chan[i].keyOn) { + unsigned int start=0; + unsigned int loop=0; + unsigned int end=0; + if (chan[i].sample>=0 && chan[i].samplesong.sampleLen) { + start=sampleOff[chan[i].sample]; + end=MIN(start+s->getCurBufLen(),getSampleMemCapacity()-1); + } + if (chan[i].audPos>0) { + start=start+MIN(chan[i].audPos,s->length8); + } + if (s->isLoopable()) { + loop=start+s->loopStart; + } + rWrite(0x05+i*16,ctrl&~0x80); // force keyoff first + rWrite(0x06+i*16,(start>>8)&0xff); + rWrite(0x07+i*16,start&0xff); + rWrite(0x08+i*16,(end>>8)&0xff); + rWrite(0x09+i*16,end&0xff); + rWrite(0x0a+i*16,(loop>>8)&0xff); + rWrite(0x0b+i*16,loop&0xff); + if (!chan[i].std.vol.had) { + chan[i].outVol=chan[i].vol; + chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255; + chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255; + rWrite(1+(i<<4),chan[i].chVolL); + rWrite(0+(i<<4),chan[i].chVolR); + } + chan[i].keyOn=false; + } + if (chan[i].keyOff) { + chan[i].keyOff=false; + } + if (chan[i].freqChanged) { + rWrite(0x02+i*16,chan[i].freq&0xff); + rWrite(0x03+i*16,chan[i].freq>>8); + chan[i].freqChanged=false; + } + rWrite(0x05+i*16,ctrl); + } + } +} + +int DivPlatformC140::dispatch(DivCommand c) { + switch (c.cmd) { + case DIV_CMD_NOTE_ON: { + DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA); + chan[c.chan].isNewYMZ=ins->type==DIV_INS_YMZ280B; + chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:255; + if (c.value!=DIV_NOTE_NULL) { + chan[c.chan].sample=ins->amiga.getSample(c.value); + c.value=ins->amiga.getFreq(c.value); + } + if (c.value!=DIV_NOTE_NULL) { + chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value); + } + if (chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) { + chan[c.chan].sample=-1; + } + if (c.value!=DIV_NOTE_NULL) { + chan[c.chan].freqChanged=true; + chan[c.chan].note=c.value; + } + chan[c.chan].active=true; + chan[c.chan].keyOn=true; + chan[c.chan].macroInit(ins); + if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { + chan[c.chan].outVol=chan[c.chan].vol; + + chan[c.chan].chVolL=(chan[c.chan].outVol*chan[c.chan].chPanL)/255; + chan[c.chan].chVolR=(chan[c.chan].outVol*chan[c.chan].chPanR)/255; + rWrite(1+(c.chan<<4),chan[c.chan].chVolL); + rWrite(0+(c.chan<<4),chan[c.chan].chVolR); + } + break; + } + case DIV_CMD_NOTE_OFF: + chan[c.chan].sample=-1; + chan[c.chan].active=false; + chan[c.chan].keyOff=true; + chan[c.chan].macroInit(NULL); + break; + case DIV_CMD_NOTE_OFF_ENV: + case DIV_CMD_ENV_RELEASE: + chan[c.chan].std.release(); + break; + case DIV_CMD_INSTRUMENT: + if (chan[c.chan].ins!=c.value || c.value2==1) { + chan[c.chan].ins=c.value; + } + break; + case DIV_CMD_VOLUME: + chan[c.chan].vol=c.value; + if (!chan[c.chan].std.vol.has) { + chan[c.chan].outVol=c.value; + } + chan[c.chan].chVolL=(c.value*chan[c.chan].chPanL)/255; + chan[c.chan].chVolR=(c.value*chan[c.chan].chPanR)/255; + rWrite(1+(c.chan<<4),chan[c.chan].chVolL); + rWrite(0+(c.chan<<4),chan[c.chan].chVolR); + break; + case DIV_CMD_GET_VOLUME: + if (chan[c.chan].std.vol.has) { + return chan[c.chan].vol; + } + return chan[c.chan].outVol; + break; + case DIV_CMD_PANNING: + chan[c.chan].chPanL=c.value; + chan[c.chan].chPanR=c.value2; + chan[c.chan].chVolL=(chan[c.chan].outVol*chan[c.chan].chPanL)/255; + chan[c.chan].chVolR=(chan[c.chan].outVol*chan[c.chan].chPanR)/255; + rWrite(1+(c.chan<<4),chan[c.chan].chVolL); + rWrite(0+(c.chan<<4),chan[c.chan].chVolR); + break; + case DIV_CMD_PITCH: + chan[c.chan].pitch=c.value; + chan[c.chan].freqChanged=true; + break; + case DIV_CMD_NOTE_PORTA: { + int destFreq=NOTE_FREQUENCY(c.value2); + bool return2=false; + if (destFreq>chan[c.chan].baseFreq) { + chan[c.chan].baseFreq+=c.value; + if (chan[c.chan].baseFreq>=destFreq) { + chan[c.chan].baseFreq=destFreq; + return2=true; + } + } else { + chan[c.chan].baseFreq-=c.value; + if (chan[c.chan].baseFreq<=destFreq) { + chan[c.chan].baseFreq=destFreq; + return2=true; + } + } + chan[c.chan].freqChanged=true; + if (return2) { + chan[c.chan].inPorta=false; + return 2; + } + break; + } + case DIV_CMD_LEGATO: { + chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(0))); + chan[c.chan].freqChanged=true; + chan[c.chan].note=c.value; + break; + } + case DIV_CMD_PRE_PORTA: + if (chan[c.chan].active && c.value2) { + if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA)); + } + if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_FREQUENCY(chan[c.chan].note); + chan[c.chan].inPorta=c.value; + break; + case DIV_CMD_SAMPLE_POS: + chan[c.chan].audPos=c.value; + chan[c.chan].setPos=true; + break; + case DIV_CMD_GET_VOLMAX: + return 255; + break; + case DIV_CMD_MACRO_OFF: + chan[c.chan].std.mask(c.value,true); + break; + case DIV_CMD_MACRO_ON: + chan[c.chan].std.mask(c.value,false); + break; + case DIV_ALWAYS_SET_VOLUME: + return 1; + break; + default: + break; + } + return 1; +} + +void DivPlatformC140::muteChannel(int ch, bool mute) { + isMuted[ch]=mute; +} + +void DivPlatformC140::forceIns() { + for (int i=0; i<24; i++) { + chan[i].insChanged=true; + chan[i].freqChanged=true; + chan[i].sample=-1; + } +} + +void* DivPlatformC140::getChanState(int ch) { + return &chan[ch]; +} + +DivMacroInt* DivPlatformC140::getChanMacroInt(int ch) { + return &chan[ch].std; +} + +DivDispatchOscBuffer* DivPlatformC140::getOscBuffer(int ch) { + return oscBuf[ch]; +} + +void DivPlatformC140::reset() { + while (!writes.empty()) writes.pop(); + memset(regPool,0,512); + c140_reset(&c140); + for (int i=0; i<24; i++) { + chan[i]=DivPlatformC140::Channel(); + chan[i].std.setEngine(parent); + rWrite(0x05+i*16,0); + } +} + +int DivPlatformC140::getOutputCount() { + return 2; +} + +void DivPlatformC140::notifyInsChange(int ins) { + for (int i=0; i<24; i++) { + if (chan[i].ins==ins) { + chan[i].insChanged=true; + } + } +} + +void DivPlatformC140::notifyWaveChange(int wave) { + // TODO when wavetables are added + // TODO they probably won't be added unless the samples reside in RAM +} + +void DivPlatformC140::notifyInsDeletion(void* ins) { + for (int i=0; i<24; i++) { + chan[i].std.notifyInsDeletion((DivInstrument*)ins); + } +} + +void DivPlatformC140::poke(unsigned int addr, unsigned short val) { + rWrite(addr,val); +} + +void DivPlatformC140::poke(std::vector& wlist) { + for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); +} + +unsigned char* DivPlatformC140::getRegisterPool() { + return regPool; +} + +int DivPlatformC140::getRegisterPoolSize() { + return 512; +} + +const void* DivPlatformC140::getSampleMem(int index) { + return index == 0 ? sampleMem : NULL; +} + +size_t DivPlatformC140::getSampleMemCapacity(int index) { + return index == 0 ? 16777216 : 0; +} + +size_t DivPlatformC140::getSampleMemUsage(int index) { + return index == 0 ? sampleMemLen : 0; +} + +bool DivPlatformC140::isSampleLoaded(int index, int sample) { + if (index!=0) return false; + if (sample<0 || sample>255) return false; + return sampleLoaded[sample]; +} + +void DivPlatformC140::renderSamples(int sysID) { + memset(sampleMem,0,getSampleMemCapacity()); + memset(sampleOff,0,256*sizeof(unsigned int)); + memset(sampleLoaded,0,256*sizeof(bool)); + + size_t memPos=0; + for (int i=0; isong.sampleLen; i++) { + DivSample* s=parent->song.sample[i]; + if (!s->renderOn[0][sysID]) { + sampleOff[i]=0; + continue; + } + + unsigned int length=s->length16; + // fit sample size to single bank size + if (length>(131072)) { + length=131072; + } + if ((memPos&0xfe0000)!=((memPos+length)&0xfe0000)) { + memPos=((memPos+0x1ffff)&0xfe0000); + } + if (memPos>=(getSampleMemCapacity())) { + logW("out of C140 memory for sample %d!",i); + break; + } + if (memPos+length>=(getSampleMemCapacity())) { + memcpy(sampleMem+(memPos/sizeof(short)),s->data16,(getSampleMemCapacity())-memPos); + logW("out of C140 memory for sample %d!",i); + } else { + memcpy(sampleMem+(memPos/sizeof(short)),s->data16,length); + } + sampleOff[i]=memPos; + sampleLoaded[i]=true; + memPos+=length; + } + sampleMemLen=memPos+256; +} + +void DivPlatformC140::setChipModel(int type) { + chipType=type; +} + +void DivPlatformC140::setFlags(const DivConfig& flags) { + chipClock = 8192000; + CHECK_CUSTOM_CLOCK; + rate=chipClock/192; + for (int i=0; i<24; i++) { + oscBuf[i]->rate=rate; + } +} + +int DivPlatformC140::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { + parent=p; + dumpWrites=false; + skipRegisterWrites=false; + + for (int i=0; i<24; i++) { + isMuted[i]=false; + oscBuf[i]=new DivDispatchOscBuffer; + } + sampleMem=new unsigned char[getSampleMemCapacity()]; + sampleMemLen=0; + c140_init(&c140); + setFlags(flags); + reset(); + + return 24; +} + +void DivPlatformC140::quit() { + delete[] sampleMem; + for (int i=0; i<24; i++) { + delete oscBuf[i]; + } +} diff --git a/src/engine/platform/c140.h b/src/engine/platform/c140.h new file mode 100644 index 000000000..830760022 --- /dev/null +++ b/src/engine/platform/c140.h @@ -0,0 +1,102 @@ +/** + * Furnace Tracker - multi-system chiptune tracker + * Copyright (C) 2021-2023 tildearrow and contributors + * + * 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. + */ + +#ifndef _C140_H +#define _C140_H + +#include "../dispatch.h" +#include "sound/c140.h" +#include "../fixedQueue.h" + +class DivPlatformC140: public DivDispatch { + struct Channel: public SharedChannel { + unsigned int audPos; + int sample, wave; + int panning; + bool setPos, isNewYMZ; + int chPanL, chPanR; + int chVolL, chVolR; + int macroVolMul; + Channel(): + SharedChannel(255), + audPos(0), + sample(-1), + wave(-1), + panning(8), + setPos(false), + isNewYMZ(false), + chPanL(255), + chPanR(255), + chVolL(255), + chVolR(255), + macroVolMul(64) {} + }; + Channel chan[24]; + DivDispatchOscBuffer* oscBuf[24]; + bool isMuted[24]; + int chipType; + unsigned int sampleOff[256]; + bool sampleLoaded[256]; + + unsigned char* sampleMem; + size_t sampleMemLen; + struct QueuedWrite { + unsigned short addr; + unsigned char val; + bool addrOrVal; + QueuedWrite(): addr(0), val(0), addrOrVal(false) {} + QueuedWrite(unsigned short a, unsigned char v): addr(a), val(v), addrOrVal(false) {} + }; + FixedQueue writes; + struct c140_t c140; + unsigned char regPool[512]; + friend void putDispatchChip(void*,int); + friend void putDispatchChan(void*,int,int); + + public: + void acquire(short** buf, size_t len); + int dispatch(DivCommand c); + void* getChanState(int chan); + DivMacroInt* getChanMacroInt(int ch); + DivDispatchOscBuffer* getOscBuffer(int chan); + unsigned char* getRegisterPool(); + int getRegisterPoolSize(); + void reset(); + void forceIns(); + void tick(bool sysTick=true); + void muteChannel(int ch, bool mute); + int getOutputCount(); + void setChipModel(int type); + void notifyInsChange(int ins); + void notifyWaveChange(int wave); + void notifyInsDeletion(void* ins); + void poke(unsigned int addr, unsigned short val); + void poke(std::vector& wlist); + const char** getRegisterSheet(); + const void* getSampleMem(int index = 0); + size_t getSampleMemCapacity(int index = 0); + size_t getSampleMemUsage(int index = 0); + bool isSampleLoaded(int index, int sample); + void renderSamples(int chipID); + void setFlags(const DivConfig& flags); + int init(DivEngine* parent, int channels, int sugRate, const DivConfig& flags); + void quit(); +}; + +#endif diff --git a/src/engine/platform/sound/c140.c b/src/engine/platform/sound/c140.c new file mode 100644 index 000000000..6057782b3 --- /dev/null +++ b/src/engine/platform/sound/c140.c @@ -0,0 +1,183 @@ +/* + +============================================================================ + +Namco C140 sound emulator +by cam900 + +This file is licensed under zlib license. + +============================================================================ + +zlib License + +(C) 2023-present cam900 and contributors + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. + +============================================================================ + +TODO: +- unknown registers (Bit 6 of control register, etc) +- Internal timer + +*/ + +#include "c140.h" + +static int c140_max(int a, int b) { return (a > b) ? a : b; } +static int c140_min(int a, int b) { return (a < b) ? a : b; } +static int c140_clamp(int v, int min, int max) { return c140_min(c140_max(v,min),max); } +static int c140_bit(int val, int bit) { return (val >> bit) & 1; } + +void c140_tick(struct c140_t *c140, const int cycle) +{ + for (int i = 0; i < 24; i++) + { + c140_voice_tick(c140, i, cycle); + c140->lout += c140->voice[i].lout; + c140->rout += c140->voice[i].rout; + } + // scale as 16bit + c140->lout >>= 5; + c140->rout >>= 5; +} + +void c140_voice_tick(struct c140_t *c140, const unsigned char voice, const int cycle) +{ + struct c140_voice_t *c140_voice = &c140->voice[voice]; + if (c140_voice->busy && c140_voice->keyon) + { + for (int c = 0; c < cycle; c++) + { + c140_voice->frac += c140_voice->freq; + if (c140_voice->frac > 0xffff) + { + c140_voice->addr += c140_voice->frac >> 16; + if (c140_voice->addr > c140_voice->end_addr) + { + if (c140_voice->loop) + { + c140_voice->addr = (c140_voice->addr + c140_voice->end_addr) - c140_voice->loop_addr; + } + else + { + c140_voice->keyon = false; + } + } + c140_voice->frac &= 0xffff; + } + // fetch 12 bit sample + signed short s1 = c140->read_sample(c140_voice->bank, c140_voice->addr) & ~0xf; + signed short s2 = c140->read_sample(c140_voice->bank, c140_voice->addr + 1) & ~0xf; + if (c140_voice->compressed) + { + s1 = c140->mulaw[(s1 >> 8) & 0xff]; + s2 = c140->mulaw[(s2 >> 8) & 0xff]; + } + // interpolate + signed int sample = s1 + (((c140_voice->frac) * (s2 - s1)) >> 16); + c140_voice->lout = sample * c140_voice->lvol; + c140_voice->rout = sample * c140_voice->rvol; + } + } + else + { + c140_voice->lout = 0; + c140_voice->rout = 0; + } +} + +void c140_keyon(struct c140_voice_t *c140_voice) +{ + c140_voice->busy = true; + c140_voice->keyon = true; + c140_voice->frac = 0; + c140_voice->addr = c140_voice->start_addr; +} + +void c140_init(struct c140_t *c140) +{ + // u-law table verified from Wii Virtual Console Arcade Starblade + for (int i = 0; i < 256; i++) + { + int j = (signed char)i; + signed char s1 = j & 7; + signed char s2 = abs(j >> 3) & 31; + + c140->mulaw[i] = 0x80 << s1 & 0xff00; + c140->mulaw[i] += s2 << (s1 ? s1 + 3 : 4); + + if (j < 0) + c140->mulaw[i] = -c140->mulaw[i]; + } +} + +void c140_reset(struct c140_t *c140) +{ + for (int i = 0; i < 24; i++) + { + c140->voice[i].busy = false; + c140->voice[i].keyon = false; + c140->voice[i].freq = 0; + c140->voice[i].bank = 0; + c140->voice[i].start_addr = 0; + c140->voice[i].loop_addr = 0; + c140->voice[i].end_addr = 0; + c140->voice[i].lvol = 0; + c140->voice[i].rvol = 0; + c140->voice[i].compressed = false; + c140->voice[i].loop = false; + c140->voice[i].addr = 0; + c140->voice[i].frac = 0; + c140->voice[i].lout = 0; + c140->voice[i].rout = 0; + } +} + +void c140_write(struct c140_t *c140, const unsigned short addr, const unsigned char data) +{ + // voice register + if (addr < 0x180) + { + struct c140_voice_t *voice = &c140->voice[addr >> 4]; + switch (addr & 0xf) + { + case 0x0: voice->rvol = data; break; + case 0x1: voice->lvol = data; break; + case 0x2: voice->freq = (voice->freq & ~0xff00) | (unsigned int)(data << 8); break; + case 0x3: voice->freq = (voice->freq & ~0x00ff) | data; break; + case 0x4: voice->bank = data; break; + case 0x5: + voice->compressed = c140_bit(data, 3); + voice->loop = c140_bit(data, 4); + if (data & 0x80) + c140_keyon(voice); + else + voice->busy = false; + break; + case 0x6: voice->start_addr = (voice->start_addr & ~0xff00) | (unsigned int)(data << 8); break; + case 0x7: voice->start_addr = (voice->start_addr & ~0x00ff) | data; break; + case 0x8: voice->end_addr = (voice->end_addr & ~0xff00) | (unsigned int)(data << 8); break; + case 0x9: voice->end_addr = (voice->end_addr & ~0x00ff) | data; break; + case 0xa: voice->loop_addr = (voice->loop_addr & ~0xff00) | (unsigned int)(data << 8); break; + case 0xb: voice->loop_addr = (voice->loop_addr & ~0x00ff) | data; break; + default: break; + } + } + // Timer +} \ No newline at end of file diff --git a/src/engine/platform/sound/c140.h b/src/engine/platform/sound/c140.h new file mode 100644 index 000000000..d0a4ff463 --- /dev/null +++ b/src/engine/platform/sound/c140.h @@ -0,0 +1,91 @@ +/* + +============================================================================ + +Namco C140 sound emulator +by cam900 + +This file is licensed under zlib license. + +============================================================================ + +zlib License + +(C) 2023-present cam900 and contributors + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. + +============================================================================ + +TODO: +- unknown registers (Bit 6 of control register, etc) +- Internal timer + +*/ + +#ifndef _C140_EMU_H +#define _C140_EMU_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct c140_voice_t +{ + bool busy; // busy flag + bool keyon; // key on flag + unsigned short freq; // sample frequency + unsigned char bank; // sample bank + unsigned short start_addr; // sample start address + unsigned short loop_addr; // sample loop address + unsigned short end_addr; // sample end address + int lvol, rvol; // left/right volume + bool compressed; // compressed sample flag + bool loop; // loop flag + unsigned short addr; // sample address + int frac; // frequency counter (.16 fixed point) + int lout, rout; // left/right output +}; + +struct c140_t +{ + struct c140_voice_t voice[24]; + signed int lout, rout; + signed short mulaw[256]; + unsigned short (*read_sample)(unsigned char bank, unsigned short addr); +}; + +void c140_tick(struct c140_t *c140, const int cycle); + +void c140_voice_tick(struct c140_t *c140, const unsigned char voice, const int cycle); + +void c140_keyon(struct c140_voice_t *c140_voice); + +void c140_write(struct c140_t *c140, const unsigned short addr, const unsigned char data); + +void c140_init(struct c140_t *c140); + +void c140_reset(struct c140_t *c140); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _C140_EMU_H diff --git a/src/gui/about.cpp b/src/gui/about.cpp index b849c6bbe..ea983787e 100644 --- a/src/gui/about.cpp +++ b/src/gui/about.cpp @@ -192,12 +192,10 @@ const char* aboutLine[]={ "mzpokeysnd POKEY emulator by Michael Borisov", "ASAP POKEY emulator by Piotr Fusik", "ported by laoo to C++", - "K005289 emulator by cam900", - "Namco 163 emulator by cam900", - "Seta X1-010 emulator by cam900", - "Konami VRC6 emulator by cam900", - "Konami SCC emulator by cam900", - "MSM6295 emulator by cam900", + "vgsound_emu (second version, modified version) by cam900", + "SM8521 emulator (modified version) by cam900", + "D65010G031 emulator (modified version) by cam900", + "Namco C140 emulator by cam900", "", "greetings to:", "NEOART Costa Rica", diff --git a/src/main.cpp b/src/main.cpp index bf2ac269b..d1fb2dc12 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -208,6 +208,9 @@ TAParamResult pVersion(String) { printf("- MAME GA20 core by Acho A. Tang, R. Belmont, Valley Bell (BSD 3-clause)\n"); printf("- Atari800 mzpokeysnd POKEY emulator by Michael Borisov (GPLv2)\n"); printf("- ASAP POKEY emulator by Piotr Fusik ported to C++ by laoo (GPLv2)\n"); + printf("- SM8521 emulator (modified version) by cam900 (zlib license)\n"); + printf("- D65010G031 emulator (modified version) by cam900 (zlib license)\n"); + printf("- C140 emulator by cam900 (zlib license)\n"); return TA_PARAM_QUIT; } From 14c5d94499e3d051cac4e87971e5381ab790794f Mon Sep 17 00:00:00 2001 From: cam900 Date: Wed, 9 Aug 2023 20:30:00 +0900 Subject: [PATCH 02/22] C140 Part 2 --- src/engine/dispatchContainer.cpp | 4 ++ src/engine/instrument.cpp | 4 ++ src/engine/instrument.h | 1 + src/engine/platform/c140.cpp | 99 +++++++++++++++++++------------- src/engine/platform/c140.h | 23 ++++++-- src/engine/platform/sound/c140.c | 10 ++-- src/engine/platform/sound/c140.h | 2 +- src/engine/song.h | 3 +- src/engine/sysDef.cpp | 12 ++++ src/engine/vgmOps.cpp | 43 ++++++++++++++ src/gui/dataList.cpp | 4 ++ src/gui/debug.cpp | 18 ++++++ src/gui/doAction.cpp | 3 +- src/gui/gui.h | 1 + src/gui/guiConst.cpp | 4 ++ src/gui/insEdit.cpp | 14 ++++- src/gui/presets.cpp | 11 ++++ src/gui/sampleEdit.cpp | 5 ++ src/gui/settings.cpp | 1 + src/gui/sysConf.cpp | 1 + src/gui/sysPartNumber.cpp | 3 + 21 files changed, 210 insertions(+), 56 deletions(-) diff --git a/src/engine/dispatchContainer.cpp b/src/engine/dispatchContainer.cpp index 61d8344a9..ac92b23f4 100644 --- a/src/engine/dispatchContainer.cpp +++ b/src/engine/dispatchContainer.cpp @@ -80,6 +80,7 @@ #include "platform/pv1000.h" #include "platform/k053260.h" #include "platform/ted.h" +#include "platform/c140.h" #include "platform/pcmdac.h" #include "platform/dummy.h" #include "../ta-log.h" @@ -504,6 +505,9 @@ void DivDispatchContainer::init(DivSystem sys, DivEngine* eng, int chanCount, do case DIV_SYSTEM_TED: dispatch=new DivPlatformTED; break; + case DIV_SYSTEM_C140: + dispatch=new DivPlatformC140; + break; case DIV_SYSTEM_PCM_DAC: dispatch=new DivPlatformPCMDAC; break; diff --git a/src/engine/instrument.cpp b/src/engine/instrument.cpp index 9fa7477f2..3931a6f50 100644 --- a/src/engine/instrument.cpp +++ b/src/engine/instrument.cpp @@ -963,6 +963,10 @@ void DivInstrument::putInsData2(SafeWriter* w, bool fui, const DivSong* song) { break; case DIV_INS_TED: break; + case DIV_INS_C140: + featureSM=true; + featureSL=true; + break; case DIV_INS_MAX: break; diff --git a/src/engine/instrument.h b/src/engine/instrument.h index ef79a97a3..cd8c56527 100644 --- a/src/engine/instrument.h +++ b/src/engine/instrument.h @@ -83,6 +83,7 @@ enum DivInstrumentType: unsigned short { DIV_INS_K053260=50, // DIV_INS_YMF292=51, DIV_INS_TED=52, + DIV_INS_C140=53, DIV_INS_MAX, DIV_INS_NULL }; diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index 62067baea..5330443ac 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -80,10 +80,9 @@ void DivPlatformC140::tick(bool sysTick) { chan[i].std.next(); if (chan[i].std.vol.had) { chan[i].outVol=(chan[i].vol*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul; - chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255; - chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255; - rWrite(1+(i<<4),chan[i].chVolL); - rWrite(0+(i<<4),chan[i].chVolR); + if (!isMuted[i]) { + chan[i].volumeChanged.changed=0xff; + } } if (NEW_ARP_STRAT) { chan[i].handleArp(); @@ -104,14 +103,16 @@ void DivPlatformC140::tick(bool sysTick) { } if (chan[i].std.panL.had) { chan[i].chPanL=chan[i].std.panL.val&255; - chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255; - rWrite(1+(i<<4),chan[i].chVolL); + if (!isMuted[i]) { + chan[i].volumeChanged.left=1; + } } if (chan[i].std.panR.had) { chan[i].chPanR=chan[i].std.panR.val&255; - chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255; - rWrite(0+(i<<4),chan[i].chVolR); + if (!isMuted[i]) { + chan[i].volumeChanged.right=1; + } } if (chan[i].std.phaseReset.had) { @@ -120,6 +121,25 @@ void DivPlatformC140::tick(bool sysTick) { chan[i].setPos=true; } } + if (chan[i].volumeChanged.changed) { + if (chan[i].volumeChanged.left) { + if (isMuted[i]) { + chan[i].chVolL=0; + } else { + chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255; + } + rWrite(1+(i<<4),chan[i].chVolL); + } + if (chan[i].volumeChanged.right) { + if (isMuted[i]) { + chan[i].chVolR=0; + } else { + chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255; + } + rWrite(0+(i<<4),chan[i].chVolR); + } + chan[i].volumeChanged.changed=0; + } if (chan[i].setPos) { // force keyon chan[i].keyOn=true; @@ -141,7 +161,7 @@ void DivPlatformC140::tick(bool sysTick) { unsigned int end=0; if (chan[i].sample>=0 && chan[i].samplesong.sampleLen) { start=sampleOff[chan[i].sample]; - end=MIN(start+s->getCurBufLen(),getSampleMemCapacity()-1); + end=MIN(start+s->length8,getSampleMemCapacity()-1); } if (chan[i].audPos>0) { start=start+MIN(chan[i].audPos,s->length8); @@ -149,19 +169,18 @@ void DivPlatformC140::tick(bool sysTick) { if (s->isLoopable()) { loop=start+s->loopStart; } - rWrite(0x05+i*16,ctrl&~0x80); // force keyoff first - rWrite(0x06+i*16,(start>>8)&0xff); - rWrite(0x07+i*16,start&0xff); - rWrite(0x08+i*16,(end>>8)&0xff); - rWrite(0x09+i*16,end&0xff); - rWrite(0x0a+i*16,(loop>>8)&0xff); - rWrite(0x0b+i*16,loop&0xff); + rWrite(0x05+(i<<4),0); // force keyoff first + rWrite(0x06+(i<<4),(start>>8)&0xff); + rWrite(0x07+(i<<4),start&0xff); + rWrite(0x08+(i<<4),(end>>8)&0xff); + rWrite(0x09+(i<<4),end&0xff); + rWrite(0x0a+(i<<4),(loop>>8)&0xff); + rWrite(0x0b+(i<<4),loop&0xff); if (!chan[i].std.vol.had) { chan[i].outVol=chan[i].vol; - chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255; - chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255; - rWrite(1+(i<<4),chan[i].chVolL); - rWrite(0+(i<<4),chan[i].chVolR); + if (!isMuted[i]) { + chan[i].volumeChanged.changed=0xff; + } } chan[i].keyOn=false; } @@ -169,11 +188,11 @@ void DivPlatformC140::tick(bool sysTick) { chan[i].keyOff=false; } if (chan[i].freqChanged) { - rWrite(0x02+i*16,chan[i].freq&0xff); - rWrite(0x03+i*16,chan[i].freq>>8); + rWrite(0x02+(i<<4),chan[i].freq>>8); + rWrite(0x03+(i<<4),chan[i].freq&0xff); chan[i].freqChanged=false; } - rWrite(0x05+i*16,ctrl); + rWrite(0x05+(i<<4),ctrl); } } } @@ -182,7 +201,6 @@ int DivPlatformC140::dispatch(DivCommand c) { switch (c.cmd) { case DIV_CMD_NOTE_ON: { DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA); - chan[c.chan].isNewYMZ=ins->type==DIV_INS_YMZ280B; chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:255; if (c.value!=DIV_NOTE_NULL) { chan[c.chan].sample=ins->amiga.getSample(c.value); @@ -203,11 +221,9 @@ int DivPlatformC140::dispatch(DivCommand c) { chan[c.chan].macroInit(ins); if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { chan[c.chan].outVol=chan[c.chan].vol; - - chan[c.chan].chVolL=(chan[c.chan].outVol*chan[c.chan].chPanL)/255; - chan[c.chan].chVolR=(chan[c.chan].outVol*chan[c.chan].chPanR)/255; - rWrite(1+(c.chan<<4),chan[c.chan].chVolL); - rWrite(0+(c.chan<<4),chan[c.chan].chVolR); + if (!isMuted[c.chan]) { + chan[c.chan].volumeChanged.changed=0xff; + } } break; } @@ -231,10 +247,9 @@ int DivPlatformC140::dispatch(DivCommand c) { if (!chan[c.chan].std.vol.has) { chan[c.chan].outVol=c.value; } - chan[c.chan].chVolL=(c.value*chan[c.chan].chPanL)/255; - chan[c.chan].chVolR=(c.value*chan[c.chan].chPanR)/255; - rWrite(1+(c.chan<<4),chan[c.chan].chVolL); - rWrite(0+(c.chan<<4),chan[c.chan].chVolR); + if (!isMuted[c.chan]) { + chan[c.chan].volumeChanged.changed=0xff; + } break; case DIV_CMD_GET_VOLUME: if (chan[c.chan].std.vol.has) { @@ -245,10 +260,9 @@ int DivPlatformC140::dispatch(DivCommand c) { case DIV_CMD_PANNING: chan[c.chan].chPanL=c.value; chan[c.chan].chPanR=c.value2; - chan[c.chan].chVolL=(chan[c.chan].outVol*chan[c.chan].chPanL)/255; - chan[c.chan].chVolR=(chan[c.chan].outVol*chan[c.chan].chPanR)/255; - rWrite(1+(c.chan<<4),chan[c.chan].chVolL); - rWrite(0+(c.chan<<4),chan[c.chan].chVolR); + if (!isMuted[c.chan]) { + chan[c.chan].volumeChanged.changed=0xff; + } break; case DIV_CMD_PITCH: chan[c.chan].pitch=c.value; @@ -314,12 +328,14 @@ int DivPlatformC140::dispatch(DivCommand c) { void DivPlatformC140::muteChannel(int ch, bool mute) { isMuted[ch]=mute; + chan[ch].volumeChanged.changed=0xff; } void DivPlatformC140::forceIns() { for (int i=0; i<24; i++) { chan[i].insChanged=true; chan[i].freqChanged=true; + chan[i].volumeChanged.changed=0xff; chan[i].sample=-1; } } @@ -343,7 +359,7 @@ void DivPlatformC140::reset() { for (int i=0; i<24; i++) { chan[i]=DivPlatformC140::Channel(); chan[i].std.setEngine(parent); - rWrite(0x05+i*16,0); + rWrite(0x05+(i<<4),0); } } @@ -435,7 +451,7 @@ void DivPlatformC140::renderSamples(int sysID) { } else { memcpy(sampleMem+(memPos/sizeof(short)),s->data16,length); } - sampleOff[i]=memPos; + sampleOff[i]=memPos>>1; sampleLoaded[i]=true; memPos+=length; } @@ -447,7 +463,7 @@ void DivPlatformC140::setChipModel(int type) { } void DivPlatformC140::setFlags(const DivConfig& flags) { - chipClock = 8192000; + chipClock=32000*256; // 8.192MHz and 12.288MHz input, verified from Assault Schematics CHECK_CUSTOM_CLOCK; rate=chipClock/192; for (int i=0; i<24; i++) { @@ -464,9 +480,10 @@ int DivPlatformC140::init(DivEngine* p, int channels, int sugRate, const DivConf isMuted[i]=false; oscBuf[i]=new DivDispatchOscBuffer; } - sampleMem=new unsigned char[getSampleMemCapacity()]; + sampleMem=new signed short[getSampleMemCapacity()/sizeof(short)]; sampleMemLen=0; c140_init(&c140); + c140.sample_mem=sampleMem; setFlags(flags); reset(); diff --git a/src/engine/platform/c140.h b/src/engine/platform/c140.h index 830760022..5604a794e 100644 --- a/src/engine/platform/c140.h +++ b/src/engine/platform/c140.h @@ -26,21 +26,33 @@ class DivPlatformC140: public DivDispatch { struct Channel: public SharedChannel { + struct VolumeUpdate { + union { // pack flag bits in single byte + struct { // flag bits + unsigned char left : 1; // left volume + unsigned char right : 1; // right volume + unsigned char reserved : 6; + }; + unsigned char changed = 0; // Packed flags are stored here + }; + + VolumeUpdate(): + changed(0) {} + } volumeChanged; + unsigned int audPos; int sample, wave; - int panning; - bool setPos, isNewYMZ; + bool setPos; int chPanL, chPanR; int chVolL, chVolR; int macroVolMul; Channel(): SharedChannel(255), + volumeChanged(VolumeUpdate()), audPos(0), sample(-1), wave(-1), - panning(8), setPos(false), - isNewYMZ(false), chPanL(255), chPanR(255), chVolL(255), @@ -54,7 +66,7 @@ class DivPlatformC140: public DivDispatch { unsigned int sampleOff[256]; bool sampleLoaded[256]; - unsigned char* sampleMem; + signed short* sampleMem; size_t sampleMemLen; struct QueuedWrite { unsigned short addr; @@ -97,6 +109,7 @@ class DivPlatformC140: public DivDispatch { void setFlags(const DivConfig& flags); int init(DivEngine* parent, int channels, int sugRate, const DivConfig& flags); void quit(); + private: }; #endif diff --git a/src/engine/platform/sound/c140.c b/src/engine/platform/sound/c140.c index 6057782b3..7481ac77e 100644 --- a/src/engine/platform/sound/c140.c +++ b/src/engine/platform/sound/c140.c @@ -46,6 +46,8 @@ static int c140_bit(int val, int bit) { return (val >> bit) & 1; } void c140_tick(struct c140_t *c140, const int cycle) { + c140->lout = 0; + c140->rout = 0; for (int i = 0; i < 24; i++) { c140_voice_tick(c140, i, cycle); @@ -53,8 +55,8 @@ void c140_tick(struct c140_t *c140, const int cycle) c140->rout += c140->voice[i].rout; } // scale as 16bit - c140->lout >>= 5; - c140->rout >>= 5; + c140->lout >>= 8; + c140->rout >>= 8; } void c140_voice_tick(struct c140_t *c140, const unsigned char voice, const int cycle) @@ -82,8 +84,8 @@ void c140_voice_tick(struct c140_t *c140, const unsigned char voice, const int c c140_voice->frac &= 0xffff; } // fetch 12 bit sample - signed short s1 = c140->read_sample(c140_voice->bank, c140_voice->addr) & ~0xf; - signed short s2 = c140->read_sample(c140_voice->bank, c140_voice->addr + 1) & ~0xf; + signed short s1 = c140->sample_mem[((unsigned int)(c140_voice->bank) << 16) | c140_voice->addr] & ~0xf; + signed short s2 = c140->sample_mem[((unsigned int)(c140_voice->bank) << 16) | ((c140_voice->addr + 1) & 0xffff)] & ~0xf; if (c140_voice->compressed) { s1 = c140->mulaw[(s1 >> 8) & 0xff]; diff --git a/src/engine/platform/sound/c140.h b/src/engine/platform/sound/c140.h index d0a4ff463..defe3f87d 100644 --- a/src/engine/platform/sound/c140.h +++ b/src/engine/platform/sound/c140.h @@ -69,7 +69,7 @@ struct c140_t struct c140_voice_t voice[24]; signed int lout, rout; signed short mulaw[256]; - unsigned short (*read_sample)(unsigned char bank, unsigned short addr); + signed short *sample_mem; }; void c140_tick(struct c140_t *c140, const int cycle); diff --git a/src/engine/song.h b/src/engine/song.h index 7fc8dc2a8..803f87360 100644 --- a/src/engine/song.h +++ b/src/engine/song.h @@ -129,7 +129,8 @@ enum DivSystem { DIV_SYSTEM_SM8521, DIV_SYSTEM_PV1000, DIV_SYSTEM_K053260, - DIV_SYSTEM_TED + DIV_SYSTEM_TED, + DIV_SYSTEM_C140 }; enum DivEffectType: unsigned short { diff --git a/src/engine/sysDef.cpp b/src/engine/sysDef.cpp index 08ed8f4f5..39caf88b4 100644 --- a/src/engine/sysDef.cpp +++ b/src/engine/sysDef.cpp @@ -1872,6 +1872,18 @@ void DivEngine::registerSystems() { {} ); + // TODO: Custom sample format + sysDefs[DIV_SYSTEM_C140]=new DivSysDef( + "Namco C140", NULL, 0xfe/*Placeholder*/, 0, 24, false, true, 0x161, false, 1U<writeC(0); } break; + case DIV_SYSTEM_C140: + for (int i=0; i<24; i++) { + w->writeC(0xd4); // mute + w->writeS(baseAddr2S|(i<<4)|0); + w->writeC(0); + w->writeC(0xd4); + w->writeS(baseAddr2S|(i<<4)|1); + w->writeC(0); + w->writeC(0xd4); // keyoff + w->writeS(baseAddr2S|(i<<4)|5); + w->writeC(0); + } + break; default: break; } @@ -1044,6 +1057,11 @@ void DivEngine::performVGMWrite(SafeWriter* w, DivSystem sys, DivRegWrite& write w->writeC(baseAddr2|(write.addr&0x3f)); w->writeC(write.val&0xff); break; + case DIV_SYSTEM_C140: + w->writeC(0xd4); + w->writeC(baseAddr2S|(write.addr&0x1ff)); + w->writeC(write.val&0xff); + break; default: logW("write not handled!"); break; @@ -1217,6 +1235,7 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop, int version, bool p DivDispatch* writeMSM6295[2]={NULL,NULL}; DivDispatch* writeGA20[2]={NULL,NULL}; DivDispatch* writeK053260[2]={NULL,NULL}; + DivDispatch* writeC140[2]={NULL,NULL}; DivDispatch* writeNES[2]={NULL,NULL}; int writeNESIndex[2]={0,0}; @@ -1765,6 +1784,21 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop, int version, bool p willExport[i]=true; } break; + case DIV_SYSTEM_C140: + if (!hasNamco) { + hasNamco=disCont[i].dispatch->chipClock; + CHIP_VOL(40,0.4); + willExport[i]=true; + writeC140[0]=disCont[i].dispatch; + } else if (!(hasNamco&0x40000000)) { + isSecond[i]=true; + CHIP_VOL_SECOND(40,0.4); + willExport[i]=true; + writeC140[1]=disCont[i].dispatch; + hasNamco|=0x40000000; + howManyChips++; + } + break; default: break; } @@ -2163,6 +2197,15 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop, int version, bool p w->writeI(0); w->write(writeES5506[i]->getSampleMem(),writeES5506[i]->getSampleMemUsage()); } + if (writeC140[i]!=NULL && writeC140[i]->getSampleMemUsage()>0) { + w->writeC(0x67); + w->writeC(0x66); + w->writeC(0x8d); + w->writeI((writeC140[i]->getSampleMemUsage()+8)|(i*0x80000000)); + w->writeI(writeC140[i]->getSampleMemCapacity()); + w->writeI(0); + w->write(writeC140[i]->getSampleMem(),writeC140[i]->getSampleMemUsage()); + } } // initialize streams diff --git a/src/gui/dataList.cpp b/src/gui/dataList.cpp index 8e9bc33ec..edc1a9145 100644 --- a/src/gui/dataList.cpp +++ b/src/gui/dataList.cpp @@ -286,6 +286,10 @@ void FurnaceGUI::insListItem(int i, int dir, int asset) { ImGui::PushStyleColor(ImGuiCol_Text,uiColors[GUI_COLOR_INSTR_TED]); name=fmt::sprintf(ICON_FA_BAR_CHART "##_INS%d",i); break; + case DIV_INS_C140: + ImGui::PushStyleColor(ImGuiCol_Text,uiColors[GUI_COLOR_INSTR_C140]); + name=fmt::sprintf(ICON_FA_BAR_CHART "##_INS%d",i); + break; default: ImGui::PushStyleColor(ImGuiCol_Text,uiColors[GUI_COLOR_INSTR_UNKNOWN]); name=fmt::sprintf(ICON_FA_QUESTION "##_INS%d",i); diff --git a/src/gui/debug.cpp b/src/gui/debug.cpp index d2b2f44ab..a2089f47f 100644 --- a/src/gui/debug.cpp +++ b/src/gui/debug.cpp @@ -54,6 +54,7 @@ #include "../engine/platform/sm8521.h" #include "../engine/platform/pv1000.h" #include "../engine/platform/k053260.h" +#include "../engine/platform/c140.h" #include "../engine/platform/dummy.h" #define COMMON_CHIP_DEBUG \ @@ -1093,6 +1094,23 @@ void putDispatchChan(void* data, int chanNum, int type) { ImGui::TextColored(ch->reverse?colorOn:colorOff,">> Reverse"); break; } + case DIV_SYSTEM_C140: { + DivPlatformC140::Channel* ch=(DivPlatformC140::Channel*)data; + ImGui::Text("> C140"); + COMMON_CHAN_DEBUG; + ImGui::Text("* Sample: %d",ch->sample); + ImGui::Text(" - pos: %d",ch->audPos); + ImGui::Text("- chPanL: %.2x",ch->chPanL); + ImGui::Text("- chPanR: %.2x",ch->chPanR); + ImGui::Text("- chVolL: %.2x",ch->chVolL); + ImGui::Text("- chVolR: %.2x",ch->chVolR); + ImGui::Text("- macroVolMul: %.2x",ch->macroVolMul); + COMMON_CHAN_DEBUG_BOOL; + ImGui::TextColored(ch->volumeChanged.left?colorOn:colorOff,">> VolumeChangedLeft"); + ImGui::TextColored(ch->volumeChanged.right?colorOn:colorOff,">> VolumeChangedRight"); + ImGui::TextColored(ch->setPos?colorOn:colorOff,">> SetPos"); + break; + } default: ImGui::Text("Unimplemented chip! Help!"); break; diff --git a/src/gui/doAction.cpp b/src/gui/doAction.cpp index aca82821a..f74c4243c 100644 --- a/src/gui/doAction.cpp +++ b/src/gui/doAction.cpp @@ -1396,7 +1396,8 @@ void FurnaceGUI::doAction(int what) { i==DIV_INS_ES5506 || i==DIV_INS_K007232 || i==DIV_INS_GA20 || - i==DIV_INS_K053260) { + i==DIV_INS_K053260 || + i==DIV_INS_C140) { makeInsTypeList.push_back(i); } } diff --git a/src/gui/gui.h b/src/gui/gui.h index 0a14965c9..53d887041 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -258,6 +258,7 @@ enum FurnaceGUIColors { GUI_COLOR_INSTR_K053260, GUI_COLOR_INSTR_SCSP, GUI_COLOR_INSTR_TED, + GUI_COLOR_INSTR_C140, GUI_COLOR_INSTR_UNKNOWN, GUI_COLOR_CHANNEL_BG, diff --git a/src/gui/guiConst.cpp b/src/gui/guiConst.cpp index a4c5c956d..f04d51ec2 100644 --- a/src/gui/guiConst.cpp +++ b/src/gui/guiConst.cpp @@ -170,6 +170,7 @@ const char* insTypes[DIV_INS_MAX+1]={ "K053260", "SCSP", "TED", + "C140", NULL }; @@ -934,6 +935,7 @@ const FurnaceGUIColorDef guiColors[GUI_COLOR_MAX]={ D(GUI_COLOR_INSTR_K053260,"",ImVec4(1.0f,0.8f,0.1f,1.0f)), D(GUI_COLOR_INSTR_SCSP,"",ImVec4(0.5f,0.5f,0.5f,1.0f)), D(GUI_COLOR_INSTR_TED,"",ImVec4(0.7f,0.6f,1.0f,1.0f)), + D(GUI_COLOR_INSTR_C140,"",ImVec4(0.7f,0.6f,1.0f,1.0f)), D(GUI_COLOR_INSTR_UNKNOWN,"",ImVec4(0.3f,0.3f,0.3f,1.0f)), D(GUI_COLOR_CHANNEL_BG,"",ImVec4(0.4f,0.6f,0.8f,1.0f)), @@ -1119,6 +1121,7 @@ const int availableSystems[]={ DIV_SYSTEM_PV1000, DIV_SYSTEM_K053260, DIV_SYSTEM_TED, + DIV_SYSTEM_C140, DIV_SYSTEM_PCM_DAC, DIV_SYSTEM_PONG, 0 // don't remove this last one! @@ -1229,6 +1232,7 @@ const int chipsSample[]={ DIV_SYSTEM_PCM_DAC, DIV_SYSTEM_ES5506, DIV_SYSTEM_K053260, + DIV_SYSTEM_C140, 0 // don't remove this last one! }; diff --git a/src/gui/insEdit.cpp b/src/gui/insEdit.cpp index e5f60d86b..6a89e7422 100644 --- a/src/gui/insEdit.cpp +++ b/src/gui/insEdit.cpp @@ -4411,7 +4411,8 @@ void FurnaceGUI::drawInsEdit() { ins->type==DIV_INS_ES5506 || ins->type==DIV_INS_K007232 || ins->type==DIV_INS_GA20 || - ins->type==DIV_INS_K053260) { + ins->type==DIV_INS_K053260 || + ins->type==DIV_INS_C140) { if (ImGui::BeginTabItem((ins->type==DIV_INS_SU)?"Sound Unit":"Sample")) { String sName; bool wannaOpenSMPopup=false; @@ -5397,7 +5398,7 @@ void FurnaceGUI::drawInsEdit() { volMax=31; } if (ins->type==DIV_INS_ADPCMB || ins->type==DIV_INS_YMZ280B || ins->type==DIV_INS_RF5C68 || - ins->type==DIV_INS_GA20) { + ins->type==DIV_INS_GA20 || ins->type==DIV_INS_C140) { volMax=255; } if (ins->type==DIV_INS_QSOUND) { @@ -5457,7 +5458,8 @@ void FurnaceGUI::drawInsEdit() { if (ins->type==DIV_INS_TIA || ins->type==DIV_INS_AMIGA || ins->type==DIV_INS_SCC || ins->type==DIV_INS_PET || ins->type==DIV_INS_SEGAPCM || ins->type==DIV_INS_FM || ins->type==DIV_INS_K007232 || ins->type==DIV_INS_GA20 || - ins->type==DIV_INS_SM8521 || ins->type==DIV_INS_PV1000 || ins->type==DIV_INS_K053260) { + ins->type==DIV_INS_SM8521 || ins->type==DIV_INS_PV1000 || ins->type==DIV_INS_K053260 || + ins->type==DIV_INS_C140) { dutyMax=0; } if (ins->type==DIV_INS_VBOY) { @@ -5565,6 +5567,7 @@ void FurnaceGUI::drawInsEdit() { if (ins->type==DIV_INS_K053260) waveMax=0; if (ins->type==DIV_INS_POKEMINI) waveMax=0; if (ins->type==DIV_INS_TED) waveMax=0; + if (ins->type==DIV_INS_C140) waveMax=0; if (ins->type==DIV_INS_SU || ins->type==DIV_INS_POKEY) waveMax=7; if (ins->type==DIV_INS_PET) { waveMax=8; @@ -5695,6 +5698,10 @@ void FurnaceGUI::drawInsEdit() { panMin=0; panMax=127; } + if (ins->type==DIV_INS_C140) { + panMin=0; + panMax=255; + } if (ins->type==DIV_INS_ES5506) { panMax=4095; } @@ -5780,6 +5787,7 @@ void FurnaceGUI::drawInsEdit() { ins->type==DIV_INS_K007232 || ins->type==DIV_INS_GA20 || ins->type==DIV_INS_K053260 || + ins->type==DIV_INS_C140 || ins->type==DIV_INS_TED) { macroList.push_back(FurnaceGUIMacroDesc("Phase Reset",&ins->std.phaseResetMacro,0,1,32,uiColors[GUI_COLOR_MACRO_OTHER],false,NULL,NULL,true)); } diff --git a/src/gui/presets.cpp b/src/gui/presets.cpp index d23622ad2..9f78f360d 100644 --- a/src/gui/presets.cpp +++ b/src/gui/presets.cpp @@ -2024,6 +2024,12 @@ void FurnaceGUI::initSystemPresets() { ) // "" } ); + ENTRY( + "Namco System 2", { + CH(DIV_SYSTEM_YM2151, 1.0f, 0, ""), + CH(DIV_SYSTEM_C140, 1.0f, 0, "") + } + ); ENTRY( "Taito Arcade", { CH(DIV_SYSTEM_YM2610B, 1.0f, 0, "") @@ -2530,6 +2536,11 @@ void FurnaceGUI::initSystemPresets() { CH(DIV_SYSTEM_K053260, 1.0f, 0, "") } ); + ENTRY( + "Namco C140", { + CH(DIV_SYSTEM_C140, 1.0f, 0, "") + } + ); CATEGORY_END; CATEGORY_BEGIN("Wavetable","chips which use user-specified waveforms to generate sound."); diff --git a/src/gui/sampleEdit.cpp b/src/gui/sampleEdit.cpp index 99ccbd22c..390b8d571 100644 --- a/src/gui/sampleEdit.cpp +++ b/src/gui/sampleEdit.cpp @@ -277,6 +277,11 @@ void FurnaceGUI::drawSampleEdit() { SAMPLE_WARN(warnLength,"K053260: maximum sample length is 65535"); } break; + case DIV_SYSTEM_C140: + if (sample->samples>65535) { + SAMPLE_WARN(warnLength,"C140: maximum sample length is 65535"); + } + break; default: break; } diff --git a/src/gui/settings.cpp b/src/gui/settings.cpp index f663a9e40..26884f51b 100644 --- a/src/gui/settings.cpp +++ b/src/gui/settings.cpp @@ -2671,6 +2671,7 @@ void FurnaceGUI::drawSettings() { UI_COLOR_CONFIG(GUI_COLOR_INSTR_SM8521,"SM8521"); UI_COLOR_CONFIG(GUI_COLOR_INSTR_PV1000,"PV-1000"); UI_COLOR_CONFIG(GUI_COLOR_INSTR_K053260,"K053260"); + UI_COLOR_CONFIG(GUI_COLOR_INSTR_C140,"C140"); UI_COLOR_CONFIG(GUI_COLOR_INSTR_UNKNOWN,"Other/Unknown"); ImGui::TreePop(); } diff --git a/src/gui/sysConf.cpp b/src/gui/sysConf.cpp index 9e54ae7fd..278b489b9 100644 --- a/src/gui/sysConf.cpp +++ b/src/gui/sysConf.cpp @@ -2052,6 +2052,7 @@ bool FurnaceGUI::drawSysConf(int chan, DivSystem type, DivConfig& flags, bool mo case DIV_SYSTEM_GA20: case DIV_SYSTEM_PV1000: case DIV_SYSTEM_VERA: + case DIV_SYSTEM_C140: break; case DIV_SYSTEM_YMU759: supportsCustomRate=false; diff --git a/src/gui/sysPartNumber.cpp b/src/gui/sysPartNumber.cpp index 94dc822b8..22a602663 100644 --- a/src/gui/sysPartNumber.cpp +++ b/src/gui/sysPartNumber.cpp @@ -265,6 +265,9 @@ const char* FurnaceGUI::getSystemPartNumber(DivSystem sys, DivConfig& flags) { case DIV_SYSTEM_TED: return "TED"; break; + case DIV_SYSTEM_C140: + return "C140"; + break; default: return FurnaceGUI::getSystemName(sys); break; From 1e2cd8282a918ca6f26fcc43cb91dfab0773aa23 Mon Sep 17 00:00:00 2001 From: cam900 Date: Wed, 9 Aug 2023 20:33:42 +0900 Subject: [PATCH 03/22] Change Mulaw algorithm --- src/engine/platform/sound/c140.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/engine/platform/sound/c140.c b/src/engine/platform/sound/c140.c index 7481ac77e..da5d997fd 100644 --- a/src/engine/platform/sound/c140.c +++ b/src/engine/platform/sound/c140.c @@ -43,6 +43,7 @@ static int c140_max(int a, int b) { return (a > b) ? a : b; } static int c140_min(int a, int b) { return (a < b) ? a : b; } static int c140_clamp(int v, int min, int max) { return c140_min(c140_max(v,min),max); } static int c140_bit(int val, int bit) { return (val >> bit) & 1; } +static int c140_bitfield(int val, int bit, int len) { return (val >> bit) & ((1 << len) - 1);} void c140_tick(struct c140_t *c140, const int cycle) { @@ -117,15 +118,18 @@ void c140_init(struct c140_t *c140) // u-law table verified from Wii Virtual Console Arcade Starblade for (int i = 0; i < 256; i++) { - int j = (signed char)i; - signed char s1 = j & 7; - signed char s2 = abs(j >> 3) & 31; - - c140->mulaw[i] = 0x80 << s1 & 0xff00; - c140->mulaw[i] += s2 << (s1 ? s1 + 3 : 4); - - if (j < 0) - c140->mulaw[i] = -c140->mulaw[i]; + const unsigned char exponent = c140_bitfield(i, 0, 3); + const unsigned char mantissa = c140_bitfield(i, 3, 4); + if (c140_bit(i, 7)) + { + c140->mulaw[i] = (signed short)(((exponent ? 0xfe00 : 0xff00) | (mantissa << 4)) + << (exponent ? exponent - 1 : 0)); + } + else + { + c140->mulaw[i] = (signed short)(((exponent ? 0x100 : 0) | (mantissa << 4)) + << (exponent ? exponent - 1 : 0)); + } } } From 85aa3736ade17a623d1457f8a88eb6bb12c72d74 Mon Sep 17 00:00:00 2001 From: cam900 Date: Wed, 9 Aug 2023 20:34:57 +0900 Subject: [PATCH 04/22] oops --- src/engine/vgmOps.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/vgmOps.cpp b/src/engine/vgmOps.cpp index f5e6d53dc..48afca4c4 100644 --- a/src/engine/vgmOps.cpp +++ b/src/engine/vgmOps.cpp @@ -1059,7 +1059,7 @@ void DivEngine::performVGMWrite(SafeWriter* w, DivSystem sys, DivRegWrite& write break; case DIV_SYSTEM_C140: w->writeC(0xd4); - w->writeC(baseAddr2S|(write.addr&0x1ff)); + w->writeS(baseAddr2S|(write.addr&0x1ff)); w->writeC(write.val&0xff); break; default: From 46b83833e1e1c6003ab1839658ddb3529f20d81e Mon Sep 17 00:00:00 2001 From: cam900 Date: Wed, 9 Aug 2023 20:36:59 +0900 Subject: [PATCH 05/22] Less louder output --- src/engine/platform/sound/c140.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/platform/sound/c140.c b/src/engine/platform/sound/c140.c index da5d997fd..dc0ff02ec 100644 --- a/src/engine/platform/sound/c140.c +++ b/src/engine/platform/sound/c140.c @@ -56,8 +56,8 @@ void c140_tick(struct c140_t *c140, const int cycle) c140->rout += c140->voice[i].rout; } // scale as 16bit - c140->lout >>= 8; - c140->rout >>= 8; + c140->lout >>= 13; + c140->rout >>= 13; } void c140_voice_tick(struct c140_t *c140, const unsigned char voice, const int cycle) From 72d04777c013988ed8cf6da27c62a9d784a59dff Mon Sep 17 00:00:00 2001 From: cam900 Date: Wed, 9 Aug 2023 20:38:17 +0900 Subject: [PATCH 06/22] Less silent output --- src/engine/platform/sound/c140.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/platform/sound/c140.c b/src/engine/platform/sound/c140.c index dc0ff02ec..948b2f074 100644 --- a/src/engine/platform/sound/c140.c +++ b/src/engine/platform/sound/c140.c @@ -56,8 +56,8 @@ void c140_tick(struct c140_t *c140, const int cycle) c140->rout += c140->voice[i].rout; } // scale as 16bit - c140->lout >>= 13; - c140->rout >>= 13; + c140->lout >>= 10; + c140->rout >>= 10; } void c140_voice_tick(struct c140_t *c140, const unsigned char voice, const int cycle) From 21d1dfefa1bc28ddca87c445fdb7f764fc2c6af1 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Wed, 9 Aug 2023 22:00:03 -0500 Subject: [PATCH 07/22] many changes --- src/engine/platform/c140.cpp | 67 ++++++++++++-------------------- src/engine/platform/c140.h | 19 ++------- src/engine/platform/sound/c140.c | 12 ++++-- src/engine/platform/sound/c140.h | 7 +++- src/gui/debug.cpp | 2 - 5 files changed, 43 insertions(+), 64 deletions(-) diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index 5330443ac..95b9dc021 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -80,9 +80,8 @@ void DivPlatformC140::tick(bool sysTick) { chan[i].std.next(); if (chan[i].std.vol.had) { chan[i].outVol=(chan[i].vol*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul; - if (!isMuted[i]) { - chan[i].volumeChanged.changed=0xff; - } + chan[i].volChangedL=true; + chan[i].volChangedR=true; } if (NEW_ARP_STRAT) { chan[i].handleArp(); @@ -103,16 +102,12 @@ void DivPlatformC140::tick(bool sysTick) { } if (chan[i].std.panL.had) { chan[i].chPanL=chan[i].std.panL.val&255; - if (!isMuted[i]) { - chan[i].volumeChanged.left=1; - } + chan[i].volChangedL=true; } if (chan[i].std.panR.had) { chan[i].chPanR=chan[i].std.panR.val&255; - if (!isMuted[i]) { - chan[i].volumeChanged.right=1; - } + chan[i].volChangedR=true; } if (chan[i].std.phaseReset.had) { @@ -121,24 +116,15 @@ void DivPlatformC140::tick(bool sysTick) { chan[i].setPos=true; } } - if (chan[i].volumeChanged.changed) { - if (chan[i].volumeChanged.left) { - if (isMuted[i]) { - chan[i].chVolL=0; - } else { - chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255; - } - rWrite(1+(i<<4),chan[i].chVolL); - } - if (chan[i].volumeChanged.right) { - if (isMuted[i]) { - chan[i].chVolR=0; - } else { - chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255; - } - rWrite(0+(i<<4),chan[i].chVolR); - } - chan[i].volumeChanged.changed=0; + if (chan[i].volChangedL) { + chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255; + rWrite(1+(i<<4),chan[i].chVolL); + chan[i].volChangedL=false; + } + if (chan[i].volChangedR) { + chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255; + rWrite(0+(i<<4),chan[i].chVolR); + chan[i].volChangedR=false; } if (chan[i].setPos) { // force keyon @@ -178,9 +164,8 @@ void DivPlatformC140::tick(bool sysTick) { rWrite(0x0b+(i<<4),loop&0xff); if (!chan[i].std.vol.had) { chan[i].outVol=chan[i].vol; - if (!isMuted[i]) { - chan[i].volumeChanged.changed=0xff; - } + chan[i].volChangedL=true; + chan[i].volChangedR=true; } chan[i].keyOn=false; } @@ -221,9 +206,8 @@ int DivPlatformC140::dispatch(DivCommand c) { chan[c.chan].macroInit(ins); if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { chan[c.chan].outVol=chan[c.chan].vol; - if (!isMuted[c.chan]) { - chan[c.chan].volumeChanged.changed=0xff; - } + chan[c.chan].volChangedL=true; + chan[c.chan].volChangedR=true; } break; } @@ -247,9 +231,8 @@ int DivPlatformC140::dispatch(DivCommand c) { if (!chan[c.chan].std.vol.has) { chan[c.chan].outVol=c.value; } - if (!isMuted[c.chan]) { - chan[c.chan].volumeChanged.changed=0xff; - } + chan[c.chan].volChangedL=true; + chan[c.chan].volChangedR=true; break; case DIV_CMD_GET_VOLUME: if (chan[c.chan].std.vol.has) { @@ -260,9 +243,8 @@ int DivPlatformC140::dispatch(DivCommand c) { case DIV_CMD_PANNING: chan[c.chan].chPanL=c.value; chan[c.chan].chPanR=c.value2; - if (!isMuted[c.chan]) { - chan[c.chan].volumeChanged.changed=0xff; - } + chan[c.chan].volChangedL=true; + chan[c.chan].volChangedR=true; break; case DIV_CMD_PITCH: chan[c.chan].pitch=c.value; @@ -328,14 +310,15 @@ int DivPlatformC140::dispatch(DivCommand c) { void DivPlatformC140::muteChannel(int ch, bool mute) { isMuted[ch]=mute; - chan[ch].volumeChanged.changed=0xff; + c140.voice[ch].muted=mute; } void DivPlatformC140::forceIns() { for (int i=0; i<24; i++) { chan[i].insChanged=true; chan[i].freqChanged=true; - chan[i].volumeChanged.changed=0xff; + chan[i].volChangedL=true; + chan[i].volChangedR=true; chan[i].sample=-1; } } @@ -480,7 +463,7 @@ int DivPlatformC140::init(DivEngine* p, int channels, int sugRate, const DivConf isMuted[i]=false; oscBuf[i]=new DivDispatchOscBuffer; } - sampleMem=new signed short[getSampleMemCapacity()/sizeof(short)]; + sampleMem=new short[getSampleMemCapacity()>>1]; sampleMemLen=0; c140_init(&c140); c140.sample_mem=sampleMem; diff --git a/src/engine/platform/c140.h b/src/engine/platform/c140.h index 5604a794e..fa221eec2 100644 --- a/src/engine/platform/c140.h +++ b/src/engine/platform/c140.h @@ -26,33 +26,20 @@ class DivPlatformC140: public DivDispatch { struct Channel: public SharedChannel { - struct VolumeUpdate { - union { // pack flag bits in single byte - struct { // flag bits - unsigned char left : 1; // left volume - unsigned char right : 1; // right volume - unsigned char reserved : 6; - }; - unsigned char changed = 0; // Packed flags are stored here - }; - - VolumeUpdate(): - changed(0) {} - } volumeChanged; - unsigned int audPos; int sample, wave; - bool setPos; + bool setPos, volChangedL, volChangedR; int chPanL, chPanR; int chVolL, chVolR; int macroVolMul; Channel(): SharedChannel(255), - volumeChanged(VolumeUpdate()), audPos(0), sample(-1), wave(-1), setPos(false), + volChangedL(false), + volChangedR(false), chPanL(255), chPanR(255), chVolL(255), diff --git a/src/engine/platform/sound/c140.c b/src/engine/platform/sound/c140.c index 948b2f074..d22677b24 100644 --- a/src/engine/platform/sound/c140.c +++ b/src/engine/platform/sound/c140.c @@ -52,8 +52,10 @@ void c140_tick(struct c140_t *c140, const int cycle) for (int i = 0; i < 24; i++) { c140_voice_tick(c140, i, cycle); - c140->lout += c140->voice[i].lout; - c140->rout += c140->voice[i].rout; + if (!c140->voice[i].muted) { + c140->lout += c140->voice[i].lout; + c140->rout += c140->voice[i].rout; + } } // scale as 16bit c140->lout >>= 10; @@ -131,6 +133,10 @@ void c140_init(struct c140_t *c140) << (exponent ? exponent - 1 : 0)); } } + for (int i = 0; i < 24; i++) + { + c140->voice[i].muted = false; + } } void c140_reset(struct c140_t *c140) @@ -186,4 +192,4 @@ void c140_write(struct c140_t *c140, const unsigned short addr, const unsigned c } } // Timer -} \ No newline at end of file +} diff --git a/src/engine/platform/sound/c140.h b/src/engine/platform/sound/c140.h index defe3f87d..6fe2c4930 100644 --- a/src/engine/platform/sound/c140.h +++ b/src/engine/platform/sound/c140.h @@ -2,9 +2,13 @@ ============================================================================ -Namco C140 sound emulator +MODIFIED Namco C140 sound emulator - MODIFIED VERSION by cam900 +MODIFICATION by tildearrow - adds muting function +THIS IS NOT THE ORIGINAL VERSION - you can find the original one in +commit 72d04777c013988ed8cf6da27c62a9d784a59dff + This file is licensed under zlib license. ============================================================================ @@ -49,6 +53,7 @@ extern "C" struct c140_voice_t { + bool muted; // muted - can be set by user bool busy; // busy flag bool keyon; // key on flag unsigned short freq; // sample frequency diff --git a/src/gui/debug.cpp b/src/gui/debug.cpp index a2089f47f..6e4a091da 100644 --- a/src/gui/debug.cpp +++ b/src/gui/debug.cpp @@ -1106,8 +1106,6 @@ void putDispatchChan(void* data, int chanNum, int type) { ImGui::Text("- chVolR: %.2x",ch->chVolR); ImGui::Text("- macroVolMul: %.2x",ch->macroVolMul); COMMON_CHAN_DEBUG_BOOL; - ImGui::TextColored(ch->volumeChanged.left?colorOn:colorOff,">> VolumeChangedLeft"); - ImGui::TextColored(ch->volumeChanged.right?colorOn:colorOff,">> VolumeChangedRight"); ImGui::TextColored(ch->setPos?colorOn:colorOff,">> SetPos"); break; } From 4d7d610f8c84e32885a052f1c8cd5c2e6d718c04 Mon Sep 17 00:00:00 2001 From: cam900 Date: Thu, 10 Aug 2023 20:24:10 +0900 Subject: [PATCH 08/22] C140 fixes and optimizations Minor optimization Move output shift function to dispatch Fix loop Fix pan scaling if using INS_AMIGA correct about --- src/engine/platform/c140.cpp | 13 +++++-- src/engine/platform/c140.h | 4 +- src/engine/platform/sound/c140.c | 64 ++++++++++++++++++-------------- src/engine/platform/sound/c140.h | 2 +- src/gui/about.cpp | 2 +- src/gui/debug.cpp | 3 ++ src/main.cpp | 2 +- 7 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index 95b9dc021..7685ebefb 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -59,6 +59,9 @@ void DivPlatformC140::acquire(short** buf, size_t len) { } c140_tick(&c140, 1); + // scale as 16bit + c140.lout >>= 10; + c140.rout >>= 10; if (c140.lout<-32768) c140.lout=-32768; if (c140.lout>32767) c140.lout=32767; @@ -101,12 +104,12 @@ void DivPlatformC140::tick(bool sysTick) { chan[i].freqChanged=true; } if (chan[i].std.panL.had) { - chan[i].chPanL=chan[i].std.panL.val&255; + chan[i].chPanL=(255*(chan[i].std.panL.val&255))/chan[i].macroPanMul; chan[i].volChangedL=true; } if (chan[i].std.panR.had) { - chan[i].chPanR=chan[i].std.panR.val&255; + chan[i].chPanR=(255*(chan[i].std.panR.val&255))/chan[i].macroPanMul; chan[i].volChangedR=true; } @@ -147,13 +150,14 @@ void DivPlatformC140::tick(bool sysTick) { unsigned int end=0; if (chan[i].sample>=0 && chan[i].samplesong.sampleLen) { start=sampleOff[chan[i].sample]; - end=MIN(start+s->length8,getSampleMemCapacity()-1); + end=MIN(start+s->length8-1,65535); } if (chan[i].audPos>0) { start=start+MIN(chan[i].audPos,s->length8); } if (s->isLoopable()) { - loop=start+s->loopStart; + loop=MIN(start+s->loopStart,65535); + end=MIN(start+s->loopEnd-1,65535); } rWrite(0x05+(i<<4),0); // force keyoff first rWrite(0x06+(i<<4),(start>>8)&0xff); @@ -187,6 +191,7 @@ int DivPlatformC140::dispatch(DivCommand c) { case DIV_CMD_NOTE_ON: { DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA); chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:255; + chan[c.chan].macroPanMul=ins->type==DIV_INS_AMIGA?127:255; if (c.value!=DIV_NOTE_NULL) { chan[c.chan].sample=ins->amiga.getSample(c.value); c.value=ins->amiga.getFreq(c.value); diff --git a/src/engine/platform/c140.h b/src/engine/platform/c140.h index fa221eec2..8d3176300 100644 --- a/src/engine/platform/c140.h +++ b/src/engine/platform/c140.h @@ -32,6 +32,7 @@ class DivPlatformC140: public DivDispatch { int chPanL, chPanR; int chVolL, chVolR; int macroVolMul; + int macroPanMul; Channel(): SharedChannel(255), audPos(0), @@ -44,7 +45,8 @@ class DivPlatformC140: public DivDispatch { chPanR(255), chVolL(255), chVolR(255), - macroVolMul(64) {} + macroVolMul(64), + macroPanMul(127) {} }; Channel chan[24]; DivDispatchOscBuffer* oscBuf[24]; diff --git a/src/engine/platform/sound/c140.c b/src/engine/platform/sound/c140.c index d22677b24..3d48c1300 100644 --- a/src/engine/platform/sound/c140.c +++ b/src/engine/platform/sound/c140.c @@ -2,9 +2,13 @@ ============================================================================ -Namco C140 sound emulator +MODIFIED Namco C140 sound emulator - MODIFIED VERSION by cam900 +MODIFICATION by tildearrow - adds muting function +THIS IS NOT THE ORIGINAL VERSION - you can find the original one in +commit 72d04777c013988ed8cf6da27c62a9d784a59dff + This file is licensed under zlib license. ============================================================================ @@ -52,58 +56,64 @@ void c140_tick(struct c140_t *c140, const int cycle) for (int i = 0; i < 24; i++) { c140_voice_tick(c140, i, cycle); - if (!c140->voice[i].muted) { - c140->lout += c140->voice[i].lout; - c140->rout += c140->voice[i].rout; - } + c140->lout += c140->voice[i].lout; + c140->rout += c140->voice[i].rout; } - // scale as 16bit - c140->lout >>= 10; - c140->rout >>= 10; } -void c140_voice_tick(struct c140_t *c140, const unsigned char voice, const int cycle) +void c140_voice_tick(struct c140_t *c140, const unsigned char v, const int cycle) { - struct c140_voice_t *c140_voice = &c140->voice[voice]; - if (c140_voice->busy && c140_voice->keyon) + struct c140_voice_t *voice = &c140->voice[v]; + if (voice->busy && voice->keyon) { for (int c = 0; c < cycle; c++) { - c140_voice->frac += c140_voice->freq; - if (c140_voice->frac > 0xffff) + voice->frac += voice->freq; + if (voice->frac > 0xffff) { - c140_voice->addr += c140_voice->frac >> 16; - if (c140_voice->addr > c140_voice->end_addr) + voice->addr += voice->frac >> 16; + if (voice->addr > voice->end_addr) { - if (c140_voice->loop) + if (voice->loop) { - c140_voice->addr = (c140_voice->addr + c140_voice->end_addr) - c140_voice->loop_addr; + voice->addr = (voice->addr + voice->loop_addr) - voice->end_addr; } else { - c140_voice->keyon = false; + voice->keyon = false; + voice->lout = 0; + voice->rout = 0; + return; } } - c140_voice->frac &= 0xffff; + voice->frac &= 0xffff; } + } + if (!voice->muted) + { // fetch 12 bit sample - signed short s1 = c140->sample_mem[((unsigned int)(c140_voice->bank) << 16) | c140_voice->addr] & ~0xf; - signed short s2 = c140->sample_mem[((unsigned int)(c140_voice->bank) << 16) | ((c140_voice->addr + 1) & 0xffff)] & ~0xf; - if (c140_voice->compressed) + signed short s1 = c140->sample_mem[((unsigned int)(voice->bank) << 16) | voice->addr] & ~0xf; + signed short s2 = c140->sample_mem[((unsigned int)(voice->bank) << 16) | ((voice->addr + 1) & 0xffff)] & ~0xf; + if (voice->compressed) { s1 = c140->mulaw[(s1 >> 8) & 0xff]; s2 = c140->mulaw[(s2 >> 8) & 0xff]; } // interpolate - signed int sample = s1 + (((c140_voice->frac) * (s2 - s1)) >> 16); - c140_voice->lout = sample * c140_voice->lvol; - c140_voice->rout = sample * c140_voice->rvol; + signed int sample = s1 + (((voice->frac) * (s2 - s1)) >> 16); + voice->lout = sample * voice->lvol; + voice->rout = sample * voice->rvol; + } + else + { + voice->lout = 0; + voice->rout = 0; } } else { - c140_voice->lout = 0; - c140_voice->rout = 0; + voice->lout = 0; + voice->rout = 0; } } diff --git a/src/engine/platform/sound/c140.h b/src/engine/platform/sound/c140.h index 6fe2c4930..21a8b8a47 100644 --- a/src/engine/platform/sound/c140.h +++ b/src/engine/platform/sound/c140.h @@ -79,7 +79,7 @@ struct c140_t void c140_tick(struct c140_t *c140, const int cycle); -void c140_voice_tick(struct c140_t *c140, const unsigned char voice, const int cycle); +void c140_voice_tick(struct c140_t *c140, const unsigned char v, const int cycle); void c140_keyon(struct c140_voice_t *c140_voice); diff --git a/src/gui/about.cpp b/src/gui/about.cpp index ea983787e..48fdc2582 100644 --- a/src/gui/about.cpp +++ b/src/gui/about.cpp @@ -195,7 +195,7 @@ const char* aboutLine[]={ "vgsound_emu (second version, modified version) by cam900", "SM8521 emulator (modified version) by cam900", "D65010G031 emulator (modified version) by cam900", - "Namco C140 emulator by cam900", + "Namco C140 (modified version) emulator by cam900", "", "greetings to:", "NEOART Costa Rica", diff --git a/src/gui/debug.cpp b/src/gui/debug.cpp index 6e4a091da..508ad50fe 100644 --- a/src/gui/debug.cpp +++ b/src/gui/debug.cpp @@ -1105,7 +1105,10 @@ void putDispatchChan(void* data, int chanNum, int type) { ImGui::Text("- chVolL: %.2x",ch->chVolL); ImGui::Text("- chVolR: %.2x",ch->chVolR); ImGui::Text("- macroVolMul: %.2x",ch->macroVolMul); + ImGui::Text("- macroPanMul: %.2x",ch->macroPanMul); COMMON_CHAN_DEBUG_BOOL; + ImGui::TextColored(ch->volChangedL?colorOn:colorOff,">> VolChangedL"); + ImGui::TextColored(ch->volChangedR?colorOn:colorOff,">> VolChangedR"); ImGui::TextColored(ch->setPos?colorOn:colorOff,">> SetPos"); break; } diff --git a/src/main.cpp b/src/main.cpp index d1fb2dc12..2c12678a9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -210,7 +210,7 @@ TAParamResult pVersion(String) { printf("- ASAP POKEY emulator by Piotr Fusik ported to C++ by laoo (GPLv2)\n"); printf("- SM8521 emulator (modified version) by cam900 (zlib license)\n"); printf("- D65010G031 emulator (modified version) by cam900 (zlib license)\n"); - printf("- C140 emulator by cam900 (zlib license)\n"); + printf("- C140 emulator (modified version) by cam900 (zlib license)\n"); return TA_PARAM_QUIT; } From 882b1c0aae531f233a3ece2007794229501dbba6 Mon Sep 17 00:00:00 2001 From: cam900 Date: Thu, 10 Aug 2023 20:27:30 +0900 Subject: [PATCH 09/22] Add C140 status debug window --- src/gui/debug.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/gui/debug.cpp b/src/gui/debug.cpp index 508ad50fe..c578d3a30 100644 --- a/src/gui/debug.cpp +++ b/src/gui/debug.cpp @@ -547,6 +547,13 @@ void putDispatchChip(void* data, int type) { COMMON_CHIP_DEBUG_BOOL; break; } + case DIV_SYSTEM_C140: { + DivPlatformC140* ch=(DivPlatformC140*)data; + ImGui::Text("> C140"); + COMMON_CHIP_DEBUG; + COMMON_CHIP_DEBUG_BOOL; + break; + } default: ImGui::Text("Unimplemented chip! Help!"); break; From 139ef4be29d05bf4764eccd00a62153349b7bbe1 Mon Sep 17 00:00:00 2001 From: cam900 Date: Thu, 10 Aug 2023 20:42:57 +0900 Subject: [PATCH 10/22] Remove unused variable --- src/engine/platform/c140.cpp | 4 ---- src/engine/platform/c140.h | 2 -- 2 files changed, 6 deletions(-) diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index 7685ebefb..b2404eab8 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -446,10 +446,6 @@ void DivPlatformC140::renderSamples(int sysID) { sampleMemLen=memPos+256; } -void DivPlatformC140::setChipModel(int type) { - chipType=type; -} - void DivPlatformC140::setFlags(const DivConfig& flags) { chipClock=32000*256; // 8.192MHz and 12.288MHz input, verified from Assault Schematics CHECK_CUSTOM_CLOCK; diff --git a/src/engine/platform/c140.h b/src/engine/platform/c140.h index 8d3176300..98ab77b63 100644 --- a/src/engine/platform/c140.h +++ b/src/engine/platform/c140.h @@ -51,7 +51,6 @@ class DivPlatformC140: public DivDispatch { Channel chan[24]; DivDispatchOscBuffer* oscBuf[24]; bool isMuted[24]; - int chipType; unsigned int sampleOff[256]; bool sampleLoaded[256]; @@ -83,7 +82,6 @@ class DivPlatformC140: public DivDispatch { void tick(bool sysTick=true); void muteChannel(int ch, bool mute); int getOutputCount(); - void setChipModel(int type); void notifyInsChange(int ins); void notifyWaveChange(int wave); void notifyInsDeletion(void* ins); From e0ca653462fdf4b75fa45826ef18aded0b808c03 Mon Sep 17 00:00:00 2001 From: cam900 Date: Thu, 10 Aug 2023 22:13:13 +0900 Subject: [PATCH 11/22] I forgot --- src/engine/platform/c140.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index b2404eab8..165ec92ca 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -159,6 +159,7 @@ void DivPlatformC140::tick(bool sysTick) { loop=MIN(start+s->loopStart,65535); end=MIN(start+s->loopEnd-1,65535); } + rWrite(0x04+(i<<4),(start>>16)&0xff); rWrite(0x05+(i<<4),0); // force keyoff first rWrite(0x06+(i<<4),(start>>8)&0xff); rWrite(0x07+(i<<4),start&0xff); From 0311d712b12cef646b22910ba6da77dd46f0d3d1 Mon Sep 17 00:00:00 2001 From: cam900 Date: Thu, 10 Aug 2023 22:16:19 +0900 Subject: [PATCH 12/22] Fix audPos --- src/engine/platform/c140.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index 165ec92ca..a7a3b162b 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -145,22 +145,24 @@ void DivPlatformC140::tick(bool sysTick) { if (chan[i].freq>65535) chan[i].freq=65535; ctrl|=(chan[i].active?0x80:0)|((s->isLoopable())?0x10:0); if (chan[i].keyOn) { + unsigned int bank=0; unsigned int start=0; unsigned int loop=0; unsigned int end=0; if (chan[i].sample>=0 && chan[i].samplesong.sampleLen) { - start=sampleOff[chan[i].sample]; + bank=(sampleOff[chan[i].sample]>>16)&0xff; + start=sampleOff[chan[i].sample]&0xffff; end=MIN(start+s->length8-1,65535); } if (chan[i].audPos>0) { - start=start+MIN(chan[i].audPos,s->length8); + start=MIN(start+MIN(chan[i].audPos,s->length8),65535); } if (s->isLoopable()) { loop=MIN(start+s->loopStart,65535); end=MIN(start+s->loopEnd-1,65535); } - rWrite(0x04+(i<<4),(start>>16)&0xff); rWrite(0x05+(i<<4),0); // force keyoff first + rWrite(0x04+(i<<4),bank); rWrite(0x06+(i<<4),(start>>8)&0xff); rWrite(0x07+(i<<4),start&0xff); rWrite(0x08+(i<<4),(end>>8)&0xff); From 27e454e7aa71a867555a2101cf6f195cc5596e81 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 12 Aug 2023 01:54:12 -0500 Subject: [PATCH 13/22] =?UTF-8?q?C140:=20help=20=C2=B5-law=20ISN'T=20WORKI?= =?UTF-8?q?NG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- papers/format.md | 1 + src/engine/platform/c140.cpp | 22 ++++++++++++++++----- src/engine/sample.cpp | 38 ++++++++++++++++++++++++++++++++++++ src/engine/sample.h | 7 ++++++- src/engine/sysDef.cpp | 5 ++--- src/gui/guiConst.cpp | 2 +- src/gui/sampleEdit.cpp | 2 +- 7 files changed, 66 insertions(+), 11 deletions(-) diff --git a/papers/format.md b/papers/format.md index 46bb4c1fa..9eef48522 100644 --- a/papers/format.md +++ b/papers/format.md @@ -219,6 +219,7 @@ size | description | - 0xcb: Casio PV-1000 - 3 channels | - 0xcc: K053260 - 4 channels | - 0xcd: TED - 2 channels + | - 0xce: Namco C140 - 24 channels | - 0xde: YM2610B extended - 19 channels | - 0xe0: QSound - 19 channels | - 0xfc: Pong - 1 channel diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index a7a3b162b..55f4983ad 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -143,7 +143,7 @@ void DivPlatformC140::tick(bool sysTick) { chan[i].freq=(int)(off*parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,2,chan[i].pitch2,chipClock,CHIP_FREQBASE)); if (chan[i].freq<0) chan[i].freq=0; if (chan[i].freq>65535) chan[i].freq=65535; - ctrl|=(chan[i].active?0x80:0)|((s->isLoopable())?0x10:0); + ctrl|=(chan[i].active?0x80:0)|((s->isLoopable())?0x10:0)|((s->depth==DIV_SAMPLE_DEPTH_MULAW)?0x08:0); if (chan[i].keyOn) { unsigned int bank=0; unsigned int start=0; @@ -174,7 +174,6 @@ void DivPlatformC140::tick(bool sysTick) { chan[i].volChangedL=true; chan[i].volChangedR=true; } - chan[i].keyOn=false; } if (chan[i].keyOff) { chan[i].keyOff=false; @@ -184,7 +183,10 @@ void DivPlatformC140::tick(bool sysTick) { rWrite(0x03+(i<<4),chan[i].freq&0xff); chan[i].freqChanged=false; } - rWrite(0x05+(i<<4),ctrl); + if (chan[i].keyOn) { + rWrite(0x05+(i<<4),ctrl); + chan[i].keyOn=false; + } } } } @@ -437,10 +439,20 @@ void DivPlatformC140::renderSamples(int sysID) { break; } if (memPos+length>=(getSampleMemCapacity())) { - memcpy(sampleMem+(memPos/sizeof(short)),s->data16,(getSampleMemCapacity())-memPos); + if (s->depth==DIV_SAMPLE_DEPTH_MULAW) { + + } else { + memcpy(sampleMem+(memPos/sizeof(short)),s->data16,(getSampleMemCapacity())-memPos); + } logW("out of C140 memory for sample %d!",i); } else { - memcpy(sampleMem+(memPos/sizeof(short)),s->data16,length); + if (s->depth==DIV_SAMPLE_DEPTH_MULAW) { + for (unsigned int i=0; idataMuLaw[i]<<8)^0xff00; + } + } else { + memcpy(sampleMem+(memPos/sizeof(short)),s->data16,length); + } } sampleOff[i]=memPos>>1; sampleLoaded[i]=true; diff --git a/src/engine/sample.cpp b/src/engine/sample.cpp index 17cd235c1..127f3eff6 100644 --- a/src/engine/sample.cpp +++ b/src/engine/sample.cpp @@ -269,6 +269,9 @@ int DivSample::getSampleOffset(int offset, int length, DivSampleDepth depth) { case DIV_SAMPLE_DEPTH_VOX: off=(offset+1)/2; break; + case DIV_SAMPLE_DEPTH_MULAW: + off=offset; + break; case DIV_SAMPLE_DEPTH_16BIT: off=offset*2; break; @@ -316,6 +319,10 @@ int DivSample::getSampleOffset(int offset, int length, DivSampleDepth depth) { off=(offset+1)/2; len=(length+1)/2; break; + case DIV_SAMPLE_DEPTH_MULAW: + off=offset; + len=length; + break; case DIV_SAMPLE_DEPTH_16BIT: off=offset*2; len=length*2; @@ -365,6 +372,9 @@ int DivSample::getEndPosition(DivSampleDepth depth) { case DIV_SAMPLE_DEPTH_VOX: off=lengthVOX; break; + case DIV_SAMPLE_DEPTH_MULAW: + off=lengthMuLaw; + break; case DIV_SAMPLE_DEPTH_16BIT: off=length16; break; @@ -538,6 +548,12 @@ bool DivSample::initInternal(DivSampleDepth d, int count) { dataVOX=new unsigned char[lengthVOX]; memset(dataVOX,0,lengthVOX); break; + case DIV_SAMPLE_DEPTH_MULAW: // 8-bit µ-law + if (dataMuLaw!=NULL) delete[] dataMuLaw; + lengthMuLaw=count; + dataMuLaw=new unsigned char[(count+4095)&(~0xfff)]; + memset(dataMuLaw,0,(count+4095)&(~0xfff)); + break; case DIV_SAMPLE_DEPTH_16BIT: // 16-bit if (data16!=NULL) delete[] data16; length16=count*2; @@ -1155,6 +1171,13 @@ void DivSample::render(unsigned int formatMask) { case DIV_SAMPLE_DEPTH_VOX: // VOX oki_decode(dataVOX,data16,samples); break; + case DIV_SAMPLE_DEPTH_MULAW: // 8-bit µ-law PCM + for (unsigned int i=0; i>24)|((si&0x03f80000)>>19))^0xff; + } + } } void* DivSample::getCurBuf() { @@ -1255,6 +1288,8 @@ void* DivSample::getCurBuf() { return dataBRR; case DIV_SAMPLE_DEPTH_VOX: return dataVOX; + case DIV_SAMPLE_DEPTH_MULAW: + return dataMuLaw; case DIV_SAMPLE_DEPTH_16BIT: return data16; default: @@ -1283,6 +1318,8 @@ unsigned int DivSample::getCurBufLen() { return lengthBRR; case DIV_SAMPLE_DEPTH_VOX: return lengthVOX; + case DIV_SAMPLE_DEPTH_MULAW: + return lengthMuLaw; case DIV_SAMPLE_DEPTH_16BIT: return length16; default: @@ -1392,4 +1429,5 @@ DivSample::~DivSample() { if (dataB) delete[] dataB; if (dataBRR) delete[] dataBRR; if (dataVOX) delete[] dataVOX; + if (dataMuLaw) delete[] dataMuLaw; } diff --git a/src/engine/sample.h b/src/engine/sample.h index ce1bf1fa2..bbf90d4f5 100644 --- a/src/engine/sample.h +++ b/src/engine/sample.h @@ -43,6 +43,7 @@ enum DivSampleDepth: unsigned char { DIV_SAMPLE_DEPTH_8BIT=8, DIV_SAMPLE_DEPTH_BRR=9, DIV_SAMPLE_DEPTH_VOX=10, + DIV_SAMPLE_DEPTH_MULAW=11, DIV_SAMPLE_DEPTH_16BIT=16, DIV_SAMPLE_DEPTH_MAX // boundary for sample depth }; @@ -108,6 +109,7 @@ struct DivSample { // - 8: 8-bit PCM // - 9: BRR (SNES) // - 10: VOX ADPCM + // - 11: 8-bit µ-law PCM // - 16: 16-bit PCM DivSampleDepth depth; bool loop, brrEmphasis, dither; @@ -130,8 +132,9 @@ struct DivSample { unsigned char* dataB; // 6 unsigned char* dataBRR; // 9 unsigned char* dataVOX; // 10 + unsigned char* dataMuLaw; // 11 - unsigned int length8, length16, length1, lengthDPCM, lengthZ, lengthQSoundA, lengthA, lengthB, lengthBRR, lengthVOX; + unsigned int length8, length16, length1, lengthDPCM, lengthZ, lengthQSoundA, lengthA, lengthB, lengthBRR, lengthVOX, lengthMuLaw; unsigned int samples; @@ -337,6 +340,7 @@ struct DivSample { dataB(NULL), dataBRR(NULL), dataVOX(NULL), + dataMuLaw(NULL), length8(0), length16(0), length1(0), @@ -347,6 +351,7 @@ struct DivSample { lengthB(0), lengthBRR(0), lengthVOX(0), + lengthMuLaw(0), samples(0) { for (int i=0; idepth==DIV_SAMPLE_DEPTH_8BIT)?255:32767); if (posX>=0) { - statusBar2=fmt::sprintf("(%d, %d)",posX,posY); + statusBar2=fmt::sprintf("(%d, %d) $%.2x",posX,posY,((unsigned char*)sample->getCurBuf())[posX]); } } From 94cb733c60737b297acc8dc8f46fad5d9ab7eee3 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 12 Aug 2023 02:52:50 -0500 Subject: [PATCH 14/22] fix build error hopefully --- src/engine/platform/c140.cpp | 2 +- src/engine/sample.cpp | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index 55f4983ad..f2ee8dfef 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -448,7 +448,7 @@ void DivPlatformC140::renderSamples(int sysID) { } else { if (s->depth==DIV_SAMPLE_DEPTH_MULAW) { for (unsigned int i=0; idataMuLaw[i]<<8)^0xff00; + sampleMem[i+(memPos/sizeof(short))]=((s->dataMuLaw[i]^0xff)<<8); } } else { memcpy(sampleMem+(memPos/sizeof(short)),s->data16,length); diff --git a/src/engine/sample.cpp b/src/engine/sample.cpp index 127f3eff6..8ddcf2297 100644 --- a/src/engine/sample.cpp +++ b/src/engine/sample.cpp @@ -1128,6 +1128,11 @@ bool DivSample::resample(double sRate, double tRate, int filter) { #define NOT_IN_FORMAT(x) (depth!=x && formatMask&(1U<<(unsigned int)x)) +union IntFloat { + unsigned int i; + float f; +}; + void DivSample::render(unsigned int formatMask) { // step 1: convert to 16-bit if needed if (depth!=DIV_SAMPLE_DEPTH_16BIT) { @@ -1173,9 +1178,10 @@ void DivSample::render(unsigned int formatMask) { break; case DIV_SAMPLE_DEPTH_MULAW: // 8-bit µ-law PCM for (unsigned int i=0; i>24)|((si&0x03f80000)>>19))^0xff; + IntFloat s; + s.f=(float)-data16[i]; + s.f/=32768.0f; + s.i-=0x3f800000; + dataMuLaw[i]=(((s.i&0x80000000)>>24)|((s.i&0x03f80000)>>19))^0xff; } } } From 06243c9d9d29fa81f665d16b854e61feda73dd66 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 12 Aug 2023 02:53:05 -0500 Subject: [PATCH 15/22] asdfasdf --- src/engine/platform/c140.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index f2ee8dfef..aadc7c168 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -448,7 +448,7 @@ void DivPlatformC140::renderSamples(int sysID) { } else { if (s->depth==DIV_SAMPLE_DEPTH_MULAW) { for (unsigned int i=0; idataMuLaw[i]^0xff)<<8); + sampleMem[i+(memPos/sizeof(short))]=(s->dataMuLaw[i]<<8); } } else { memcpy(sampleMem+(memPos/sizeof(short)),s->data16,length); From 320d965f78c931e7ef78d9ffb04c55d29353a2f0 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 12 Aug 2023 03:42:10 -0500 Subject: [PATCH 16/22] =?UTF-8?q?fix=20=C2=B5-law=20encoding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/engine/platform/c140.cpp | 2 +- src/engine/sample.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index aadc7c168..b679aaeee 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -448,7 +448,7 @@ void DivPlatformC140::renderSamples(int sysID) { } else { if (s->depth==DIV_SAMPLE_DEPTH_MULAW) { for (unsigned int i=0; idataMuLaw[i]<<8); + sampleMem[i+(memPos/sizeof(short))]=((s->dataMuLaw[i])<<8); } } else { memcpy(sampleMem+(memPos/sizeof(short)),s->data16,length); diff --git a/src/engine/sample.cpp b/src/engine/sample.cpp index 8ddcf2297..c2ff85a6a 100644 --- a/src/engine/sample.cpp +++ b/src/engine/sample.cpp @@ -1266,10 +1266,11 @@ void DivSample::render(unsigned int formatMask) { if (!initInternal(DIV_SAMPLE_DEPTH_MULAW,samples)) return; for (unsigned int i=0; i>24)|((s.i&0x03f80000)>>19))^0xff; + dataMuLaw[i]=(((data16[i]<0)?0x80:0)|(s.i&0x03f80000)>>19)^0xff; } } } From f12d6012344a44b1fd6bc4b189c004c1adc76fad Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 12 Aug 2023 03:45:08 -0500 Subject: [PATCH 17/22] C140: fix key off --- src/engine/platform/c140.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index b679aaeee..110e73d84 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -137,6 +137,7 @@ void DivPlatformC140::tick(bool sysTick) { chan[i].audPos=0; } if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { + bool writeCtrl=false; DivSample* s=parent->getSample(chan[i].sample); unsigned char ctrl=0; double off=(s->centerRate>=1)?((double)s->centerRate/8363.0):1.0; @@ -174,8 +175,11 @@ void DivPlatformC140::tick(bool sysTick) { chan[i].volChangedL=true; chan[i].volChangedR=true; } + writeCtrl=true; + chan[i].keyOn=false; } if (chan[i].keyOff) { + writeCtrl=true; chan[i].keyOff=false; } if (chan[i].freqChanged) { @@ -183,9 +187,8 @@ void DivPlatformC140::tick(bool sysTick) { rWrite(0x03+(i<<4),chan[i].freq&0xff); chan[i].freqChanged=false; } - if (chan[i].keyOn) { + if (writeCtrl) { rWrite(0x05+(i<<4),ctrl); - chan[i].keyOn=false; } } } From 984a3b745086cfb0e15b4e027d49162ca1a99906 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 12 Aug 2023 04:07:14 -0500 Subject: [PATCH 18/22] YAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA --- src/engine/platform/c140.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index 110e73d84..ce730999f 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -451,7 +451,10 @@ void DivPlatformC140::renderSamples(int sysID) { } else { if (s->depth==DIV_SAMPLE_DEPTH_MULAW) { for (unsigned int i=0; idataMuLaw[i])<<8); + unsigned char x=s->dataMuLaw[i]^0xff; + if (x&0x80) x^=15; + unsigned char c140Mu=(x&0x80)|((x&15)<<3)|((x&0x70)>>4); + sampleMem[i+(memPos/sizeof(short))]=((c140Mu)<<8); } } else { memcpy(sampleMem+(memPos/sizeof(short)),s->data16,length); From 6d1df99254b0a5eb794e1750a659397778bca4e3 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 12 Aug 2023 04:12:10 -0500 Subject: [PATCH 19/22] GUI: remove debug --- src/gui/sampleEdit.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/sampleEdit.cpp b/src/gui/sampleEdit.cpp index b4ab5123c..390b8d571 100644 --- a/src/gui/sampleEdit.cpp +++ b/src/gui/sampleEdit.cpp @@ -1608,7 +1608,7 @@ void FurnaceGUI::drawSampleEdit() { } posY=(0.5-pos.y/rectSize.y)*((sample->depth==DIV_SAMPLE_DEPTH_8BIT)?255:32767); if (posX>=0) { - statusBar2=fmt::sprintf("(%d, %d) $%.2x",posX,posY,((unsigned char*)sample->getCurBuf())[posX]); + statusBar2=fmt::sprintf("(%d, %d)",posX,posY); } } From 1e580d6936c5607345635e5627ecfaae4586ad2f Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 12 Aug 2023 04:12:22 -0500 Subject: [PATCH 20/22] C140: comment --- src/engine/platform/c140.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/engine/platform/c140.cpp b/src/engine/platform/c140.cpp index ce730999f..d98827754 100644 --- a/src/engine/platform/c140.cpp +++ b/src/engine/platform/c140.cpp @@ -441,9 +441,15 @@ void DivPlatformC140::renderSamples(int sysID) { logW("out of C140 memory for sample %d!",i); break; } + // why is C140 not G.711-compliant? this weird bit mangling had me puzzled for 3 hours... if (memPos+length>=(getSampleMemCapacity())) { if (s->depth==DIV_SAMPLE_DEPTH_MULAW) { - + for (unsigned int i=0; i<(getSampleMemCapacity())-memPos; i++) { + unsigned char x=s->dataMuLaw[i]^0xff; + if (x&0x80) x^=15; + unsigned char c140Mu=(x&0x80)|((x&15)<<3)|((x&0x70)>>4); + sampleMem[i+(memPos/sizeof(short))]=((c140Mu)<<8); + } } else { memcpy(sampleMem+(memPos/sizeof(short)),s->data16,(getSampleMemCapacity())-memPos); } From 1f10c31414d504c108ef09d53a8d31824d83560f Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 12 Aug 2023 04:19:20 -0500 Subject: [PATCH 21/22] update ins type --- papers/newIns.md | 1 + src/gui/dataList.cpp | 2 +- src/gui/guiConst.cpp | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/papers/newIns.md b/papers/newIns.md index 786a553c3..4ef3be31d 100644 --- a/papers/newIns.md +++ b/papers/newIns.md @@ -119,6 +119,7 @@ the following instrument types are available: - 49: PV-1000 - 50: K053260 - 52: TED +- 53: C140 the following feature codes are recognized: diff --git a/src/gui/dataList.cpp b/src/gui/dataList.cpp index edc1a9145..fcdc7bb93 100644 --- a/src/gui/dataList.cpp +++ b/src/gui/dataList.cpp @@ -288,7 +288,7 @@ void FurnaceGUI::insListItem(int i, int dir, int asset) { break; case DIV_INS_C140: ImGui::PushStyleColor(ImGuiCol_Text,uiColors[GUI_COLOR_INSTR_C140]); - name=fmt::sprintf(ICON_FA_BAR_CHART "##_INS%d",i); + name=fmt::sprintf(ICON_FA_VOLUME_UP "##_INS%d",i); break; default: ImGui::PushStyleColor(ImGuiCol_Text,uiColors[GUI_COLOR_INSTR_UNKNOWN]); diff --git a/src/gui/guiConst.cpp b/src/gui/guiConst.cpp index 02d844da9..b5369a6c6 100644 --- a/src/gui/guiConst.cpp +++ b/src/gui/guiConst.cpp @@ -935,7 +935,7 @@ const FurnaceGUIColorDef guiColors[GUI_COLOR_MAX]={ D(GUI_COLOR_INSTR_K053260,"",ImVec4(1.0f,0.8f,0.1f,1.0f)), D(GUI_COLOR_INSTR_SCSP,"",ImVec4(0.5f,0.5f,0.5f,1.0f)), D(GUI_COLOR_INSTR_TED,"",ImVec4(0.7f,0.6f,1.0f,1.0f)), - D(GUI_COLOR_INSTR_C140,"",ImVec4(0.7f,0.6f,1.0f,1.0f)), + D(GUI_COLOR_INSTR_C140,"",ImVec4(1.0f,1.0f,0.0f,1.0f)), D(GUI_COLOR_INSTR_UNKNOWN,"",ImVec4(0.3f,0.3f,0.3f,1.0f)), D(GUI_COLOR_CHANNEL_BG,"",ImVec4(0.4f,0.6f,0.8f,1.0f)), From 908ecf411d4fd886b6bd4a71ae76222bf9049d29 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 12 Aug 2023 04:33:33 -0500 Subject: [PATCH 22/22] C140: partially working VGM export --- src/engine/vgmOps.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/engine/vgmOps.cpp b/src/engine/vgmOps.cpp index 48afca4c4..11d3a6a9a 100644 --- a/src/engine/vgmOps.cpp +++ b/src/engine/vgmOps.cpp @@ -1059,7 +1059,7 @@ void DivEngine::performVGMWrite(SafeWriter* w, DivSystem sys, DivRegWrite& write break; case DIV_SYSTEM_C140: w->writeC(0xd4); - w->writeS(baseAddr2S|(write.addr&0x1ff)); + w->writeS_BE(baseAddr2S|(write.addr&0x1ff)); w->writeC(write.val&0xff); break; default: @@ -1786,7 +1786,8 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop, int version, bool p break; case DIV_SYSTEM_C140: if (!hasNamco) { - hasNamco=disCont[i].dispatch->chipClock; + // ?!?!?! + hasNamco=disCont[i].dispatch->rate/2; CHIP_VOL(40,0.4); willExport[i]=true; writeC140[0]=disCont[i].dispatch;