EnvelopeGenerator.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 ENVELOPEGENERATOR_H
#define ENVELOPEGENERATOR_H
#include "siddefs-fp.h"
namespace reSIDfp
{
class EnvelopeGenerator
{
private:
enum State
{
ATTACK, DECAY_SUSTAIN, RELEASE
};
private:
int lfsr;
int rate;
int exponential_counter;
int exponential_counter_period;
int attack;
int decay;
int sustain;
int release;
State state;
bool hold_zero;
bool envelope_pipeline;
bool gate;
unsigned char envelope_counter;
short dac[256];
private:
static const int adsrtable[16];
private:
void set_exponential_counter();
public:
void setChipModel(ChipModel chipModel);
void clock();
short output() const { return dac[envelope_counter]; }
EnvelopeGenerator() :
lfsr(0),
rate(0),
exponential_counter(0),
exponential_counter_period(1),
attack(0),
decay(0),
sustain(0),
release(0),
state(RELEASE),
hold_zero(true),
envelope_pipeline(false),
gate(false),
envelope_counter(0) {}
void reset();
void writeCONTROL_REG(unsigned char control);
void writeATTACK_DECAY(unsigned char attack_decay);
void writeSUSTAIN_RELEASE(unsigned char sustain_release);
unsigned char readENV() const { return envelope_counter; }
};
} // namespace reSIDfp
#if RESID_INLINING || defined(ENVELOPEGENERATOR_CPP)
namespace reSIDfp
{
RESID_INLINE
void EnvelopeGenerator::clock()
{
if (unlikely(envelope_pipeline))
{
--envelope_counter;
envelope_pipeline = false;
// Check for change of exponential counter period.
set_exponential_counter();
}
// Check for ADSR delay bug.
// If the rate counter comparison value is set below the current value of the
// rate counter, the counter will continue counting up until it wraps around
// to zero at 2^15 = 0x8000, and then count rate_period - 1 before the
// envelope can constly be stepped.
// This has been verified by sampling ENV3.
//
// Envelope is now implemented like in the real machine with a shift register
// so the ADSR delay bug should be correcly modeled
// check to see if LFSR matches table value
if (likely(lfsr != rate))
{
// it wasn't a match, clock the LFSR once
// by performing XOR on last 2 bits
const int feedback = ((lfsr << 14) ^ (lfsr << 13)) & 0x4000;
lfsr = (lfsr >> 1) | feedback;
return;
}
// reset LFSR
lfsr = 0x7fff;
// The first envelope step in the attack state also resets the exponential
// counter. This has been verified by sampling ENV3.
//
if (state == ATTACK || ++exponential_counter == exponential_counter_period)
{
// likely (~50%)
exponential_counter = 0;
// Check whether the envelope counter is frozen at zero.
if (unlikely(hold_zero))
{
return;
}
switch (state)
{
case ATTACK:
// The envelope counter can flip from 0xff to 0x00 by changing state to
// release, then to attack. The envelope counter is then frozen at
// zero; to unlock this situation the state must be changed to release,
// then to attack. This has been verified by sampling ENV3.
//
++envelope_counter;
if (unlikely(envelope_counter == 0xff))
{
state = DECAY_SUSTAIN;
rate = adsrtable[decay];
}
break;
case DECAY_SUSTAIN:
// From the sustain levels it follows that both the low and high 4 bits
// of the envelope counter are compared to the 4-bit sustain value.
// This has been verified by sampling ENV3.
//
// For a detailed description see:
// http://ploguechipsounds.blogspot.it/2010/11/new-research-on-sid-adsr.html
if (likely(envelope_counter == (sustain << 4 | sustain)))
{
return;
}
// fall-through
case RELEASE:
// The envelope counter can flip from 0x00 to 0xff by changing state to
// attack, then to release. The envelope counter will then continue
// counting down in the release state.
// This has been verified by sampling ENV3.
// NB! The operation below requires two's complement integer.
//
if (unlikely(exponential_counter_period != 1))
{
// The decrement is delayed one cycle.
envelope_pipeline = true;
return;
}
--envelope_counter;
break;
}
// Check for change of exponential counter period.
set_exponential_counter();
}
}
} // namespace reSIDfp
#endif
#endif
reSIDfp::EnvelopeGenerator::clock
void clock()
Definition: EnvelopeGenerator.h:208
reSIDfp::EnvelopeGenerator::reset
void reset()
Definition: EnvelopeGenerator.cpp:120
reSIDfp
Definition: Dac.cpp:25
reSIDfp::EnvelopeGenerator::setChipModel
void setChipModel(ChipModel chipModel)
Definition: EnvelopeGenerator.cpp:99
reSIDfp::EnvelopeGenerator::writeCONTROL_REG
void writeCONTROL_REG(unsigned char control)
Definition: EnvelopeGenerator.cpp:141
reSIDfp::EnvelopeGenerator::EnvelopeGenerator
EnvelopeGenerator()
Definition: EnvelopeGenerator.h:148
reSIDfp::EnvelopeGenerator::readENV
unsigned char readENV() const
Definition: EnvelopeGenerator.h:197
reSIDfp::EnvelopeGenerator::output
short output() const
Definition: EnvelopeGenerator.h:143
reSIDfp::EnvelopeGenerator
Definition: EnvelopeGenerator.h:42
reSIDfp::EnvelopeGenerator::writeSUSTAIN_RELEASE
void writeSUSTAIN_RELEASE(unsigned char sustain_release)
Definition: EnvelopeGenerator.cpp:190
reSIDfp::EnvelopeGenerator::writeATTACK_DECAY
void writeATTACK_DECAY(unsigned char attack_decay)
Definition: EnvelopeGenerator.cpp:175