source: latest/src/sc_int.h @ 1

Last change on this file since 1 was 1, checked in by buchmann, 17 years ago

Initial import from CVS repository

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