rlm@0: // Fast SNES SPC-700 DSP emulator (about 3x speed of accurate one) rlm@0: rlm@0: // snes_spc 0.9.0 rlm@0: #ifndef SPC_DSP_H rlm@0: #define SPC_DSP_H rlm@0: rlm@0: #include "blargg_common.h" rlm@0: rlm@0: class SPC_DSP { rlm@0: public: rlm@0: typedef BOOST::uint8_t uint8_t; rlm@0: rlm@0: // Setup rlm@0: rlm@0: // Initializes DSP and has it use the 64K RAM provided rlm@0: void init( void* ram_64k ); rlm@0: rlm@0: // Sets destination for output samples. If out is NULL or out_size is 0, rlm@0: // doesn't generate any. rlm@0: typedef short sample_t; rlm@0: void set_output( sample_t* out, int out_size ); rlm@0: rlm@0: // Number of samples written to output since it was last set, always rlm@0: // a multiple of 2. Undefined if more samples were generated than rlm@0: // output buffer could hold. rlm@0: int sample_count() const; rlm@0: rlm@0: // Emulation rlm@0: rlm@0: // Resets DSP to power-on state rlm@0: void reset(); rlm@0: rlm@0: // Emulates pressing reset switch on SNES rlm@0: void soft_reset(); rlm@0: rlm@0: // Reads/writes DSP registers. For accuracy, you must first call spc_run_dsp() rlm@0: // to catch the DSP up to present. rlm@0: int read ( int addr ) const; rlm@0: void write( int addr, int data ); rlm@0: rlm@0: // Runs DSP for specified number of clocks (~1024000 per second). Every 32 clocks rlm@0: // a pair of samples is be generated. rlm@0: void run( int clock_count ); rlm@0: rlm@0: // Sound control rlm@0: rlm@0: // Mutes voices corresponding to non-zero bits in mask (overrides VxVOL with 0). rlm@0: // Reduces emulation accuracy. rlm@0: enum { voice_count = 8 }; rlm@0: void mute_voices( int mask ); rlm@0: rlm@0: // If true, prevents channels and global volumes from being phase-negated rlm@0: void disable_surround( bool disable = true ); rlm@0: rlm@0: // State rlm@0: rlm@0: // Resets DSP and uses supplied values to initialize registers rlm@0: enum { register_count = 128 }; rlm@0: void load( uint8_t const regs [register_count] ); rlm@0: rlm@0: // DSP register addresses rlm@0: rlm@0: // Global registers rlm@0: enum { rlm@0: r_mvoll = 0x0C, r_mvolr = 0x1C, rlm@0: r_evoll = 0x2C, r_evolr = 0x3C, rlm@0: r_kon = 0x4C, r_koff = 0x5C, rlm@0: r_flg = 0x6C, r_endx = 0x7C, rlm@0: r_efb = 0x0D, r_pmon = 0x2D, rlm@0: r_non = 0x3D, r_eon = 0x4D, rlm@0: r_dir = 0x5D, r_esa = 0x6D, rlm@0: r_edl = 0x7D, rlm@0: r_fir = 0x0F // 8 coefficients at 0x0F, 0x1F ... 0x7F rlm@0: }; rlm@0: rlm@0: // Voice registers rlm@0: enum { rlm@0: v_voll = 0x00, v_volr = 0x01, rlm@0: v_pitchl = 0x02, v_pitchh = 0x03, rlm@0: v_srcn = 0x04, v_adsr0 = 0x05, rlm@0: v_adsr1 = 0x06, v_gain = 0x07, rlm@0: v_envx = 0x08, v_outx = 0x09 rlm@0: }; rlm@0: rlm@0: public: rlm@0: enum { extra_size = 16 }; rlm@0: sample_t* extra() { return m.extra; } rlm@0: sample_t const* out_pos() const { return m.out; } rlm@0: public: rlm@0: BLARGG_DISABLE_NOTHROW rlm@0: rlm@0: typedef BOOST::int8_t int8_t; rlm@0: typedef BOOST::int16_t int16_t; rlm@0: rlm@0: enum { echo_hist_size = 8 }; rlm@0: rlm@0: enum env_mode_t { env_release, env_attack, env_decay, env_sustain }; rlm@0: enum { brr_buf_size = 12 }; rlm@0: struct voice_t rlm@0: { rlm@0: int buf [brr_buf_size*2];// decoded samples (twice the size to simplify wrap handling) rlm@0: int* buf_pos; // place in buffer where next samples will be decoded rlm@0: int interp_pos; // relative fractional position in sample (0x1000 = 1.0) rlm@0: int brr_addr; // address of current BRR block rlm@0: int brr_offset; // current decoding offset in BRR block rlm@0: int kon_delay; // KON delay/current setup phase rlm@0: env_mode_t env_mode; rlm@0: int env; // current envelope level rlm@0: int hidden_env; // used by GAIN mode 7, very obscure quirk rlm@0: int volume [2]; // copy of volume from DSP registers, with surround disabled rlm@0: int enabled; // -1 if enabled, 0 if muted rlm@0: }; rlm@0: private: rlm@0: struct state_t rlm@0: { rlm@0: uint8_t regs [register_count]; rlm@0: rlm@0: // Echo history keeps most recent 8 samples (twice the size to simplify wrap handling) rlm@0: int echo_hist [echo_hist_size * 2] [2]; rlm@0: int (*echo_hist_pos) [2]; // &echo_hist [0 to 7] rlm@0: rlm@0: int every_other_sample; // toggles every sample rlm@0: int kon; // KON value when last checked rlm@0: int noise; rlm@0: int echo_offset; // offset from ESA in echo buffer rlm@0: int echo_length; // number of bytes that echo_offset will stop at rlm@0: int phase; // next clock cycle to run (0-31) rlm@0: unsigned counters [4]; rlm@0: rlm@0: int new_kon; rlm@0: int t_koff; rlm@0: rlm@0: voice_t voices [voice_count]; rlm@0: rlm@0: unsigned* counter_select [32]; rlm@0: rlm@0: // non-emulation state rlm@0: uint8_t* ram; // 64K shared RAM between DSP and SMP rlm@0: int mute_mask; rlm@0: int surround_threshold; rlm@0: sample_t* out; rlm@0: sample_t* out_end; rlm@0: sample_t* out_begin; rlm@0: sample_t extra [extra_size]; rlm@0: }; rlm@0: state_t m; rlm@0: rlm@0: void init_counter(); rlm@0: void run_counter( int ); rlm@0: void soft_reset_common(); rlm@0: void write_outline( int addr, int data ); rlm@0: void update_voice_vol( int addr ); rlm@0: }; rlm@0: rlm@0: #include rlm@0: rlm@0: inline int SPC_DSP::sample_count() const { return m.out - m.out_begin; } rlm@0: rlm@0: inline int SPC_DSP::read( int addr ) const rlm@0: { rlm@0: assert( (unsigned) addr < register_count ); rlm@0: return m.regs [addr]; rlm@0: } rlm@0: rlm@0: inline void SPC_DSP::update_voice_vol( int addr ) rlm@0: { rlm@0: int l = (int8_t) m.regs [addr + v_voll]; rlm@0: int r = (int8_t) m.regs [addr + v_volr]; rlm@0: rlm@0: if ( l * r < m.surround_threshold ) rlm@0: { rlm@0: // signs differ, so negate those that are negative rlm@0: l ^= l >> 7; rlm@0: r ^= r >> 7; rlm@0: } rlm@0: rlm@0: voice_t& v = m.voices [addr >> 4]; rlm@0: int enabled = v.enabled; rlm@0: v.volume [0] = l & enabled; rlm@0: v.volume [1] = r & enabled; rlm@0: } rlm@0: rlm@0: inline void SPC_DSP::write( int addr, int data ) rlm@0: { rlm@0: assert( (unsigned) addr < register_count ); rlm@0: rlm@0: m.regs [addr] = (uint8_t) data; rlm@0: int low = addr & 0x0F; rlm@0: if ( low < 0x2 ) // voice volumes rlm@0: { rlm@0: update_voice_vol( low ^ addr ); rlm@0: } rlm@0: else if ( low == 0xC ) rlm@0: { rlm@0: if ( addr == r_kon ) rlm@0: m.new_kon = (uint8_t) data; rlm@0: rlm@0: if ( addr == r_endx ) // always cleared, regardless of data written rlm@0: m.regs [r_endx] = 0; rlm@0: } rlm@0: } rlm@0: rlm@0: inline void SPC_DSP::disable_surround( bool disable ) rlm@0: { rlm@0: m.surround_threshold = disable ? 0 : -0x4000; rlm@0: } rlm@0: rlm@0: #define SPC_NO_COPY_STATE_FUNCS 1 rlm@0: rlm@0: #define SPC_LESS_ACCURATE 1 rlm@0: rlm@0: #endif