libresidfp 0.9.3
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
286 void enableOld6581caps(bool enable);
287};
288
289} // namespace reSIDfp
290
291#if RESIDFP_INLINING || defined(SID_CPP)
292
293#include <algorithm>
294
295#include "Filter.h"
296#include "resample/Resampler.h"
297
298namespace reSIDfp
299{
300
301RESIDFP_INLINE
302void SID::ageBusValue(unsigned int n)
303{
304 if (likely(busValueTtl != 0))
305 {
306 busValueTtl -= n;
307
308 if (unlikely(busValueTtl <= 0))
309 {
310 busValue = 0;
311 busValueTtl = 0;
312 }
313 }
314}
315
316RESIDFP_INLINE
317int SID::clock(unsigned int cycles, short* buf)
318{
319 ageBusValue(cycles);
320 int s = 0;
321
322 while (cycles != 0)
323 {
324 unsigned int delta_t = std::min(nextVoiceSync, cycles);
325
326 if (likely(delta_t > 0))
327 {
328 for (unsigned int i = 0; i < delta_t; i++)
329 {
330 // clock waveform generators
331 voice[0].wave()->clock();
332 voice[1].wave()->clock();
333 voice[2].wave()->clock();
334
335 // clock envelope generators
336 voice[0].envelope()->clock();
337 voice[1].envelope()->clock();
338 voice[2].envelope()->clock();
339
340 const int sidOutput = static_cast<int>(filter->clock(voice[0], voice[1], voice[2]));
341 const int c64Output = externalFilter.clock(sidOutput + INT16_MIN);
342 if (unlikely(resampler->input(c64Output)))
343 {
344 buf[s++] = resampler->getOutput(scaleFactor);
345 }
346 }
347
348 cycles -= delta_t;
349 nextVoiceSync -= delta_t;
350 }
351
352 if (unlikely(nextVoiceSync == 0))
353 {
354 voiceSync(true);
355 }
356 }
357
358 return s;
359}
360
361} // namespace reSIDfp
362
363#endif
364
365#endif
void clock()
Definition EnvelopeGenerator.h:177
Definition ExternalFilter.h:85
int clock(int input)
Definition ExternalFilter.h:132
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:317
void setChipModel(ChipModel model)
Definition SID.cpp:220
void input(int value)
Definition SID.cpp:330
unsigned char read(int offset)
Definition SID.cpp:336
void write(int offset, unsigned char value)
Definition SID.cpp:371
void setSamplingParameters(double clockFrequency, SamplingMethod method, double samplingFrequency)
Definition SID.cpp:490
void setFilter6581Range(double adjustment)
Definition SID.cpp:165
ChipModel getChipModel() const
Definition SID.h:149
void setCombinedWaveforms(CombinedWaveforms cws)
Definition SID.cpp:285
void setFilter6581Curve(double filterCurve)
Definition SID.cpp:160
void setFilter8580Curve(double filterCurve)
Definition SID.cpp:170
void enableOld6581caps(bool enable)
Definition SID.cpp:181
void enableFilter(bool enable)
Definition SID.cpp:175
void reset()
Definition SID.cpp:309
void clockSilent(unsigned int cycles)
Definition SID.cpp:509
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