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_BIT_H
00061 #define SC_BIT_H
00062 
00063 
00064 #include "sysc/datatypes/int/sc_nbdefs.h"
00065 #include "sysc/utils/sc_iostream.h"
00066 
00067 
00068 namespace sc_dt
00069 {
00070 
00071 
00072 class sc_bit;
00073 
00074 
00075 class sc_logic;
00076 
00077 extern void sc_deprecated_sc_bit();
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 
00086 class sc_bit
00087 {
00088     
00089 
00090     static void invalid_value( char );
00091     static void invalid_value( int );
00092 
00093     static bool to_value( char c )
00094         {
00095             if( c != '0' && c != '1' ) {
00096                 invalid_value( c );
00097             }
00098             return ( c == '0' ? false : true );
00099         }
00100 
00101     static bool to_value( int i )
00102         {
00103             if( i != 0 && i != 1 ) {
00104                 invalid_value( i );
00105             }
00106             return ( i == 0 ? false : true );
00107         }
00108     static bool to_value( bool b )
00109        { return b; }
00110 
00111 #define DEFN_TO_VALUE_T(tp)              \
00112     static bool to_value( tp i )         \
00113        { return to_value( (int) i); }
00114 
00115     DEFN_TO_VALUE_T(unsigned)
00116     DEFN_TO_VALUE_T(long)
00117     DEFN_TO_VALUE_T(unsigned long)
00118     DEFN_TO_VALUE_T(int64)
00119     DEFN_TO_VALUE_T(uint64)
00120 
00121 #undef DEFN_TO_VALUE_T
00122 
00123 public:
00124 
00125     
00126     
00127 
00128     sc_bit()
00129         : m_val( false )
00130         {
00131             sc_deprecated_sc_bit();
00132         }
00133 
00134 #define DEFN_CTOR_T(tp)              \
00135     explicit sc_bit( tp a )          \
00136        : m_val( to_value(a) )       \
00137        { sc_deprecated_sc_bit(); }
00138 
00139     DEFN_CTOR_T(bool)
00140     DEFN_CTOR_T(char)
00141     DEFN_CTOR_T(int)
00142     DEFN_CTOR_T(unsigned)
00143     DEFN_CTOR_T(long)
00144     DEFN_CTOR_T(unsigned long)
00145     DEFN_CTOR_T(int64)
00146     DEFN_CTOR_T(uint64)
00147 
00148 #undef DEFN_CTOR_T
00149 
00150     explicit sc_bit( const sc_logic& a );  
00151 
00152 
00153     
00154     
00155 
00156     sc_bit( const sc_bit& a )
00157         : m_val( a.m_val )
00158         {}
00159 
00160 
00161     
00162     
00163 
00164     ~sc_bit()
00165         {}
00166 
00167 
00168     
00169     
00170 
00171     sc_bit& operator = ( const sc_bit& b )
00172         { m_val = b.m_val; return *this; }
00173 
00174 #define DEFN_ASN_OP_T(op,tp) \
00175     sc_bit& operator op( tp b ) \
00176        { return ( *this op sc_bit( b ) ); }
00177 #define DEFN_ASN_OP(op) \
00178     DEFN_ASN_OP_T(op,int) \
00179     DEFN_ASN_OP_T(op,bool) \
00180     DEFN_ASN_OP_T(op,char)
00181 
00182     DEFN_ASN_OP(=)
00183     DEFN_ASN_OP_T(=,int64)
00184     DEFN_ASN_OP_T(=,uint64)
00185     DEFN_ASN_OP_T(=,long)
00186     DEFN_ASN_OP_T(=,unsigned long)
00187 
00188     sc_bit& operator = ( const sc_logic& b );  
00189 
00190 
00191     
00192 
00193     sc_bit& operator &= ( const sc_bit& b )
00194         { m_val = ( m_val && b.m_val ); return *this; }
00195 
00196     sc_bit& operator |= ( const sc_bit& b )
00197         { m_val = ( m_val || b.m_val ); return *this; }
00198 
00199     sc_bit& operator ^= ( const sc_bit& b )
00200         { m_val = ( m_val != b.m_val ); return *this; }
00201 
00202     DEFN_ASN_OP(&=)
00203     DEFN_ASN_OP(|=)
00204     DEFN_ASN_OP(^=)
00205 
00206 #undef DEFN_ASN_OP_T
00207 #undef DEFN_ASN_OP
00208 
00209     
00210     
00211 
00212     
00213 
00214     operator bool () const
00215         { return m_val; }
00216 
00217     bool operator ! () const  
00218         { return ! m_val; }
00219 
00220 
00221     
00222 
00223     bool to_bool() const  
00224         { return m_val; }
00225 
00226     char to_char() const
00227         { return ( m_val ? '1' : '0' ); }
00228 
00229 
00230     
00231 
00232     
00233 
00234     friend bool operator == ( const sc_bit& a, const sc_bit& b );
00235     friend bool operator != ( const sc_bit& a, const sc_bit& b );
00236 
00237     
00238 
00239     
00240 
00241     
00242 
00243     friend const sc_bit operator ~ ( const sc_bit& a );
00244 
00245     
00246 
00247     sc_bit& b_not()
00248         { m_val = ( ! m_val ); return *this; }
00249 
00250     
00251 
00252     friend const sc_bit operator | ( const sc_bit& a, const sc_bit& b );
00253     friend const sc_bit operator & ( const sc_bit& a, const sc_bit& b );
00254     friend const sc_bit operator ^ ( const sc_bit& a, const sc_bit& b );
00255 
00256     
00257 
00258     void print( ::std::ostream& os = ::std::cout ) const
00259        { os << to_bool(); }
00260 
00261     void scan( ::std::istream& = ::std::cin );
00262 
00263 private:
00264     bool m_val;
00265 };
00266 
00267 
00268 
00269 
00270 #define DEFN_BIN_FUN_T(ret,fun,tp)          \
00271     inline ret fun( const sc_bit& a, tp b ) \
00272        { return fun(a, sc_bit(b) ); }      \
00273     inline ret fun( tp b, const sc_bit& a ) \
00274        { return fun( sc_bit(a), b ); }
00275 
00276 #define DEFN_BIN_FUN(ret,fun) \
00277       DEFN_BIN_FUN_T(ret,fun,bool) \
00278       DEFN_BIN_FUN_T(ret,fun,char) \
00279       DEFN_BIN_FUN_T(ret,fun,int)
00280 
00281 
00282 
00283 inline bool operator == ( const sc_bit& a, const sc_bit& b )
00284     { return ( a.m_val == b.m_val ); }
00285 
00286 inline bool operator != ( const sc_bit& a, const sc_bit& b )
00287     { return ( a.m_val != b.m_val ); }
00288 
00289 DEFN_BIN_FUN(bool,operator==)
00290 DEFN_BIN_FUN(bool,operator!=)
00291 
00292 
00293 
00294 inline bool equal( const sc_bit& a, const sc_bit& b )
00295     { return ( a == b ); }
00296 
00297 inline bool not_equal( const sc_bit& a, const sc_bit& b )
00298     { return ( a != b ); }
00299 
00300 DEFN_BIN_FUN(bool,equal)
00301 DEFN_BIN_FUN(bool,not_equal)
00302 
00303 
00304 
00305 
00306 
00307 
00308     
00309 
00310     inline const sc_bit operator ~ ( const sc_bit& a )
00311        { return sc_bit( ! a.m_val ); }
00312 
00313 
00314     
00315 
00316     inline const sc_bit b_not( const sc_bit& a )
00317        { return ( ~ a ); }
00318 
00319 
00320     
00321 
00322     inline void b_not( sc_bit& r, const sc_bit& a )
00323        { r = ( ~ a ); }
00324 
00325     
00326 
00327     
00328 
00329     inline const sc_bit operator & ( const sc_bit& a, const sc_bit& b )
00330         { return sc_bit( a.m_val && b.m_val ); }
00331 
00332     inline const sc_bit operator | ( const sc_bit& a, const sc_bit& b )
00333         { return sc_bit( a.m_val || b.m_val ); }
00334 
00335     inline const sc_bit operator ^ ( const sc_bit& a, const sc_bit& b )
00336         { return sc_bit( a.m_val != b.m_val ); }
00337 
00338     DEFN_BIN_FUN(const sc_bit,operator&)
00339     DEFN_BIN_FUN(const sc_bit,operator|)
00340     DEFN_BIN_FUN(const sc_bit,operator^)
00341 
00342     
00343 
00344     inline const sc_bit b_and ( const sc_bit& a, const sc_bit& b )
00345         { return a & b; }
00346 
00347     inline const sc_bit b_or ( const sc_bit& a, const sc_bit& b )
00348         { return a | b; }
00349 
00350     inline const sc_bit b_xor ( const sc_bit& a, const sc_bit& b )
00351         { return a ^ b; }
00352 
00353     DEFN_BIN_FUN(const sc_bit,b_and)
00354     DEFN_BIN_FUN(const sc_bit,b_or)
00355     DEFN_BIN_FUN(const sc_bit,b_xor)
00356 
00357     
00358 
00359 #define DEFN_TRN_FUN_T(fun,tp)                                     \
00360     inline void fun( sc_bit& r, const sc_bit& a, tp b )            \
00361         { r = fun( a, sc_bit(b) ); }                               \
00362     inline void fun( sc_bit& r, tp a, const sc_bit& b )            \
00363         { r = fun( sc_bit(a), b ); }
00364 
00365 #define DEFN_TRN_FUN(fun) \
00366     inline void fun( sc_bit& r, const sc_bit& a, const sc_bit& b ) \
00367         { r = fun( a , b ); }                                      \
00368     DEFN_TRN_FUN_T(fun,int)                                        \
00369     DEFN_TRN_FUN_T(fun,bool)                                       \
00370     DEFN_TRN_FUN_T(fun,char)
00371 
00372     DEFN_TRN_FUN( b_and )
00373     DEFN_TRN_FUN( b_or )
00374     DEFN_TRN_FUN( b_xor )
00375 
00376 #undef DEFN_BIN_FUN_T
00377 #undef DEFN_BIN_FUN
00378 #undef DEFN_TRN_FUN_T
00379 #undef DEFN_TRN_FUN
00380 
00381 
00382 
00383 
00384 inline
00385 ::std::ostream&
00386 operator << ( ::std::ostream& os, const sc_bit& a )
00387 {
00388     a.print( os );
00389     return os;
00390 }
00391 
00392 inline
00393 ::std::istream&
00394 operator >> ( ::std::istream& is, sc_bit& a )
00395 {
00396     a.scan( is );
00397     return is;
00398 }
00399 
00400 } 
00401 
00402 
00403 #endif
00404 
00405