From 8ea60f37c597c8e613bd432f10a04920ca526f1b Mon Sep 17 00:00:00 2001 From: tildearrow Date: Mon, 23 May 2022 19:01:10 -0500 Subject: [PATCH] MSM6258: start work - DO NOT USE! --- CMakeLists.txt | 1 + src/engine/dispatchContainer.cpp | 4 + src/engine/platform/msm6258.cpp | 334 +++++++++++++++++++++ src/engine/platform/msm6258.h | 129 ++++++++ src/engine/platform/sound/oki/okim6258.cpp | 13 +- src/engine/platform/sound/oki/okim6258.h | 11 +- 6 files changed, 486 insertions(+), 6 deletions(-) create mode 100644 src/engine/platform/msm6258.cpp create mode 100644 src/engine/platform/msm6258.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d8935643..6c26ef6c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -398,6 +398,7 @@ src/engine/platform/fds.cpp src/engine/platform/tia.cpp src/engine/platform/saa.cpp src/engine/platform/amiga.cpp +src/engine/platform/msm6258.cpp src/engine/platform/msm6295.cpp src/engine/platform/pcspkr.cpp src/engine/platform/segapcm.cpp diff --git a/src/engine/dispatchContainer.cpp b/src/engine/dispatchContainer.cpp index e5df04157..4bda45ca5 100644 --- a/src/engine/dispatchContainer.cpp +++ b/src/engine/dispatchContainer.cpp @@ -21,6 +21,7 @@ #include "engine.h" #include "platform/genesis.h" #include "platform/genesisext.h" +#include "platform/msm6258.h" #include "platform/msm6295.h" #include "platform/namcowsg.h" #include "platform/sms.h" @@ -365,6 +366,9 @@ void DivDispatchContainer::init(DivSystem sys, DivEngine* eng, int chanCount, do case DIV_SYSTEM_SOUND_UNIT: dispatch=new DivPlatformSoundUnit; break; + case DIV_SYSTEM_MSM6258: + dispatch=new DivPlatformMSM6258; + break; case DIV_SYSTEM_MSM6295: dispatch=new DivPlatformMSM6295; break; diff --git a/src/engine/platform/msm6258.cpp b/src/engine/platform/msm6258.cpp new file mode 100644 index 000000000..d9a68289f --- /dev/null +++ b/src/engine/platform/msm6258.cpp @@ -0,0 +1,334 @@ +/** + * 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 "msm6258.h" +#include "../engine.h" +#include "../../ta-log.h" +#include "sound/oki/okim6258.h" +#include +#include + +#define rWrite(v) if (!skipRegisterWrites) {writes.emplace(0,v); if (dumpWrites) {addWrite(0,v);} } + +const char** DivPlatformMSM6258::getRegisterSheet() { + return NULL; +} + +const char* DivPlatformMSM6258::getEffectName(unsigned char effect) { + return NULL; +} + +void DivPlatformMSM6258::acquire(short* bufL, short* bufR, size_t start, size_t len) { + for (size_t h=start; hcommand_w(w.val); + writes.pop(); + delay=32; + } + } else { + delay--; + } + + msm->sound_stream_update(outs,1); + bufL[h]=msm->data_w(0)?32767:0; + + /*if (++updateOsc>=22) { + updateOsc=0; + // TODO: per-channel osc + for (int i=0; i<1; i++) { + oscBuf[i]->data[oscBuf[i]->needle++]=msm->m_voice[i].m_muted?0:(msm->m_voice[i].m_out<<6); + } + }*/ + } +} + +void DivPlatformMSM6258::tick(bool sysTick) { + // nothing +} + +int DivPlatformMSM6258::dispatch(DivCommand c) { + switch (c.cmd) { + case DIV_CMD_NOTE_ON: { + DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_FM); + if (ins->type==DIV_INS_AMIGA) { + chan[c.chan].furnacePCM=true; + } else { + chan[c.chan].furnacePCM=false; + } + if (skipRegisterWrites) break; + if (chan[c.chan].furnacePCM) { + chan[c.chan].macroInit(ins); + if (!chan[c.chan].std.vol.will) { + chan[c.chan].outVol=chan[c.chan].vol; + } + chan[c.chan].sample=ins->amiga.getSample(c.value); + if (chan[c.chan].sample>=0 && chan[c.chan].samplesong.sampleLen) { + //DivSample* s=parent->getSample(chan[c.chan].sample); + if (c.value!=DIV_NOTE_NULL) { + chan[c.chan].note=c.value; + chan[c.chan].freqChanged=true; + } + chan[c.chan].active=true; + chan[c.chan].keyOn=true; + msm->ctrl_w(1); + msm->ctrl_w(2); + //rWrite((8<=parent->song.sampleLen) { + break; + } + //DivSample* s=parent->getSample(12*sampleBank+c.value%12); + chan[c.chan].sample=12*sampleBank+c.value%12; + //rWrite((8<ctrl_w(1); + msm->ctrl_w(2); + } + break; + } + case DIV_CMD_NOTE_OFF: + chan[c.chan].keyOff=true; + chan[c.chan].keyOn=false; + chan[c.chan].active=false; + //rWrite((8<(parent->song.sample.size()/12)) { + sampleBank=parent->song.sample.size()/12; + } + break; + case DIV_CMD_LEGATO: { + break; + } + case DIV_ALWAYS_SET_VOLUME: + return 0; + break; + case DIV_CMD_GET_VOLMAX: + return 8; + break; + case DIV_CMD_PRE_PORTA: + break; + case DIV_CMD_PRE_NOTE: + break; + default: + //printf("WARNING: unimplemented command %d\n",c.cmd); + break; + } + return 1; +} + +void DivPlatformMSM6258::muteChannel(int ch, bool mute) { + isMuted[ch]=mute; +} + +void DivPlatformMSM6258::forceIns() { + while (!writes.empty()) writes.pop(); + for (int i=0; i<1; i++) { + chan[i].insChanged=true; + } +} + +void* DivPlatformMSM6258::getChanState(int ch) { + return &chan[ch]; +} + +DivDispatchOscBuffer* DivPlatformMSM6258::getOscBuffer(int ch) { + return oscBuf[ch]; +} + +unsigned char* DivPlatformMSM6258::getRegisterPool() { + return NULL; +} + +int DivPlatformMSM6258::getRegisterPoolSize() { + return 0; +} + +void DivPlatformMSM6258::poke(unsigned int addr, unsigned short val) { + //immWrite(addr,val); +} + +void DivPlatformMSM6258::poke(std::vector& wlist) { + //for (DivRegWrite& i: wlist) immWrite(i.addr,i.val); +} + +void DivPlatformMSM6258::reset() { + while (!writes.empty()) writes.pop(); + msm->device_reset(); + if (dumpWrites) { + addWrite(0xffffffff,0); + } + for (int i=0; i<1; i++) { + chan[i]=DivPlatformMSM6258::Channel(); + chan[i].std.setEngine(parent); + } + for (int i=0; i<1; i++) { + chan[i].vol=8; + chan[i].outVol=8; + } + + sampleBank=0; + + delay=0; +} + +bool DivPlatformMSM6258::keyOffAffectsArp(int ch) { + return false; +} + +void DivPlatformMSM6258::notifyInsChange(int ins) { + for (int i=0; i<1; i++) { + if (chan[i].ins==ins) { + chan[i].insChanged=true; + } + } +} + +void DivPlatformMSM6258::notifyInsDeletion(void* ins) { +} + +const void* DivPlatformMSM6258::getSampleMem(int index) { + return index == 0 ? adpcmMem : NULL; +} + +size_t DivPlatformMSM6258::getSampleMemCapacity(int index) { + return index == 0 ? 262144 : 0; +} + +size_t DivPlatformMSM6258::getSampleMemUsage(int index) { + return index == 0 ? adpcmMemLen : 0; +} + +void DivPlatformMSM6258::renderSamples() { + memset(adpcmMem,0,getSampleMemCapacity(0)); + + // sample data + size_t memPos=0; + int sampleCount=parent->song.sampleLen; + if (sampleCount>128) sampleCount=128; + for (int i=0; isong.sample[i]; + int paddedLen=s->lengthVOX; + if (memPos>=getSampleMemCapacity(0)) { + logW("out of ADPCM memory for sample %d!",i); + break; + } + if (memPos+paddedLen>=getSampleMemCapacity(0)) { + memcpy(adpcmMem+memPos,s->dataVOX,getSampleMemCapacity(0)-memPos); + logW("out of ADPCM memory for sample %d!",i); + } else { + memcpy(adpcmMem+memPos,s->dataVOX,paddedLen); + } + s->offVOX=memPos; + memPos+=paddedLen; + } + adpcmMemLen=memPos+256; +} + +void DivPlatformMSM6258::setFlags(unsigned int flags) { + if (flags&1) { + chipClock=4096000; + } else { + chipClock=4000000; + } + rate=chipClock/256; + for (int i=0; i<1; i++) { + isMuted[i]=false; + oscBuf[i]->rate=rate/256; + } +} + +int DivPlatformMSM6258::init(DivEngine* p, int channels, int sugRate, unsigned int flags) { + parent=p; + adpcmMem=new unsigned char[getSampleMemCapacity(0)]; + adpcmMemLen=0; + dumpWrites=false; + skipRegisterWrites=false; + updateOsc=0; + for (int i=0; i<1; i++) { + isMuted[i]=false; + oscBuf[i]=new DivDispatchOscBuffer; + } + msm=new okim6258_device(4000000); + msm->device_start(); + setFlags(flags); + reset(); + return 4; +} + +void DivPlatformMSM6258::quit() { + for (int i=0; i<1; i++) { + delete oscBuf[i]; + } + delete msm; + delete[] adpcmMem; +} + +DivPlatformMSM6258::~DivPlatformMSM6258() { +} diff --git a/src/engine/platform/msm6258.h b/src/engine/platform/msm6258.h new file mode 100644 index 000000000..1c820c51d --- /dev/null +++ b/src/engine/platform/msm6258.h @@ -0,0 +1,129 @@ +/** + * 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 _MSM6258_H +#define _MSM6258_H +#include "../dispatch.h" +#include "../macroInt.h" +#include +#include "sound/oki/okim6258.h" + +class DivPlatformMSM6258: public DivDispatch { + protected: + const unsigned short chanOffs[6]={ + 0x00, 0x01, 0x02, 0x100, 0x101, 0x102 + }; + + struct Channel { + unsigned char freqH, freqL; + int freq, baseFreq, pitch, pitch2, portaPauseFreq, note, ins; + unsigned char psgMode, autoEnvNum, autoEnvDen; + signed char konCycles; + bool active, insChanged, freqChanged, keyOn, keyOff, portaPause, inPorta, furnacePCM, hardReset; + int vol, outVol; + int sample; + unsigned char pan; + DivMacroInt std; + void macroInit(DivInstrument* which) { + std.init(which); + pitch2=0; + } + Channel(): + freqH(0), + freqL(0), + freq(0), + baseFreq(0), + pitch(0), + pitch2(0), + portaPauseFreq(0), + note(0), + ins(-1), + psgMode(1), + autoEnvNum(0), + autoEnvDen(0), + active(false), + insChanged(true), + freqChanged(false), + keyOn(false), + keyOff(false), + portaPause(false), + inPorta(false), + furnacePCM(false), + hardReset(false), + vol(0), + outVol(15), + sample(-1), + pan(3) {} + }; + Channel chan[1]; + DivDispatchOscBuffer* oscBuf[1]; + bool isMuted[1]; + struct QueuedWrite { + unsigned short addr; + unsigned char val; + bool addrOrVal; + QueuedWrite(unsigned short a, unsigned char v): addr(a), val(v), addrOrVal(false) {} + }; + std::queue writes; + okim6258_device* msm; + unsigned char regPool[512]; + unsigned char lastBusy; + + unsigned char* adpcmMem; + size_t adpcmMemLen; + unsigned char sampleBank; + + int delay, updateOsc; + + bool extMode; + + short oldWrites[512]; + short pendingWrites[512]; + + friend void putDispatchChan(void*,int,int); + + public: + void acquire(short* bufL, short* bufR, size_t start, size_t len); + int dispatch(DivCommand c); + void* getChanState(int chan); + 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 notifyInsChange(int ins); + void notifyInsDeletion(void* ins); + void poke(unsigned int addr, unsigned short val); + void poke(std::vector& wlist); + void setFlags(unsigned int flags); + const char** getRegisterSheet(); + const char* getEffectName(unsigned char effect); + const void* getSampleMem(int index); + size_t getSampleMemCapacity(int index); + size_t getSampleMemUsage(int index); + void renderSamples(); + + int init(DivEngine* parent, int channels, int sugRate, unsigned int flags); + void quit(); + ~DivPlatformMSM6258(); +}; +#endif diff --git a/src/engine/platform/sound/oki/okim6258.cpp b/src/engine/platform/sound/oki/okim6258.cpp index 5ccfcb3f3..035eeb3c8 100644 --- a/src/engine/platform/sound/oki/okim6258.cpp +++ b/src/engine/platform/sound/oki/okim6258.cpp @@ -12,6 +12,7 @@ #include "okim6258.h" +#include #include #include @@ -113,6 +114,7 @@ void okim6258_device::device_start() m_signal = -2; m_step = 0; + m_has_data = false; } @@ -125,6 +127,7 @@ void okim6258_device::device_reset() m_signal = -2; m_step = 0; m_status = 0; + m_has_data = false; } @@ -150,6 +153,8 @@ void okim6258_device::sound_stream_update(short** outputs, int len) nibble_shift ^= 4; + if (nibble_shift==0) m_has_data=false; + buffer[sampindex]=sample; } @@ -241,10 +246,13 @@ uint8_t okim6258_device::status_r() okim6258_data_w -- write to the control port of an OKIM6258-compatible chip ***********************************************************************************************/ -void okim6258_device::data_w(uint8_t data) +bool okim6258_device::data_w(uint8_t data) { + if (m_has_data) return false; m_data_in = data; m_nibble_shift = 0; + m_has_data = true; + return true; } @@ -259,6 +267,8 @@ void okim6258_device::ctrl_w(uint8_t data) if (data & COMMAND_STOP) { m_status &= ~(STATUS_PLAYING | STATUS_RECORDING); + m_has_data = false; + printf("stopping\n"); return; } @@ -272,6 +282,7 @@ void okim6258_device::ctrl_w(uint8_t data) m_signal = -2; m_step = 0; m_nibble_shift = 0; + printf("playing\n"); } } else diff --git a/src/engine/platform/sound/oki/okim6258.h b/src/engine/platform/sound/oki/okim6258.h index 689bac5a0..88a429d31 100644 --- a/src/engine/platform/sound/oki/okim6258.h +++ b/src/engine/platform/sound/oki/okim6258.h @@ -31,19 +31,19 @@ public: void set_outbits(int outbit) { m_output_bits = outbit; } uint8_t status_r(); - void data_w(uint8_t data); + bool data_w(uint8_t data); void ctrl_w(uint8_t data); void set_divider(int val); int get_vclk(); // device-levels - virtual void device_start(); - virtual void device_reset(); - virtual void device_clock_changed(); + void device_start(); + void device_reset(); + void device_clock_changed(); // sound stream updates - virtual void sound_stream_update(short** outputs, int len); + void sound_stream_update(short** outputs, int len); private: int16_t clock_adpcm(uint8_t nibble); @@ -54,6 +54,7 @@ private: uint32_t m_divider; /* master clock divider */ uint8_t m_adpcm_type; /* 3/4 bit ADPCM select */ uint8_t m_data_in; /* ADPCM data-in register */ + bool m_has_data; /* whether we already have data */ uint8_t m_nibble_shift; /* nibble select */ uint8_t m_output_bits; /* D/A precision is 10-bits but 12-bit data can be