WaveformGenerator.h
/*
* This file is part of libsidplayfp, a SID player engine.
*
* Copyright 2011-2013 Leandro Nini <drfiemost@users.sourceforge.net>
* Copyright 2007-2010 Antti Lankila
* Copyright 2004,2010 Dag Lem <resid@nimrod.no>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef WAVEFORMGENERATOR_H
#define WAVEFORMGENERATOR_H
#include "siddefs-fp.h"
#include "array.h"
namespace reSIDfp
{
class WaveformGenerator
{
private:
matrix_t* model_wave;
short* wave;
// PWout = (PWn/40.95)%
int pw;
unsigned int shift_register;
int shift_register_reset;
int shift_pipeline;
int ring_msb_mask;
int no_noise;
int noise_output;
int no_noise_or_noise_output;
int no_pulse;
int pulse_output;
int waveform;
int floating_output_ttl;
int waveform_output;
int accumulator;
// Fout = (Fn*Fclk/16777216)Hz
int freq;
bool test;
bool sync;
bool msb_rising;
short dac[4096];
private:
void clock_shift_register();
void write_shift_register();
void reset_shift_register();
void set_noise_output();
public:
void setWaveformModels(matrix_t* models);
void setChipModel(ChipModel chipModel);
void clock();
void synchronize(WaveformGenerator* syncDest, const WaveformGenerator* syncSource) const;
WaveformGenerator() :
model_wave(0),
wave(0),
pw(0),
shift_register(0),
shift_register_reset(0),
shift_pipeline(0),
ring_msb_mask(0),
no_noise(0),
noise_output(0),
no_noise_or_noise_output(no_noise | noise_output),
no_pulse(0),
pulse_output(0),
waveform(0),
floating_output_ttl(0),
waveform_output(0),
accumulator(0),
freq(0),
test(false),
sync(false),
msb_rising(false) {}
void writeFREQ_LO(unsigned char freq_lo) { freq = (freq & 0xff00) | (freq_lo & 0xff); }
void writeFREQ_HI(unsigned char freq_hi) { freq = (freq_hi << 8 & 0xff00) | (freq & 0xff); }
void writePW_LO(unsigned char pw_lo) { pw = (pw & 0xf00) | (pw_lo & 0x0ff); }
void writePW_HI(unsigned char pw_hi) { pw = (pw_hi << 8 & 0xf00) | (pw & 0x0ff); }
void writeCONTROL_REG(unsigned char control);
void reset();
short output(const WaveformGenerator* ringModulator);
unsigned char readOSC() const { return (unsigned char)(waveform_output >> 4); }
int readAccumulator() const { return accumulator; }
int readFreq() const { return freq; }
bool readTest() const { return test; }
bool readSync() const { return sync; }
};
} // namespace reSIDfp
#if RESID_INLINING || defined(WAVEFORMGENERATOR_CPP)
namespace reSIDfp
{
RESID_INLINE
void WaveformGenerator::clock()
{
if (unlikely(test))
{
if (unlikely(shift_register_reset != 0) && unlikely(--shift_register_reset == 0))
{
reset_shift_register();
}
// The test bit sets pulse high.
pulse_output = 0xfff;
}
else
{
// Calculate new accumulator value;
const int accumulator_next = (accumulator + freq) & 0xffffff;
const int accumulator_bits_set = ~accumulator & accumulator_next;
accumulator = accumulator_next;
// Check whether the MSB is set high. This is used for synchronization.
msb_rising = (accumulator_bits_set & 0x800000) != 0;
// Shift noise register once for each time accumulator bit 19 is set high.
// The shift is delayed 2 cycles.
if (unlikely((accumulator_bits_set & 0x080000) != 0))
{
// Pipeline: Detect rising bit, shift phase 1, shift phase 2.
shift_pipeline = 2;
}
else if (unlikely(shift_pipeline != 0) && --shift_pipeline == 0)
{
clock_shift_register();
}
}
}
RESID_INLINE
short WaveformGenerator::output(const WaveformGenerator* ringModulator)
{
// Set output value.
if (likely(waveform != 0))
{
// The bit masks no_pulse and no_noise are used to achieve branch-free
// calculation of the output value.
const int ix = (accumulator ^ (ringModulator->accumulator & ring_msb_mask)) >> 12;
waveform_output = wave[ix] & (no_pulse | pulse_output) & no_noise_or_noise_output;
if (unlikely(waveform > 0x8))
{
// Combined waveforms that include noise
// write to the shift register.
write_shift_register();
}
}
else
{
// Age floating DAC input.
if (likely(floating_output_ttl != 0) && unlikely(--floating_output_ttl == 0))
{
waveform_output = 0;
}
}
// The pulse level is defined as (accumulator >> 12) >= pw ? 0xfff : 0x000.
// The expression -((accumulator >> 12) >= pw) & 0xfff yields the same
// results without any branching (and thus without any pipeline stalls).
// NB! This expression relies on that the result of a boolean expression
// is either 0 or 1, and furthermore requires two's complement integer.
// A few more cycles may be saved by storing the pulse width left shifted
// 12 bits, and dropping the and with 0xfff (this is valid since pulse is
// used as a bit mask on 12 bit values), yielding the expression
// -(accumulator >= pw24). However this only results in negligible savings.
// The result of the pulse width compare is delayed one cycle.
// Push next pulse level into pulse level pipeline.
pulse_output = ((accumulator >> 12) >= pw) ? 0xfff : 0x000;
// DAC imperfections are emulated by using waveform_output as an index
// into a DAC lookup table. readOSC() uses waveform_output directly.
return dac[waveform_output];
}
} // namespace reSIDfp
#endif
#endif
array
Definition: array.h:41
reSIDfp
Definition: Dac.cpp:25
reSIDfp::WaveformGenerator::readSync
bool readSync() const
Definition: WaveformGenerator.h:271
reSIDfp::WaveformGenerator::writeCONTROL_REG
void writeCONTROL_REG(unsigned char control)
Definition: WaveformGenerator.cpp:135
reSIDfp::WaveformGenerator::writePW_LO
void writePW_LO(unsigned char pw_lo)
Definition: WaveformGenerator.h:218
reSIDfp::WaveformGenerator::readFreq
int readFreq() const
Definition: WaveformGenerator.h:261
reSIDfp::WaveformGenerator::synchronize
void synchronize(WaveformGenerator *syncDest, const WaveformGenerator *syncSource) const
Definition: WaveformGenerator.cpp:124
reSIDfp::WaveformGenerator::writeFREQ_LO
void writeFREQ_LO(unsigned char freq_lo)
Definition: WaveformGenerator.h:201
reSIDfp::WaveformGenerator::writeFREQ_HI
void writeFREQ_HI(unsigned char freq_hi)
Definition: WaveformGenerator.h:208
reSIDfp::WaveformGenerator::readTest
bool readTest() const
Definition: WaveformGenerator.h:266
reSIDfp::WaveformGenerator::WaveformGenerator
WaveformGenerator()
Definition: WaveformGenerator.h:174
reSIDfp::WaveformGenerator::output
short output(const WaveformGenerator *ringModulator)
Definition: WaveformGenerator.h:319
reSIDfp::WaveformGenerator::clock
void clock()
Definition: WaveformGenerator.h:282
reSIDfp::WaveformGenerator::readAccumulator
int readAccumulator() const
Definition: WaveformGenerator.h:256
reSIDfp::WaveformGenerator::reset
void reset()
Definition: WaveformGenerator.cpp:202
reSIDfp::WaveformGenerator::setChipModel
void setChipModel(ChipModel chipModel)
Definition: WaveformGenerator.cpp:96
reSIDfp::WaveformGenerator::writePW_HI
void writePW_HI(unsigned char pw_hi)
Definition: WaveformGenerator.h:225
reSIDfp::WaveformGenerator
Definition: WaveformGenerator.h:86
reSIDfp::WaveformGenerator::readOSC
unsigned char readOSC() const
Definition: WaveformGenerator.h:251