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-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_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 // $Log: sc_logic.h,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 #ifndef SC_LOGIC_H
00047 #define SC_LOGIC_H
00048 
00049 
00050 #include <cstdio>
00051 
00052 #include "sysc/utils/sc_iostream.h"
00053 #include "sysc/kernel/sc_macros.h"
00054 #include "sysc/utils/sc_mempool.h"
00055 #include "sysc/datatypes/bit/sc_bit.h"
00056 
00057 
00058 namespace sc_dt
00059 {
00060 
00061 // classes defined in this module
00062 class sc_logic;
00063 
00064 
00065 // ----------------------------------------------------------------------------
00066 //  ENUM : sc_logic_value_t
00067 //
00068 //  Enumeration of values for sc_logic.
00069 // ----------------------------------------------------------------------------
00070 
00071 enum sc_logic_value_t
00072 {
00073     Log_0 = 0,
00074     Log_1,
00075     Log_Z,
00076     Log_X
00077 };
00078 
00079 
00080 // friend operator declarations
00081     // bitwise and
00082 
00083     inline const sc_logic operator & ( const sc_logic& a, const sc_logic& b );
00084 
00085     inline const sc_logic operator & ( const sc_logic& a, sc_logic_value_t b );
00086 
00087     inline const sc_logic operator & ( const sc_logic& a, bool b );
00088 
00089     inline const sc_logic operator & ( const sc_logic& a, char b );
00090 
00091     inline const sc_logic operator & ( const sc_logic& a, int b );
00092 
00093     inline const sc_logic operator & ( sc_logic_value_t a, const sc_logic& b );
00094 
00095     inline const sc_logic operator & ( bool a, const sc_logic& b );
00096 
00097     inline const sc_logic operator & ( char a, const sc_logic& b );
00098 
00099     inline const sc_logic operator & ( int a, const sc_logic& b );
00100 
00101 
00102     // bitwise or
00103 
00104     inline const sc_logic operator | ( const sc_logic& a, const sc_logic& b );
00105 
00106     inline const sc_logic operator | ( const sc_logic& a, sc_logic_value_t b );
00107 
00108     inline const sc_logic operator | ( const sc_logic& a, bool b );
00109 
00110     inline const sc_logic operator | ( const sc_logic& a, char b );
00111 
00112     inline const sc_logic operator | ( const sc_logic& a, int b );
00113 
00114     inline const sc_logic operator | ( sc_logic_value_t a, const sc_logic& b );
00115 
00116     inline const sc_logic operator | ( bool a, const sc_logic& b );
00117 
00118     inline const sc_logic operator | ( char a, const sc_logic& b );
00119 
00120     inline const sc_logic operator | ( int a, const sc_logic& b );
00121 
00122 
00123     // bitwise xor
00124 
00125     inline const sc_logic operator ^ ( const sc_logic& a, const sc_logic& b );
00126 
00127     inline const sc_logic operator ^ ( const sc_logic& a, sc_logic_value_t b );
00128 
00129     inline const sc_logic operator ^ ( const sc_logic& a, bool b );
00130 
00131     inline const sc_logic operator ^ ( const sc_logic& a, char b );
00132 
00133     inline const sc_logic operator ^ ( const sc_logic& a, int b );
00134 
00135     inline const sc_logic operator ^ ( sc_logic_value_t a, const sc_logic& b );
00136 
00137     inline const sc_logic operator ^ ( bool a, const sc_logic& b );
00138 
00139     inline const sc_logic operator ^ ( char a, const sc_logic& b );
00140 
00141     inline const sc_logic operator ^ ( int a, const sc_logic& b );
00142 
00143 
00144     // relational operators and functions
00145 
00146     inline bool operator == ( const sc_logic& a, const sc_logic& b );
00147 
00148     inline bool operator == ( const sc_logic& a, sc_logic_value_t b );
00149 
00150     inline bool operator == ( const sc_logic& a, bool b );
00151 
00152     inline bool operator == ( const sc_logic& a, char b );
00153 
00154     inline bool operator == ( const sc_logic& a, int b );
00155 
00156     inline bool operator == ( sc_logic_value_t a, const sc_logic& b );
00157 
00158     inline bool operator == ( bool a, const sc_logic& b );
00159 
00160     inline bool operator == ( char a, const sc_logic& b );
00161 
00162     inline bool operator == ( int a, const sc_logic& b );
00163 
00164 
00165     inline bool operator != ( const sc_logic& a, const sc_logic& b );
00166 
00167     inline bool operator != ( const sc_logic& a, sc_logic_value_t b );
00168 
00169     inline bool operator != ( const sc_logic& a, bool b );
00170 
00171     inline bool operator != ( const sc_logic& a, char b );
00172 
00173     inline bool operator != ( const sc_logic& a, int b );
00174 
00175     inline bool operator != ( sc_logic_value_t a, const sc_logic& b );
00176 
00177     inline bool operator != ( bool a, const sc_logic& b );
00178 
00179     inline bool operator != ( char a, const sc_logic& b );
00180 
00181     inline bool operator != ( int a, const sc_logic& b );
00182 
00183 
00184 
00185 
00186 // ----------------------------------------------------------------------------
00187 //  CLASS : sc_logic
00188 //
00189 //  Four-valued logic type.
00190 // ----------------------------------------------------------------------------
00191 
00192 class sc_logic
00193 {
00194     friend class sc_logic_resolve;
00195 
00196 private:
00197 
00198     // support methods
00199 
00200     static void invalid_value( sc_logic_value_t );
00201     static void invalid_value( char );
00202     static void invalid_value( int );
00203 
00204     static sc_logic_value_t to_value( sc_logic_value_t v )
00205     {
00206         if( v < Log_0 || v > Log_X ) {
00207         invalid_value( v );
00208         }
00209         return v;
00210     }
00211 
00212     static sc_logic_value_t to_value( bool b )
00213     { return ( b ? Log_1 : Log_0 ); }
00214 
00215     static sc_logic_value_t to_value( char c )
00216     {
00217         sc_logic_value_t v;
00218         unsigned int index = (int)c;
00219         if ( index > 127 )
00220         {
00221             invalid_value(c);
00222         v = Log_X;
00223         }
00224         else
00225         {
00226         v = char_to_logic[index];
00227         if( v < Log_0 || v > Log_X ) {
00228             invalid_value( c );
00229         }
00230         }
00231         return v;
00232     }
00233 
00234     static sc_logic_value_t to_value( int i )
00235     {
00236         if( i < 0 || i > 3 ) {
00237         invalid_value( i );
00238         }
00239         return sc_logic_value_t( i );
00240     }
00241 
00242 
00243     void invalid_01() const;
00244 
00245 public:
00246 
00247     // conversion tables
00248 
00249     static const sc_logic_value_t char_to_logic[128];
00250     static const char logic_to_char[4];
00251     static const sc_logic_value_t and_table[4][4];
00252     static const sc_logic_value_t or_table[4][4];
00253     static const sc_logic_value_t xor_table[4][4];
00254     static const sc_logic_value_t not_table[4];
00255 
00256 
00257     // constructors
00258 
00259     sc_logic()
00260     : m_val( Log_X )
00261     {}
00262 
00263     sc_logic( const sc_logic& a )
00264     : m_val( a.m_val )
00265     {}
00266 
00267     sc_logic( sc_logic_value_t v )
00268     : m_val( to_value( v ) )
00269     {}
00270 
00271     explicit sc_logic( bool a )
00272     : m_val( to_value( a ) )
00273     {}
00274 
00275     explicit sc_logic( char a )
00276     : m_val( to_value( a ) )
00277     {}
00278 
00279     explicit sc_logic( int a )
00280     : m_val( to_value( a ) )
00281     {}
00282 
00283     explicit sc_logic( const sc_bit& a )
00284     : m_val( to_value( a.to_bool() ) )
00285     {}
00286 
00287 
00288     // destructor
00289 
00290     ~sc_logic()
00291     {}
00292 
00293 
00294     // assignment operators
00295 
00296     sc_logic& operator = ( const sc_logic& a )
00297     { m_val = a.m_val; return *this; }
00298 
00299     sc_logic& operator = ( sc_logic_value_t v )
00300     { *this = sc_logic( v ); return *this; }
00301 
00302     sc_logic& operator = ( bool a )
00303     { *this = sc_logic( a ); return *this; }
00304 
00305     sc_logic& operator = ( char a )
00306     { *this = sc_logic( a ); return *this; }
00307 
00308     sc_logic& operator = ( int a )
00309     { *this = sc_logic( a ); return *this; }
00310 
00311     sc_logic& operator = ( const sc_bit& a )
00312     { *this = sc_logic( a ); return *this; }
00313 
00314 
00315     // bitwise assignment operators
00316 
00317     sc_logic& operator &= ( const sc_logic& b )
00318     { m_val = and_table[m_val][b.m_val]; return *this; }
00319 
00320     sc_logic& operator &= ( sc_logic_value_t v )
00321     { *this &= sc_logic( v ); return *this; }
00322 
00323     sc_logic& operator &= ( bool b )
00324     { *this &= sc_logic( b ); return *this; }
00325 
00326     sc_logic& operator &= ( char b )
00327     { *this &= sc_logic( b ); return *this; }
00328 
00329     sc_logic& operator &= ( int b )
00330     { *this &= sc_logic( b ); return *this; }
00331 
00332 
00333     sc_logic& operator |= ( const sc_logic& b )
00334         { m_val = or_table[m_val][b.m_val]; return *this; }
00335 
00336     sc_logic& operator |= ( sc_logic_value_t v )
00337     { *this |= sc_logic( v ); return *this; }
00338 
00339     sc_logic& operator |= ( bool b )
00340     { *this |= sc_logic( b ); return *this; }
00341 
00342     sc_logic& operator |= ( char b )
00343     { *this |= sc_logic( b ); return *this; }
00344 
00345     sc_logic& operator |= ( int b )
00346     { *this |= sc_logic( b ); return *this; }
00347 
00348 
00349     sc_logic& operator ^= ( const sc_logic& b )
00350         { m_val = xor_table[m_val][b.m_val]; return *this; }
00351 
00352     sc_logic& operator ^= ( sc_logic_value_t v )
00353     { *this ^= sc_logic( v ); return *this; }
00354 
00355     sc_logic& operator ^= ( bool b )
00356     { *this ^= sc_logic( b ); return *this; }
00357 
00358     sc_logic& operator ^= ( char b )
00359     { *this ^= sc_logic( b ); return *this; }
00360 
00361     sc_logic& operator ^= ( int b )
00362     { *this ^= sc_logic( b ); return *this; }
00363 
00364 
00365     // bitwise operators and functions
00366 
00367     // bitwise complement
00368 
00369     const sc_logic operator ~ () const
00370     { return sc_logic( not_table[m_val] ); }
00371 
00372     sc_logic& b_not()
00373     { m_val = not_table[m_val]; return *this; }
00374 
00375 
00376     // bitwise and
00377 
00378     friend const sc_logic operator & ( const sc_logic& a, const sc_logic& b )
00379     { return sc_logic( sc_logic::and_table[a.m_val][b.m_val] ); }
00380 
00381     friend const sc_logic operator & ( const sc_logic& a, sc_logic_value_t b )
00382     { return ( a & sc_logic( b ) ); }
00383 
00384     friend const sc_logic operator & ( const sc_logic& a, bool b )
00385     { return ( a & sc_logic( b ) ); }
00386 
00387     friend const sc_logic operator & ( const sc_logic& a, char b )
00388     { return ( a & sc_logic( b ) ); }
00389 
00390     friend const sc_logic operator & ( const sc_logic& a, int b )
00391     { return ( a & sc_logic( b ) ); }
00392 
00393     friend const sc_logic operator & ( sc_logic_value_t a, const sc_logic& b )
00394     { return ( sc_logic( a ) & b ); }
00395 
00396     friend const sc_logic operator & ( bool a, const sc_logic& b )
00397     { return ( sc_logic( a ) & b ); }
00398 
00399     friend const sc_logic operator & ( char a, const sc_logic& b )
00400     { return ( sc_logic( a ) & b ); }
00401 
00402     friend const sc_logic operator & ( int a, const sc_logic& b )
00403     { return ( sc_logic( a ) & b ); }
00404 
00405 
00406     // bitwise or
00407 
00408     friend const sc_logic operator | ( const sc_logic& a, const sc_logic& b )
00409     { return sc_logic( sc_logic::or_table[a.m_val][b.m_val] ); }
00410 
00411     friend const sc_logic operator | ( const sc_logic& a, sc_logic_value_t b )
00412     { return ( a | sc_logic( b ) ); }
00413 
00414     friend const sc_logic operator | ( const sc_logic& a, bool b )
00415     { return ( a | sc_logic( b ) ); }
00416 
00417     friend const sc_logic operator | ( const sc_logic& a, char b )
00418     { return ( a | sc_logic( b ) ); }
00419 
00420     friend const sc_logic operator | ( const sc_logic& a, int b )
00421     { return ( a | sc_logic( b ) ); }
00422 
00423     friend const sc_logic operator | ( sc_logic_value_t a, const sc_logic& b )
00424     { return ( sc_logic( a ) | b ); }
00425 
00426     friend const sc_logic operator | ( bool a, const sc_logic& b )
00427     { return ( sc_logic( a ) | b ); }
00428 
00429     friend const sc_logic operator | ( char a, const sc_logic& b )
00430     { return ( sc_logic( a ) | b ); }
00431 
00432     friend const sc_logic operator | ( int a, const sc_logic& b )
00433     { return ( sc_logic( a ) | b ); }
00434 
00435 
00436     // bitwise xor
00437 
00438     friend const sc_logic operator ^ ( const sc_logic& a, const sc_logic& b )
00439     { return sc_logic( sc_logic::xor_table[a.m_val][b.m_val] ); }
00440 
00441     friend const sc_logic operator ^ ( const sc_logic& a, sc_logic_value_t b )
00442     { return ( a ^ sc_logic( b ) ); }
00443 
00444     friend const sc_logic operator ^ ( const sc_logic& a, bool b )
00445     { return ( a ^ sc_logic( b ) ); }
00446 
00447     friend const sc_logic operator ^ ( const sc_logic& a, char b )
00448     { return ( a ^ sc_logic( b ) ); }
00449 
00450     friend const sc_logic operator ^ ( const sc_logic& a, int b )
00451     { return ( a ^ sc_logic( b ) ); }
00452 
00453     friend const sc_logic operator ^ ( sc_logic_value_t a, const sc_logic& b )
00454     { return ( sc_logic( a ) ^ b ); }
00455 
00456     friend const sc_logic operator ^ ( bool a, const sc_logic& b )
00457     { return ( sc_logic( a ) ^ b ); }
00458 
00459     friend const sc_logic operator ^ ( char a, const sc_logic& b )
00460     { return ( sc_logic( a ) ^ b ); }
00461 
00462     friend const sc_logic operator ^ ( int a, const sc_logic& b )
00463     { return ( sc_logic( a ) ^ b ); }
00464 
00465 
00466     // relational operators and functions
00467 
00468     friend bool operator == ( const sc_logic& a, const sc_logic& b )
00469     { return ( (int) a.m_val == b.m_val ); }
00470 
00471     friend bool operator == ( const sc_logic& a, sc_logic_value_t b )
00472     { return ( a == sc_logic( b ) ); }
00473 
00474     friend bool operator == ( const sc_logic& a, bool b )
00475     { return ( a == sc_logic( b ) ); }
00476 
00477     friend bool operator == ( const sc_logic& a, char b )
00478     { return ( a == sc_logic( b ) ); }
00479 
00480     friend bool operator == ( const sc_logic& a, int b )
00481     { return ( a == sc_logic( b ) ); }
00482 
00483     friend bool operator == ( sc_logic_value_t a, const sc_logic& b )
00484     { return ( sc_logic( a ) == b ); }
00485 
00486     friend bool operator == ( bool a, const sc_logic& b )
00487     { return ( sc_logic( a ) == b ); }
00488 
00489     friend bool operator == ( char a, const sc_logic& b )
00490     { return ( sc_logic( a ) == b ); }
00491 
00492     friend bool operator == ( int a, const sc_logic& b )
00493     { return ( sc_logic( a ) == b ); }
00494 
00495 
00496     friend bool operator != ( const sc_logic& a, const sc_logic& b )
00497     { return ( (int) a.m_val != b.m_val ); }
00498 
00499     friend bool operator != ( const sc_logic& a, sc_logic_value_t b )
00500     { return ( a != sc_logic( b ) ); }
00501 
00502     friend bool operator != ( const sc_logic& a, bool b )
00503     { return ( a != sc_logic( b ) ); }
00504 
00505     friend bool operator != ( const sc_logic& a, char b )
00506     { return ( a != sc_logic( b ) ); }
00507 
00508     friend bool operator != ( const sc_logic& a, int b )
00509     { return ( a != sc_logic( b ) ); }
00510 
00511     friend bool operator != ( sc_logic_value_t a, const sc_logic& b )
00512     { return ( sc_logic( a ) != b ); }
00513 
00514     friend bool operator != ( bool a, const sc_logic& b )
00515     { return ( sc_logic( a ) != b ); }
00516 
00517     friend bool operator != ( char a, const sc_logic& b )
00518     { return ( sc_logic( a ) != b ); }
00519 
00520     friend bool operator != ( int a, const sc_logic& b )
00521     { return ( sc_logic( a ) != b ); }
00522 
00523 
00524     // explicit conversions
00525 
00526     sc_logic_value_t value() const
00527     { return m_val; }
00528 
00529 
00530     bool is_01() const
00531     { return ( (int) m_val == Log_0 || (int) m_val == Log_1 ); }
00532 
00533     bool to_bool() const
00534     { if( ! is_01() ) { invalid_01(); } return ( (int) m_val != Log_0 ); }
00535 
00536     char to_char() const
00537     { return logic_to_char[m_val]; }
00538 
00539 
00540     // other methods
00541 
00542     void print( ::std::ostream& os = ::std::cout ) const
00543     { os << to_char(); }
00544 
00545     void scan( ::std::istream& is = ::std::cin );
00546 
00547 
00548     // memory (de)allocation
00549 
00550     static void* operator new( std::size_t, void* p ) // placement new
00551     { return p; }
00552 
00553     static void* operator new( std::size_t sz )
00554     { return sc_core::sc_mempool::allocate( sz ); }
00555 
00556     static void operator delete( void* p, std::size_t sz )
00557     { sc_core::sc_mempool::release( p, sz ); }
00558 
00559     static void* operator new [] ( std::size_t sz )
00560     { return sc_core::sc_mempool::allocate( sz ); }
00561 
00562     static void operator delete [] ( void* p, std::size_t sz )
00563     { sc_core::sc_mempool::release( p, sz ); }
00564 
00565 private:
00566 
00567     sc_logic_value_t m_val;
00568 
00569 private:
00570 
00571     // disabled
00572     explicit sc_logic( const char* );
00573     sc_logic& operator = ( const char* );
00574 };
00575 
00576 
00577 // ----------------------------------------------------------------------------
00578 
00579 inline
00580 ::std::ostream&
00581 operator << ( ::std::ostream& os, const sc_logic& a )
00582 {
00583     a.print( os );
00584     return os;
00585 }
00586 
00587 inline
00588 ::std::istream&
00589 operator >> ( ::std::istream& is, sc_logic& a )
00590 {
00591     a.scan( is );
00592     return is;
00593 }
00594 
00595 
00596 extern const sc_logic SC_LOGIC_0;
00597 extern const sc_logic SC_LOGIC_1;
00598 extern const sc_logic SC_LOGIC_Z;
00599 extern const sc_logic SC_LOGIC_X;
00600 
00601 // #ifdef SC_DT_DEPRECATED
00602 extern const sc_logic sc_logic_0;
00603 extern const sc_logic sc_logic_1;
00604 extern const sc_logic sc_logic_Z;
00605 extern const sc_logic sc_logic_X;
00606 // #endif
00607 
00608 } // namespace sc_dt
00609 
00610 
00611 #endif

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