DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
src/hardware/reSID/wave.cpp
00001 //  ---------------------------------------------------------------------------
00002 //  This file is part of reSID, a MOS6581 SID emulator engine.
00003 //  Copyright (C) 2004  Dag Lem <resid@nimrod.no>
00004 //
00005 //  This program is free software; you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation; either version 2 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program; if not, write to the Free Software
00017 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //  ---------------------------------------------------------------------------
00019 
00020 #define __WAVE_CC__
00021 #include "wave.h"
00022 
00023 // ----------------------------------------------------------------------------
00024 // Constructor.
00025 // ----------------------------------------------------------------------------
00026 WaveformGenerator::WaveformGenerator()
00027 {
00028   sync_source = this;
00029 
00030   set_chip_model(MOS6581);
00031 
00032   reset();
00033 }
00034 
00035 
00036 // ----------------------------------------------------------------------------
00037 // Set sync source.
00038 // ----------------------------------------------------------------------------
00039 void WaveformGenerator::set_sync_source(WaveformGenerator* source)
00040 {
00041   sync_source = source;
00042   source->sync_dest = this;
00043 }
00044 
00045 
00046 // ----------------------------------------------------------------------------
00047 // Set chip model.
00048 // ----------------------------------------------------------------------------
00049 void WaveformGenerator::set_chip_model(chip_model model)
00050 {
00051   if (model == MOS6581) {
00052     wave__ST = wave6581__ST;
00053     wave_P_T = wave6581_P_T;
00054     wave_PS_ = wave6581_PS_;
00055     wave_PST = wave6581_PST;
00056   }
00057   else {
00058     wave__ST = wave8580__ST;
00059     wave_P_T = wave8580_P_T;
00060     wave_PS_ = wave8580_PS_;
00061     wave_PST = wave8580_PST;
00062   }
00063 }
00064 
00065 
00066 // ----------------------------------------------------------------------------
00067 // Register functions.
00068 // ----------------------------------------------------------------------------
00069 void WaveformGenerator::writeFREQ_LO(reg8 freq_lo)
00070 {
00071   freq = (freq & 0xff00) | (freq_lo & 0x00ff);
00072 }
00073 
00074 void WaveformGenerator::writeFREQ_HI(reg8 freq_hi)
00075 {
00076   freq = ((freq_hi << 8) & 0xff00) | (freq & 0x00ff);
00077 }
00078 
00079 void WaveformGenerator::writePW_LO(reg8 pw_lo)
00080 {
00081   pw = (pw & 0xf00) | (pw_lo & 0x0ff);
00082 }
00083 
00084 void WaveformGenerator::writePW_HI(reg8 pw_hi)
00085 {
00086   pw = ((pw_hi << 8) & 0xf00) | (pw & 0x0ff);
00087 }
00088 
00089 void WaveformGenerator::writeCONTROL_REG(reg8 control)
00090 {
00091   waveform = (control >> 4) & 0x0f;
00092   ring_mod = control & 0x04;
00093   sync = control & 0x02;
00094 
00095   reg8 test_next = control & 0x08;
00096 
00097   // Test bit set.
00098   // The accumulator and the shift register are both cleared.
00099   // NB! The shift register is not really cleared immediately. It seems like
00100   // the individual bits in the shift register start to fade down towards
00101   // zero when test is set. All bits reach zero within approximately
00102   // $2000 - $4000 cycles.
00103   // This is not modeled. There should fortunately be little audible output
00104   // from this peculiar behavior.
00105   if (test_next) {
00106     accumulator = 0;
00107     shift_register = 0;
00108   }
00109   // Test bit cleared.
00110   // The accumulator starts counting, and the shift register is reset to
00111   // the value 0x7ffff8.
00112   // NB! The shift register will not actually be set to this exact value if the
00113   // shift register bits have not had time to fade to zero.
00114   // This is not modeled.
00115   else if (test) {
00116     shift_register = 0x7ffff8;
00117   }
00118 
00119   test = test_next;
00120 
00121   // The gate bit is handled by the EnvelopeGenerator.
00122 }
00123 
00124 reg8 WaveformGenerator::readOSC()
00125 {
00126   return output() >> 4;
00127 }
00128 
00129 // ----------------------------------------------------------------------------
00130 // SID reset.
00131 // ----------------------------------------------------------------------------
00132 void WaveformGenerator::reset()
00133 {
00134   accumulator = 0;
00135   shift_register = 0x7ffff8;
00136   freq = 0;
00137   pw = 0;
00138 
00139   test = 0;
00140   ring_mod = 0;
00141   sync = 0;
00142 
00143   msb_rising = false;
00144 }
00145