libsidplayfp 2.13.1
SID.h
1/*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2011-2025 Leandro Nini <drfiemost@users.sourceforge.net>
5 * Copyright 2007-2010 Antti Lankila
6 * Copyright 2004 Dag Lem <resid@nimrod.no>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23#ifndef SIDFP_H
24#define SIDFP_H
25
26#include <memory>
27
28#include "siddefs-fp.h"
29#include "ExternalFilter.h"
30#include "Potentiometer.h"
31#include "Voice.h"
32
33#include "sidcxx11.h"
34
35namespace reSIDfp
36{
37
38class Filter;
39class Filter6581;
40class Filter8580;
41class Resampler;
42
46class SIDError
47{
48private:
49 const char* message;
50
51public:
52 SIDError(const char* msg) :
53 message(msg) {}
54 const char* getMessage() const { return message; }
55};
56
60class SID
61{
62private:
64 Filter* filter;
65
67 Filter6581* const filter6581;
68
70 Filter8580* const filter8580;
71
73 std::unique_ptr<Resampler> resampler;
74
79 ExternalFilter externalFilter;
80
82 Potentiometer potX;
83
85 Potentiometer potY;
86
88 Voice voice[3];
89
91 int scaleFactor;
92
94 int busValueTtl;
95
97 int modelTTL;
98
100 unsigned int nextVoiceSync;
101
103 ChipModel model;
104
106 CombinedWaveforms cws;
107
109 unsigned char busValue;
110
116 float envDAC[256];
117
123 float oscDAC[4096];
124
125private:
131 void ageBusValue(unsigned int n);
132
139 void voiceSync(bool sync);
140
141public:
142 SID();
143 ~SID();
144
151 void setChipModel(ChipModel model);
152
156 ChipModel getChipModel() const { return model; }
157
164 void setCombinedWaveforms(CombinedWaveforms cws);
165
169 void reset();
170
179 void input(int value);
180
201 unsigned char read(int offset);
202
209 void write(int offset, unsigned char value);
210
237 double clockFrequency,
238 SamplingMethod method,
239 double samplingFrequency
240 );
241
249 int clock(unsigned int cycles, short* buf);
250
261 void clockSilent(unsigned int cycles);
262
268 void setFilter6581Curve(double filterCurve);
269
275 void setFilter6581Range ( double adjustment );
276
282 void setFilter8580Curve(double filterCurve);
283
289 void enableFilter(bool enable);
290};
291
292} // namespace reSIDfp
293
294#if RESID_INLINING || defined(SID_CPP)
295
296#include <algorithm>
297
298#include "Filter.h"
299#include "ExternalFilter.h"
300#include "Voice.h"
301#include "resample/Resampler.h"
302
303namespace reSIDfp
304{
305
306RESID_INLINE
307void SID::ageBusValue(unsigned int n)
308{
309 if (likely(busValueTtl != 0))
310 {
311 busValueTtl -= n;
312
313 if (unlikely(busValueTtl <= 0))
314 {
315 busValue = 0;
316 busValueTtl = 0;
317 }
318 }
319}
320
321RESID_INLINE
322int SID::clock(unsigned int cycles, short* buf)
323{
324 ageBusValue(cycles);
325 int s = 0;
326
327 while (cycles != 0)
328 {
329 unsigned int delta_t = std::min(nextVoiceSync, cycles);
330
331 if (likely(delta_t > 0))
332 {
333 for (unsigned int i = 0; i < delta_t; i++)
334 {
335 // clock waveform generators
336 voice[0].wave()->clock();
337 voice[1].wave()->clock();
338 voice[2].wave()->clock();
339
340 // clock envelope generators
341 voice[0].envelope()->clock();
342 voice[1].envelope()->clock();
343 voice[2].envelope()->clock();
344
345 const int sidOutput = static_cast<int>(filter->clock(voice[0], voice[1], voice[2]));
346 const int c64Output = externalFilter.clock(sidOutput - (1 << 15));
347 if (unlikely(resampler->input(c64Output)))
348 {
349 buf[s++] = resampler->getOutput(scaleFactor);
350 }
351 }
352
353 cycles -= delta_t;
354 nextVoiceSync -= delta_t;
355 }
356
357 if (unlikely(nextVoiceSync == 0))
358 {
359 voiceSync(true);
360 }
361 }
362
363 return s;
364}
365
366} // namespace reSIDfp
367
368#endif
369
370#endif
Definition ExternalFilter.h:66
Definition Filter6581.h:321
Definition Filter.h:38
Definition Potentiometer.h:38
Definition Resampler.h:40
void setChipModel(ChipModel model)
Definition SID.cpp:211
void input(int value)
Definition SID.cpp:321
unsigned char read(int offset)
Definition SID.cpp:327
void write(int offset, unsigned char value)
Definition SID.cpp:362
void setSamplingParameters(double clockFrequency, SamplingMethod method, double samplingFrequency)
Definition SID.cpp:481
void setFilter6581Range(double adjustment)
Definition SID.cpp:161
ChipModel getChipModel() const
Definition SID.h:156
void setCombinedWaveforms(CombinedWaveforms cws)
Definition SID.cpp:276
void setFilter6581Curve(double filterCurve)
Definition SID.cpp:156
void setFilter8580Curve(double filterCurve)
Definition SID.cpp:166
void enableFilter(bool enable)
Definition SID.cpp:171
void reset()
Definition SID.cpp:300
int clock(unsigned int cycles, short *buf)
Definition SID.h:322
void clockSilent(unsigned int cycles)
Definition SID.cpp:500
Definition Voice.h:37