23#include "resid-config.h"
36class WaveformGenerator
41 void set_sync_source(WaveformGenerator*);
42 void set_chip_model(chip_model model);
45 void clock(cycle_count delta_t);
49 void writeFREQ_LO(reg8);
50 void writeFREQ_HI(reg8);
51 void writePW_LO(reg8);
52 void writePW_HI(reg8);
53 void writeCONTROL_REG(reg8);
60 void set_waveform_output();
61 void set_waveform_output(cycle_count delta_t);
64 void clock_shift_register();
65 void write_shift_register();
66 void set_noise_output();
68 void shiftreg_bitfade();
70 const WaveformGenerator* sync_source;
71 WaveformGenerator* sync_dest;
87 cycle_count shift_register_reset;
89 cycle_count shift_pipeline;
93 unsigned short no_noise;
94 unsigned short noise_output;
95 unsigned short no_noise_or_noise_output;
96 unsigned short no_pulse;
97 unsigned short pulse_output;
103 reg12 tri_saw_pipeline;
113 reg12 waveform_output;
115 cycle_count floating_output_ttl;
117 chip_model sid_model;
120 unsigned short* wave;
121 static unsigned short model_wave[2][8][1 << 12];
123 static unsigned short model_dac[2][1 << 12];
136#if RESID_INLINING || defined(RESID_WAVE_CC)
142void WaveformGenerator::clock()
144 if (unlikely(test)) {
146 if (unlikely(shift_register_reset) && unlikely(!--shift_register_reset)) {
151 pulse_output = 0xfff;
155 reg24 accumulator_next = (accumulator + freq) & 0xffffff;
156 reg24 accumulator_bits_set = ~accumulator & accumulator_next;
157 accumulator = accumulator_next;
160 msb_rising = (accumulator_bits_set & 0x800000) ?
true : false;
164 if (unlikely(accumulator_bits_set & 0x080000)) {
168 else if (unlikely(shift_pipeline) && !--shift_pipeline) {
169 clock_shift_register();
178void WaveformGenerator::clock(cycle_count delta_t)
180 if (unlikely(test)) {
182 if (shift_register_reset) {
183 shift_register_reset -= delta_t;
184 if (unlikely(shift_register_reset <= 0)) {
185 shift_register = 0x7fffff;
186 shift_register_reset = 0;
194 pulse_output = 0xfff;
198 reg24 delta_accumulator = delta_t*freq;
199 reg24 accumulator_next = (accumulator + delta_accumulator) & 0xffffff;
200 reg24 accumulator_bits_set = ~accumulator & accumulator_next;
201 accumulator = accumulator_next;
204 msb_rising = (accumulator_bits_set & 0x800000) ?
true : false;
211 reg24 shift_period = 0x100000;
213 while (delta_accumulator) {
214 if (likely(delta_accumulator < shift_period)) {
215 shift_period = delta_accumulator;
218 if (likely(shift_period <= 0x080000)) {
220 if (((accumulator - shift_period) & 0x080000) || !(accumulator & 0x080000))
227 if (((accumulator - shift_period) & 0x080000) && !(accumulator & 0x080000))
236 clock_shift_register();
238 delta_accumulator -= shift_period;
243 pulse_output = (accumulator >> 12) >= pw ? 0xfff : 0x000;
256void WaveformGenerator::synchronize()
261 if (unlikely(msb_rising) && sync_dest->sync && !(sync && sync_source->msb_rising)) {
262 sync_dest->accumulator = 0;
321RESID_INLINE
void WaveformGenerator::clock_shift_register()
324 reg24 bit0 = ((shift_register >> 22) ^ (shift_register >> 17)) & 0x1;
325 shift_register = ((shift_register << 1) | bit0) & 0x7fffff;
331RESID_INLINE
void WaveformGenerator::write_shift_register()
340 ~((1<<20)|(1<<18)|(1<<14)|(1<<11)|(1<<9)|(1<<5)|(1<<2)|(1<<0)) |
341 ((waveform_output & 0x800) << 9) |
342 ((waveform_output & 0x400) << 8) |
343 ((waveform_output & 0x200) << 5) |
344 ((waveform_output & 0x100) << 3) |
345 ((waveform_output & 0x080) << 2) |
346 ((waveform_output & 0x040) >> 1) |
347 ((waveform_output & 0x020) >> 3) |
348 ((waveform_output & 0x010) >> 4);
350 noise_output &= waveform_output;
351 no_noise_or_noise_output = no_noise | noise_output;
354RESID_INLINE
void WaveformGenerator::set_noise_output()
356 noise_output = (
unsigned short)(((shift_register & 0x100000) >> 9) |
357 ((shift_register & 0x040000) >> 8) |
358 ((shift_register & 0x004000) >> 5) |
359 ((shift_register & 0x000800) >> 3) |
360 ((shift_register & 0x000200) >> 2) |
361 ((shift_register & 0x000020) << 1) |
362 ((shift_register & 0x000004) << 3) |
363 ((shift_register & 0x000001) << 4) );
365 no_noise_or_noise_output = no_noise | noise_output;
447static reg12 noise_pulse6581(reg12 noise)
449 return (noise < 0xf00) ? 0x000 : noise & (noise<<1) & (noise<<2);
452static reg12 noise_pulse8580(reg12 noise)
454 return (noise < 0xfc0) ? noise & (noise << 1) : 0xfc0;
458void WaveformGenerator::set_waveform_output()
461 if (likely(waveform)) {
464 int ix = (accumulator ^ (~sync_source->accumulator & ring_msb_mask)) >> 12;
466 waveform_output = wave[ix] & (no_pulse | pulse_output) & no_noise_or_noise_output;
468 if (unlikely((waveform & 0xc) == 0xc))
470 waveform_output = (sid_model == MOS6581) ?
471 noise_pulse6581(waveform_output) : noise_pulse8580(waveform_output);
477 if ((waveform & 3) && (sid_model == MOS8580))
479 osc3 = tri_saw_pipeline & (no_pulse | pulse_output) & no_noise_or_noise_output;
480 tri_saw_pipeline = wave[ix];
484 osc3 = waveform_output;
487 if ((waveform & 0x2) && unlikely(waveform & 0xd) && (sid_model == MOS6581)) {
490 accumulator &= (waveform_output << 12) | 0x7fffff;
493 if (unlikely(waveform > 0x8) && likely(!test) && likely(shift_pipeline != 1)) {
495 write_shift_register();
500 if (likely(floating_output_ttl) && unlikely(!--floating_output_ttl)) {
517 pulse_output = -((accumulator >> 12) >= pw) & 0xfff;
521void WaveformGenerator::set_waveform_output(cycle_count delta_t)
524 if (likely(waveform)) {
527 int ix = (accumulator ^ (~sync_source->accumulator & ring_msb_mask)) >> 12;
529 wave[ix] & (no_pulse | pulse_output) & no_noise_or_noise_output;
531 osc3 = waveform_output;
533 if ((waveform & 0x2) && unlikely(waveform & 0xd) && (sid_model == MOS6581)) {
534 accumulator &= (waveform_output << 12) | 0x7fffff;
537 if (unlikely(waveform > 0x8) && likely(!test)) {
541 write_shift_register();
545 if (likely(floating_output_ttl)) {
547 floating_output_ttl -= delta_t;
548 if (unlikely(floating_output_ttl <= 0)) {
549 floating_output_ttl = 0;
550 osc3 = waveform_output = 0;
587short WaveformGenerator::output()
591 return model_dac[sid_model][waveform_output];