sc_bv_base.hh revision 12853:e23d6f09069a
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.h -- 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// $Log: sc_bv_base.h,v $
39// Revision 1.3  2011/08/26 22:32:00  acg
40//  Torsten Maehne: added parentheses to make opearator ordering more obvious.
41//
42// Revision 1.2  2011/08/15 16:43:24  acg
43//  Torsten Maehne: changes to remove unused argument warnings.
44//
45// Revision 1.1.1.1  2006/12/15 20:20:04  acg
46// SystemC 2.3
47//
48// Revision 1.3  2006/01/13 18:53:53  acg
49// Andy Goodrich: added $Log command so that CVS comments are reproduced in
50// the source.
51//
52
53#ifndef __SYSTEMC_EXT_DT_BIT_SC_BV_BASE_HH__
54#define __SYSTEMC_EXT_DT_BIT_SC_BV_BASE_HH__
55
56#include "../int/sc_length_param.hh"
57#include "sc_bit_proxies.hh"
58#include "sc_proxy.hh"
59
60namespace sc_dt
61{
62
63// classes defined in this module
64class sc_bv_base;
65
66
67// ----------------------------------------------------------------------------
68//  CLASS : sc_bv_base
69//
70//  Arbitrary size bit vector base class.
71// ----------------------------------------------------------------------------
72
73class sc_bv_base : public sc_proxy<sc_bv_base>
74{
75    friend class sc_lv_base;
76
77    void init(int length_, bool init_value=false);
78    void assign_from_string(const std::string &);
79
80  public:
81    // typedefs
82    typedef sc_proxy<sc_bv_base> base_type;
83    typedef base_type::value_type value_type;
84
85    // constructors
86    explicit sc_bv_base(int length_=sc_length_param().len()) :
87        m_len(0), m_size(0), m_data(0)
88    {
89        init(length_);
90    }
91
92    explicit sc_bv_base(bool a, int length_=sc_length_param().len()) :
93        m_len(0), m_size(0), m_data(0)
94    {
95        init(length_, a);
96    }
97
98    sc_bv_base(const char *a);
99    sc_bv_base(const char *a, int length_);
100
101    template <class X>
102    sc_bv_base(const sc_proxy<X> &a) : m_len(0), m_size(0), m_data(0)
103    {
104        init(a.back_cast().length());
105        base_type::assign_(a);
106    }
107
108    sc_bv_base(const sc_bv_base &a);
109
110    // destructor
111    virtual ~sc_bv_base() { delete [] m_data; }
112
113    // assignment operators
114    template <class X>
115    sc_bv_base &
116    operator = (const sc_proxy<X> &a)
117    {
118        assign_p_(*this, a);
119        return *this;
120    }
121
122    sc_bv_base &
123    operator = (const sc_bv_base &a)
124    {
125        assign_p_(*this, a);
126        return *this;
127    }
128
129    sc_bv_base &operator = (const char *a);
130
131    sc_bv_base &
132    operator = (const bool *a)
133    {
134        base_type::assign_(a);
135        return *this;
136    }
137
138    sc_bv_base &
139    operator = (const sc_logic *a)
140    {
141        base_type::assign_(a);
142        return *this;
143    }
144
145    sc_bv_base &
146    operator = (const sc_unsigned &a)
147    {
148        base_type::assign_(a);
149        return *this;
150    }
151
152    sc_bv_base &
153    operator = (const sc_signed &a)
154    {
155        base_type::assign_(a);
156        return *this;
157    }
158
159    sc_bv_base &
160    operator = (const sc_uint_base &a)
161    {
162        base_type::assign_(a);
163        return *this;
164    }
165
166    sc_bv_base &
167    operator = (const sc_int_base &a)
168    {
169        base_type::assign_(a);
170        return *this;
171    }
172
173    sc_bv_base &
174    operator = (unsigned long a)
175    {
176        base_type::assign_(a);
177        return *this;
178    }
179
180    sc_bv_base &
181    operator = (long a)
182    {
183        base_type::assign_(a);
184        return *this;
185    }
186
187    sc_bv_base &
188    operator = (unsigned int a)
189    {
190        base_type::assign_(a);
191        return *this;
192    }
193
194    sc_bv_base &
195    operator = (int a)
196    {
197        base_type::assign_(a);
198        return *this;
199    }
200
201    sc_bv_base &
202    operator = (uint64 a)
203    {
204        base_type::assign_(a);
205        return *this;
206    }
207
208    sc_bv_base &
209    operator = (int64 a)
210    {
211        base_type::assign_(a);
212        return *this;
213    }
214
215    // common methods
216    int length() const { return m_len; }
217    int size() const { return m_size; }
218
219    value_type get_bit(int i) const;
220    void set_bit(int i, value_type value);
221
222    sc_digit get_word(int i) const { return m_data[i]; }
223
224    void set_word(int i, sc_digit w) { m_data[i] = w; }
225
226    sc_digit get_cword(int /*i*/) const { return SC_DIGIT_ZERO; }
227
228    void set_cword(int i, sc_digit w);
229
230    void clean_tail();
231
232    // other methods
233    bool is_01() const { return true; }
234
235  protected:
236    int m_len; // length in bits
237    int m_size; // size of data array
238    sc_digit *m_data; // data array
239};
240
241
242// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
243
244// common methods
245inline sc_bv_base::value_type
246sc_bv_base::get_bit(int i) const
247{
248    int wi = i / SC_DIGIT_SIZE;
249    int bi = i % SC_DIGIT_SIZE;
250    return value_type((m_data[wi] >> bi) & SC_DIGIT_ONE);
251}
252
253inline void
254sc_bv_base::set_bit(int i, value_type value)
255{
256    int wi = i / SC_DIGIT_SIZE;
257    int bi = i % SC_DIGIT_SIZE;
258    sc_digit mask = SC_DIGIT_ONE << bi;
259    m_data[wi] |= mask; // set bit to 1
260    m_data[wi] &= value << bi | ~mask;
261}
262
263inline void
264sc_bv_base::set_cword(int /*i*/, sc_digit w)
265{
266    if (w) {
267        SC_REPORT_WARNING("sc_bv cannot contain values X and Z", 0);
268    }
269}
270
271inline void
272sc_bv_base::clean_tail()
273{
274    int wi = m_size - 1;
275    int bi = m_len % SC_DIGIT_SIZE;
276    if (bi != 0)
277        m_data[wi] &= ~SC_DIGIT_ZERO >> (SC_DIGIT_SIZE - bi);
278}
279
280} // namespace sc_dt
281
282#endif // __SYSTEMC_EXT_DT_BIT_SC_BV_BASE_HH__
283