annotate fast_dsp/SPC_DSP.h @ 7:6a2c890c22db

fixed Makefile to not delete generated wav files
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 07:25:01 -0700
parents e38dacceb958
children
rev   line source
rlm@0 1 // Fast SNES SPC-700 DSP emulator (about 3x speed of accurate one)
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 class SPC_DSP {
rlm@0 10 public:
rlm@0 11 typedef BOOST::uint8_t uint8_t;
rlm@0 12
rlm@0 13 // Setup
rlm@0 14
rlm@0 15 // Initializes DSP and has it use the 64K RAM provided
rlm@0 16 void init( void* ram_64k );
rlm@0 17
rlm@0 18 // Sets destination for output samples. If out is NULL or out_size is 0,
rlm@0 19 // doesn't generate any.
rlm@0 20 typedef short sample_t;
rlm@0 21 void set_output( sample_t* out, int out_size );
rlm@0 22
rlm@0 23 // Number of samples written to output since it was last set, always
rlm@0 24 // a multiple of 2. Undefined if more samples were generated than
rlm@0 25 // output buffer could hold.
rlm@0 26 int sample_count() const;
rlm@0 27
rlm@0 28 // Emulation
rlm@0 29
rlm@0 30 // Resets DSP to power-on state
rlm@0 31 void reset();
rlm@0 32
rlm@0 33 // Emulates pressing reset switch on SNES
rlm@0 34 void soft_reset();
rlm@0 35
rlm@0 36 // Reads/writes DSP registers. For accuracy, you must first call spc_run_dsp()
rlm@0 37 // to catch the DSP up to present.
rlm@0 38 int read ( int addr ) const;
rlm@0 39 void write( int addr, int data );
rlm@0 40
rlm@0 41 // Runs DSP for specified number of clocks (~1024000 per second). Every 32 clocks
rlm@0 42 // a pair of samples is be generated.
rlm@0 43 void run( int clock_count );
rlm@0 44
rlm@0 45 // Sound control
rlm@0 46
rlm@0 47 // Mutes voices corresponding to non-zero bits in mask (overrides VxVOL with 0).
rlm@0 48 // Reduces emulation accuracy.
rlm@0 49 enum { voice_count = 8 };
rlm@0 50 void mute_voices( int mask );
rlm@0 51
rlm@0 52 // If true, prevents channels and global volumes from being phase-negated
rlm@0 53 void disable_surround( bool disable = true );
rlm@0 54
rlm@0 55 // State
rlm@0 56
rlm@0 57 // Resets DSP and uses supplied values to initialize registers
rlm@0 58 enum { register_count = 128 };
rlm@0 59 void load( uint8_t const regs [register_count] );
rlm@0 60
rlm@0 61 // DSP register addresses
rlm@0 62
rlm@0 63 // Global registers
rlm@0 64 enum {
rlm@0 65 r_mvoll = 0x0C, r_mvolr = 0x1C,
rlm@0 66 r_evoll = 0x2C, r_evolr = 0x3C,
rlm@0 67 r_kon = 0x4C, r_koff = 0x5C,
rlm@0 68 r_flg = 0x6C, r_endx = 0x7C,
rlm@0 69 r_efb = 0x0D, r_pmon = 0x2D,
rlm@0 70 r_non = 0x3D, r_eon = 0x4D,
rlm@0 71 r_dir = 0x5D, r_esa = 0x6D,
rlm@0 72 r_edl = 0x7D,
rlm@0 73 r_fir = 0x0F // 8 coefficients at 0x0F, 0x1F ... 0x7F
rlm@0 74 };
rlm@0 75
rlm@0 76 // Voice registers
rlm@0 77 enum {
rlm@0 78 v_voll = 0x00, v_volr = 0x01,
rlm@0 79 v_pitchl = 0x02, v_pitchh = 0x03,
rlm@0 80 v_srcn = 0x04, v_adsr0 = 0x05,
rlm@0 81 v_adsr1 = 0x06, v_gain = 0x07,
rlm@0 82 v_envx = 0x08, v_outx = 0x09
rlm@0 83 };
rlm@0 84
rlm@0 85 public:
rlm@0 86 enum { extra_size = 16 };
rlm@0 87 sample_t* extra() { return m.extra; }
rlm@0 88 sample_t const* out_pos() const { return m.out; }
rlm@0 89 public:
rlm@0 90 BLARGG_DISABLE_NOTHROW
rlm@0 91
rlm@0 92 typedef BOOST::int8_t int8_t;
rlm@0 93 typedef BOOST::int16_t int16_t;
rlm@0 94
rlm@0 95 enum { echo_hist_size = 8 };
rlm@0 96
rlm@0 97 enum env_mode_t { env_release, env_attack, env_decay, env_sustain };
rlm@0 98 enum { brr_buf_size = 12 };
rlm@0 99 struct voice_t
rlm@0 100 {
rlm@0 101 int buf [brr_buf_size*2];// decoded samples (twice the size to simplify wrap handling)
rlm@0 102 int* buf_pos; // place in buffer where next samples will be decoded
rlm@0 103 int interp_pos; // relative fractional position in sample (0x1000 = 1.0)
rlm@0 104 int brr_addr; // address of current BRR block
rlm@0 105 int brr_offset; // current decoding offset in BRR block
rlm@0 106 int kon_delay; // KON delay/current setup phase
rlm@0 107 env_mode_t env_mode;
rlm@0 108 int env; // current envelope level
rlm@0 109 int hidden_env; // used by GAIN mode 7, very obscure quirk
rlm@0 110 int volume [2]; // copy of volume from DSP registers, with surround disabled
rlm@0 111 int enabled; // -1 if enabled, 0 if muted
rlm@0 112 };
rlm@0 113 private:
rlm@0 114 struct state_t
rlm@0 115 {
rlm@0 116 uint8_t regs [register_count];
rlm@0 117
rlm@0 118 // Echo history keeps most recent 8 samples (twice the size to simplify wrap handling)
rlm@0 119 int echo_hist [echo_hist_size * 2] [2];
rlm@0 120 int (*echo_hist_pos) [2]; // &echo_hist [0 to 7]
rlm@0 121
rlm@0 122 int every_other_sample; // toggles every sample
rlm@0 123 int kon; // KON value when last checked
rlm@0 124 int noise;
rlm@0 125 int echo_offset; // offset from ESA in echo buffer
rlm@0 126 int echo_length; // number of bytes that echo_offset will stop at
rlm@0 127 int phase; // next clock cycle to run (0-31)
rlm@0 128 unsigned counters [4];
rlm@0 129
rlm@0 130 int new_kon;
rlm@0 131 int t_koff;
rlm@0 132
rlm@0 133 voice_t voices [voice_count];
rlm@0 134
rlm@0 135 unsigned* counter_select [32];
rlm@0 136
rlm@0 137 // non-emulation state
rlm@0 138 uint8_t* ram; // 64K shared RAM between DSP and SMP
rlm@0 139 int mute_mask;
rlm@0 140 int surround_threshold;
rlm@0 141 sample_t* out;
rlm@0 142 sample_t* out_end;
rlm@0 143 sample_t* out_begin;
rlm@0 144 sample_t extra [extra_size];
rlm@0 145 };
rlm@0 146 state_t m;
rlm@0 147
rlm@0 148 void init_counter();
rlm@0 149 void run_counter( int );
rlm@0 150 void soft_reset_common();
rlm@0 151 void write_outline( int addr, int data );
rlm@0 152 void update_voice_vol( int addr );
rlm@0 153 };
rlm@0 154
rlm@0 155 #include <assert.h>
rlm@0 156
rlm@0 157 inline int SPC_DSP::sample_count() const { return m.out - m.out_begin; }
rlm@0 158
rlm@0 159 inline int SPC_DSP::read( int addr ) const
rlm@0 160 {
rlm@0 161 assert( (unsigned) addr < register_count );
rlm@0 162 return m.regs [addr];
rlm@0 163 }
rlm@0 164
rlm@0 165 inline void SPC_DSP::update_voice_vol( int addr )
rlm@0 166 {
rlm@0 167 int l = (int8_t) m.regs [addr + v_voll];
rlm@0 168 int r = (int8_t) m.regs [addr + v_volr];
rlm@0 169
rlm@0 170 if ( l * r < m.surround_threshold )
rlm@0 171 {
rlm@0 172 // signs differ, so negate those that are negative
rlm@0 173 l ^= l >> 7;
rlm@0 174 r ^= r >> 7;
rlm@0 175 }
rlm@0 176
rlm@0 177 voice_t& v = m.voices [addr >> 4];
rlm@0 178 int enabled = v.enabled;
rlm@0 179 v.volume [0] = l & enabled;
rlm@0 180 v.volume [1] = r & enabled;
rlm@0 181 }
rlm@0 182
rlm@0 183 inline void SPC_DSP::write( int addr, int data )
rlm@0 184 {
rlm@0 185 assert( (unsigned) addr < register_count );
rlm@0 186
rlm@0 187 m.regs [addr] = (uint8_t) data;
rlm@0 188 int low = addr & 0x0F;
rlm@0 189 if ( low < 0x2 ) // voice volumes
rlm@0 190 {
rlm@0 191 update_voice_vol( low ^ addr );
rlm@0 192 }
rlm@0 193 else if ( low == 0xC )
rlm@0 194 {
rlm@0 195 if ( addr == r_kon )
rlm@0 196 m.new_kon = (uint8_t) data;
rlm@0 197
rlm@0 198 if ( addr == r_endx ) // always cleared, regardless of data written
rlm@0 199 m.regs [r_endx] = 0;
rlm@0 200 }
rlm@0 201 }
rlm@0 202
rlm@0 203 inline void SPC_DSP::disable_surround( bool disable )
rlm@0 204 {
rlm@0 205 m.surround_threshold = disable ? 0 : -0x4000;
rlm@0 206 }
rlm@0 207
rlm@0 208 #define SPC_NO_COPY_STATE_FUNCS 1
rlm@0 209
rlm@0 210 #define SPC_LESS_ACCURATE 1
rlm@0 211
rlm@0 212 #endif