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 __SYSTEMC_EXT_DT_BIT_SC_BIT_HH__
63#define __SYSTEMC_EXT_DT_BIT_SC_BIT_HH__
64
65#include <iostream>
66
67#include "../int/sc_nbdefs.hh"
68
69namespace sc_dt
70{
71
72// classes defined in this module
73class sc_bit;
74
75// forward class declarations
76class sc_logic;
77
78extern void sc_deprecated_sc_bit();
79
80// ----------------------------------------------------------------------------
81//  CLASS : sc_bit
82//
83//  Bit class.
84//  Note: VSIA compatibility indicated.
85// ----------------------------------------------------------------------------
86
87class sc_bit
88{
89    // support methods
90
91    static void invalid_value(char);
92    static void invalid_value(int);
93
94    static bool
95    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
104    to_value(int i)
105    {
106        if (i != 0 && i != 1) {
107            invalid_value(i);
108        }
109        return (i == 0 ? false : true);
110    }
111    static bool to_value(bool b) { return b; }
112
113#define DEFN_TO_VALUE_T(tp) \
114    static bool to_value(tp i) { return to_value((int)i); }
115
116    DEFN_TO_VALUE_T(unsigned)
117    DEFN_TO_VALUE_T(long)
118    DEFN_TO_VALUE_T(unsigned long)
119    DEFN_TO_VALUE_T(int64)
120    DEFN_TO_VALUE_T(uint64)
121
122#undef DEFN_TO_VALUE_T
123
124  public:
125    // constructors
126    // MANDATORY
127    sc_bit() : m_val(false) { sc_deprecated_sc_bit(); }
128
129#define DEFN_CTOR_T(tp)              \
130    explicit sc_bit(tp a) : m_val(to_value(a)) { sc_deprecated_sc_bit(); }
131
132    DEFN_CTOR_T(bool)
133    DEFN_CTOR_T(char)
134    DEFN_CTOR_T(int)
135    DEFN_CTOR_T(unsigned)
136    DEFN_CTOR_T(long)
137    DEFN_CTOR_T(unsigned long)
138    DEFN_CTOR_T(int64)
139    DEFN_CTOR_T(uint64)
140
141#undef DEFN_CTOR_T
142
143    explicit sc_bit(const sc_logic &a);  // non-VSIA
144
145    // copy constructor
146    // MANDATORY
147    sc_bit(const sc_bit &a) : m_val(a.m_val) {}
148
149    // destructor
150    // MANDATORY
151    ~sc_bit() {}
152
153    // assignment operators
154    // MANDATORY
155    sc_bit &
156    operator = (const sc_bit &b)
157    {
158        m_val = b.m_val;
159        return *this;
160    }
161
162#define DEFN_ASN_OP_T(op, tp) \
163    sc_bit &operator op(tp b) { return (*this op sc_bit(b)); }
164#define DEFN_ASN_OP(op) \
165    DEFN_ASN_OP_T(op,int) \
166    DEFN_ASN_OP_T(op,bool) \
167    DEFN_ASN_OP_T(op,char)
168
169    DEFN_ASN_OP(=)
170    DEFN_ASN_OP_T(=,int64)
171    DEFN_ASN_OP_T(=,uint64)
172    DEFN_ASN_OP_T(=,long)
173    DEFN_ASN_OP_T(=,unsigned long)
174
175    sc_bit &operator = (const sc_logic &b); // non-VSIA
176
177    // bitwise assignment operators
178    sc_bit &
179    operator &= (const sc_bit &b)
180    {
181        m_val = (m_val && b.m_val);
182        return *this;
183    }
184
185    sc_bit &
186    operator |= (const sc_bit &b)
187    {
188        m_val = (m_val || b.m_val);
189        return *this;
190    }
191
192    sc_bit &
193    operator ^= (const sc_bit &b)
194    {
195        m_val = (m_val != b.m_val);
196        return *this;
197    }
198
199    DEFN_ASN_OP(&=)
200    DEFN_ASN_OP(|=)
201    DEFN_ASN_OP(^=)
202
203#undef DEFN_ASN_OP_T
204#undef DEFN_ASN_OP
205
206    // conversions
207    // MANDATORY
208
209    // implicit conversion to bool
210    operator bool () const { return m_val; }
211
212    // non-VSIA
213    bool operator ! () const { return !m_val; }
214
215
216    // explicit conversions - non-VSIA
217    bool to_bool() const { return m_val; }
218    char to_char() const { return (m_val ? '1' : '0'); }
219
220    // relational operators and functions
221    // MANDATORY
222    friend bool operator == (const sc_bit &a, const sc_bit &b);
223    friend bool operator != (const sc_bit &a, const sc_bit &b);
224
225    // bitwise operators and functions
226
227    // bitwise complement
228    // MANDATORY
229    friend const sc_bit operator ~ (const sc_bit &a);
230
231    // RECOMMENDED
232    sc_bit &
233    b_not()
234    {
235        m_val = (!m_val);
236        return *this;
237    }
238
239    // binary bit-wise operations
240    friend const sc_bit operator | (const sc_bit &a, const sc_bit &b);
241    friend const sc_bit operator & (const sc_bit &a, const sc_bit &b);
242    friend const sc_bit operator ^ (const sc_bit &a, const sc_bit &b);
243
244    // other methods
245    void print(::std::ostream &os=::std::cout) const { os << to_bool(); }
246    void scan(::std::istream & =::std::cin);
247
248  private:
249    bool m_val;
250};
251
252// ----------------------------------------------------------------------------
253// relational operators and functions
254
255#define DEFN_BIN_FUN_T(ret,fun,tp) \
256    inline ret fun(const sc_bit& a, tp b) { return fun(a, sc_bit(b)); } \
257    inline ret fun(tp b, const sc_bit &a) { return fun(sc_bit(a), b); }
258
259#define DEFN_BIN_FUN(ret,fun) \
260      DEFN_BIN_FUN_T(ret,fun,bool) \
261      DEFN_BIN_FUN_T(ret,fun,char) \
262      DEFN_BIN_FUN_T(ret,fun,int)
263
264// MANDATORY
265inline bool
266operator == (const sc_bit &a, const sc_bit &b)
267{
268    return (a.m_val == b.m_val);
269}
270
271inline bool
272operator != (const sc_bit &a, const sc_bit &b)
273{
274    return (a.m_val != b.m_val);
275}
276
277DEFN_BIN_FUN(bool, operator ==)
278DEFN_BIN_FUN(bool, operator !=)
279
280// OPTIONAL
281
282inline bool equal(const sc_bit &a, const sc_bit &b) { return (a == b); }
283
284inline bool not_equal(const sc_bit &a, const sc_bit &b) { return (a != b); }
285
286DEFN_BIN_FUN(bool,equal)
287DEFN_BIN_FUN(bool,not_equal)
288
289// ----------------------------------------------------------------------------
290// bitwise operators and functions
291
292// bitwise complement
293
294// MANDATORY
295inline const sc_bit operator ~ (const sc_bit &a) { return sc_bit(!a.m_val); }
296
297// OPTIONAL
298inline const sc_bit b_not(const sc_bit &a) { return (~a); }
299
300// RECOMMENDED
301inline void b_not(sc_bit &r, const sc_bit &a) { r = (~a); }
302
303// binary bit-wise operations
304// MANDATORY
305inline const sc_bit
306operator & (const sc_bit &a, const sc_bit &b)
307{
308    return sc_bit(a.m_val && b.m_val);
309}
310
311inline const sc_bit
312operator | (const sc_bit &a, const sc_bit &b)
313{
314    return sc_bit(a.m_val || b.m_val);
315}
316
317inline const sc_bit
318operator ^ (const sc_bit &a, const sc_bit &b)
319{
320    return sc_bit(a.m_val != b.m_val);
321}
322
323DEFN_BIN_FUN(const sc_bit,operator&)
324DEFN_BIN_FUN(const sc_bit,operator|)
325DEFN_BIN_FUN(const sc_bit,operator^)
326
327// OPTIONAL
328inline const sc_bit b_and(const sc_bit &a, const sc_bit &b) { return a & b; }
329inline const sc_bit b_or(const sc_bit &a, const sc_bit &b) { return a | b; }
330inline const sc_bit b_xor(const sc_bit &a, const sc_bit &b) { return a ^ b; }
331
332DEFN_BIN_FUN(const sc_bit,b_and)
333DEFN_BIN_FUN(const sc_bit,b_or)
334DEFN_BIN_FUN(const sc_bit,b_xor)
335
336// RECOMMENDED
337
338#define DEFN_TRN_FUN_T(fun,tp) \
339    inline void \
340    fun(sc_bit &r, const sc_bit &a, tp b) \
341    { r = fun(a, sc_bit(b)); } \
342    inline void \
343    fun(sc_bit &r, tp a, const sc_bit &b) \
344    { r = fun(sc_bit(a), b); }
345
346#define DEFN_TRN_FUN(fun) \
347    inline void \
348    fun(sc_bit &r, const sc_bit &a, const sc_bit &b) { r = fun(a , b); } \
349    DEFN_TRN_FUN_T(fun, int) \
350    DEFN_TRN_FUN_T(fun, bool) \
351    DEFN_TRN_FUN_T(fun, char)
352
353    DEFN_TRN_FUN(b_and)
354    DEFN_TRN_FUN(b_or)
355    DEFN_TRN_FUN(b_xor)
356
357#undef DEFN_BIN_FUN_T
358#undef DEFN_BIN_FUN
359#undef DEFN_TRN_FUN_T
360#undef DEFN_TRN_FUN
361
362
363// ----------------------------------------------------------------------------
364
365inline ::std::ostream &
366operator << (::std::ostream &os, const sc_bit &a)
367{
368    a.print(os);
369    return os;
370}
371
372inline ::std::istream &
373operator >> (::std::istream &is, sc_bit &a)
374{
375    a.scan(is);
376    return is;
377}
378
379} // namespace sc_dt
380
381#endif // __SYSTEMC_EXT_DT_BIT_SC_BIT_HH__
382