1 //  ---------------------------------------------------------------------------
2 //  This file is part of reSID, a MOS6581 SID emulator engine.
3 //  Copyright (C) 2004  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 __WAVE_CC__
21 #include "wave.h"
22 
23 // ----------------------------------------------------------------------------
24 // Constructor.
25 // ----------------------------------------------------------------------------
WaveformGenerator()26 WaveformGenerator::WaveformGenerator()
27 {
28   sync_source = this;
29 
30   set_chip_model(MOS6581);
31 
32   reset();
33 }
34 
35 
36 // ----------------------------------------------------------------------------
37 // Set sync source.
38 // ----------------------------------------------------------------------------
set_sync_source(WaveformGenerator * source)39 void WaveformGenerator::set_sync_source(WaveformGenerator* source)
40 {
41   sync_source = source;
42   source->sync_dest = this;
43 }
44 
45 
46 // ----------------------------------------------------------------------------
47 // Set chip model.
48 // ----------------------------------------------------------------------------
set_chip_model(chip_model model)49 void WaveformGenerator::set_chip_model(chip_model model)
50 {
51   if (model == MOS6581) {
52     wave__ST = wave6581__ST;
53     wave_P_T = wave6581_P_T;
54     wave_PS_ = wave6581_PS_;
55     wave_PST = wave6581_PST;
56   }
57   else {
58     wave__ST = wave8580__ST;
59     wave_P_T = wave8580_P_T;
60     wave_PS_ = wave8580_PS_;
61     wave_PST = wave8580_PST;
62   }
63 }
64 
65 
66 // ----------------------------------------------------------------------------
67 // Register functions.
68 // ----------------------------------------------------------------------------
writeFREQ_LO(reg8 freq_lo)69 void WaveformGenerator::writeFREQ_LO(reg8 freq_lo)
70 {
71   freq = ( freq & 0xff00 ) | ( freq_lo & 0x00ff );
72 }
73 
writeFREQ_HI(reg8 freq_hi)74 void WaveformGenerator::writeFREQ_HI(reg8 freq_hi)
75 {
76   freq = ( (freq_hi << 8) & 0xff00 ) | ( freq & 0x00ff );
77 }
78 
writePW_LO(reg8 pw_lo)79 void WaveformGenerator::writePW_LO(reg8 pw_lo)
80 {
81   pw = ( pw & 0xf00 ) | ( pw_lo & 0x0ff );
82 }
83 
writePW_HI(reg8 pw_hi)84 void WaveformGenerator::writePW_HI(reg8 pw_hi)
85 {
86   pw = ( (pw_hi << 8) & 0xf00 ) | ( pw & 0x0ff );
87 }
88 
writeCONTROL_REG(reg8 control)89 void WaveformGenerator::writeCONTROL_REG(reg8 control)
90 {
91   waveform = (control >> 4) & 0x0f;
92   ring_mod = control & 0x04;
93   sync = control & 0x02;
94 
95   reg8 test_next = control & 0x08;
96 
97   // Test bit set.
98   // The accumulator and the shift register are both cleared.
99   // NB! The shift register is not really cleared immediately. It seems like
100   // the individual bits in the shift register start to fade down towards
101   // zero when test is set. All bits reach zero within approximately
102   // $2000 - $4000 cycles.
103   // This is not modeled. There should fortunately be little audible output
104   // from this peculiar behavior.
105   if (test_next) {
106     accumulator = 0;
107     shift_register = 0;
108   }
109   // Test bit cleared.
110   // The accumulator starts counting, and the shift register is reset to
111   // the value 0x7ffff8.
112   // NB! The shift register will not actually be set to this exact value if the
113   // shift register bits have not had time to fade to zero.
114   // This is not modeled.
115   else if (test) {
116     shift_register = 0x7ffff8;
117   }
118 
119   test = test_next;
120 
121   // The gate bit is handled by the EnvelopeGenerator.
122 }
123 
readOSC()124 reg8 WaveformGenerator::readOSC()
125 {
126   return output() >> 4;
127 }
128 
129 // ----------------------------------------------------------------------------
130 // SID reset.
131 // ----------------------------------------------------------------------------
reset()132 void WaveformGenerator::reset()
133 {
134   accumulator = 0;
135   shift_register = 0x7ffff8;
136   freq = 0;
137   pw = 0;
138 
139   test = 0;
140   ring_mod = 0;
141   sync = 0;
142 
143   msb_rising = false;
144 }
145