112854Sgabeblack@google.com/*****************************************************************************
212854Sgabeblack@google.com
312854Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412854Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
512854Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
612854Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
712854Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
812854Sgabeblack@google.com  License.  You may obtain a copy of the License at
912854Sgabeblack@google.com
1012854Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1112854Sgabeblack@google.com
1212854Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1312854Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1412854Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512854Sgabeblack@google.com  implied.  See the License for the specific language governing
1612854Sgabeblack@google.com  permissions and limitations under the License.
1712854Sgabeblack@google.com
1812854Sgabeblack@google.com *****************************************************************************/
1912854Sgabeblack@google.com
2012854Sgabeblack@google.com/*****************************************************************************
2112854Sgabeblack@google.com
2212854Sgabeblack@google.com  sc_lv_base.cpp -- Arbitrary size logic vector class.
2312854Sgabeblack@google.com
2412854Sgabeblack@google.com  Original Author: Gene Bushuyev, Synopsys, Inc.
2512854Sgabeblack@google.com
2612854Sgabeblack@google.com *****************************************************************************/
2712854Sgabeblack@google.com
2812854Sgabeblack@google.com/*****************************************************************************
2912854Sgabeblack@google.com
3012854Sgabeblack@google.com  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
3112854Sgabeblack@google.com  changes you are making here.
3212854Sgabeblack@google.com
3312854Sgabeblack@google.com      Name, Affiliation, Date:
3412854Sgabeblack@google.com  Description of Modification:
3512854Sgabeblack@google.com
3612854Sgabeblack@google.com *****************************************************************************/
3712854Sgabeblack@google.com
3812854Sgabeblack@google.com
3912854Sgabeblack@google.com// $Log: sc_lv_base.cpp,v $
4012854Sgabeblack@google.com// Revision 1.2  2011/08/24 22:05:40  acg
4112854Sgabeblack@google.com//  Torsten Maehne: initialization changes to remove warnings.
4212854Sgabeblack@google.com//
4312854Sgabeblack@google.com// Revision 1.1.1.1  2006/12/15 20:20:04  acg
4412854Sgabeblack@google.com// SystemC 2.3
4512854Sgabeblack@google.com//
4612854Sgabeblack@google.com// Revision 1.3  2006/01/13 18:53:53  acg
4712854Sgabeblack@google.com// Andy Goodrich: added $Log command so that CVS comments are reproduced in
4812854Sgabeblack@google.com// the source.
4912854Sgabeblack@google.com//
5012854Sgabeblack@google.com
5112854Sgabeblack@google.com#include <sstream>
5212854Sgabeblack@google.com
5313325Sgabeblack@google.com#include "systemc/ext/dt/bit/messages.hh"
5412854Sgabeblack@google.com#include "systemc/ext/dt/bit/sc_lv_base.hh"
5513322Sgabeblack@google.com#include "systemc/ext/utils/messages.hh"
5612854Sgabeblack@google.com
5712854Sgabeblack@google.comnamespace sc_dt
5812854Sgabeblack@google.com{
5912854Sgabeblack@google.com
6012854Sgabeblack@google.com// explicit template instantiations
6112854Sgabeblack@google.comtemplate class sc_proxy<sc_lv_base>;
6212854Sgabeblack@google.comtemplate class sc_proxy<sc_bv_base>;
6312854Sgabeblack@google.com
6412854Sgabeblack@google.comvoid
6512854Sgabeblack@google.comsc_proxy_out_of_bounds(const char *msg, int64 val)
6612854Sgabeblack@google.com{
6712854Sgabeblack@google.com    std::stringstream ss;
6812854Sgabeblack@google.com    if (msg != NULL)
6912854Sgabeblack@google.com        ss << msg;
7012854Sgabeblack@google.com    if (val != 0)
7112854Sgabeblack@google.com        ss << val;
7213322Sgabeblack@google.com    SC_REPORT_ERROR(sc_core::SC_ID_OUT_OF_BOUNDS_, ss.str().c_str());
7312854Sgabeblack@google.com}
7412854Sgabeblack@google.com
7512854Sgabeblack@google.com// ----------------------------------------------------------------------------
7612854Sgabeblack@google.com//  CLASS : sc_lv_base
7712854Sgabeblack@google.com//
7812854Sgabeblack@google.com//  Arbitrary size logic vector base class.
7912854Sgabeblack@google.com// ----------------------------------------------------------------------------
8012854Sgabeblack@google.com
8112854Sgabeblack@google.comstatic const sc_digit data_array[] = {
8212854Sgabeblack@google.com    SC_DIGIT_ZERO, ~SC_DIGIT_ZERO, SC_DIGIT_ZERO, ~SC_DIGIT_ZERO
8312854Sgabeblack@google.com};
8412854Sgabeblack@google.com
8512854Sgabeblack@google.comstatic const sc_digit ctrl_array[] = {
8612854Sgabeblack@google.com    SC_DIGIT_ZERO, SC_DIGIT_ZERO, ~SC_DIGIT_ZERO, ~SC_DIGIT_ZERO
8712854Sgabeblack@google.com};
8812854Sgabeblack@google.com
8912854Sgabeblack@google.comvoid
9012854Sgabeblack@google.comsc_lv_base::init(int length_, const sc_logic& init_value)
9112854Sgabeblack@google.com{
9212854Sgabeblack@google.com    // check the length
9312854Sgabeblack@google.com    if (length_ <= 0) {
9413325Sgabeblack@google.com        SC_REPORT_ERROR(sc_core::SC_ID_ZERO_LENGTH_, 0);
9512854Sgabeblack@google.com        sc_core::sc_abort(); // can't recover from here
9612854Sgabeblack@google.com    }
9712854Sgabeblack@google.com    // allocate memory for the data and control words
9812854Sgabeblack@google.com    m_len = length_;
9912854Sgabeblack@google.com    m_size = (m_len - 1) / SC_DIGIT_SIZE + 1;
10012854Sgabeblack@google.com    m_data = new sc_digit[m_size * 2];
10112854Sgabeblack@google.com    m_ctrl = m_data + m_size;
10212854Sgabeblack@google.com    // initialize the bits to 'init_value'
10312854Sgabeblack@google.com    sc_digit dw = data_array[init_value.value()];
10412854Sgabeblack@google.com    sc_digit cw = ctrl_array[init_value.value()];
10512854Sgabeblack@google.com    int sz = m_size;
10612854Sgabeblack@google.com    for (int i = 0; i < sz; ++i) {
10712854Sgabeblack@google.com        m_data[i] = dw;
10812854Sgabeblack@google.com        m_ctrl[i] = cw;
10912854Sgabeblack@google.com    }
11012854Sgabeblack@google.com    clean_tail();
11112854Sgabeblack@google.com}
11212854Sgabeblack@google.com
11312854Sgabeblack@google.comvoid
11412854Sgabeblack@google.comsc_lv_base::assign_from_string(const std::string &s)
11512854Sgabeblack@google.com{
11612854Sgabeblack@google.com    // s must have been converted to bin
11712854Sgabeblack@google.com    int len = m_len;
11812854Sgabeblack@google.com    int s_len = s.length() - 1;
11912854Sgabeblack@google.com    int min_len = sc_min(len, s_len);
12012854Sgabeblack@google.com    int i = 0;
12112854Sgabeblack@google.com    for (; i < min_len; ++i) {
12212854Sgabeblack@google.com        char c = s[s_len - i - 1];
12312854Sgabeblack@google.com        set_bit(i, sc_logic::char_to_logic[(int)c]);
12412854Sgabeblack@google.com    }
12512854Sgabeblack@google.com    // if formatted, fill the rest with sign(s), otherwise fill with zeros
12612854Sgabeblack@google.com    sc_logic_value_t fill = (s[s_len] == 'F' ? sc_logic_value_t(s[0] - '0')
12712854Sgabeblack@google.com                                             : sc_logic_value_t(0));
12812854Sgabeblack@google.com    for (; i < len; ++i) {
12912854Sgabeblack@google.com        set_bit(i, fill);
13012854Sgabeblack@google.com    }
13112854Sgabeblack@google.com}
13212854Sgabeblack@google.com
13312854Sgabeblack@google.com// constructors
13412854Sgabeblack@google.comsc_lv_base::sc_lv_base(const char *a) :
13512854Sgabeblack@google.com    m_len(0), m_size(0), m_data(0), m_ctrl(0)
13612854Sgabeblack@google.com{
13712854Sgabeblack@google.com    std::string s = convert_to_bin(a);
13812854Sgabeblack@google.com    init(s.length() - 1);
13912854Sgabeblack@google.com    assign_from_string(s);
14012854Sgabeblack@google.com}
14112854Sgabeblack@google.com
14212854Sgabeblack@google.comsc_lv_base::sc_lv_base(const char *a, int length_) :
14312854Sgabeblack@google.com    m_len(0), m_size(0), m_data(0), m_ctrl(0)
14412854Sgabeblack@google.com{
14512854Sgabeblack@google.com    init(length_);
14612854Sgabeblack@google.com    assign_from_string(convert_to_bin(a));
14712854Sgabeblack@google.com}
14812854Sgabeblack@google.com
14912854Sgabeblack@google.comsc_lv_base::sc_lv_base(const sc_lv_base &a) :
15012854Sgabeblack@google.com    sc_proxy<sc_lv_base>(), m_len(a.m_len), m_size(a.m_size),
15112854Sgabeblack@google.com      m_data(new sc_digit[m_size * 2]), m_ctrl(m_data + m_size)
15212854Sgabeblack@google.com{
15312854Sgabeblack@google.com    // copy the bits
15412854Sgabeblack@google.com    int sz = m_size;
15512854Sgabeblack@google.com    for (int i = 0; i < sz; ++i) {
15612854Sgabeblack@google.com        m_data[i] = a.m_data[i];
15712854Sgabeblack@google.com        m_ctrl[i] = a.m_ctrl[i];
15812854Sgabeblack@google.com    }
15912854Sgabeblack@google.com}
16012854Sgabeblack@google.com
16112854Sgabeblack@google.com// assignment operators
16212854Sgabeblack@google.comsc_lv_base &
16312854Sgabeblack@google.comsc_lv_base::operator = (const char *a)
16412854Sgabeblack@google.com{
16512854Sgabeblack@google.com    assign_from_string(convert_to_bin(a));
16612854Sgabeblack@google.com    return *this;
16712854Sgabeblack@google.com}
16812854Sgabeblack@google.com
16912854Sgabeblack@google.com// returns true if logic vector contains only 0's and 1's
17012854Sgabeblack@google.combool
17112854Sgabeblack@google.comsc_lv_base::is_01() const
17212854Sgabeblack@google.com{
17312854Sgabeblack@google.com    int sz = m_size;
17412854Sgabeblack@google.com    for (int i = 0; i < sz; ++i) {
17512854Sgabeblack@google.com        if (m_ctrl[i] != 0) {
17612854Sgabeblack@google.com            return false;
17712854Sgabeblack@google.com        }
17812854Sgabeblack@google.com    }
17912854Sgabeblack@google.com    return true;
18012854Sgabeblack@google.com}
18112854Sgabeblack@google.com
18212854Sgabeblack@google.com} // namespace sc_dt
183