libresidfp 1.3.0
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
28#include <cassert>
29#include <cstdint>
30
32#include "siddefs-fp.h"
33#include "ExternalFilter.h"
34#include "Filter.h"
35#include "Voice.h"
36#include "Limiter.h"
37#include "Noise.h"
38
39#include "State.h"
40
41namespace reSIDfp
42{
43
44class Filter;
45class Filter6581;
46class Filter8580;
47class Resampler;
48
53{
54private:
55 const char* message;
56
57public:
58 explicit SIDError(const char* msg) :
59 message(msg) {}
60 const char* getMessage() const { return message; }
61};
62
66class SID
67{
68 friend class State;
69
70private:
72 Filter* filter;
73
75 Filter6581* const filter6581;
76
78 Filter8580* const filter8580;
79
81 std::unique_ptr<Resampler> resampler;
82
87 ExternalFilter externalFilter;
88
90 Voice voice[3];
91
92 Noise m_noise;
93
95 int scaleFactor;
96
98 int busValueTtl;
99
101 int modelTTL;
102
104 unsigned int nextVoiceSync;
105
106 unsigned int offset_6581;
107
109 ChipModel model;
110
113
114 // Dac leakage
115 double dacLeakage;
116
118 uint8_t busValue;
119
121
122 uint8_t paddleX = 0xff;
123 uint8_t paddleY = 0xff;
125
131 float envDAC[256];
132
138 float oscDAC[4096];
139
141 Params* p;
142
143private:
149 void ageBusValue(int n);
150
157 void voiceSync(bool sync);
158
160 inline void clockWaveGen()
161 {
162 voice[0].wave()->clock();
163 voice[1].wave()->clock();
164 voice[2].wave()->clock();
165 }
166
168 inline void clockEnvGen()
169 {
170 voice[0].envelope()->clock();
171 voice[1].envelope()->clock();
172 voice[2].envelope()->clock();
173 }
174
176 inline int32_t clockFilt()
177 {
178 uint16_t filtOutput = filter->clock(voice[0], voice[1], voice[2]);
179 int32_t exFiltInput = static_cast<int32_t>(filtOutput) + INT16_MIN;
180 return externalFilter.clock(exFiltInput);
181 }
182
184 inline int16_t getOutput()
185 {
186 const int32_t out = (scaleFactor * resampler->output()) / 2;
187 return Limiter::softClip(out + m_noise.output());
188 }
189
190private:
191 SID(const SID&) = delete;
192 SID& operator=(const SID&) = delete;
193
194public:
195 SID();
196 ~SID();
197
204 void setChipModel(ChipModel model);
205
209 ChipModel getChipModel() const { return model; }
210
218
222 void reset();
223
232 void input(int value);
233
240 uint8_t peek(int offset) const;
241
262 uint8_t read(int offset);
263
270 void write(int offset, uint8_t value);
271
297 double clockFrequency,
298 SamplingMethod method,
299 double samplingFrequency
300 );
301
309 int clock(unsigned int cycles, int16_t* buf);
310
318 int clock(int16_t* buf, int bufSize);
319
327 void clockDigital(unsigned int cycles);
328
339 void clockSilent(unsigned int cycles);
340
346 void setFilter6581Curve(double filterCurve);
347
353 void setFilter6581Range(double adjustment);
354
360 void setFilter8580Curve(double filterCurve);
361
367 void enableFilter(bool enable);
368
372 void setPaddle(uint8_t x, uint8_t y);
373
374 /*
375 * Set the DAC leakage level.
376 */
377 void setDacLeakage(double level);
378
379 /*
380 * Set the 6581 wave offset.
381 */
382 void setOffset6581(double offset);
383
384 /*
385 * Set the DC-Blocker resistance.
386 */
387 void setDCBRes(double res);
388
392 void set6581caps(CapsType type);
393
397 void enableStrongPS(bool enable);
398
399 /*
400 * Set the background noise level.
401 */
402 void setNoiseLevel(int level);
403};
404
405} // namespace reSIDfp
406
407#if RESIDFP_INLINING || defined(SID_CPP)
408
409#include <algorithm>
410
411#include "resample/Resampler.h"
412
413namespace reSIDfp
414{
415
416RESIDFP_INLINE
417void SID::ageBusValue(int n)
418{
419 if (likely(busValueTtl != 0))
420 {
421 busValueTtl -= n;
422
423 if (unlikely(busValueTtl <= 0))
424 {
425 busValue = 0;
426 busValueTtl = 0;
427 }
428 }
429}
430
431RESIDFP_INLINE
432int SID::clock(unsigned int cycles, int16_t* buf)
433{
434 assert(buf);
435
436 ageBusValue(cycles);
437 int s = 0;
438
439 while (cycles != 0)
440 {
441 unsigned int delta_t = std::min(nextVoiceSync, cycles);
442
443 if (likely(delta_t > 0))
444 {
445 for (unsigned int i = 0; i < delta_t; i++)
446 {
447 clockWaveGen();
448 clockEnvGen();
449
450 int32_t output = clockFilt();
451 if (unlikely(resampler->input(output)))
452 {
453 buf[s++] = getOutput();
454 }
455 }
456
457 cycles -= delta_t;
458 nextVoiceSync -= delta_t;
459 }
460
461 if (unlikely(nextVoiceSync == 0))
462 {
463 voiceSync(true);
464 }
465 }
466
467 return s;
468}
469
470RESIDFP_INLINE
471int SID::clock(int16_t* buf, int bufSize)
472{
473 assert(buf);
474 assert(bufSize > 0);
475
476 int cycles = 0;
477
478 for (int s = 0; s < bufSize;)
479 {
480 unsigned int delta_t = nextVoiceSync;
481
482 if (likely(delta_t > 0))
483 {
484 unsigned int i = 0;
485 do
486 {
487 i++;
488 clockWaveGen();
489 clockEnvGen();
490
491 int32_t output = clockFilt();
492 if (unlikely(resampler->input(output)))
493 {
494 buf[s++] = getOutput();
495 if (unlikely(s == bufSize))
496 {
497 break;
498 }
499 }
500 }
501 while (i < delta_t);
502
503 cycles += i;
504 nextVoiceSync -= i;
505 }
506
507 if (likely(nextVoiceSync == 0))
508 {
509 voiceSync(true);
510 }
511 }
512
513 ageBusValue(cycles);
514
515 return cycles;
516}
517
518} // namespace reSIDfp
519
520#endif
521
522#endif
void clock()
Definition EnvelopeGenerator.h:181
Definition ExternalFilter.h:87
int32_t clock(int32_t input)
Definition ExternalFilter.h:150
Definition Filter6581.h:321
Definition Filter.h:39
uint16_t clock(Voice &voice1, Voice &voice2, Voice &voice3)
Definition Filter.h:216
Definition Noise.h:28
Definition SID.h:53
Definition SID.h:67
void setChipModel(ChipModel model)
Definition SID.cpp:234
uint8_t peek(int offset) const
Definition SID.cpp:351
void input(int value)
Definition SID.cpp:345
int clock(unsigned int cycles, int16_t *buf)
Definition SID.h:432
void setSamplingParameters(double clockFrequency, SamplingMethod method, double samplingFrequency)
Definition SID.cpp:526
void setFilter6581Range(double adjustment)
Definition SID.cpp:182
void setPaddle(uint8_t x, uint8_t y)
Definition SID.cpp:617
ChipModel getChipModel() const
Definition SID.h:209
void clockDigital(unsigned int cycles)
Definition SID.cpp:552
uint8_t read(int offset)
Definition SID.cpp:372
void setCombinedWaveforms(CombinedWaveforms cws)
Definition SID.cpp:300
void setFilter6581Curve(double filterCurve)
Definition SID.cpp:176
void setFilter8580Curve(double filterCurve)
Definition SID.cpp:188
void enableFilter(bool enable)
Definition SID.cpp:194
void reset()
Definition SID.cpp:324
void enableStrongPS(bool enable)
Definition SID.cpp:647
void write(int offset, uint8_t value)
Definition SID.cpp:407
void set6581caps(CapsType type)
Definition SID.cpp:641
void clockSilent(unsigned int cycles)
Definition SID.cpp:583
Definition State.h:46
Definition Voice.h:39
void clock()
Definition WaveformGenerator.h:320
SamplingMethod
Definition residfp_defs.h:78
CombinedWaveforms
Definition residfp_defs.h:73
ChipModel
Definition residfp_defs.h:65
CapsType
Definition residfp_defs.h:87
Definition State.h:36