libsidplayfp  2.2.2
WaveformGenerator.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2016 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 #include "sidcxx11.h"
30 
31 namespace reSIDfp
32 {
33 
87 {
88 private:
89  matrix_t* model_wave;
90 
91  short* wave;
92 
93  // PWout = (PWn/40.95)%
94  unsigned int pw;
95 
96  unsigned int shift_register;
97 
99  int shift_pipeline;
100 
101  unsigned int ring_msb_mask;
102  unsigned int no_noise;
103  unsigned int noise_output;
104  unsigned int no_noise_or_noise_output;
105  unsigned int no_pulse;
106  unsigned int pulse_output;
107 
109  unsigned int waveform;
110 
111  unsigned int waveform_output;
112 
114  unsigned int accumulator;
115 
116  // Fout = (Fn*Fclk/16777216)Hz
117  unsigned int freq;
118 
120  unsigned int tri_saw_pipeline;
121 
123  unsigned int osc3;
124 
126  unsigned int shift_register_reset;
127 
128  // The wave signal TTL when no waveform is selected
129  unsigned int floating_output_ttl;
130 
132 
133  bool test;
134  bool sync;
136 
138  bool msb_rising;
139 
140  bool is6581;
141 
142  float dac[4096];
143 
144 private:
145  void clock_shift_register(unsigned int bit0);
146 
147  unsigned int get_noise_writeback();
148 
149  void write_shift_register();
150 
151  void set_noise_output();
152 
153  void set_no_noise_or_noise_output();
154 
155  void waveBitfade();
156 
157  void shiftregBitfade();
158 
159 public:
160  void setWaveformModels(matrix_t* models);
161 
169  void setChipModel(ChipModel chipModel);
170 
174  void clock();
175 
184  void synchronize(WaveformGenerator* syncDest, const WaveformGenerator* syncSource) const;
185 
190  model_wave(nullptr),
191  wave(nullptr),
192  pw(0),
193  shift_register(0),
194  shift_pipeline(0),
195  ring_msb_mask(0),
196  no_noise(0),
197  noise_output(0),
198  no_noise_or_noise_output(0),
199  no_pulse(0),
200  pulse_output(0),
201  waveform(0),
202  waveform_output(0),
203  accumulator(0x555555), // Accumulator's even bits are high on powerup
204  freq(0),
205  tri_saw_pipeline(0x555),
206  osc3(0),
207  shift_register_reset(0),
208  floating_output_ttl(0),
209  test(false),
210  sync(false),
211  msb_rising(false),
212  is6581(true) {}
213 
219  void writeFREQ_LO(unsigned char freq_lo) { freq = (freq & 0xff00) | (freq_lo & 0xff); }
220 
226  void writeFREQ_HI(unsigned char freq_hi) { freq = (freq_hi << 8 & 0xff00) | (freq & 0xff); }
227 
233  void writePW_LO(unsigned char pw_lo) { pw = (pw & 0xf00) | (pw_lo & 0x0ff); }
234 
240  void writePW_HI(unsigned char pw_hi) { pw = (pw_hi << 8 & 0xf00) | (pw & 0x0ff); }
241 
247  void writeCONTROL_REG(unsigned char control);
248 
252  void reset();
253 
260  float output(const WaveformGenerator* ringModulator);
261 
265  unsigned char readOSC() const { return static_cast<unsigned char>(osc3 >> 4); }
266 
270  unsigned int readAccumulator() const { return accumulator; }
271 
275  unsigned int readFreq() const { return freq; }
276 
280  bool readTest() const { return test; }
281 
285  bool readSync() const { return sync; }
286 };
287 
288 } // namespace reSIDfp
289 
290 #if RESID_INLINING || defined(WAVEFORMGENERATOR_CPP)
291 
292 namespace reSIDfp
293 {
294 
295 RESID_INLINE
297 {
298  if (unlikely(test))
299  {
300  if (unlikely(shift_register_reset != 0) && unlikely(--shift_register_reset == 0))
301  {
302  shiftregBitfade();
303 
304  // New noise waveform output.
305  set_noise_output();
306  }
307 
308  // The test bit sets pulse high.
309  pulse_output = 0xfff;
310  }
311  else
312  {
313  // Calculate new accumulator value;
314  const unsigned int accumulator_old = accumulator;
315  accumulator = (accumulator + freq) & 0xffffff;
316 
317  // Check which bit have changed
318  const unsigned int accumulator_bits_set = ~accumulator_old & accumulator;
319 
320  // Check whether the MSB is set high. This is used for synchronization.
321  msb_rising = (accumulator_bits_set & 0x800000) != 0;
322 
323  // Shift noise register once for each time accumulator bit 19 is set high.
324  // The shift is delayed 2 cycles.
325  if (unlikely((accumulator_bits_set & 0x080000) != 0))
326  {
327  // Pipeline: Detect rising bit, shift phase 1, shift phase 2.
328  shift_pipeline = 2;
329  }
330  else if (unlikely(shift_pipeline != 0) && --shift_pipeline == 0)
331  {
332  // bit0 = (bit22 | test) ^ bit17
333  clock_shift_register(((shift_register << 22) ^ (shift_register << 17)) & (1 << 22));
334  }
335  }
336 }
337 
338 RESID_INLINE
339 float WaveformGenerator::output(const WaveformGenerator* ringModulator)
340 {
341  // Set output value.
342  if (likely(waveform != 0))
343  {
344  const unsigned int ix = (accumulator ^ (~ringModulator->accumulator & ring_msb_mask)) >> 12;
345 
346  // The bit masks no_pulse and no_noise are used to achieve branch-free
347  // calculation of the output value.
348  waveform_output = wave[ix] & (no_pulse | pulse_output) & no_noise_or_noise_output;
349 
350  // Triangle/Sawtooth output is delayed half cycle on 8580.
351  // This will appear as a one cycle delay on OSC3 as it is latched first phase of the clock.
352  if ((waveform & 3) && !is6581)
353  {
354  osc3 = tri_saw_pipeline & (no_pulse | pulse_output) & no_noise_or_noise_output;
355  tri_saw_pipeline = wave[ix];
356  }
357  else
358  {
359  osc3 = waveform_output;
360  }
361 
362  // In the 6581 the top bit of the accumulator may be driven low by combined waveforms
363  // when the sawtooth is selected
364  // FIXME doesn't seem to always happen
365  if ((waveform & 2) && unlikely(waveform & 0xd) && is6581)
366  accumulator &= (waveform_output << 12) | 0x7fffff;
367 
368  write_shift_register();
369  }
370  else
371  {
372  // Age floating DAC input.
373  if (likely(floating_output_ttl != 0) && unlikely(--floating_output_ttl == 0))
374  {
375  waveBitfade();
376  }
377  }
378 
379  // The pulse level is defined as (accumulator >> 12) >= pw ? 0xfff : 0x000.
380  // The expression -((accumulator >> 12) >= pw) & 0xfff yields the same
381  // results without any branching (and thus without any pipeline stalls).
382  // NB! This expression relies on that the result of a boolean expression
383  // is either 0 or 1, and furthermore requires two's complement integer.
384  // A few more cycles may be saved by storing the pulse width left shifted
385  // 12 bits, and dropping the and with 0xfff (this is valid since pulse is
386  // used as a bit mask on 12 bit values), yielding the expression
387  // -(accumulator >= pw24). However this only results in negligible savings.
388 
389  // The result of the pulse width compare is delayed one cycle.
390  // Push next pulse level into pulse level pipeline.
391  pulse_output = ((accumulator >> 12) >= pw) ? 0xfff : 0x000;
392 
393  // DAC imperfections are emulated by using waveform_output as an index
394  // into a DAC lookup table. readOSC() uses waveform_output directly.
395  return dac[waveform_output];
396 }
397 
398 } // namespace reSIDfp
399 
400 #endif
401 
402 #endif
Definition: array.h:43
Definition: WaveformGenerator.h:87
void synchronize(WaveformGenerator *syncDest, const WaveformGenerator *syncSource) const
Definition: WaveformGenerator.cpp:193
bool readTest() const
Definition: WaveformGenerator.h:280
void clock()
Definition: WaveformGenerator.h:296
unsigned int readFreq() const
Definition: WaveformGenerator.h:275
void writePW_HI(unsigned char pw_hi)
Definition: WaveformGenerator.h:240
void writeFREQ_HI(unsigned char freq_hi)
Definition: WaveformGenerator.h:226
void writePW_LO(unsigned char pw_lo)
Definition: WaveformGenerator.h:233
unsigned char readOSC() const
Definition: WaveformGenerator.h:265
void writeFREQ_LO(unsigned char freq_lo)
Definition: WaveformGenerator.h:219
void writeCONTROL_REG(unsigned char control)
Definition: WaveformGenerator.cpp:263
WaveformGenerator()
Definition: WaveformGenerator.h:189
void setChipModel(ChipModel chipModel)
Definition: WaveformGenerator.cpp:177
unsigned int readAccumulator() const
Definition: WaveformGenerator.h:270
bool readSync() const
Definition: WaveformGenerator.h:285
void reset()
Definition: WaveformGenerator.cpp:346
float output(const WaveformGenerator *ringModulator)
Definition: WaveformGenerator.h:339