| 1 |
/* |
| 2 |
* This file is part of libsidplayfp, a SID player engine. |
| 3 |
* |
| 4 |
* Copyright 2011-2016 Leandro Nini <[email protected]> |
| 5 |
* Copyright 2007-2010 Antti Lankila |
| 6 |
* Copyright 2004 Dag Lem <[email protected]> |
| 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 |
|
| 30 |
#include "sidcxx11.h" |
| 31 |
|
| 32 |
namespace reSIDfp |
| 33 |
{ |
| 34 |
|
| 35 |
class Filter; |
| 36 |
class Filter6581; |
| 37 |
class Filter8580; |
| 38 |
class ExternalFilter; |
| 39 |
class Potentiometer; |
| 40 |
class Voice; |
| 41 |
class Resampler; |
| 42 |
|
| 43 |
/** |
| 44 |
* SID error exception. |
| 45 |
*/ |
| 46 |
class SIDError |
| 47 |
{ |
| 48 |
private: |
| 49 |
const char* message; |
| 50 |
|
| 51 |
public: |
| 52 |
SIDError(const char* msg) : |
| 53 |
message(msg) {} |
| 54 |
const char* getMessage() const { return message; } |
| 55 |
}; |
| 56 |
|
| 57 |
/** |
| 58 |
* MOS6581/MOS8580 emulation. |
| 59 |
*/ |
| 60 |
class SID |
| 61 |
{ |
| 62 |
private: |
| 63 |
/// Currently active filter |
| 64 |
Filter* filter; |
| 65 |
|
| 66 |
/// Filter used, if model is set to 6581 |
| 67 |
std::unique_ptr<Filter6581> const filter6581; |
| 68 |
|
| 69 |
/// Filter used, if model is set to 8580 |
| 70 |
std::unique_ptr<Filter8580> const filter8580; |
| 71 |
|
| 72 |
/** |
| 73 |
* External filter that provides high-pass and low-pass filtering |
| 74 |
* to adjust sound tone slightly. |
| 75 |
*/ |
| 76 |
std::unique_ptr<ExternalFilter> const externalFilter; |
| 77 |
|
| 78 |
/// Resampler used by audio generation code. |
| 79 |
std::unique_ptr<Resampler> resampler; |
| 80 |
|
| 81 |
/// Paddle X register support |
| 82 |
std::unique_ptr<Potentiometer> const potX; |
| 83 |
|
| 84 |
/// Paddle Y register support |
| 85 |
std::unique_ptr<Potentiometer> const potY; |
| 86 |
|
| 87 |
/// SID voices |
| 88 |
std::unique_ptr<Voice> voice[3]; |
| 89 |
|
| 90 |
/// Time to live for the last written value |
| 91 |
int busValueTtl; |
| 92 |
|
| 93 |
/// Current chip model's bus value TTL |
| 94 |
int modelTTL; |
| 95 |
|
| 96 |
/// Time until #voiceSync must be run. |
| 97 |
unsigned int nextVoiceSync; |
| 98 |
|
| 99 |
/// Currently active chip model. |
| 100 |
ChipModel model; |
| 101 |
|
| 102 |
/// Last written value |
| 103 |
unsigned char busValue; |
| 104 |
|
| 105 |
/// Flags for muted channels |
| 106 |
bool muted[3]; |
| 107 |
|
| 108 |
/** |
| 109 |
* Emulated nonlinearity of the envelope DAC. |
| 110 |
* |
| 111 |
* @See Dac |
| 112 |
*/ |
| 113 |
float envDAC[256]; |
| 114 |
|
| 115 |
/** |
| 116 |
* Emulated nonlinearity of the oscillator DAC. |
| 117 |
* |
| 118 |
* @See Dac |
| 119 |
*/ |
| 120 |
float oscDAC[4096]; |
| 121 |
|
| 122 |
private: |
| 123 |
/** |
| 124 |
* Age the bus value and zero it if it's TTL has expired. |
| 125 |
* |
| 126 |
* @param n the number of cycles |
| 127 |
*/ |
| 128 |
void ageBusValue(unsigned int n); |
| 129 |
|
| 130 |
/** |
| 131 |
* Get output sample. |
| 132 |
* |
| 133 |
* @return the output sample |
| 134 |
*/ |
| 135 |
int output(); |
| 136 |
|
| 137 |
/** |
| 138 |
* Calculate the numebr of cycles according to current parameters |
| 139 |
* that it takes to reach sync. |
| 140 |
* |
| 141 |
* @param sync whether to do the actual voice synchronization |
| 142 |
*/ |
| 143 |
void voiceSync(bool sync); |
| 144 |
|
| 145 |
public: |
| 146 |
SID(); |
| 147 |
~SID(); |
| 148 |
|
| 149 |
int lastChanOut[3]; |
| 150 |
|
| 151 |
/** |
| 152 |
* Set chip model. |
| 153 |
* |
| 154 |
* @param model chip model to use |
| 155 |
* @throw SIDError |
| 156 |
*/ |
| 157 |
void setChipModel(ChipModel model); |
| 158 |
|
| 159 |
/** |
| 160 |
* Get currently emulated chip model. |
| 161 |
*/ |
| 162 |
ChipModel getChipModel() const { return model; } |
| 163 |
|
| 164 |
/** |
| 165 |
* SID reset. |
| 166 |
*/ |
| 167 |
void reset(); |
| 168 |
|
| 169 |
/** |
| 170 |
* 16-bit input (EXT IN). Write 16-bit sample to audio input. NB! The caller |
| 171 |
* is responsible for keeping the value within 16 bits. Note that to mix in |
| 172 |
* an external audio signal, the signal should be resampled to 1MHz first to |
| 173 |
* avoid sampling noise. |
| 174 |
* |
| 175 |
* @param value input level to set |
| 176 |
*/ |
| 177 |
void input(int value); |
| 178 |
|
| 179 |
/** |
| 180 |
* Read registers. |
| 181 |
* |
| 182 |
* Reading a write only register returns the last char written to any SID register. |
| 183 |
* The individual bits in this value start to fade down towards zero after a few cycles. |
| 184 |
* All bits reach zero within approximately $2000 - $4000 cycles. |
| 185 |
* It has been claimed that this fading happens in an orderly fashion, |
| 186 |
* however sampling of write only registers reveals that this is not the case. |
| 187 |
* NOTE: This is not correctly modeled. |
| 188 |
* The actual use of write only registers has largely been made |
| 189 |
* in the belief that all SID registers are readable. |
| 190 |
* To support this belief the read would have to be done immediately |
| 191 |
* after a write to the same register (remember that an intermediate write |
| 192 |
* to another register would yield that value instead). |
| 193 |
* With this in mind we return the last value written to any SID register |
| 194 |
* for $2000 cycles without modeling the bit fading. |
| 195 |
* |
| 196 |
* @param offset SID register to read |
| 197 |
* @return value read from chip |
| 198 |
*/ |
| 199 |
unsigned char read(int offset); |
| 200 |
|
| 201 |
/** |
| 202 |
* Write registers. |
| 203 |
* |
| 204 |
* @param offset chip register to write |
| 205 |
* @param value value to write |
| 206 |
*/ |
| 207 |
void write(int offset, unsigned char value); |
| 208 |
|
| 209 |
/** |
| 210 |
* SID voice muting. |
| 211 |
* |
| 212 |
* @param channel channel to modify |
| 213 |
* @param enable is muted? |
| 214 |
*/ |
| 215 |
void mute(int channel, bool enable) { muted[channel] = enable; } |
| 216 |
|
| 217 |
/** |
| 218 |
* Setting of SID sampling parameters. |
| 219 |
* |
| 220 |
* Use a clock freqency of 985248Hz for PAL C64, 1022730Hz for NTSC C64. |
| 221 |
* The default end of passband frequency is pass_freq = 0.9*sample_freq/2 |
| 222 |
* for sample frequencies up to ~ 44.1kHz, and 20kHz for higher sample frequencies. |
| 223 |
* |
| 224 |
* For resampling, the ratio between the clock frequency and the sample frequency |
| 225 |
* is limited as follows: 125*clock_freq/sample_freq < 16384 |
| 226 |
* E.g. provided a clock frequency of ~ 1MHz, the sample frequency can not be set |
| 227 |
* lower than ~ 8kHz. A lower sample frequency would make the resampling code |
| 228 |
* overfill its 16k sample ring buffer. |
| 229 |
* |
| 230 |
* The end of passband frequency is also limited: pass_freq <= 0.9*sample_freq/2 |
| 231 |
* |
| 232 |
* E.g. for a 44.1kHz sampling rate the end of passband frequency |
| 233 |
* is limited to slightly below 20kHz. |
| 234 |
* This constraint ensures that the FIR table is not overfilled. |
| 235 |
* |
| 236 |
* @param clockFrequency System clock frequency at Hz |
| 237 |
* @param method sampling method to use |
| 238 |
* @param samplingFrequency Desired output sampling rate |
| 239 |
* @param highestAccurateFrequency |
| 240 |
* @throw SIDError |
| 241 |
*/ |
| 242 |
void setSamplingParameters(double clockFrequency, SamplingMethod method, double samplingFrequency, double highestAccurateFrequency); |
| 243 |
|
| 244 |
/** |
| 245 |
* Clock SID forward using chosen output sampling algorithm. |
| 246 |
* |
| 247 |
* @param cycles c64 clocks to clock |
| 248 |
* @param buf audio output buffer |
| 249 |
* @return number of samples produced |
| 250 |
*/ |
| 251 |
int clock(unsigned int cycles, short* buf); |
| 252 |
|
| 253 |
/** |
| 254 |
* Clock SID forward with no audio production. |
| 255 |
* |
| 256 |
* _Warning_: |
| 257 |
* You can't mix this method of clocking with the audio-producing |
| 258 |
* clock() because components that don't affect OSC3/ENV3 are not |
| 259 |
* emulated. |
| 260 |
* |
| 261 |
* @param cycles c64 clocks to clock. |
| 262 |
*/ |
| 263 |
void clockSilent(unsigned int cycles); |
| 264 |
|
| 265 |
/** |
| 266 |
* Set filter curve parameter for 6581 model. |
| 267 |
* |
| 268 |
* @see Filter6581::setFilterCurve(double) |
| 269 |
*/ |
| 270 |
void setFilter6581Curve(double filterCurve); |
| 271 |
|
| 272 |
/** |
| 273 |
* Set filter curve parameter for 8580 model. |
| 274 |
* |
| 275 |
* @see Filter8580::setFilterCurve(double) |
| 276 |
*/ |
| 277 |
void setFilter8580Curve(double filterCurve); |
| 278 |
|
| 279 |
/** |
| 280 |
* Enable filter emulation. |
| 281 |
* |
| 282 |
* @param enable false to turn off filter emulation |
| 283 |
*/ |
| 284 |
void enableFilter(bool enable); |
| 285 |
}; |
| 286 |
|
| 287 |
} // namespace reSIDfp |
| 288 |
|
| 289 |
#if RESID_INLINING || defined(SID_CPP) |
| 290 |
|
| 291 |
#include <algorithm> |
| 292 |
|
| 293 |
#include "Filter.h" |
| 294 |
#include "ExternalFilter.h" |
| 295 |
#include "Voice.h" |
| 296 |
#include "resample/Resampler.h" |
| 297 |
|
| 298 |
namespace reSIDfp |
| 299 |
{ |
| 300 |
|
| 301 |
RESID_INLINE |
| 302 |
void 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 |
|
| 316 |
RESID_INLINE |
| 317 |
int SID::output() |
| 318 |
{ |
| 319 |
const int v1 = voice[0]->output(voice[2]->wave()); |
| 320 |
const int v2 = voice[1]->output(voice[0]->wave()); |
| 321 |
const int v3 = voice[2]->output(voice[1]->wave()); |
| 322 |
|
| 323 |
lastChanOut[0]=muted[0]?0:v1; |
| 324 |
lastChanOut[1]=muted[1]?0:v2; |
| 325 |
lastChanOut[2]=muted[2]?0:v3; |
| 326 |
|
| 327 |
return externalFilter->clock(filter->clock(muted[0]?0:v1, muted[1]?0:v2, muted[2]?0:v3)); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
RESID_INLINE |
| 332 |
int SID::clock(unsigned int cycles, short* buf) |
| 333 |
{ |
| 334 |
ageBusValue(cycles); |
| 335 |
int s = 0; |
| 336 |
|
| 337 |
while (cycles != 0) |
| 338 |
{ |
| 339 |
unsigned int delta_t = std::min(nextVoiceSync, cycles); |
| 340 |
|
| 341 |
if (likely(delta_t > 0)) |
| 342 |
{ |
| 343 |
for (unsigned int i = 0; i < delta_t; i++) |
| 344 |
{ |
| 345 |
// clock waveform generators |
| 346 |
voice[0]->wave()->clock(); |
| 347 |
voice[1]->wave()->clock(); |
| 348 |
voice[2]->wave()->clock(); |
| 349 |
|
| 350 |
// clock envelope generators |
| 351 |
voice[0]->envelope()->clock(); |
| 352 |
voice[1]->envelope()->clock(); |
| 353 |
voice[2]->envelope()->clock(); |
| 354 |
|
| 355 |
if (unlikely(resampler->input(output()))) |
| 356 |
{ |
| 357 |
buf[s++] = resampler->getOutput(); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
cycles -= delta_t; |
| 362 |
nextVoiceSync -= delta_t; |
| 363 |
} |
| 364 |
|
| 365 |
if (unlikely(nextVoiceSync == 0)) |
| 366 |
{ |
| 367 |
voiceSync(true); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
return s; |
| 372 |
} |
| 373 |
|
| 374 |
} // namespace reSIDfp |
| 375 |
|
| 376 |
#endif |
| 377 |
|
| 378 |
#endif |