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