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