diff snes_spc/SPC_DSP.h @ 0:e38dacceb958

initial import
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 05:53:11 -0700
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/snes_spc/SPC_DSP.h	Fri Oct 21 05:53:11 2011 -0700
     1.3 @@ -0,0 +1,304 @@
     1.4 +// Highly accurate SNES SPC-700 DSP emulator
     1.5 +
     1.6 +// snes_spc 0.9.0
     1.7 +#ifndef SPC_DSP_H
     1.8 +#define SPC_DSP_H
     1.9 +
    1.10 +#include "blargg_common.h"
    1.11 +
    1.12 +extern "C" { typedef void (*dsp_copy_func_t)( unsigned char** io, void* state, size_t ); }
    1.13 +
    1.14 +class SPC_DSP {
    1.15 +public:
    1.16 +	typedef BOOST::uint8_t uint8_t;
    1.17 +	
    1.18 +// Setup
    1.19 +
    1.20 +	// Initializes DSP and has it use the 64K RAM provided
    1.21 +	void init( void* ram_64k );
    1.22 +
    1.23 +	// Sets destination for output samples. If out is NULL or out_size is 0,
    1.24 +	// doesn't generate any.
    1.25 +	typedef short sample_t;
    1.26 +	void set_output( sample_t* out, int out_size );
    1.27 +
    1.28 +	// Number of samples written to output since it was last set, always
    1.29 +	// a multiple of 2. Undefined if more samples were generated than
    1.30 +	// output buffer could hold.
    1.31 +	int sample_count() const;
    1.32 +
    1.33 +// Emulation
    1.34 +
    1.35 +	// Resets DSP to power-on state
    1.36 +	void reset();
    1.37 +
    1.38 +	// Emulates pressing reset switch on SNES
    1.39 +	void soft_reset();
    1.40 +	
    1.41 +	// Reads/writes DSP registers. For accuracy, you must first call run()
    1.42 +	// to catch the DSP up to present.
    1.43 +	int  read ( int addr ) const;
    1.44 +	void write( int addr, int data );
    1.45 +
    1.46 +	// Runs DSP for specified number of clocks (~1024000 per second). Every 32 clocks
    1.47 +	// a pair of samples is be generated.
    1.48 +	void run( int clock_count );
    1.49 +	
    1.50 +// Sound control
    1.51 +
    1.52 +	// Mutes voices corresponding to non-zero bits in mask (issues repeated KOFF events).
    1.53 +	// Reduces emulation accuracy.
    1.54 +	enum { voice_count = 8 };
    1.55 +	void mute_voices( int mask );
    1.56 +
    1.57 +// State
    1.58 +	
    1.59 +	// Resets DSP and uses supplied values to initialize registers
    1.60 +	enum { register_count = 128 };
    1.61 +	void load( uint8_t const regs [register_count] );
    1.62 +
    1.63 +	// Saves/loads exact emulator state
    1.64 +	enum { state_size = 640 }; // maximum space needed when saving
    1.65 +	typedef dsp_copy_func_t copy_func_t;
    1.66 +	void copy_state( unsigned char** io, copy_func_t );
    1.67 +
    1.68 +	// Returns non-zero if new key-on events occurred since last call
    1.69 +	bool check_kon();
    1.70 +	
    1.71 +// DSP register addresses
    1.72 +
    1.73 +	// Global registers
    1.74 +	enum {
    1.75 +	    r_mvoll = 0x0C, r_mvolr = 0x1C,
    1.76 +	    r_evoll = 0x2C, r_evolr = 0x3C,
    1.77 +	    r_kon   = 0x4C, r_koff  = 0x5C,
    1.78 +	    r_flg   = 0x6C, r_endx  = 0x7C,
    1.79 +	    r_efb   = 0x0D, r_pmon  = 0x2D,
    1.80 +	    r_non   = 0x3D, r_eon   = 0x4D,
    1.81 +	    r_dir   = 0x5D, r_esa   = 0x6D,
    1.82 +	    r_edl   = 0x7D,
    1.83 +	    r_fir   = 0x0F // 8 coefficients at 0x0F, 0x1F ... 0x7F
    1.84 +	};
    1.85 +
    1.86 +	// Voice registers
    1.87 +	enum {
    1.88 +		v_voll   = 0x00, v_volr   = 0x01,
    1.89 +		v_pitchl = 0x02, v_pitchh = 0x03,
    1.90 +		v_srcn   = 0x04, v_adsr0  = 0x05,
    1.91 +		v_adsr1  = 0x06, v_gain   = 0x07,
    1.92 +		v_envx   = 0x08, v_outx   = 0x09
    1.93 +	};
    1.94 +
    1.95 +public:
    1.96 +	enum { extra_size = 16 };
    1.97 +	sample_t* extra()               { return m.extra; }
    1.98 +	sample_t const* out_pos() const { return m.out; }
    1.99 +	void disable_surround( bool ) { } // not supported
   1.100 +public:
   1.101 +	BLARGG_DISABLE_NOTHROW
   1.102 +	
   1.103 +	typedef BOOST::int8_t   int8_t;
   1.104 +	typedef BOOST::int16_t int16_t;
   1.105 +	
   1.106 +	enum { echo_hist_size = 8 };
   1.107 +	
   1.108 +	enum env_mode_t { env_release, env_attack, env_decay, env_sustain };
   1.109 +	enum { brr_buf_size = 12 };
   1.110 +	struct voice_t
   1.111 +	{
   1.112 +		int buf [brr_buf_size*2];// decoded samples (twice the size to simplify wrap handling)
   1.113 +		int buf_pos;            // place in buffer where next samples will be decoded
   1.114 +		int interp_pos;         // relative fractional position in sample (0x1000 = 1.0)
   1.115 +		int brr_addr;           // address of current BRR block
   1.116 +		int brr_offset;         // current decoding offset in BRR block
   1.117 +		uint8_t* regs;          // pointer to voice's DSP registers
   1.118 +		int vbit;               // bitmask for voice: 0x01 for voice 0, 0x02 for voice 1, etc.
   1.119 +		int kon_delay;          // KON delay/current setup phase
   1.120 +		env_mode_t env_mode;
   1.121 +		int env;                // current envelope level
   1.122 +		int hidden_env;         // used by GAIN mode 7, very obscure quirk
   1.123 +		uint8_t t_envx_out;
   1.124 +	};
   1.125 +private:
   1.126 +	enum { brr_block_size = 9 };
   1.127 +	
   1.128 +	struct state_t
   1.129 +	{
   1.130 +		uint8_t regs [register_count];
   1.131 +		
   1.132 +		// Echo history keeps most recent 8 samples (twice the size to simplify wrap handling)
   1.133 +		int echo_hist [echo_hist_size * 2] [2];
   1.134 +		int (*echo_hist_pos) [2]; // &echo_hist [0 to 7]
   1.135 +		
   1.136 +		int every_other_sample; // toggles every sample
   1.137 +		int kon;                // KON value when last checked
   1.138 +		int noise;
   1.139 +		int counter;
   1.140 +		int echo_offset;        // offset from ESA in echo buffer
   1.141 +		int echo_length;        // number of bytes that echo_offset will stop at
   1.142 +		int phase;              // next clock cycle to run (0-31)
   1.143 +		bool kon_check;         // set when a new KON occurs
   1.144 +		
   1.145 +		// Hidden registers also written to when main register is written to
   1.146 +		int new_kon;
   1.147 +		uint8_t endx_buf;
   1.148 +		uint8_t envx_buf;
   1.149 +		uint8_t outx_buf;
   1.150 +		
   1.151 +		// Temporary state between clocks
   1.152 +		
   1.153 +		// read once per sample
   1.154 +		int t_pmon;
   1.155 +		int t_non;
   1.156 +		int t_eon;
   1.157 +		int t_dir;
   1.158 +		int t_koff;
   1.159 +		
   1.160 +		// read a few clocks ahead then used
   1.161 +		int t_brr_next_addr;
   1.162 +		int t_adsr0;
   1.163 +		int t_brr_header;
   1.164 +		int t_brr_byte;
   1.165 +		int t_srcn;
   1.166 +		int t_esa;
   1.167 +		int t_echo_enabled;
   1.168 +		
   1.169 +		// internal state that is recalculated every sample
   1.170 +		int t_dir_addr;
   1.171 +		int t_pitch;
   1.172 +		int t_output;
   1.173 +		int t_looped;
   1.174 +		int t_echo_ptr;
   1.175 +		
   1.176 +		// left/right sums
   1.177 +		int t_main_out [2];
   1.178 +		int t_echo_out [2];
   1.179 +		int t_echo_in  [2];
   1.180 +		
   1.181 +		voice_t voices [voice_count];
   1.182 +		
   1.183 +		// non-emulation state
   1.184 +		uint8_t* ram; // 64K shared RAM between DSP and SMP
   1.185 +		int mute_mask;
   1.186 +		sample_t* out;
   1.187 +		sample_t* out_end;
   1.188 +		sample_t* out_begin;
   1.189 +		sample_t extra [extra_size];
   1.190 +	};
   1.191 +	state_t m;
   1.192 +	
   1.193 +	void init_counter();
   1.194 +	void run_counters();
   1.195 +	unsigned read_counter( int rate );
   1.196 +	
   1.197 +	int  interpolate( voice_t const* v );
   1.198 +	void run_envelope( voice_t* const v );
   1.199 +	void decode_brr( voice_t* v );
   1.200 +
   1.201 +	void misc_27();
   1.202 +	void misc_28();
   1.203 +	void misc_29();
   1.204 +	void misc_30();
   1.205 +
   1.206 +	void voice_output( voice_t const* v, int ch );
   1.207 +	void voice_V1( voice_t* const );
   1.208 +	void voice_V2( voice_t* const );
   1.209 +	void voice_V3( voice_t* const );
   1.210 +	void voice_V3a( voice_t* const );
   1.211 +	void voice_V3b( voice_t* const );
   1.212 +	void voice_V3c( voice_t* const );
   1.213 +	void voice_V4( voice_t* const );
   1.214 +	void voice_V5( voice_t* const );
   1.215 +	void voice_V6( voice_t* const );
   1.216 +	void voice_V7( voice_t* const );
   1.217 +	void voice_V8( voice_t* const );
   1.218 +	void voice_V9( voice_t* const );
   1.219 +	void voice_V7_V4_V1( voice_t* const );
   1.220 +	void voice_V8_V5_V2( voice_t* const );
   1.221 +	void voice_V9_V6_V3( voice_t* const );
   1.222 +
   1.223 +	void echo_read( int ch );
   1.224 +	int  echo_output( int ch );
   1.225 +	void echo_write( int ch );
   1.226 +	void echo_22();
   1.227 +	void echo_23();
   1.228 +	void echo_24();
   1.229 +	void echo_25();
   1.230 +	void echo_26();
   1.231 +	void echo_27();
   1.232 +	void echo_28();
   1.233 +	void echo_29();
   1.234 +	void echo_30();
   1.235 +	
   1.236 +	void soft_reset_common();
   1.237 +};
   1.238 +
   1.239 +#include <assert.h>
   1.240 +
   1.241 +inline int SPC_DSP::sample_count() const { return m.out - m.out_begin; }
   1.242 +
   1.243 +inline int SPC_DSP::read( int addr ) const
   1.244 +{
   1.245 +	assert( (unsigned) addr < register_count );
   1.246 +	return m.regs [addr];
   1.247 +}
   1.248 +
   1.249 +inline void SPC_DSP::write( int addr, int data )
   1.250 +{
   1.251 +	assert( (unsigned) addr < register_count );
   1.252 +	
   1.253 +	m.regs [addr] = (uint8_t) data;
   1.254 +	switch ( addr & 0x0F )
   1.255 +	{
   1.256 +	case v_envx:
   1.257 +		m.envx_buf = (uint8_t) data;
   1.258 +		break;
   1.259 +		
   1.260 +	case v_outx:
   1.261 +		m.outx_buf = (uint8_t) data;
   1.262 +		break;
   1.263 +	
   1.264 +	case 0x0C:
   1.265 +		if ( addr == r_kon )
   1.266 +			m.new_kon = (uint8_t) data;
   1.267 +		
   1.268 +		if ( addr == r_endx ) // always cleared, regardless of data written
   1.269 +		{
   1.270 +			m.endx_buf = 0;
   1.271 +			m.regs [r_endx] = 0;
   1.272 +		}
   1.273 +		break;
   1.274 +	}
   1.275 +}
   1.276 +
   1.277 +inline void SPC_DSP::mute_voices( int mask ) { m.mute_mask = mask; }
   1.278 +
   1.279 +inline bool SPC_DSP::check_kon()
   1.280 +{
   1.281 +	bool old = m.kon_check;
   1.282 +	m.kon_check = 0;
   1.283 +	return old;
   1.284 +}
   1.285 +
   1.286 +#if !SPC_NO_COPY_STATE_FUNCS
   1.287 +
   1.288 +class SPC_State_Copier {
   1.289 +	SPC_DSP::copy_func_t func;
   1.290 +	unsigned char** buf;
   1.291 +public:
   1.292 +	SPC_State_Copier( unsigned char** p, SPC_DSP::copy_func_t f ) { func = f; buf = p; }
   1.293 +	void copy( void* state, size_t size );
   1.294 +	int copy_int( int state, int size );
   1.295 +	void skip( int count );
   1.296 +	void extra();
   1.297 +};
   1.298 +
   1.299 +#define SPC_COPY( type, state )\
   1.300 +{\
   1.301 +	state = (BOOST::type) copier.copy_int( state, sizeof (BOOST::type) );\
   1.302 +	assert( (BOOST::type) state == state );\
   1.303 +}
   1.304 +
   1.305 +#endif
   1.306 +
   1.307 +#endif