112853Sgabeblack@google.com/*****************************************************************************
212853Sgabeblack@google.com
312853Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412853Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
512853Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
612853Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
712853Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
812853Sgabeblack@google.com  License.  You may obtain a copy of the License at
912853Sgabeblack@google.com
1012853Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1112853Sgabeblack@google.com
1212853Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1312853Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1412853Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512853Sgabeblack@google.com  implied.  See the License for the specific language governing
1612853Sgabeblack@google.com  permissions and limitations under the License.
1712853Sgabeblack@google.com
1812853Sgabeblack@google.com *****************************************************************************/
1912853Sgabeblack@google.com
2012853Sgabeblack@google.com/*****************************************************************************
2112853Sgabeblack@google.com
2212853Sgabeblack@google.com  sc_bit.h -- Bit class.
2312853Sgabeblack@google.com
2412853Sgabeblack@google.com  Original Author: Stan Y. Liao, Synopsys, Inc.
2512853Sgabeblack@google.com
2612853Sgabeblack@google.com *****************************************************************************/
2712853Sgabeblack@google.com
2812853Sgabeblack@google.com/*****************************************************************************
2912853Sgabeblack@google.com
3012853Sgabeblack@google.com  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
3112853Sgabeblack@google.com  changes you are making here.
3212853Sgabeblack@google.com
3312853Sgabeblack@google.com      Name, Affiliation, Date:
3412853Sgabeblack@google.com  Description of Modification:
3512853Sgabeblack@google.com
3612853Sgabeblack@google.com *****************************************************************************/
3712853Sgabeblack@google.com
3812853Sgabeblack@google.com// $Log: sc_bit.h,v $
3912853Sgabeblack@google.com// Revision 1.2  2011/08/07 18:54:19  acg
4012853Sgabeblack@google.com//  Philipp A. Hartmann: remove friend function declarations that implement
4112853Sgabeblack@google.com//  code, and clean up how bit and logic operators are defined in general.
4212853Sgabeblack@google.com//
4312853Sgabeblack@google.com// Revision 1.1.1.1  2006/12/15 20:20:04  acg
4412853Sgabeblack@google.com// SystemC 2.3
4512853Sgabeblack@google.com//
4612853Sgabeblack@google.com// Revision 1.6  2006/05/08 17:49:59  acg
4712853Sgabeblack@google.com//   Andy Goodrich: Added David Long's declarations for friend operators,
4812853Sgabeblack@google.com//   functions, and methods, to keep the Microsoft compiler happy.
4912853Sgabeblack@google.com//
5012853Sgabeblack@google.com// Revision 1.5  2006/04/12 20:17:52  acg
5112853Sgabeblack@google.com//  Andy Goodrich: enabled deprecation message for sc_bit.
5212853Sgabeblack@google.com//
5312853Sgabeblack@google.com// Revision 1.4  2006/01/24 20:50:55  acg
5412853Sgabeblack@google.com// Andy Goodrich: added warnings indicating that sc_bit is deprecated and that
5512853Sgabeblack@google.com// the C bool data type should be used in its place.
5612853Sgabeblack@google.com//
5712853Sgabeblack@google.com// Revision 1.3  2006/01/13 18:53:53  acg
5812853Sgabeblack@google.com// Andy Goodrich: added $Log command so that CVS comments are reproduced in
5912853Sgabeblack@google.com// the source.
6012853Sgabeblack@google.com//
6112853Sgabeblack@google.com
6212853Sgabeblack@google.com#ifndef __SYSTEMC_EXT_DT_BIT_SC_BIT_HH__
6312853Sgabeblack@google.com#define __SYSTEMC_EXT_DT_BIT_SC_BIT_HH__
6412853Sgabeblack@google.com
6512853Sgabeblack@google.com#include <iostream>
6612853Sgabeblack@google.com
6712853Sgabeblack@google.com#include "../int/sc_nbdefs.hh"
6812853Sgabeblack@google.com
6912853Sgabeblack@google.comnamespace sc_dt
7012853Sgabeblack@google.com{
7112853Sgabeblack@google.com
7212853Sgabeblack@google.com// classes defined in this module
7312853Sgabeblack@google.comclass sc_bit;
7412853Sgabeblack@google.com
7512853Sgabeblack@google.com// forward class declarations
7612853Sgabeblack@google.comclass sc_logic;
7712853Sgabeblack@google.com
7812853Sgabeblack@google.comextern void sc_deprecated_sc_bit();
7912853Sgabeblack@google.com
8012853Sgabeblack@google.com// ----------------------------------------------------------------------------
8112853Sgabeblack@google.com//  CLASS : sc_bit
8212853Sgabeblack@google.com//
8312853Sgabeblack@google.com//  Bit class.
8412853Sgabeblack@google.com//  Note: VSIA compatibility indicated.
8512853Sgabeblack@google.com// ----------------------------------------------------------------------------
8612853Sgabeblack@google.com
8712853Sgabeblack@google.comclass sc_bit
8812853Sgabeblack@google.com{
8912853Sgabeblack@google.com    // support methods
9012853Sgabeblack@google.com
9112853Sgabeblack@google.com    static void invalid_value(char);
9212853Sgabeblack@google.com    static void invalid_value(int);
9312853Sgabeblack@google.com
9412853Sgabeblack@google.com    static bool
9512853Sgabeblack@google.com    to_value(char c)
9612853Sgabeblack@google.com    {
9712853Sgabeblack@google.com        if (c != '0' && c != '1') {
9812853Sgabeblack@google.com            invalid_value(c);
9912853Sgabeblack@google.com        }
10012853Sgabeblack@google.com        return (c == '0' ? false : true);
10112853Sgabeblack@google.com    }
10212853Sgabeblack@google.com
10312853Sgabeblack@google.com    static bool
10412853Sgabeblack@google.com    to_value(int i)
10512853Sgabeblack@google.com    {
10612853Sgabeblack@google.com        if (i != 0 && i != 1) {
10712853Sgabeblack@google.com            invalid_value(i);
10812853Sgabeblack@google.com        }
10912853Sgabeblack@google.com        return (i == 0 ? false : true);
11012853Sgabeblack@google.com    }
11112853Sgabeblack@google.com    static bool to_value(bool b) { return b; }
11212853Sgabeblack@google.com
11312853Sgabeblack@google.com#define DEFN_TO_VALUE_T(tp) \
11412853Sgabeblack@google.com    static bool to_value(tp i) { return to_value((int)i); }
11512853Sgabeblack@google.com
11612853Sgabeblack@google.com    DEFN_TO_VALUE_T(unsigned)
11712853Sgabeblack@google.com    DEFN_TO_VALUE_T(long)
11812853Sgabeblack@google.com    DEFN_TO_VALUE_T(unsigned long)
11912853Sgabeblack@google.com    DEFN_TO_VALUE_T(int64)
12012853Sgabeblack@google.com    DEFN_TO_VALUE_T(uint64)
12112853Sgabeblack@google.com
12212853Sgabeblack@google.com#undef DEFN_TO_VALUE_T
12312853Sgabeblack@google.com
12412853Sgabeblack@google.com  public:
12512853Sgabeblack@google.com    // constructors
12612853Sgabeblack@google.com    // MANDATORY
12712853Sgabeblack@google.com    sc_bit() : m_val(false) { sc_deprecated_sc_bit(); }
12812853Sgabeblack@google.com
12912853Sgabeblack@google.com#define DEFN_CTOR_T(tp)              \
13012853Sgabeblack@google.com    explicit sc_bit(tp a) : m_val(to_value(a)) { sc_deprecated_sc_bit(); }
13112853Sgabeblack@google.com
13212853Sgabeblack@google.com    DEFN_CTOR_T(bool)
13312853Sgabeblack@google.com    DEFN_CTOR_T(char)
13412853Sgabeblack@google.com    DEFN_CTOR_T(int)
13512853Sgabeblack@google.com    DEFN_CTOR_T(unsigned)
13612853Sgabeblack@google.com    DEFN_CTOR_T(long)
13712853Sgabeblack@google.com    DEFN_CTOR_T(unsigned long)
13812853Sgabeblack@google.com    DEFN_CTOR_T(int64)
13912853Sgabeblack@google.com    DEFN_CTOR_T(uint64)
14012853Sgabeblack@google.com
14112853Sgabeblack@google.com#undef DEFN_CTOR_T
14212853Sgabeblack@google.com
14312853Sgabeblack@google.com    explicit sc_bit(const sc_logic &a);  // non-VSIA
14412853Sgabeblack@google.com
14512853Sgabeblack@google.com    // copy constructor
14612853Sgabeblack@google.com    // MANDATORY
14712853Sgabeblack@google.com    sc_bit(const sc_bit &a) : m_val(a.m_val) {}
14812853Sgabeblack@google.com
14912853Sgabeblack@google.com    // destructor
15012853Sgabeblack@google.com    // MANDATORY
15112853Sgabeblack@google.com    ~sc_bit() {}
15212853Sgabeblack@google.com
15312853Sgabeblack@google.com    // assignment operators
15412853Sgabeblack@google.com    // MANDATORY
15512853Sgabeblack@google.com    sc_bit &
15612853Sgabeblack@google.com    operator = (const sc_bit &b)
15712853Sgabeblack@google.com    {
15812853Sgabeblack@google.com        m_val = b.m_val;
15912853Sgabeblack@google.com        return *this;
16012853Sgabeblack@google.com    }
16112853Sgabeblack@google.com
16212853Sgabeblack@google.com#define DEFN_ASN_OP_T(op, tp) \
16312853Sgabeblack@google.com    sc_bit &operator op(tp b) { return (*this op sc_bit(b)); }
16412853Sgabeblack@google.com#define DEFN_ASN_OP(op) \
16512853Sgabeblack@google.com    DEFN_ASN_OP_T(op,int) \
16612853Sgabeblack@google.com    DEFN_ASN_OP_T(op,bool) \
16712853Sgabeblack@google.com    DEFN_ASN_OP_T(op,char)
16812853Sgabeblack@google.com
16912853Sgabeblack@google.com    DEFN_ASN_OP(=)
17012853Sgabeblack@google.com    DEFN_ASN_OP_T(=,int64)
17112853Sgabeblack@google.com    DEFN_ASN_OP_T(=,uint64)
17212853Sgabeblack@google.com    DEFN_ASN_OP_T(=,long)
17312853Sgabeblack@google.com    DEFN_ASN_OP_T(=,unsigned long)
17412853Sgabeblack@google.com
17512853Sgabeblack@google.com    sc_bit &operator = (const sc_logic &b); // non-VSIA
17612853Sgabeblack@google.com
17712853Sgabeblack@google.com    // bitwise assignment operators
17812853Sgabeblack@google.com    sc_bit &
17912853Sgabeblack@google.com    operator &= (const sc_bit &b)
18012853Sgabeblack@google.com    {
18112853Sgabeblack@google.com        m_val = (m_val && b.m_val);
18212853Sgabeblack@google.com        return *this;
18312853Sgabeblack@google.com    }
18412853Sgabeblack@google.com
18512853Sgabeblack@google.com    sc_bit &
18612853Sgabeblack@google.com    operator |= (const sc_bit &b)
18712853Sgabeblack@google.com    {
18812853Sgabeblack@google.com        m_val = (m_val || b.m_val);
18912853Sgabeblack@google.com        return *this;
19012853Sgabeblack@google.com    }
19112853Sgabeblack@google.com
19212853Sgabeblack@google.com    sc_bit &
19312853Sgabeblack@google.com    operator ^= (const sc_bit &b)
19412853Sgabeblack@google.com    {
19512853Sgabeblack@google.com        m_val = (m_val != b.m_val);
19612853Sgabeblack@google.com        return *this;
19712853Sgabeblack@google.com    }
19812853Sgabeblack@google.com
19912853Sgabeblack@google.com    DEFN_ASN_OP(&=)
20012853Sgabeblack@google.com    DEFN_ASN_OP(|=)
20112853Sgabeblack@google.com    DEFN_ASN_OP(^=)
20212853Sgabeblack@google.com
20312853Sgabeblack@google.com#undef DEFN_ASN_OP_T
20412853Sgabeblack@google.com#undef DEFN_ASN_OP
20512853Sgabeblack@google.com
20612853Sgabeblack@google.com    // conversions
20712853Sgabeblack@google.com    // MANDATORY
20812853Sgabeblack@google.com
20912853Sgabeblack@google.com    // implicit conversion to bool
21012853Sgabeblack@google.com    operator bool () const { return m_val; }
21112853Sgabeblack@google.com
21212853Sgabeblack@google.com    // non-VSIA
21312853Sgabeblack@google.com    bool operator ! () const { return !m_val; }
21412853Sgabeblack@google.com
21512853Sgabeblack@google.com
21612853Sgabeblack@google.com    // explicit conversions - non-VSIA
21712853Sgabeblack@google.com    bool to_bool() const { return m_val; }
21812853Sgabeblack@google.com    char to_char() const { return (m_val ? '1' : '0'); }
21912853Sgabeblack@google.com
22012853Sgabeblack@google.com    // relational operators and functions
22112853Sgabeblack@google.com    // MANDATORY
22212853Sgabeblack@google.com    friend bool operator == (const sc_bit &a, const sc_bit &b);
22312853Sgabeblack@google.com    friend bool operator != (const sc_bit &a, const sc_bit &b);
22412853Sgabeblack@google.com
22512853Sgabeblack@google.com    // bitwise operators and functions
22612853Sgabeblack@google.com
22712853Sgabeblack@google.com    // bitwise complement
22812853Sgabeblack@google.com    // MANDATORY
22912853Sgabeblack@google.com    friend const sc_bit operator ~ (const sc_bit &a);
23012853Sgabeblack@google.com
23112853Sgabeblack@google.com    // RECOMMENDED
23212853Sgabeblack@google.com    sc_bit &
23312853Sgabeblack@google.com    b_not()
23412853Sgabeblack@google.com    {
23512853Sgabeblack@google.com        m_val = (!m_val);
23612853Sgabeblack@google.com        return *this;
23712853Sgabeblack@google.com    }
23812853Sgabeblack@google.com
23912853Sgabeblack@google.com    // binary bit-wise operations
24012853Sgabeblack@google.com    friend const sc_bit operator | (const sc_bit &a, const sc_bit &b);
24112853Sgabeblack@google.com    friend const sc_bit operator & (const sc_bit &a, const sc_bit &b);
24212853Sgabeblack@google.com    friend const sc_bit operator ^ (const sc_bit &a, const sc_bit &b);
24312853Sgabeblack@google.com
24412853Sgabeblack@google.com    // other methods
24512853Sgabeblack@google.com    void print(::std::ostream &os=::std::cout) const { os << to_bool(); }
24612853Sgabeblack@google.com    void scan(::std::istream & =::std::cin);
24712853Sgabeblack@google.com
24812853Sgabeblack@google.com  private:
24912853Sgabeblack@google.com    bool m_val;
25012853Sgabeblack@google.com};
25112853Sgabeblack@google.com
25212853Sgabeblack@google.com// ----------------------------------------------------------------------------
25312853Sgabeblack@google.com// relational operators and functions
25412853Sgabeblack@google.com
25512853Sgabeblack@google.com#define DEFN_BIN_FUN_T(ret,fun,tp) \
25612853Sgabeblack@google.com    inline ret fun(const sc_bit& a, tp b) { return fun(a, sc_bit(b)); } \
25712853Sgabeblack@google.com    inline ret fun(tp b, const sc_bit &a) { return fun(sc_bit(a), b); }
25812853Sgabeblack@google.com
25912853Sgabeblack@google.com#define DEFN_BIN_FUN(ret,fun) \
26012853Sgabeblack@google.com      DEFN_BIN_FUN_T(ret,fun,bool) \
26112853Sgabeblack@google.com      DEFN_BIN_FUN_T(ret,fun,char) \
26212853Sgabeblack@google.com      DEFN_BIN_FUN_T(ret,fun,int)
26312853Sgabeblack@google.com
26412853Sgabeblack@google.com// MANDATORY
26512853Sgabeblack@google.cominline bool
26612853Sgabeblack@google.comoperator == (const sc_bit &a, const sc_bit &b)
26712853Sgabeblack@google.com{
26812853Sgabeblack@google.com    return (a.m_val == b.m_val);
26912853Sgabeblack@google.com}
27012853Sgabeblack@google.com
27112853Sgabeblack@google.cominline bool
27212853Sgabeblack@google.comoperator != (const sc_bit &a, const sc_bit &b)
27312853Sgabeblack@google.com{
27412853Sgabeblack@google.com    return (a.m_val != b.m_val);
27512853Sgabeblack@google.com}
27612853Sgabeblack@google.com
27712853Sgabeblack@google.comDEFN_BIN_FUN(bool, operator ==)
27812853Sgabeblack@google.comDEFN_BIN_FUN(bool, operator !=)
27912853Sgabeblack@google.com
28012853Sgabeblack@google.com// OPTIONAL
28112853Sgabeblack@google.com
28212853Sgabeblack@google.cominline bool equal(const sc_bit &a, const sc_bit &b) { return (a == b); }
28312853Sgabeblack@google.com
28412853Sgabeblack@google.cominline bool not_equal(const sc_bit &a, const sc_bit &b) { return (a != b); }
28512853Sgabeblack@google.com
28612853Sgabeblack@google.comDEFN_BIN_FUN(bool,equal)
28712853Sgabeblack@google.comDEFN_BIN_FUN(bool,not_equal)
28812853Sgabeblack@google.com
28912853Sgabeblack@google.com// ----------------------------------------------------------------------------
29012853Sgabeblack@google.com// bitwise operators and functions
29112853Sgabeblack@google.com
29212853Sgabeblack@google.com// bitwise complement
29312853Sgabeblack@google.com
29412853Sgabeblack@google.com// MANDATORY
29512853Sgabeblack@google.cominline const sc_bit operator ~ (const sc_bit &a) { return sc_bit(!a.m_val); }
29612853Sgabeblack@google.com
29712853Sgabeblack@google.com// OPTIONAL
29812853Sgabeblack@google.cominline const sc_bit b_not(const sc_bit &a) { return (~a); }
29912853Sgabeblack@google.com
30012853Sgabeblack@google.com// RECOMMENDED
30112853Sgabeblack@google.cominline void b_not(sc_bit &r, const sc_bit &a) { r = (~a); }
30212853Sgabeblack@google.com
30312853Sgabeblack@google.com// binary bit-wise operations
30412853Sgabeblack@google.com// MANDATORY
30512853Sgabeblack@google.cominline const sc_bit
30612853Sgabeblack@google.comoperator & (const sc_bit &a, const sc_bit &b)
30712853Sgabeblack@google.com{
30812853Sgabeblack@google.com    return sc_bit(a.m_val && b.m_val);
30912853Sgabeblack@google.com}
31012853Sgabeblack@google.com
31112853Sgabeblack@google.cominline const sc_bit
31212853Sgabeblack@google.comoperator | (const sc_bit &a, const sc_bit &b)
31312853Sgabeblack@google.com{
31412853Sgabeblack@google.com    return sc_bit(a.m_val || b.m_val);
31512853Sgabeblack@google.com}
31612853Sgabeblack@google.com
31712853Sgabeblack@google.cominline const sc_bit
31812853Sgabeblack@google.comoperator ^ (const sc_bit &a, const sc_bit &b)
31912853Sgabeblack@google.com{
32012853Sgabeblack@google.com    return sc_bit(a.m_val != b.m_val);
32112853Sgabeblack@google.com}
32212853Sgabeblack@google.com
32312853Sgabeblack@google.comDEFN_BIN_FUN(const sc_bit,operator&)
32412853Sgabeblack@google.comDEFN_BIN_FUN(const sc_bit,operator|)
32512853Sgabeblack@google.comDEFN_BIN_FUN(const sc_bit,operator^)
32612853Sgabeblack@google.com
32712853Sgabeblack@google.com// OPTIONAL
32812853Sgabeblack@google.cominline const sc_bit b_and(const sc_bit &a, const sc_bit &b) { return a & b; }
32912853Sgabeblack@google.cominline const sc_bit b_or(const sc_bit &a, const sc_bit &b) { return a | b; }
33012853Sgabeblack@google.cominline const sc_bit b_xor(const sc_bit &a, const sc_bit &b) { return a ^ b; }
33112853Sgabeblack@google.com
33212853Sgabeblack@google.comDEFN_BIN_FUN(const sc_bit,b_and)
33312853Sgabeblack@google.comDEFN_BIN_FUN(const sc_bit,b_or)
33412853Sgabeblack@google.comDEFN_BIN_FUN(const sc_bit,b_xor)
33512853Sgabeblack@google.com
33612853Sgabeblack@google.com// RECOMMENDED
33712853Sgabeblack@google.com
33812853Sgabeblack@google.com#define DEFN_TRN_FUN_T(fun,tp) \
33912853Sgabeblack@google.com    inline void \
34012853Sgabeblack@google.com    fun(sc_bit &r, const sc_bit &a, tp b) \
34112853Sgabeblack@google.com    { r = fun(a, sc_bit(b)); } \
34212853Sgabeblack@google.com    inline void \
34312853Sgabeblack@google.com    fun(sc_bit &r, tp a, const sc_bit &b) \
34412853Sgabeblack@google.com    { r = fun(sc_bit(a), b); }
34512853Sgabeblack@google.com
34612853Sgabeblack@google.com#define DEFN_TRN_FUN(fun) \
34712853Sgabeblack@google.com    inline void \
34812853Sgabeblack@google.com    fun(sc_bit &r, const sc_bit &a, const sc_bit &b) { r = fun(a , b); } \
34912853Sgabeblack@google.com    DEFN_TRN_FUN_T(fun, int) \
35012853Sgabeblack@google.com    DEFN_TRN_FUN_T(fun, bool) \
35112853Sgabeblack@google.com    DEFN_TRN_FUN_T(fun, char)
35212853Sgabeblack@google.com
35312853Sgabeblack@google.com    DEFN_TRN_FUN(b_and)
35412853Sgabeblack@google.com    DEFN_TRN_FUN(b_or)
35512853Sgabeblack@google.com    DEFN_TRN_FUN(b_xor)
35612853Sgabeblack@google.com
35712853Sgabeblack@google.com#undef DEFN_BIN_FUN_T
35812853Sgabeblack@google.com#undef DEFN_BIN_FUN
35912853Sgabeblack@google.com#undef DEFN_TRN_FUN_T
36012853Sgabeblack@google.com#undef DEFN_TRN_FUN
36112853Sgabeblack@google.com
36212853Sgabeblack@google.com
36312853Sgabeblack@google.com// ----------------------------------------------------------------------------
36412853Sgabeblack@google.com
36512853Sgabeblack@google.cominline ::std::ostream &
36612853Sgabeblack@google.comoperator << (::std::ostream &os, const sc_bit &a)
36712853Sgabeblack@google.com{
36812853Sgabeblack@google.com    a.print(os);
36912853Sgabeblack@google.com    return os;
37012853Sgabeblack@google.com}
37112853Sgabeblack@google.com
37212853Sgabeblack@google.cominline ::std::istream &
37312853Sgabeblack@google.comoperator >> (::std::istream &is, sc_bit &a)
37412853Sgabeblack@google.com{
37512853Sgabeblack@google.com    a.scan(is);
37612853Sgabeblack@google.com    return is;
37712853Sgabeblack@google.com}
37812853Sgabeblack@google.com
37912853Sgabeblack@google.com} // namespace sc_dt
38012853Sgabeblack@google.com
38112853Sgabeblack@google.com#endif // __SYSTEMC_EXT_DT_BIT_SC_BIT_HH__
382