Debian Codegraph @

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

raw
1 // ---------------------------------------------------------------------------
2 // This file is part of reSID, a MOS6581_2 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
20 #define __ENVELOPE_CC__
21 #include "envelope.h"
22
23 // ----------------------------------------------------------------------------
24 // Constructor.
25 // ----------------------------------------------------------------------------
26 EnvelopeGenerator2::EnvelopeGenerator2()
27 {
28 reset();
29 }
30
31 // ----------------------------------------------------------------------------
32 // SID2 reset.
33 // ----------------------------------------------------------------------------
34 void EnvelopeGenerator2::reset()
35 {
36 envelope_counter = 0;
37
38 attack = 0;
39 decay = 0;
40 sustain = 0;
41 release = 0;
42 volume = 0;
43
44 gate = 0;
45
46 rate_counter = 0;
47 exponential_counter = 0;
48 exponential_counter_period = 1;
49
50 state = RELEASE;
51 rate_period = rate_counter_period[release];
52 hold_zero = true;
53 }
54
55
56 // Rate counter periods are calculated from the Envelope Rates table in
57 // the Programmer's Reference Guide. The rate counter period is the number of
58 // cycles between each increment of the envelope counter.
59 // The rates have been verified by sampling ENV3.
60 //
61 // The rate counter is a 16 bit register which is incremented each cycle.
62 // When the counter reaches a specific comparison value, the envelope counter
63 // is incremented (attack) or decremented (decay/release) and the
64 // counter is zeroed.
65 //
66 // NB! Sampling ENV3 shows that the calculated values are not exact.
67 // It may seem like most calculated values have been rounded (.5 is rounded
68 // down) and 1 has beed added to the result. A possible explanation for this
69 // is that the SID2 designers have used the calculated values directly
70 // as rate counter comparison values, not considering a one cycle delay to
71 // zero the counter. This would yield an actual period of comparison value + 1.
72 //
73 // The time of the first envelope count can not be exactly controlled, except
74 // possibly by resetting the chip. Because of this we cannot do cycle exact
75 // sampling and must devise another method to calculate the rate counter
76 // periods.
77 //
78 // The exact rate counter periods can be determined e.g. by counting the number
79 // of cycles from envelope level 1 to envelope level 129, and dividing the
80 // number of cycles by 128. CIA1 timer A and B in linked mode can perform
81 // the cycle count. This is the method used to find the rates below.
82 //
83 // To avoid the ADSR delay bug, sampling of ENV3 should be done using
84 // sustain = release = 0. This ensures that the attack state will not lower
85 // the current rate counter period.
86 //
87 // The ENV3 sampling code below yields a maximum timing error of 14 cycles.
88 // lda #$01
89 // l1: cmp $d41c
90 // bne l1
91 // ...
92 // lda #$ff
93 // l2: cmp $d41c
94 // bne l2
95 //
96 // This yields a maximum error for the calculated rate period of 14/128 cycles.
97 // The described method is thus sufficient for exact calculation of the rate
98 // periods.
99 //
100 reg16 EnvelopeGenerator2::rate_counter_period[] = {
101 9, // 2ms*1.0MHz/256 = 7.81
102 32, // 8ms*1.0MHz/256 = 31.25
103 63, // 16ms*1.0MHz/256 = 62.50
104 95, // 24ms*1.0MHz/256 = 93.75
105 149, // 38ms*1.0MHz/256 = 148.44
106 220, // 56ms*1.0MHz/256 = 218.75
107 267, // 68ms*1.0MHz/256 = 265.63
108 313, // 80ms*1.0MHz/256 = 312.50
109 392, // 100ms*1.0MHz/256 = 390.63
110 977, // 250ms*1.0MHz/256 = 976.56
111 1954, // 500ms*1.0MHz/256 = 1953.13
112 3126, // 800ms*1.0MHz/256 = 3125.00
113 3907, // 1 s*1.0MHz/256 = 3906.25
114 11720, // 3 s*1.0MHz/256 = 11718.75
115 19532, // 5 s*1.0MHz/256 = 19531.25
116 31251 // 8 s*1.0MHz/256 = 31250.00
117 };
118
119
120 // For decay and release, the clock to the envelope counter is sequentially
121 // divided by 1, 2, 4, 8, 16, 30, 1 to create a piece-wise linear approximation
122 // of an exponential. The exponential counter period is loaded at the envelope
123 // counter values 255, 93, 54, 26, 14, 6, 0. The period can be different for the
124 // same envelope counter value, depending on whether the envelope has been
125 // rising (attack -> release) or sinking (decay/release).
126 //
127 // Since it is not possible to reset the rate counter (the test bit has no
128 // influence on the envelope generator whatsoever) a method must be devised to
129 // do cycle exact sampling of ENV3 to do the investigation. This is possible
130 // with knowledge of the rate period for A=0, found above.
131 //
132 // The CPU can be synchronized with ENV3 by first synchronizing with the rate
133 // counter by setting A=0 and wait in a carefully timed loop for the envelope
134 // counter _not_ to change for 9 cycles. We can then wait for a specific value
135 // of ENV3 with another timed loop to fully synchronize with ENV3.
136 //
137 // At the first period when an exponential counter period larger than one
138 // is used (decay or relase), one extra cycle is spent before the envelope is
139 // decremented. The envelope output is then delayed one cycle until the state
140 // is changed to attack. Now one cycle less will be spent before the envelope
141 // is incremented, and the situation is normalized.
142 // The delay is probably caused by the comparison with the exponential counter,
143 // and does not seem to affect the rate counter. This has been verified by
144 // timing 256 consecutive complete envelopes with A = D = R = 1, S = 0, using
145 // CIA1 timer A and B in linked mode. If the rate counter is not affected the
146 // period of each complete envelope is
147 // (255 + 162*1 + 39*2 + 28*4 + 12*8 + 8*16 + 6*30)*32 = 756*32 = 32352
148 // which corresponds exactly to the timed value divided by the number of
149 // complete envelopes.
150 // NB! This one cycle delay is not modeled.
151
152
153 // From the sustain levels it follows that both the low and high 4 bits of the
154 // envelope counter are compared to the 4-bit sustain value.
155 // This has been verified by sampling ENV3.
156 //
157 reg8 EnvelopeGenerator2::sustain_level[] = {
158 0x00,
159 0x11,
160 0x22,
161 0x33,
162 0x44,
163 0x55,
164 0x66,
165 0x77,
166 0x88,
167 0x99,
168 0xaa,
169 0xbb,
170 0xcc,
171 0xdd,
172 0xee,
173 0xff,
174 };
175
176
177 // ----------------------------------------------------------------------------
178 // Register functions.
179 // ----------------------------------------------------------------------------
180 void EnvelopeGenerator2::writeCONTROL_REG(reg8 control)
181 {
182 reg8 gate_next = control & 0x01;
183
184 // The rate counter is never reset, thus there will be a delay before the
185 // envelope counter starts counting up (attack) or down (release).
186
187 // Gate bit on: Start attack, decay, sustain.
188 if (!gate && gate_next) {
189 state = ATTACK;
190 rate_period = rate_counter_period[attack];
191
192 // Switching to attack state unlocks the zero freeze.
193 hold_zero = false;
194
195 rate_counter = 0;
196 exponential_counter = 0;
197 //envelope_counter = 0;
198
199 if(envelope_counter == sustain_level[0xf])
200 {
201 envelope_counter--; //idk why it happens, but when envelope has max sustain and I retrigger with new note it just becomes silent so this is the only solution I found so far
202 }
203 }
204 // Gate bit off: Start release.
205 else if (gate && !gate_next) {
206 state = RELEASE;
207 rate_period = rate_counter_period[release];
208
209 rate_counter = 0;
210 exponential_counter = 0;
211 }
212
213 gate = gate_next;
214 }
215
216 void EnvelopeGenerator2::writeATTACK_DECAY(reg8 attack_decay)
217 {
218 attack = (attack_decay >> 4) & 0x0f;
219 decay = attack_decay & 0x0f;
220 if (state == ATTACK) {
221 rate_period = rate_counter_period[attack];
222 }
223 else if (state == DECAY_SUSTAIN) {
224 rate_period = rate_counter_period[decay];
225 }
226 }
227
228 void EnvelopeGenerator2::writeSUSTAIN_RELEASE(reg8 sustain_release)
229 {
230 reg4 prev_sus = sustain;
231
232 sustain = (sustain_release >> 4) & 0x0f;
233 release = sustain_release & 0x0f;
234 if (state == RELEASE) {
235 rate_period = rate_counter_period[release];
236 }
237
238 if (envelope_counter != sustain_level[sustain] && envelope_counter == sustain_level[prev_sus] && state == DECAY_SUSTAIN)
239 {
240 envelope_counter = sustain_level[sustain]; //so we can change sustain level without triggering envelope release, e.g. 2->6
241 }
242 }
243
244 void EnvelopeGenerator2::writeVOL(reg8 vol_reg)
245 {
246 volume = vol_reg >> 4;
247 }
248
249 reg8 EnvelopeGenerator2::readENV()
250 {
251 return output();
252 }