sc_bv_base.h revision 12027:1eb7dc7aa10b
11736SN/A/*****************************************************************************
27778Sgblack@eecs.umich.edu
31736SN/A  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
41736SN/A  more contributor license agreements.  See the NOTICE file distributed
51736SN/A  with this work for additional information regarding copyright ownership.
61736SN/A  Accellera licenses this file to you under the Apache License, Version 2.0
71736SN/A  (the "License"); you may not use this file except in compliance with the
81736SN/A  License.  You may obtain a copy of the License at
91736SN/A
101736SN/A    http://www.apache.org/licenses/LICENSE-2.0
111736SN/A
121736SN/A  Unless required by applicable law or agreed to in writing, software
131736SN/A  distributed under the License is distributed on an "AS IS" BASIS,
141736SN/A  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
151736SN/A  implied.  See the License for the specific language governing
161736SN/A  permissions and limitations under the License.
171736SN/A
181736SN/A *****************************************************************************/
191736SN/A
201736SN/A/*****************************************************************************
211736SN/A
221736SN/A  sc_bv_base.h -- Arbitrary size bit vector class.
231736SN/A
241736SN/A  Original Author: Gene Bushuyev, Synopsys, Inc.
251736SN/A
261736SN/A *****************************************************************************/
272665SN/A
282665SN/A/*****************************************************************************
297778Sgblack@eecs.umich.edu
301736SN/A  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
311519SN/A  changes you are making here.
3212247Sgabeblack@google.com
3312247Sgabeblack@google.com      Name, Affiliation, Date:
3412247Sgabeblack@google.com  Description of Modification:
3512247Sgabeblack@google.com
3612247Sgabeblack@google.com *****************************************************************************/
3712247Sgabeblack@google.com
3812247Sgabeblack@google.com// $Log: sc_bv_base.h,v $
3912247Sgabeblack@google.com// Revision 1.3  2011/08/26 22:32:00  acg
4012247Sgabeblack@google.com//  Torsten Maehne: added parentheses to make opearator ordering more obvious.
4112247Sgabeblack@google.com//
4212247Sgabeblack@google.com// Revision 1.2  2011/08/15 16:43:24  acg
4312247Sgabeblack@google.com//  Torsten Maehne: changes to remove unused argument warnings.
441519SN/A//
451519SN/A// Revision 1.1.1.1  2006/12/15 20:20:04  acg
461519SN/A// SystemC 2.3
471519SN/A//
481519SN/A// Revision 1.3  2006/01/13 18:53:53  acg
491519SN/A// Andy Goodrich: added $Log command so that CVS comments are reproduced in
501519SN/A// the source.
511519SN/A//
521519SN/A
531519SN/A#ifndef SC_BV_BASE_H
5412247Sgabeblack@google.com#define SC_BV_BASE_H
5512247Sgabeblack@google.com
5612247Sgabeblack@google.com
5712247Sgabeblack@google.com#include "sysc/datatypes/bit/sc_bit_ids.h"
5812247Sgabeblack@google.com#include "sysc/datatypes/bit/sc_bit_proxies.h"
5912247Sgabeblack@google.com#include "sysc/datatypes/bit/sc_proxy.h"
6012247Sgabeblack@google.com#include "sysc/datatypes/int/sc_length_param.h"
6112247Sgabeblack@google.com
6212247Sgabeblack@google.com
6312247Sgabeblack@google.comnamespace sc_dt
6412247Sgabeblack@google.com{
6512247Sgabeblack@google.com
6612247Sgabeblack@google.com// classes defined in this module
6712247Sgabeblack@google.comclass sc_bv_base;
6812247Sgabeblack@google.com
6912247Sgabeblack@google.com
7012247Sgabeblack@google.com// ----------------------------------------------------------------------------
7112247Sgabeblack@google.com//  CLASS : sc_bv_base
7212247Sgabeblack@google.com//
7312247Sgabeblack@google.com//  Arbitrary size bit vector base class.
7412247Sgabeblack@google.com// ----------------------------------------------------------------------------
7512247Sgabeblack@google.com
7612247Sgabeblack@google.comclass sc_bv_base
7712247Sgabeblack@google.com    : public sc_proxy<sc_bv_base>
7812247Sgabeblack@google.com{
7912247Sgabeblack@google.com    friend class sc_lv_base;
8012247Sgabeblack@google.com
8112247Sgabeblack@google.com
8212247Sgabeblack@google.com    void init( int length_, bool init_value = false );
8312247Sgabeblack@google.com
8412247Sgabeblack@google.com    void assign_from_string( const std::string& );
8512247Sgabeblack@google.com
8612247Sgabeblack@google.compublic:
8712247Sgabeblack@google.com
8812247Sgabeblack@google.com    // typedefs
8912247Sgabeblack@google.com
9012247Sgabeblack@google.com    typedef sc_proxy<sc_bv_base> base_type;
911519SN/A
9213663Sandreas.sandberg@arm.com
931606SN/A    // constructors
9412247Sgabeblack@google.com
9512247Sgabeblack@google.com    explicit sc_bv_base( int length_ = sc_length_param().len() )
9613696Sgabeblack@google.com	: m_len( 0 ), m_size( 0 ), m_data( 0 )
9712247Sgabeblack@google.com	{ init( length_ ); }
9812247Sgabeblack@google.com
9913696Sgabeblack@google.com    explicit sc_bv_base( bool a,
10013696Sgabeblack@google.com			 int length_ = sc_length_param().len() )
10113696Sgabeblack@google.com	: m_len( 0 ), m_size( 0 ), m_data( 0 )
10213696Sgabeblack@google.com	{ init( length_, a ); }
10313696Sgabeblack@google.com
10413696Sgabeblack@google.com    sc_bv_base( const char* a );
10513696Sgabeblack@google.com
10612247Sgabeblack@google.com    sc_bv_base( const char* a, int length_ );
10712247Sgabeblack@google.com
10812247Sgabeblack@google.com    template <class X>
10913696Sgabeblack@google.com    sc_bv_base( const sc_proxy<X>& a )
11012247Sgabeblack@google.com	: m_len( 0 ), m_size( 0 ), m_data( 0 )
11112247Sgabeblack@google.com	{ init( a.back_cast().length() ); base_type::assign_( a ); }
11212247Sgabeblack@google.com
11312247Sgabeblack@google.com    sc_bv_base( const sc_bv_base& a );
11412247Sgabeblack@google.com
11513696Sgabeblack@google.com#ifdef SC_DT_DEPRECATED
11612247Sgabeblack@google.com
1171606SN/A    explicit sc_bv_base( const sc_unsigned& a )
11813696Sgabeblack@google.com	: m_len( 0 ), m_size( 0 ), m_data( 0 )
11913696Sgabeblack@google.com	{ init( a.length() ); base_type::assign_( a ); }
12013696Sgabeblack@google.com
12113696Sgabeblack@google.com    explicit sc_bv_base( const sc_signed& a )
12212247Sgabeblack@google.com	: m_len( 0 ), m_size( 0 ), m_data( 0 )
12312247Sgabeblack@google.com	{ init( a.length() ); base_type::assign_( a ); }
12412247Sgabeblack@google.com
12512247Sgabeblack@google.com    explicit sc_bv_base( const sc_uint_base& a)
12612247Sgabeblack@google.com	: m_len( 0 ), m_size( 0 ), m_data( 0 )
12712247Sgabeblack@google.com	{ init( a.length() ); base_type::assign_( a ); }
12812247Sgabeblack@google.com
12912247Sgabeblack@google.com    explicit sc_bv_base( const sc_int_base& a)
13013696Sgabeblack@google.com	: m_len( 0 ), m_size( 0 ), m_data( 0 )
13113696Sgabeblack@google.com	{ init( a.length() ); base_type::assign_( a ); }
1321606SN/A
13312247Sgabeblack@google.com#endif
13412247Sgabeblack@google.com
13512247Sgabeblack@google.com
13612247Sgabeblack@google.com    // destructor
13712247Sgabeblack@google.com
1381858SN/A    virtual ~sc_bv_base()
1391606SN/A	{ delete [] m_data; }
14012247Sgabeblack@google.com
1411606SN/A
1421606SN/A    // assignment operators
14312247Sgabeblack@google.com
14412247Sgabeblack@google.com    template <class X>
14512247Sgabeblack@google.com    sc_bv_base& operator = ( const sc_proxy<X>& a )
14612247Sgabeblack@google.com	{ assign_p_( *this, a ); return *this; }
1471858SN/A
1481519SN/A    sc_bv_base& operator = ( const sc_bv_base& a )
1491589SN/A	{ assign_p_( *this, a ); return *this; }
15012247Sgabeblack@google.com
1511519SN/A    sc_bv_base& operator = ( const char* a );
1521589SN/A
15312247Sgabeblack@google.com    sc_bv_base& operator = ( const bool* a )
1541606SN/A	{ base_type::assign_( a ); return *this; }
1554167SN/A
1561606SN/A    sc_bv_base& operator = ( const sc_logic* a )
1571606SN/A	{ base_type::assign_( a ); return *this; }
15812247Sgabeblack@google.com
15913663Sandreas.sandberg@arm.com    sc_bv_base& operator = ( const sc_unsigned& a )
1601606SN/A	{ base_type::assign_( a ); return *this; }
1611606SN/A
1621606SN/A    sc_bv_base& operator = ( const sc_signed& a )
16312247Sgabeblack@google.com	{ base_type::assign_( a ); return *this; }
1641606SN/A
1651606SN/A    sc_bv_base& operator = ( const sc_uint_base& a )
1661606SN/A	{ base_type::assign_( a ); return *this; }
16713663Sandreas.sandberg@arm.com
1681606SN/A    sc_bv_base& operator = ( const sc_int_base& a )
1694167SN/A	{ base_type::assign_( a ); return *this; }
1704167SN/A
1714167SN/A    sc_bv_base& operator = ( unsigned long a )
17212247Sgabeblack@google.com	{ base_type::assign_( a ); return *this; }
1734167SN/A
1744167SN/A    sc_bv_base& operator = ( long a )
1754167SN/A	{ base_type::assign_( a ); return *this; }
1764167SN/A
17712247Sgabeblack@google.com    sc_bv_base& operator = ( unsigned int a )
17813663Sandreas.sandberg@arm.com	{ base_type::assign_( a ); return *this; }
1794167SN/A
1804167SN/A    sc_bv_base& operator = ( int a )
18113663Sandreas.sandberg@arm.com	{ base_type::assign_( a ); return *this; }
1821519SN/A
1831589SN/A    sc_bv_base& operator = ( uint64 a )
18412247Sgabeblack@google.com	{ base_type::assign_( a ); return *this; }
1851519SN/A
1861589SN/A    sc_bv_base& operator = ( int64 a )
18712247Sgabeblack@google.com	{ base_type::assign_( a ); return *this; }
1881519SN/A
1891589SN/A
19012247Sgabeblack@google.com#if 0
1917777Sgblack@eecs.umich.edu
1927777Sgblack@eecs.umich.edu    // bitwise complement
1937777Sgblack@eecs.umich.edu
19413663Sandreas.sandberg@arm.com    sc_bv_base& b_not();
1957777Sgblack@eecs.umich.edu
1967777Sgblack@eecs.umich.edu    const sc_bv_base operator ~ () const
1977777Sgblack@eecs.umich.edu	{ sc_bv_base a( *this ); return a.b_not(); }
19813663Sandreas.sandberg@arm.com
1997777Sgblack@eecs.umich.edu
2007777Sgblack@eecs.umich.edu    // bitwise left shift
2017777Sgblack@eecs.umich.edu
20213663Sandreas.sandberg@arm.com    sc_bv_base& operator <<= ( int n );
2037777Sgblack@eecs.umich.edu
2047777Sgblack@eecs.umich.edu    const sc_bv_base operator << ( int n ) const
2057777Sgblack@eecs.umich.edu	{ sc_bv_base a( *this ); return ( a <<= n ); }
2067777Sgblack@eecs.umich.edu
2077777Sgblack@eecs.umich.edu
2087777Sgblack@eecs.umich.edu    // bitwise right shift
20913663Sandreas.sandberg@arm.com
2107777Sgblack@eecs.umich.edu    sc_bv_base& operator >>= ( int n );
2117777Sgblack@eecs.umich.edu
2127777Sgblack@eecs.umich.edu    const sc_bv_base operator >> ( int n ) const
2137777Sgblack@eecs.umich.edu	{ sc_bv_base a( *this ); return ( a >>= n ); }
2147777Sgblack@eecs.umich.edu
2157777Sgblack@eecs.umich.edu
21613663Sandreas.sandberg@arm.com    // bitwise left rotate
2177777Sgblack@eecs.umich.edu
2187777Sgblack@eecs.umich.edu    sc_bv_base& lrotate( int n );
2197777Sgblack@eecs.umich.edu
2207777Sgblack@eecs.umich.edu
2217777Sgblack@eecs.umich.edu    // bitwise right rotate
2227777Sgblack@eecs.umich.edu
2237777Sgblack@eecs.umich.edu    sc_bv_base& rrotate( int n );
2247777Sgblack@eecs.umich.edu
2257777Sgblack@eecs.umich.edu#endif
2267777Sgblack@eecs.umich.edu
22713663Sandreas.sandberg@arm.com
2287777Sgblack@eecs.umich.edu    // common methods
22913663Sandreas.sandberg@arm.com
2307777Sgblack@eecs.umich.edu    int length() const
2317777Sgblack@eecs.umich.edu	{ return m_len; }
2327777Sgblack@eecs.umich.edu
23313663Sandreas.sandberg@arm.com    int size() const
2347777Sgblack@eecs.umich.edu	{ return m_size; }
2357777Sgblack@eecs.umich.edu
2367777Sgblack@eecs.umich.edu    sc_logic_value_t get_bit( int i ) const;
2377777Sgblack@eecs.umich.edu    void set_bit( int i, sc_logic_value_t value );
23813663Sandreas.sandberg@arm.com
2397777Sgblack@eecs.umich.edu    sc_digit get_word( int i ) const
2409827Sakash.bagdia@arm.com	{ return m_data[i]; }
2419827Sakash.bagdia@arm.com
24212247Sgabeblack@google.com    void set_word( int i, sc_digit w )
2439827Sakash.bagdia@arm.com	{ m_data[i] = w; }
24410427Sandreas.hansson@arm.com
24512247Sgabeblack@google.com    sc_digit get_cword( int /*i*/ ) const
24612251Sgabeblack@google.com	{ return SC_DIGIT_ZERO; }
24712251Sgabeblack@google.com
24812251Sgabeblack@google.com    void set_cword( int i, sc_digit w );
249
250    void clean_tail();
251
252
253    // other methods
254
255    bool is_01() const
256	{ return true; }
257
258protected:
259
260    int     m_len;  // length in bits
261    int     m_size; // size of data array
262    sc_digit* m_data; // data array
263};
264
265
266// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
267
268#if 0
269
270// bitwise left rotate
271
272inline
273const sc_bv_base
274lrotate( const sc_bv_base& x, int n )
275{
276    sc_bv_base a( x );
277    return a.lrotate( n );
278}
279
280
281// bitwise right rotate
282
283inline
284const sc_bv_base
285rrotate( const sc_bv_base& x, int n )
286{
287    sc_bv_base a( x );
288    return a.rrotate( n );
289}
290
291#endif
292
293
294// common methods
295
296inline
297sc_logic_value_t
298sc_bv_base::get_bit( int i ) const
299{
300    int wi = i / SC_DIGIT_SIZE;
301    int bi = i % SC_DIGIT_SIZE;
302    return sc_logic_value_t( (m_data[wi] >> bi) & SC_DIGIT_ONE );
303}
304
305inline
306void
307sc_bv_base::set_bit( int i, sc_logic_value_t value )
308{
309    int wi = i / SC_DIGIT_SIZE;
310    int bi = i % SC_DIGIT_SIZE;
311    sc_digit mask = SC_DIGIT_ONE << bi;
312    m_data[wi] |= mask; // set bit to 1
313    m_data[wi] &= value << bi | ~mask;
314}
315
316
317inline
318void
319sc_bv_base::set_cword( int /*i*/, sc_digit w )
320{
321    if( w ) {
322	SC_REPORT_WARNING( sc_core::SC_ID_SC_BV_CANNOT_CONTAIN_X_AND_Z_, 0 );
323    }
324}
325
326
327inline
328void
329sc_bv_base::clean_tail()
330{
331    int wi = m_size - 1;
332    int bi = m_len % SC_DIGIT_SIZE;
333	if ( bi != 0 ) m_data[wi] &= ~SC_DIGIT_ZERO >> (SC_DIGIT_SIZE - bi);
334}
335
336} // namespace sc_dt
337
338
339#endif
340