libresidfp 1.3.0
WaveformGenerator.h
1/*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2011-2025 Leandro Nini <drfiemost@users.sourceforge.net>
5 * Copyright 2007-2010 Antti Lankila
6 * Copyright 2004,2010 Dag Lem <resid@nimrod.no>
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#ifndef WAVEFORMGENERATOR_H
24#define WAVEFORMGENERATOR_H
25
26#include "siddefs-fp.h"
27#include "array.h"
28
29// print SR debugging info
30//#define TRACE 1
31
32#ifdef TRACE
33# include <iostream>
34#endif
35
36namespace reSIDfp
37{
38
92{
93 friend class State;
94
95private:
96 rc_matrix_t model_wave;
97 rc_matrix_t model_pulldown;
98
99 int16_t* wave = nullptr;
100 int16_t* pulldown = nullptr;
101
102 // PWout = (PWn/40.95)%
103 uint32_t pw = 0;
104
105 uint32_t shift_register = 0;
106
108 uint32_t shift_latch = 0;
109
111 int shift_pipeline = 0;
112
113 uint32_t ring_msb_mask = 0;
114 uint32_t no_noise = 0;
115 uint32_t noise_output = 0;
116 uint32_t no_noise_or_noise_output = 0;
117 uint32_t no_pulse = 0;
118 uint32_t pulse_output = 0;
119
120 uint32_t waveform_output = 0;
121
123 uint32_t accumulator = 0x555555; // Accumulator's even bits are high on powerup
124
125 // Fout = (Fn*Fclk/16777216)Hz
126 uint32_t freq = 0;
127
129 uint32_t tri_saw_pipeline = 0x555;
130
132 uint32_t osc3 = 0;
133
135 unsigned int shift_register_reset = 0;
136
137 // The wave signal TTL when no waveform is selected.
138 unsigned int floating_output_ttl = 0;
139
141 uint8_t waveform = 0;
142
144
145 bool test = false;
146 bool sync = false;
148
150 bool test_or_reset;
151
153 bool msb_rising = false;
154
155 bool is6581; //-V730_NOINIT this is initialized in the SID constructor
156
157 bool strongPS = false;
158 bool msb_pulldown = false;
159
161
162 const WaveformGenerator* prevVoice;
163 WaveformGenerator* nextVoice;
165
166private:
167 void shift_phase2(uint8_t waveform_old, uint8_t waveform_new);
168
169 void write_shift_register();
170
171 void set_noise_output();
172
173 void set_no_noise_or_noise_output();
174
175 void waveBitfade();
176
177 void shiftregBitfade();
178
179 inline void setWave() { wave = (*model_wave)[waveform & 0x3]; }
180
181 inline void setPulldown()
182 {
183 // We assume tha combinations including noise
184 // behave the same as without
185 switch (waveform & 0x7)
186 {
187 case 3:
188 pulldown = (*model_pulldown)[0];
189 break;
190 case 4:
191 pulldown = (waveform & 0x8) ? (*model_pulldown)[4] : nullptr;
192 break;
193 case 5:
194 pulldown = (*model_pulldown)[1];
195 break;
196 case 6:
197 pulldown = (*model_pulldown)[2];
198 break;
199 case 7:
200 pulldown = (*model_pulldown)[3];
201 break;
202 default:
203 pulldown = nullptr;
204 }
205 }
206
207public:
208 void setWaveformModels(rc_matrix_t models);
209 void setPulldownModels(rc_matrix_t models);
210
211 inline void setStrongPS(bool sps) { strongPS = sps; }
212
213 void setOtherWaveforms(const WaveformGenerator* prev, WaveformGenerator* next)
214 {
215 prevVoice = prev;
216 nextVoice = next;
217 }
218
225 void setModel(bool new_is6581) { is6581 = new_is6581; }
226
230 void clock();
231
237 void synchronize() const;
238
244 void writeFREQ_LO(uint8_t freq_lo) { freq = (freq & 0xff00) | (freq_lo & 0xff); }
245
251 void writeFREQ_HI(uint8_t freq_hi) { freq = (freq_hi << 8 & 0xff00) | (freq & 0xff); }
252
258 void writePW_LO(uint8_t pw_lo) { pw = (pw & 0xf00) | (pw_lo & 0x0ff); }
259
265 void writePW_HI(uint8_t pw_hi) { pw = (pw_hi << 8 & 0xf00) | (pw & 0x0ff); }
266
272 void writeCONTROL_REG(uint8_t control);
273
277 void reset();
278
284 uint32_t output();
285
289 uint8_t readOSC() const { return static_cast<uint8_t>(osc3 >> 4); }
290
294 uint32_t readAccumulator() const { return accumulator; }
295
299 uint32_t readFreq() const { return freq; }
300
304 bool readTest() const { return test; }
305
309 bool readFollowingVoiceSync() const { return nextVoice->sync; }
310};
311
312} // namespace reSIDfp
313
314#if RESIDFP_INLINING || defined(WAVEFORMGENERATOR_CPP)
315
316namespace reSIDfp
317{
318
319RESIDFP_INLINE
321{
322 if (unlikely(test))
323 {
324 if (unlikely(shift_register_reset != 0) && unlikely(--shift_register_reset == 0))
325 {
326#ifdef TRACE
327 std::cout << "shiftregBitfade" << std::endl;
328#endif
329 shiftregBitfade();
330 shift_latch = shift_register;
331
332 // New noise waveform output.
333 set_noise_output();
334 }
335
336 // Latch the test bit value for shift phase 2.
337 test_or_reset = true;
338
339 // The test bit sets pulse high.
340 pulse_output = 0xfff;
341 }
342 else
343 {
344 // Calculate new accumulator value;
345 const uint32_t accumulator_old = accumulator;
346 accumulator = (accumulator + freq) & 0xffffff;
347
348 // Check which bit have changed from low to high
349 const uint32_t accumulator_bits_set = ~accumulator_old & accumulator;
350
351 // Check whether the MSB is set high. This is used for synchronization.
352 msb_rising = (accumulator_bits_set & 0x800000) != 0;
353
354 // Shift noise register once for each time accumulator bit 19 is set high.
355 // The shift is delayed 2 cycles.
356 if (unlikely((accumulator_bits_set & 0x080000) != 0))
357 {
358 // Pipeline: Detect rising bit, shift phase 1, shift phase 2.
359 shift_pipeline = 2;
360 }
361 else if (unlikely(shift_pipeline != 0))
362 {
363 switch (--shift_pipeline)
364 {
365 case 0:
366#ifdef TRACE
367 std::cout << "shift phase 2" << std::endl;
368#endif
369 shift_phase2(waveform, waveform);
370 break;
371 case 1:
372#ifdef TRACE
373 std::cout << "shift phase 1" << std::endl;
374#endif
375 // Start shift phase 1.
376 test_or_reset = false;
377 shift_latch = shift_register;
378 break;
379 }
380 }
381 }
382}
383
384RESIDFP_INLINE
386{
387 // Set output value.
388 if (likely(waveform != 0))
389 {
390 const uint32_t ix = (accumulator ^ (~prevVoice->accumulator & ring_msb_mask)) >> 12;
391
392 // The bit masks no_pulse and no_noise are used to achieve branch-free
393 // calculation of the output value.
394 waveform_output = wave[ix] & (no_pulse | pulse_output) & no_noise_or_noise_output;
395 if (pulldown != nullptr)
396 waveform_output = pulldown[waveform_output];
397
398 // Triangle/Sawtooth output is delayed half cycle on 8580.
399 // This will appear as a one cycle delay on OSC3 as it is latched
400 // in the first phase of the clock.
401 if (!is6581 && (waveform & 3))
402 {
403 osc3 = tri_saw_pipeline & (no_pulse | pulse_output) & no_noise_or_noise_output;
404 if (pulldown != nullptr)
405 osc3 = pulldown[osc3];
406 tri_saw_pipeline = wave[ix];
407 }
408 else
409 {
410 osc3 = waveform_output;
411 }
412
413 // In the 6581 the top bit of the accumulator may be driven low by combined waveforms
414 // when the sawtooth is selected
415 // On some chips it doesn't happen with P+S
416 if (msb_pulldown && ((waveform_output & 0x800) == 0))
417 {
418 msb_rising = false;
419 accumulator &= 0x7fffff;
420 }
421
422 write_shift_register();
423 }
424 else
425 {
426 // Age floating DAC input.
427 if (likely(floating_output_ttl != 0) && unlikely(--floating_output_ttl == 0))
428 {
429 waveBitfade();
430 }
431 }
432
433 // The pulse level is defined as (accumulator >> 12) >= pw ? 0xfff : 0x000.
434 // The expression -((accumulator >> 12) >= pw) & 0xfff yields the same
435 // results without any branching (and thus without any pipeline stalls).
436 // NB! This expression relies on that the result of a boolean expression
437 // is either 0 or 1, and furthermore requires two's complement integer.
438 // A few more cycles may be saved by storing the pulse width left shifted
439 // 12 bits, and dropping the and with 0xfff (this is valid since pulse is
440 // used as a bit mask on 12 bit values), yielding the expression
441 // -(accumulator >= pw24). However this only results in negligible savings.
442
443 // The result of the pulse width compare is delayed one cycle.
444 // Push next pulse level into pulse level pipeline.
445 pulse_output = ((accumulator >> 12) >= pw) ? 0xfff : 0x000;
446
447 return waveform_output;
448}
449
450} // namespace reSIDfp
451
452#endif
453
454#endif
Definition State.h:46
Definition WaveformGenerator.h:92
void setModel(bool new_is6581)
Definition WaveformGenerator.h:225
bool readTest() const
Definition WaveformGenerator.h:304
void writeFREQ_LO(uint8_t freq_lo)
Definition WaveformGenerator.h:244
uint32_t output()
Definition WaveformGenerator.h:385
uint32_t readAccumulator() const
Definition WaveformGenerator.h:294
void writeCONTROL_REG(uint8_t control)
Definition WaveformGenerator.cpp:354
void clock()
Definition WaveformGenerator.h:320
uint32_t readFreq() const
Definition WaveformGenerator.h:299
void writeFREQ_HI(uint8_t freq_hi)
Definition WaveformGenerator.h:251
uint8_t readOSC() const
Definition WaveformGenerator.h:289
void writePW_HI(uint8_t pw_hi)
Definition WaveformGenerator.h:265
bool readFollowingVoiceSync() const
Definition WaveformGenerator.h:309
void reset()
Definition WaveformGenerator.cpp:437
void writePW_LO(uint8_t pw_lo)
Definition WaveformGenerator.h:258
void synchronize() const
Definition WaveformGenerator.cpp:338