libresidfp 0.9.1
SID.h
1/*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2011-2026 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#include <cstdint>
28
30#include "siddefs-fp.h"
31#include "ExternalFilter.h"
32#include "Voice.h"
33
34namespace reSIDfp
35{
36
37class Filter;
38class Filter6581;
39class Filter8580;
40class Resampler;
41
46{
47private:
48 const char* message;
49
50public:
51 SIDError(const char* msg) :
52 message(msg) {}
53 const char* getMessage() const { return message; }
54};
55
59class SID
60{
61private:
63 Filter* filter;
64
66 Filter6581* const filter6581;
67
69 Filter8580* const filter8580;
70
72 std::unique_ptr<Resampler> resampler;
73
78 ExternalFilter externalFilter;
79
81 Voice voice[3];
82
84 int scaleFactor;
85
87 int busValueTtl;
88
90 int modelTTL;
91
93 unsigned int nextVoiceSync;
94
96 ChipModel model;
97
100
102 unsigned char busValue;
103
109 float envDAC[256];
110
116 float oscDAC[4096];
117
118private:
124 void ageBusValue(unsigned int n);
125
132 void voiceSync(bool sync);
133
134public:
135 SID();
136 ~SID();
137
144 void setChipModel(ChipModel model);
145
149 ChipModel getChipModel() const { return model; }
150
158
162 void reset();
163
172 void input(int value);
173
194 unsigned char read(int offset);
195
202 void write(int offset, unsigned char value);
203
229 double clockFrequency,
230 SamplingMethod method,
231 double samplingFrequency
232 );
233
241 int clock(unsigned int cycles, short* buf);
242
253 void clockSilent(unsigned int cycles);
254
260 void setFilter6581Curve(double filterCurve);
261
267 void setFilter6581Range ( double adjustment );
268
274 void setFilter8580Curve(double filterCurve);
275
281 void enableFilter(bool enable);
282};
283
284} // namespace reSIDfp
285
286#if RESIDFP_INLINING || defined(SID_CPP)
287
288#include <algorithm>
289
290#include "Filter.h"
291#include "resample/Resampler.h"
292
293namespace reSIDfp
294{
295
296RESIDFP_INLINE
297void SID::ageBusValue(unsigned int n)
298{
299 if (likely(busValueTtl != 0))
300 {
301 busValueTtl -= n;
302
303 if (unlikely(busValueTtl <= 0))
304 {
305 busValue = 0;
306 busValueTtl = 0;
307 }
308 }
309}
310
311RESIDFP_INLINE
312int SID::clock(unsigned int cycles, short* buf)
313{
314 ageBusValue(cycles);
315 int s = 0;
316
317 while (cycles != 0)
318 {
319 unsigned int delta_t = std::min(nextVoiceSync, cycles);
320
321 if (likely(delta_t > 0))
322 {
323 for (unsigned int i = 0; i < delta_t; i++)
324 {
325 // clock waveform generators
326 voice[0].wave()->clock();
327 voice[1].wave()->clock();
328 voice[2].wave()->clock();
329
330 // clock envelope generators
331 voice[0].envelope()->clock();
332 voice[1].envelope()->clock();
333 voice[2].envelope()->clock();
334
335 const int sidOutput = static_cast<int>(filter->clock(voice[0], voice[1], voice[2]));
336 const int c64Output = externalFilter.clock(sidOutput + INT16_MIN);
337 if (unlikely(resampler->input(c64Output)))
338 {
339 buf[s++] = resampler->getOutput(scaleFactor);
340 }
341 }
342
343 cycles -= delta_t;
344 nextVoiceSync -= delta_t;
345 }
346
347 if (unlikely(nextVoiceSync == 0))
348 {
349 voiceSync(true);
350 }
351 }
352
353 return s;
354}
355
356} // namespace reSIDfp
357
358#endif
359
360#endif
void clock()
Definition EnvelopeGenerator.h:177
Definition ExternalFilter.h:66
int clock(int input)
Definition ExternalFilter.h:113
Definition Filter6581.h:321
Definition Filter.h:38
unsigned short clock(Voice &v1, Voice &v2, Voice &v3)
Definition Filter.h:214
Definition SID.h:46
Definition SID.h:60
int clock(unsigned int cycles, short *buf)
Definition SID.h:312
void setChipModel(ChipModel model)
Definition SID.cpp:215
void input(int value)
Definition SID.cpp:325
unsigned char read(int offset)
Definition SID.cpp:331
void write(int offset, unsigned char value)
Definition SID.cpp:366
void setSamplingParameters(double clockFrequency, SamplingMethod method, double samplingFrequency)
Definition SID.cpp:485
void setFilter6581Range(double adjustment)
Definition SID.cpp:165
ChipModel getChipModel() const
Definition SID.h:149
void setCombinedWaveforms(CombinedWaveforms cws)
Definition SID.cpp:280
void setFilter6581Curve(double filterCurve)
Definition SID.cpp:160
void setFilter8580Curve(double filterCurve)
Definition SID.cpp:170
void enableFilter(bool enable)
Definition SID.cpp:175
void reset()
Definition SID.cpp:304
void clockSilent(unsigned int cycles)
Definition SID.cpp:504
Definition Voice.h:37
void clock()
Definition WaveformGenerator.h:285
SamplingMethod
Definition residfp_defs.h:67
CombinedWaveforms
Definition residfp_defs.h:62
ChipModel
Definition residfp_defs.h:54