LMMS
Loading...
Searching...
No Matches
sid.h
Go to the documentation of this file.
1
2
3// ---------------------------------------------------------------------------
4// This file is part of reSID, a MOS6581 SID emulator engine.
5// Copyright (C) 2010 Dag Lem <resid@nimrod.no>
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20// ---------------------------------------------------------------------------
21
22#ifndef RESID_SID_H
23#define RESID_SID_H
24
25#include "resid-config.h"
26#include "voice.h"
27#if NEW_8580_FILTER
28#include "filter8580new.h"
29#else
30#include "filter.h"
31#endif
32#include "extfilt.h"
33#include "pot.h"
34
35namespace reSID
36{
37
38class SID
39{
40public:
41 SID();
43
44 void set_chip_model(chip_model model);
45 void set_voice_mask(reg4 mask);
46 void enable_filter(bool enable);
47 void adjust_filter_bias(double dac_bias);
48 void enable_external_filter(bool enable);
49 bool set_sampling_parameters(double clock_freq, sampling_method method,
50 double sample_freq, double pass_freq = -1,
51 double filter_scale = 0.97);
52 void adjust_sampling_frequency(double sample_freq);
53 void enable_raw_debug_output(bool enable);
54
55 void clock();
56 void clock(cycle_count delta_t);
57 int clock(cycle_count& delta_t, short* buf, int n, int interleave = 1);
58 void reset();
59
60 // Read/write registers.
61 reg8 read(reg8 offset);
62 void write(reg8 offset, reg8 value);
63
64 // Read/write state.
65 class State
66 {
67 public:
69
70 char sid_register[0x20];
71
73 cycle_count bus_value_ttl;
74 cycle_count write_pipeline;
77
78 reg24 accumulator[3];
80 cycle_count shift_register_reset[3];
81 cycle_count shift_pipeline[3];
82 reg16 pulse_output[3];
83 cycle_count floating_output_ttl[3];
84
85 reg16 rate_counter[3];
91 bool hold_zero[3];
92 cycle_count envelope_pipeline[3];
93 };
94
96 void write_state(const State& state);
97
98 // 16-bit input (EXT IN).
99 void input(short sample);
100
101 // 16-bit output (AUDIO OUT).
102 int output();
103
104 void debugoutput(void);
105
106 protected:
107 static double I0(double x);
108 int clock_fast(cycle_count& delta_t, short* buf, int n, int interleave);
109 int clock_interpolate(cycle_count& delta_t, short* buf, int n, int interleave);
110 int clock_resample(cycle_count& delta_t, short* buf, int n, int interleave);
111 int clock_resample_fastmem(cycle_count& delta_t, short* buf, int n, int interleave);
112 void write();
113
114 chip_model sid_model;
120
122 cycle_count bus_value_ttl;
123
124 // The data bus TTL for the selected chip model
125 cycle_count databus_ttl;
126
127 // Pipeline for writes on the MOS8580.
128 cycle_count write_pipeline;
130
132
133 enum {
134 // Resampling constants.
135 // The error in interpolated lookup is bounded by 1.234/L^2,
136 // while the error in non-interpolated lookup is bounded by
137 // 0.7854/L + 0.4113/L^2, see
138 // http://www-ccrma.stanford.edu/~jos/resample/Choice_Table_Size.html
139 // For a resolution of 16 bits this yields L >= 285 and L >= 51473,
140 // respectively.
141 FIR_N = 125,
142 FIR_RES = 285,
145
146 RINGSIZE = 1 << 14,
148
149 // Fixed point constants (16.16 bits).
151 FIXP_MASK = 0xffff
152 };
153
154 // Sampling variables.
155 sampling_method sampling;
156 cycle_count cycles_per_sample;
157 cycle_count sample_offset;
160 int fir_N;
162 double fir_beta;
165
166 // Ring buffer with overflow for contiguous storage of RINGSIZE samples.
167 short* sample;
168
169 // FIR_RES filter tables (FIR_N*FIR_RES).
170 short* fir;
171
172 bool raw_debug_output; // FIXME: should be private?
173};
174
175
176// ----------------------------------------------------------------------------
177// Inline functions.
178// The following functions are defined inline because they are called every
179// time a sample is calculated.
180// ----------------------------------------------------------------------------
181
182#if RESID_INLINING || defined(RESID_SID_CC)
183
184// ----------------------------------------------------------------------------
185// Read 16-bit sample from audio output.
186// ----------------------------------------------------------------------------
187RESID_INLINE
188int SID::output()
189{
190 return extfilt.output();
191}
192
193
194// ----------------------------------------------------------------------------
195// SID clocking - 1 cycle.
196// ----------------------------------------------------------------------------
197RESID_INLINE
198void SID::clock()
199{
200 int i;
201
202 // Clock amplitude modulators.
203 for (i = 0; i < 3; i++) {
205 }
206
207 // Clock oscillators.
208 for (i = 0; i < 3; i++) {
209 voice[i].wave.clock();
210 }
211
212 // Synchronize oscillators.
213 for (i = 0; i < 3; i++) {
214 voice[i].wave.synchronize();
215 }
216
217 // Calculate waveform output.
218 for (i = 0; i < 3; i++) {
219 voice[i].wave.set_waveform_output();
220 }
221
222 // Clock filter.
223 filter.clock(voice[0].output(), voice[1].output(), voice[2].output());
224
225 // Clock external filter.
226 extfilt.clock(filter.output());
227
228 // Pipelined writes on the MOS8580.
230 write();
231 }
232
233 // Age bus value.
234 if (unlikely(!--bus_value_ttl)) {
235 bus_value = 0;
236 }
237
239 debugoutput();
240 }
241}
242
243#endif // RESID_INLINING || defined(RESID_SID_CC)
244
245} // namespace reSID
246
247#endif // not RESID_SID_H
#define unlikely(x)
Definition CarlaDefines.h:163
static const unsigned long mask[]
Definition bitwise.c:31
State
Definition envelope.h:42
Definition extfilt.h:56
Definition filter.h:347
Definition pot.h:29
Definition sid.h:66
cycle_count write_pipeline
Definition sid.h:74
EnvelopeGenerator::State envelope_state[3]
Definition sid.h:90
reg16 exponential_counter_period[3]
Definition sid.h:88
reg8 bus_value
Definition sid.h:72
char sid_register[0x20]
Definition sid.h:70
cycle_count shift_pipeline[3]
Definition sid.h:81
reg16 exponential_counter[3]
Definition sid.h:87
bool hold_zero[3]
Definition sid.h:91
reg16 rate_counter_period[3]
Definition sid.h:86
reg16 pulse_output[3]
Definition sid.h:82
cycle_count envelope_pipeline[3]
Definition sid.h:92
reg24 shift_register[3]
Definition sid.h:79
cycle_count shift_register_reset[3]
Definition sid.h:80
reg4 voice_mask
Definition sid.h:76
reg16 rate_counter[3]
Definition sid.h:85
reg24 accumulator[3]
Definition sid.h:78
reg8 write_address
Definition sid.h:75
cycle_count bus_value_ttl
Definition sid.h:73
reg8 envelope_counter[3]
Definition sid.h:89
cycle_count floating_output_ttl[3]
Definition sid.h:83
int clock_fast(cycle_count &delta_t, short *buf, int n, int interleave)
void adjust_filter_bias(double dac_bias)
cycle_count bus_value_ttl
Definition sid.h:122
void enable_filter(bool enable)
void write(reg8 offset, reg8 value)
Potentiometer potx
Definition sid.h:118
bool set_sampling_parameters(double clock_freq, sampling_method method, double sample_freq, double pass_freq=-1, double filter_scale=0.97)
double fir_filter_scale
Definition sid.h:164
Voice voice[3]
Definition sid.h:115
int output()
reg8 bus_value
Definition sid.h:121
void set_voice_mask(reg4 mask)
double fir_beta
Definition sid.h:162
void debugoutput(void)
Filter filter
Definition sid.h:116
void set_chip_model(chip_model model)
short * sample
Definition sid.h:167
State read_state()
double clock_frequency
Definition sid.h:131
sampling_method sampling
Definition sid.h:155
cycle_count cycles_per_sample
Definition sid.h:156
int clock_resample(cycle_count &delta_t, short *buf, int n, int interleave)
int clock_interpolate(cycle_count &delta_t, short *buf, int n, int interleave)
cycle_count databus_ttl
Definition sid.h:125
int clock_resample_fastmem(cycle_count &delta_t, short *buf, int n, int interleave)
void write()
void clock()
short sample_now
Definition sid.h:159
void enable_external_filter(bool enable)
void clock(cycle_count delta_t)
short * fir
Definition sid.h:170
int sample_index
Definition sid.h:158
@ FIR_RES
Definition sid.h:142
@ FIXP_SHIFT
Definition sid.h:150
@ FIR_N
Definition sid.h:141
@ RINGSIZE
Definition sid.h:146
@ FIR_RES_FASTMEM
Definition sid.h:143
@ FIR_SHIFT
Definition sid.h:144
@ RINGMASK
Definition sid.h:147
@ FIXP_MASK
Definition sid.h:151
reg8 read(reg8 offset)
reg8 write_address
Definition sid.h:129
static double I0(double x)
cycle_count write_pipeline
Definition sid.h:128
int fir_N
Definition sid.h:160
void adjust_sampling_frequency(double sample_freq)
chip_model sid_model
Definition sid.h:114
ExternalFilter extfilt
Definition sid.h:117
Potentiometer poty
Definition sid.h:119
void input(short sample)
double fir_f_cycles_per_sample
Definition sid.h:163
void reset()
int fir_RES
Definition sid.h:161
cycle_count sample_offset
Definition sid.h:157
bool raw_debug_output
Definition sid.h:172
void write_state(const State &state)
void enable_raw_debug_output(bool enable)
short sample_prev
Definition sid.h:159
int clock(cycle_count &delta_t, short *buf, int n, int interleave=1)
Definition voice.h:31
EnvelopeGenerator envelope
Definition voice.h:46
register unsigned i
Definition inflate.c:1575
static PuglViewHint int value
Definition pugl.h:1708
Definition dac.h:24
double x(PointIter p)
Definition spline.h:188
int n
Definition crypt.c:458
static ZCONST char Far * method[NUM_METHODS]
Definition zipinfo.c:1008