From 5a15721ab7eb98f5767b0cb39981e5734e1fe4d1 Mon Sep 17 00:00:00 2001 From: Natt Akuma Date: Sat, 18 Feb 2023 22:43:29 +0700 Subject: [PATCH] Add QuadTone beeper engine --- CMakeLists.txt | 1 + src/engine/dispatchContainer.cpp | 4 + src/engine/fileOps.cpp | 1 + src/engine/platform/zxbeeperqt.cpp | 389 +++++++++++++++++++++++++++++ src/engine/platform/zxbeeperqt.h | 73 ++++++ src/engine/song.h | 3 +- src/engine/sysDef.cpp | 13 + src/gui/guiConst.cpp | 2 + 8 files changed, 485 insertions(+), 1 deletion(-) create mode 100644 src/engine/platform/zxbeeperqt.cpp create mode 100644 src/engine/platform/zxbeeperqt.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 80e2c2fd..e74d9074 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -523,6 +523,7 @@ src/engine/platform/t6w28.cpp src/engine/platform/vb.cpp src/engine/platform/vera.cpp src/engine/platform/zxbeeper.cpp +src/engine/platform/zxbeeperqt.cpp src/engine/platform/bubsyswsg.cpp src/engine/platform/n163.cpp src/engine/platform/pet.cpp diff --git a/src/engine/dispatchContainer.cpp b/src/engine/dispatchContainer.cpp index 4e4e5d96..17ccee49 100644 --- a/src/engine/dispatchContainer.cpp +++ b/src/engine/dispatchContainer.cpp @@ -59,6 +59,7 @@ #include "platform/lynx.h" #include "platform/pokey.h" #include "platform/zxbeeper.h" +#include "platform/zxbeeperqt.h" #include "platform/bubsyswsg.h" #include "platform/n163.h" #include "platform/pet.h" @@ -386,6 +387,9 @@ void DivDispatchContainer::init(DivSystem sys, DivEngine* eng, int chanCount, do case DIV_SYSTEM_SFX_BEEPER: dispatch=new DivPlatformZXBeeper; break; + case DIV_SYSTEM_SFX_BEEPER_QT: + dispatch=new DivPlatformZXBeeperQT; + break; case DIV_SYSTEM_LYNX: dispatch=new DivPlatformLynx; break; diff --git a/src/engine/fileOps.cpp b/src/engine/fileOps.cpp index 9ba9dd9b..40bb2238 100644 --- a/src/engine/fileOps.cpp +++ b/src/engine/fileOps.cpp @@ -1440,6 +1440,7 @@ void DivEngine::convertOldFlags(unsigned int oldFlags, DivConfig& newFlags, DivS } break; case DIV_SYSTEM_SFX_BEEPER: + case DIV_SYSTEM_SFX_BEEPER_QT: newFlags.set("clockSel",(int)(oldFlags&1)); break; case DIV_SYSTEM_SCC: diff --git a/src/engine/platform/zxbeeperqt.cpp b/src/engine/platform/zxbeeperqt.cpp new file mode 100644 index 00000000..0ac046db --- /dev/null +++ b/src/engine/platform/zxbeeperqt.cpp @@ -0,0 +1,389 @@ +/** + * Furnace Tracker - multi-system chiptune tracker + * Copyright (C) 2021-2022 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 "zxbeeperqt.h" +#include "../engine.h" +#include + +#define rWrite(a,v) regPool[a]=v +#define CHIP_FREQBASE 2048*320 +#define CHIP_DIVIDER 16*13 + +const char** DivPlatformZXBeeperQT::getRegisterSheet() { + return NULL; +} + +void DivPlatformZXBeeperQT::acquire(short** buf, size_t len) { + bool o=false; + for (size_t h=0; h=0 && curSamplesong.sampleLen) { + while (curSamplePeriod>=chan[4].freq) { + DivSample* s=parent->getSample(curSample); + if (s->samples>0) { + o=(!isMuted[4]&&s->data8[curSamplePos++]>0); + if (curSamplePos>=s->samples) curSample=-1; + // (theoretical) 32KiB limit + if (curSamplePos>=32768*8) curSample=-1; + } else { + curSample=-1; + } + curSamplePeriod-=chan[4].freq; + } + curSamplePeriod+=40; + if ((outputClock&3)==0) { + oscBuf[0]->data[oscBuf[0]->needle++]=0; + oscBuf[1]->data[oscBuf[1]->needle++]=0; + oscBuf[2]->data[oscBuf[2]->needle++]=0; + oscBuf[3]->data[oscBuf[3]->needle++]=0; + oscBuf[4]->data[oscBuf[4]->needle++]=o?32767:0; + } + } else { + int ch=outputClock/2; + int b=ch*4; + if ((outputClock&1)==0) { + chan[ch].sPosition+=(regPool[1+b]<<8)|regPool[0+b]; + chan[ch].out=regPool[3+b]+((((chan[ch].sPosition>>8)&0xff)data[oscBuf[4]->needle++]=0; + } + o=chan[ch].out&0x10; + oscBuf[ch]->data[oscBuf[ch]->needle++]=o?32767:0; + chan[ch].out<<=1; + } + outputClock=(outputClock+1)&7; + buf[0][h]=o?32767:0; + } +} + +void DivPlatformZXBeeperQT::tick(bool sysTick) { + for (int i=0; i<4; i++) { + chan[i].std.next(); + if (chan[i].std.vol.had) { + chan[i].outVol=(MIN(chan[i].vol,2)*MIN(chan[i].std.vol.val,2)/2); + writeOutVol(i); + } + if (chan[i].std.duty.had) { + chan[i].duty=chan[i].std.duty.val; + rWrite(2+i*4,chan[i].duty^0xff); + } + 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,-65535,65535); + } else { + chan[i].pitch2=chan[i].std.pitch.val; + } + chan[i].freqChanged=true; + } + if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { + if (chan[i].active) { + chan[i].freq=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>32768) chan[i].freq=32768; + rWrite(0+i*4,chan[i].freq&0xff); + rWrite(1+i*4,chan[i].freq>>8); + } + if (chan[i].keyOn) chan[i].keyOn=false; + if (chan[i].keyOff) chan[i].keyOff=false; + chan[i].freqChanged=false; + } + } + if (NEW_ARP_STRAT) { + chan[4].handleArp(); + } else if (chan[4].std.arp.had) { + if (!chan[4].inPorta) { + chan[4].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[4].note,chan[4].std.arp.val)); + } + chan[4].freqChanged=true; + } + if (chan[4].std.pitch.had) { + if (chan[4].std.pitch.mode) { + chan[4].pitch2+=chan[4].std.pitch.val; + CLAMP_VAR(chan[4].pitch2,-65535,65535); + } else { + chan[4].pitch2=chan[4].std.pitch.val; + } + chan[4].freqChanged=true; + } + if (chan[4].freqChanged || chan[4].keyOn || chan[4].keyOff) { + if (chan[4].active) { + double off=CHIP_DIVIDER; + if (curSample>=0 && curSamplesong.sampleLen) { + DivSample* s=parent->getSample(curSample); + off=(s->centerRate>=1)?(CHIP_DIVIDER*(double)s->centerRate/8363.0):CHIP_DIVIDER; + } + chan[4].freq=parent->calcFreq(chan[4].baseFreq,chan[4].pitch,chan[4].fixedArp?chan[4].baseNoteOverride:chan[4].arpOff,chan[4].fixedArp,true,2,chan[4].pitch2,chipClock,off); + if (chan[4].freq>259) chan[4].freq=259; + if (chan[4].freq<3) chan[4].freq=3; + chan[4].freq*=13; + } + if (chan[4].keyOn) chan[4].keyOn=false; + if (chan[4].keyOff) chan[4].keyOff=false; + chan[4].freqChanged=false; + } +} + +int DivPlatformZXBeeperQT::dispatch(DivCommand c) { + switch (c.cmd) { + case DIV_CMD_NOTE_ON: { + if (c.chan<4) { + DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_BEEPER); + if (c.value!=DIV_NOTE_NULL) { + chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value); + 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); + chan[c.chan].insChanged=false; + if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { + chan[c.chan].outVol=chan[c.chan].vol; + writeOutVol(c.chan); + } + } else { + DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA); + if (c.value!=DIV_NOTE_NULL) { + chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); + chan[c.chan].freqChanged=true; + chan[c.chan].note=c.value; + curSample=ins->amiga.getSample(c.value); + // TODO support offset commands + curSamplePos=0; + curSamplePeriod=0; + } + chan[c.chan].active=true; + chan[c.chan].keyOn=true; + chan[c.chan].macroInit(ins); + chan[c.chan].insChanged=false; + } + break; + } + case DIV_CMD_NOTE_OFF: + chan[c.chan].active=false; + chan[c.chan].keyOff=true; + chan[c.chan].macroInit(NULL); + writeOutVol(c.chan); + if (c.chan>=4) { + curSample=-1; + } + 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; + chan[c.chan].insChanged=true; + } + break; + case DIV_CMD_VOLUME: + if (chan[c.chan].vol!=c.value) { + chan[c.chan].vol=c.value; + if (!chan[c.chan].std.vol.has) { + chan[c.chan].outVol=c.value; + writeOutVol(c.chan); + } + } + 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_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_STD_NOISE_MODE: + chan[c.chan].duty=c.value; + if (c.chan<4) rWrite(2+c.chan*4,chan[c.chan].duty^0xff); + break; + case DIV_CMD_LEGATO: + chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(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_BEEPER)); + } + 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_GET_VOLMAX: + return 2; + 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 DivPlatformZXBeeperQT::writeOutVol(int ch) { + if (ch>=4) return; + unsigned char val=(chan[ch].outVol>=1)?((chan[ch].outVol>=2)?31:7):0; + rWrite(3+ch*4,(!isMuted[ch]&&chan[ch].active)?val:0); +} + +void DivPlatformZXBeeperQT::muteChannel(int ch, bool mute) { + isMuted[ch]=mute; + writeOutVol(ch); +} + +void DivPlatformZXBeeperQT::forceIns() { + for (int i=0; i<5; i++) { + chan[i].insChanged=true; + chan[i].freqChanged=true; + } +} + +void* DivPlatformZXBeeperQT::getChanState(int ch) { + return &chan[ch]; +} + +DivMacroInt* DivPlatformZXBeeperQT::getChanMacroInt(int ch) { + return &chan[ch].std; +} + +DivDispatchOscBuffer* DivPlatformZXBeeperQT::getOscBuffer(int ch) { + return oscBuf[ch]; +} + +unsigned char* DivPlatformZXBeeperQT::getRegisterPool() { + return regPool; +} + +int DivPlatformZXBeeperQT::getRegisterPoolSize() { + return 16; +} + +void DivPlatformZXBeeperQT::reset() { + memset(regPool,0,16); + for (int i=0; i<5; i++) { + chan[i]=DivPlatformZXBeeperQT::Channel(); + chan[i].std.setEngine(parent); + if (i<4) rWrite(2+i*4,128); + } + cycles=0; + curChan=0; + sOffTimer=0; + ulaOut=0; + curSample=-1; + curSamplePos=0; + curSamplePeriod=0; + outputClock=0; +} + +bool DivPlatformZXBeeperQT::keyOffAffectsArp(int ch) { + return true; +} + +void DivPlatformZXBeeperQT::notifyWaveChange(int wave) { +} + +void DivPlatformZXBeeperQT::notifyInsDeletion(void* ins) { + for (int i=0; i<5; i++) { + chan[i].std.notifyInsDeletion((DivInstrument*)ins); + } +} + +void DivPlatformZXBeeperQT::setFlags(const DivConfig& flags) { + if (flags.getInt("clockSel",0)) { + chipClock=COLOR_PAL*4.0/5.0; + } else { + chipClock=COLOR_NTSC; + } + CHECK_CUSTOM_CLOCK; + rate=chipClock/40; + for (int i=0; i<4; i++) { + oscBuf[i]->rate=rate/4; + } +} + +void DivPlatformZXBeeperQT::poke(unsigned int addr, unsigned short val) { + rWrite(addr,val); +} + +void DivPlatformZXBeeperQT::poke(std::vector& wlist) { + for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); +} + +int DivPlatformZXBeeperQT::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { + parent=p; + for (int i=0; i<5; i++) { + isMuted[i]=false; + oscBuf[i]=new DivDispatchOscBuffer; + } + setFlags(flags); + reset(); + return 5; +} + +void DivPlatformZXBeeperQT::quit() { + for (int i=0; i<5; i++) { + delete oscBuf[i]; + } +} + +DivPlatformZXBeeperQT::~DivPlatformZXBeeperQT() { +} diff --git a/src/engine/platform/zxbeeperqt.h b/src/engine/platform/zxbeeperqt.h new file mode 100644 index 00000000..d13bc5ae --- /dev/null +++ b/src/engine/platform/zxbeeperqt.h @@ -0,0 +1,73 @@ +/** + * Furnace Tracker - multi-system chiptune tracker + * Copyright (C) 2021-2022 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 _ZXBEEPERQT_H +#define _ZXBEEPERQT_H + +#include "../dispatch.h" + +class DivPlatformZXBeeperQT: public DivDispatch { + struct Channel: public SharedChannel { + unsigned short sPosition; + unsigned char duty; + unsigned char out; + Channel(): + SharedChannel(2), + sPosition(0), + duty(128), + out(0) {} + }; + Channel chan[5]; + DivDispatchOscBuffer* oscBuf[5]; + bool isMuted[5]; + unsigned char ulaOut; + + int cycles, curChan, sOffTimer, delay, curSample, curSamplePeriod; + unsigned int curSamplePos; + unsigned int outputClock; + unsigned char regPool[16]; + 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); + bool keyOffAffectsArp(int ch); + void setFlags(const DivConfig& flags); + void notifyWaveChange(int wave); + void notifyInsDeletion(void* ins); + void poke(unsigned int addr, unsigned short val); + void poke(std::vector& wlist); + const char** getRegisterSheet(); + int init(DivEngine* parent, int channels, int sugRate, const DivConfig& flags); + void quit(); + ~DivPlatformZXBeeperQT(); + private: + void writeOutVol(int ch); +}; + +#endif diff --git a/src/engine/song.h b/src/engine/song.h index 61083c18..9ac073da 100644 --- a/src/engine/song.h +++ b/src/engine/song.h @@ -124,7 +124,8 @@ enum DivSystem { DIV_SYSTEM_YM2610_CSM, DIV_SYSTEM_YM2610B_CSM, DIV_SYSTEM_YM2203_CSM, - DIV_SYSTEM_YM2608_CSM + DIV_SYSTEM_YM2608_CSM, + DIV_SYSTEM_SFX_BEEPER_QT }; struct DivSubSong { diff --git a/src/engine/sysDef.cpp b/src/engine/sysDef.cpp index da899ae8..4aa7c368 100644 --- a/src/engine/sysDef.cpp +++ b/src/engine/sysDef.cpp @@ -1778,6 +1778,19 @@ void DivEngine::registerSystems() { {DIV_INS_AMIGA, DIV_INS_AMIGA, DIV_INS_AMIGA, DIV_INS_AMIGA} ); + sysDefs[DIV_SYSTEM_SFX_BEEPER_QT]=new DivSysDef( + "ZX Spectrum Beeper (QuadTone Engine)", NULL, 0xc8, 0, 5, false, true, 0, false, 1U<