rlm@0: // Highly accurate SNES SPC-700 DSP emulator 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: extern "C" { typedef void (*dsp_copy_func_t)( unsigned char** io, void* state, size_t ); } 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 run() 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 (issues repeated KOFF events). rlm@0: // Reduces emulation accuracy. rlm@0: enum { voice_count = 8 }; rlm@0: void mute_voices( int mask ); 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: // Saves/loads exact emulator state rlm@0: enum { state_size = 640 }; // maximum space needed when saving rlm@0: typedef dsp_copy_func_t copy_func_t; rlm@0: void copy_state( unsigned char** io, copy_func_t ); rlm@0: rlm@0: // Returns non-zero if new key-on events occurred since last call rlm@0: bool check_kon(); 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: void disable_surround( bool ) { } // not supported 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: uint8_t* regs; // pointer to voice's DSP registers rlm@0: int vbit; // bitmask for voice: 0x01 for voice 0, 0x02 for voice 1, etc. 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: uint8_t t_envx_out; rlm@0: }; rlm@0: private: rlm@0: enum { brr_block_size = 9 }; rlm@0: 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 counter; 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: bool kon_check; // set when a new KON occurs rlm@0: rlm@0: // Hidden registers also written to when main register is written to rlm@0: int new_kon; rlm@0: uint8_t endx_buf; rlm@0: uint8_t envx_buf; rlm@0: uint8_t outx_buf; rlm@0: rlm@0: // Temporary state between clocks rlm@0: rlm@0: // read once per sample rlm@0: int t_pmon; rlm@0: int t_non; rlm@0: int t_eon; rlm@0: int t_dir; rlm@0: int t_koff; rlm@0: rlm@0: // read a few clocks ahead then used rlm@0: int t_brr_next_addr; rlm@0: int t_adsr0; rlm@0: int t_brr_header; rlm@0: int t_brr_byte; rlm@0: int t_srcn; rlm@0: int t_esa; rlm@0: int t_echo_enabled; rlm@0: rlm@0: // internal state that is recalculated every sample rlm@0: int t_dir_addr; rlm@0: int t_pitch; rlm@0: int t_output; rlm@0: int t_looped; rlm@0: int t_echo_ptr; rlm@0: rlm@0: // left/right sums rlm@0: int t_main_out [2]; rlm@0: int t_echo_out [2]; rlm@0: int t_echo_in [2]; rlm@0: rlm@0: voice_t voices [voice_count]; 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: 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_counters(); rlm@0: unsigned read_counter( int rate ); rlm@0: rlm@0: int interpolate( voice_t const* v ); rlm@0: void run_envelope( voice_t* const v ); rlm@0: void decode_brr( voice_t* v ); rlm@0: rlm@0: void misc_27(); rlm@0: void misc_28(); rlm@0: void misc_29(); rlm@0: void misc_30(); rlm@0: rlm@0: void voice_output( voice_t const* v, int ch ); rlm@0: void voice_V1( voice_t* const ); rlm@0: void voice_V2( voice_t* const ); rlm@0: void voice_V3( voice_t* const ); rlm@0: void voice_V3a( voice_t* const ); rlm@0: void voice_V3b( voice_t* const ); rlm@0: void voice_V3c( voice_t* const ); rlm@0: void voice_V4( voice_t* const ); rlm@0: void voice_V5( voice_t* const ); rlm@0: void voice_V6( voice_t* const ); rlm@0: void voice_V7( voice_t* const ); rlm@0: void voice_V8( voice_t* const ); rlm@0: void voice_V9( voice_t* const ); rlm@0: void voice_V7_V4_V1( voice_t* const ); rlm@0: void voice_V8_V5_V2( voice_t* const ); rlm@0: void voice_V9_V6_V3( voice_t* const ); rlm@0: rlm@0: void echo_read( int ch ); rlm@0: int echo_output( int ch ); rlm@0: void echo_write( int ch ); rlm@0: void echo_22(); rlm@0: void echo_23(); rlm@0: void echo_24(); rlm@0: void echo_25(); rlm@0: void echo_26(); rlm@0: void echo_27(); rlm@0: void echo_28(); rlm@0: void echo_29(); rlm@0: void echo_30(); rlm@0: rlm@0: void soft_reset_common(); 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::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: switch ( addr & 0x0F ) rlm@0: { rlm@0: case v_envx: rlm@0: m.envx_buf = (uint8_t) data; rlm@0: break; rlm@0: rlm@0: case v_outx: rlm@0: m.outx_buf = (uint8_t) data; rlm@0: break; rlm@0: rlm@0: case 0x0C: 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: { rlm@0: m.endx_buf = 0; rlm@0: m.regs [r_endx] = 0; rlm@0: } rlm@0: break; rlm@0: } rlm@0: } rlm@0: rlm@0: inline void SPC_DSP::mute_voices( int mask ) { m.mute_mask = mask; } rlm@0: rlm@0: inline bool SPC_DSP::check_kon() rlm@0: { rlm@0: bool old = m.kon_check; rlm@0: m.kon_check = 0; rlm@0: return old; rlm@0: } rlm@0: rlm@0: #if !SPC_NO_COPY_STATE_FUNCS rlm@0: rlm@0: class SPC_State_Copier { rlm@0: SPC_DSP::copy_func_t func; rlm@0: unsigned char** buf; rlm@0: public: rlm@0: SPC_State_Copier( unsigned char** p, SPC_DSP::copy_func_t f ) { func = f; buf = p; } rlm@0: void copy( void* state, size_t size ); rlm@0: int copy_int( int state, int size ); rlm@0: void skip( int count ); rlm@0: void extra(); rlm@0: }; rlm@0: rlm@0: #define SPC_COPY( type, state )\ rlm@0: {\ rlm@0: state = (BOOST::type) copier.copy_int( state, sizeof (BOOST::type) );\ rlm@0: assert( (BOOST::type) state == state );\ rlm@0: } rlm@0: rlm@0: #endif rlm@0: rlm@0: #endif