diff --git a/CMakeLists.txt b/CMakeLists.txt index ca10cf91..105b576d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -433,6 +433,8 @@ src/engine/platform/sound/vic20sound.c src/engine/platform/sound/ymz280b.cpp +src/engine/platform/sound/vsu.cpp + src/engine/platform/sound/rf5c68.cpp src/engine/platform/sound/oki/msm5232.cpp @@ -504,6 +506,7 @@ src/engine/platform/x1_010.cpp src/engine/platform/lynx.cpp src/engine/platform/su.cpp src/engine/platform/swan.cpp +src/engine/platform/vb.cpp src/engine/platform/vera.cpp src/engine/platform/zxbeeper.cpp src/engine/platform/bubsyswsg.cpp diff --git a/src/engine/dispatchContainer.cpp b/src/engine/dispatchContainer.cpp index 28b4114a..db985a9a 100644 --- a/src/engine/dispatchContainer.cpp +++ b/src/engine/dispatchContainer.cpp @@ -67,6 +67,7 @@ #include "platform/ymz280b.h" #include "platform/rf5c68.h" #include "platform/snes.h" +#include "platform/vb.h" #include "platform/pcmdac.h" #include "platform/dummy.h" #include "../ta-log.h" @@ -342,6 +343,9 @@ void DivDispatchContainer::init(DivSystem sys, DivEngine* eng, int chanCount, do case DIV_SYSTEM_SWAN: dispatch=new DivPlatformSwan; break; + case DIV_SYSTEM_VBOY: + dispatch=new DivPlatformVB; + break; case DIV_SYSTEM_VERA: dispatch=new DivPlatformVERA; break; diff --git a/src/engine/platform/sound/t6w28/T6W28_Apu.cpp b/src/engine/platform/sound/t6w28/T6W28_Apu.cpp new file mode 100644 index 00000000..4414f619 --- /dev/null +++ b/src/engine/platform/sound/t6w28/T6W28_Apu.cpp @@ -0,0 +1,431 @@ +// T6W28_Snd_Emu + +#include "T6W28_Apu.h" + +#undef require +#define require( expr ) assert( expr ) + +/* Copyright (C) 2003-2006 Shay Green. This module is free software; you +can redistribute it and/or modify it under the terms of the GNU Lesser +General Public License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. This +module 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 Lesser General Public License for +more details. You should have received a copy of the GNU Lesser General +Public License along with this module; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +// T6W28_Osc + +namespace MDFN_IEN_NGP +{ + +T6W28_Osc::T6W28_Osc() +{ + outputs [0] = NULL; // always stays NULL + outputs [1] = NULL; + outputs [2] = NULL; + outputs [3] = NULL; +} + +void T6W28_Osc::reset() +{ + delay = 0; + last_amp_left = 0; + last_amp_right = 0; + + volume_left = 0; + volume_right = 0; +} + +// T6W28_Square + +blip_inline void T6W28_Square::reset() +{ + period = 0; + phase = 0; + T6W28_Osc::reset(); +} + +void T6W28_Square::run( sms_time_t time, sms_time_t end_time ) +{ + if ((!volume_left && !volume_right) || period <= 128 ) + { + // ignore 16kHz and higher + if ( last_amp_left ) + { + synth->offset( time, -last_amp_left, outputs[2] ); + last_amp_left = 0; + } + + if ( last_amp_right ) + { + synth->offset( time, -last_amp_right, outputs[1] ); + last_amp_right = 0; + } + + time += delay; + if ( !period ) + { + time = end_time; + } + else if ( time < end_time ) + { + // keep calculating phase + int count = (end_time - time + period - 1) / period; + phase = (phase + count) & 1; + time += count * period; + } + } + else + { + int amp_left = phase ? volume_left : -volume_left; + int amp_right = phase ? volume_right : -volume_right; + + { + int delta_left = amp_left - last_amp_left; + int delta_right = amp_right - last_amp_right; + + if ( delta_left ) + { + last_amp_left = amp_left; + synth->offset( time, delta_left, outputs[2] ); + } + + if ( delta_right ) + { + last_amp_right = amp_right; + synth->offset( time, delta_right, outputs[1] ); + } + } + + time += delay; + if ( time < end_time ) + { + Blip_Buffer* const output_left = this->outputs[2]; + Blip_Buffer* const output_right = this->outputs[1]; + + int delta_left = amp_left * 2; + int delta_right = amp_right * 2; + do + { + delta_left = -delta_left; + delta_right = -delta_right; + + synth->offset_inline( time, delta_left, output_left ); + synth->offset_inline( time, delta_right, output_right ); + time += period; + phase ^= 1; + } + while ( time < end_time ); + + this->last_amp_left = phase ? volume_left : -volume_left; + this->last_amp_right = phase ? volume_right : -volume_right; + } + } + delay = time - end_time; +} + +// T6W28_Noise + +static const int noise_periods [3] = { 0x100, 0x200, 0x400 }; + +blip_inline void T6W28_Noise::reset() +{ + period = &noise_periods [0]; + shifter = 0x4000; + tap = 13; + period_extra = 0; + T6W28_Osc::reset(); +} + +void T6W28_Noise::run( sms_time_t time, sms_time_t end_time ) +{ + int amp_left = volume_left; + int amp_right = volume_right; + + if ( shifter & 1 ) + { + amp_left = -amp_left; + amp_right = -amp_right; + } + + { + int delta_left = amp_left - last_amp_left; + int delta_right = amp_right - last_amp_right; + + if ( delta_left ) + { + last_amp_left = amp_left; + synth.offset( time, delta_left, outputs[2] ); + } + + if ( delta_right ) + { + last_amp_right = amp_right; + synth.offset( time, delta_right, outputs[1] ); + } + } + + time += delay; + + if ( !volume_left && !volume_right ) + time = end_time; + + if ( time < end_time ) + { + Blip_Buffer* const output_left = this->outputs[2]; + Blip_Buffer* const output_right = this->outputs[1]; + + unsigned l_shifter = this->shifter; + int delta_left = amp_left * 2; + int delta_right = amp_right * 2; + + int l_period = *this->period * 2; + if ( !l_period ) + l_period = 16; + + do + { + int changed = (l_shifter + 1) & 2; // set if prev and next bits differ + l_shifter = (((l_shifter << 14) ^ (l_shifter << tap)) & 0x4000) | (l_shifter >> 1); + if ( changed ) + { + delta_left = -delta_left; + synth.offset_inline( time, delta_left, output_left ); + + delta_right = -delta_right; + synth.offset_inline( time, delta_right, output_right ); + } + time += l_period; + } + while ( time < end_time ); + + this->shifter = l_shifter; + this->last_amp_left = delta_left >> 1; + this->last_amp_right = delta_right >> 1; + } + delay = time - end_time; +} + +// T6W28_Apu + +T6W28_Apu::T6W28_Apu() +{ + for ( int i = 0; i < 3; i++ ) + { + squares [i].synth = &square_synth; + oscs [i] = &squares [i]; + } + oscs [3] = &noise; + + volume( 1.0 ); + reset(); +} + +T6W28_Apu::~T6W28_Apu() +{ +} + +void T6W28_Apu::volume( double vol ) +{ + vol *= 0.85 / (osc_count * 64 * 2); + square_synth.volume( vol ); + noise.synth.volume( vol ); +} + +void T6W28_Apu::treble_eq( const blip_eq_t& eq ) +{ + square_synth.treble_eq( eq ); + noise.synth.treble_eq( eq ); +} + +void T6W28_Apu::osc_output( int index, Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ) +{ + require( (unsigned) index < osc_count ); + require( (center && left && right) || (!center && !left && !right) ); + T6W28_Osc& osc = *oscs [index]; + osc.outputs [1] = right; + osc.outputs [2] = left; + osc.outputs [3] = center; +} + +void T6W28_Apu::output( Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ) +{ + for ( int i = 0; i < osc_count; i++ ) + osc_output( i, center, left, right ); +} + +void T6W28_Apu::reset() +{ + last_time = 0; + latch_left = 0; + latch_right = 0; + + squares [0].reset(); + squares [1].reset(); + squares [2].reset(); + noise.reset(); +} + +void T6W28_Apu::run_until( sms_time_t end_time ) +{ + require( end_time >= last_time ); // end_time must not be before previous time + + if ( end_time > last_time ) + { + // run oscillators + for ( int i = 0; i < osc_count; ++i ) + { + T6W28_Osc& osc = *oscs [i]; + if ( osc.outputs[1] ) + { + if ( i < 3 ) + squares [i].run( last_time, end_time ); + else + noise.run( last_time, end_time ); + } + } + + last_time = end_time; + } +} + +bool T6W28_Apu::end_frame( sms_time_t end_time ) +{ + if ( end_time > last_time ) + run_until( end_time ); + + assert( last_time >= end_time ); + last_time -= end_time; + + return(1); +} + +static const unsigned char volumes [16] = { + // volumes [i] = 64 * pow( 1.26, 15 - i ) / pow( 1.26, 15 ) + 64, 50, 39, 31, 24, 19, 15, 12, 9, 7, 5, 4, 3, 2, 1, 0 +}; + +void T6W28_Apu::write_data_left( sms_time_t time, int data ) +{ + require( (unsigned) data <= 0xFF ); + + run_until( time ); + + if ( data & 0x80 ) + latch_left = data; + + int index = (latch_left >> 5) & 3; + + if ( latch_left & 0x10 ) + { + oscs [index]->volume_left = volumes [data & 15]; + } + else if ( index < 3 ) + { + T6W28_Square& sq = squares [index]; + if ( data & 0x80 ) + sq.period = (sq.period & 0xFF00) | (data << 4 & 0x00FF); + else + sq.period = (sq.period & 0x00FF) | (data << 8 & 0x3F00); + } +} + +void T6W28_Apu::write_data_right( sms_time_t time, int data ) +{ + require( (unsigned) data <= 0xFF ); + + run_until( time ); + + if ( data & 0x80 ) + latch_right = data; + + int index = (latch_right >> 5) & 3; + //printf("%d\n", index); + + if ( latch_right & 0x10 ) + { + oscs [index]->volume_right = volumes [data & 15]; + } + else if ( index == 2 ) + { + if ( data & 0x80 ) + noise.period_extra = (noise.period_extra & 0xFF00) | (data << 4 & 0x00FF); + else + noise.period_extra = (noise.period_extra & 0x00FF) | (data << 8 & 0x3F00); + } + else if(index == 3) + { + int select = data & 3; + if ( select < 3 ) + noise.period = &noise_periods [select]; + else + noise.period = &noise.period_extra; + + int const tap_disabled = 16; + noise.tap = (data & 0x04) ? 13 : tap_disabled; + noise.shifter = 0x4000; + } +} + + +void T6W28_Apu::save_state(T6W28_ApuState *ret) +{ + for(int x = 0; x < 4; x++) + { + ret->delay[x] = oscs[x]->delay; + ret->volume_left[x] = oscs[x]->volume_left; + ret->volume_right[x] = oscs[x]->volume_right; + } + for(int x = 0; x < 3; x++) + { + ret->sq_period[x] = squares[x].period; + ret->sq_phase[x] = squares[x].phase; + } + ret->noise_shifter = noise.shifter; + ret->noise_tap = noise.tap; + ret->noise_period_extra = noise.period_extra; + + if(noise.period == &noise_periods[0]) + ret->noise_period = 0; + else if(noise.period == &noise_periods[1]) + ret->noise_period = 1; + else if(noise.period == &noise_periods[2]) + ret->noise_period = 2; + else ret->noise_period = 3; + + ret->latch_left = latch_left; + ret->latch_right = latch_right; +} + +void T6W28_Apu::load_state(const T6W28_ApuState *state) +{ + for(int x = 0; x < 4; x++) + { + oscs[x]->delay = state->delay[x] & ((x == 3) ? 0x7FFF : 0x3FFF); + oscs[x]->volume_left = state->volume_left[x]; + oscs[x]->volume_right = state->volume_right[x]; + } + for(int x = 0; x < 3; x++) + { + squares[x].period = state->sq_period[x] & 0x3FFF; + squares[x].phase = state->sq_phase[x]; + } + noise.shifter = state->noise_shifter; + noise.tap = state->noise_tap; + noise.period_extra = state->noise_period_extra & 0x3FFF; + + unsigned select = state->noise_period; + + if ( select < 3 ) + noise.period = &noise_periods [select]; + else + noise.period = &noise.period_extra; + + latch_left = state->latch_left; + latch_right = state->latch_right; +} + +} diff --git a/src/engine/platform/sound/t6w28/T6W28_Apu.h b/src/engine/platform/sound/t6w28/T6W28_Apu.h new file mode 100644 index 00000000..bd173948 --- /dev/null +++ b/src/engine/platform/sound/t6w28/T6W28_Apu.h @@ -0,0 +1,94 @@ +// T6W28_Snd_Emu + +#ifndef SMS_APU_H +#define SMS_APU_H + +namespace MDFN_IEN_NGP +{ + +typedef long sms_time_t; // clock cycle count + +} + +#include "T6W28_Oscs.h" + +namespace MDFN_IEN_NGP +{ + +typedef struct +{ + int sq_period[3]; + int sq_phase[3]; + unsigned int noise_period; + unsigned int noise_period_extra; + unsigned int noise_shifter; + unsigned int noise_tap; + + int delay[4]; + int volume_left[4]; + int volume_right[4]; + unsigned char latch_left, latch_right; +} T6W28_ApuState; + +class T6W28_Apu { +public: + // Set overall volume of all oscillators, where 1.0 is full volume + void volume( double ); + + // Set treble equalization + void treble_eq( const blip_eq_t& ); + + // Outputs can be assigned to a single buffer for mono output, or to three + // buffers for stereo output (using Stereo_Buffer to do the mixing). + + // Assign all oscillator outputs to specified buffer(s). If buffer + // is NULL, silences all oscillators. + void output( Blip_Buffer* mono ); + void output( Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ); + + // Assign single oscillator output to buffer(s). Valid indicies are 0 to 3, + // which refer to Square 1, Square 2, Square 3, and Noise. If buffer is NULL, + // silences oscillator. + enum { osc_count = 4 }; + void osc_output( int index, Blip_Buffer* mono ); + void osc_output( int index, Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ); + + // Reset oscillators and internal state + void reset(); + + // Write to data port + void write_data_left( sms_time_t, int ); + void write_data_right( sms_time_t, int ); + + // Run all oscillators up to specified time, end current frame, then + // start a new frame at time 0. Returns true if any oscillators added + // sound to one of the left/right buffers, false if they only added + // to the center buffer. + bool end_frame( sms_time_t ); + +public: + T6W28_Apu(); + ~T6W28_Apu(); +private: + // noncopyable + T6W28_Apu( const T6W28_Apu& ); + T6W28_Apu& operator = ( const T6W28_Apu& ); + + T6W28_Osc* oscs [osc_count]; + T6W28_Square squares [3]; + //T6W28_Square::Synth square_synth; // used by squares + sms_time_t last_time; + int latch_left, latch_right; + T6W28_Noise noise; + + void run_until( sms_time_t ); +}; + +inline void T6W28_Apu::output( Blip_Buffer* b ) { output( b, b, b ); } + +inline void T6W28_Apu::osc_output( int i, Blip_Buffer* b ) { osc_output( i, b, b, b ); } + +} + +#endif + diff --git a/src/engine/platform/sound/t6w28/T6W28_Oscs.h b/src/engine/platform/sound/t6w28/T6W28_Oscs.h new file mode 100644 index 00000000..876b3ed1 --- /dev/null +++ b/src/engine/platform/sound/t6w28/T6W28_Oscs.h @@ -0,0 +1,50 @@ + +// Private oscillators used by T6W28_Apu + +// T6W28_Snd_Emu + +#ifndef SMS_OSCS_H +#define SMS_OSCS_H + +namespace MDFN_IEN_NGP +{ + +struct T6W28_Osc +{ + int output_select; + + int delay; + int last_amp_left; + int last_amp_right; + + int volume_left; + int volume_right; + + T6W28_Osc(); + void reset(); +}; + +struct T6W28_Square : T6W28_Osc +{ + int period; + int phase; + + void reset(); + void run( sms_time_t, sms_time_t ); +}; + +struct T6W28_Noise : T6W28_Osc +{ + const int* period; + int period_extra; + unsigned shifter; + unsigned tap; + + void reset(); + void run( sms_time_t, sms_time_t ); +}; + +} + +#endif + diff --git a/src/engine/platform/sound/vsu.cpp b/src/engine/platform/sound/vsu.cpp new file mode 100644 index 00000000..b2b6518e --- /dev/null +++ b/src/engine/platform/sound/vsu.cpp @@ -0,0 +1,499 @@ +/******************************************************************************/ +/* Mednafen Virtual Boy Emulation Module */ +/******************************************************************************/ +/* vsu.cpp: +** Copyright (C) 2010-2016 Mednafen Team +** +** 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 "vsu.h" +#include +#include +#include + +static const unsigned int Tap_LUT[8] = { 15 - 1, 11 - 1, 14 - 1, 5 - 1, 9 - 1, 7 - 1, 10 - 1, 12 - 1 }; + +#define MDFN_UNLIKELY(x) x + +VSU::VSU() +{ + for(int ch = 0; ch < 6; ch++) + { + for(int lr = 0; lr < 2; lr++) + last_output[ch][lr] = 0; + } +} + +VSU::~VSU() +{ + +} + +void VSU::SetSoundRate(double rate) +{ + /* + for(int y = 0; y < 2; y++) + { + sbuf[y].set_sample_rate(rate ? rate : 44100, 50); + sbuf[y].clock_rate((long)(VB_MASTER_CLOCK / 4)); + sbuf[y].bass_freq(20); + } + */ +} + +void VSU::Power(void) +{ + SweepControl = 0; + SweepModCounter = 0; + SweepModClockDivider = 1; + + for(int ch = 0; ch < 6; ch++) + { + IntlControl[ch] = 0; + LeftLevel[ch] = 0; + RightLevel[ch] = 0; + Frequency[ch] = 0; + EnvControl[ch] = 0; + RAMAddress[ch] = 0; + + EffFreq[ch] = 0; + Envelope[ch] = 0; + WavePos[ch] = 0; + FreqCounter[ch] = 1; + IntervalCounter[ch] = 0; + EnvelopeCounter[ch] = 1; + + EffectsClockDivider[ch] = 4800; + IntervalClockDivider[ch] = 4; + EnvelopeClockDivider[ch] = 4; + + LatcherClockDivider[ch] = 120; + } + + ModWavePos = 0; + + NoiseLatcherClockDivider = 120; + NoiseLatcher = 0; + + lfsr = 0; + + memset(WaveData, 0, sizeof(WaveData)); + memset(ModData, 0, sizeof(ModData)); + + last_ts = 0; +} + +void VSU::Write(int timestamp, unsigned int A, unsigned char V) +{ + if(MDFN_UNLIKELY(A & 0x3)) + { + return; + } + // + // + A &= 0x7FF; + + //Update(timestamp); + + printf("VSU Write: %d, %08x %02x\n", timestamp, A, V); + + if(A < 0x280) + WaveData[A >> 7][(A >> 2) & 0x1F] = V & 0x3F; + else if(A < 0x400) + { + if(A >= 0x300) + printf("Modulation mirror write? %08x %02x\n", A, V); + ModData[(A >> 2) & 0x1F] = V; + } + else if(A < 0x600) + { + int ch = (A >> 6) & 0xF; + + //if(ch < 6) + printf("Ch: %d, Reg: %d, Value: %02x\n", ch, (A >> 2) & 0xF, V); + + if(ch > 5) + { + if(A == 0x580 && (V & 1)) + { + //puts("STOP, HAMMER TIME"); + for(int i = 0; i < 6; i++) + IntlControl[i] &= ~0x80; + } + } + else + switch((A >> 2) & 0xF) + { + case 0x0: IntlControl[ch] = V & ~0x40; + + if(V & 0x80) + { + EffFreq[ch] = Frequency[ch]; + if(ch == 5) + FreqCounter[ch] = 10 * (2048 - EffFreq[ch]); + else + FreqCounter[ch] = 2048 - EffFreq[ch]; + IntervalCounter[ch] = (V & 0x1F) + 1; + EnvelopeCounter[ch] = (EnvControl[ch] & 0x7) + 1; + + if(ch == 4) + { + SweepModCounter = (SweepControl >> 4) & 7; + SweepModClockDivider = (SweepControl & 0x80) ? 8 : 1; + ModWavePos = 0; + } + + WavePos[ch] = 0; + + if(ch == 5) // Not sure if this is correct. + lfsr = 1; + + //if(!(IntlControl[ch] & 0x80)) + // Envelope[ch] = (EnvControl[ch] >> 4) & 0xF; + + EffectsClockDivider[ch] = 4800; + IntervalClockDivider[ch] = 4; + EnvelopeClockDivider[ch] = 4; + } + break; + + case 0x1: LeftLevel[ch] = (V >> 4) & 0xF; + RightLevel[ch] = (V >> 0) & 0xF; + break; + + case 0x2: Frequency[ch] &= 0xFF00; + Frequency[ch] |= V << 0; + EffFreq[ch] &= 0xFF00; + EffFreq[ch] |= V << 0; + break; + + case 0x3: Frequency[ch] &= 0x00FF; + Frequency[ch] |= (V & 0x7) << 8; + EffFreq[ch] &= 0x00FF; + EffFreq[ch] |= (V & 0x7) << 8; + break; + + case 0x4: EnvControl[ch] &= 0xFF00; + EnvControl[ch] |= V << 0; + + Envelope[ch] = (V >> 4) & 0xF; + break; + + case 0x5: EnvControl[ch] &= 0x00FF; + if(ch == 4) + EnvControl[ch] |= (V & 0x73) << 8; + else if(ch == 5) + { + EnvControl[ch] |= (V & 0x73) << 8; + lfsr = 1; + } + else + EnvControl[ch] |= (V & 0x03) << 8; + break; + + case 0x6: RAMAddress[ch] = V & 0xF; + break; + + case 0x7: if(ch == 4) + { + SweepControl = V; + } + break; + } + } +} + +inline void VSU::CalcCurrentOutput(int ch, int &left, int &right) +{ + if(!(IntlControl[ch] & 0x80)) + { + left = right = 0; + return; + } + + int WD; + int l_ol, r_ol; + + if(ch == 5) + WD = NoiseLatcher; //(NoiseLatcher << 6) - NoiseLatcher; + else + { + if(RAMAddress[ch] > 4) + WD = 0; + else + WD = WaveData[RAMAddress[ch]][WavePos[ch]]; // - 0x20; + } + l_ol = Envelope[ch] * LeftLevel[ch]; + if(l_ol) + { + l_ol >>= 3; + l_ol += 1; + } + + r_ol = Envelope[ch] * RightLevel[ch]; + if(r_ol) + { + r_ol >>= 3; + r_ol += 1; + } + + left = WD * l_ol; + right = WD * r_ol; +} + +void VSU::Update(int timestamp) +{ + //puts("VSU Start"); + int left, right; + + for(int ch = 0; ch < 6; ch++) + { + int clocks = timestamp - last_ts; + //int running_timestamp = last_ts; + + // Output sound here + CalcCurrentOutput(ch, left, right); + /*Synth.offset_inline(running_timestamp, left - last_output[ch][0], &sbuf[0]); + Synth.offset_inline(running_timestamp, right - last_output[ch][1], &sbuf[1]);*/ + last_output[ch][0] = left; + last_output[ch][1] = right; + + if(!(IntlControl[ch] & 0x80)) + continue; + + while(clocks > 0) + { + int chunk_clocks = clocks; + + if(chunk_clocks > EffectsClockDivider[ch]) + chunk_clocks = EffectsClockDivider[ch]; + + if(ch == 5) + { + if(chunk_clocks > NoiseLatcherClockDivider) + chunk_clocks = NoiseLatcherClockDivider; + } + else + { + if(EffFreq[ch] >= 2040) + { + if(chunk_clocks > LatcherClockDivider[ch]) + chunk_clocks = LatcherClockDivider[ch]; + } + else + { + if(chunk_clocks > FreqCounter[ch]) + chunk_clocks = FreqCounter[ch]; + } + } + + if(ch == 5 && chunk_clocks > NoiseLatcherClockDivider) + chunk_clocks = NoiseLatcherClockDivider; + + FreqCounter[ch] -= chunk_clocks; + while(FreqCounter[ch] <= 0) + { + if(ch == 5) + { + int feedback = ((lfsr >> 7) & 1) ^ ((lfsr >> Tap_LUT[(EnvControl[5] >> 12) & 0x7]) & 1) ^ 1; + lfsr = ((lfsr << 1) & 0x7FFF) | feedback; + + FreqCounter[ch] += 10 * (2048 - EffFreq[ch]); + } + else + { + FreqCounter[ch] += 2048 - EffFreq[ch]; + WavePos[ch] = (WavePos[ch] + 1) & 0x1F; + } + } + + LatcherClockDivider[ch] -= chunk_clocks; + while(LatcherClockDivider[ch] <= 0) + LatcherClockDivider[ch] += 120; + + if(ch == 5) + { + NoiseLatcherClockDivider -= chunk_clocks; + if(!NoiseLatcherClockDivider) + { + NoiseLatcherClockDivider = 120; + NoiseLatcher = ((lfsr & 1) << 6) - (lfsr & 1); + } + } + + EffectsClockDivider[ch] -= chunk_clocks; + while(EffectsClockDivider[ch] <= 0) + { + EffectsClockDivider[ch] += 4800; + + IntervalClockDivider[ch]--; + while(IntervalClockDivider[ch] <= 0) + { + IntervalClockDivider[ch] += 4; + + if(IntlControl[ch] & 0x20) + { + IntervalCounter[ch]--; + if(!IntervalCounter[ch]) + { + IntlControl[ch] &= ~0x80; + } + } + + EnvelopeClockDivider[ch]--; + while(EnvelopeClockDivider[ch] <= 0) + { + EnvelopeClockDivider[ch] += 4; + + if(EnvControl[ch] & 0x0100) // Enveloping enabled? + { + EnvelopeCounter[ch]--; + if(!EnvelopeCounter[ch]) + { + EnvelopeCounter[ch] = (EnvControl[ch] & 0x7) + 1; + + if(EnvControl[ch] & 0x0008) // Grow + { + if(Envelope[ch] < 0xF || (EnvControl[ch] & 0x200)) + Envelope[ch] = (Envelope[ch] + 1) & 0xF; + } + else // Decay + { + if(Envelope[ch] > 0 || (EnvControl[ch] & 0x200)) + Envelope[ch] = (Envelope[ch] - 1) & 0xF; + } + } + } + + } // end while(EnvelopeClockDivider[ch] <= 0) + } // end while(IntervalClockDivider[ch] <= 0) + + if(ch == 4) + { + SweepModClockDivider--; + while(SweepModClockDivider <= 0) + { + SweepModClockDivider += (SweepControl & 0x80) ? 8 : 1; + + if(((SweepControl >> 4) & 0x7) && (EnvControl[ch] & 0x4000)) + { + if(SweepModCounter) + SweepModCounter--; + + if(!SweepModCounter) + { + SweepModCounter = (SweepControl >> 4) & 0x7; + + if(EnvControl[ch] & 0x1000) // Modulation + { + if(ModWavePos < 32 || (EnvControl[ch] & 0x2000)) + { + ModWavePos &= 0x1F; + + EffFreq[ch] = (Frequency[ch] + (signed char)ModData[ModWavePos]) & 0x7FF; + ModWavePos++; + } + } + else // Sweep + { + int delta = EffFreq[ch] >> (SweepControl & 0x7); + int NewFreq = EffFreq[ch] + ((SweepControl & 0x8) ? delta : -delta); + + //printf("Sweep(%d): Old: %d, New: %d\n", ch, EffFreq[ch], NewFreq); + + if(NewFreq < 0) + EffFreq[ch] = 0; + else if(NewFreq > 0x7FF) + { + //EffFreq[ch] = 0x7FF; + IntlControl[ch] &= ~0x80; + } + else + EffFreq[ch] = NewFreq; + } + } + } + } // end while(SweepModClockDivider <= 0) + } // end if(ch == 4) + } // end while(EffectsClockDivider[ch] <= 0) + clocks -= chunk_clocks; + //running_timestamp += chunk_clocks; + + // Output sound here too. + CalcCurrentOutput(ch, left, right); + /* + Synth.offset_inline(running_timestamp, left - last_output[ch][0], &sbuf[0]); + Synth.offset_inline(running_timestamp, right - last_output[ch][1], &sbuf[1]); + */ + last_output[ch][0] = left; + last_output[ch][1] = right; + } + } + last_ts = timestamp; + //puts("VSU End"); +} + +int VSU::EndFrame(int timestamp) +{ + int ret = 0; + + Update(timestamp); + last_ts = 0; + + /* + if(SoundBuf) + { + for(int y = 0; y < 2; y++) + { + sbuf[y].end_frame(timestamp); + ret = sbuf[y].read_samples(SoundBuf + y, SoundBufMaxSize, 1); + } + } + */ + + return ret; +} + +unsigned char VSU::PeekWave(const unsigned int which, unsigned int Address) +{ + assert(which <= 4); + + Address &= 0x1F; + + return(WaveData[which][Address]); +} + +void VSU::PokeWave(const unsigned int which, unsigned int Address, unsigned char value) +{ + assert(which <= 4); + + Address &= 0x1F; + + WaveData[which][Address] = value & 0x3F; +} + +unsigned char VSU::PeekModWave(unsigned int Address) +{ + Address &= 0x1F; + return(ModData[Address]); +} + +void VSU::PokeModWave(unsigned int Address, unsigned char value) +{ + Address &= 0x1F; + + ModData[Address] = value & 0xFF; +} diff --git a/src/engine/platform/sound/vsu.h b/src/engine/platform/sound/vsu.h new file mode 100644 index 00000000..ab43cb65 --- /dev/null +++ b/src/engine/platform/sound/vsu.h @@ -0,0 +1,97 @@ +/******************************************************************************/ +/* Mednafen Virtual Boy Emulation Module */ +/******************************************************************************/ +/* vsu.h: +** Copyright (C) 2010-2016 Mednafen Team +** +** 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 __VB_VSU_H +#define __VB_VSU_H + +class VSU +{ + public: + + int last_output[6][2]; + + VSU(); + ~VSU(); + + void SetSoundRate(double rate); + + void Power(void); + + void Write(int timestamp, unsigned int A, unsigned char V); + + int EndFrame(int timestamp); + + unsigned char PeekWave(const unsigned int which, unsigned int Address); + void PokeWave(const unsigned int which, unsigned int Address, unsigned char value); + + unsigned char PeekModWave(unsigned int Address); + void PokeModWave(unsigned int Address, unsigned char value); + + private: + + void CalcCurrentOutput(int ch, int &left, int &right); + + void Update(int timestamp); + + unsigned char IntlControl[6]; + unsigned char LeftLevel[6]; + unsigned char RightLevel[6]; + unsigned short Frequency[6]; + unsigned short EnvControl[6]; // Channel 5/6 extra functionality tacked on too. + + unsigned char RAMAddress[6]; + + unsigned char SweepControl; + + unsigned char WaveData[5][0x20]; + + unsigned char ModData[0x20]; + + // + // + // + int EffFreq[6]; + int Envelope[6]; + + int WavePos[6]; + int ModWavePos; + + int LatcherClockDivider[6]; + + int FreqCounter[6]; + int IntervalCounter[6]; + int EnvelopeCounter[6]; + int SweepModCounter; + + int EffectsClockDivider[6]; + int IntervalClockDivider[6]; + int EnvelopeClockDivider[6]; + int SweepModClockDivider; + + int NoiseLatcherClockDivider; + unsigned int NoiseLatcher; + + unsigned int lfsr; + + int last_ts; +}; + +#endif diff --git a/src/engine/platform/vb.cpp b/src/engine/platform/vb.cpp new file mode 100644 index 00000000..dcdeffff --- /dev/null +++ b/src/engine/platform/vb.cpp @@ -0,0 +1,604 @@ +/** + * 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 "vb.h" +#include "../engine.h" +#include + +//#define rWrite(a,v) pendingWrites[a]=v; +#define rWrite(a,v) if (!skipRegisterWrites) {writes.emplace(a,v); if (dumpWrites) {addWrite(a,v);} } +#define chWrite(c,a,v) rWrite(0x400+((c)<<6)+((a)<<2),v); + +#define CHIP_DIVIDER 64 + +const char* regCheatSheetVB[]={ + "Select", "0", + "MasterVol", "1", + "FreqL", "2", + "FreqH", "3", + "DataCtl", "4", + "ChanVol", "5", + "WaveCtl", "6", + "NoiseCtl", "7", + "LFOFreq", "8", + "LFOCtl", "9", + NULL +}; + +const char** DivPlatformVB::getRegisterSheet() { + return regCheatSheetVB; +} + +void DivPlatformVB::acquire(short* bufL, short* bufR, size_t start, size_t len) { + for (size_t h=start; hrate) { + DivSample* s=parent->getSample(chan[i].dacSample); + if (s->samples<=0) { + chan[i].dacSample=-1; + continue; + } + chWrite(i,0x07,0); + signed char dacData=((signed char)((unsigned char)s->data8[chan[i].dacPos]^0x80))>>3; + chan[i].dacOut=CLAMP(dacData,-16,15); + if (!isMuted[i]) { + chWrite(i,0x04,parent->song.disableSampleMacro?0xdf:(0xc0|chan[i].outVol)); + chWrite(i,0x06,chan[i].dacOut&0x1f); + } else { + chWrite(i,0x04,0xc0); + chWrite(i,0x06,0x10); + } + chan[i].dacPos++; + if (s->isLoopable() && chan[i].dacPos>=(unsigned int)s->loopEnd) { + chan[i].dacPos=s->loopStart; + } else if (chan[i].dacPos>=s->samples) { + chan[i].dacSample=-1; + } + chan[i].dacPeriod-=rate; + } + } + } + */ + + // VB part + cycles=0; + while (!writes.empty()) { + QueuedWrite w=writes.front(); + vb->Write(cycles,w.addr,w.val); + regPool[w.addr]=w.val; + //cycles+=2; + writes.pop(); + } + vb->EndFrame(4); + + tempL=0; + tempR=0; + for (int i=0; i<6; i++) { + oscBuf[i]->data[oscBuf[i]->needle++]=(vb->last_output[i][0]+vb->last_output[i][1])<<3; + tempL+=vb->last_output[i][0]<<3; + tempR+=vb->last_output[i][1]<<3; + } + + //tempL/=6; + //tempR/=6; + + if (tempL<-32768) tempL=-32768; + if (tempL>32767) tempL=32767; + if (tempR<-32768) tempR=-32768; + if (tempR>32767) tempR=32767; + + //printf("tempL: %d tempR: %d\n",tempL,tempR); + bufL[h]=tempL; + bufR[h]=tempR; + } +} + +void DivPlatformVB::updateWave(int ch) { + if (ch>=5) return; + + if (chan[ch].pcm) { + chan[ch].deferredWaveUpdate=true; + return; + } + for (int i=0; i<32; i++) { + rWrite((ch<<7)+(i<<2),chan[ch].ws.output[i]); + //chWrite(ch,0x06,chan[ch].ws.output[(i+chan[ch].antiClickWavePos)&31]); + } + chan[ch].antiClickWavePos&=31; + if (chan[ch].active) { + //chWrite(ch,0x04,0x80|chan[ch].outVol); + } + if (chan[ch].deferredWaveUpdate) { + chan[ch].deferredWaveUpdate=false; + } +} + +// TODO: in octave 6 the noise table changes to a tonal one +static unsigned char noiseFreq[12]={ + 4,13,15,18,21,23,25,27,29,31,0,2 +}; + +void DivPlatformVB::tick(bool sysTick) { + for (int i=0; i<6; i++) { + // anti-click + if (antiClickEnabled && sysTick && chan[i].freq>0) { + chan[i].antiClickPeriodCount+=(chipClock/MAX(parent->getCurHz(),1.0f)); + chan[i].antiClickWavePos+=chan[i].antiClickPeriodCount/chan[i].freq; + chan[i].antiClickPeriodCount%=chan[i].freq; + } + + chan[i].std.next(); + if (chan[i].std.vol.had) { + chan[i].outVol=VOL_SCALE_LOG(chan[i].vol&31,MIN(31,chan[i].std.vol.val),31); + if (chan[i].furnaceDac && chan[i].pcm) { + // ignore for now + } else { + //chWrite(i,0x04,0x80|chan[i].outVol); + } + } + if (chan[i].std.duty.had && i>=4) { + chan[i].noise=chan[i].std.duty.val; + chan[i].freqChanged=true; + int noiseSeek=chan[i].note; + if (noiseSeek<0) noiseSeek=0; + chWrite(i,0x07,chan[i].noise?(0x80|(parent->song.properNoiseLayout?(noiseSeek&31):noiseFreq[noiseSeek%12])):0); + } + if (chan[i].std.arp.had) { + if (!chan[i].inPorta) { + int noiseSeek=parent->calcArp(chan[i].note,chan[i].std.arp.val); + chan[i].baseFreq=NOTE_PERIODIC(noiseSeek); + if (noiseSeek<0) noiseSeek=0; + chWrite(i,0x07,chan[i].noise?(0x80|(parent->song.properNoiseLayout?(noiseSeek&31):noiseFreq[noiseSeek%12])):0); + } + chan[i].freqChanged=true; + } + if (chan[i].std.wave.had && !chan[i].pcm) { + if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) { + chan[i].wave=chan[i].std.wave.val; + chan[i].ws.changeWave1(chan[i].wave); + if (!chan[i].keyOff) chan[i].keyOn=true; + } + } + if (chan[i].std.panL.had) { + chan[i].pan&=0x0f; + chan[i].pan|=(chan[i].std.panL.val&15)<<4; + } + if (chan[i].std.panR.had) { + chan[i].pan&=0xf0; + chan[i].pan|=chan[i].std.panR.val&15; + } + if (chan[i].std.panL.had || chan[i].std.panR.had) { + //chWrite(i,0x05,isMuted[i]?0:chan[i].pan); + } + 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.phaseReset.had && chan[i].std.phaseReset.val==1) { + if (chan[i].furnaceDac && chan[i].pcm) { + if (chan[i].active && chan[i].dacSample>=0 && chan[i].dacSamplesong.sampleLen) { + chan[i].dacPos=0; + chan[i].dacPeriod=0; + //chWrite(i,0x04,parent->song.disableSampleMacro?0xdf:(0xc0|chan[i].vol)); + addWrite(0xffff0000+(i<<8),chan[i].dacSample); + chan[i].keyOn=true; + } + } + chan[i].antiClickWavePos=0; + chan[i].antiClickPeriodCount=0; + } + if (chan[i].active) { + if (chan[i].ws.tick() || (chan[i].std.phaseReset.had && chan[i].std.phaseReset.val==1) || chan[i].deferredWaveUpdate) { + updateWave(i); + } + } + if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { + //DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_PCE); + chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,true,0,chan[i].pitch2,chipClock,CHIP_DIVIDER); + if (chan[i].furnaceDac && chan[i].pcm) { + double off=1.0; + if (chan[i].dacSample>=0 && chan[i].dacSamplesong.sampleLen) { + DivSample* s=parent->getSample(chan[i].dacSample); + if (s->centerRate<1) { + off=1.0; + } else { + off=8363.0/(double)s->centerRate; + } + } + chan[i].dacRate=((double)chipClock/2)/MAX(1,off*chan[i].freq); + if (dumpWrites) addWrite(0xffff0001+(i<<8),chan[i].dacRate); + } + if (chan[i].freq>2047) chan[i].freq=2047; + chan[i].freq=2047-chan[i].freq; + chWrite(i,0x02,chan[i].freq&0xff); + chWrite(i,0x03,chan[i].freq>>8); + if (chan[i].keyOn) { + //rWrite(16+i*5,0x80); + //chWrite(i,0x04,0x80|chan[i].vol); + chWrite(i,0x01,0xff); + //chWrite(i,0x00,0xff); + chWrite(i,0x04,0xf0); + chWrite(i,0x05,0x00); + chWrite(i,0x00,0x80); + } + if (chan[i].keyOff) { + chWrite(i,0x01,0); + } + if (chan[i].keyOn) chan[i].keyOn=false; + if (chan[i].keyOff) chan[i].keyOff=false; + chan[i].freqChanged=false; + } + } +} + +int DivPlatformVB::dispatch(DivCommand c) { + switch (c.cmd) { + case DIV_CMD_NOTE_ON: { + DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_PCE); + chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:31; + if (ins->type==DIV_INS_AMIGA || ins->amiga.useSample) { + chan[c.chan].pcm=true; + } else if (chan[c.chan].furnaceDac) { + chan[c.chan].pcm=false; + } + if (chan[c.chan].pcm) { + if (ins->type==DIV_INS_AMIGA || ins->amiga.useSample) { + chan[c.chan].furnaceDac=true; + if (skipRegisterWrites) break; + chan[c.chan].dacSample=ins->amiga.getSample(c.value); + if (chan[c.chan].dacSample<0 || chan[c.chan].dacSample>=parent->song.sampleLen) { + chan[c.chan].dacSample=-1; + if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0); + break; + } else { + if (dumpWrites) { + //chWrite(c.chan,0x04,parent->song.disableSampleMacro?0xdf:(0xc0|chan[c.chan].vol)); + addWrite(0xffff0000+(c.chan<<8),chan[c.chan].dacSample); + } + } + chan[c.chan].dacPos=0; + chan[c.chan].dacPeriod=0; + 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; + } + chan[c.chan].active=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].keyOn=true; + } else { + chan[c.chan].furnaceDac=false; + if (skipRegisterWrites) break; + if (c.value!=DIV_NOTE_NULL) { + chan[c.chan].note=c.value; + } + chan[c.chan].dacSample=12*sampleBank+chan[c.chan].note%12; + if (chan[c.chan].dacSample>=parent->song.sampleLen) { + chan[c.chan].dacSample=-1; + if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0); + break; + } else { + if (dumpWrites) addWrite(0xffff0000+(c.chan<<8),chan[c.chan].dacSample); + } + chan[c.chan].dacPos=0; + chan[c.chan].dacPeriod=0; + chan[c.chan].dacRate=parent->getSample(chan[c.chan].dacSample)->rate; + if (dumpWrites) { + //chWrite(c.chan,0x04,parent->song.disableSampleMacro?0xdf:(0xc0|chan[c.chan].vol)); + addWrite(0xffff0001+(c.chan<<8),chan[c.chan].dacRate); + } + } + break; + } + 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; + int noiseSeek=chan[c.chan].note; + if (noiseSeek<0) noiseSeek=0; + chWrite(c.chan,0x07,chan[c.chan].noise?(0x80|(parent->song.properNoiseLayout?(noiseSeek&31):noiseFreq[noiseSeek%12])):0); + } + chan[c.chan].active=true; + chan[c.chan].keyOn=true; + //chWrite(c.chan,0x04,0x80|chan[c.chan].vol); + chan[c.chan].macroInit(ins); + if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { + chan[c.chan].outVol=chan[c.chan].vol; + } + if (chan[c.chan].wave<0) { + chan[c.chan].wave=0; + chan[c.chan].ws.changeWave1(chan[c.chan].wave); + } + chan[c.chan].ws.init(ins,32,63,chan[c.chan].insChanged); + chan[c.chan].insChanged=false; + break; + } + case DIV_CMD_NOTE_OFF: + chan[c.chan].dacSample=-1; + if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0); + chan[c.chan].pcm=false; + 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; + 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; + if (chan[c.chan].active && !chan[c.chan].pcm) { + //chWrite(c.chan,0x04,0x80|chan[c.chan].outVol); + } + } + } + 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_WAVE: + chan[c.chan].wave=c.value; + chan[c.chan].ws.changeWave1(chan[c.chan].wave); + chan[c.chan].keyOn=true; + break; + case DIV_CMD_PCE_LFO_MODE: + if (c.value==0) { + lfoMode=0; + } else { + lfoMode=c.value; + } + rWrite(0x08,lfoSpeed); + rWrite(0x09,lfoMode); + break; + case DIV_CMD_PCE_LFO_SPEED: + lfoSpeed=255-c.value; + rWrite(0x08,lfoSpeed); + rWrite(0x09,lfoMode); + break; + case DIV_CMD_NOTE_PORTA: { + int destFreq=NOTE_PERIODIC(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].noise=c.value; + chWrite(c.chan,0x07,chan[c.chan].noise?(0x80|chan[c.chan].note):0); + break; + case DIV_CMD_SAMPLE_MODE: + chan[c.chan].pcm=c.value; + break; + case DIV_CMD_SAMPLE_BANK: + sampleBank=c.value; + if (sampleBank>(parent->song.sample.size()/12)) { + sampleBank=parent->song.sample.size()/12; + } + break; + case DIV_CMD_PANNING: { + chan[c.chan].pan=(c.value&0xf0)|(c.value2>>4); + //chWrite(c.chan,0x05,isMuted[c.chan]?0:chan[c.chan].pan); + break; + } + case DIV_CMD_LEGATO: + chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((chan[c.chan].std.arp.will && !chan[c.chan].std.arp.mode)?(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_PCE)); + } + if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will) chan[c.chan].baseFreq=NOTE_PERIODIC(chan[c.chan].note); + chan[c.chan].inPorta=c.value; + break; + case DIV_CMD_GET_VOLMAX: + return 31; + break; + case DIV_ALWAYS_SET_VOLUME: + return 1; + break; + default: + break; + } + return 1; +} + +void DivPlatformVB::muteChannel(int ch, bool mute) { + isMuted[ch]=mute; + //chWrite(ch,0x05,isMuted[ch]?0:chan[ch].pan); + if (!isMuted[ch] && (chan[ch].pcm && chan[ch].dacSample!=-1)) { + //chWrite(ch,0x04,parent->song.disableSampleMacro?0xdf:(0xc0|chan[ch].outVol)); + //chWrite(ch,0x06,chan[ch].dacOut&0x1f); + } +} + +void DivPlatformVB::forceIns() { + for (int i=0; i<6; i++) { + chan[i].insChanged=true; + chan[i].freqChanged=true; + updateWave(i); + //chWrite(i,0x05,isMuted[i]?0:chan[i].pan); + } +} + +void* DivPlatformVB::getChanState(int ch) { + return &chan[ch]; +} + +DivMacroInt* DivPlatformVB::getChanMacroInt(int ch) { + return &chan[ch].std; +} + +DivDispatchOscBuffer* DivPlatformVB::getOscBuffer(int ch) { + return oscBuf[ch]; +} + +unsigned char* DivPlatformVB::getRegisterPool() { + return regPool; +} + +int DivPlatformVB::getRegisterPoolSize() { + return 0x600; +} + +void DivPlatformVB::reset() { + while (!writes.empty()) writes.pop(); + memset(regPool,0,0x600); + for (int i=0; i<6; i++) { + chan[i]=DivPlatformVB::Channel(); + chan[i].std.setEngine(parent); + chan[i].ws.setEngine(parent); + chan[i].ws.init(NULL,32,63,false); + } + if (dumpWrites) { + addWrite(0xffffffff,0); + } + vb->Power(); + lastPan=0xff; + tempL=0; + tempR=0; + cycles=0; + curChan=-1; + sampleBank=0; + lfoMode=0; + lfoSpeed=255; + // set per-channel initial panning + for (int i=0; i<6; i++) { + //chWrite(i,0x05,isMuted[i]?0:chan[i].pan); + } + delay=500; +} + +bool DivPlatformVB::isStereo() { + return true; +} + +bool DivPlatformVB::keyOffAffectsArp(int ch) { + return true; +} + +void DivPlatformVB::notifyWaveChange(int wave) { + for (int i=0; i<6; i++) { + if (chan[i].wave==wave) { + chan[i].ws.changeWave1(wave); + updateWave(i); + } + } +} + +void DivPlatformVB::notifyInsDeletion(void* ins) { + for (int i=0; i<6; i++) { + chan[i].std.notifyInsDeletion((DivInstrument*)ins); + } +} + +void DivPlatformVB::setFlags(const DivConfig& flags) { + chipClock=20000000.0; + antiClickEnabled=!flags.getBool("noAntiClick",false); + rate=chipClock/16; + for (int i=0; i<6; i++) { + oscBuf[i]->rate=rate; + } + + if (vb!=NULL) { + delete vb; + vb=NULL; + } + vb=new VSU; +} + +void DivPlatformVB::poke(unsigned int addr, unsigned short val) { + rWrite(addr,val); +} + +void DivPlatformVB::poke(std::vector& wlist) { + for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); +} + +int DivPlatformVB::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { + parent=p; + dumpWrites=false; + skipRegisterWrites=false; + for (int i=0; i<6; i++) { + isMuted[i]=false; + oscBuf[i]=new DivDispatchOscBuffer; + } + vb=NULL; + setFlags(flags); + reset(); + return 6; +} + +void DivPlatformVB::quit() { + for (int i=0; i<6; i++) { + delete oscBuf[i]; + } + if (vb!=NULL) { + delete vb; + vb=NULL; + } +} + +DivPlatformVB::~DivPlatformVB() { +} diff --git a/src/engine/platform/vb.h b/src/engine/platform/vb.h new file mode 100644 index 00000000..7bb22965 --- /dev/null +++ b/src/engine/platform/vb.h @@ -0,0 +1,121 @@ +/** + * 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 _PLATFORM_VB_H +#define _PLATFORM_VB_H + +#include "../dispatch.h" +#include +#include "../macroInt.h" +#include "../waveSynth.h" +#include "sound/vsu.h" + +class DivPlatformVB: public DivDispatch { + struct Channel { + int freq, baseFreq, pitch, pitch2, note, antiClickPeriodCount, antiClickWavePos; + int dacPeriod, dacRate, dacOut; + unsigned int dacPos; + int dacSample, ins; + unsigned char pan; + bool active, insChanged, freqChanged, keyOn, keyOff, inPorta, noise, pcm, furnaceDac, deferredWaveUpdate; + signed char vol, outVol, wave; + int macroVolMul; + DivMacroInt std; + DivWaveSynth ws; + void macroInit(DivInstrument* which) { + std.init(which); + pitch2=0; + } + Channel(): + freq(0), + baseFreq(0), + pitch(0), + pitch2(0), + note(0), + antiClickPeriodCount(0), + antiClickWavePos(0), + dacPeriod(0), + dacRate(0), + dacOut(0), + dacPos(0), + dacSample(-1), + ins(-1), + pan(255), + active(false), + insChanged(true), + freqChanged(false), + keyOn(false), + keyOff(false), + inPorta(false), + noise(false), + pcm(false), + furnaceDac(false), + deferredWaveUpdate(false), + vol(31), + outVol(31), + wave(-1), + macroVolMul(31) {} + }; + Channel chan[6]; + DivDispatchOscBuffer* oscBuf[6]; + bool isMuted[6]; + bool antiClickEnabled; + struct QueuedWrite { + unsigned short addr; + unsigned char val; + QueuedWrite(unsigned short a, unsigned char v): addr(a), val(v) {} + }; + std::queue writes; + unsigned char lastPan; + + int cycles, curChan, delay; + int tempL; + int tempR; + unsigned char sampleBank, lfoMode, lfoSpeed; + VSU* vb; + unsigned char regPool[0x600]; + void updateWave(int ch); + friend void putDispatchChip(void*,int); + 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); + 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 isStereo(); + 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(); + ~DivPlatformVB(); +}; + +#endif diff --git a/src/engine/sysDef.cpp b/src/engine/sysDef.cpp index cb2a3da4..86620976 100644 --- a/src/engine/sysDef.cpp +++ b/src/engine/sysDef.cpp @@ -1178,7 +1178,9 @@ void DivEngine::registerSystems() { {"Channel 1", "Channel 2", "Channel 3", "Channel 4", "Channel 5", "Noise"}, {"CH1", "CH2", "CH3", "CH4", "CH5", "NO"}, {DIV_CH_WAVE, DIV_CH_WAVE, DIV_CH_WAVE, DIV_CH_WAVE, DIV_CH_WAVE, DIV_CH_NOISE}, - {DIV_INS_VBOY, DIV_INS_VBOY, DIV_INS_VBOY, DIV_INS_VBOY, DIV_INS_VBOY, DIV_INS_VBOY} + {DIV_INS_VBOY, DIV_INS_VBOY, DIV_INS_VBOY, DIV_INS_VBOY, DIV_INS_VBOY, DIV_INS_VBOY}, + {}, + waveOnlyEffectHandlerMap ); sysDefs[DIV_SYSTEM_VRC7]=new DivSysDef( diff --git a/src/gui/guiConst.cpp b/src/gui/guiConst.cpp index 6b314963..b365ca98 100644 --- a/src/gui/guiConst.cpp +++ b/src/gui/guiConst.cpp @@ -936,6 +936,7 @@ const int availableSystems[]={ DIV_SYSTEM_QSOUND, DIV_SYSTEM_X1_010, DIV_SYSTEM_SWAN, + DIV_SYSTEM_VBOY, DIV_SYSTEM_VERA, DIV_SYSTEM_BUBSYS_WSG, DIV_SYSTEM_N163, @@ -1008,6 +1009,7 @@ const int chipsWave[]={ DIV_SYSTEM_PCE, DIV_SYSTEM_X1_010, DIV_SYSTEM_SWAN, + DIV_SYSTEM_VBOY, DIV_SYSTEM_BUBSYS_WSG, DIV_SYSTEM_N163, DIV_SYSTEM_FDS,