From 307665f531371cd24fb418f315c55178ee20871a Mon Sep 17 00:00:00 2001 From: tildearrow Date: Fri, 14 Jan 2022 16:29:27 -0500 Subject: [PATCH] prepare for SAA1099 --- CMakeLists.txt | 2 + src/engine/dispatchContainer.cpp | 4 + src/engine/platform/saa.cpp | 336 ++++++++++++++++++++++ src/engine/platform/saa.h | 67 +++++ src/engine/platform/sound/saa1099.cpp | 390 ++++++++++++++++++++++++++ src/engine/platform/sound/saa1099.h | 77 +++++ 6 files changed, 876 insertions(+) create mode 100644 src/engine/platform/saa.cpp create mode 100644 src/engine/platform/saa.h create mode 100644 src/engine/platform/sound/saa1099.cpp create mode 100644 src/engine/platform/sound/saa1099.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e2e268244..8cbbfe513 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,7 @@ extern/Nuked-OPN2/ym3438.c extern/opm/opm.c src/engine/platform/sound/sn76496.cpp src/engine/platform/sound/ay8910.cpp +src/engine/platform/sound/saa1099.cpp src/engine/platform/sound/gb/apu.c src/engine/platform/sound/gb/timing.c src/engine/platform/sound/pce_psg.cpp @@ -122,6 +123,7 @@ src/engine/platform/ym2610ext.cpp src/engine/platform/ay.cpp src/engine/platform/ay8930.cpp src/engine/platform/tia.cpp +src/engine/platform/saa.cpp src/engine/platform/dummy.cpp) set(GUI_SOURCES diff --git a/src/engine/dispatchContainer.cpp b/src/engine/dispatchContainer.cpp index 6480f0216..4836428aa 100644 --- a/src/engine/dispatchContainer.cpp +++ b/src/engine/dispatchContainer.cpp @@ -12,6 +12,7 @@ #include "platform/ay.h" #include "platform/ay8930.h" #include "platform/tia.h" +#include "platform/saa.h" #include "platform/dummy.h" #include "../ta-log.h" @@ -124,6 +125,9 @@ void DivDispatchContainer::init(DivSystem sys, DivEngine* eng, int chanCount, do case DIV_SYSTEM_TIA: dispatch=new DivPlatformTIA; break; + case DIV_SYSTEM_SAA1099: + dispatch=new DivPlatformSAA1099; + break; default: logW("this system is not supported yet! using dummy platform.\n"); dispatch=new DivPlatformDummy; diff --git a/src/engine/platform/saa.cpp b/src/engine/platform/saa.cpp new file mode 100644 index 000000000..0a30e2415 --- /dev/null +++ b/src/engine/platform/saa.cpp @@ -0,0 +1,336 @@ +#include "saa.h" +#include "../engine.h" +#include "sound/saa1099.h" +#include +#include + +#define rWrite(a,v) if (!skipRegisterWrites) {pendingWrites[a]=v;} +#define immWrite(a,v) if (!skipRegisterWrites) {writes.emplace(a,v);} + +#define PSG_FREQ_BASE 6848.0f + +void DivPlatformSAA1099::acquire(short* bufL, short* bufR, size_t start, size_t len) { + if (ayBufLencalcFreq(chan[i].baseFreq,chan[i].pitch,true); + if (chan[i].freq>4095) chan[i].freq=4095; + if (chan[i].keyOn) { + //rWrite(16+i*5+1,((chan[i].duty&3)<<6)|(63-(ins->gb.soundLen&63))); + //rWrite(16+i*5+2,((chan[i].vol<<4))|(ins->gb.envLen&7)|((ins->gb.envDir&1)<<3)); + } + if (chan[i].keyOff) { + rWrite(0x08+i,0); + } + rWrite((i)<<1,chan[i].freq&0xff); + rWrite(1+((i)<<1),chan[i].freq>>8); + if (chan[i].keyOn) chan[i].keyOn=false; + if (chan[i].keyOff) chan[i].keyOff=false; + chan[i].freqChanged=false; + } + } + + rWrite(0x07, + ~((chan[0].psgMode&1)| + ((chan[1].psgMode&1)<<1)| + ((chan[2].psgMode&1)<<2)| + ((chan[0].psgMode&2)<<2)| + ((chan[1].psgMode&2)<<3)| + ((chan[2].psgMode&2)<<4))); + + if (ayEnvSlide!=0) { + ayEnvSlideLow+=ayEnvSlide; + while (ayEnvSlideLow>7) { + ayEnvSlideLow-=8; + if (ayEnvPeriod<0xffff) { + ayEnvPeriod++; + immWrite(0x0b,ayEnvPeriod); + immWrite(0x0c,ayEnvPeriod>>8); + } + } + while (ayEnvSlideLow<-7) { + ayEnvSlideLow+=8; + if (ayEnvPeriod>0) { + ayEnvPeriod--; + immWrite(0x0b,ayEnvPeriod); + immWrite(0x0c,ayEnvPeriod>>8); + } + } + } + + for (int i=0; i<16; i++) { + if (pendingWrites[i]!=oldWrites[i]) { + immWrite(i,pendingWrites[i]&0xff); + oldWrites[i]=pendingWrites[i]; + } + } +} + +int DivPlatformSAA1099::dispatch(DivCommand c) { + switch (c.cmd) { + case DIV_CMD_NOTE_ON: { + DivInstrument* ins=parent->getIns(chan[c.chan].ins); + chan[c.chan].baseFreq=round(PSG_FREQ_BASE/pow(2.0f,((float)c.value/12.0f))); + chan[c.chan].freqChanged=true; + chan[c.chan].note=c.value; + chan[c.chan].active=true; + chan[c.chan].keyOn=true; + chan[c.chan].std.init(ins); + if (isMuted[c.chan]) { + rWrite(0x08+c.chan,0); + } else { + rWrite(0x08+c.chan,(chan[c.chan].vol&15)|((chan[c.chan].psgMode&4)<<2)); + } + break; + } + case DIV_CMD_NOTE_OFF: + chan[c.chan].keyOff=true; + chan[c.chan].active=false; + chan[c.chan].std.init(NULL); + break; + case DIV_CMD_VOLUME: { + chan[c.chan].vol=c.value; + if (!chan[c.chan].std.hasVol) { + chan[c.chan].outVol=c.value; + } + if (isMuted[c.chan]) { + rWrite(0x08+c.chan,0); + } else { + if (chan[c.chan].active) rWrite(0x08+c.chan,(chan[c.chan].vol&15)|((chan[c.chan].psgMode&4)<<2)); + } + break; + break; + } + case DIV_CMD_GET_VOLUME: { + return chan[c.chan].vol; + break; + } + case DIV_CMD_INSTRUMENT: + if (chan[c.chan].ins!=c.value) { + chan[c.chan].insChanged=true; + } + chan[c.chan].ins=c.value; + break; + case DIV_CMD_PITCH: { + chan[c.chan].pitch=c.value; + chan[c.chan].freqChanged=true; + break; + } + case DIV_CMD_NOTE_PORTA: { + int destFreq=round(PSG_FREQ_BASE/pow(2.0f,((float)c.value2/12.0f))); + 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=round(PSG_FREQ_BASE/pow(2.0f,((float)c.value/12.0f))); + chan[c.chan].freqChanged=true; + break; + } + case DIV_CMD_STD_NOISE_FREQ: + rWrite(0x06,31-c.value); + break; + case DIV_CMD_AY_ENVELOPE_SET: + ayEnvMode=c.value>>4; + rWrite(0x0d,ayEnvMode); + if (c.value&15) { + chan[c.chan].psgMode|=4; + } else { + chan[c.chan].psgMode&=~4; + } + if (isMuted[c.chan]) { + rWrite(0x08+c.chan,0); + } else { + rWrite(0x08+c.chan,(chan[c.chan].vol&15)|((chan[c.chan].psgMode&4)<<2)); + } + break; + case DIV_CMD_AY_ENVELOPE_LOW: + ayEnvPeriod&=0xff00; + ayEnvPeriod|=c.value; + immWrite(0x0b,ayEnvPeriod); + immWrite(0x0c,ayEnvPeriod>>8); + break; + case DIV_CMD_AY_ENVELOPE_HIGH: + ayEnvPeriod&=0xff; + ayEnvPeriod|=c.value<<8; + immWrite(0x0b,ayEnvPeriod); + immWrite(0x0c,ayEnvPeriod>>8); + break; + case DIV_CMD_AY_ENVELOPE_SLIDE: + ayEnvSlide=c.value; + break; + case DIV_ALWAYS_SET_VOLUME: + return 0; + break; + case DIV_CMD_GET_VOLMAX: + return 15; + break; + case DIV_CMD_PRE_PORTA: + chan[c.chan].std.init(parent->getIns(chan[c.chan].ins)); + chan[c.chan].inPorta=c.value; + break; + case DIV_CMD_PRE_NOTE: + break; + default: + //printf("WARNING: unimplemented command %d\n",c.cmd); + break; + } + return 1; +} + +void DivPlatformSAA1099::muteChannel(int ch, bool mute) { + isMuted[ch]=mute; + if (isMuted[ch]) { + rWrite(0x08+ch,0); + } else { + rWrite(0x08+ch,(chan[ch].outVol&15)|((chan[ch].psgMode&4)<<2)); + } +} + +void DivPlatformSAA1099::forceIns() { + for (int i=0; i<3; i++) { + chan[i].insChanged=true; + } + immWrite(0x0b,ayEnvPeriod); + immWrite(0x0c,ayEnvPeriod>>8); + immWrite(0x0d,ayEnvMode); +} + +void DivPlatformSAA1099::reset() { + while (!writes.empty()) writes.pop(); + saa=saa1099_device(); + for (int i=0; i<3; i++) { + chan[i]=DivPlatformSAA1099::Channel(); + chan[i].vol=0x0f; + } + + for (int i=0; i<16; i++) { + oldWrites[i]=-1; + pendingWrites[i]=-1; + } + + lastBusy=60; + dacMode=0; + dacPeriod=0; + dacPos=0; + dacRate=0; + dacSample=-1; + sampleBank=0; + ayEnvPeriod=0; + ayEnvMode=0; + ayEnvSlide=0; + ayEnvSlideLow=0; + + delay=0; + + extMode=false; +} + +bool DivPlatformSAA1099::isStereo() { + return false; +} + +bool DivPlatformSAA1099::keyOffAffectsArp(int ch) { + return true; +} + +void DivPlatformSAA1099::notifyInsDeletion(void* ins) { + for (int i=0; i<3; i++) { + chan[i].std.notifyInsDeletion((DivInstrument*)ins); + } +} + +void DivPlatformSAA1099::setPAL(bool pal) { + if (pal) { + rate=250000; + } else { + rate=250000; + } +} + +int DivPlatformSAA1099::init(DivEngine* p, int channels, int sugRate, bool pal) { + parent=p; + skipRegisterWrites=false; + for (int i=0; i<3; i++) { + isMuted[i]=false; + } + setPAL(pal); + ayBufLen=65536; + for (int i=0; i<3; i++) ayBuf[i]=new short[ayBufLen]; + reset(); + return 3; +} + +void DivPlatformSAA1099::quit() { + for (int i=0; i<3; i++) delete[] ayBuf[i]; +} \ No newline at end of file diff --git a/src/engine/platform/saa.h b/src/engine/platform/saa.h new file mode 100644 index 000000000..4a685acf8 --- /dev/null +++ b/src/engine/platform/saa.h @@ -0,0 +1,67 @@ +#ifndef _SAA_H +#define _SAA_H +#include "../dispatch.h" +#include "../macroInt.h" +#include +#include "sound/saa1099.h" + +class DivPlatformSAA1099: public DivDispatch { + protected: + struct Channel { + unsigned char freqH, freqL; + int freq, baseFreq, pitch; + unsigned char ins, note, psgMode; + signed char konCycles; + bool active, insChanged, freqChanged, keyOn, keyOff, portaPause, inPorta; + int vol, outVol; + unsigned char pan; + DivMacroInt std; + Channel(): freqH(0), freqL(0), freq(0), baseFreq(0), pitch(0), ins(-1), note(0), psgMode(1), active(false), insChanged(true), freqChanged(false), keyOn(false), keyOff(false), portaPause(false), inPorta(false), vol(0), outVol(15), pan(3) {} + }; + Channel chan[3]; + bool isMuted[3]; + 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; + saa1099_device saa; + unsigned char lastBusy; + + bool dacMode; + int dacPeriod; + int dacRate; + int dacPos; + int dacSample; + unsigned char sampleBank; + + int delay; + + bool extMode; + + short oldWrites[16]; + short pendingWrites[16]; + unsigned char ayEnvMode; + unsigned short ayEnvPeriod; + short ayEnvSlideLow; + short ayEnvSlide; + short* ayBuf[3]; + size_t ayBufLen; + + public: + void acquire(short* bufL, short* bufR, size_t start, size_t len); + int dispatch(DivCommand c); + void reset(); + void forceIns(); + void tick(); + void muteChannel(int ch, bool mute); + void setPAL(bool pal); + bool isStereo(); + bool keyOffAffectsArp(int ch); + void notifyInsDeletion(void* ins); + int init(DivEngine* parent, int channels, int sugRate, bool pal); + void quit(); +}; +#endif diff --git a/src/engine/platform/sound/saa1099.cpp b/src/engine/platform/sound/saa1099.cpp new file mode 100644 index 000000000..01c96dd8e --- /dev/null +++ b/src/engine/platform/sound/saa1099.cpp @@ -0,0 +1,390 @@ +// license:BSD-3-Clause +// copyright-holders:Juergen Buchmueller, Manuel Abadia +/*************************************************************************** + + Philips SAA1099 Sound driver + + By Juergen Buchmueller and Manuel Abadia + + SAA1099 register layout: + ======================== + + offs | 7654 3210 | description + -----+-----------+--------------------------- + 0x00 | ---- xxxx | Amplitude channel 0 (left) + 0x00 | xxxx ---- | Amplitude channel 0 (right) + 0x01 | ---- xxxx | Amplitude channel 1 (left) + 0x01 | xxxx ---- | Amplitude channel 1 (right) + 0x02 | ---- xxxx | Amplitude channel 2 (left) + 0x02 | xxxx ---- | Amplitude channel 2 (right) + 0x03 | ---- xxxx | Amplitude channel 3 (left) + 0x03 | xxxx ---- | Amplitude channel 3 (right) + 0x04 | ---- xxxx | Amplitude channel 4 (left) + 0x04 | xxxx ---- | Amplitude channel 4 (right) + 0x05 | ---- xxxx | Amplitude channel 5 (left) + 0x05 | xxxx ---- | Amplitude channel 5 (right) + | | + 0x08 | xxxx xxxx | Frequency channel 0 + 0x09 | xxxx xxxx | Frequency channel 1 + 0x0a | xxxx xxxx | Frequency channel 2 + 0x0b | xxxx xxxx | Frequency channel 3 + 0x0c | xxxx xxxx | Frequency channel 4 + 0x0d | xxxx xxxx | Frequency channel 5 + | | + 0x10 | ---- -xxx | Channel 0 octave select + 0x10 | -xxx ---- | Channel 1 octave select + 0x11 | ---- -xxx | Channel 2 octave select + 0x11 | -xxx ---- | Channel 3 octave select + 0x12 | ---- -xxx | Channel 4 octave select + 0x12 | -xxx ---- | Channel 5 octave select + | | + 0x14 | ---- ---x | Channel 0 frequency enable (0 = off, 1 = on) + 0x14 | ---- --x- | Channel 1 frequency enable (0 = off, 1 = on) + 0x14 | ---- -x-- | Channel 2 frequency enable (0 = off, 1 = on) + 0x14 | ---- x--- | Channel 3 frequency enable (0 = off, 1 = on) + 0x14 | ---x ---- | Channel 4 frequency enable (0 = off, 1 = on) + 0x14 | --x- ---- | Channel 5 frequency enable (0 = off, 1 = on) + | | + 0x15 | ---- ---x | Channel 0 noise enable (0 = off, 1 = on) + 0x15 | ---- --x- | Channel 1 noise enable (0 = off, 1 = on) + 0x15 | ---- -x-- | Channel 2 noise enable (0 = off, 1 = on) + 0x15 | ---- x--- | Channel 3 noise enable (0 = off, 1 = on) + 0x15 | ---x ---- | Channel 4 noise enable (0 = off, 1 = on) + 0x15 | --x- ---- | Channel 5 noise enable (0 = off, 1 = on) + | | + 0x16 | ---- --xx | Noise generator parameters 0 + 0x16 | --xx ---- | Noise generator parameters 1 + | | + 0x18 | --xx xxxx | Envelope generator 0 parameters + 0x18 | x--- ---- | Envelope generator 0 control enable (0 = off, 1 = on) + 0x19 | --xx xxxx | Envelope generator 1 parameters + 0x19 | x--- ---- | Envelope generator 1 control enable (0 = off, 1 = on) + | | + 0x1c | ---- ---x | All channels enable (0 = off, 1 = on) + 0x1c | ---- --x- | Synch & Reset generators + + Unspecified bits should be written as zero. + +***************************************************************************/ + +#include "saa1099.h" +#include + +#define BIT(x,n) (((x)>>(n))&1) + +static constexpr int clock_divider = 256; + +static constexpr int LEFT = 0x00; +static constexpr int RIGHT = 0x01; + +static constexpr unsigned short amplitude_lookup[16] = { + 0*32767/16, 1*32767/16, 2*32767/16, 3*32767/16, + 4*32767/16, 5*32767/16, 6*32767/16, 7*32767/16, + 8*32767/16, 9*32767/16, 10*32767/16, 11*32767/16, + 12*32767/16, 13*32767/16, 14*32767/16, 15*32767/16 +}; + +static constexpr unsigned char envelope[8][64] = { + /* zero amplitude */ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + /* maximum amplitude */ + {15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, + 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, + 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, + 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, }, + /* single decay */ + {15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + /* repetitive decay */ + {15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, + 15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, + 15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, + 15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, + /* single triangular */ + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, + 15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + /* repetitive triangular */ + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, + 15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, + 15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, + /* single attack */ + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + /* repetitive attack */ + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 } +}; + + +//************************************************************************** +// LIVE DEVICE +//************************************************************************** + +//------------------------------------------------- +// saa1099_device - constructor +//------------------------------------------------- + +saa1099_device::saa1099_device() + : m_noise_params{ 0, 0 } + , m_env_enable{ false, false } + , m_env_reverse_right{ false, false } + , m_env_mode{ 0, 0 } + , m_env_bits{ false, false } + , m_env_clock{ false, false } + , m_env_step{ 0, 0 } + , m_all_ch_enable(false) + , m_sync_state(false) + , m_selected_reg(0) +{ +} + + +//------------------------------------------------- +// device_start - device-specific startup +//------------------------------------------------- + +void saa1099_device::device_start() +{ +} + + +//------------------------------------------------- +// sound_stream_update - handle a stream update +//------------------------------------------------- + +void saa1099_device::sound_stream_update(short** outputs, int len) +{ + int j, ch; + /* if the channels are disabled we're done */ + if (!m_all_ch_enable) + { + /* init output data */ + memset(outputs[LEFT],0,len*sizeof(short)); + memset(outputs[RIGHT],0,len*sizeof(short)); + return; + } + + for (ch = 0; ch < 2; ch++) + { + switch (m_noise_params[ch]) + { + case 0: + case 1: + case 2: m_noise[ch].freq = 256 << m_noise_params[ch]; break; + case 3: m_noise[ch].freq = m_channels[ch * 3].freq(); break; // todo: this case will be clock()/[ch*3's octave divisor, 0 is = 256*2, higher numbers are higher] * 2 if the tone generator phase reset bit (0x1c bit 1) is set. + } + } + + /* fill all data needed */ + for( j = 0; j < len; j++ ) + { + int output_l = 0, output_r = 0; + + /* for each channel */ + for (ch = 0; ch < 6; ch++) + { + /* check the actual position in the square wave */ + while (m_channels[ch].counter <= 0) + { + /* calculate new frequency now after the half wave is updated */ + m_channels[ch].counter += m_channels[ch].freq(); + m_channels[ch].level ^= 1; + + /* eventually clock the envelope counters */ + if (ch == 1 && m_env_clock[0] == 0) + envelope_w(0); + if (ch == 4 && m_env_clock[1] == 0) + envelope_w(1); + } + m_channels[ch].counter -= clock_divider; + + // if the noise is enabled + unsigned char level = 0; + if (m_channels[ch].noise_enable) + { + // if the noise level is high (noise 0: chan 0-2, noise 1: chan 3-5) + level ^= m_noise[ch/3].level & 1; + } + // if the square wave is enabled + if (m_channels[ch].freq_enable) + { + // if the channel level is high + level ^= m_channels[ch].level & 1; + } + if (level) + { + output_l += m_channels[ch].amplitude[ LEFT] * m_channels[ch].envelope[ LEFT] / 16; + output_r += m_channels[ch].amplitude[RIGHT] * m_channels[ch].envelope[RIGHT] / 16; + } + } + + for (ch = 0; ch < 2; ch++) + { + /* update the state of the noise generator + * polynomial is x^18 + x^11 + x (i.e. 0x20400) and is a plain XOR, initial state is probably all 1s + * see http://www.vogons.org/viewtopic.php?f=9&t=51695 */ + while (m_noise[ch].counter <= 0) + { + m_noise[ch].counter += m_noise[ch].freq; // clock / ((511 - frequency) * 2^(8 - octave)) or clock / 2^(8 + noise period) + if( ((m_noise[ch].level & 0x20000) == 0) != ((m_noise[ch].level & 0x0400) == 0) ) + m_noise[ch].level = (m_noise[ch].level << 1) | 1; + else + m_noise[ch].level <<= 1; + } + m_noise[ch].counter -= clock_divider; + } + /* write sound data to the buffer */ + outputs[LEFT][j]=output_l; + outputs[RIGHT][j]=output_r; + } +} + + +void saa1099_device::envelope_w(int ch) +{ + if (m_env_enable[ch]) + { + int step, mode, mask; + mode = m_env_mode[ch]; + /* step from 0..63 and then loop in steps 32..63 */ + step = m_env_step[ch] = + ((m_env_step[ch] + 1) & 0x3f) | (m_env_step[ch] & 0x20); + + mask = 15; + if (m_env_bits[ch]) + mask &= ~1; /* 3 bit resolution, mask LSB */ + + m_channels[ch*3+0].envelope[ LEFT] = + m_channels[ch*3+1].envelope[ LEFT] = + m_channels[ch*3+2].envelope[ LEFT] = envelope[mode][step] & mask; + if (m_env_reverse_right[ch]) + { + m_channels[ch*3+0].envelope[RIGHT] = + m_channels[ch*3+1].envelope[RIGHT] = + m_channels[ch*3+2].envelope[RIGHT] = (15 - envelope[mode][step]) & mask; + } + else + { + m_channels[ch*3+0].envelope[RIGHT] = + m_channels[ch*3+1].envelope[RIGHT] = + m_channels[ch*3+2].envelope[RIGHT] = envelope[mode][step] & mask; + } + } + else + { + /* envelope mode off, set all envelope factors to 16 */ + m_channels[ch*3+0].envelope[ LEFT] = + m_channels[ch*3+1].envelope[ LEFT] = + m_channels[ch*3+2].envelope[ LEFT] = + m_channels[ch*3+0].envelope[RIGHT] = + m_channels[ch*3+1].envelope[RIGHT] = + m_channels[ch*3+2].envelope[RIGHT] = 16; + } +} + + +void saa1099_device::control_w(unsigned char data) +{ + if ((data & 0xff) > 0x1c) + { + /* Error! */ + } + + m_selected_reg = data & 0x1f; + if (m_selected_reg == 0x18 || m_selected_reg == 0x19) + { + /* clock the envelope channels */ + if (m_env_clock[0]) + envelope_w(0); + if (m_env_clock[1]) + envelope_w(1); + } +} + + +void saa1099_device::data_w(unsigned char data) +{ + int reg = m_selected_reg; + int ch; + + switch (reg) + { + /* channel i amplitude */ + case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: + ch = reg & 7; + m_channels[ch].amplitude[LEFT] = amplitude_lookup[data & 0x0f]; + m_channels[ch].amplitude[RIGHT] = amplitude_lookup[(data >> 4) & 0x0f]; + break; + /* channel i frequency */ + case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: + ch = reg & 7; + m_channels[ch].frequency = data & 0xff; + break; + /* channel i octave */ + case 0x10: case 0x11: case 0x12: + ch = (reg - 0x10) << 1; + m_channels[ch + 0].octave = data & 0x07; + m_channels[ch + 1].octave = (data >> 4) & 0x07; + break; + /* channel i frequency enable */ + case 0x14: + for (ch = 0; ch < 6; ch++) + m_channels[ch].freq_enable = BIT(data, ch); + break; + /* channel i noise enable */ + case 0x15: + for (ch = 0; ch < 6; ch++) + m_channels[ch].noise_enable = BIT(data, ch); + break; + /* noise generators parameters */ + case 0x16: + m_noise_params[0] = data & 0x03; + m_noise_params[1] = (data >> 4) & 0x03; + break; + /* envelope generators parameters */ + case 0x18: case 0x19: + ch = reg - 0x18; + m_env_reverse_right[ch] = BIT(data, 0); + m_env_mode[ch] = (data >> 1) & 0x07; + m_env_bits[ch] = BIT(data, 4); + m_env_clock[ch] = BIT(data, 5); + m_env_enable[ch] = BIT(data, 7); + /* reset the envelope */ + m_env_step[ch] = 0; + break; + /* channels enable & reset generators */ + case 0x1c: + m_all_ch_enable = BIT(data, 0); + m_sync_state = BIT(data, 1); + if (BIT(data, 1)) + { + /* Synch & Reset generators */ + for (int i = 0; i < 6; i++) + { + m_channels[i].level = 0; + m_channels[i].counter = m_channels[i].freq(); + } + } + break; + } +} + +void saa1099_device::write(int offset, unsigned char data) +{ + if (offset & 1) + control_w(data); + else + data_w(data); +} diff --git a/src/engine/platform/sound/saa1099.h b/src/engine/platform/sound/saa1099.h new file mode 100644 index 000000000..8ebad8a30 --- /dev/null +++ b/src/engine/platform/sound/saa1099.h @@ -0,0 +1,77 @@ +// license:BSD-3-Clause +// copyright-holders:Juergen Buchmueller, Manuel Abadia +/********************************************** + Philips SAA1099 Sound driver +**********************************************/ + +#ifndef MAME_SOUND_SAA1099_H +#define MAME_SOUND_SAA1099_H + +//************************************************************************** +// TYPE DEFINITIONS +//************************************************************************** + +// ======================> saa1099_device + +class saa1099_device +{ +public: + saa1099_device(); + + void control_w(unsigned char data); + void data_w(unsigned char data); + + void write(int offset, unsigned char data); + + // device-level overrides + void device_start(); + void device_clock_changed(); + + // sound stream update overrides + void sound_stream_update(short** outputs, int len); + +private: + struct saa1099_channel + { + saa1099_channel() : amplitude{ 0, 0 }, envelope{ 0, 0 } { } + + unsigned char frequency = 0; // frequency (0x00..0xff) + bool freq_enable = false; // frequency enable + bool noise_enable = false; // noise enable + unsigned char octave = 0; // octave (0x00..0x07) + unsigned short amplitude[2]; // amplitude + unsigned char envelope[2]; // envelope (0x00..0x0f or 0x10 == off) + + /* vars to simulate the square wave */ + inline unsigned int freq() const { return (511 - frequency) << (8 - octave); } // clock / ((511 - frequency) * 2^(8 - octave)) + int counter = 0; + unsigned char level = 0; + }; + + struct saa1099_noise + { + saa1099_noise() { } + + /* vars to simulate the noise generator output */ + int counter = 0; + int freq = 0; + unsigned int level = 0xffffffffU; // noise polynomial shifter + }; + + void envelope_w(int ch); + + unsigned char m_noise_params[2]; // noise generators parameters + bool m_env_enable[2]; // envelope generators enable + bool m_env_reverse_right[2]; // envelope reversed for right channel + unsigned char m_env_mode[2]; // envelope generators mode + bool m_env_bits[2]; // true = 3 bits resolution + bool m_env_clock[2]; // envelope clock mode (true external) + unsigned char m_env_step[2]; // current envelope step + bool m_all_ch_enable; // all channels enable + bool m_sync_state; // sync all channels + unsigned char m_selected_reg; // selected register + saa1099_channel m_channels[6]; // channels + saa1099_noise m_noise[2]; // noise generators +}; + +#endif // MAME_SOUND_SAA1099_H