Debian Codegraph @

src/engine/platform/sound/sid2/envelope.h

raw
1 // ---------------------------------------------------------------------------
2 // This file is part of reSID, a MOS6581 SID2 emulator engine.
3 // Copyright (C) 2004 Dag Lem <[email protected]>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // ---------------------------------------------------------------------------
19 #include "siddefs.h"
20 #include <stdint.h>
21
22 // ----------------------------------------------------------------------------
23 // A 15 bit counter is used to implement the envelope rates, in effect
24 // dividing the clock to the envelope counter by the currently selected rate
25 // period.
26 // In addition, another counter is used to implement the exponential envelope
27 // decay, in effect further dividing the clock to the envelope counter.
28 // The period of this counter is set to 1, 2, 4, 8, 16, 30 at the envelope
29 // counter values 255, 93, 54, 26, 14, 6, respectively.
30 // ----------------------------------------------------------------------------
31 class EnvelopeGenerator2
32 {
33 public:
34 EnvelopeGenerator2();
35
36 enum State { ATTACK, DECAY_SUSTAIN, RELEASE };
37
38 RESID_INLINE void clock();
39 void reset();
40
41 void writeCONTROL_REG(reg8);
42 void writeATTACK_DECAY(reg8);
43 void writeSUSTAIN_RELEASE(reg8);
44 void writeVOL(reg8);
45 reg8 readENV();
46
47 // 8-bit envelope output.
48 RESID_INLINE reg8 output();
49
50 protected:
51 reg16 rate_counter;
52 reg16 rate_period;
53 reg8 exponential_counter;
54 reg8 exponential_counter_period;
55 reg8 envelope_counter;
56 bool hold_zero;
57
58 reg4 attack;
59 reg4 decay;
60 reg4 sustain;
61 reg4 release;
62
63 reg8 gate;
64
65 reg8 volume;
66
67 State state;
68
69 // Lookup table to convert from attack, decay, or release value to rate
70 // counter period.
71 static reg16 rate_counter_period[];
72
73 // The 16 selectable sustain levels.
74 static reg8 sustain_level[];
75
76 friend class SID2;
77 };
78
79
80 // ----------------------------------------------------------------------------
81 // Inline functions.
82 // The following functions are defined inline because they are called every
83 // time a sample is calculated.
84 // ----------------------------------------------------------------------------
85
86 #if RESID_INLINING || defined(__ENVELOPE_CC__)
87
88 // ----------------------------------------------------------------------------
89 // SID2 clocking - 1 cycle.
90 // ----------------------------------------------------------------------------
91 RESID_INLINE
92 void EnvelopeGenerator2::clock()
93 {
94 // Check for ADSR delay bug.
95 // If the rate counter comparison value is set below the current value of the
96 // rate counter, the counter will continue counting up until it wraps around
97 // to zero at 2^15 = 0x8000, and then count rate_period - 1 before the
98 // envelope can finally be stepped.
99 // This has been verified by sampling ENV3.
100 //
101 //if (++rate_counter & 0x8000) {
102 // ++rate_counter &= 0x7fff;
103 //}
104
105 if(rate_counter < rate_period)
106 {
107 rate_counter++;
108 }
109
110 if(rate_counter > rate_period)
111 {
112 rate_counter = rate_period; //so you can do alternating writes (e.g. writing attack 10-11-10-11-... results in the somewhat average envelope speed)
113 }
114
115 if (rate_counter != rate_period) {
116 return;
117 }
118
119 rate_counter = 0;
120
121 // The first envelope step in the attack state also resets the exponential
122 // counter. This has been verified by sampling ENV3.
123 //
124 if (state == ATTACK || ++exponential_counter == exponential_counter_period)
125 {
126 exponential_counter = 0;
127
128 // Check whether the envelope counter is frozen at zero.
129 if (hold_zero) {
130 return;
131 }
132
133 switch (state) {
134 case ATTACK:
135 // The envelope counter can flip from 0xff to 0x00 by changing state to
136 // release, then to attack. The envelope counter is then frozen at
137 // zero; to unlock this situation the state must be changed to release,
138 // then to attack. This has been verified by sampling ENV3.
139 //
140 ++envelope_counter &= 0xff;
141 if (envelope_counter == 0xff) {
142 state = DECAY_SUSTAIN;
143 rate_period = rate_counter_period[decay];
144 }
145 break;
146 case DECAY_SUSTAIN:
147 if (envelope_counter != sustain_level[sustain]) {
148 --envelope_counter;
149 }
150 break;
151 case RELEASE:
152 // The envelope counter can flip from 0x00 to 0xff by changing state to
153 // attack, then to release. The envelope counter will then continue
154 // counting down in the release state.
155 // This has been verified by sampling ENV3.
156 // NB! The operation below requires two's complement integer.
157 //
158 //--envelope_counter &= 0xff;
159 envelope_counter--;
160 break;
161 }
162
163 // Check for change of exponential counter period.
164 switch (envelope_counter) {
165 case 0xff:
166 exponential_counter_period = 1;
167 break;
168 case 0x5d:
169 exponential_counter_period = 2;
170 break;
171 case 0x36:
172 exponential_counter_period = 4;
173 break;
174 case 0x1a:
175 exponential_counter_period = 8;
176 break;
177 case 0x0e:
178 exponential_counter_period = 16;
179 break;
180 case 0x06:
181 exponential_counter_period = 30;
182 break;
183 case 0x00:
184 exponential_counter_period = 1;
185
186 // When the envelope counter is changed to zero, it is frozen at zero.
187 // This has been verified by sampling ENV3.
188 hold_zero = true;
189 break;
190 }
191 }
192 }
193
194 // ----------------------------------------------------------------------------
195 // Read the envelope generator output.
196 // ----------------------------------------------------------------------------
197 RESID_INLINE
198 reg8 EnvelopeGenerator2::output()
199 {
200 return (reg8)((uint32_t)envelope_counter * volume / 0xf);
201 }
202
203 #endif // RESID_INLINING || defined(__ENVELOPE_CC__)
204