112855Sgabeblack@google.com/*****************************************************************************
212855Sgabeblack@google.com
312855Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412855Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
512855Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
612855Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
712855Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
812855Sgabeblack@google.com  License.  You may obtain a copy of the License at
912855Sgabeblack@google.com
1012855Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1112855Sgabeblack@google.com
1212855Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1312855Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1412855Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512855Sgabeblack@google.com  implied.  See the License for the specific language governing
1612855Sgabeblack@google.com  permissions and limitations under the License.
1712855Sgabeblack@google.com
1812855Sgabeblack@google.com *****************************************************************************/
1912855Sgabeblack@google.com
2012855Sgabeblack@google.com/*****************************************************************************
2112855Sgabeblack@google.com
2212855Sgabeblack@google.com  test_bitref.cpp -- Test using bitrefs in boolean contexts
2312855Sgabeblack@google.com
2412855Sgabeblack@google.com  Original Author: Philipp A. Hartmann, OFFIS, 2013-10-28
2512855Sgabeblack@google.com
2612855Sgabeblack@google.com*****************************************************************************/
2712855Sgabeblack@google.com
2812855Sgabeblack@google.com#include <systemc.h>
2912855Sgabeblack@google.com
3012855Sgabeblack@google.com#define sc_nassert( expr ) \
3112855Sgabeblack@google.com  sc_assert( !(expr) )
3212855Sgabeblack@google.com
3312855Sgabeblack@google.comusing sc_dt::sc_bitref;
3412855Sgabeblack@google.comusing sc_dt::sc_bitref_r;
3512855Sgabeblack@google.comusing sc_dt::sc_concref;
3612855Sgabeblack@google.comusing sc_dt::sc_concref_r;
3712855Sgabeblack@google.comusing sc_dt::sc_subref;
3812855Sgabeblack@google.comusing sc_dt::sc_subref_r;
3912855Sgabeblack@google.com
4012855Sgabeblack@google.comint sc_main(int,char*[])
4112855Sgabeblack@google.com{
4212855Sgabeblack@google.com  sc_bv<8> bv = "00101010";
4312855Sgabeblack@google.com  sc_lv<8> lv = "1XZ01010";
4412855Sgabeblack@google.com
4512855Sgabeblack@google.com  // plain bitrefs
4612855Sgabeblack@google.com  {
4712855Sgabeblack@google.com    sc_nassert( bv[0] );
4812855Sgabeblack@google.com    sc_assert ( !bv[0] );
4912855Sgabeblack@google.com    // sc_assert( ~bv[0] ); // could not convert ... to ‘bool’
5012855Sgabeblack@google.com    sc_nassert( bv[0].to_bool() );
5112855Sgabeblack@google.com    sc_assert ( !bv[0].to_bool() );
5212855Sgabeblack@google.com    sc_assert( (~bv[0]).to_bool() );
5312855Sgabeblack@google.com
5412855Sgabeblack@google.com#if IEEE_1666_CPLUSPLUS >= 201103L
5512855Sgabeblack@google.com    sc_assert( bv[1] );
5612855Sgabeblack@google.com#endif
5712855Sgabeblack@google.com    sc_nassert( !bv[1] );
5812855Sgabeblack@google.com    sc_assert ( bv[1].to_bool() );
5912855Sgabeblack@google.com    sc_nassert( (~bv[1]).to_bool() );
6012855Sgabeblack@google.com
6112855Sgabeblack@google.com    sc_nassert( bv[0] == true );
6212855Sgabeblack@google.com    sc_assert ( bv[0] != true );
6312855Sgabeblack@google.com    sc_assert ( bv[0] == SC_LOGIC_0 );
6412855Sgabeblack@google.com    sc_nassert( bv[0].to_bool() );
6512855Sgabeblack@google.com    sc_assert( bv[1].to_bool() );
6612855Sgabeblack@google.com    sc_nassert( bv[0] != 0 );
6712855Sgabeblack@google.com    sc_nassert( bv[0] == bv[1] );
6812855Sgabeblack@google.com    sc_nassert( SC_LOGIC_1 != bv[1] );
6912855Sgabeblack@google.com
7012855Sgabeblack@google.com    sc_assert ( bv[0] == '0' );
7112855Sgabeblack@google.com    sc_nassert( bv[1] == '0' );
7212855Sgabeblack@google.com    sc_assert ( bv[1] == '1' );
7312855Sgabeblack@google.com    sc_nassert( bv[0] == '1' );
7412855Sgabeblack@google.com
7512855Sgabeblack@google.com    sc_nassert( bv[0] != '0' );
7612855Sgabeblack@google.com    sc_assert ( bv[1] != '0' );
7712855Sgabeblack@google.com    sc_nassert( bv[1] != '1' );
7812855Sgabeblack@google.com    sc_assert ( bv[0] != '1' );
7912855Sgabeblack@google.com
8012855Sgabeblack@google.com    sc_assert ( '0' == bv[0] );
8112855Sgabeblack@google.com    sc_nassert( '0' == bv[1] );
8212855Sgabeblack@google.com    sc_assert ( '1' == bv[1] );
8312855Sgabeblack@google.com    sc_nassert( '1' == bv[0] );
8412855Sgabeblack@google.com
8512855Sgabeblack@google.com    sc_nassert( '0' != bv[0] );
8612855Sgabeblack@google.com    sc_assert ( '0' != bv[1] );
8712855Sgabeblack@google.com    sc_nassert( '1' != bv[1] );
8812855Sgabeblack@google.com    sc_assert ( '1' != bv[0] );
8912855Sgabeblack@google.com
9012855Sgabeblack@google.com    sc_nassert( ~bv[0] == '0' );
9112855Sgabeblack@google.com    sc_assert ( ~bv[1] == '0' );
9212855Sgabeblack@google.com    sc_nassert( ~bv[1] == '1' );
9312855Sgabeblack@google.com    sc_assert ( ~bv[0] == '1' );
9412855Sgabeblack@google.com
9512855Sgabeblack@google.com    sc_assert ( ~bv[0] != '0' );
9612855Sgabeblack@google.com    sc_nassert( ~bv[1] != '0' );
9712855Sgabeblack@google.com    sc_assert ( ~bv[1] != '1' );
9812855Sgabeblack@google.com    sc_nassert( ~bv[0] != '1' );
9912855Sgabeblack@google.com
10012855Sgabeblack@google.com    sc_nassert( '0' == ~bv[0] );
10112855Sgabeblack@google.com    sc_assert ( '0' == ~bv[1] );
10212855Sgabeblack@google.com    sc_nassert( '1' == ~bv[1] );
10312855Sgabeblack@google.com    sc_assert ( '1' == ~bv[0] );
10412855Sgabeblack@google.com
10512855Sgabeblack@google.com    sc_assert ( '0' != ~bv[0] );
10612855Sgabeblack@google.com    sc_nassert( '0' != ~bv[1] );
10712855Sgabeblack@google.com    sc_assert ( '1' != ~bv[1] );
10812855Sgabeblack@google.com    sc_nassert( '1' != ~bv[0] );
10912855Sgabeblack@google.com
11012855Sgabeblack@google.com
11112855Sgabeblack@google.com
11212855Sgabeblack@google.com    sc_assert( bv[0] == lv[0] );
11312855Sgabeblack@google.com    sc_assert( bv[0] != lv[6] );
11412855Sgabeblack@google.com
11512855Sgabeblack@google.com    sc_assert( bv.or_reduce() );
11612855Sgabeblack@google.com    sc_assert( !bv.nor_reduce() );
11712855Sgabeblack@google.com
11812855Sgabeblack@google.com    sc_assert ( lv[0] == false );
11912855Sgabeblack@google.com    sc_assert ( lv[5] == SC_LOGIC_Z );
12012855Sgabeblack@google.com    sc_assert ( lv[6] == SC_LOGIC_X );
12112855Sgabeblack@google.com    sc_assert ( lv[6].value() == SC_LOGIC_X );
12212855Sgabeblack@google.com
12312855Sgabeblack@google.com    // sc_assert( ~lv[0] ); // could not convert ... to ‘bool’
12412855Sgabeblack@google.com    // sc_assert( lv[7] );  // could not convert ... to ‘bool’
12512855Sgabeblack@google.com    // sc_assert( !lv[0] ); // could not convert ... to ‘bool’
12612855Sgabeblack@google.com
12712855Sgabeblack@google.com    sc_assert( !lv[0].to_bool() );
12812855Sgabeblack@google.com    sc_assert( (~lv[0]).to_bool() );
12912855Sgabeblack@google.com
13012855Sgabeblack@google.com    // with warnings
13112855Sgabeblack@google.com    sc_assert( lv[5].to_bool() );
13212855Sgabeblack@google.com    sc_assert( lv[6].to_bool() );
13312855Sgabeblack@google.com  }
13412855Sgabeblack@google.com
13512855Sgabeblack@google.com  // bitrefs to subrefs
13612855Sgabeblack@google.com  {
13712855Sgabeblack@google.com    /* auto */ sc_subref_r< sc_bv_base > bv_range_r = bv.range(5,1);
13812855Sgabeblack@google.com    /* auto */ sc_subref_r< sc_lv_base > lv_range_r = lv.range(6,2);
13912855Sgabeblack@google.com
14012855Sgabeblack@google.com    /* auto */ sc_subref< sc_bv_base >   bv_range   = bv.range(5,1);
14112855Sgabeblack@google.com    /* auto */ sc_subref< sc_lv_base >   lv_range   = lv.range(6,2);
14212855Sgabeblack@google.com
14312855Sgabeblack@google.com#if IEEE_1666_CPLUSPLUS >= 201103L
14412855Sgabeblack@google.com    sc_assert( bv_range[0] );
14512855Sgabeblack@google.com#endif
14612855Sgabeblack@google.com    sc_nassert( !bv_range[0] );
14712855Sgabeblack@google.com    sc_assert( !bv_range[1] );
14812855Sgabeblack@google.com    sc_assert( (~bv_range[1]).to_bool() );
14912855Sgabeblack@google.com
15012855Sgabeblack@google.com    sc_assert( bv[1] == bv_range_r[0] );
15112855Sgabeblack@google.com
15212855Sgabeblack@google.com    bv_range[0] = false;
15312855Sgabeblack@google.com    sc_assert( !bv[1] );
15412855Sgabeblack@google.com    sc_assert( bv[1] == bv_range_r[0] );
15512855Sgabeblack@google.com    sc_assert( bv[1] == bv_range[0] );
15612855Sgabeblack@google.com
15712855Sgabeblack@google.com    bv_range[0] = SC_LOGIC_1;
15812855Sgabeblack@google.com#if IEEE_1666_CPLUSPLUS >= 201103L
15912855Sgabeblack@google.com    sc_assert( bv[1] );
16012855Sgabeblack@google.com#endif
16112855Sgabeblack@google.com    sc_assert( bv[1] == bv_range_r[0] );
16212855Sgabeblack@google.com    sc_assert( bv[1] == bv_range[0] );
16312855Sgabeblack@google.com
16412855Sgabeblack@google.com    // sc_assert( ~lv_range[0] ); // could not convert ... to ‘bool’
16512855Sgabeblack@google.com    // sc_assert( lv_range_r[7] );  // could not convert ... to ‘bool’
16612855Sgabeblack@google.com
16712855Sgabeblack@google.com    sc_assert( !lv_range_r[0].to_bool() );
16812855Sgabeblack@google.com    sc_assert( lv_range_r[1].to_bool() );
16912855Sgabeblack@google.com    sc_nassert( (~lv_range[1]).to_bool() );
17012855Sgabeblack@google.com
17112855Sgabeblack@google.com    // with warnings
17212855Sgabeblack@google.com    sc_assert( lv_range[3].to_bool() );
17312855Sgabeblack@google.com    sc_assert( lv_range_r[4].to_bool() );
17412855Sgabeblack@google.com  }
17512855Sgabeblack@google.com
17612855Sgabeblack@google.com  // bitrefs to concrefs
17712855Sgabeblack@google.com  {
17812855Sgabeblack@google.com    /* auto */ sc_concref< sc_concref<sc_subref<sc_bv_base>, sc_subref<sc_bv_base> >
17912855Sgabeblack@google.com                         , sc_bitref<sc_bv_base> >
18012855Sgabeblack@google.com                 bv_range = ( bv.range(7,6) , bv.range(5,1), bv[1] );
18112855Sgabeblack@google.com    /* auto */ sc_concref_r< sc_concref_r<sc_subref<sc_bv_base>, sc_subref<sc_lv_base> >
18212855Sgabeblack@google.com                           , sc_bv_base >
18312855Sgabeblack@google.com      lv_range_r = ( bv.range(7,6) , lv.range(6,2), true );
18412855Sgabeblack@google.com
18512855Sgabeblack@google.com#if IEEE_1666_CPLUSPLUS >= 201103L
18612855Sgabeblack@google.com    sc_assert( bv_range[0] );
18712855Sgabeblack@google.com#endif
18812855Sgabeblack@google.com    sc_nassert( !bv_range[0] );
18912855Sgabeblack@google.com    sc_assert( !bv_range[2] );
19012855Sgabeblack@google.com    // sc_assert( ~bv_range[2] ); // could not convert ... to ‘bool’
19112855Sgabeblack@google.com
19212855Sgabeblack@google.com    sc_assert( bv[1] == bv_range[0] );
19312855Sgabeblack@google.com
19412855Sgabeblack@google.com    bv_range[0] = false;
19512855Sgabeblack@google.com    sc_assert( !bv[1] );
19612855Sgabeblack@google.com    sc_assert( bv[1] == bv_range[0] );
19712855Sgabeblack@google.com
19812855Sgabeblack@google.com    bv_range[0] = SC_LOGIC_1;
19912855Sgabeblack@google.com#if IEEE_1666_CPLUSPLUS >= 201103L
20012855Sgabeblack@google.com    sc_assert( bv[1] );
20112855Sgabeblack@google.com#endif
20212855Sgabeblack@google.com    sc_assert( bv[1] == bv_range[0] );
20312855Sgabeblack@google.com
20412855Sgabeblack@google.com    // sc_assert( ~lv_range_r[0] ); // could not convert ... to ‘bool’
20512855Sgabeblack@google.com    // sc_assert( lv_range_r[7] );  // could not convert ... to ‘bool’
20612855Sgabeblack@google.com    // sc_assert( !lv_range_r[0] ); // could not convert ... to ‘bool’
20712855Sgabeblack@google.com
20812855Sgabeblack@google.com    sc_assert( lv_range_r[0].to_bool() );
20912855Sgabeblack@google.com    sc_assert( !lv_range_r[1].to_bool() );
21012855Sgabeblack@google.com    sc_assert( (~lv_range_r[1]).to_bool() );
21112855Sgabeblack@google.com
21212855Sgabeblack@google.com    // with warnings
21312855Sgabeblack@google.com    sc_assert( lv_range_r[4].to_bool() );
21412855Sgabeblack@google.com    sc_assert( lv_range_r[5].to_bool() );
21512855Sgabeblack@google.com  }
21612855Sgabeblack@google.com
21712855Sgabeblack@google.com  return 0;
21812855Sgabeblack@google.com}
219