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