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