gem5  v22.1.0.0
sc_bv_base.cc
Go to the documentation of this file.
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 
63 
64 namespace sc_dt
65 {
66 
67 // ----------------------------------------------------------------------------
68 // CLASS : sc_bv_base
69 //
70 // Arbitrary size bit vector base class.
71 // ----------------------------------------------------------------------------
72 
73 void
74 sc_bv_base::init(int length_, bool init_value)
75 {
76  // check the length
77  if (length_ <= 0) {
79  sc_core::sc_abort(); // can't recover from here
80  }
81  // allocate memory for the data and control words
82  m_len = length_;
83  m_size = (m_len - 1) / SC_DIGIT_SIZE + 1;
84  m_data = new sc_digit[m_size];
85  // initialize the bits to 'init_value'
86  sc_digit dw = init_value ? ~SC_DIGIT_ZERO : SC_DIGIT_ZERO;
87  int sz = m_size;
88  for (int i = 0; i < sz; ++i) {
89  m_data[i] = dw;
90  }
91  clean_tail();
92 }
93 
94 void
95 sc_bv_base::assign_from_string(const std::string &s)
96 {
97  // s must have been converted to bin
98  int len = m_len;
99  int s_len = s.length() - 1;
100  int min_len = sc_min(len, s_len);
101  int i = 0;
102  for (; i < min_len; ++i) {
103  char c = s[s_len - i - 1];
104  if (c != '0' && c != '1') {
106  "string can contain only '0' and '1' characters");
107  // may continue, if suppressed
108  c = '0';
109  }
110  set_bit(i, sc_logic_value_t(c - '0'));
111  }
112  // if formatted, fill the rest with sign(s), otherwise fill with zeros
113  sc_logic_value_t fill = (s[s_len] == 'F' ? sc_logic_value_t(s[0] - '0')
114  : sc_logic_value_t(0));
115  for (; i < len; ++i) {
116  set_bit(i, fill);
117  }
118 }
119 
120 // constructors
121 sc_bv_base::sc_bv_base(const char *a) : m_len(0), m_size(0), m_data(0)
122 {
123  std::string s = convert_to_bin(a);
124  init(s.length() - 1);
126 }
127 
128 sc_bv_base::sc_bv_base(const char *a, int length_) :
129  m_len(0), m_size(0), m_data(0)
130 {
131  init(length_);
133 }
134 
136  sc_proxy<sc_bv_base>(), m_len(a.m_len), m_size(a.m_size),
137  m_data(new sc_digit[m_size])
138 {
139  // copy the bits
140  int sz = m_size;
141  for (int i = 0; i < sz; ++i) {
142  m_data[i] = a.m_data[i];
143  }
144 }
145 
146 // assignment operators
147 sc_bv_base &
149 {
151  return *this;
152 }
153 
154 
155 // ----------------------------------------------------------------------------
156 // convert formatted string to binary string
157 
158 const std::string
159 convert_to_bin(const char *s)
160 {
161  // Beware: logic character strings cannot start with '0x' or '0X',
162  // because this is seen as a hexadecimal encoding prefix!
163 
164  if (s == 0) {
166  "character string is zero");
167  return std::string();
168  }
169  if (*s == 0) {
171  "character string is empty");
172  return std::string();
173  }
174 
175  int n = strlen(s);
176  int i = 0;
177  if (s[0] == '-' || s[0] == '+') {
178  ++i;
179  }
180  if (n > (i + 2) && s[i] == '0') {
181  if (s[i + 1] == 'b' || s[i + 1] == 'B') {
182  if (s[i + 2] == '0' || s[i + 2] == '1') {
183  std::string str(&s[2]);
184  str += "F";
185  return str;
186  }
187  }
188  if (s[i + 1] == 'b' || s[i + 1] == 'B' ||
189  s[i + 1] == 'c' || s[i + 1] == 'C' ||
190  s[i + 1] == 'd' || s[i + 1] == 'D' ||
191  s[i + 1] == 'o' || s[i + 1] == 'O' ||
192  s[i + 1] == 'x' || s[i + 1] == 'X') {
193  try {
194  // worst case length = n * 4
195  sc_fix a(s, n * 4, n * 4, SC_TRN, SC_WRAP, 0, SC_ON);
196  std::string str = a.to_bin();
197  str += "F"; // mark the string as formatted
198  // get rid of prefix (0b) and redundant leading bits
199  const char *p = str.c_str() + 2;
200  while (p[1] && p[0] == p[1]) {
201  ++p;
202  }
203  return std::string(p);
204  } catch (const sc_core::sc_report &) {
205  std::stringstream msg;
206  msg << "character string '" << s << "' is not valid";
208  msg.str().c_str());
209  return std::string();
210  }
211  }
212  }
213 
214  // bin by default
215  std::string str(s);
216  str += "U"; // mark the string as unformatted
217  return str;
218 }
219 
220 // convert binary string to formatted string
221 const std::string
222 convert_to_fmt(const std::string &s, sc_numrep numrep, bool w_prefix)
223 {
224  int n = s.length();
225  std::string str("0bus");
226  // str += "0bus";
227  str += s;
228  sc_ufix a(str.c_str(), n, n, SC_TRN, SC_WRAP, 0, SC_ON);
229  return a.to_string(numrep, w_prefix);
230 }
231 
232 } // namespace sc_dt
sc_digit * m_data
Definition: sc_bv_base.hh:239
void init(int length_, bool init_value=false)
Definition: sc_bv_base.cc:74
void set_bit(int i, value_type value)
Definition: sc_bv_base.hh:255
sc_bv_base(int length_=sc_length_param().len())
Definition: sc_bv_base.hh:87
sc_bv_base & operator=(const sc_proxy< X > &a)
Definition: sc_bv_base.hh:117
void assign_from_string(const std::string &)
Definition: sc_bv_base.cc:95
uint16_t len
Definition: helpers.cc:62
Bitfield< 31 > n
Definition: misc_types.hh:462
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 8 > a
Definition: misc_types.hh:66
Bitfield< 1 > s
Definition: pagetable.hh:64
Bitfield< 2 > c
Definition: pagetable.hh:63
Bitfield< 54 > p
Definition: pagetable.hh:70
const char SC_ID_CANNOT_CONVERT_[]
Definition: messages.cc:37
void sc_abort()
Definition: sc_report.cc:178
const char SC_ID_ZERO_LENGTH_[]
Definition: messages.cc:40
Definition: sc_bit.cc:68
sc_logic_value_t
Definition: sc_logic.hh:85
@ SC_TRN
Definition: sc_fxdefs.hh:98
const int SC_DIGIT_SIZE
Definition: sc_proxy.hh:98
const sc_digit SC_DIGIT_ZERO
Definition: sc_proxy.hh:100
sc_numrep
Definition: sc_nbdefs.hh:82
const T sc_min(const T &a, const T &b)
Definition: functions.hh:59
@ SC_ON
Definition: sc_fxdefs.hh:146
unsigned int sc_digit
Definition: sc_nbdefs.hh:163
const std::string convert_to_fmt(const std::string &s, sc_numrep numrep, bool w_prefix)
Definition: sc_bv_base.cc:222
const std::string convert_to_bin(const char *s)
Definition: sc_bv_base.cc:159
@ SC_WRAP
Definition: sc_fxdefs.hh:122
#define SC_REPORT_ERROR(msg_type, msg)

Generated on Wed Dec 21 2022 10:22:40 for gem5 by doxygen 1.9.1