1 //  ---------------------------------------------------------------------------
2 //  This file is part of reSID, a MOS6581 SID emulator engine.
3 //  Copyright (C) 2010  Dag Lem <resid@nimrod.no>
4 //
5 //  This program is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation; either version 2 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 //  ---------------------------------------------------------------------------
19 
20 #define RESID_WAVE_CC
21 
22 #include "wave.h"
23 #include "dac.h"
24 
25 namespace reSID
26 {
27 
28 // Number of cycles after which the shift register is reset
29 // when the test bit is set.
30 const cycle_count SHIFT_REGISTER_RESET_START_6581 =    9768; // 0x8000
31 const cycle_count SHIFT_REGISTER_RESET_BIT_6581   =    1000;
32 const cycle_count SHIFT_REGISTER_RESET_START_8580 = 2519864; // 0x950000
33 const cycle_count SHIFT_REGISTER_RESET_BIT_8580   =  315000;
34 
35 // Number of cycles after which the waveform output fades to 0 when setting
36 // the waveform register to 0.
37 //
38 // We have two SOAS/C samplings showing that floating DAC
39 // keeps its state for at least 0x14000 cycles.
40 //
41 // This can't be found via sampling OSC3, it seems that
42 // the actual analog output must be sampled and timed.
43 const cycle_count FLOATING_OUTPUT_TTL_START_6581 =  182000;  // ~200ms
44 const cycle_count FLOATING_OUTPUT_TTL_BIT_6581   =    1500;
45 const cycle_count FLOATING_OUTPUT_TTL_START_8580 = 4400000; // ~5s
46 const cycle_count FLOATING_OUTPUT_TTL_BIT_8580   =   50000;
47 
48 // Waveform lookup tables.
49 unsigned short WaveformGenerator::model_wave[2][8][1 << 12] = {
50   {
51     {0},
52     {0},
53     {0},
54 #include "wave6581__ST.h"
55     {0},
56 #include "wave6581_P_T.h"
57 #include "wave6581_PS_.h"
58 #include "wave6581_PST.h"
59   },
60   {
61     {0},
62     {0},
63     {0},
64 #include "wave8580__ST.h"
65     {0},
66 #include "wave8580_P_T.h"
67 #include "wave8580_PS_.h"
68 #include "wave8580_PST.h"
69   }
70 };
71 
72 
73 // DAC lookup tables.
74 unsigned short WaveformGenerator::model_dac[2][1 << 12] = {
75   {0},
76   {0},
77 };
78 
79 
80 // ----------------------------------------------------------------------------
81 // Constructor.
82 // ----------------------------------------------------------------------------
WaveformGenerator()83 WaveformGenerator::WaveformGenerator()
84 {
85   static bool class_init;
86 
87   if (!class_init) {
88     // Calculate tables for normal waveforms.
89     accumulator = 0;
90     for (int i = 0; i < (1 << 12); i++) {
91       reg24 msb = accumulator & 0x800000;
92 
93       // Noise mask, triangle, sawtooth, pulse mask.
94       // The triangle calculation is made branch-free, just for the hell of it.
95       model_wave[0][0][i] = model_wave[1][0][i] = 0xfff;
96       model_wave[0][1][i] = model_wave[1][1][i] = ((accumulator ^ -!!msb) >> 11) & 0xffe;
97       model_wave[0][2][i] = model_wave[1][2][i] = accumulator >> 12;
98       model_wave[0][4][i] = model_wave[1][4][i] = 0xfff;
99 
100       accumulator += 0x1000;
101     }
102 
103     // Build DAC lookup tables for 12-bit DACs.
104     // MOS 6581: 2R/R ~ 2.20, missing termination resistor.
105     build_dac_table(model_dac[0], 12, 2.20, false);
106     // MOS 8580: 2R/R ~ 2.00, correct termination.
107     build_dac_table(model_dac[1], 12, 2.00, true);
108 
109     class_init = true;
110   }
111 
112   sync_source = this;
113 
114   sid_model = MOS6581;
115 
116   // Accumulator's even bits are high on powerup
117   accumulator = 0x555555;
118 
119   tri_saw_pipeline = 0x555;
120 
121   reset();
122 }
123 
124 
125 // ----------------------------------------------------------------------------
126 // Set sync source.
127 // ----------------------------------------------------------------------------
set_sync_source(WaveformGenerator * source)128 void WaveformGenerator::set_sync_source(WaveformGenerator* source)
129 {
130   sync_source = source;
131   source->sync_dest = this;
132 }
133 
134 
135 // ----------------------------------------------------------------------------
136 // Set chip model.
137 // ----------------------------------------------------------------------------
set_chip_model(chip_model model)138 void WaveformGenerator::set_chip_model(chip_model model)
139 {
140   sid_model = model;
141   wave = model_wave[model][waveform & 0x7];
142 }
143 
144 
145 // ----------------------------------------------------------------------------
146 // Register functions.
147 // ----------------------------------------------------------------------------
writeFREQ_LO(reg8 freq_lo)148 void WaveformGenerator::writeFREQ_LO(reg8 freq_lo)
149 {
150   freq = (freq & 0xff00) | (freq_lo & 0x00ff);
151 }
152 
writeFREQ_HI(reg8 freq_hi)153 void WaveformGenerator::writeFREQ_HI(reg8 freq_hi)
154 {
155   freq = ((freq_hi << 8) & 0xff00) | (freq & 0x00ff);
156 }
157 
writePW_LO(reg8 pw_lo)158 void WaveformGenerator::writePW_LO(reg8 pw_lo)
159 {
160   pw = (pw & 0xf00) | (pw_lo & 0x0ff);
161   // Push next pulse level into pulse level pipeline.
162   pulse_output = (accumulator >> 12) >= pw ? 0xfff : 0x000;
163 }
164 
writePW_HI(reg8 pw_hi)165 void WaveformGenerator::writePW_HI(reg8 pw_hi)
166 {
167   pw = ((pw_hi << 8) & 0xf00) | (pw & 0x0ff);
168   // Push next pulse level into pulse level pipeline.
169   pulse_output = (accumulator >> 12) >= pw ? 0xfff : 0x000;
170 }
171 
do_pre_writeback(reg8 waveform_prev,reg8 waveform,bool is6581)172 bool do_pre_writeback(reg8 waveform_prev, reg8 waveform, bool is6581)
173 {
174     // no writeback without combined waveforms
175     if (likely(waveform_prev <= 0x8))
176         return false;
177     // This need more investigation
178     if (waveform == 8)
179         return false;
180     if (waveform_prev == 0xc)
181         return false;
182     // What's happening here?
183     if (is6581 &&
184             ((((waveform_prev & 0x3) == 0x1) && ((waveform & 0x3) == 0x2))
185             || (((waveform_prev & 0x3) == 0x2) && ((waveform & 0x3) == 0x1))))
186         return false;
187     // ok do the writeback
188     return true;
189 }
190 
writeCONTROL_REG(reg8 control)191 void WaveformGenerator::writeCONTROL_REG(reg8 control)
192 {
193   reg8 waveform_prev = waveform;
194   reg8 test_prev = test;
195   waveform = (control >> 4) & 0x0f;
196   test = control & 0x08;
197   ring_mod = control & 0x04;
198   sync = control & 0x02;
199 
200   // Set up waveform table.
201   wave = model_wave[sid_model][waveform & 0x7];
202 
203   // Substitution of accumulator MSB when sawtooth = 0, ring_mod = 1.
204   ring_msb_mask = ((~control >> 5) & (control >> 2) & 0x1) << 23;
205 
206   // no_noise and no_pulse are used in set_waveform_output() as bitmasks to
207   // only let the noise or pulse influence the output when the noise or pulse
208   // waveforms are selected.
209   no_noise = waveform & 0x8 ? 0x000 : 0xfff;
210   no_noise_or_noise_output = no_noise | noise_output;
211   no_pulse = waveform & 0x4 ? 0x000 : 0xfff;
212 
213   // Test bit rising.
214   // The accumulator is cleared, while the the shift register is prepared for
215   // shifting by interconnecting the register bits. The internal SRAM cells
216   // start to slowly rise up towards one. The SRAM cells reach one within
217   // approximately $8000 cycles, yielding a shift register value of
218   // 0x7fffff.
219   if (!test_prev && test) {
220     // Reset accumulator.
221     accumulator = 0;
222 
223     // Flush shift pipeline.
224     shift_pipeline = 0;
225 
226     // Set reset time for shift register.
227     shift_register_reset = (sid_model == MOS6581) ? SHIFT_REGISTER_RESET_START_6581 : SHIFT_REGISTER_RESET_START_8580;
228 
229     // The test bit sets pulse high.
230     pulse_output = 0xfff;
231   }
232   else if (test_prev && !test) {
233     // When the test bit is falling, the second phase of the shift is
234     // completed by enabling SRAM write.
235 
236     // During first phase of the shift the bits are interconnected
237     // and the output of each bit is latched into the following.
238     // The output may overwrite the latched value.
239     if (do_pre_writeback(waveform_prev, waveform, sid_model == MOS6581)) {
240         write_shift_register();
241     }
242 
243     // bit0 = (bit22 | test) ^ bit17 = 1 ^ bit17 = ~bit17
244     reg24 bit0 = (~shift_register >> 17) & 0x1;
245     shift_register = ((shift_register << 1) | bit0) & 0x7fffff;
246 
247     // Set new noise waveform output.
248     set_noise_output();
249   }
250 
251   if (waveform) {
252     // Set new waveform output.
253     set_waveform_output();
254   }
255   else if (waveform_prev) {
256     // Change to floating DAC input.
257     // Reset fading time for floating DAC input.
258     floating_output_ttl = (sid_model == MOS6581) ? FLOATING_OUTPUT_TTL_START_6581 : FLOATING_OUTPUT_TTL_START_8580;
259   }
260 
261   // The gate bit is handled by the EnvelopeGenerator.
262 }
263 
wave_bitfade()264 void WaveformGenerator::wave_bitfade()
265 {
266   waveform_output &= waveform_output >> 1;
267   osc3 = waveform_output;
268   if (waveform_output != 0)
269     floating_output_ttl = (sid_model == MOS6581) ? FLOATING_OUTPUT_TTL_BIT_6581 : FLOATING_OUTPUT_TTL_BIT_8580;
270 }
271 
shiftreg_bitfade()272 void WaveformGenerator::shiftreg_bitfade()
273 {
274   shift_register |= 1;
275   shift_register |= shift_register << 1;
276 
277   // New noise waveform output.
278   set_noise_output();
279   if (shift_register != 0x7fffff)
280     shift_register_reset = (sid_model == MOS6581) ? SHIFT_REGISTER_RESET_BIT_6581 : SHIFT_REGISTER_RESET_BIT_8580;
281 }
282 
readOSC()283 reg8 WaveformGenerator::readOSC()
284 {
285   return osc3 >> 4;
286 }
287 
288 // ----------------------------------------------------------------------------
289 // SID reset.
290 // ----------------------------------------------------------------------------
reset()291 void WaveformGenerator::reset()
292 {
293   // accumulator is not changed on reset
294   freq = 0;
295   pw = 0;
296 
297   msb_rising = false;
298 
299   waveform = 0;
300   test = 0;
301   ring_mod = 0;
302   sync = 0;
303 
304   wave = model_wave[sid_model][0];
305 
306   ring_msb_mask = 0;
307   no_noise = 0xfff;
308   no_pulse = 0xfff;
309   pulse_output = 0xfff;
310 
311   // reset shift register
312   // when reset is released the shift register is clocked once
313   shift_register = 0x7ffffe;
314   shift_register_reset = 0;
315   set_noise_output();
316 
317   shift_pipeline = 0;
318 
319   waveform_output = 0;
320   osc3 = 0;
321   floating_output_ttl = 0;
322 }
323 
324 } // namespace reSID
325