libsidplayfp  1.4.1
WaveformGenerator.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2013 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 namespace reSIDfp
30 {
31 
88 {
89 private:
90  matrix_t* model_wave;
91 
92  // PWout = (PWn/40.95)%
93  int pw;
94 
95  int shift_register;
96 
98  int shift_register_reset;
99 
101  int shift_pipeline;
102 
103  int ring_msb_mask;
104  int no_noise;
105  int noise_output;
106  int no_noise_or_noise_output;
107  int no_pulse;
108  int pulse_output;
109 
111  int waveform;
112 
113  int floating_output_ttl;
114 
115  short* wave;
116 
117  int waveform_output;
118 
120  int accumulator;
121 
122  // Fout = (Fn*Fclk/16777216)Hz
123  int freq;
124 
126 
127  bool test;
128  bool sync;
130 
132  bool msb_rising;
133 
134  short dac[4096];
135 
136 private:
137  void clock_shift_register();
138 
139  void write_shift_register();
140 
141  void reset_shift_register();
142 
143  void set_noise_output();
144 
145 public:
146  void setWaveformModels(matrix_t* models);
147 
155  void setChipModel(ChipModel chipModel);
156 
160  void clock();
161 
170  void synchronize(WaveformGenerator* syncDest, const WaveformGenerator* syncSource) const;
171 
176  model_wave(0),
177  pw(0),
178  shift_register(0),
179  shift_register_reset(0),
180  shift_pipeline(0),
181  ring_msb_mask(0),
182  no_noise(0),
183  noise_output(0),
184  no_noise_or_noise_output(no_noise | noise_output),
185  no_pulse(0),
186  pulse_output(0),
187  waveform(0),
188  floating_output_ttl(0),
189  wave(0),
190  waveform_output(0),
191  accumulator(0),
192  freq(0),
193  test(false),
194  sync(false),
195  msb_rising(false) {}
196 
202  void writeFREQ_LO(unsigned char freq_lo) { freq = (freq & 0xff00) | (freq_lo & 0xff); }
203 
209  void writeFREQ_HI(unsigned char freq_hi) { freq = (freq_hi << 8 & 0xff00) | (freq & 0xff); }
210 
219  void writePW_LO(unsigned char pw_lo) { pw = (pw & 0xf00) | (pw_lo & 0x0ff); }
220 
226  void writePW_HI(unsigned char pw_hi) { pw = (pw_hi << 8 & 0xf00) | (pw & 0x0ff); }
227 
233  void writeCONTROL_REG(unsigned char control);
234 
238  void reset();
239 
247  short output(const WaveformGenerator* ringModulator);
248 
252  unsigned char readOSC() const { return (unsigned char)(waveform_output >> 4); }
253 
257  int readAccumulator() const { return accumulator; }
258 
262  int readFreq() const { return freq; }
263 
267  bool readTest() const { return test; }
268 
272  bool readSync() const { return sync; }
273 };
274 
275 } // namespace reSIDfp
276 
277 #if RESID_INLINING || defined(WAVEFORMGENERATOR_CPP)
278 
279 namespace reSIDfp
280 {
281 
282 RESID_INLINE
284 {
285  if (unlikely(test))
286  {
287  if (unlikely(shift_register_reset != 0) && unlikely(--shift_register_reset == 0))
288  {
289  reset_shift_register();
290  }
291 
292  // The test bit sets pulse high.
293  pulse_output = 0xfff;
294  }
295  else
296  {
297  // Calculate new accumulator value;
298  const int accumulator_next = (accumulator + freq) & 0xffffff;
299  const int accumulator_bits_set = ~accumulator & accumulator_next;
300  accumulator = accumulator_next;
301 
302  // Check whether the MSB is set high. This is used for synchronization.
303  msb_rising = (accumulator_bits_set & 0x800000) != 0;
304 
305  // Shift noise register once for each time accumulator bit 19 is set high.
306  // The shift is delayed 2 cycles.
307  if (unlikely((accumulator_bits_set & 0x080000) != 0))
308  {
309  // Pipeline: Detect rising bit, shift phase 1, shift phase 2.
310  shift_pipeline = 2;
311  }
312  else if (unlikely(shift_pipeline) != 0 && --shift_pipeline == 0)
313  {
314  clock_shift_register();
315  }
316  }
317 }
318 
319 RESID_INLINE
320 short WaveformGenerator::output(const WaveformGenerator* ringModulator)
321 {
322  // Set output value.
323  if (likely(waveform != 0))
324  {
325  // The bit masks no_pulse and no_noise are used to achieve branch-free
326  // calculation of the output value.
327  const int ix = (accumulator ^ (ringModulator->accumulator & ring_msb_mask)) >> 12;
328  waveform_output = wave[ix] & (no_pulse | pulse_output) & no_noise_or_noise_output;
329 
330  if (unlikely(waveform > 0x8))
331  {
332  // Combined waveforms that include noise
333  // write to the shift register.
334  write_shift_register();
335  }
336  }
337  else
338  {
339  // Age floating DAC input.
340  if (likely(floating_output_ttl != 0) && unlikely(--floating_output_ttl == 0))
341  {
342  waveform_output = 0;
343  }
344  }
345 
346  // The pulse level is defined as (accumulator >> 12) >= pw ? 0xfff : 0x000.
347  // The expression -((accumulator >> 12) >= pw) & 0xfff yields the same
348  // results without any branching (and thus without any pipeline stalls).
349  // NB! This expression relies on that the result of a boolean expression
350  // is either 0 or 1, and furthermore requires two's complement integer.
351  // A few more cycles may be saved by storing the pulse width left shifted
352  // 12 bits, and dropping the and with 0xfff (this is valid since pulse is
353  // used as a bit mask on 12 bit values), yielding the expression
354  // -(accumulator >= pw24). However this only results in negligible savings.
355 
356  // The result of the pulse width compare is delayed one cycle.
357  // Push next pulse level into pulse level pipeline.
358  pulse_output = ((accumulator >> 12) >= pw) ? 0xfff : 0x000;
359 
360  // DAC imperfections are emulated by using waveform_output as an index
361  // into a DAC lookup table. readOSC() uses waveform_output directly.
362  return dac[waveform_output];
363 }
364 
365 } // namespace reSIDfp
366 
367 #endif
368 
369 #endif
Definition: array.h:41
int readFreq() const
Definition: WaveformGenerator.h:262
void writeCONTROL_REG(unsigned char control)
Definition: WaveformGenerator.cpp:135
void writePW_LO(unsigned char pw_lo)
Definition: WaveformGenerator.h:219
void writeFREQ_LO(unsigned char freq_lo)
Definition: WaveformGenerator.h:202
unsigned char readOSC() const
Definition: WaveformGenerator.h:252
bool readTest() const
Definition: WaveformGenerator.h:267
void writeFREQ_HI(unsigned char freq_hi)
Definition: WaveformGenerator.h:209
bool readSync() const
Definition: WaveformGenerator.h:272
WaveformGenerator()
Definition: WaveformGenerator.h:175
int readAccumulator() const
Definition: WaveformGenerator.h:257
short output(const WaveformGenerator *ringModulator)
Definition: WaveformGenerator.h:320
void clock()
Definition: WaveformGenerator.h:283
void reset()
Definition: WaveformGenerator.cpp:195
void setChipModel(ChipModel chipModel)
Definition: WaveformGenerator.cpp:96
void synchronize(WaveformGenerator *syncDest, const WaveformGenerator *syncSource) const
Definition: WaveformGenerator.cpp:124
void writePW_HI(unsigned char pw_hi)
Definition: WaveformGenerator.h:226
Definition: WaveformGenerator.h:87