Debian Codegraph @

src/engine/platform/sound/c64_fp/WaveformGenerator.cpp

raw
1 /*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2011-2021 Leandro Nini <[email protected]>
5 * Copyright 2007-2010 Antti Lankila
6 * Copyright 2004 Dag Lem <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23 #define WAVEFORMGENERATOR_CPP
24
25 #include "WaveformGenerator.h"
26
27 /*
28 * This fixes tests
29 * SID/wb_testsuite/noise_writeback_check_8_to_C_old
30 * SID/wb_testsuite/noise_writeback_check_9_to_C_old
31 * SID/wb_testsuite/noise_writeback_check_A_to_C_old
32 * SID/wb_testsuite/noise_writeback_check_C_to_C_old
33 *
34 * but breaks SID/wf12nsr/wf12nsr
35 *
36 * needs more digging...
37 */
38 //#define NO_WB_NOI_PUL
39
40 namespace reSIDfp
41 {
42
43 /**
44 * Number of cycles after which the waveform output fades to 0 when setting
45 * the waveform register to 0.
46 * Values measured on warm chips (6581R3/R4 and 8580R5)
47 * checking OSC3.
48 * Times vary wildly with temperature and may differ
49 * from chip to chip so the numbers here represent
50 * only the big difference between the old and new models.
51 *
52 * See [VICE Bug #290](http://sourceforge.net/p/vice-emu/bugs/290/)
53 * and [VICE Bug #1128](http://sourceforge.net/p/vice-emu/bugs/1128/)
54 */
55 // ~95ms
56 const unsigned int FLOATING_OUTPUT_TTL_6581R3 = 54000;
57 const unsigned int FLOATING_OUTPUT_FADE_6581R3 = 1400;
58 // ~1s
59 //const unsigned int FLOATING_OUTPUT_TTL_6581R4 = 1000000;
60 // ~1s
61 const unsigned int FLOATING_OUTPUT_TTL_8580R5 = 800000;
62 const unsigned int FLOATING_OUTPUT_FADE_8580R5 = 50000;
63
64 /**
65 * Number of cycles after which the shift register is reset
66 * when the test bit is set.
67 * Values measured on warm chips (6581R3/R4 and 8580R5)
68 * checking OSC3.
69 * Times vary wildly with temperature and may differ
70 * from chip to chip so the numbers here represent
71 * only the big difference between the old and new models.
72 */
73 // ~210ms
74 const unsigned int SHIFT_REGISTER_RESET_6581R3 = 50000;
75 const unsigned int SHIFT_REGISTER_FADE_6581R3 = 15000;
76 // ~2.15s
77 //const unsigned int SHIFT_REGISTER_RESET_6581R4 = 2150000;
78 // ~2.8s
79 const unsigned int SHIFT_REGISTER_RESET_8580R5 = 986000;
80 const unsigned int SHIFT_REGISTER_FADE_8580R5 = 314300;
81
82 /*
83 * This is what happens when the lfsr is clocked:
84 *
85 * cycle 0: bit 19 of the accumulator goes from low to high, the noise register acts normally,
86 * the output may overwrite a bit;
87 *
88 * cycle 1: first phase of the shift, the bits are interconnected and the output of each bit
89 * is latched into the following. The output may overwrite the latched value.
90 *
91 * cycle 2: second phase of the shift, the latched value becomes active in the first
92 * half of the clock and from the second half the register returns to normal operation.
93 *
94 * When the test or reset lines are active the first phase is executed at every cyle
95 * until the signal is released triggering the second phase.
96 */
97 void WaveformGenerator::clock_shift_register(unsigned int bit0)
98 {
99 shift_register = (shift_register >> 1) | bit0;
100
101 // New noise waveform output.
102 set_noise_output();
103 }
104
105 unsigned int WaveformGenerator::get_noise_writeback()
106 {
107 return
108 ~(
109 (1 << 2) | // Bit 20
110 (1 << 4) | // Bit 18
111 (1 << 8) | // Bit 14
112 (1 << 11) | // Bit 11
113 (1 << 13) | // Bit 9
114 (1 << 17) | // Bit 5
115 (1 << 20) | // Bit 2
116 (1 << 22) // Bit 0
117 ) |
118 ((waveform_output & (1 << 11)) >> 9) | // Bit 11 -> bit 20
119 ((waveform_output & (1 << 10)) >> 6) | // Bit 10 -> bit 18
120 ((waveform_output & (1 << 9)) >> 1) | // Bit 9 -> bit 14
121 ((waveform_output & (1 << 8)) << 3) | // Bit 8 -> bit 11
122 ((waveform_output & (1 << 7)) << 6) | // Bit 7 -> bit 9
123 ((waveform_output & (1 << 6)) << 11) | // Bit 6 -> bit 5
124 ((waveform_output & (1 << 5)) << 15) | // Bit 5 -> bit 2
125 ((waveform_output & (1 << 4)) << 18); // Bit 4 -> bit 0
126 }
127
128 void WaveformGenerator::write_shift_register()
129 {
130 if (unlikely(waveform > 0x8) && likely(!test) && likely(shift_pipeline != 1))
131 {
132 // Write changes to the shift register output caused by combined waveforms
133 // back into the shift register. This happens only when the register is clocked
134 // (see $D1+$81_wave_test [1]) or when the test bit is falling.
135 // A bit once set to zero cannot be changed, hence the and'ing.
136 //
137 // [1] ftp://ftp.untergrund.net/users/nata/sid_test/$D1+$81_wave_test.7z
138 //
139 // FIXME: Write test program to check the effect of 1 bits and whether
140 // neighboring bits are affected.
141
142 #ifdef NO_WB_NOI_PUL
143 if (waveform == 0xc)
144 return;
145 #endif
146 shift_register &= get_noise_writeback();
147
148 noise_output &= waveform_output;
149 set_no_noise_or_noise_output();
150 }
151 }
152
153 void WaveformGenerator::set_noise_output()
154 {
155 noise_output =
156 ((shift_register & (1 << 2)) << 9) | // Bit 20 -> bit 11
157 ((shift_register & (1 << 4)) << 6) | // Bit 18 -> bit 10
158 ((shift_register & (1 << 8)) << 1) | // Bit 14 -> bit 9
159 ((shift_register & (1 << 11)) >> 3) | // Bit 11 -> bit 8
160 ((shift_register & (1 << 13)) >> 6) | // Bit 9 -> bit 7
161 ((shift_register & (1 << 17)) >> 11) | // Bit 5 -> bit 6
162 ((shift_register & (1 << 20)) >> 15) | // Bit 2 -> bit 5
163 ((shift_register & (1 << 22)) >> 18); // Bit 0 -> bit 4
164
165 set_no_noise_or_noise_output();
166 }
167
168 void WaveformGenerator::setWaveformModels(matrix_t* models)
169 {
170 model_wave = models;
171 }
172
173 void WaveformGenerator::synchronize(WaveformGenerator* syncDest, const WaveformGenerator* syncSource) const
174 {
175 // A special case occurs when a sync source is synced itself on the same
176 // cycle as when its MSB is set high. In this case the destination will
177 // not be synced. This has been verified by sampling OSC3.
178 if (unlikely(msb_rising) && syncDest->sync && !(sync && syncSource->msb_rising))
179 {
180 syncDest->accumulator = 0;
181 }
182 }
183
184 bool do_pre_writeback(unsigned int waveform_prev, unsigned int waveform, bool is6581)
185 {
186 // no writeback without combined waveforms
187 if (likely(waveform_prev <= 0x8))
188 return false;
189 // no writeback when changing to noise
190 if (waveform == 8)
191 return false;
192 // What's happening here?
193 if (is6581 &&
194 ((((waveform_prev & 0x3) == 0x1) && ((waveform & 0x3) == 0x2))
195 || (((waveform_prev & 0x3) == 0x2) && ((waveform & 0x3) == 0x1))))
196 return false;
197 if (waveform_prev == 0xc)
198 {
199 if (is6581)
200 return false;
201 else if ((waveform != 0x9) && (waveform != 0xe))
202 return false;
203 }
204 #ifdef NO_WB_NOI_PUL
205 if (waveform == 0xc)
206 return false;
207 #endif
208 // ok do the writeback
209 return true;
210 }
211
212 /*
213 * When noise and pulse are combined all the bits are
214 * connected and the four lower ones are grounded.
215 * This causes the adjacent bits to be pulled down,
216 * with different strength depending on model.
217 *
218 * This is just a rough attempt at modelling the effect.
219 */
220
221 static unsigned int noise_pulse6581(unsigned int noise)
222 {
223 return (noise < 0xf00) ? 0x000 : noise & (noise << 1) & (noise << 2);
224 }
225
226 static unsigned int noise_pulse8580(unsigned int noise)
227 {
228 return (noise < 0xfc0) ? noise & (noise << 1) : 0xfc0;
229 }
230
231 void WaveformGenerator::set_no_noise_or_noise_output()
232 {
233 no_noise_or_noise_output = no_noise | noise_output;
234
235 // pulse+noise
236 if (unlikely((waveform & 0xc) == 0xc))
237 no_noise_or_noise_output = is6581
238 ? noise_pulse6581(no_noise_or_noise_output)
239 : noise_pulse8580(no_noise_or_noise_output);
240
241 }
242
243 void WaveformGenerator::writeCONTROL_REG(unsigned char control)
244 {
245 const unsigned int waveform_prev = waveform;
246 const bool test_prev = test;
247
248 waveform = (control >> 4) & 0x0f;
249 test = (control & 0x08) != 0;
250 sync = (control & 0x02) != 0;
251
252 // Substitution of accumulator MSB when sawtooth = 0, ring_mod = 1.
253 ring_msb_mask = ((~control >> 5) & (control >> 2) & 0x1) << 23;
254
255 if (waveform != waveform_prev)
256 {
257 // Set up waveform table.
258 wave = (*model_wave)[waveform & 0x7];
259
260 // no_noise and no_pulse are used in set_waveform_output() as bitmasks to
261 // only let the noise or pulse influence the output when the noise or pulse
262 // waveforms are selected.
263 no_noise = (waveform & 0x8) != 0 ? 0x000 : 0xfff;
264 set_no_noise_or_noise_output();
265 no_pulse = (waveform & 0x4) != 0 ? 0x000 : 0xfff;
266
267 if (waveform == 0)
268 {
269 // Change to floating DAC input.
270 // Reset fading time for floating DAC input.
271 floating_output_ttl = is6581 ? FLOATING_OUTPUT_TTL_6581R3 : FLOATING_OUTPUT_TTL_8580R5;
272 }
273 }
274
275 if (test != test_prev)
276 {
277 if (test)
278 {
279 // Reset accumulator.
280 accumulator = 0;
281
282 // Flush shift pipeline.
283 shift_pipeline = 0;
284
285 // Set reset time for shift register.
286 shift_register_reset = is6581 ? SHIFT_REGISTER_RESET_6581R3 : SHIFT_REGISTER_RESET_8580R5;
287 }
288 else
289 {
290 // When the test bit is falling, the second phase of the shift is
291 // completed by enabling SRAM write.
292
293 // During first phase of the shift the bits are interconnected
294 // and the output of each bit is latched into the following.
295 // The output may overwrite the latched value.
296 if (do_pre_writeback(waveform_prev, waveform, is6581))
297 {
298 shift_register &= get_noise_writeback();
299 }
300
301 // bit0 = (bit22 | test) ^ bit17 = 1 ^ bit17 = ~bit17
302 clock_shift_register((~shift_register << 17) & (1 << 22));
303 }
304 }
305 }
306
307 void WaveformGenerator::waveBitfade()
308 {
309 waveform_output &= waveform_output >> 1;
310 osc3 = waveform_output;
311 if (waveform_output != 0)
312 floating_output_ttl = is6581 ? FLOATING_OUTPUT_FADE_6581R3 : FLOATING_OUTPUT_FADE_8580R5;
313 }
314
315 void WaveformGenerator::shiftregBitfade()
316 {
317 shift_register |= shift_register >> 1;
318 shift_register |= 0x400000;
319 if (shift_register != 0x7fffff)
320 shift_register_reset = is6581 ? SHIFT_REGISTER_FADE_6581R3 : SHIFT_REGISTER_FADE_8580R5;
321 }
322
323 void WaveformGenerator::reset()
324 {
325 // accumulator is not changed on reset
326 freq = 0;
327 pw = 0;
328
329 msb_rising = false;
330
331 waveform = 0;
332 osc3 = 0;
333
334 test = false;
335 sync = false;
336
337 wave = model_wave ? (*model_wave)[0] : nullptr;
338
339 ring_msb_mask = 0;
340 no_noise = 0xfff;
341 no_pulse = 0xfff;
342 pulse_output = 0xfff;
343
344 shift_register_reset = 0;
345 shift_register = 0x7fffff;
346 // when reset is released the shift register is clocked once
347 // so the lower bit is zeroed out
348 // bit0 = (bit22 | test) ^ bit17 = 1 ^ 1 = 0
349 clock_shift_register(0);
350
351 shift_pipeline = 0;
352
353 waveform_output = 0;
354 floating_output_ttl = 0;
355 }
356
357 } // namespace reSIDfp