sc_lv_base.cpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002 
00003   The following code is derived, directly or indirectly, from the SystemC
00004   source code Copyright (c) 1996-2006 by all Contributors.
00005   All Rights reserved.
00006 
00007   The contents of this file are subject to the restrictions and limitations
00008   set forth in the SystemC Open Source License Version 2.4 (the "License");
00009   You may not use this file except in compliance with such restrictions and
00010   limitations. You may obtain instructions on how to receive a copy of the
00011   License at http://www.systemc.org/. Software distributed by Contributors
00012   under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
00013   ANY KIND, either express or implied. See the License for the specific
00014   language governing rights and limitations under the License.
00015 
00016  *****************************************************************************/
00017 
00018 /*****************************************************************************
00019 
00020   sc_lv_base.cpp -- Arbitrary size logic vector class.
00021 
00022   Original Author: Gene Bushuyev, Synopsys, Inc.
00023 
00024  *****************************************************************************/
00025 
00026 /*****************************************************************************
00027 
00028   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00029   changes you are making here.
00030 
00031       Name, Affiliation, Date:
00032   Description of Modification:
00033 
00034  *****************************************************************************/
00035 
00036 
00037 // $Log: sc_lv_base.cpp,v $
00038 // Revision 1.1.1.1  2006/12/15 20:31:36  acg
00039 // SystemC 2.2
00040 //
00041 // Revision 1.3  2006/01/13 18:53:53  acg
00042 // Andy Goodrich: added $Log command so that CVS comments are reproduced in
00043 // the source.
00044 //
00045 
00046 #include "sysc/datatypes/bit/sc_bit_ids.h"
00047 #include "sysc/datatypes/bit/sc_lv_base.h"
00048 
00049 
00050 namespace sc_dt
00051 {
00052 
00053 // ----------------------------------------------------------------------------
00054 //  CLASS : sc_lv_base
00055 //
00056 //  Arbitrary size logic vector base class.
00057 // ----------------------------------------------------------------------------
00058 
00059 static const sc_digit data_array[] =
00060     { SC_DIGIT_ZERO, ~SC_DIGIT_ZERO, SC_DIGIT_ZERO, ~SC_DIGIT_ZERO };
00061 
00062 static const sc_digit ctrl_array[] =
00063     { SC_DIGIT_ZERO, SC_DIGIT_ZERO, ~SC_DIGIT_ZERO, ~SC_DIGIT_ZERO };
00064 
00065 
00066 void
00067 sc_lv_base::init( int length_, const sc_logic& init_value )
00068 {
00069     // check the length
00070     if( length_ <= 0 ) {
00071     SC_REPORT_ERROR( sc_core::SC_ID_ZERO_LENGTH_, 0 );
00072     }
00073     // allocate memory for the data and control words
00074     m_len = length_;
00075     m_size = (m_len - 1) / SC_DIGIT_SIZE + 1;
00076     m_data = new sc_digit[m_size * 2];
00077     m_ctrl = m_data + m_size;
00078     // initialize the bits to 'init_value'
00079     sc_digit dw = data_array[init_value.value()];
00080     sc_digit cw = ctrl_array[init_value.value()];
00081     int sz = m_size;
00082     for( int i = 0; i < sz; ++ i ) {
00083     m_data[i] = dw;
00084     m_ctrl[i] = cw;
00085     }
00086     clean_tail();
00087 }
00088 
00089 
00090 void
00091 sc_lv_base::assign_from_string( const std::string& s )
00092 {
00093     // s must have been converted to bin
00094     int len = m_len;
00095     int s_len = s.length() - 1;
00096     int min_len = sc_min( len, s_len );
00097     int i = 0;
00098     for( ; i < min_len; ++ i ) {
00099     char c = s[s_len - i - 1];
00100     set_bit( i, sc_logic::char_to_logic[(int)c] );
00101     }
00102     // if formatted, fill the rest with sign(s), otherwise fill with zeros
00103     sc_logic_value_t fill = (s[s_len] == 'F' ? sc_logic_value_t( s[0] - '0' )
00104                                      : sc_logic_value_t( 0 ));
00105     for( ; i < len; ++ i ) {
00106     set_bit( i, fill );
00107     }
00108 }
00109 
00110 
00111 // constructors
00112 
00113 sc_lv_base::sc_lv_base( const char* a )
00114     : m_len( 0 ), m_size( 0 ), m_data( 0 ), m_ctrl( 0 )
00115 {
00116     std::string s = convert_to_bin( a );
00117     init( s.length() - 1 );
00118     assign_from_string( s );
00119 }
00120 
00121 sc_lv_base::sc_lv_base( const char* a, int length_ )
00122     : m_len( 0 ), m_size( 0 ), m_data( 0 ), m_ctrl( 0 )
00123 {
00124     init( length_ );
00125     assign_from_string( convert_to_bin( a ) );
00126 }
00127 
00128 sc_lv_base::sc_lv_base( const sc_lv_base& a )
00129     : m_len( a.m_len ),
00130       m_size( a.m_size ),
00131       m_data( new sc_digit[m_size * 2] ),
00132       m_ctrl( m_data + m_size )
00133 {
00134     // copy the bits
00135     int sz = m_size;
00136     for( int i = 0; i < sz; ++ i ) {
00137     m_data[i] = a.m_data[i];
00138     m_ctrl[i] = a.m_ctrl[i];
00139     }
00140 }
00141 
00142 
00143 // assignment operators
00144 
00145 sc_lv_base&
00146 sc_lv_base::operator = ( const char* a )
00147 {
00148     assign_from_string( convert_to_bin( a ) );
00149     return *this;
00150 }
00151 
00152 
00153 // returns true if logic vector contains only 0's and 1's
00154 
00155 bool
00156 sc_lv_base::is_01() const
00157 {
00158     int sz = m_size;
00159     for( int i = 0; i < sz; ++ i ) {
00160     if( m_ctrl[i] != 0 ) {
00161         return false;
00162     }
00163     }
00164     return true;
00165 }
00166 
00167 } // namespace sc_dt

Generated on Wed Jan 21 15:32:07 2009 for SystemC by  doxygen 1.5.5