DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
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 along
00016 //  with this program; if not, write to the Free Software Foundation, Inc.,
00017 //  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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   sync_dest = NULL;
00030   waveform = 0;
00031 
00032   set_chip_model(MOS6581);
00033 
00034   reset();
00035 }
00036 
00037 
00038 // ----------------------------------------------------------------------------
00039 // Set sync source.
00040 // ----------------------------------------------------------------------------
00041 void WaveformGenerator::set_sync_source(WaveformGenerator* source)
00042 {
00043   sync_source = source;
00044   source->sync_dest = this;
00045 }
00046 
00047 
00048 // ----------------------------------------------------------------------------
00049 // Set chip model.
00050 // ----------------------------------------------------------------------------
00051 void WaveformGenerator::set_chip_model(chip_model model)
00052 {
00053   if (model == MOS6581) {
00054     wave__ST = wave6581__ST;
00055     wave_P_T = wave6581_P_T;
00056     wave_PS_ = wave6581_PS_;
00057     wave_PST = wave6581_PST;
00058   }
00059   else {
00060     wave__ST = wave8580__ST;
00061     wave_P_T = wave8580_P_T;
00062     wave_PS_ = wave8580_PS_;
00063     wave_PST = wave8580_PST;
00064   }
00065 }
00066 
00067 
00068 // ----------------------------------------------------------------------------
00069 // Register functions.
00070 // ----------------------------------------------------------------------------
00071 void WaveformGenerator::writeFREQ_LO(reg8 freq_lo)
00072 {
00073   freq = (freq & 0xff00) | (freq_lo & 0x00ff);
00074 }
00075 
00076 void WaveformGenerator::writeFREQ_HI(reg8 freq_hi)
00077 {
00078   freq = ((freq_hi << 8) & 0xff00) | (freq & 0x00ff);
00079 }
00080 
00081 void WaveformGenerator::writePW_LO(reg8 pw_lo)
00082 {
00083   pw = (pw & 0xf00) | (pw_lo & 0x0ff);
00084 }
00085 
00086 void WaveformGenerator::writePW_HI(reg8 pw_hi)
00087 {
00088   pw = ((pw_hi << 8) & 0xf00) | (pw & 0x0ff);
00089 }
00090 
00091 void WaveformGenerator::writeCONTROL_REG(reg8 control)
00092 {
00093   waveform = (control >> 4) & 0x0f;
00094   ring_mod = control & 0x04;
00095   sync = control & 0x02;
00096 
00097   reg8 test_next = control & 0x08;
00098 
00099   // Test bit set.
00100   // The accumulator and the shift register are both cleared.
00101   // NB! The shift register is not really cleared immediately. It seems like
00102   // the individual bits in the shift register start to fade down towards
00103   // zero when test is set. All bits reach zero within approximately
00104   // $2000 - $4000 cycles.
00105   // This is not modeled. There should fortunately be little audible output
00106   // from this peculiar behavior.
00107   if (test_next) {
00108     accumulator = 0;
00109     shift_register = 0;
00110   }
00111   // Test bit cleared.
00112   // The accumulator starts counting, and the shift register is reset to
00113   // the value 0x7ffff8.
00114   // NB! The shift register will not actually be set to this exact value if the
00115   // shift register bits have not had time to fade to zero.
00116   // This is not modeled.
00117   else if (test) {
00118     shift_register = 0x7ffff8;
00119   }
00120 
00121   test = test_next;
00122 
00123   // The gate bit is handled by the EnvelopeGenerator.
00124 }
00125 
00126 reg8 WaveformGenerator::readOSC()
00127 {
00128   return output() >> 4;
00129 }
00130 
00131 // ----------------------------------------------------------------------------
00132 // SID reset.
00133 // ----------------------------------------------------------------------------
00134 void WaveformGenerator::reset()
00135 {
00136   accumulator = 0;
00137   shift_register = 0x7ffff8;
00138   freq = 0;
00139   pw = 0;
00140 
00141   test = 0;
00142   ring_mod = 0;
00143   sync = 0;
00144 
00145   msb_rising = false;
00146 }
00147