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