| 1 |
/* |
| 2 |
* This file is part of libsidplayfp, a SID player engine. |
| 3 |
* |
| 4 |
* Copyright 2011-2020 Leandro Nini <[email protected]> |
| 5 |
* Copyright 2018 VICE Project |
| 6 |
* Copyright 2007-2010 Antti Lankila |
| 7 |
* Copyright 2004,2010 Dag Lem <[email protected]> |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation; either version 2 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
#define ENVELOPEGENERATOR_CPP |
| 25 |
|
| 26 |
#include "EnvelopeGenerator.h" |
| 27 |
|
| 28 |
namespace reSIDfp |
| 29 |
{ |
| 30 |
|
| 31 |
/** |
| 32 |
* Lookup table to convert from attack, decay, or release value to rate |
| 33 |
* counter period. |
| 34 |
* |
| 35 |
* The rate counter is a 15 bit register which is left shifted each cycle. |
| 36 |
* When the counter reaches a specific comparison value, |
| 37 |
* the envelope counter is incremented (attack) or decremented |
| 38 |
* (decay/release) and the rate counter is resetted. |
| 39 |
* |
| 40 |
* see [kevtris.org](http://blog.kevtris.org/?p=13) |
| 41 |
*/ |
| 42 |
const unsigned int EnvelopeGenerator::adsrtable[16] = |
| 43 |
{ |
| 44 |
0x007f, |
| 45 |
0x3000, |
| 46 |
0x1e00, |
| 47 |
0x0660, |
| 48 |
0x0182, |
| 49 |
0x5573, |
| 50 |
0x000e, |
| 51 |
0x3805, |
| 52 |
0x2424, |
| 53 |
0x2220, |
| 54 |
0x090c, |
| 55 |
0x0ecd, |
| 56 |
0x010e, |
| 57 |
0x23f7, |
| 58 |
0x5237, |
| 59 |
0x64a8 |
| 60 |
}; |
| 61 |
|
| 62 |
void EnvelopeGenerator::reset() |
| 63 |
{ |
| 64 |
envelope_pipeline = 0; |
| 65 |
|
| 66 |
state_pipeline = 0; |
| 67 |
|
| 68 |
attack = 0; |
| 69 |
decay = 0; |
| 70 |
sustain = 0; |
| 71 |
release = 0; |
| 72 |
|
| 73 |
gate = false; |
| 74 |
|
| 75 |
resetLfsr = false; |
| 76 |
|
| 77 |
exponential_counter = 0; |
| 78 |
exponential_counter_period = 1; |
| 79 |
new_exponential_counter_period = 0; |
| 80 |
|
| 81 |
state = RELEASE; |
| 82 |
counter_enabled = true; |
| 83 |
rate = 0; |
| 84 |
|
| 85 |
envelope_counter = 0; |
| 86 |
env3 = 0; |
| 87 |
lfsr = 0x7fff; |
| 88 |
} |
| 89 |
|
| 90 |
void EnvelopeGenerator::writeCONTROL_REG(unsigned char control) |
| 91 |
{ |
| 92 |
const bool gate_next = (control & 0x01) != 0; |
| 93 |
|
| 94 |
if (gate_next != gate) |
| 95 |
{ |
| 96 |
gate = gate_next; |
| 97 |
|
| 98 |
// The rate counter is never reset, thus there will be a delay before the |
| 99 |
// envelope counter starts counting up (attack) or down (release). |
| 100 |
|
| 101 |
if (gate_next) |
| 102 |
{ |
| 103 |
// Gate bit on: Start attack, decay, sustain. |
| 104 |
next_state = ATTACK; |
| 105 |
state_pipeline = 2; |
| 106 |
|
| 107 |
if (resetLfsr || (exponential_pipeline == 2)) |
| 108 |
{ |
| 109 |
envelope_pipeline = (exponential_counter_period == 1) || (exponential_pipeline == 2) ? 2 : 4; |
| 110 |
} |
| 111 |
else if (exponential_pipeline == 1) |
| 112 |
{ |
| 113 |
state_pipeline = 3; |
| 114 |
} |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
// Gate bit off: Start release. |
| 119 |
next_state = RELEASE; |
| 120 |
state_pipeline = envelope_pipeline > 0 ? 3 : 2; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void EnvelopeGenerator::writeATTACK_DECAY(unsigned char attack_decay) |
| 126 |
{ |
| 127 |
attack = (attack_decay >> 4) & 0x0f; |
| 128 |
decay = attack_decay & 0x0f; |
| 129 |
|
| 130 |
if (state == ATTACK) |
| 131 |
{ |
| 132 |
rate = adsrtable[attack]; |
| 133 |
} |
| 134 |
else if (state == DECAY_SUSTAIN) |
| 135 |
{ |
| 136 |
rate = adsrtable[decay]; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void EnvelopeGenerator::writeSUSTAIN_RELEASE(unsigned char sustain_release) |
| 141 |
{ |
| 142 |
// From the sustain levels it follows that both the low and high 4 bits |
| 143 |
// of the envelope counter are compared to the 4-bit sustain value. |
| 144 |
// This has been verified by sampling ENV3. |
| 145 |
// |
| 146 |
// For a detailed description see: |
| 147 |
// http://ploguechipsounds.blogspot.it/2010/11/new-research-on-sid-adsr.html |
| 148 |
sustain = (sustain_release & 0xf0) | ((sustain_release >> 4) & 0x0f); |
| 149 |
|
| 150 |
release = sustain_release & 0x0f; |
| 151 |
|
| 152 |
if (state == RELEASE) |
| 153 |
{ |
| 154 |
rate = adsrtable[release]; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
} // namespace reSIDfp |