23 #include "resid-config.h"
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 reset_shift_register();
67 void set_noise_output();
86 cycle_count shift_register_reset;
88 cycle_count shift_pipeline;
92 unsigned short no_noise;
93 unsigned short noise_output;
94 unsigned short no_noise_or_noise_output;
95 unsigned short no_pulse;
96 unsigned short pulse_output;
102 reg12 tri_saw_pipeline;
112 reg12 waveform_output;
114 cycle_count floating_output_ttl;
116 chip_model sid_model;
119 unsigned short* wave;
120 static unsigned short model_wave[2][8][1 << 12];
122 static unsigned short model_dac[2][1 << 12];
135 #if RESID_INLINING || defined(RESID_WAVE_CC)
141 void WaveformGenerator::clock()
143 if (unlikely(test)) {
145 if (unlikely(shift_register_reset) && unlikely(!--shift_register_reset)) {
146 reset_shift_register();
150 pulse_output = 0xfff;
154 reg24 accumulator_next = (accumulator + freq) & 0xffffff;
155 reg24 accumulator_bits_set = ~accumulator & accumulator_next;
156 accumulator = accumulator_next;
159 msb_rising = (accumulator_bits_set & 0x800000) ?
true :
false;
163 if (unlikely(accumulator_bits_set & 0x080000)) {
167 else if (unlikely(shift_pipeline) && !--shift_pipeline) {
168 clock_shift_register();
177 void WaveformGenerator::clock(cycle_count delta_t)
179 if (unlikely(test)) {
181 if (shift_register_reset) {
182 shift_register_reset -= delta_t;
183 if (unlikely(shift_register_reset <= 0)) {
184 reset_shift_register();
189 pulse_output = 0xfff;
193 reg24 delta_accumulator = delta_t*freq;
194 reg24 accumulator_next = (accumulator + delta_accumulator) & 0xffffff;
195 reg24 accumulator_bits_set = ~accumulator & accumulator_next;
196 accumulator = accumulator_next;
199 msb_rising = (accumulator_bits_set & 0x800000) ?
true :
false;
206 reg24 shift_period = 0x100000;
208 while (delta_accumulator) {
209 if (likely(delta_accumulator < shift_period)) {
210 shift_period = delta_accumulator;
213 if (likely(shift_period <= 0x080000)) {
215 if (((accumulator - shift_period) & 0x080000) || !(accumulator & 0x080000))
222 if (((accumulator - shift_period) & 0x080000) && !(accumulator & 0x080000))
231 clock_shift_register();
233 delta_accumulator -= shift_period;
238 pulse_output = (accumulator >> 12) >= pw ? 0xfff : 0x000;
251 void WaveformGenerator::synchronize()
256 if (unlikely(msb_rising) && sync_dest->sync && !(sync && sync_source->msb_rising)) {
257 sync_dest->accumulator = 0;
316 RESID_INLINE
void WaveformGenerator::clock_shift_register()
319 reg24 bit0 = ((shift_register >> 22) ^ (shift_register >> 17)) & 0x1;
320 shift_register = ((shift_register << 1) | bit0) & 0x7fffff;
326 RESID_INLINE
void WaveformGenerator::write_shift_register()
335 ~((1<<20)|(1<<18)|(1<<14)|(1<<11)|(1<<9)|(1<<5)|(1<<2)|(1<<0)) |
336 ((waveform_output & 0x800) << 9) |
337 ((waveform_output & 0x400) << 8) |
338 ((waveform_output & 0x200) << 5) |
339 ((waveform_output & 0x100) << 3) |
340 ((waveform_output & 0x080) << 2) |
341 ((waveform_output & 0x040) >> 1) |
342 ((waveform_output & 0x020) >> 3) |
343 ((waveform_output & 0x010) >> 4);
345 noise_output &= waveform_output;
346 no_noise_or_noise_output = no_noise | noise_output;
349 RESID_INLINE
void WaveformGenerator::reset_shift_register()
351 shift_register = 0x7fffff;
352 shift_register_reset = 0;
358 RESID_INLINE
void WaveformGenerator::set_noise_output()
361 ((shift_register & 0x100000) >> 9) |
362 ((shift_register & 0x040000) >> 8) |
363 ((shift_register & 0x004000) >> 5) |
364 ((shift_register & 0x000800) >> 3) |
365 ((shift_register & 0x000200) >> 2) |
366 ((shift_register & 0x000020) << 1) |
367 ((shift_register & 0x000004) << 3) |
368 ((shift_register & 0x000001) << 4);
370 no_noise_or_noise_output = no_noise | noise_output;
453 void WaveformGenerator::set_waveform_output()
456 if (likely(waveform)) {
459 int ix = (accumulator ^ (~sync_source->accumulator & ring_msb_mask)) >> 12;
461 waveform_output = wave[ix] & (no_pulse | pulse_output) & no_noise_or_noise_output;
466 if ((waveform & 3) && (sid_model == MOS8580))
468 osc3 = tri_saw_pipeline & (no_pulse | pulse_output) & no_noise_or_noise_output;
469 tri_saw_pipeline = wave[ix];
473 osc3 = waveform_output;
476 if ((waveform & 0x2) && unlikely(waveform & 0xd) && (sid_model == MOS6581)) {
479 accumulator &= (waveform_output << 12) | 0x7fffff;
482 if (unlikely(waveform > 0x8) && likely(!test) && likely(shift_pipeline != 1)) {
484 write_shift_register();
489 if (likely(floating_output_ttl) && unlikely(!--floating_output_ttl)) {
506 pulse_output = -((accumulator >> 12) >= pw) & 0xfff;
510 void WaveformGenerator::set_waveform_output(cycle_count delta_t)
513 if (likely(waveform)) {
516 int ix = (accumulator ^ (~sync_source->accumulator & ring_msb_mask)) >> 12;
518 wave[ix] & (no_pulse | pulse_output) & no_noise_or_noise_output;
520 osc3 = waveform_output;
522 if ((waveform & 0x2) && unlikely(waveform & 0xd) && (sid_model == MOS6581)) {
523 accumulator &= (waveform_output << 12) | 0x7fffff;
526 if (unlikely(waveform > 0x8) && likely(!test)) {
530 write_shift_register();
534 if (likely(floating_output_ttl)) {
536 floating_output_ttl -= delta_t;
537 if (unlikely(floating_output_ttl <= 0)) {
538 floating_output_ttl = 0;
576 short WaveformGenerator::output()
580 return model_dac[sid_model][waveform_output];
583 #endif // RESID_INLINING || defined(RESID_WAVE_CC)
587 #endif // not RESID_WAVE_H