libsidplayfp  2.2.2
EnvelopeGenerator.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2020 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2018 VICE Project
6  * Copyright 2007-2010 Antti Lankila
7  * Copyright 2004,2010 Dag Lem <resid@nimrod.no>
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 #ifndef ENVELOPEGENERATOR_H
25 #define ENVELOPEGENERATOR_H
26 
27 #include "siddefs-fp.h"
28 
29 namespace reSIDfp
30 {
31 
44 {
45 private:
50  enum State
51  {
52  ATTACK, DECAY_SUSTAIN, RELEASE
53  };
54 
55 private:
57  unsigned int lfsr;
58 
60  unsigned int rate;
61 
66  unsigned int exponential_counter;
67 
72  unsigned int exponential_counter_period;
73  unsigned int new_exponential_counter_period;
74 
75  unsigned int state_pipeline;
76 
78  unsigned int envelope_pipeline;
79 
80  unsigned int exponential_pipeline;
81 
83  State state;
84  State next_state;
85 
87  bool counter_enabled;
88 
90  bool gate;
91 
93  bool resetLfsr;
94 
96  unsigned char envelope_counter;
97 
99  unsigned char attack;
100 
102  unsigned char decay;
103 
105  unsigned char sustain;
106 
108  unsigned char release;
109 
111  unsigned char env3;
112 
118  float dac[256];
119 
120 private:
121  static const unsigned int adsrtable[16];
122 
123 private:
124  void set_exponential_counter();
125 
126  void state_change();
127 
128 public:
136  void setChipModel(ChipModel chipModel);
137 
141  void clock();
142 
148  float output() const { return dac[envelope_counter]; }
149 
154  lfsr(0x7fff),
155  rate(0),
156  exponential_counter(0),
157  exponential_counter_period(1),
158  new_exponential_counter_period(0),
159  state_pipeline(0),
160  envelope_pipeline(0),
161  exponential_pipeline(0),
162  state(RELEASE),
163  next_state(RELEASE),
164  counter_enabled(true),
165  gate(false),
166  resetLfsr(false),
167  envelope_counter(0xaa),
168  attack(0),
169  decay(0),
170  sustain(0),
171  release(0),
172  env3(0)
173  {}
174 
178  void reset();
179 
186  void writeCONTROL_REG(unsigned char control);
187 
194  void writeATTACK_DECAY(unsigned char attack_decay);
195 
202  void writeSUSTAIN_RELEASE(unsigned char sustain_release);
203 
209  unsigned char readENV() const { return env3; }
210 };
211 
212 } // namespace reSIDfp
213 
214 #if RESID_INLINING || defined(ENVELOPEGENERATOR_CPP)
215 
216 namespace reSIDfp
217 {
218 
219 RESID_INLINE
221 {
222  env3 = envelope_counter;
223 
224  if (unlikely(new_exponential_counter_period > 0))
225  {
226  exponential_counter_period = new_exponential_counter_period;
227  new_exponential_counter_period = 0;
228  }
229 
230  if (unlikely(state_pipeline))
231  {
232  state_change();
233  }
234 
235  if (unlikely(envelope_pipeline != 0) && (--envelope_pipeline == 0))
236  {
237  if (likely(counter_enabled))
238  {
239  if (state == ATTACK)
240  {
241  if (++envelope_counter==0xff)
242  {
243  next_state = DECAY_SUSTAIN;
244  state_pipeline = 3;
245  }
246  }
247  else if ((state == DECAY_SUSTAIN) || (state == RELEASE))
248  {
249  if (--envelope_counter==0x00)
250  {
251  counter_enabled = false;
252  }
253  }
254 
255  set_exponential_counter();
256  }
257  }
258  else if (unlikely(exponential_pipeline != 0) && (--exponential_pipeline == 0))
259  {
260  exponential_counter = 0;
261 
262  if (((state == DECAY_SUSTAIN) && (envelope_counter != sustain))
263  || (state == RELEASE))
264  {
265  // The envelope counter can flip from 0x00 to 0xff by changing state to
266  // attack, then to release. The envelope counter will then continue
267  // counting down in the release state.
268  // This has been verified by sampling ENV3.
269 
270  envelope_pipeline = 1;
271  }
272  }
273  else if (unlikely(resetLfsr))
274  {
275  lfsr = 0x7fff;
276  resetLfsr = false;
277 
278  if (state == ATTACK)
279  {
280  // The first envelope step in the attack state also resets the exponential
281  // counter. This has been verified by sampling ENV3.
282  exponential_counter = 0; // NOTE this is actually delayed one cycle, not modeled
283 
284  // The envelope counter can flip from 0xff to 0x00 by changing state to
285  // release, then to attack. The envelope counter is then frozen at
286  // zero; to unlock this situation the state must be changed to release,
287  // then to attack. This has been verified by sampling ENV3.
288 
289  envelope_pipeline = 2;
290  }
291  else
292  {
293  if (counter_enabled && (++exponential_counter == exponential_counter_period))
294  exponential_pipeline = exponential_counter_period != 1 ? 2 : 1;
295  }
296  }
297 
298  // ADSR delay bug.
299  // If the rate counter comparison value is set below the current value of the
300  // rate counter, the counter will continue counting up until it wraps around
301  // to zero at 2^15 = 0x8000, and then count rate_period - 1 before the
302  // envelope can constly be stepped.
303  // This has been verified by sampling ENV3.
304 
305  // check to see if LFSR matches table value
306  if (likely(lfsr != rate))
307  {
308  // it wasn't a match, clock the LFSR once
309  // by performing XOR on last 2 bits
310  const unsigned int feedback = ((lfsr << 14) ^ (lfsr << 13)) & 0x4000;
311  lfsr = (lfsr >> 1) | feedback;
312  }
313  else
314  {
315  resetLfsr = true;
316  }
317 }
318 
358 RESID_INLINE
359 void EnvelopeGenerator::state_change()
360 {
361  state_pipeline--;
362 
363  switch (next_state)
364  {
365  case ATTACK:
366  if (state_pipeline == 1)
367  {
368  // The decay rate is "accidentally" enabled during first cycle of attack phase
369  rate = adsrtable[decay];
370  }
371  else if (state_pipeline == 0)
372  {
373  state = ATTACK;
374  // The attack rate is correctly enabled during second cycle of attack phase
375  rate = adsrtable[attack];
376  counter_enabled = true;
377  }
378  break;
379  case DECAY_SUSTAIN:
380  if (state_pipeline == 0)
381  {
382  state = DECAY_SUSTAIN;
383  rate = adsrtable[decay];
384  }
385  break;
386  case RELEASE:
387  if (((state == ATTACK) && (state_pipeline == 0))
388  || ((state == DECAY_SUSTAIN) && (state_pipeline == 1)))
389  {
390  state = RELEASE;
391  rate = adsrtable[release];
392  }
393  break;
394  }
395 }
396 
397 RESID_INLINE
398 void EnvelopeGenerator::set_exponential_counter()
399 {
400  // Check for change of exponential counter period.
401  //
402  // For a detailed description see:
403  // http://ploguechipsounds.blogspot.it/2010/03/sid-6581r3-adsr-tables-up-close.html
404  switch (envelope_counter)
405  {
406  case 0xff:
407  case 0x00:
408  new_exponential_counter_period = 1;
409  break;
410 
411  case 0x5d:
412  new_exponential_counter_period = 2;
413  break;
414 
415  case 0x36:
416  new_exponential_counter_period = 4;
417  break;
418 
419  case 0x1a:
420  new_exponential_counter_period = 8;
421  break;
422 
423  case 0x0e:
424  new_exponential_counter_period = 16;
425  break;
426 
427  case 0x06:
428  new_exponential_counter_period = 30;
429  break;
430  }
431 }
432 
433 } // namespace reSIDfp
434 
435 #endif
436 
437 #endif
Definition: EnvelopeGenerator.h:44
void writeATTACK_DECAY(unsigned char attack_decay)
Definition: EnvelopeGenerator.cpp:137
void reset()
Definition: EnvelopeGenerator.cpp:77
void writeSUSTAIN_RELEASE(unsigned char sustain_release)
Definition: EnvelopeGenerator.cpp:152
float output() const
Definition: EnvelopeGenerator.h:148
void writeCONTROL_REG(unsigned char control)
Definition: EnvelopeGenerator.cpp:102
unsigned char readENV() const
Definition: EnvelopeGenerator.h:209
void setChipModel(ChipModel chipModel)
Definition: EnvelopeGenerator.cpp:66
void clock()
Definition: EnvelopeGenerator.h:220
EnvelopeGenerator()
Definition: EnvelopeGenerator.h:153