annotate snes_spc/SPC_DSP.h @ 1:2ad543b339f1

enhanced makefile
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 06:03:53 -0700
parents e38dacceb958
children
rev   line source
rlm@0 1 // Highly accurate SNES SPC-700 DSP emulator
rlm@0 2
rlm@0 3 // snes_spc 0.9.0
rlm@0 4 #ifndef SPC_DSP_H
rlm@0 5 #define SPC_DSP_H
rlm@0 6
rlm@0 7 #include "blargg_common.h"
rlm@0 8
rlm@0 9 extern "C" { typedef void (*dsp_copy_func_t)( unsigned char** io, void* state, size_t ); }
rlm@0 10
rlm@0 11 class SPC_DSP {
rlm@0 12 public:
rlm@0 13 typedef BOOST::uint8_t uint8_t;
rlm@0 14
rlm@0 15 // Setup
rlm@0 16
rlm@0 17 // Initializes DSP and has it use the 64K RAM provided
rlm@0 18 void init( void* ram_64k );
rlm@0 19
rlm@0 20 // Sets destination for output samples. If out is NULL or out_size is 0,
rlm@0 21 // doesn't generate any.
rlm@0 22 typedef short sample_t;
rlm@0 23 void set_output( sample_t* out, int out_size );
rlm@0 24
rlm@0 25 // Number of samples written to output since it was last set, always
rlm@0 26 // a multiple of 2. Undefined if more samples were generated than
rlm@0 27 // output buffer could hold.
rlm@0 28 int sample_count() const;
rlm@0 29
rlm@0 30 // Emulation
rlm@0 31
rlm@0 32 // Resets DSP to power-on state
rlm@0 33 void reset();
rlm@0 34
rlm@0 35 // Emulates pressing reset switch on SNES
rlm@0 36 void soft_reset();
rlm@0 37
rlm@0 38 // Reads/writes DSP registers. For accuracy, you must first call run()
rlm@0 39 // to catch the DSP up to present.
rlm@0 40 int read ( int addr ) const;
rlm@0 41 void write( int addr, int data );
rlm@0 42
rlm@0 43 // Runs DSP for specified number of clocks (~1024000 per second). Every 32 clocks
rlm@0 44 // a pair of samples is be generated.
rlm@0 45 void run( int clock_count );
rlm@0 46
rlm@0 47 // Sound control
rlm@0 48
rlm@0 49 // Mutes voices corresponding to non-zero bits in mask (issues repeated KOFF events).
rlm@0 50 // Reduces emulation accuracy.
rlm@0 51 enum { voice_count = 8 };
rlm@0 52 void mute_voices( int mask );
rlm@0 53
rlm@0 54 // State
rlm@0 55
rlm@0 56 // Resets DSP and uses supplied values to initialize registers
rlm@0 57 enum { register_count = 128 };
rlm@0 58 void load( uint8_t const regs [register_count] );
rlm@0 59
rlm@0 60 // Saves/loads exact emulator state
rlm@0 61 enum { state_size = 640 }; // maximum space needed when saving
rlm@0 62 typedef dsp_copy_func_t copy_func_t;
rlm@0 63 void copy_state( unsigned char** io, copy_func_t );
rlm@0 64
rlm@0 65 // Returns non-zero if new key-on events occurred since last call
rlm@0 66 bool check_kon();
rlm@0 67
rlm@0 68 // DSP register addresses
rlm@0 69
rlm@0 70 // Global registers
rlm@0 71 enum {
rlm@0 72 r_mvoll = 0x0C, r_mvolr = 0x1C,
rlm@0 73 r_evoll = 0x2C, r_evolr = 0x3C,
rlm@0 74 r_kon = 0x4C, r_koff = 0x5C,
rlm@0 75 r_flg = 0x6C, r_endx = 0x7C,
rlm@0 76 r_efb = 0x0D, r_pmon = 0x2D,
rlm@0 77 r_non = 0x3D, r_eon = 0x4D,
rlm@0 78 r_dir = 0x5D, r_esa = 0x6D,
rlm@0 79 r_edl = 0x7D,
rlm@0 80 r_fir = 0x0F // 8 coefficients at 0x0F, 0x1F ... 0x7F
rlm@0 81 };
rlm@0 82
rlm@0 83 // Voice registers
rlm@0 84 enum {
rlm@0 85 v_voll = 0x00, v_volr = 0x01,
rlm@0 86 v_pitchl = 0x02, v_pitchh = 0x03,
rlm@0 87 v_srcn = 0x04, v_adsr0 = 0x05,
rlm@0 88 v_adsr1 = 0x06, v_gain = 0x07,
rlm@0 89 v_envx = 0x08, v_outx = 0x09
rlm@0 90 };
rlm@0 91
rlm@0 92 public:
rlm@0 93 enum { extra_size = 16 };
rlm@0 94 sample_t* extra() { return m.extra; }
rlm@0 95 sample_t const* out_pos() const { return m.out; }
rlm@0 96 void disable_surround( bool ) { } // not supported
rlm@0 97 public:
rlm@0 98 BLARGG_DISABLE_NOTHROW
rlm@0 99
rlm@0 100 typedef BOOST::int8_t int8_t;
rlm@0 101 typedef BOOST::int16_t int16_t;
rlm@0 102
rlm@0 103 enum { echo_hist_size = 8 };
rlm@0 104
rlm@0 105 enum env_mode_t { env_release, env_attack, env_decay, env_sustain };
rlm@0 106 enum { brr_buf_size = 12 };
rlm@0 107 struct voice_t
rlm@0 108 {
rlm@0 109 int buf [brr_buf_size*2];// decoded samples (twice the size to simplify wrap handling)
rlm@0 110 int buf_pos; // place in buffer where next samples will be decoded
rlm@0 111 int interp_pos; // relative fractional position in sample (0x1000 = 1.0)
rlm@0 112 int brr_addr; // address of current BRR block
rlm@0 113 int brr_offset; // current decoding offset in BRR block
rlm@0 114 uint8_t* regs; // pointer to voice's DSP registers
rlm@0 115 int vbit; // bitmask for voice: 0x01 for voice 0, 0x02 for voice 1, etc.
rlm@0 116 int kon_delay; // KON delay/current setup phase
rlm@0 117 env_mode_t env_mode;
rlm@0 118 int env; // current envelope level
rlm@0 119 int hidden_env; // used by GAIN mode 7, very obscure quirk
rlm@0 120 uint8_t t_envx_out;
rlm@0 121 };
rlm@0 122 private:
rlm@0 123 enum { brr_block_size = 9 };
rlm@0 124
rlm@0 125 struct state_t
rlm@0 126 {
rlm@0 127 uint8_t regs [register_count];
rlm@0 128
rlm@0 129 // Echo history keeps most recent 8 samples (twice the size to simplify wrap handling)
rlm@0 130 int echo_hist [echo_hist_size * 2] [2];
rlm@0 131 int (*echo_hist_pos) [2]; // &echo_hist [0 to 7]
rlm@0 132
rlm@0 133 int every_other_sample; // toggles every sample
rlm@0 134 int kon; // KON value when last checked
rlm@0 135 int noise;
rlm@0 136 int counter;
rlm@0 137 int echo_offset; // offset from ESA in echo buffer
rlm@0 138 int echo_length; // number of bytes that echo_offset will stop at
rlm@0 139 int phase; // next clock cycle to run (0-31)
rlm@0 140 bool kon_check; // set when a new KON occurs
rlm@0 141
rlm@0 142 // Hidden registers also written to when main register is written to
rlm@0 143 int new_kon;
rlm@0 144 uint8_t endx_buf;
rlm@0 145 uint8_t envx_buf;
rlm@0 146 uint8_t outx_buf;
rlm@0 147
rlm@0 148 // Temporary state between clocks
rlm@0 149
rlm@0 150 // read once per sample
rlm@0 151 int t_pmon;
rlm@0 152 int t_non;
rlm@0 153 int t_eon;
rlm@0 154 int t_dir;
rlm@0 155 int t_koff;
rlm@0 156
rlm@0 157 // read a few clocks ahead then used
rlm@0 158 int t_brr_next_addr;
rlm@0 159 int t_adsr0;
rlm@0 160 int t_brr_header;
rlm@0 161 int t_brr_byte;
rlm@0 162 int t_srcn;
rlm@0 163 int t_esa;
rlm@0 164 int t_echo_enabled;
rlm@0 165
rlm@0 166 // internal state that is recalculated every sample
rlm@0 167 int t_dir_addr;
rlm@0 168 int t_pitch;
rlm@0 169 int t_output;
rlm@0 170 int t_looped;
rlm@0 171 int t_echo_ptr;
rlm@0 172
rlm@0 173 // left/right sums
rlm@0 174 int t_main_out [2];
rlm@0 175 int t_echo_out [2];
rlm@0 176 int t_echo_in [2];
rlm@0 177
rlm@0 178 voice_t voices [voice_count];
rlm@0 179
rlm@0 180 // non-emulation state
rlm@0 181 uint8_t* ram; // 64K shared RAM between DSP and SMP
rlm@0 182 int mute_mask;
rlm@0 183 sample_t* out;
rlm@0 184 sample_t* out_end;
rlm@0 185 sample_t* out_begin;
rlm@0 186 sample_t extra [extra_size];
rlm@0 187 };
rlm@0 188 state_t m;
rlm@0 189
rlm@0 190 void init_counter();
rlm@0 191 void run_counters();
rlm@0 192 unsigned read_counter( int rate );
rlm@0 193
rlm@0 194 int interpolate( voice_t const* v );
rlm@0 195 void run_envelope( voice_t* const v );
rlm@0 196 void decode_brr( voice_t* v );
rlm@0 197
rlm@0 198 void misc_27();
rlm@0 199 void misc_28();
rlm@0 200 void misc_29();
rlm@0 201 void misc_30();
rlm@0 202
rlm@0 203 void voice_output( voice_t const* v, int ch );
rlm@0 204 void voice_V1( voice_t* const );
rlm@0 205 void voice_V2( voice_t* const );
rlm@0 206 void voice_V3( voice_t* const );
rlm@0 207 void voice_V3a( voice_t* const );
rlm@0 208 void voice_V3b( voice_t* const );
rlm@0 209 void voice_V3c( voice_t* const );
rlm@0 210 void voice_V4( voice_t* const );
rlm@0 211 void voice_V5( voice_t* const );
rlm@0 212 void voice_V6( voice_t* const );
rlm@0 213 void voice_V7( voice_t* const );
rlm@0 214 void voice_V8( voice_t* const );
rlm@0 215 void voice_V9( voice_t* const );
rlm@0 216 void voice_V7_V4_V1( voice_t* const );
rlm@0 217 void voice_V8_V5_V2( voice_t* const );
rlm@0 218 void voice_V9_V6_V3( voice_t* const );
rlm@0 219
rlm@0 220 void echo_read( int ch );
rlm@0 221 int echo_output( int ch );
rlm@0 222 void echo_write( int ch );
rlm@0 223 void echo_22();
rlm@0 224 void echo_23();
rlm@0 225 void echo_24();
rlm@0 226 void echo_25();
rlm@0 227 void echo_26();
rlm@0 228 void echo_27();
rlm@0 229 void echo_28();
rlm@0 230 void echo_29();
rlm@0 231 void echo_30();
rlm@0 232
rlm@0 233 void soft_reset_common();
rlm@0 234 };
rlm@0 235
rlm@0 236 #include <assert.h>
rlm@0 237
rlm@0 238 inline int SPC_DSP::sample_count() const { return m.out - m.out_begin; }
rlm@0 239
rlm@0 240 inline int SPC_DSP::read( int addr ) const
rlm@0 241 {
rlm@0 242 assert( (unsigned) addr < register_count );
rlm@0 243 return m.regs [addr];
rlm@0 244 }
rlm@0 245
rlm@0 246 inline void SPC_DSP::write( int addr, int data )
rlm@0 247 {
rlm@0 248 assert( (unsigned) addr < register_count );
rlm@0 249
rlm@0 250 m.regs [addr] = (uint8_t) data;
rlm@0 251 switch ( addr & 0x0F )
rlm@0 252 {
rlm@0 253 case v_envx:
rlm@0 254 m.envx_buf = (uint8_t) data;
rlm@0 255 break;
rlm@0 256
rlm@0 257 case v_outx:
rlm@0 258 m.outx_buf = (uint8_t) data;
rlm@0 259 break;
rlm@0 260
rlm@0 261 case 0x0C:
rlm@0 262 if ( addr == r_kon )
rlm@0 263 m.new_kon = (uint8_t) data;
rlm@0 264
rlm@0 265 if ( addr == r_endx ) // always cleared, regardless of data written
rlm@0 266 {
rlm@0 267 m.endx_buf = 0;
rlm@0 268 m.regs [r_endx] = 0;
rlm@0 269 }
rlm@0 270 break;
rlm@0 271 }
rlm@0 272 }
rlm@0 273
rlm@0 274 inline void SPC_DSP::mute_voices( int mask ) { m.mute_mask = mask; }
rlm@0 275
rlm@0 276 inline bool SPC_DSP::check_kon()
rlm@0 277 {
rlm@0 278 bool old = m.kon_check;
rlm@0 279 m.kon_check = 0;
rlm@0 280 return old;
rlm@0 281 }
rlm@0 282
rlm@0 283 #if !SPC_NO_COPY_STATE_FUNCS
rlm@0 284
rlm@0 285 class SPC_State_Copier {
rlm@0 286 SPC_DSP::copy_func_t func;
rlm@0 287 unsigned char** buf;
rlm@0 288 public:
rlm@0 289 SPC_State_Copier( unsigned char** p, SPC_DSP::copy_func_t f ) { func = f; buf = p; }
rlm@0 290 void copy( void* state, size_t size );
rlm@0 291 int copy_int( int state, int size );
rlm@0 292 void skip( int count );
rlm@0 293 void extra();
rlm@0 294 };
rlm@0 295
rlm@0 296 #define SPC_COPY( type, state )\
rlm@0 297 {\
rlm@0 298 state = (BOOST::type) copier.copy_int( state, sizeof (BOOST::type) );\
rlm@0 299 assert( (BOOST::type) state == state );\
rlm@0 300 }
rlm@0 301
rlm@0 302 #endif
rlm@0 303
rlm@0 304 #endif