Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

sc_logic.h

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-2004 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.3 (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_logic.h -- C++ implementation of logic type. Behaves
00021                 pretty much the same way as HDLs except with 4 values.
00022 
00023   Original Author: Stan Y. Liao, Synopsys, Inc.
00024 
00025  *****************************************************************************/
00026 
00027 /*****************************************************************************
00028 
00029   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00030   changes you are making here.
00031 
00032       Name, Affiliation, Date:
00033   Description of Modification:
00034 
00035  *****************************************************************************/
00036 
00037 #ifndef SC_LOGIC_H
00038 #define SC_LOGIC_H
00039 
00040 
00041 #include <stdio.h>
00042 
00043 #include "systemc/utils/sc_iostream.h"
00044 #include "systemc/kernel/sc_macros.h"
00045 #include "systemc/utils/sc_mempool.h"
00046 #include "systemc/datatypes/bit/sc_bit.h"
00047 
00048 
00049 namespace sc_dt
00050 {
00051 
00052 // classes defined in this module
00053 class sc_logic;
00054 
00055 
00056 // ----------------------------------------------------------------------------
00057 //  ENUM : sc_logic_value_t
00058 //
00059 //  Enumeration of values for sc_logic.
00060 // ----------------------------------------------------------------------------
00061 
00062 enum sc_logic_value_t
00063 {
00064     Log_0 = 0,
00065     Log_1,
00066     Log_Z,
00067     Log_X
00068 };
00069 
00070 
00071 // ----------------------------------------------------------------------------
00072 //  CLASS : sc_logic
00073 //
00074 //  Four-valued logic type.
00075 // ----------------------------------------------------------------------------
00076 
00077 class sc_logic
00078 {
00079     friend class sc_logic_resolve;
00080 
00081 private:
00082 
00083     // support methods
00084 
00085     static void invalid_value( sc_logic_value_t );
00086     static void invalid_value( char );
00087     static void invalid_value( int );
00088 
00089     static sc_logic_value_t to_value( sc_logic_value_t v )
00090   {
00091       if( v < Log_0 || v > Log_X ) {
00092     invalid_value( v );
00093       }
00094       return v;
00095   }
00096 
00097     static sc_logic_value_t to_value( bool b )
00098   { return ( b ? Log_1 : Log_0 ); }
00099 
00100     static sc_logic_value_t to_value( char c )
00101   {
00102       sc_logic_value_t v = char_to_logic[(int)c];
00103       if( v < Log_0 || v > Log_X ) {
00104     invalid_value( c );
00105       }
00106       return v;
00107   }
00108 
00109     static sc_logic_value_t to_value( int i )
00110   {
00111       if( i < 0 || i > 3 ) {
00112     invalid_value( i );
00113       }
00114       return sc_logic_value_t( i );
00115   }
00116 
00117 
00118     void invalid_01() const;
00119 
00120 public:
00121 
00122     // conversion tables
00123 
00124     static const sc_logic_value_t char_to_logic[128];
00125     static const char logic_to_char[4];
00126     static const sc_logic_value_t and_table[4][4];
00127     static const sc_logic_value_t or_table[4][4];
00128     static const sc_logic_value_t xor_table[4][4];
00129     static const sc_logic_value_t not_table[4];
00130 
00131 
00132     // constructors
00133 
00134     sc_logic()
00135   : m_val( Log_X )
00136   {}
00137 
00138     sc_logic( const sc_logic& a )
00139   : m_val( a.m_val )
00140   {}
00141 
00142     sc_logic( sc_logic_value_t v )
00143   : m_val( to_value( v ) )
00144   {}
00145 
00146     explicit sc_logic( bool a )
00147   : m_val( to_value( a ) )
00148   {}
00149 
00150     explicit sc_logic( char a )
00151   : m_val( to_value( a ) )
00152   {}
00153 
00154     explicit sc_logic( int a )
00155   : m_val( to_value( a ) )
00156   {}
00157 
00158     explicit sc_logic( const sc_bit& a )
00159   : m_val( to_value( a.to_bool() ) )
00160   {}
00161 
00162 
00163     // destructor
00164 
00165     ~sc_logic()
00166   {}
00167 
00168 
00169     // assignment operators
00170 
00171     sc_logic& operator = ( const sc_logic& a )
00172   { m_val = a.m_val; return *this; }
00173 
00174     sc_logic& operator = ( sc_logic_value_t v )
00175   { *this = sc_logic( v ); return *this; }
00176 
00177     sc_logic& operator = ( bool a )
00178   { *this = sc_logic( a ); return *this; }
00179 
00180     sc_logic& operator = ( char a )
00181   { *this = sc_logic( a ); return *this; }
00182 
00183     sc_logic& operator = ( int a )
00184   { *this = sc_logic( a ); return *this; }
00185 
00186     sc_logic& operator = ( const sc_bit& a )
00187   { *this = sc_logic( a ); return *this; }
00188 
00189 
00190     // bitwise assignment operators
00191 
00192     sc_logic& operator &= ( const sc_logic& b )
00193   { m_val = and_table[m_val][b.m_val]; return *this; }
00194 
00195     sc_logic& operator &= ( sc_logic_value_t v )
00196   { *this &= sc_logic( v ); return *this; }
00197 
00198     sc_logic& operator &= ( bool b )
00199   { *this &= sc_logic( b ); return *this; }
00200 
00201     sc_logic& operator &= ( char b )
00202   { *this &= sc_logic( b ); return *this; }
00203 
00204     sc_logic& operator &= ( int b )
00205   { *this &= sc_logic( b ); return *this; }
00206 
00207 
00208     sc_logic& operator |= ( const sc_logic& b )
00209         { m_val = or_table[m_val][b.m_val]; return *this; }
00210 
00211     sc_logic& operator |= ( sc_logic_value_t v )
00212   { *this |= sc_logic( v ); return *this; }
00213 
00214     sc_logic& operator |= ( bool b )
00215   { *this |= sc_logic( b ); return *this; }
00216 
00217     sc_logic& operator |= ( char b )
00218   { *this |= sc_logic( b ); return *this; }
00219 
00220     sc_logic& operator |= ( int b )
00221   { *this |= sc_logic( b ); return *this; }
00222 
00223 
00224     sc_logic& operator ^= ( const sc_logic& b )
00225         { m_val = xor_table[m_val][b.m_val]; return *this; }
00226 
00227     sc_logic& operator ^= ( sc_logic_value_t v )
00228   { *this ^= sc_logic( v ); return *this; }
00229 
00230     sc_logic& operator ^= ( bool b )
00231   { *this ^= sc_logic( b ); return *this; }
00232 
00233     sc_logic& operator ^= ( char b )
00234   { *this ^= sc_logic( b ); return *this; }
00235 
00236     sc_logic& operator ^= ( int b )
00237   { *this ^= sc_logic( b ); return *this; }
00238 
00239 
00240     // bitwise operators and functions
00241 
00242     // bitwise complement
00243 
00244     const sc_logic operator ~ () const
00245   { return sc_logic( not_table[m_val] ); }
00246 
00247     sc_logic& b_not()
00248   { m_val = not_table[m_val]; return *this; }
00249 
00250 
00251     // bitwise and
00252 
00253     friend const sc_logic operator & ( const sc_logic& a, const sc_logic& b )
00254   { return sc_logic( and_table[a.m_val][b.m_val] ); }
00255 
00256     friend const sc_logic operator & ( const sc_logic& a, sc_logic_value_t b )
00257   { return ( a & sc_logic( b ) ); }
00258 
00259     friend const sc_logic operator & ( const sc_logic& a, bool b )
00260   { return ( a & sc_logic( b ) ); }
00261 
00262     friend const sc_logic operator & ( const sc_logic& a, char b )
00263   { return ( a & sc_logic( b ) ); }
00264 
00265     friend const sc_logic operator & ( const sc_logic& a, int b )
00266   { return ( a & sc_logic( b ) ); }
00267 
00268     friend const sc_logic operator & ( sc_logic_value_t a, const sc_logic& b )
00269   { return ( sc_logic( a ) & b ); }
00270 
00271     friend const sc_logic operator & ( bool a, const sc_logic& b )
00272   { return ( sc_logic( a ) & b ); }
00273 
00274     friend const sc_logic operator & ( char a, const sc_logic& b )
00275   { return ( sc_logic( a ) & b ); }
00276 
00277     friend const sc_logic operator & ( int a, const sc_logic& b )
00278   { return ( sc_logic( a ) & b ); }
00279 
00280 
00281     // bitwise or
00282 
00283     friend const sc_logic operator | ( const sc_logic& a, const sc_logic& b )
00284   { return sc_logic( or_table[a.m_val][b.m_val] ); }
00285 
00286     friend const sc_logic operator | ( const sc_logic& a, sc_logic_value_t b )
00287   { return ( a | sc_logic( b ) ); }
00288 
00289     friend const sc_logic operator | ( const sc_logic& a, bool b )
00290   { return ( a | sc_logic( b ) ); }
00291 
00292     friend const sc_logic operator | ( const sc_logic& a, char b )
00293   { return ( a | sc_logic( b ) ); }
00294 
00295     friend const sc_logic operator | ( const sc_logic& a, int b )
00296   { return ( a | sc_logic( b ) ); }
00297 
00298     friend const sc_logic operator | ( sc_logic_value_t a, const sc_logic& b )
00299   { return ( sc_logic( a ) | b ); }
00300 
00301     friend const sc_logic operator | ( bool a, const sc_logic& b )
00302   { return ( sc_logic( a ) | b ); }
00303 
00304     friend const sc_logic operator | ( char a, const sc_logic& b )
00305   { return ( sc_logic( a ) | b ); }
00306 
00307     friend const sc_logic operator | ( int a, const sc_logic& b )
00308   { return ( sc_logic( a ) | b ); }
00309 
00310 
00311     // bitwise xor
00312 
00313     friend const sc_logic operator ^ ( const sc_logic& a, const sc_logic& b )
00314   { return sc_logic( xor_table[a.m_val][b.m_val] ); }
00315 
00316     friend const sc_logic operator ^ ( const sc_logic& a, sc_logic_value_t b )
00317   { return ( a ^ sc_logic( b ) ); }
00318 
00319     friend const sc_logic operator ^ ( const sc_logic& a, bool b )
00320   { return ( a ^ sc_logic( b ) ); }
00321 
00322     friend const sc_logic operator ^ ( const sc_logic& a, char b )
00323   { return ( a ^ sc_logic( b ) ); }
00324 
00325     friend const sc_logic operator ^ ( const sc_logic& a, int b )
00326   { return ( a ^ sc_logic( b ) ); }
00327 
00328     friend const sc_logic operator ^ ( sc_logic_value_t a, const sc_logic& b )
00329   { return ( sc_logic( a ) ^ b ); }
00330 
00331     friend const sc_logic operator ^ ( bool a, const sc_logic& b )
00332   { return ( sc_logic( a ) ^ b ); }
00333 
00334     friend const sc_logic operator ^ ( char a, const sc_logic& b )
00335   { return ( sc_logic( a ) ^ b ); }
00336 
00337     friend const sc_logic operator ^ ( int a, const sc_logic& b )
00338   { return ( sc_logic( a ) ^ b ); }
00339 
00340 
00341     // relational operators and functions
00342 
00343     friend bool operator == ( const sc_logic& a, const sc_logic& b )
00344   { return ( (int) a.m_val == b.m_val ); }
00345 
00346     friend bool operator == ( const sc_logic& a, sc_logic_value_t b )
00347   { return ( a == sc_logic( b ) ); }
00348 
00349     friend bool operator == ( const sc_logic& a, bool b )
00350   { return ( a == sc_logic( b ) ); }
00351 
00352     friend bool operator == ( const sc_logic& a, char b )
00353   { return ( a == sc_logic( b ) ); }
00354 
00355     friend bool operator == ( const sc_logic& a, int b )
00356   { return ( a == sc_logic( b ) ); }
00357 
00358     friend bool operator == ( sc_logic_value_t a, const sc_logic& b )
00359   { return ( sc_logic( a ) == b ); }
00360 
00361     friend bool operator == ( bool a, const sc_logic& b )
00362   { return ( sc_logic( a ) == b ); }
00363 
00364     friend bool operator == ( char a, const sc_logic& b )
00365   { return ( sc_logic( a ) == b ); }
00366 
00367     friend bool operator == ( int a, const sc_logic& b )
00368   { return ( sc_logic( a ) == b ); }
00369 
00370 
00371     friend bool operator != ( const sc_logic& a, const sc_logic& b )
00372   { return ( (int) a.m_val != b.m_val ); }
00373 
00374     friend bool operator != ( const sc_logic& a, sc_logic_value_t b )
00375   { return ( a != sc_logic( b ) ); }
00376 
00377     friend bool operator != ( const sc_logic& a, bool b )
00378   { return ( a != sc_logic( b ) ); }
00379 
00380     friend bool operator != ( const sc_logic& a, char b )
00381   { return ( a != sc_logic( b ) ); }
00382 
00383     friend bool operator != ( const sc_logic& a, int b )
00384   { return ( a != sc_logic( b ) ); }
00385 
00386     friend bool operator != ( sc_logic_value_t a, const sc_logic& b )
00387   { return ( sc_logic( a ) != b ); }
00388 
00389     friend bool operator != ( bool a, const sc_logic& b )
00390   { return ( sc_logic( a ) != b ); }
00391 
00392     friend bool operator != ( char a, const sc_logic& b )
00393   { return ( sc_logic( a ) != b ); }
00394 
00395     friend bool operator != ( int a, const sc_logic& b )
00396   { return ( sc_logic( a ) != b ); }
00397 
00398 
00399     // explicit conversions
00400 
00401     sc_logic_value_t value() const
00402   { return m_val; }
00403 
00404 
00405     bool is_01() const
00406   { return ( (int) m_val == Log_0 || (int) m_val == Log_1 ); }
00407 
00408     bool to_bool() const
00409   { if( ! is_01() ) { invalid_01(); } return ( (int) m_val != Log_0 ); }
00410 
00411     char to_char() const
00412   { return logic_to_char[m_val]; }
00413 
00414 
00415     // other methods
00416 
00417     void print( ostream& os = cout ) const
00418   { os << to_char(); }
00419 
00420     void scan( istream& is = cin );
00421 
00422 
00423     // memory (de)allocation
00424 
00425     static void* operator new( size_t, void* p ) // placement new
00426   { return p; }
00427 
00428     static void* operator new( size_t sz )
00429   { return sc_mempool::allocate( sz ); }
00430 
00431     static void operator delete( void* p, size_t sz )
00432   { sc_mempool::release( p, sz ); }
00433 
00434     static void* operator new [] ( size_t sz )
00435   { return sc_mempool::allocate( sz ); }
00436 
00437     static void operator delete [] ( void* p, size_t sz )
00438   { sc_mempool::release( p, sz ); }
00439 
00440 private:
00441 
00442     sc_logic_value_t m_val;
00443 
00444 private:
00445 
00446     // disabled
00447     explicit sc_logic( const char* );
00448     sc_logic& operator = ( const char* );
00449 };
00450 
00451 
00452 // ----------------------------------------------------------------------------
00453 
00454 inline
00455 ostream&
00456 operator << ( ostream& os, const sc_logic& a )
00457 {
00458     a.print( os );
00459     return os;
00460 }
00461 
00462 inline
00463 istream&
00464 operator >> ( istream& is, sc_logic& a )
00465 {
00466     a.scan( is );
00467     return is;
00468 }
00469 
00470 
00471 extern const sc_logic SC_LOGIC_0;
00472 extern const sc_logic SC_LOGIC_1;
00473 extern const sc_logic SC_LOGIC_Z;
00474 extern const sc_logic SC_LOGIC_X;
00475 
00476 // #ifdef SC_DT_DEPRECATED
00477 extern const sc_logic sc_logic_0;
00478 extern const sc_logic sc_logic_1;
00479 extern const sc_logic sc_logic_Z;
00480 extern const sc_logic sc_logic_X;
00481 // #endif
00482 
00483 } // namespace sc_dt
00484 
00485 
00486 #endif

Generated on Fri Jan 14 08:29:03 2005 for SystemC2.1beta11(excludingMSLib)(IncludingSCV)\nProvidedby:www.openverificationfoundation.org by doxygen1.2.18