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