libsidplayfp  2.1.0
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 
119  // 8580 tri/saw pipeline
120  unsigned int tri_saw_pipeline;
121  unsigned int osc3;
122 
124  unsigned int shift_register_reset;
125 
126  unsigned int floating_output_ttl;
127 
129 
130  bool test;
131  bool sync;
133 
135  bool msb_rising;
136 
137  bool is6581;
138 
139  float dac[4096];
140 
141 private:
142  void clock_shift_register(unsigned int bit0);
143 
144  unsigned int get_noise_writeback();
145 
146  void write_shift_register();
147 
148  void set_noise_output();
149 
150  void waveBitfade();
151 
152  void shiftregBitfade();
153 
154 public:
155  void setWaveformModels(matrix_t* models);
156 
164  void setChipModel(ChipModel chipModel);
165 
169  void clock();
170 
179  void synchronize(WaveformGenerator* syncDest, const WaveformGenerator* syncSource) const;
180 
185  model_wave(nullptr),
186  wave(nullptr),
187  pw(0),
188  shift_register(0),
189  shift_pipeline(0),
190  ring_msb_mask(0),
191  no_noise(0),
192  noise_output(0),
193  no_noise_or_noise_output(no_noise | noise_output),
194  no_pulse(0),
195  pulse_output(0),
196  waveform(0),
197  waveform_output(0),
198  accumulator(0x555555), // Accumulator's even bits are high on powerup
199  freq(0),
200  tri_saw_pipeline(0x555),
201  osc3(0),
202  shift_register_reset(0),
203  floating_output_ttl(0),
204  test(false),
205  sync(false),
206  msb_rising(false),
207  is6581(true) {}
208 
214  void writeFREQ_LO(unsigned char freq_lo) { freq = (freq & 0xff00) | (freq_lo & 0xff); }
215 
221  void writeFREQ_HI(unsigned char freq_hi) { freq = (freq_hi << 8 & 0xff00) | (freq & 0xff); }
222 
228  void writePW_LO(unsigned char pw_lo) { pw = (pw & 0xf00) | (pw_lo & 0x0ff); }
229 
235  void writePW_HI(unsigned char pw_hi) { pw = (pw_hi << 8 & 0xf00) | (pw & 0x0ff); }
236 
242  void writeCONTROL_REG(unsigned char control);
243 
247  void reset();
248 
255  float output(const WaveformGenerator* ringModulator);
256 
260  unsigned char readOSC() const { return static_cast<unsigned char>(osc3 >> 4); }
261 
265  unsigned int readAccumulator() const { return accumulator; }
266 
270  unsigned int readFreq() const { return freq; }
271 
275  bool readTest() const { return test; }
276 
280  bool readSync() const { return sync; }
281 };
282 
283 } // namespace reSIDfp
284 
285 #if RESID_INLINING || defined(WAVEFORMGENERATOR_CPP)
286 
287 namespace reSIDfp
288 {
289 
290 RESID_INLINE
292 {
293  if (unlikely(test))
294  {
295  if (unlikely(shift_register_reset != 0) && unlikely(--shift_register_reset == 0))
296  {
297  shiftregBitfade();
298 
299  // New noise waveform output.
300  set_noise_output();
301  }
302 
303  // The test bit sets pulse high.
304  pulse_output = 0xfff;
305  }
306  else
307  {
308  // Calculate new accumulator value;
309  const unsigned int accumulator_old = accumulator;
310  accumulator = (accumulator + freq) & 0xffffff;
311 
312  // Check which bit have changed
313  const unsigned int accumulator_bits_set = ~accumulator_old & accumulator;
314 
315  // Check whether the MSB is set high. This is used for synchronization.
316  msb_rising = (accumulator_bits_set & 0x800000) != 0;
317 
318  // Shift noise register once for each time accumulator bit 19 is set high.
319  // The shift is delayed 2 cycles.
320  if (unlikely((accumulator_bits_set & 0x080000) != 0))
321  {
322  // Pipeline: Detect rising bit, shift phase 1, shift phase 2.
323  shift_pipeline = 2;
324  }
325  else if (unlikely(shift_pipeline != 0) && --shift_pipeline == 0)
326  {
327  // bit0 = (bit22 | test) ^ bit17
328  clock_shift_register(((shift_register << 22) ^ (shift_register << 17)) & (1 << 22));
329  }
330  }
331 }
332 
333 static unsigned int noise_pulse6581(unsigned int noise)
334 {
335  return (noise < 0xf00) ? 0x000 : noise & (noise << 1) & (noise << 2);
336 }
337 
338 static unsigned int noise_pulse8580(unsigned int noise)
339 {
340  return (noise < 0xfc0) ? noise & (noise << 1) : 0xfc0;
341 }
342 
343 RESID_INLINE
344 float WaveformGenerator::output(const WaveformGenerator* ringModulator)
345 {
346  // Set output value.
347  if (likely(waveform != 0))
348  {
349  const unsigned int ix = (accumulator ^ (~ringModulator->accumulator & ring_msb_mask)) >> 12;
350 
351  // The bit masks no_pulse and no_noise are used to achieve branch-free
352  // calculation of the output value.
353  waveform_output = wave[ix] & (no_pulse | pulse_output) & no_noise_or_noise_output;
354 
355  // pulse+noise
356  if (unlikely((waveform & 0xc) == 0xc))
357  waveform_output = is6581 ? noise_pulse6581(waveform_output) : noise_pulse8580(waveform_output);
358 
359  // Triangle/Sawtooth output is delayed half cycle on 8580.
360  // This will appear as a one cycle delay on OSC3 as it is latched first phase of the clock.
361  if ((waveform & 3) && !is6581)
362  {
363  osc3 = tri_saw_pipeline & (no_pulse | pulse_output) & no_noise_or_noise_output;
364  tri_saw_pipeline = wave[ix];
365  }
366  else
367  {
368  osc3 = waveform_output;
369  }
370 
371  // In the 6581 the top bit of the accumulator may be driven low by combined waveforms
372  // when the sawtooth is selected
373  // FIXME doesn't seem to always happen
374  if ((waveform & 2) && unlikely(waveform & 0xd) && is6581)
375  accumulator &= (waveform_output << 12) | 0x7fffff;
376 
377  write_shift_register();
378  }
379  else
380  {
381  // Age floating DAC input.
382  if (likely(floating_output_ttl != 0) && unlikely(--floating_output_ttl == 0))
383  {
384  waveBitfade();
385  }
386  }
387 
388  // The pulse level is defined as (accumulator >> 12) >= pw ? 0xfff : 0x000.
389  // The expression -((accumulator >> 12) >= pw) & 0xfff yields the same
390  // results without any branching (and thus without any pipeline stalls).
391  // NB! This expression relies on that the result of a boolean expression
392  // is either 0 or 1, and furthermore requires two's complement integer.
393  // A few more cycles may be saved by storing the pulse width left shifted
394  // 12 bits, and dropping the and with 0xfff (this is valid since pulse is
395  // used as a bit mask on 12 bit values), yielding the expression
396  // -(accumulator >= pw24). However this only results in negligible savings.
397 
398  // The result of the pulse width compare is delayed one cycle.
399  // Push next pulse level into pulse level pipeline.
400  pulse_output = ((accumulator >> 12) >= pw) ? 0xfff : 0x000;
401 
402  // DAC imperfections are emulated by using waveform_output as an index
403  // into a DAC lookup table. readOSC() uses waveform_output directly.
404  return dac[waveform_output];
405 }
406 
407 } // namespace reSIDfp
408 
409 #endif
410 
411 #endif
Definition: array.h:43
Definition: WaveformGenerator.h:87
void synchronize(WaveformGenerator *syncDest, const WaveformGenerator *syncSource) const
Definition: WaveformGenerator.cpp:176
bool readTest() const
Definition: WaveformGenerator.h:275
void clock()
Definition: WaveformGenerator.h:291
unsigned int readFreq() const
Definition: WaveformGenerator.h:270
void writePW_HI(unsigned char pw_hi)
Definition: WaveformGenerator.h:235
void writeFREQ_HI(unsigned char freq_hi)
Definition: WaveformGenerator.h:221
void writePW_LO(unsigned char pw_lo)
Definition: WaveformGenerator.h:228
unsigned char readOSC() const
Definition: WaveformGenerator.h:260
void writeFREQ_LO(unsigned char freq_lo)
Definition: WaveformGenerator.h:214
void writeCONTROL_REG(unsigned char control)
Definition: WaveformGenerator.cpp:206
WaveformGenerator()
Definition: WaveformGenerator.h:184
void setChipModel(ChipModel chipModel)
Definition: WaveformGenerator.cpp:160
unsigned int readAccumulator() const
Definition: WaveformGenerator.h:265
bool readSync() const
Definition: WaveformGenerator.h:280
void reset()
Definition: WaveformGenerator.cpp:289
float output(const WaveformGenerator *ringModulator)
Definition: WaveformGenerator.h:344