source: sources/src/sc_int.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: 8.7 KB
Line 
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>
20#include <cstdlib>
21//#include <stdint.h>
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
32#include "sc_nbdefs.h"
33
34namespace sc_dt {
35
36typedef sc_int<1> sc_int_bit_ref;
37typedef sc_int<1> sc_int_bit_ref_r;
38
39
40#define MASK32(W) ((~ (const uint32) 0) >> (sizeof (uint32) * 8 - W))
41#define MASK64(W) ((~ (const uint64) 0) >> (sizeof (uint64) * 8 - W))
42
43template<int W> struct s_int_type { typedef int64 int_type; };
44
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
46DECLAR_INT_TYPE(1);
47DECLAR_INT_TYPE(2);
48DECLAR_INT_TYPE(3);
49DECLAR_INT_TYPE(4);
50DECLAR_INT_TYPE(5);
51DECLAR_INT_TYPE(6);
52DECLAR_INT_TYPE(7);
53DECLAR_INT_TYPE(8);
54#undef DECLAR_INT_TYPE
55
56#define DECLAR_INT_TYPE(W) template<> struct s_int_type<W> { typedef int16 int_type; }
57DECLAR_INT_TYPE(9);
58DECLAR_INT_TYPE(10);
59DECLAR_INT_TYPE(11);
60DECLAR_INT_TYPE(12);
61DECLAR_INT_TYPE(13);
62DECLAR_INT_TYPE(14);
63DECLAR_INT_TYPE(15);
64DECLAR_INT_TYPE(16);
65#undef DECLAR_INT_TYPE
66
67#define DECLAR_INT_TYPE(W) template<> struct s_int_type<W> { typedef int32 int_type; }
68DECLAR_INT_TYPE(17);
69DECLAR_INT_TYPE(18);
70DECLAR_INT_TYPE(19);
71DECLAR_INT_TYPE(20);
72DECLAR_INT_TYPE(21);
73DECLAR_INT_TYPE(22);
74DECLAR_INT_TYPE(23);
75DECLAR_INT_TYPE(24);
76DECLAR_INT_TYPE(25);
77DECLAR_INT_TYPE(26);
78DECLAR_INT_TYPE(27);
79DECLAR_INT_TYPE(28);
80DECLAR_INT_TYPE(29);
81DECLAR_INT_TYPE(30);
82DECLAR_INT_TYPE(31);
83DECLAR_INT_TYPE(32);
84#undef DECLAR_INT_TYPE
85
86// --------------------------------
87// CLASS : sc_int_subref_r
88//
89// Used for range, concat functions
90
91class sc_int_subref_r {
92
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_) {
99        val = val_;
100        left = left_;
101        right = right_;
102    }
103   
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();
107        return *this;
108    }
109
110    private :
111    void print_warning() const;
112
113};
114
115//
116// to fix : propagation of the sign flag
117//
118
119class sc_int_subref_r;
120
121template < int W /* = SC_INTWIDTH */>
122class sc_int {
123
124    /***********************/
125    /* SYSTEMCASS SPECIFIC */
126    /***********************/
127    typedef sc_int<W> this_type;
128    public:
129    typedef typename s_int_type<W>::int_type data_type;
130    private:
131    typedef data_type sc_int_subref;
132
133    // internal
134    union {
135        val_field<W, (sizeof(data_type) * 8) - W,data_type> vf; /* To compute */
136        data_type val;          /* To return an int reference (read function) */
137    };
138
139
140    /***********************/
141    /*        L R M        */
142    /***********************/
143    public:
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); }
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
161    sc_int(const sc_int & val_) { val = val_.val; }
162    sc_int(const sc_int_subref_r & a) { val = 0; write(a); } 
163    /* the user needs to cast explicitly result of range () method. */
164
165
166    /***********************/
167    /* SYSTEMCASS SPECIFIC */
168    /***********************/
169
170    // read/write
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    }
177
178    /***********************/
179    /*        L R M        */
180    /***********************/
181
182    // operators
183    inline operator const data_type & () const {
184        return read();
185    }
186
187    template < typename T > inline sc_int & operator = (const T & val_) {
188        write(val_);
189        return *this;
190    }
191
192    inline sc_int & operator = (const sc_int_subref_r & a) {
193        write(a);
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    }
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); }
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(^=)
228#undef DEFINE_OPERATOR
229
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    }
236
237    template <int W2> 
238    inline sc_int<W + W2> operator , (const sc_int<W2> & b) const {
239        sc_int<W + W2> res = read() << W2;
240        res += b.read();
241        return res;
242    }
243
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    }
248
249    template <int W2>
250    inline sc_int<W2> operator , (const sc_int_subref_r & v) const {
251        std::cerr << "Warning : \n";
252        return sc_int<W2> (v.read());
253    }
254
255    inline sc_int_subref range(int left, int right) {
256        return (data_type)((data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right;
257    }
258
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);
261    }
262
263#if 0
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";
269#endif
270
271};
272
273inline std::ostream & operator << (std::ostream & o, const sc_int_subref_r & s) {
274    return o << s.val;
275}
276
277#undef MASK32
278#undef MASK64
279
280} /* end of sc_dt namespace */
281
282#endif /* __SC_INT_H__ */
283
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
Note: See TracBrowser for help on using the repository browser.