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