-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathenvelope.h
425 lines (370 loc) · 12.7 KB
/
envelope.h
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// ---------------------------------------------------------------------------
// This file is part of reSID, a MOS6581 SID emulator engine.
// Copyright (C) 2010 Dag Lem <[email protected]>
//
// 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 RESID_ENVELOPE_H
#define RESID_ENVELOPE_H
#include "resid-config.h"
namespace reSID
{
// ----------------------------------------------------------------------------
// A 15 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, 2, 4, 8, 16, 30 at the envelope
// counter values 255, 93, 54, 26, 14, 6, respectively.
// ----------------------------------------------------------------------------
class EnvelopeGenerator
{
public:
EnvelopeGenerator();
enum State { ATTACK, DECAY_SUSTAIN, RELEASE, FREEZED };
void set_chip_model(chip_model model);
void clock();
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.
short output();
protected:
void set_exponential_counter();
void state_change();
reg16 rate_counter;
reg16 rate_period;
reg8 exponential_counter;
reg8 exponential_counter_period;
reg8 new_exponential_counter_period;
reg8 envelope_counter;
reg8 env3;
// Emulation of pipeline delay for envelope decrement.
cycle_count envelope_pipeline;
cycle_count exponential_pipeline;
cycle_count state_pipeline;
bool hold_zero;
bool reset_rate_counter;
reg4 attack;
reg4 decay;
reg4 sustain;
reg4 release;
reg8 gate;
State state;
State next_state;
chip_model sid_model;
// Lookup table to convert from attack, decay, or release value to rate
// counter period.
static reg16 rate_counter_period[];
// The 16 selectable sustain levels.
static reg8 sustain_level[];
// DAC lookup tables.
static unsigned short model_dac[2][1 << 8];
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(RESID_ENVELOPE_CC)
// ----------------------------------------------------------------------------
// SID clocking - 1 cycle.
// ----------------------------------------------------------------------------
RESID_INLINE
void EnvelopeGenerator::clock()
{
// The ENV3 value is sampled at the first phase of the clock
env3 = envelope_counter;
if (unlikely(state_pipeline)) {
state_change();
}
// If the exponential counter period != 1, the envelope decrement is delayed
// 1 cycle. This is only modeled for single cycle clocking.
if (unlikely(envelope_pipeline != 0) && (--envelope_pipeline == 0)) {
if (likely(!hold_zero)) {
if (state == ATTACK) {
++envelope_counter &= 0xff;
if (unlikely(envelope_counter == 0xff)) {
state = DECAY_SUSTAIN;
rate_period = rate_counter_period[decay];
}
}
else if ((state == DECAY_SUSTAIN) || (state == RELEASE)) {
--envelope_counter &= 0xff;
}
set_exponential_counter();
}
}
if (unlikely(exponential_pipeline != 0) && (--exponential_pipeline == 0)) {
exponential_counter = 0;
if (((state == DECAY_SUSTAIN) && (envelope_counter != sustain_level[sustain]))
|| (state == 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.
envelope_pipeline = 1;
}
}
else if (unlikely(reset_rate_counter)) {
rate_counter = 0;
reset_rate_counter = false;
if (state == ATTACK) {
// The first envelope step in the attack state also resets the exponential
// counter. This has been verified by sampling ENV3.
exponential_counter = 0; // NOTE this is actually delayed one cycle, not modeled
// 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_pipeline = 2;
}
else {
if ((!hold_zero) && ++exponential_counter == exponential_counter_period) {
exponential_pipeline = exponential_counter_period != 1 ? 2 : 1;
}
}
}
// 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 finally be stepped.
// This has been verified by sampling ENV3.
//
if (likely(rate_counter != rate_period)) {
if (unlikely(++rate_counter & 0x8000)) {
++rate_counter &= 0x7fff;
}
}
else
reset_rate_counter = true;
}
// ----------------------------------------------------------------------------
// SID clocking - delta_t cycles.
// ----------------------------------------------------------------------------
RESID_INLINE
void EnvelopeGenerator::clock(cycle_count delta_t)
{
// NB! Any pipelined envelope counter decrement from single cycle clocking
// will be lost. It is not worth the trouble to flush the pipeline here.
if (unlikely(state_pipeline)) {
if (next_state == ATTACK) {
state = ATTACK;
hold_zero = false;
rate_period = rate_counter_period[attack];
} else if (next_state == RELEASE) {
state = RELEASE;
rate_period = rate_counter_period[release];
} else if (next_state == FREEZED) {
hold_zero = true;
}
state_pipeline = 0;
}
// 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 finally be stepped.
// This has been verified by sampling ENV3.
//
// NB! This requires two's complement integer.
int rate_step = rate_period - rate_counter;
if (unlikely(rate_step <= 0)) {
rate_step += 0x7fff;
}
while (delta_t) {
if (delta_t < rate_step) {
// likely (~65%)
rate_counter += delta_t;
if (unlikely(rate_counter & 0x8000)) {
++rate_counter &= 0x7fff;
}
return;
}
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) {
// likely (~50%)
exponential_counter = 0;
// Check whether the envelope counter is frozen at zero.
if (unlikely(hold_zero)) {
rate_step = rate_period;
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 (unlikely(envelope_counter == 0xff)) {
state = DECAY_SUSTAIN;
rate_period = rate_counter_period[decay];
}
break;
case DECAY_SUSTAIN:
if (likely(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;
case FREEZED:
// we should never get here
break;
}
// Check for change of exponential counter period.
set_exponential_counter();
if (unlikely(new_exponential_counter_period > 0)) {
exponential_counter_period = new_exponential_counter_period;
new_exponential_counter_period = 0;
if (next_state == FREEZED) {
hold_zero = true;
}
}
}
rate_step = rate_period;
}
}
/**
* This is what happens on chip during state switching,
* based on die reverse engineering and transistor level
* emulation.
*
* Attack
*
* 0 - Gate on
* 1 - Counting direction changes
* During this cycle the decay rate is "accidentally" activated
* 2 - Counter is being inverted
* Now the attack rate is correctly activated
* Counter is enabled
* 3 - Counter will be counting upward from now on
*
* Decay
*
* 0 - Counter == $ff
* 1 - Counting direction changes
* The attack state is still active
* 2 - Counter is being inverted
* During this cycle the decay state is activated
* 3 - Counter will be counting downward from now on
*
* Release
*
* 0 - Gate off
* 1 - During this cycle the release state is activated if coming from sustain/decay
* *2 - Counter is being inverted, the release state is activated
* *3 - Counter will be counting downward from now on
*
* (* only if coming directly from Attack state)
*
* Freeze
*
* 0 - Counter == $00
* 1 - Nothing
* 2 - Counter is disabled
*/
RESID_INLINE
void EnvelopeGenerator::state_change()
{
state_pipeline--;
switch (next_state) {
case ATTACK:
if (state_pipeline == 0) {
state = ATTACK;
// The attack register is correctly activated during second cycle of attack phase
rate_period = rate_counter_period[attack];
hold_zero = false;
}
break;
case DECAY_SUSTAIN:
break;
case RELEASE:
if (((state == ATTACK) && (state_pipeline == 0))
|| ((state == DECAY_SUSTAIN) && (state_pipeline == 1))) {
state = RELEASE;
rate_period = rate_counter_period[release];
}
break;
case FREEZED:
break;
}
}
// ----------------------------------------------------------------------------
// Read the envelope generator output.
// ----------------------------------------------------------------------------
RESID_INLINE
short EnvelopeGenerator::output()
{
// DAC imperfections are emulated by using envelope_counter as an index
// into a DAC lookup table. readENV() uses envelope_counter directly.
return model_dac[sid_model][envelope_counter];
}
RESID_INLINE
void EnvelopeGenerator::set_exponential_counter()
{
// Check for change of exponential counter period.
switch (envelope_counter) {
case 0xff:
exponential_counter_period = 1;
break;
case 0x5d:
exponential_counter_period = 2;
break;
case 0x36:
exponential_counter_period = 4;
break;
case 0x1a:
exponential_counter_period = 8;
break;
case 0x0e:
exponential_counter_period = 16;
break;
case 0x06:
exponential_counter_period = 30;
break;
case 0x00:
// TODO write a test to verify that 0x00 really changes the period
// e.g. set R = 0xf, gate on to 0x06, gate off to 0x00, gate on to 0x04,
// gate off, sample.
exponential_counter_period = 1;
// When the envelope counter is changed to zero, it is frozen at zero.
// This has been verified by sampling ENV3.
hold_zero = true;
break;
}
}
#endif // RESID_INLINING || defined(RESID_ENVELOPE_CC)
} // namespace reSID
#endif // not RESID_ENVELOPE_H