source: branches/with_autoconf/src/sc_uint.h @ 8

Last change on this file since 8 was 8, checked in by nipo, 16 years ago

Checkin autotools magic

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