[1] | 1 | /*------------------------------------------------------------\ |
---|
| 2 | | | |
---|
| 3 | | Tool : systemcass | |
---|
| 4 | | | |
---|
| 5 | | File : sc_int.h | |
---|
| 6 | | | |
---|
| 7 | | Author : Buchmann Richard | |
---|
| 8 | | | |
---|
| 9 | | Date : 09_07_2004 | |
---|
| 10 | | | |
---|
| 11 | \------------------------------------------------------------*/ |
---|
| 12 | #ifndef __SC_INT_H__ |
---|
| 13 | #define __SC_INT_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> |
---|
[22] | 20 | #include <cstdlib> |
---|
[59] | 21 | //#include <stdint.h> |
---|
[1] | 22 | |
---|
| 23 | // ---------------------------------------------------------------------------- |
---|
| 24 | // CLASS : sc_int<W> |
---|
| 25 | // |
---|
| 26 | // Template class sc_int<W> is the interface that the user sees. It is |
---|
| 27 | // derived from sc_int_base and most of its methods are just wrappers |
---|
| 28 | // that call the corresponding method in the parent class. Note that |
---|
| 29 | // the width of sc_int datatype is specified as a template parameter. |
---|
| 30 | // ---------------------------------------------------------------------------- |
---|
| 31 | |
---|
[27] | 32 | #include "sc_nbdefs.h" |
---|
[1] | 33 | |
---|
| 34 | namespace sc_dt { |
---|
| 35 | |
---|
| 36 | typedef sc_int<1> sc_int_bit_ref; |
---|
| 37 | typedef sc_int<1> sc_int_bit_ref_r; |
---|
| 38 | |
---|
[52] | 39 | |
---|
[59] | 40 | #define MASK32(W) ((~ (const uint32) 0) >> (sizeof (uint32) * 8 - W)) |
---|
| 41 | #define MASK64(W) ((~ (const uint64) 0) >> (sizeof (uint64) * 8 - W)) |
---|
[1] | 42 | |
---|
[52] | 43 | template<int W> struct s_int_type { typedef int64 int_type; }; |
---|
| 44 | |
---|
[59] | 45 | #define DECLAR_INT_TYPE(W) template<> struct s_int_type<W> { typedef smallest_int int_type; } // not declared as int8 because << operator threats like a character |
---|
[52] | 46 | DECLAR_INT_TYPE(1); |
---|
| 47 | DECLAR_INT_TYPE(2); |
---|
| 48 | DECLAR_INT_TYPE(3); |
---|
| 49 | DECLAR_INT_TYPE(4); |
---|
| 50 | DECLAR_INT_TYPE(5); |
---|
| 51 | DECLAR_INT_TYPE(6); |
---|
| 52 | DECLAR_INT_TYPE(7); |
---|
| 53 | DECLAR_INT_TYPE(8); |
---|
[1] | 54 | #undef DECLAR_INT_TYPE |
---|
[52] | 55 | |
---|
[1] | 56 | #define DECLAR_INT_TYPE(W) template<> struct s_int_type<W> { typedef int16 int_type; } |
---|
[59] | 57 | DECLAR_INT_TYPE(9); |
---|
[1] | 58 | DECLAR_INT_TYPE(10); |
---|
| 59 | DECLAR_INT_TYPE(11); |
---|
| 60 | DECLAR_INT_TYPE(12); |
---|
| 61 | DECLAR_INT_TYPE(13); |
---|
| 62 | DECLAR_INT_TYPE(14); |
---|
| 63 | DECLAR_INT_TYPE(15); |
---|
| 64 | DECLAR_INT_TYPE(16); |
---|
| 65 | #undef DECLAR_INT_TYPE |
---|
[52] | 66 | |
---|
[1] | 67 | #define DECLAR_INT_TYPE(W) template<> struct s_int_type<W> { typedef int32 int_type; } |
---|
| 68 | DECLAR_INT_TYPE(17); |
---|
| 69 | DECLAR_INT_TYPE(18); |
---|
| 70 | DECLAR_INT_TYPE(19); |
---|
| 71 | DECLAR_INT_TYPE(20); |
---|
| 72 | DECLAR_INT_TYPE(21); |
---|
| 73 | DECLAR_INT_TYPE(22); |
---|
| 74 | DECLAR_INT_TYPE(23); |
---|
| 75 | DECLAR_INT_TYPE(24); |
---|
| 76 | DECLAR_INT_TYPE(25); |
---|
| 77 | DECLAR_INT_TYPE(26); |
---|
| 78 | DECLAR_INT_TYPE(27); |
---|
| 79 | DECLAR_INT_TYPE(28); |
---|
| 80 | DECLAR_INT_TYPE(29); |
---|
| 81 | DECLAR_INT_TYPE(30); |
---|
| 82 | DECLAR_INT_TYPE(31); |
---|
| 83 | DECLAR_INT_TYPE(32); |
---|
| 84 | #undef DECLAR_INT_TYPE |
---|
| 85 | |
---|
| 86 | // -------------------------------- |
---|
| 87 | // CLASS : sc_int_subref_r |
---|
| 88 | // |
---|
| 89 | // Used for range, concat functions |
---|
| 90 | |
---|
[52] | 91 | class sc_int_subref_r { |
---|
[1] | 92 | |
---|
[52] | 93 | template <int W> friend class sc_dt::sc_int; |
---|
| 94 | int left, right; |
---|
| 95 | |
---|
| 96 | public : |
---|
| 97 | int64 val; |
---|
| 98 | sc_int_subref_r(int64 val_, int left_, int right_) { |
---|
[59] | 99 | val = val_; |
---|
| 100 | left = left_; |
---|
| 101 | right = right_; |
---|
[52] | 102 | } |
---|
| 103 | |
---|
[59] | 104 | inline int64 read() const { return val; } |
---|
| 105 | inline const sc_int_subref_r & operator | (const sc_int_subref_r & v) const { |
---|
| 106 | print_warning(); |
---|
[52] | 107 | return *this; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | private : |
---|
[59] | 111 | void print_warning() const; |
---|
[52] | 112 | |
---|
[1] | 113 | }; |
---|
| 114 | |
---|
| 115 | // |
---|
| 116 | // to fix : propagation of the sign flag |
---|
| 117 | // |
---|
| 118 | |
---|
| 119 | class sc_int_subref_r; |
---|
| 120 | |
---|
[59] | 121 | template < int W /* = SC_INTWIDTH */> |
---|
[52] | 122 | class sc_int { |
---|
[1] | 123 | |
---|
[52] | 124 | /***********************/ |
---|
| 125 | /* SYSTEMCASS SPECIFIC */ |
---|
| 126 | /***********************/ |
---|
| 127 | typedef sc_int<W> this_type; |
---|
[59] | 128 | public: |
---|
[52] | 129 | typedef typename s_int_type<W>::int_type data_type; |
---|
[59] | 130 | private: |
---|
[52] | 131 | typedef data_type sc_int_subref; |
---|
[1] | 132 | |
---|
[52] | 133 | // internal |
---|
| 134 | union { |
---|
[59] | 135 | val_field<W, (sizeof(data_type) * 8) - W,data_type> vf; /* To compute */ |
---|
[52] | 136 | data_type val; /* To return an int reference (read function) */ |
---|
| 137 | }; |
---|
[1] | 138 | |
---|
| 139 | |
---|
[52] | 140 | /***********************/ |
---|
| 141 | /* L R M */ |
---|
| 142 | /***********************/ |
---|
| 143 | public: |
---|
[59] | 144 | sc_int() { val = 0; } |
---|
| 145 | sc_int(const char * a) { val = 0; write(std::atoi(a)); } |
---|
| 146 | sc_int(unsigned short a) { val = 0; write(a); } |
---|
| 147 | sc_int(short a) { val = 0; write(a); } |
---|
| 148 | sc_int(unsigned long a) { val = 0; write(a); } |
---|
| 149 | sc_int(long a) { val = 0; write(a); } |
---|
| 150 | sc_int(unsigned int a) { val = 0; write(a); } |
---|
| 151 | sc_int(int a) { val = 0; write(a); } |
---|
| 152 | sc_int(int64 a) { val = 0; write(a); } |
---|
| 153 | sc_int(uint64 a) { val = 0; write(a); } |
---|
| 154 | sc_int(double a) { val = 0; write(a); } |
---|
[52] | 155 | |
---|
| 156 | template <int W2> sc_int (const sc_int<W2> &val_) { val = 0; write (val_.read());} |
---|
| 157 | /* this template doesn't redefine default copy constructor of sc_int. |
---|
| 158 | * So, we define by this way |
---|
| 159 | */ |
---|
| 160 | |
---|
[59] | 161 | sc_int(const sc_int & val_) { val = val_.val; } |
---|
| 162 | sc_int(const sc_int_subref_r & a) { val = 0; write(a); } |
---|
[1] | 163 | /* the user needs to cast explicitly result of range () method. */ |
---|
| 164 | |
---|
| 165 | |
---|
[52] | 166 | /***********************/ |
---|
| 167 | /* SYSTEMCASS SPECIFIC */ |
---|
| 168 | /***********************/ |
---|
[1] | 169 | |
---|
[52] | 170 | // read/write |
---|
[59] | 171 | inline const data_type & read() const { return val; } |
---|
| 172 | inline void write(data_type val_) { vf.valW = val_; } |
---|
| 173 | template <int W2> void write(const sc_int<W2> val_) { write (val_.read ()); } |
---|
| 174 | inline void write(const sc_int_subref_r & s) { |
---|
| 175 | write (s.read()); |
---|
| 176 | } |
---|
[1] | 177 | |
---|
[52] | 178 | /***********************/ |
---|
| 179 | /* L R M */ |
---|
| 180 | /***********************/ |
---|
[1] | 181 | |
---|
[52] | 182 | // operators |
---|
| 183 | inline operator const data_type & () const { |
---|
[59] | 184 | return read(); |
---|
[52] | 185 | } |
---|
[22] | 186 | |
---|
[59] | 187 | template < typename T > inline sc_int & operator = (const T & val_) { |
---|
| 188 | write(val_); |
---|
[52] | 189 | return *this; |
---|
| 190 | } |
---|
| 191 | |
---|
[59] | 192 | inline sc_int & operator = (const sc_int_subref_r & a) { |
---|
| 193 | write(a); |
---|
[52] | 194 | return *this; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | // explicit conversions |
---|
| 198 | inline uint32 to_uint() const { return val & MASK32(W); } |
---|
| 199 | inline int32 to_int() const { return val & MASK32(W); } |
---|
| 200 | inline uint64 to_uint64() const { return val & MASK64(W); } |
---|
| 201 | inline int64 to_int64() const { return val & MASK64(W); } |
---|
| 202 | |
---|
| 203 | // explicit conversion to character string |
---|
| 204 | const sc_string to_string(sc_numrep numrep = SC_DEC) const { |
---|
| 205 | return sc_dt::to_string (val, W, numrep); |
---|
| 206 | } |
---|
[59] | 207 | const sc_string to_dec() const { return to_string(SC_DEC); } |
---|
| 208 | const sc_string to_bin() const { return to_string(SC_BIN); } |
---|
| 209 | const sc_string to_oct() const { return to_string(SC_OCT); } |
---|
| 210 | const sc_string to_hex() const { return to_string(SC_HEX); } |
---|
[52] | 211 | |
---|
| 212 | // arithmetic |
---|
| 213 | #define DEFINE_OPERATOR(OPER) \ |
---|
| 214 | template <typename T> \ |
---|
| 215 | inline sc_int & operator OPER (T v) \ |
---|
| 216 | { vf.valW OPER v; return *this; } |
---|
| 217 | |
---|
| 218 | DEFINE_OPERATOR(<<=) |
---|
| 219 | DEFINE_OPERATOR(>>=) |
---|
| 220 | DEFINE_OPERATOR(+=) |
---|
| 221 | DEFINE_OPERATOR(-=) |
---|
| 222 | DEFINE_OPERATOR(*=) |
---|
| 223 | DEFINE_OPERATOR(/=) |
---|
| 224 | DEFINE_OPERATOR(%=) |
---|
| 225 | DEFINE_OPERATOR(&=) |
---|
| 226 | DEFINE_OPERATOR(|=) |
---|
| 227 | DEFINE_OPERATOR(^=) |
---|
[22] | 228 | #undef DEFINE_OPERATOR |
---|
| 229 | |
---|
[52] | 230 | inline sc_int_bit_ref & operator [] (int v) { |
---|
| 231 | return (vf.valW >> v) & 1; |
---|
| 232 | } |
---|
| 233 | inline sc_int_bit_ref_r & operator [] (int v) const { |
---|
| 234 | return (vf.valW >> v) & 1; |
---|
| 235 | } |
---|
[1] | 236 | |
---|
[52] | 237 | template <int W2> |
---|
| 238 | inline sc_int<W + W2> operator , (const sc_int<W2> & b) const { |
---|
[59] | 239 | sc_int<W + W2> res = read() << W2; |
---|
| 240 | res += b.read(); |
---|
[52] | 241 | return res; |
---|
| 242 | } |
---|
[1] | 243 | |
---|
[52] | 244 | inline sc_int<W + 1> operator , (bool b) const { |
---|
| 245 | sc_int<W + 1> res = read(); res <<= 1; res += b; |
---|
| 246 | return res; |
---|
| 247 | } |
---|
[1] | 248 | |
---|
[52] | 249 | template <int W2> |
---|
[59] | 250 | inline sc_int<W2> operator , (const sc_int_subref_r & v) const { |
---|
[52] | 251 | std::cerr << "Warning : \n"; |
---|
| 252 | return sc_int<W2> (v.read()); |
---|
| 253 | } |
---|
[1] | 254 | |
---|
[59] | 255 | inline sc_int_subref range(int left, int right) { |
---|
[52] | 256 | return (data_type)((data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right; |
---|
| 257 | } |
---|
[1] | 258 | |
---|
[59] | 259 | inline sc_int_subref_r range(int left, int right) const { |
---|
| 260 | return sc_int_subref_r(((data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right, left, right); |
---|
[52] | 261 | } |
---|
[59] | 262 | |
---|
[1] | 263 | #if 0 |
---|
[52] | 264 | std::cerr << "range(" << left << "," << right << ")\n"; |
---|
| 265 | std::cerr << "val = " << val << "\n"; |
---|
| 266 | std::cerr << "~0 >> " << (sizeof (data_type) * 8 - left - 1) << " = " << (data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) << "\n"; |
---|
| 267 | std::cerr << ((data_type) ((((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right) << "\n"; |
---|
| 268 | std::cerr << "data_type = " << sizeof (data_type) << "\n"; |
---|
[1] | 269 | #endif |
---|
| 270 | |
---|
| 271 | }; |
---|
| 272 | |
---|
[59] | 273 | inline std::ostream & operator << (std::ostream & o, const sc_int_subref_r & s) { |
---|
[52] | 274 | return o << s.val; |
---|
| 275 | } |
---|
[1] | 276 | |
---|
[52] | 277 | #undef MASK32 |
---|
| 278 | #undef MASK64 |
---|
| 279 | |
---|
[1] | 280 | } /* end of sc_dt namespace */ |
---|
| 281 | |
---|
| 282 | #endif /* __SC_INT_H__ */ |
---|
| 283 | |
---|
[52] | 284 | /* |
---|
| 285 | # Local Variables: |
---|
| 286 | # tab-width: 4; |
---|
| 287 | # c-basic-offset: 4; |
---|
| 288 | # c-file-offsets:((innamespace . 0)(inline-open . 0)); |
---|
| 289 | # indent-tabs-mode: nil; |
---|
| 290 | # End: |
---|
| 291 | # |
---|
| 292 | # vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 293 | */ |
---|
| 294 | |
---|