[1] | 1 | /*------------------------------------------------------------\ |
---|
| 2 | | | |
---|
| 3 | | Tool : systemcass | |
---|
| 4 | | | |
---|
| 5 | | File : sc_uint.h | |
---|
| 6 | | | |
---|
| 7 | | Author : Buchmann Richard | |
---|
| 8 | | | |
---|
| 9 | | Date : 09_07_2004 | |
---|
| 10 | | | |
---|
| 11 | \------------------------------------------------------------*/ |
---|
| 12 | #ifndef __SC_UINT_H__ |
---|
| 13 | #define __SC_UINT_H__ |
---|
| 14 | |
---|
| 15 | #include <data_field.h> |
---|
| 16 | #include <sc_numrep.h> |
---|
| 17 | #include <sc_bit.h> |
---|
| 18 | #include <sc_logic.h> |
---|
| 19 | #include <sc_bv.h> |
---|
[20] | 20 | #include <cstdlib> |
---|
[1] | 21 | |
---|
| 22 | // ---------------------------------------------------------------------------- |
---|
| 23 | // CLASS : sc_uint<W> |
---|
| 24 | // |
---|
| 25 | // ---------------------------------------------------------------------------- |
---|
| 26 | |
---|
[8] | 27 | #include "sc_nbdefs.h" |
---|
[1] | 28 | |
---|
| 29 | namespace sc_dt { |
---|
| 30 | |
---|
| 31 | typedef sc_uint<1> sc_uint_bit_ref; |
---|
| 32 | typedef sc_uint<1> sc_uint_bit_ref_r; |
---|
| 33 | |
---|
| 34 | // -------------------------------- |
---|
| 35 | // CLASS : sc_uint_subref_r |
---|
| 36 | // |
---|
| 37 | // Used for range, concat functions |
---|
| 38 | class sc_uint_subref_r |
---|
| 39 | { |
---|
| 40 | template <int W> friend class sc_dt::sc_uint; |
---|
| 41 | int left, right; |
---|
| 42 | public : uint64 val; |
---|
| 43 | sc_uint_subref_r (uint64 val_, int left_, int right_) |
---|
| 44 | { |
---|
| 45 | val = val_; left = left_; right = right_; |
---|
| 46 | } |
---|
| 47 | public: |
---|
| 48 | inline uint64 read () const { return val; } |
---|
| 49 | inline const sc_uint_subref_r& operator | (const sc_uint_subref_r &v) const |
---|
| 50 | { print_warning (); return *this; } |
---|
| 51 | |
---|
| 52 | private :void print_warning () const; |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | // |
---|
| 56 | #define MASK32(W) ((~ (const uint32)0) >> (sizeof (uint32) * 8 - W)) |
---|
| 57 | #define MASK64(W) ((~ (const uint64)0) >> (sizeof (uint64) * 8 - W)) |
---|
| 58 | // |
---|
| 59 | |
---|
| 60 | template<int W> struct s_uint_type { typedef uint64 uint_type;}; |
---|
| 61 | #define DECLAR_UINT_TYPE(W) template<> struct s_uint_type<W> { typedef smallest_uint uint_type; }// not declared as uint16 because << operator threats like a character |
---|
| 62 | DECLAR_UINT_TYPE( 1); |
---|
| 63 | DECLAR_UINT_TYPE( 2); |
---|
| 64 | DECLAR_UINT_TYPE( 3); |
---|
| 65 | DECLAR_UINT_TYPE( 4); |
---|
| 66 | DECLAR_UINT_TYPE( 5); |
---|
| 67 | DECLAR_UINT_TYPE( 6); |
---|
| 68 | DECLAR_UINT_TYPE( 7); |
---|
| 69 | DECLAR_UINT_TYPE( 8); |
---|
| 70 | #undef DECLAR_UINT_TYPE |
---|
| 71 | #define DECLAR_UINT_TYPE(W) template<> struct s_uint_type<W> { typedef uint16 uint_type; } |
---|
| 72 | DECLAR_UINT_TYPE( 9); |
---|
| 73 | DECLAR_UINT_TYPE(10); |
---|
| 74 | DECLAR_UINT_TYPE(11); |
---|
| 75 | DECLAR_UINT_TYPE(12); |
---|
| 76 | DECLAR_UINT_TYPE(13); |
---|
| 77 | DECLAR_UINT_TYPE(14); |
---|
| 78 | DECLAR_UINT_TYPE(15); |
---|
| 79 | DECLAR_UINT_TYPE(16); |
---|
| 80 | #undef DECLAR_UINT_TYPE |
---|
| 81 | #define DECLAR_UINT_TYPE(W) template<> struct s_uint_type<W> { typedef uint32 uint_type; } |
---|
| 82 | DECLAR_UINT_TYPE(17); |
---|
| 83 | DECLAR_UINT_TYPE(18); |
---|
| 84 | DECLAR_UINT_TYPE(19); |
---|
| 85 | DECLAR_UINT_TYPE(20); |
---|
| 86 | DECLAR_UINT_TYPE(21); |
---|
| 87 | DECLAR_UINT_TYPE(22); |
---|
| 88 | DECLAR_UINT_TYPE(23); |
---|
| 89 | DECLAR_UINT_TYPE(24); |
---|
| 90 | DECLAR_UINT_TYPE(25); |
---|
| 91 | DECLAR_UINT_TYPE(26); |
---|
| 92 | DECLAR_UINT_TYPE(27); |
---|
| 93 | DECLAR_UINT_TYPE(28); |
---|
| 94 | DECLAR_UINT_TYPE(29); |
---|
| 95 | DECLAR_UINT_TYPE(30); |
---|
| 96 | DECLAR_UINT_TYPE(31); |
---|
| 97 | DECLAR_UINT_TYPE(32); |
---|
| 98 | #undef DECLAR_UINT_TYPE |
---|
| 99 | |
---|
| 100 | class sc_uint_subref_r; |
---|
| 101 | |
---|
| 102 | template<int W> /* width of data type = SC_INTWIDTH */ |
---|
| 103 | class sc_uint |
---|
| 104 | { |
---|
| 105 | /***********************/ |
---|
| 106 | /* SYSTEMCASS SPECIFIC */ |
---|
| 107 | /***********************/ |
---|
| 108 | typedef sc_uint<W> this_type; |
---|
| 109 | typedef typename s_uint_type<W>::uint_type data_type; |
---|
| 110 | |
---|
| 111 | typedef data_type sc_uint_subref; |
---|
| 112 | //typedef data_type sc_uint_subref_r; |
---|
| 113 | /* removed since "operator ," was going wrong */ |
---|
| 114 | |
---|
| 115 | // internal |
---|
| 116 | union { |
---|
| 117 | val_field<W,(sizeof (data_type) * 8) - W,data_type> vf; /* To compute */ |
---|
| 118 | data_type val; /* To return an int reference (read function) */ |
---|
| 119 | // data_type valW:W; /* works with little endianess only */ |
---|
| 120 | // bool valB[W]; /* removed since 1 bool takes 1 byte */ |
---|
| 121 | }; |
---|
| 122 | |
---|
| 123 | /***********************/ |
---|
| 124 | /* L R M */ |
---|
| 125 | /***********************/ |
---|
| 126 | public: |
---|
| 127 | sc_uint () { val = 0; } |
---|
| 128 | // sc_uint (data_type val_) { val = 0; write (val_); } |
---|
[20] | 129 | sc_uint (const char *a) { val = 0; write (std::atoi (a)); } |
---|
[1] | 130 | sc_uint (unsigned short a){ val = 0; write (a); } |
---|
| 131 | sc_uint (short a) { val = 0; write (a); } |
---|
| 132 | sc_uint (unsigned long a) { val = 0; write (a); } |
---|
| 133 | sc_uint (long a) { val = 0; write (a); } |
---|
| 134 | sc_uint (unsigned int a) { val = 0; write (a); } |
---|
| 135 | sc_uint (int a) { val = 0; write (a); } |
---|
| 136 | sc_uint (int64 a) { val = 0; write (a); } |
---|
| 137 | sc_uint (uint64 a) { val = 0; write (a); } |
---|
| 138 | sc_uint (double a) { val = 0; write (a); } |
---|
| 139 | |
---|
| 140 | template <int W2> sc_uint (const sc_uint<W2> &val_) { val = 0; write (val_.read());} |
---|
| 141 | /* this template doesn't redefine default copy constructor of sc_uint. |
---|
| 142 | * So, we define by this way |
---|
| 143 | */ |
---|
| 144 | sc_uint (const sc_uint &val_) { val = 0; write (val_.read()); } |
---|
| 145 | sc_uint (const sc_uint_subref_r &val_) { val = 0; write (val_.read()); } |
---|
| 146 | /* the user needs to cast explicitly result of range () method. */ |
---|
| 147 | |
---|
| 148 | /***********************/ |
---|
| 149 | /* SYSTEMCASS SPECIFIC */ |
---|
| 150 | /***********************/ |
---|
| 151 | // read/write |
---|
| 152 | inline const data_type& read() const { return val; } |
---|
| 153 | inline void write(data_type val_) { vf.valW = val_; } |
---|
| 154 | template <int W2> inline void write (const sc_uint<W2> val_) { write (val_.read()); } |
---|
| 155 | //inline void write (const sc_uint<W> val_) { write (val_.read()); } |
---|
| 156 | inline void write (const sc_uint_subref_r& s) { write (s.read()); } |
---|
| 157 | |
---|
| 158 | /***********************/ |
---|
| 159 | /* L R M */ |
---|
| 160 | /***********************/ |
---|
| 161 | // operators |
---|
| 162 | inline operator const data_type& () const { return read (); } |
---|
| 163 | // inline void write(uint64 val_) { val = val_ & MASK64(W); } |
---|
| 164 | // inline void write(unsigned int val_) { val = val_ & MASK32(W); } |
---|
| 165 | template <typename T> inline sc_uint& operator = (const T& val_) |
---|
| 166 | { write (val_); return *this; } |
---|
| 167 | inline sc_uint& operator = (const sc_uint_subref_r& a) |
---|
| 168 | { write (a); return *this; } |
---|
| 169 | |
---|
| 170 | // explicit conversions |
---|
| 171 | inline uint32 to_uint () const {return val & MASK32(W);} |
---|
| 172 | inline int32 to_int () const {return val & MASK32(W);} |
---|
| 173 | inline uint64 to_uint64 () const {return val & MASK64(W);} |
---|
| 174 | inline int64 to_int64 () const {return val & MASK64(W);} |
---|
| 175 | |
---|
| 176 | // explicit conversion to character string |
---|
| 177 | const sc_string to_string ( sc_numrep numrep = SC_DEC ) const |
---|
| 178 | { return sc_dt::to_string (val, W, numrep); } |
---|
| 179 | const sc_string to_dec() const { return to_string (SC_DEC); } |
---|
| 180 | const sc_string to_bin() const { return to_string (SC_BIN); } |
---|
| 181 | const sc_string to_oct() const { return to_string (SC_OCT); } |
---|
| 182 | const sc_string to_hex() const { return to_string (SC_HEX); } |
---|
| 183 | |
---|
| 184 | // arithmetic |
---|
[24] | 185 | #define DEFINE_OPERATOR(OPER) \ |
---|
| 186 | template <typename T> \ |
---|
| 187 | inline sc_uint& operator OPER (T v)\ |
---|
| 188 | { vf.valW OPER v; return *this; } |
---|
| 189 | |
---|
| 190 | DEFINE_OPERATOR(<<=) |
---|
| 191 | DEFINE_OPERATOR(>>=) |
---|
| 192 | DEFINE_OPERATOR(+=) |
---|
| 193 | DEFINE_OPERATOR(-=) |
---|
| 194 | DEFINE_OPERATOR(*=) |
---|
| 195 | DEFINE_OPERATOR(/=) |
---|
| 196 | DEFINE_OPERATOR(%=) |
---|
| 197 | DEFINE_OPERATOR(&=) |
---|
| 198 | DEFINE_OPERATOR(|=) |
---|
| 199 | DEFINE_OPERATOR(^=) |
---|
| 200 | #undef DEFINE_OPERATOR |
---|
| 201 | |
---|
| 202 | #if 0 |
---|
| 203 | #define DEFINE_OPERATOR(OPER) \ |
---|
| 204 | friend bool operator OPER (const data_type& a, const data_type& b); |
---|
| 205 | // { return (a.valW) OPER (b.valW); } |
---|
| 206 | |
---|
| 207 | DEFINE_OPERATOR(==) |
---|
| 208 | DEFINE_OPERATOR(!=) |
---|
| 209 | DEFINE_OPERATOR(>=) |
---|
| 210 | DEFINE_OPERATOR(<=) |
---|
| 211 | #undef DEFINE_OPERATOR |
---|
| 212 | #endif |
---|
[1] | 213 | inline sc_uint_bit_ref operator [] (int v) |
---|
| 214 | { return (vf.valW >> v) & 1; } |
---|
| 215 | inline sc_uint_bit_ref_r operator [] (int v) const |
---|
| 216 | { return (vf.valW >> v) & 1; } |
---|
| 217 | |
---|
| 218 | template <int W2> |
---|
| 219 | inline sc_uint<W+W2> operator , (const sc_uint<W2> &b) const |
---|
| 220 | { sc_uint<W+W2> res = read() << W2; res += b.read(); return res; } |
---|
| 221 | #if 0 |
---|
| 222 | std::cerr << "[" << to_bin() << "," << b.to_bin() << " = " << res.to_bin() << "]\n"; |
---|
| 223 | #endif |
---|
| 224 | |
---|
| 225 | inline sc_uint<W+1> operator , (bool b) const |
---|
| 226 | { sc_uint<W+1> res = read(); res <<= 1; res += b; return res; } |
---|
| 227 | #if 0 |
---|
| 228 | std::cerr << "[" << to_bin() << "," << b.to_bin() << " = " << res.to_bin() << "]\n"; |
---|
| 229 | #endif |
---|
| 230 | |
---|
| 231 | template <int W2> |
---|
| 232 | inline sc_uint<W2> operator , (const sc_uint_subref_r &v) const |
---|
| 233 | { std::cerr << "Warning : \n"; return sc_uint<W2> (v.read()); } |
---|
| 234 | |
---|
| 235 | inline sc_uint_subref range (int left, int right) |
---|
| 236 | { return (data_type)((data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right; } |
---|
| 237 | |
---|
| 238 | inline sc_uint_subref_r range (int left, int right) const |
---|
| 239 | { return sc_uint_subref_r (((data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right, left, right); } |
---|
| 240 | #if 0 |
---|
| 241 | std::cerr << "range(" << left << "," << right << ")\n"; |
---|
| 242 | std::cerr << "val = " << val << "\n"; |
---|
| 243 | std::cerr << "~0 >> " << (sizeof (data_type) * 8 - left - 1) << " = " << (data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) << "\n"; |
---|
| 244 | std::cerr << ((data_type) ((((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right) << "\n"; |
---|
| 245 | std::cerr << "data_type = " << sizeof (data_type) << "\n"; |
---|
| 246 | #endif |
---|
| 247 | |
---|
| 248 | }; |
---|
| 249 | |
---|
| 250 | // |
---|
| 251 | // no sign flag to threat |
---|
| 252 | // |
---|
| 253 | |
---|
| 254 | inline std::ostream& operator << (std::ostream &o, const sc_uint_subref_r& s) |
---|
| 255 | { return o << s.val; } |
---|
| 256 | |
---|
| 257 | } /* end of sc_dt namespace */ |
---|
| 258 | |
---|
| 259 | #endif /* __SC_UINT_H__ */ |
---|
| 260 | |
---|