sc_bv_base.cc revision 12854:c95c35407325
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_bv_base.cpp -- Arbitrary size bit vector class.
23
24  Original Author: Gene Bushuyev, 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
39// $Log: sc_bv_base.cpp,v $
40// Revision 1.2  2011/08/24 22:05:40  acg
41//  Torsten Maehne: initialization changes to remove warnings.
42//
43// Revision 1.1.1.1  2006/12/15 20:20:04  acg
44// SystemC 2.3
45//
46// Revision 1.4  2006/04/11 23:12:26  acg
47//   Andy Goodrich: Fixed bug in parsing of extended string constants like
48//   0bus1110011.
49//
50// Revision 1.3  2006/01/13 18:53:53  acg
51// Andy Goodrich: added $Log command so that CVS comments are reproduced in
52// the source.
53//
54
55#include <cstring>
56#include <sstream>
57
58#include "systemc/ext/dt/bit/sc_bv_base.hh"
59#include "systemc/ext/dt/fx/sc_fix.hh"
60#include "systemc/ext/dt/fx/sc_ufix.hh"
61#include "systemc/ext/utils/sc_report.hh"
62
63namespace sc_dt
64{
65
66// ----------------------------------------------------------------------------
67//  CLASS : sc_bv_base
68//
69//  Arbitrary size bit vector base class.
70// ----------------------------------------------------------------------------
71
72void
73sc_bv_base::init(int length_, bool init_value)
74{
75    // check the length
76    if (length_ <= 0) {
77        SC_REPORT_ERROR("zero length", 0);
78        sc_core::sc_abort(); // can't recover from here
79    }
80    // allocate memory for the data and control words
81    m_len = length_;
82    m_size = (m_len - 1) / SC_DIGIT_SIZE + 1;
83    m_data = new sc_digit[m_size];
84    // initialize the bits to 'init_value'
85    sc_digit dw = init_value ? ~SC_DIGIT_ZERO : SC_DIGIT_ZERO;
86    int sz = m_size;
87    for (int i = 0; i < sz; ++i) {
88        m_data[i] = dw;
89    }
90    clean_tail();
91}
92
93void
94sc_bv_base::assign_from_string(const std::string &s)
95{
96    // s must have been converted to bin
97    int len = m_len;
98    int s_len = s.length() - 1;
99    int min_len = sc_min(len, s_len);
100    int i = 0;
101    for (; i < min_len; ++i) {
102        char c = s[s_len - i - 1];
103        if (c != '0' && c != '1') {
104            SC_REPORT_ERROR("cannot perform conversion",
105                "string can contain only '0' and '1' characters");
106            // may continue, if suppressed
107            c = '0';
108        }
109        set_bit(i, sc_logic_value_t(c - '0'));
110    }
111    // if formatted, fill the rest with sign(s), otherwise fill with zeros
112    sc_logic_value_t fill = (s[s_len] == 'F' ? sc_logic_value_t(s[0] - '0')
113                                             : sc_logic_value_t(0));
114    for (; i < len; ++i) {
115        set_bit(i, fill);
116    }
117}
118
119// constructors
120sc_bv_base::sc_bv_base(const char *a) : m_len(0), m_size(0), m_data(0)
121{
122    std::string s = convert_to_bin(a);
123    init(s.length() - 1);
124    assign_from_string(s);
125}
126
127sc_bv_base::sc_bv_base(const char *a, int length_) :
128    m_len(0), m_size(0), m_data(0)
129{
130    init(length_);
131    assign_from_string(convert_to_bin(a));
132}
133
134sc_bv_base::sc_bv_base(const sc_bv_base &a) :
135    sc_proxy<sc_bv_base>(), m_len(a.m_len), m_size(a.m_size),
136    m_data(new sc_digit[m_size])
137{
138    // copy the bits
139    int sz = m_size;
140    for (int i = 0; i < sz; ++i) {
141        m_data[i] = a.m_data[i];
142    }
143}
144
145// assignment operators
146sc_bv_base &
147sc_bv_base::operator = (const char *a)
148{
149    assign_from_string(convert_to_bin(a));
150    return *this;
151}
152
153
154// ----------------------------------------------------------------------------
155// convert formatted string to binary string
156
157const std::string
158convert_to_bin(const char *s)
159{
160    // Beware: logic character strings cannot start with '0x' or '0X',
161    //         because this is seen as a hexadecimal encoding prefix!
162
163    if (s == 0) {
164        SC_REPORT_ERROR("cannot perform conversion",
165            "character string is zero");
166        return std::string();
167    }
168    if (*s == 0) {
169        SC_REPORT_ERROR("cannot perform conversion",
170            "character string is empty");
171        return std::string();
172    }
173
174    int n = strlen(s);
175    int i = 0;
176    if (s[0] == '-' || s[0] == '+') {
177        ++i;
178    }
179    if (n > (i + 2) && s[i] == '0') {
180        if (s[i + 1] == 'b' || s[i + 1] == 'B') {
181            if (s[i + 2] == '0' || s[i + 2] == '1') {
182                std::string str(&s[2]);
183                str += "F";
184                return str;
185            }
186        }
187        if (s[i + 1] == 'b' || s[i + 1] == 'B' ||
188            s[i + 1] == 'c' || s[i + 1] == 'C' ||
189            s[i + 1] == 'd' || s[i + 1] == 'D' ||
190            s[i + 1] == 'o' || s[i + 1] == 'O' ||
191            s[i + 1] == 'x' || s[i + 1] == 'X') {
192            try {
193                // worst case length = n * 4
194                sc_fix a(s, n * 4, n * 4, SC_TRN, SC_WRAP, 0, SC_ON);
195                std::string str = a.to_bin();
196                str += "F"; // mark the string as formatted
197                // get rid of prefix (0b) and redundant leading bits
198                const char *p = str.c_str() + 2;
199                while (p[1] && p[0] == p[1]) {
200                    ++p;
201                }
202                return std::string(p);
203            } catch (const sc_core::sc_report &) {
204                std::stringstream msg;
205                msg << "character string '" << s << "' is not valid";
206                SC_REPORT_ERROR("cannot perform conversion",
207                        msg.str().c_str());
208                return std::string();
209            }
210        }
211    }
212
213    // bin by default
214    std::string str(s);
215    str += "U"; // mark the string as unformatted
216    return str;
217}
218
219// convert binary string to formatted string
220const std::string
221convert_to_fmt(const std::string &s, sc_numrep numrep, bool w_prefix)
222{
223    int n = s.length();
224    std::string str("0bus");
225    // str += "0bus";
226    str += s;
227    sc_ufix a(str.c_str(), n, n, SC_TRN, SC_WRAP, 0, SC_ON);
228    return a.to_string(numrep, w_prefix);
229}
230
231} // namespace sc_dt
232