Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libsnes/bsnes/snes/dsp/dsp.hpp
2 views
1
struct DSP : public Processor {
2
enum : bool { Threaded = true };
3
alwaysinline void step(unsigned clocks);
4
alwaysinline void synchronize_smp();
5
6
uint8 read(uint8 addr);
7
void write(uint8 addr, uint8 data);
8
9
void enter();
10
void power();
11
void reset();
12
13
void serialize(serializer&);
14
DSP();
15
~DSP();
16
17
privileged:
18
//global registers
19
enum global_reg_t {
20
r_mvoll = 0x0c, r_mvolr = 0x1c,
21
r_evoll = 0x2c, r_evolr = 0x3c,
22
r_kon = 0x4c, r_koff = 0x5c,
23
r_flg = 0x6c, r_endx = 0x7c,
24
r_efb = 0x0d, r_pmon = 0x2d,
25
r_non = 0x3d, r_eon = 0x4d,
26
r_dir = 0x5d, r_esa = 0x6d,
27
r_edl = 0x7d, r_fir = 0x0f, //8 coefficients at 0x0f, 0x1f, ... 0x7f
28
};
29
30
//voice registers
31
enum voice_reg_t {
32
v_voll = 0x00, v_volr = 0x01,
33
v_pitchl = 0x02, v_pitchh = 0x03,
34
v_srcn = 0x04, v_adsr0 = 0x05,
35
v_adsr1 = 0x06, v_gain = 0x07,
36
v_envx = 0x08, v_outx = 0x09,
37
};
38
39
//internal envelope modes
40
enum env_mode_t { env_release, env_attack, env_decay, env_sustain };
41
42
//internal constants
43
enum { echo_hist_size = 8 };
44
enum { brr_buf_size = 12 };
45
enum { brr_block_size = 9 };
46
47
//global state
48
struct state_t {
49
uint8 regs[128];
50
51
modulo_array<int, echo_hist_size> echo_hist[2]; //echo history keeps most recent 8 samples
52
int echo_hist_pos;
53
54
bool every_other_sample; //toggles every sample
55
int kon; //KON value when last checked
56
int noise;
57
int counter;
58
int echo_offset; //offset from ESA in echo buffer
59
int echo_length; //number of bytes that echo_offset will stop at
60
61
//hidden registers also written to when main register is written to
62
int new_kon;
63
int endx_buf;
64
int envx_buf;
65
int outx_buf;
66
67
//temporary state between clocks
68
69
//read once per sample
70
int t_pmon;
71
int t_non;
72
int t_eon;
73
int t_dir;
74
int t_koff;
75
76
//read a few clocks ahead before used
77
int t_brr_next_addr;
78
int t_adsr0;
79
int t_brr_header;
80
int t_brr_byte;
81
int t_srcn;
82
int t_esa;
83
int t_echo_disabled;
84
85
//internal state that is recalculated every sample
86
int t_dir_addr;
87
int t_pitch;
88
int t_output;
89
int t_looped;
90
int t_echo_ptr;
91
92
//left/right sums
93
int t_main_out[2];
94
int t_echo_out[2];
95
int t_echo_in [2];
96
} state;
97
98
//voice state
99
struct voice_t {
100
modulo_array<int, brr_buf_size> buffer; //decoded samples
101
int buf_pos; //place in buffer where next samples will be decoded
102
int interp_pos; //relative fractional position in sample (0x1000 = 1.0)
103
int brr_addr; //address of current BRR block
104
int brr_offset; //current decoding offset in BRR block
105
int vbit; //bitmask for voice: 0x01 for voice 0, 0x02 for voice 1, etc
106
int vidx; //voice channel register index: 0x00 for voice 0, 0x10 for voice 1, etc
107
int kon_delay; //KON delay/current setup phase
108
int env_mode;
109
int env; //current envelope level
110
int t_envx_out;
111
int hidden_env; //used by GAIN mode 7, very obscure quirk
112
} voice[8];
113
114
//gaussian
115
static const int16 gaussian_table[512];
116
int gaussian_interpolate(const voice_t &v);
117
118
//counter
119
enum { counter_range = 2048 * 5 * 3 }; //30720 (0x7800)
120
static const uint16 counter_rate[32];
121
static const uint16 counter_offset[32];
122
void counter_tick();
123
bool counter_poll(unsigned rate);
124
125
//envelope
126
void envelope_run(voice_t &v);
127
128
//brr
129
void brr_decode(voice_t &v);
130
131
//misc
132
void misc_27();
133
void misc_28();
134
void misc_29();
135
void misc_30();
136
137
//voice
138
void voice_output(voice_t &v, bool channel);
139
void voice_1 (voice_t &v);
140
void voice_2 (voice_t &v);
141
void voice_3 (voice_t &v);
142
void voice_3a(voice_t &v);
143
void voice_3b(voice_t &v);
144
void voice_3c(voice_t &v);
145
void voice_4 (voice_t &v);
146
void voice_5 (voice_t &v);
147
void voice_6 (voice_t &v);
148
void voice_7 (voice_t &v);
149
void voice_8 (voice_t &v);
150
void voice_9 (voice_t &v);
151
152
//echo
153
int calc_fir(int i, bool channel);
154
int echo_output(bool channel);
155
void echo_read(bool channel);
156
void echo_write(bool channel);
157
void echo_22();
158
void echo_23();
159
void echo_24();
160
void echo_25();
161
void echo_26();
162
void echo_27();
163
void echo_28();
164
void echo_29();
165
void echo_30();
166
167
//dsp
168
static void Enter();
169
void tick();
170
};
171
172
extern DSP dsp;
173
174