SystemC  2.3.1
Accellera SystemC proof-of-concept library
sc_bit.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  The following code is derived, directly or indirectly, from the SystemC
4  source code Copyright (c) 1996-2014 by all Contributors.
5  All Rights reserved.
6 
7  The contents of this file are subject to the restrictions and limitations
8  set forth in the SystemC Open Source License (the "License");
9  You may not use this file except in compliance with such restrictions and
10  limitations. You may obtain instructions on how to receive a copy of the
11  License at http://www.accellera.org/. Software distributed by Contributors
12  under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
13  ANY KIND, either express or implied. See the License for the specific
14  language governing rights and limitations under the License.
15 
16  *****************************************************************************/
17 
18 /*****************************************************************************
19 
20  sc_bit.h -- Bit class.
21 
22  Original Author: Stan Y. Liao, Synopsys, Inc.
23 
24  *****************************************************************************/
25 
26 /*****************************************************************************
27 
28  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
29  changes you are making here.
30 
31  Name, Affiliation, Date:
32  Description of Modification:
33 
34  *****************************************************************************/
35 
36 // $Log: sc_bit.h,v $
37 // Revision 1.2 2011/08/07 18:54:19 acg
38 // Philipp A. Hartmann: remove friend function declarations that implement
39 // code, and clean up how bit and logic operators are defined in general.
40 //
41 // Revision 1.1.1.1 2006/12/15 20:20:04 acg
42 // SystemC 2.3
43 //
44 // Revision 1.6 2006/05/08 17:49:59 acg
45 // Andy Goodrich: Added David Long's declarations for friend operators,
46 // functions, and methods, to keep the Microsoft compiler happy.
47 //
48 // Revision 1.5 2006/04/12 20:17:52 acg
49 // Andy Goodrich: enabled deprecation message for sc_bit.
50 //
51 // Revision 1.4 2006/01/24 20:50:55 acg
52 // Andy Goodrich: added warnings indicating that sc_bit is deprecated and that
53 // the C bool data type should be used in its place.
54 //
55 // Revision 1.3 2006/01/13 18:53:53 acg
56 // Andy Goodrich: added $Log command so that CVS comments are reproduced in
57 // the source.
58 //
59 
60 #ifndef SC_BIT_H
61 #define SC_BIT_H
62 
63 
65 #include "sysc/utils/sc_iostream.h"
66 
67 
68 namespace sc_dt
69 {
70 
71 // classes defined in this module
72 class sc_bit;
73 
74 // forward class declarations
75 class sc_logic;
76 
77 extern void sc_deprecated_sc_bit();
78 
79 // ----------------------------------------------------------------------------
80 // CLASS : sc_bit
81 //
82 // Bit class.
83 // Note: VSIA compatibility indicated.
84 // ----------------------------------------------------------------------------
85 
86 class sc_bit
87 {
88  // support methods
89 
90  static void invalid_value( char );
91  static void invalid_value( int );
92 
93  static bool to_value( char c )
94  {
95  if( c != '0' && c != '1' ) {
96  invalid_value( c );
97  }
98  return ( c == '0' ? false : true );
99  }
100 
101  static bool to_value( int i )
102  {
103  if( i != 0 && i != 1 ) {
104  invalid_value( i );
105  }
106  return ( i == 0 ? false : true );
107  }
108  static bool to_value( bool b )
109  { return b; }
110 
111 #define DEFN_TO_VALUE_T(tp) \
112  static bool to_value( tp i ) \
113  { return to_value( (int) i); }
114 
115  DEFN_TO_VALUE_T(unsigned)
116  DEFN_TO_VALUE_T(long)
117  DEFN_TO_VALUE_T(unsigned long)
120 
121 #undef DEFN_TO_VALUE_T
122 
123 public:
124 
125  // constructors
126  // MANDATORY
127 
129  : m_val( false )
130  {
132  }
133 
134 #define DEFN_CTOR_T(tp) \
135  explicit sc_bit( tp a ) \
136  : m_val( to_value(a) ) \
137  { sc_deprecated_sc_bit(); }
138 
139  DEFN_CTOR_T(bool)
140  DEFN_CTOR_T(char)
141  DEFN_CTOR_T(int)
142  DEFN_CTOR_T(unsigned)
143  DEFN_CTOR_T(long)
144  DEFN_CTOR_T(unsigned long)
147 
148 #undef DEFN_CTOR_T
149 
150  explicit sc_bit( const sc_logic& a ); // non-VSIA
151 
152 
153  // copy constructor
154  // MANDATORY
155 
156  sc_bit( const sc_bit& a )
157  : m_val( a.m_val )
158  {}
159 
160 
161  // destructor
162  // MANDATORY
163 
165  {}
166 
167 
168  // assignment operators
169  // MANDATORY
170 
171  sc_bit& operator = ( const sc_bit& b )
172  { m_val = b.m_val; return *this; }
173 
174 #define DEFN_ASN_OP_T(op,tp) \
175  sc_bit& operator op( tp b ) \
176  { return ( *this op sc_bit( b ) ); }
177 #define DEFN_ASN_OP(op) \
178  DEFN_ASN_OP_T(op,int) \
179  DEFN_ASN_OP_T(op,bool) \
180  DEFN_ASN_OP_T(op,char)
181 
182  DEFN_ASN_OP(=)
185  DEFN_ASN_OP_T(=,long)
186  DEFN_ASN_OP_T(=,unsigned long)
187 
188  sc_bit& operator = ( const sc_logic& b ); // non-VSIA
189 
190 
191  // bitwise assignment operators
192 
193  sc_bit& operator &= ( const sc_bit& b )
194  { m_val = ( m_val && b.m_val ); return *this; }
195 
197  { m_val = ( m_val || b.m_val ); return *this; }
198 
200  { m_val = ( m_val != b.m_val ); return *this; }
201 
202  DEFN_ASN_OP(&=)
203  DEFN_ASN_OP(|=)
204  DEFN_ASN_OP(^=)
205 
206 #undef DEFN_ASN_OP_T
207 #undef DEFN_ASN_OP
208 
209  // conversions
210  // MANDATORY
211 
212  // implicit conversion to bool
213 
214  operator bool () const
215  { return m_val; }
216 
217  bool operator ! () const // non-VSIA
218  { return ! m_val; }
219 
220 
221  // explicit conversions
222 
223  bool to_bool() const // non-VSIA
224  { return m_val; }
225 
226  char to_char() const
227  { return ( m_val ? '1' : '0' ); }
228 
229 
230  // relational operators and functions
231 
232  // MANDATORY
233 
234  friend bool operator == ( const sc_bit& a, const sc_bit& b );
235  friend bool operator != ( const sc_bit& a, const sc_bit& b );
236 
237  // bitwise operators and functions
238 
239  // bitwise complement
240 
241  // MANDATORY
242 
243  friend const sc_bit operator ~ ( const sc_bit& a );
244 
245  // RECOMMENDED
246 
248  { m_val = ( ! m_val ); return *this; }
249 
250  // binary bit-wise operations
251 
252  friend const sc_bit operator | ( const sc_bit& a, const sc_bit& b );
253  friend const sc_bit operator & ( const sc_bit& a, const sc_bit& b );
254  friend const sc_bit operator ^ ( const sc_bit& a, const sc_bit& b );
255 
256  // other methods
257 
258  void print( ::std::ostream& os = ::std::cout ) const
259  { os << to_bool(); }
260 
261  void scan( ::std::istream& = ::std::cin );
262 
263 private:
264  bool m_val;
265 };
266 
267 // ----------------------------------------------------------------------------
268 // relational operators and functions
269 
270 #define DEFN_BIN_FUN_T(ret,fun,tp) \
271  inline ret fun( const sc_bit& a, tp b ) \
272  { return fun(a, sc_bit(b) ); } \
273  inline ret fun( tp b, const sc_bit& a ) \
274  { return fun( sc_bit(a), b ); }
275 
276 #define DEFN_BIN_FUN(ret,fun) \
277  DEFN_BIN_FUN_T(ret,fun,bool) \
278  DEFN_BIN_FUN_T(ret,fun,char) \
279  DEFN_BIN_FUN_T(ret,fun,int)
280 
281 // MANDATORY
282 
283 inline bool operator == ( const sc_bit& a, const sc_bit& b )
284  { return ( a.m_val == b.m_val ); }
285 
286 inline bool operator != ( const sc_bit& a, const sc_bit& b )
287  { return ( a.m_val != b.m_val ); }
288 
289 DEFN_BIN_FUN(bool,operator==)
290 DEFN_BIN_FUN(bool,operator!=)
291 
292 // OPTIONAL
293 
294 inline bool equal( const sc_bit& a, const sc_bit& b )
295  { return ( a == b ); }
296 
297 inline bool not_equal( const sc_bit& a, const sc_bit& b )
298  { return ( a != b ); }
299 
300 DEFN_BIN_FUN(bool,equal)
302 
303 // ----------------------------------------------------------------------------
304 // bitwise operators and functions
305 
306 // bitwise complement
307 
308  // MANDATORY
309 
310  inline const sc_bit operator ~ ( const sc_bit& a )
311  { return sc_bit( ! a.m_val ); }
312 
313 
314  // OPTIONAL
315 
316  inline const sc_bit b_not( const sc_bit& a )
317  { return ( ~ a ); }
318 
319 
320  // RECOMMENDED
321 
322  inline void b_not( sc_bit& r, const sc_bit& a )
323  { r = ( ~ a ); }
324 
325  // binary bit-wise operations
326 
327  // MANDATORY
328 
329  inline const sc_bit operator & ( const sc_bit& a, const sc_bit& b )
330  { return sc_bit( a.m_val && b.m_val ); }
331 
332  inline const sc_bit operator | ( const sc_bit& a, const sc_bit& b )
333  { return sc_bit( a.m_val || b.m_val ); }
334 
335  inline const sc_bit operator ^ ( const sc_bit& a, const sc_bit& b )
336  { return sc_bit( a.m_val != b.m_val ); }
337 
338  DEFN_BIN_FUN(const sc_bit,operator&)
339  DEFN_BIN_FUN(const sc_bit,operator|)
340  DEFN_BIN_FUN(const sc_bit,operator^)
341 
342  // OPTIONAL
343 
344  inline const sc_bit b_and ( const sc_bit& a, const sc_bit& b )
345  { return a & b; }
346 
347  inline const sc_bit b_or ( const sc_bit& a, const sc_bit& b )
348  { return a | b; }
349 
350  inline const sc_bit b_xor ( const sc_bit& a, const sc_bit& b )
351  { return a ^ b; }
352 
353  DEFN_BIN_FUN(const sc_bit,b_and)
354  DEFN_BIN_FUN(const sc_bit,b_or)
355  DEFN_BIN_FUN(const sc_bit,b_xor)
356 
357  // RECOMMENDED
358 
359 #define DEFN_TRN_FUN_T(fun,tp) \
360  inline void fun( sc_bit& r, const sc_bit& a, tp b ) \
361  { r = fun( a, sc_bit(b) ); } \
362  inline void fun( sc_bit& r, tp a, const sc_bit& b ) \
363  { r = fun( sc_bit(a), b ); }
364 
365 #define DEFN_TRN_FUN(fun) \
366  inline void fun( sc_bit& r, const sc_bit& a, const sc_bit& b ) \
367  { r = fun( a , b ); } \
368  DEFN_TRN_FUN_T(fun,int) \
369  DEFN_TRN_FUN_T(fun,bool) \
370  DEFN_TRN_FUN_T(fun,char)
371 
373  DEFN_TRN_FUN( b_or )
375 
376 #undef DEFN_BIN_FUN_T
377 #undef DEFN_BIN_FUN
378 #undef DEFN_TRN_FUN_T
379 #undef DEFN_TRN_FUN
380 
381 
382 // ----------------------------------------------------------------------------
383 
384 inline
385 ::std::ostream&
386 operator << ( ::std::ostream& os, const sc_bit& a )
387 {
388  a.print( os );
389  return os;
390 }
391 
392 inline
393 ::std::istream&
394 operator >> ( ::std::istream& is, sc_bit& a )
395 {
396  a.scan( is );
397  return is;
398 }
399 
400 } // namespace sc_dt
401 
402 
403 #endif
404 
405 // Taf!
char to_char() const
Definition: sc_bit.h:226
#define DEFN_TO_VALUE_T(tp)
Definition: sc_bit.h:111
void sc_deprecated_sc_bit()
#define DEFN_TRN_FUN(fun)
Definition: sc_bit.h:365
sc_bit & operator^=(const sc_bit &b)
Definition: sc_bit.h:199
friend const sc_bit operator^(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:335
const sc_bit operator|(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:332
const sc_bit b_or(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:347
#define DEFN_ASN_OP_T(op, tp)
Definition: sc_bit.h:174
bool to_bool() const
Definition: sc_bit.h:223
sc_bit & operator=(const sc_bit &b)
Definition: sc_bit.h:171
uint64_t uint64
Definition: sc_nbdefs.h:183
#define DEFN_ASN_OP(op)
Definition: sc_bit.h:177
inline::std::istream & operator>>(::std::istream &is, sc_bit &a)
Definition: sc_bit.h:394
friend const sc_bit operator&(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:329
uint64 const sc_uint_base int b
Definition: sc_fxval.h:1003
void scan(::std::istream &=::std::cin)
int64_t int64
Definition: sc_nbdefs.h:182
friend const sc_bit operator~(const sc_bit &a)
Definition: sc_bit.h:310
#define DEFN_CTOR_T(tp)
Definition: sc_bit.h:134
const sc_bit operator&(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:329
sc_bit & operator|=(const sc_bit &b)
Definition: sc_bit.h:196
void print(::std::ostream &os=::std::cout) const
Definition: sc_bit.h:258
const sc_bit operator^(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:335
const sc_bit b_and(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:344
friend bool operator==(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:283
const sc_bit b_xor(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:350
bool operator!=(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:286
const sc_bit b_not(const sc_bit &a)
Definition: sc_bit.h:316
bool operator!() const
Definition: sc_bit.h:217
sc_bit & b_not()
Definition: sc_bit.h:247
bool operator==(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:283
#define DEFN_BIN_FUN(ret, fun)
Definition: sc_bit.h:276
friend const sc_bit operator|(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:332
friend bool operator!=(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:286
sc_bit(const sc_bit &a)
Definition: sc_bit.h:156
bool equal(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:294
inline::std::ostream & operator<<(::std::ostream &os, const sc_bit &a)
Definition: sc_bit.h:386
bool not_equal(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:297