summaryrefslogtreecommitdiff
path: root/sid/sidplay-libs-2.1.0/resid/envelope.h
blob: d0b528ffa2a1f6fc6b391284158dd3aada406ec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//  ---------------------------------------------------------------------------
//  This file is part of reSID, a MOS6581 SID emulator engine.
//  Copyright (C) 2002  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//  ---------------------------------------------------------------------------

#ifndef __ENVELOPE_H__
#define __ENVELOPE_H__

#include "siddefs.h"

RESID_NAMESPACE_START

// ----------------------------------------------------------------------------
// A 16 bit counter is used to implement the envelope rates, in effect
// dividing the clock to the envelope counter by the currently selected rate
// period.
// In addition, another counter is used to implement the exponential envelope
// decay, in effect further dividing the clock to the envelope counter.
// The period of this counter is set to 1 in the attack state, and is
// successively set to 1, 2, 4, 8, 16, 30 at the envelope counter values
// 255, 93, 54, 26, 14, 6 in the decay and release states.
// ----------------------------------------------------------------------------
class EnvelopeGenerator
{
public:
  EnvelopeGenerator();

  RESID_INLINE void clock();
  RESID_INLINE void clock(cycle_count delta_t);
  void reset();

  void writeCONTROL_REG(reg8);
  void writeATTACK_DECAY(reg8);
  void writeSUSTAIN_RELEASE(reg8);
  reg8 readENV();

  // 8-bit envelope output.
  RESID_INLINE reg8 output();

protected:
  reg16 rate_counter;
  reg16 rate_period;
  reg16 exponential_counter;
  reg8 envelope_counter;
  bool hold_zero;

  reg4 attack;
  reg4 decay;
  reg4 sustain;
  reg4 release;

  reg8 gate;

  enum { ATTACK, DECAY_SUSTAIN, RELEASE } state;

  // Lookup table to convert from attack, decay, or release value to rate
  // counter period.
  static reg16 rate_counter_period[];

  // Lookup table to directly, from the envelope counter, find the current
  // exponential counter period.
  static reg8 exponential_counter_period[];

  // The 16 selectable sustain levels.
  static reg8 sustain_level[];

friend class SID;
};


// ----------------------------------------------------------------------------
// Inline functions.
// The following functions are defined inline because they are called every
// time a sample is calculated.
// ----------------------------------------------------------------------------

#if RESID_INLINING || defined(__ENVELOPE_CC__)

// ----------------------------------------------------------------------------
// SID clocking - 1 cycle.
// ----------------------------------------------------------------------------
RESID_INLINE
void EnvelopeGenerator::clock()
{
  // 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 to 2^15 = 0x8000,
  // and then count rate_period twice before the envelope can finally be
  // stepped. In this process one extra rate_counter step is taken.
  // This has been verified by sampling ENV3.
  // A possible explanation for this behavior is that the 16 bit rate counter
  // is compared with a 15 bit comparator for reset and with a 16 bit
  // comparator for envelope steps.
  //
  if ((++rate_counter & 0x7fff) != rate_period) {
    return;
  }

  if (rate_counter & 0x8000) {
    rate_counter = 1;
    return;
  }

  rate_counter = 0;

  // 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[envelope_counter])
  {
    exponential_counter = 0;

    // Check whether the envelope counter is frozen at zero.
    if (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 &= 0xff;
      if (envelope_counter == 0xff) {
	state = DECAY_SUSTAIN;
	rate_period = rate_counter_period[decay];
      }
      break;
    case DECAY_SUSTAIN:
      if (envelope_counter != sustain_level[sustain]) {
	--envelope_counter;
      }
      break;
    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.
      //
      --envelope_counter &= 0xff;
      break;
    }
    
    // When the envelope counter is changed to zero, it is frozen at zero.
    // This has been verified by sampling ENV3.
    //
    if (envelope_counter == 0) {
      hold_zero = true;
    }
  }
}


// ----------------------------------------------------------------------------
// SID clocking - delta_t cycles.
// ----------------------------------------------------------------------------
RESID_INLINE
void EnvelopeGenerator::clock(cycle_count delta_t)
{
  // 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 to 2^15 = 0x8000,
  // and then count rate_period twice before the envelope can finally be
  // stepped. In this process one extra rate_counter step is taken.
  // This has been verified by sampling ENV3.
  // A possible explanation for this behavior is that the 16 bit rate counter
  // is compared with a 15 bit comparator for reset and with a 16 bit
  // comparator for envelope steps.
  //
  reg16 rate_counter_15 = rate_counter & 0x7fff;

  // NB! This requires two's complement integer.
  int rate_step = rate_period - rate_counter_15;
  if (rate_step < 0) {
    rate_step += 0x8000;
  }

  for (; delta_t; rate_step = rate_period) {
    if (delta_t < rate_step) {
      rate_counter += delta_t;
      return;
    }

    if ((rate_counter + rate_step) & 0x8000) {
      rate_counter = 1;
      delta_t -= rate_step;
      rate_step = rate_period - 1;
      continue;
    }

    rate_counter = 0;
    delta_t -= rate_step;

    // 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[envelope_counter])
    {
      exponential_counter = 0;

      // Check whether the envelope counter is frozen at zero.
      if (hold_zero) {
	continue;
      }

      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 &= 0xff;
	if (envelope_counter == 0xff) {
	  state = DECAY_SUSTAIN;
	  rate_period = rate_counter_period[decay];
	  rate_step = rate_period;
	}
	break;
      case DECAY_SUSTAIN:
	if (envelope_counter != sustain_level[sustain]) {
	  --envelope_counter;
	}
	break;
      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.
	//
	--envelope_counter &= 0xff;
	break;
      }

      // When the envelope counter is changed to zero, it is frozen at zero.
      // This has been verified by sampling ENV3.
      //
      if (envelope_counter == 0) {
	hold_zero = true;
      }
    }
  }
}


// ----------------------------------------------------------------------------
// Read the envelope generator output.
// ----------------------------------------------------------------------------
RESID_INLINE
reg8 EnvelopeGenerator::output()
{
  return envelope_counter;
}

#endif // RESID_INLINING || defined(__ENVELOPE_CC__)

RESID_NAMESPACE_STOP

#endif // not __ENVELOPE_H__