src/sysc/datatypes/bit/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-2005 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_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 <cstdio>
00042 
00043 #include "sysc/utils/sc_iostream.h"
00044 #include "sysc/kernel/sc_macros.h"
00045 #include "sysc/utils/sc_mempool.h"
00046 #include "sysc/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;
00103             unsigned int index = (int)c;
00104             if ( index > 127 )
00105             {
00106                 invalid_value(c);
00107                 v = Log_X;
00108             }
00109             else
00110             {
00111                 v = char_to_logic[index];
00112                 if( v < Log_0 || v > Log_X ) {
00113                     invalid_value( c );
00114                 }
00115             }
00116             return v;
00117         }
00118 
00119     static sc_logic_value_t to_value( int i )
00120         {
00121             if( i < 0 || i > 3 ) {
00122                 invalid_value( i );
00123             }
00124             return sc_logic_value_t( i );
00125         }
00126 
00127 
00128     void invalid_01() const;
00129 
00130 public:
00131 
00132     // conversion tables
00133 
00134     static const sc_logic_value_t char_to_logic[128];
00135     static const char logic_to_char[4];
00136     static const sc_logic_value_t and_table[4][4];
00137     static const sc_logic_value_t or_table[4][4];
00138     static const sc_logic_value_t xor_table[4][4];
00139     static const sc_logic_value_t not_table[4];
00140 
00141 
00142     // constructors
00143 
00144     sc_logic()
00145         : m_val( Log_X )
00146         {}
00147 
00148     sc_logic( const sc_logic& a )
00149         : m_val( a.m_val )
00150         {}
00151 
00152     sc_logic( sc_logic_value_t v )
00153         : m_val( to_value( v ) )
00154         {}
00155 
00156     explicit sc_logic( bool a )
00157         : m_val( to_value( a ) )
00158         {}
00159 
00160     explicit sc_logic( char a )
00161         : m_val( to_value( a ) )
00162         {}
00163 
00164     explicit sc_logic( int a )
00165         : m_val( to_value( a ) )
00166         {}
00167 
00168     explicit sc_logic( const sc_bit& a )
00169         : m_val( to_value( a.to_bool() ) )
00170         {}
00171 
00172 
00173     // destructor
00174 
00175     ~sc_logic()
00176         {}
00177 
00178 
00179     // assignment operators
00180 
00181     sc_logic& operator = ( const sc_logic& a )
00182         { m_val = a.m_val; return *this; }
00183 
00184     sc_logic& operator = ( sc_logic_value_t v )
00185         { *this = sc_logic( v ); return *this; }
00186 
00187     sc_logic& operator = ( bool a )
00188         { *this = sc_logic( a ); return *this; }
00189 
00190     sc_logic& operator = ( char a )
00191         { *this = sc_logic( a ); return *this; }
00192 
00193     sc_logic& operator = ( int a )
00194         { *this = sc_logic( a ); return *this; }
00195 
00196     sc_logic& operator = ( const sc_bit& a )
00197         { *this = sc_logic( a ); return *this; }
00198 
00199 
00200     // bitwise assignment operators
00201 
00202     sc_logic& operator &= ( const sc_logic& b )
00203         { m_val = and_table[m_val][b.m_val]; return *this; }
00204 
00205     sc_logic& operator &= ( sc_logic_value_t v )
00206         { *this &= sc_logic( v ); return *this; }
00207 
00208     sc_logic& operator &= ( bool b )
00209         { *this &= sc_logic( b ); return *this; }
00210 
00211     sc_logic& operator &= ( char b )
00212         { *this &= sc_logic( b ); return *this; }
00213 
00214     sc_logic& operator &= ( int b )
00215         { *this &= sc_logic( b ); return *this; }
00216 
00217 
00218     sc_logic& operator |= ( const sc_logic& b )
00219         { m_val = or_table[m_val][b.m_val]; return *this; }
00220 
00221     sc_logic& operator |= ( sc_logic_value_t v )
00222         { *this |= sc_logic( v ); return *this; }
00223 
00224     sc_logic& operator |= ( bool b )
00225         { *this |= sc_logic( b ); return *this; }
00226 
00227     sc_logic& operator |= ( char b )
00228         { *this |= sc_logic( b ); return *this; }
00229 
00230     sc_logic& operator |= ( int b )
00231         { *this |= sc_logic( b ); return *this; }
00232 
00233 
00234     sc_logic& operator ^= ( const sc_logic& b )
00235         { m_val = xor_table[m_val][b.m_val]; return *this; }
00236 
00237     sc_logic& operator ^= ( sc_logic_value_t v )
00238         { *this ^= sc_logic( v ); return *this; }
00239 
00240     sc_logic& operator ^= ( bool b )
00241         { *this ^= sc_logic( b ); return *this; }
00242 
00243     sc_logic& operator ^= ( char b )
00244         { *this ^= sc_logic( b ); return *this; }
00245 
00246     sc_logic& operator ^= ( int b )
00247         { *this ^= sc_logic( b ); return *this; }
00248 
00249 
00250     // bitwise operators and functions
00251 
00252     // bitwise complement
00253 
00254     const sc_logic operator ~ () const
00255         { return sc_logic( not_table[m_val] ); }
00256 
00257     sc_logic& b_not()
00258         { m_val = not_table[m_val]; return *this; }
00259 
00260 
00261     // bitwise and
00262 
00263     friend const sc_logic operator & ( const sc_logic& a, const sc_logic& b )
00264         { return sc_logic( and_table[a.m_val][b.m_val] ); }
00265 
00266     friend const sc_logic operator & ( const sc_logic& a, sc_logic_value_t b )
00267         { return ( a & sc_logic( b ) ); }
00268 
00269     friend const sc_logic operator & ( const sc_logic& a, bool b )
00270         { return ( a & sc_logic( b ) ); }
00271 
00272     friend const sc_logic operator & ( const sc_logic& a, char b )
00273         { return ( a & sc_logic( b ) ); }
00274 
00275     friend const sc_logic operator & ( const sc_logic& a, int b )
00276         { return ( a & sc_logic( b ) ); }
00277 
00278     friend const sc_logic operator & ( sc_logic_value_t a, const sc_logic& b )
00279         { return ( sc_logic( a ) & b ); }
00280 
00281     friend const sc_logic operator & ( bool a, const sc_logic& b )
00282         { return ( sc_logic( a ) & b ); }
00283 
00284     friend const sc_logic operator & ( char a, const sc_logic& b )
00285         { return ( sc_logic( a ) & b ); }
00286 
00287     friend const sc_logic operator & ( int a, const sc_logic& b )
00288         { return ( sc_logic( a ) & b ); }
00289 
00290 
00291     // bitwise or
00292 
00293     friend const sc_logic operator | ( const sc_logic& a, const sc_logic& b )
00294         { return sc_logic( or_table[a.m_val][b.m_val] ); }
00295 
00296     friend const sc_logic operator | ( const sc_logic& a, sc_logic_value_t b )
00297         { return ( a | sc_logic( b ) ); }
00298 
00299     friend const sc_logic operator | ( const sc_logic& a, bool b )
00300         { return ( a | sc_logic( b ) ); }
00301 
00302     friend const sc_logic operator | ( const sc_logic& a, char b )
00303         { return ( a | sc_logic( b ) ); }
00304 
00305     friend const sc_logic operator | ( const sc_logic& a, int b )
00306         { return ( a | sc_logic( b ) ); }
00307 
00308     friend const sc_logic operator | ( sc_logic_value_t a, const sc_logic& b )
00309         { return ( sc_logic( a ) | b ); }
00310 
00311     friend const sc_logic operator | ( bool a, const sc_logic& b )
00312         { return ( sc_logic( a ) | b ); }
00313 
00314     friend const sc_logic operator | ( char a, const sc_logic& b )
00315         { return ( sc_logic( a ) | b ); }
00316 
00317     friend const sc_logic operator | ( int a, const sc_logic& b )
00318         { return ( sc_logic( a ) | b ); }
00319 
00320 
00321     // bitwise xor
00322 
00323     friend const sc_logic operator ^ ( const sc_logic& a, const sc_logic& b )
00324         { return sc_logic( xor_table[a.m_val][b.m_val] ); }
00325 
00326     friend const sc_logic operator ^ ( const sc_logic& a, sc_logic_value_t b )
00327         { return ( a ^ sc_logic( b ) ); }
00328 
00329     friend const sc_logic operator ^ ( const sc_logic& a, bool b )
00330         { return ( a ^ sc_logic( b ) ); }
00331 
00332     friend const sc_logic operator ^ ( const sc_logic& a, char b )
00333         { return ( a ^ sc_logic( b ) ); }
00334 
00335     friend const sc_logic operator ^ ( const sc_logic& a, int b )
00336         { return ( a ^ sc_logic( b ) ); }
00337 
00338     friend const sc_logic operator ^ ( sc_logic_value_t a, const sc_logic& b )
00339         { return ( sc_logic( a ) ^ b ); }
00340 
00341     friend const sc_logic operator ^ ( bool a, const sc_logic& b )
00342         { return ( sc_logic( a ) ^ b ); }
00343 
00344     friend const sc_logic operator ^ ( char a, const sc_logic& b )
00345         { return ( sc_logic( a ) ^ b ); }
00346 
00347     friend const sc_logic operator ^ ( int a, const sc_logic& b )
00348         { return ( sc_logic( a ) ^ b ); }
00349 
00350 
00351     // relational operators and functions
00352 
00353     friend bool operator == ( const sc_logic& a, const sc_logic& b )
00354         { return ( (int) a.m_val == b.m_val ); }
00355 
00356     friend bool operator == ( const sc_logic& a, sc_logic_value_t b )
00357         { return ( a == sc_logic( b ) ); }
00358 
00359     friend bool operator == ( const sc_logic& a, bool b )
00360         { return ( a == sc_logic( b ) ); }
00361 
00362     friend bool operator == ( const sc_logic& a, char b )
00363         { return ( a == sc_logic( b ) ); }
00364 
00365     friend bool operator == ( const sc_logic& a, int b )
00366         { return ( a == sc_logic( b ) ); }
00367 
00368     friend bool operator == ( sc_logic_value_t a, const sc_logic& b )
00369         { return ( sc_logic( a ) == b ); }
00370 
00371     friend bool operator == ( bool a, const sc_logic& b )
00372         { return ( sc_logic( a ) == b ); }
00373 
00374     friend bool operator == ( char a, const sc_logic& b )
00375         { return ( sc_logic( a ) == b ); }
00376 
00377     friend bool operator == ( int a, const sc_logic& b )
00378         { return ( sc_logic( a ) == b ); }
00379 
00380 
00381     friend bool operator != ( const sc_logic& a, const sc_logic& b )
00382         { return ( (int) a.m_val != b.m_val ); }
00383 
00384     friend bool operator != ( const sc_logic& a, sc_logic_value_t b )
00385         { return ( a != sc_logic( b ) ); }
00386 
00387     friend bool operator != ( const sc_logic& a, bool b )
00388         { return ( a != sc_logic( b ) ); }
00389 
00390     friend bool operator != ( const sc_logic& a, char b )
00391         { return ( a != sc_logic( b ) ); }
00392 
00393     friend bool operator != ( const sc_logic& a, int b )
00394         { return ( a != sc_logic( b ) ); }
00395 
00396     friend bool operator != ( sc_logic_value_t a, const sc_logic& b )
00397         { return ( sc_logic( a ) != b ); }
00398 
00399     friend bool operator != ( bool a, const sc_logic& b )
00400         { return ( sc_logic( a ) != b ); }
00401 
00402     friend bool operator != ( char a, const sc_logic& b )
00403         { return ( sc_logic( a ) != b ); }
00404 
00405     friend bool operator != ( int a, const sc_logic& b )
00406         { return ( sc_logic( a ) != b ); }
00407 
00408 
00409     // explicit conversions
00410 
00411     sc_logic_value_t value() const
00412         { return m_val; }
00413 
00414 
00415     bool is_01() const
00416         { return ( (int) m_val == Log_0 || (int) m_val == Log_1 ); }
00417 
00418     bool to_bool() const
00419         { if( ! is_01() ) { invalid_01(); } return ( (int) m_val != Log_0 ); }
00420 
00421     char to_char() const
00422         { return logic_to_char[m_val]; }
00423 
00424 
00425     // other methods
00426 
00427     void print( ::std::ostream& os = ::std::cout ) const
00428         { os << to_char(); }
00429 
00430     void scan( ::std::istream& is = ::std::cin );
00431 
00432 
00433     // memory (de)allocation
00434 
00435     static void* operator new( size_t, void* p ) // placement new
00436         { return p; }
00437 
00438     static void* operator new( size_t sz )
00439         { return sc_core::sc_mempool::allocate( sz ); }
00440 
00441     static void operator delete( void* p, size_t sz )
00442         { sc_core::sc_mempool::release( p, sz ); }
00443 
00444     static void* operator new [] ( size_t sz )
00445         { return sc_core::sc_mempool::allocate( sz ); }
00446 
00447     static void operator delete [] ( void* p, size_t sz )
00448         { sc_core::sc_mempool::release( p, sz ); }
00449 
00450 private:
00451 
00452     sc_logic_value_t m_val;
00453 
00454 private:
00455 
00456     // disabled
00457     explicit sc_logic( const char* );
00458     sc_logic& operator = ( const char* );
00459 };
00460 
00461 
00462 // ----------------------------------------------------------------------------
00463 
00464 inline
00465 ::std::ostream&
00466 operator << ( ::std::ostream& os, const sc_logic& a )
00467 {
00468     a.print( os );
00469     return os;
00470 }
00471 
00472 inline
00473 ::std::istream&
00474 operator >> ( ::std::istream& is, sc_logic& a )
00475 {
00476     a.scan( is );
00477     return is;
00478 }
00479 
00480 
00481 extern const sc_logic SC_LOGIC_0;
00482 extern const sc_logic SC_LOGIC_1;
00483 extern const sc_logic SC_LOGIC_Z;
00484 extern const sc_logic SC_LOGIC_X;
00485 
00486 // #ifdef SC_DT_DEPRECATED
00487 extern const sc_logic sc_logic_0;
00488 extern const sc_logic sc_logic_1;
00489 extern const sc_logic sc_logic_Z;
00490 extern const sc_logic sc_logic_X;
00491 // #endif
00492 
00493 } // namespace sc_dt
00494 
00495 
00496 #endif

Generated on Wed Apr 25 13:53:26 2007 for SystemC by  doxygen 1.5.1