00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 #ifndef SC_LOGIC_H
00061 #define SC_LOGIC_H
00062 
00063 
00064 #include <cstdio>
00065 
00066 #include "sysc/utils/sc_iostream.h"
00067 #include "sysc/kernel/sc_macros.h"
00068 #include "sysc/utils/sc_mempool.h"
00069 #include "sysc/datatypes/bit/sc_bit.h"
00070 
00071 
00072 namespace sc_dt
00073 {
00074 
00075 
00076 class sc_logic;
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 enum sc_logic_value_t
00086 {
00087     Log_0 = 0,
00088     Log_1,
00089     Log_Z,
00090     Log_X
00091 };
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 class sc_logic
00100 {
00101 private:
00102 
00103     
00104 
00105     static void invalid_value( sc_logic_value_t );
00106     static void invalid_value( char );
00107     static void invalid_value( int );
00108 
00109     static sc_logic_value_t to_value( sc_logic_value_t v )
00110         {
00111             if( v < Log_0 || v > Log_X ) {
00112                 invalid_value( v );
00113             }
00114             return v;
00115         }
00116 
00117     static sc_logic_value_t to_value( bool b )
00118         { return ( b ? Log_1 : Log_0 ); }
00119 
00120     static sc_logic_value_t to_value( char c )
00121         {
00122             sc_logic_value_t v;
00123             unsigned int index = (int)c;
00124             if ( index > 127 )
00125             {
00126                 invalid_value(c);
00127                 v = Log_X;
00128             }
00129             else
00130             {
00131                 v = char_to_logic[index];
00132                 if( v < Log_0 || v > Log_X ) {
00133                     invalid_value( c );
00134                 }
00135             }
00136             return v;
00137         }
00138 
00139     static sc_logic_value_t to_value( int i )
00140         {
00141             if( i < 0 || i > 3 ) {
00142                 invalid_value( i );
00143             }
00144             return sc_logic_value_t( i );
00145         }
00146 
00147 
00148     void invalid_01() const;
00149 
00150 public:
00151 
00152     
00153 
00154     static const sc_logic_value_t char_to_logic[128];
00155     static const char logic_to_char[4];
00156     static const sc_logic_value_t and_table[4][4];
00157     static const sc_logic_value_t or_table[4][4];
00158     static const sc_logic_value_t xor_table[4][4];
00159     static const sc_logic_value_t not_table[4];
00160 
00161 
00162     
00163 
00164     sc_logic()
00165         : m_val( Log_X )
00166         {}
00167 
00168     sc_logic( const sc_logic& a )
00169         : m_val( a.m_val )
00170         {}
00171 
00172     sc_logic( sc_logic_value_t v )
00173         : m_val( to_value( v ) )
00174         {}
00175 
00176     explicit sc_logic( bool a )
00177         : m_val( to_value( a ) )
00178         {}
00179 
00180     explicit sc_logic( char a )
00181         : m_val( to_value( a ) )
00182         {}
00183 
00184     explicit sc_logic( int a )
00185         : m_val( to_value( a ) )
00186         {}
00187 
00188     explicit sc_logic( const sc_bit& a )
00189         : m_val( to_value( a.to_bool() ) )
00190         {}
00191 
00192 
00193     
00194 
00195     ~sc_logic()
00196         {}
00197 
00198 
00199     
00200 
00201 #define DEFN_ASN_OP_T(op,tp)                       \
00202     sc_logic& operator op ( tp v )                 \
00203         { *this op sc_logic( v ); return *this; }
00204 
00205 #define DEFN_ASN_OP(op)                            \
00206     DEFN_ASN_OP_T(op, sc_logic_value_t)            \
00207     DEFN_ASN_OP_T(op, bool)                        \
00208     DEFN_ASN_OP_T(op, char)                        \
00209     DEFN_ASN_OP_T(op, int )                        \
00210     DEFN_ASN_OP_T(op, const sc_bit& )
00211 
00212     sc_logic& operator = ( const sc_logic& a )
00213         { m_val = a.m_val; return *this; }
00214 
00215     sc_logic& operator &= ( const sc_logic& b )
00216         { m_val = and_table[m_val][b.m_val]; return *this; }
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 ^= ( const sc_logic& b )
00222         { m_val = xor_table[m_val][b.m_val]; return *this; }
00223 
00224     DEFN_ASN_OP(=)
00225     DEFN_ASN_OP(&=)
00226     DEFN_ASN_OP(|=)
00227     DEFN_ASN_OP(^=)
00228 
00229 #undef DEFN_ASN_OP_T
00230 #undef DEFN_ASN_OP
00231 
00232 
00233     
00234 
00235 
00236     friend const sc_logic operator & ( const sc_logic&, const sc_logic& );
00237     friend const sc_logic operator | ( const sc_logic&, const sc_logic& );
00238     friend const sc_logic operator ^ ( const sc_logic&, const sc_logic& );
00239 
00240     
00241 
00242     friend bool operator == ( const sc_logic&, const sc_logic& );
00243     friend bool operator != ( const sc_logic&, const sc_logic& );
00244 
00245     
00246 
00247     const sc_logic operator ~ () const
00248         { return sc_logic( not_table[m_val] ); }
00249 
00250     sc_logic& b_not()
00251         { m_val = not_table[m_val]; return *this; }
00252 
00253 
00254     
00255 
00256     sc_logic_value_t value() const
00257         { return m_val; }
00258 
00259 
00260     bool is_01() const
00261         { return ( (int) m_val == Log_0 || (int) m_val == Log_1 ); }
00262 
00263     bool to_bool() const
00264         { if( ! is_01() ) { invalid_01(); } return ( (int) m_val != Log_0 ); }
00265 
00266     char to_char() const
00267         { return logic_to_char[m_val]; }
00268 
00269 
00270     
00271 
00272     void print( ::std::ostream& os = ::std::cout ) const
00273         { os << to_char(); }
00274 
00275     void scan( ::std::istream& is = ::std::cin );
00276 
00277 
00278     
00279 
00280     static void* operator new( std::size_t, void* p ) 
00281         { return p; }
00282 
00283     static void* operator new( std::size_t sz )
00284         { return sc_core::sc_mempool::allocate( sz ); }
00285 
00286     static void operator delete( void* p, std::size_t sz )
00287         { sc_core::sc_mempool::release( p, sz ); }
00288 
00289     static void* operator new [] ( std::size_t sz )
00290         { return sc_core::sc_mempool::allocate( sz ); }
00291 
00292     static void operator delete [] ( void* p, std::size_t sz )
00293         { sc_core::sc_mempool::release( p, sz ); }
00294 
00295 private:
00296 
00297     sc_logic_value_t m_val;
00298 
00299 private:
00300 
00301     
00302     explicit sc_logic( const char* );
00303     sc_logic& operator = ( const char* );
00304 };
00305 
00306 
00307 
00308 
00309 
00310 inline const sc_logic operator & ( const sc_logic& a, const sc_logic& b )
00311        { return sc_logic( sc_logic::and_table[a.m_val][b.m_val] ); }
00312 
00313 inline const sc_logic operator | ( const sc_logic& a, const sc_logic& b )
00314        { return sc_logic( sc_logic::or_table[a.m_val][b.m_val] ); }
00315 
00316 inline const sc_logic operator ^ ( const sc_logic& a, const sc_logic& b )
00317        { return sc_logic( sc_logic::xor_table[a.m_val][b.m_val] ); }
00318 
00319 #define DEFN_BIN_OP_T(ret,op,tp)                       \
00320     inline ret operator op ( const sc_logic& a, tp b ) \
00321         { return ( a op sc_logic( b ) ); }             \
00322     inline ret operator op ( tp a, const sc_logic& b ) \
00323         { return ( sc_logic( a ) op b ); }
00324 
00325 #define DEFN_BIN_OP(ret,op)                            \
00326     DEFN_BIN_OP_T(ret,op,sc_logic_value_t)             \
00327     DEFN_BIN_OP_T(ret,op,bool)                         \
00328     DEFN_BIN_OP_T(ret,op,char)                         \
00329     DEFN_BIN_OP_T(ret,op,int)
00330 
00331 DEFN_BIN_OP(const sc_logic,&)
00332 DEFN_BIN_OP(const sc_logic,|)
00333 DEFN_BIN_OP(const sc_logic,^)
00334 
00335 
00336 
00337 inline bool operator == ( const sc_logic& a, const sc_logic& b )
00338        { return ( (int) a.m_val == b.m_val ); }
00339 
00340 inline bool operator != ( const sc_logic& a, const sc_logic& b )
00341        { return ( (int) a.m_val != b.m_val ); }
00342 
00343 DEFN_BIN_OP(bool,==)
00344 DEFN_BIN_OP(bool,!=)
00345 
00346 #undef DEFN_BIN_OP_T
00347 #undef DEFN_BIN_OP
00348 
00349 
00350 
00351 inline
00352 ::std::ostream&
00353 operator << ( ::std::ostream& os, const sc_logic& a )
00354 {
00355     a.print( os );
00356     return os;
00357 }
00358 
00359 inline
00360 ::std::istream&
00361 operator >> ( ::std::istream& is, sc_logic& a )
00362 {
00363     a.scan( is );
00364     return is;
00365 }
00366 
00367 
00368 extern const sc_logic SC_LOGIC_0;
00369 extern const sc_logic SC_LOGIC_1;
00370 extern const sc_logic SC_LOGIC_Z;
00371 extern const sc_logic SC_LOGIC_X;
00372 
00373 
00374 extern const sc_logic sc_logic_0;
00375 extern const sc_logic sc_logic_1;
00376 extern const sc_logic sc_logic_Z;
00377 extern const sc_logic sc_logic_X;
00378 
00379 
00380 } 
00381 
00382 #endif