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