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_bv_base.h -- Arbitrary size bit vector class.
2312853Sgabeblack@google.com
2412853Sgabeblack@google.com  Original Author: Gene Bushuyev, 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_bv_base.h,v $
3912853Sgabeblack@google.com// Revision 1.3  2011/08/26 22:32:00  acg
4012853Sgabeblack@google.com//  Torsten Maehne: added parentheses to make opearator ordering more obvious.
4112853Sgabeblack@google.com//
4212853Sgabeblack@google.com// Revision 1.2  2011/08/15 16:43:24  acg
4312853Sgabeblack@google.com//  Torsten Maehne: changes to remove unused argument warnings.
4412853Sgabeblack@google.com//
4512853Sgabeblack@google.com// Revision 1.1.1.1  2006/12/15 20:20:04  acg
4612853Sgabeblack@google.com// SystemC 2.3
4712853Sgabeblack@google.com//
4812853Sgabeblack@google.com// Revision 1.3  2006/01/13 18:53:53  acg
4912853Sgabeblack@google.com// Andy Goodrich: added $Log command so that CVS comments are reproduced in
5012853Sgabeblack@google.com// the source.
5112853Sgabeblack@google.com//
5212853Sgabeblack@google.com
5312853Sgabeblack@google.com#ifndef __SYSTEMC_EXT_DT_BIT_SC_BV_BASE_HH__
5412853Sgabeblack@google.com#define __SYSTEMC_EXT_DT_BIT_SC_BV_BASE_HH__
5512853Sgabeblack@google.com
5612853Sgabeblack@google.com#include "../int/sc_length_param.hh"
5713325Sgabeblack@google.com#include "messages.hh"
5812853Sgabeblack@google.com#include "sc_bit_proxies.hh"
5912853Sgabeblack@google.com#include "sc_proxy.hh"
6012853Sgabeblack@google.com
6112853Sgabeblack@google.comnamespace sc_dt
6212853Sgabeblack@google.com{
6312853Sgabeblack@google.com
6412853Sgabeblack@google.com// classes defined in this module
6512853Sgabeblack@google.comclass sc_bv_base;
6612853Sgabeblack@google.com
6712853Sgabeblack@google.com
6812853Sgabeblack@google.com// ----------------------------------------------------------------------------
6912853Sgabeblack@google.com//  CLASS : sc_bv_base
7012853Sgabeblack@google.com//
7112853Sgabeblack@google.com//  Arbitrary size bit vector base class.
7212853Sgabeblack@google.com// ----------------------------------------------------------------------------
7312853Sgabeblack@google.com
7412853Sgabeblack@google.comclass sc_bv_base : public sc_proxy<sc_bv_base>
7512853Sgabeblack@google.com{
7612853Sgabeblack@google.com    friend class sc_lv_base;
7712853Sgabeblack@google.com
7812853Sgabeblack@google.com    void init(int length_, bool init_value=false);
7912853Sgabeblack@google.com    void assign_from_string(const std::string &);
8012853Sgabeblack@google.com
8112853Sgabeblack@google.com  public:
8212853Sgabeblack@google.com    // typedefs
8312853Sgabeblack@google.com    typedef sc_proxy<sc_bv_base> base_type;
8412853Sgabeblack@google.com    typedef base_type::value_type value_type;
8512853Sgabeblack@google.com
8612853Sgabeblack@google.com    // constructors
8712853Sgabeblack@google.com    explicit sc_bv_base(int length_=sc_length_param().len()) :
8812853Sgabeblack@google.com        m_len(0), m_size(0), m_data(0)
8912853Sgabeblack@google.com    {
9012853Sgabeblack@google.com        init(length_);
9112853Sgabeblack@google.com    }
9212853Sgabeblack@google.com
9312853Sgabeblack@google.com    explicit sc_bv_base(bool a, int length_=sc_length_param().len()) :
9412853Sgabeblack@google.com        m_len(0), m_size(0), m_data(0)
9512853Sgabeblack@google.com    {
9612853Sgabeblack@google.com        init(length_, a);
9712853Sgabeblack@google.com    }
9812853Sgabeblack@google.com
9912853Sgabeblack@google.com    sc_bv_base(const char *a);
10012853Sgabeblack@google.com    sc_bv_base(const char *a, int length_);
10112853Sgabeblack@google.com
10212853Sgabeblack@google.com    template <class X>
10312853Sgabeblack@google.com    sc_bv_base(const sc_proxy<X> &a) : m_len(0), m_size(0), m_data(0)
10412853Sgabeblack@google.com    {
10512853Sgabeblack@google.com        init(a.back_cast().length());
10612853Sgabeblack@google.com        base_type::assign_(a);
10712853Sgabeblack@google.com    }
10812853Sgabeblack@google.com
10912853Sgabeblack@google.com    sc_bv_base(const sc_bv_base &a);
11012853Sgabeblack@google.com
11112853Sgabeblack@google.com    // destructor
11212853Sgabeblack@google.com    virtual ~sc_bv_base() { delete [] m_data; }
11312853Sgabeblack@google.com
11412853Sgabeblack@google.com    // assignment operators
11512853Sgabeblack@google.com    template <class X>
11612853Sgabeblack@google.com    sc_bv_base &
11712853Sgabeblack@google.com    operator = (const sc_proxy<X> &a)
11812853Sgabeblack@google.com    {
11912853Sgabeblack@google.com        assign_p_(*this, a);
12012853Sgabeblack@google.com        return *this;
12112853Sgabeblack@google.com    }
12212853Sgabeblack@google.com
12312853Sgabeblack@google.com    sc_bv_base &
12412853Sgabeblack@google.com    operator = (const sc_bv_base &a)
12512853Sgabeblack@google.com    {
12612853Sgabeblack@google.com        assign_p_(*this, a);
12712853Sgabeblack@google.com        return *this;
12812853Sgabeblack@google.com    }
12912853Sgabeblack@google.com
13012853Sgabeblack@google.com    sc_bv_base &operator = (const char *a);
13112853Sgabeblack@google.com
13212853Sgabeblack@google.com    sc_bv_base &
13312853Sgabeblack@google.com    operator = (const bool *a)
13412853Sgabeblack@google.com    {
13512853Sgabeblack@google.com        base_type::assign_(a);
13612853Sgabeblack@google.com        return *this;
13712853Sgabeblack@google.com    }
13812853Sgabeblack@google.com
13912853Sgabeblack@google.com    sc_bv_base &
14012853Sgabeblack@google.com    operator = (const sc_logic *a)
14112853Sgabeblack@google.com    {
14212853Sgabeblack@google.com        base_type::assign_(a);
14312853Sgabeblack@google.com        return *this;
14412853Sgabeblack@google.com    }
14512853Sgabeblack@google.com
14612853Sgabeblack@google.com    sc_bv_base &
14712853Sgabeblack@google.com    operator = (const sc_unsigned &a)
14812853Sgabeblack@google.com    {
14912853Sgabeblack@google.com        base_type::assign_(a);
15012853Sgabeblack@google.com        return *this;
15112853Sgabeblack@google.com    }
15212853Sgabeblack@google.com
15312853Sgabeblack@google.com    sc_bv_base &
15412853Sgabeblack@google.com    operator = (const sc_signed &a)
15512853Sgabeblack@google.com    {
15612853Sgabeblack@google.com        base_type::assign_(a);
15712853Sgabeblack@google.com        return *this;
15812853Sgabeblack@google.com    }
15912853Sgabeblack@google.com
16012853Sgabeblack@google.com    sc_bv_base &
16112853Sgabeblack@google.com    operator = (const sc_uint_base &a)
16212853Sgabeblack@google.com    {
16312853Sgabeblack@google.com        base_type::assign_(a);
16412853Sgabeblack@google.com        return *this;
16512853Sgabeblack@google.com    }
16612853Sgabeblack@google.com
16712853Sgabeblack@google.com    sc_bv_base &
16812853Sgabeblack@google.com    operator = (const sc_int_base &a)
16912853Sgabeblack@google.com    {
17012853Sgabeblack@google.com        base_type::assign_(a);
17112853Sgabeblack@google.com        return *this;
17212853Sgabeblack@google.com    }
17312853Sgabeblack@google.com
17412853Sgabeblack@google.com    sc_bv_base &
17512853Sgabeblack@google.com    operator = (unsigned long a)
17612853Sgabeblack@google.com    {
17712853Sgabeblack@google.com        base_type::assign_(a);
17812853Sgabeblack@google.com        return *this;
17912853Sgabeblack@google.com    }
18012853Sgabeblack@google.com
18112853Sgabeblack@google.com    sc_bv_base &
18212853Sgabeblack@google.com    operator = (long a)
18312853Sgabeblack@google.com    {
18412853Sgabeblack@google.com        base_type::assign_(a);
18512853Sgabeblack@google.com        return *this;
18612853Sgabeblack@google.com    }
18712853Sgabeblack@google.com
18812853Sgabeblack@google.com    sc_bv_base &
18912853Sgabeblack@google.com    operator = (unsigned int a)
19012853Sgabeblack@google.com    {
19112853Sgabeblack@google.com        base_type::assign_(a);
19212853Sgabeblack@google.com        return *this;
19312853Sgabeblack@google.com    }
19412853Sgabeblack@google.com
19512853Sgabeblack@google.com    sc_bv_base &
19612853Sgabeblack@google.com    operator = (int a)
19712853Sgabeblack@google.com    {
19812853Sgabeblack@google.com        base_type::assign_(a);
19912853Sgabeblack@google.com        return *this;
20012853Sgabeblack@google.com    }
20112853Sgabeblack@google.com
20212853Sgabeblack@google.com    sc_bv_base &
20312853Sgabeblack@google.com    operator = (uint64 a)
20412853Sgabeblack@google.com    {
20512853Sgabeblack@google.com        base_type::assign_(a);
20612853Sgabeblack@google.com        return *this;
20712853Sgabeblack@google.com    }
20812853Sgabeblack@google.com
20912853Sgabeblack@google.com    sc_bv_base &
21012853Sgabeblack@google.com    operator = (int64 a)
21112853Sgabeblack@google.com    {
21212853Sgabeblack@google.com        base_type::assign_(a);
21312853Sgabeblack@google.com        return *this;
21412853Sgabeblack@google.com    }
21512853Sgabeblack@google.com
21612853Sgabeblack@google.com    // common methods
21712853Sgabeblack@google.com    int length() const { return m_len; }
21812853Sgabeblack@google.com    int size() const { return m_size; }
21912853Sgabeblack@google.com
22012853Sgabeblack@google.com    value_type get_bit(int i) const;
22112853Sgabeblack@google.com    void set_bit(int i, value_type value);
22212853Sgabeblack@google.com
22312853Sgabeblack@google.com    sc_digit get_word(int i) const { return m_data[i]; }
22412853Sgabeblack@google.com
22512853Sgabeblack@google.com    void set_word(int i, sc_digit w) { m_data[i] = w; }
22612853Sgabeblack@google.com
22712853Sgabeblack@google.com    sc_digit get_cword(int /*i*/) const { return SC_DIGIT_ZERO; }
22812853Sgabeblack@google.com
22912853Sgabeblack@google.com    void set_cword(int i, sc_digit w);
23012853Sgabeblack@google.com
23112853Sgabeblack@google.com    void clean_tail();
23212853Sgabeblack@google.com
23312853Sgabeblack@google.com    // other methods
23412853Sgabeblack@google.com    bool is_01() const { return true; }
23512853Sgabeblack@google.com
23612853Sgabeblack@google.com  protected:
23712853Sgabeblack@google.com    int m_len; // length in bits
23812853Sgabeblack@google.com    int m_size; // size of data array
23912853Sgabeblack@google.com    sc_digit *m_data; // data array
24012853Sgabeblack@google.com};
24112853Sgabeblack@google.com
24212853Sgabeblack@google.com
24312853Sgabeblack@google.com// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
24412853Sgabeblack@google.com
24512853Sgabeblack@google.com// common methods
24612853Sgabeblack@google.cominline sc_bv_base::value_type
24712853Sgabeblack@google.comsc_bv_base::get_bit(int i) const
24812853Sgabeblack@google.com{
24912853Sgabeblack@google.com    int wi = i / SC_DIGIT_SIZE;
25012853Sgabeblack@google.com    int bi = i % SC_DIGIT_SIZE;
25112853Sgabeblack@google.com    return value_type((m_data[wi] >> bi) & SC_DIGIT_ONE);
25212853Sgabeblack@google.com}
25312853Sgabeblack@google.com
25412853Sgabeblack@google.cominline void
25512853Sgabeblack@google.comsc_bv_base::set_bit(int i, value_type value)
25612853Sgabeblack@google.com{
25712853Sgabeblack@google.com    int wi = i / SC_DIGIT_SIZE;
25812853Sgabeblack@google.com    int bi = i % SC_DIGIT_SIZE;
25912853Sgabeblack@google.com    sc_digit mask = SC_DIGIT_ONE << bi;
26012853Sgabeblack@google.com    m_data[wi] |= mask; // set bit to 1
26112853Sgabeblack@google.com    m_data[wi] &= value << bi | ~mask;
26212853Sgabeblack@google.com}
26312853Sgabeblack@google.com
26412853Sgabeblack@google.cominline void
26512853Sgabeblack@google.comsc_bv_base::set_cword(int /*i*/, sc_digit w)
26612853Sgabeblack@google.com{
26712853Sgabeblack@google.com    if (w) {
26813325Sgabeblack@google.com        SC_REPORT_WARNING(sc_core::SC_ID_SC_BV_CANNOT_CONTAIN_X_AND_Z_, 0);
26912853Sgabeblack@google.com    }
27012853Sgabeblack@google.com}
27112853Sgabeblack@google.com
27212853Sgabeblack@google.cominline void
27312853Sgabeblack@google.comsc_bv_base::clean_tail()
27412853Sgabeblack@google.com{
27512853Sgabeblack@google.com    int wi = m_size - 1;
27612853Sgabeblack@google.com    int bi = m_len % SC_DIGIT_SIZE;
27712853Sgabeblack@google.com    if (bi != 0)
27812853Sgabeblack@google.com        m_data[wi] &= ~SC_DIGIT_ZERO >> (SC_DIGIT_SIZE - bi);
27912853Sgabeblack@google.com}
28012853Sgabeblack@google.com
28112853Sgabeblack@google.com} // namespace sc_dt
28212853Sgabeblack@google.com
28312853Sgabeblack@google.com#endif // __SYSTEMC_EXT_DT_BIT_SC_BV_BASE_HH__
284