20 #ifndef RESID_ENVELOPE_H
21 #define RESID_ENVELOPE_H
23 #include "resid-config.h"
42 enum State { ATTACK, DECAY_SUSTAIN, RELEASE, FREEZED };
44 void set_chip_model(chip_model model);
47 void clock(cycle_count delta_t);
50 void writeCONTROL_REG(reg8);
51 void writeATTACK_DECAY(reg8);
52 void writeSUSTAIN_RELEASE(reg8);
59 void set_exponential_counter();
65 reg8 exponential_counter;
66 reg8 exponential_counter_period;
67 reg8 new_exponential_counter_period;
68 reg8 envelope_counter;
71 cycle_count envelope_pipeline;
72 cycle_count exponential_pipeline;
73 cycle_count state_pipeline;
75 bool reset_rate_counter;
91 static reg16 rate_counter_period[];
94 static reg8 sustain_level[];
97 static unsigned short model_dac[2][1 << 8];
109 #if RESID_INLINING || defined(RESID_ENVELOPE_CC)
115 void EnvelopeGenerator::clock()
118 env3 = envelope_counter;
120 if (unlikely(state_pipeline)) {
126 if (unlikely(envelope_pipeline != 0) && (--envelope_pipeline == 0)) {
127 if (likely(!hold_zero)) {
128 if (state == ATTACK) {
129 ++envelope_counter &= 0xff;
130 if (unlikely(envelope_counter == 0xff)) {
131 state = DECAY_SUSTAIN;
132 rate_period = rate_counter_period[decay];
135 else if ((state == DECAY_SUSTAIN) || (state == RELEASE)) {
136 --envelope_counter &= 0xff;
139 set_exponential_counter();
143 if (unlikely(exponential_pipeline != 0) && (--exponential_pipeline == 0)) {
144 exponential_counter = 0;
146 if (((state == DECAY_SUSTAIN) && (envelope_counter != sustain_level[sustain]))
147 || (state == RELEASE)) {
153 envelope_pipeline = 1;
156 else if (unlikely(reset_rate_counter)) {
158 reset_rate_counter =
false;
160 if (state == ATTACK) {
163 exponential_counter = 0;
170 envelope_pipeline = 2;
173 if ((!hold_zero) && ++exponential_counter == exponential_counter_period) {
174 exponential_pipeline = exponential_counter_period != 1 ? 2 : 1;
186 if (likely(rate_counter != rate_period)) {
187 if (unlikely(++rate_counter & 0x8000)) {
188 ++rate_counter &= 0x7fff;
192 reset_rate_counter =
true;
200 void EnvelopeGenerator::clock(cycle_count delta_t)
205 if (unlikely(state_pipeline)) {
206 if (next_state == ATTACK) {
209 rate_period = rate_counter_period[attack];
210 }
else if (next_state == RELEASE) {
212 rate_period = rate_counter_period[release];
213 }
else if (next_state == FREEZED) {
228 int rate_step = rate_period - rate_counter;
229 if (unlikely(rate_step <= 0)) {
234 if (delta_t < rate_step) {
236 rate_counter += delta_t;
237 if (unlikely(rate_counter & 0x8000)) {
238 ++rate_counter &= 0x7fff;
244 delta_t -= rate_step;
249 if (state == ATTACK || ++exponential_counter == exponential_counter_period) {
251 exponential_counter = 0;
254 if (unlikely(hold_zero)) {
255 rate_step = rate_period;
266 ++envelope_counter &= 0xff;
267 if (unlikely(envelope_counter == 0xff)) {
268 state = DECAY_SUSTAIN;
269 rate_period = rate_counter_period[decay];
273 if (likely(envelope_counter != sustain_level[sustain])) {
284 --envelope_counter &= 0xff;
292 set_exponential_counter();
293 if (unlikely(new_exponential_counter_period > 0)) {
294 exponential_counter_period = new_exponential_counter_period;
295 new_exponential_counter_period = 0;
296 if (next_state == FREEZED) {
302 rate_step = rate_period;
350 switch (next_state) {
352 if (state_pipeline == 0) {
355 rate_period = rate_counter_period[attack];
362 if (((state == ATTACK) && (state_pipeline == 0))
363 || ((state == DECAY_SUSTAIN) && (state_pipeline == 1))) {
365 rate_period = rate_counter_period[release];
378 short EnvelopeGenerator::output()
382 return model_dac[sid_model][envelope_counter];
386 void EnvelopeGenerator::set_exponential_counter()
389 switch (envelope_counter) {
391 exponential_counter_period = 1;
394 exponential_counter_period = 2;
397 exponential_counter_period = 4;
400 exponential_counter_period = 8;
403 exponential_counter_period = 16;
406 exponential_counter_period = 30;
412 exponential_counter_period = 1;
421 #endif // RESID_INLINING || defined(RESID_ENVELOPE_CC)
425 #endif // not RESID_ENVELOPE_H