source: sources/src/sc_uint.h

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