test_bitref.cpp revision 12855:588919e0e4aa
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  test_bitref.cpp -- Test using bitrefs in boolean contexts
23
24  Original Author: Philipp A. Hartmann, OFFIS, 2013-10-28
25
26*****************************************************************************/
27
28#include <systemc.h>
29
30#define sc_nassert( expr ) \
31  sc_assert( !(expr) )
32
33using sc_dt::sc_bitref;
34using sc_dt::sc_bitref_r;
35using sc_dt::sc_concref;
36using sc_dt::sc_concref_r;
37using sc_dt::sc_subref;
38using sc_dt::sc_subref_r;
39
40int sc_main(int,char*[])
41{
42  sc_bv<8> bv = "00101010";
43  sc_lv<8> lv = "1XZ01010";
44
45  // plain bitrefs
46  {
47    sc_nassert( bv[0] );
48    sc_assert ( !bv[0] );
49    // sc_assert( ~bv[0] ); // could not convert ... to ‘bool’
50    sc_nassert( bv[0].to_bool() );
51    sc_assert ( !bv[0].to_bool() );
52    sc_assert( (~bv[0]).to_bool() );
53
54#if IEEE_1666_CPLUSPLUS >= 201103L
55    sc_assert( bv[1] );
56#endif
57    sc_nassert( !bv[1] );
58    sc_assert ( bv[1].to_bool() );
59    sc_nassert( (~bv[1]).to_bool() );
60
61    sc_nassert( bv[0] == true );
62    sc_assert ( bv[0] != true );
63    sc_assert ( bv[0] == SC_LOGIC_0 );
64    sc_nassert( bv[0].to_bool() );
65    sc_assert( bv[1].to_bool() );
66    sc_nassert( bv[0] != 0 );
67    sc_nassert( bv[0] == bv[1] );
68    sc_nassert( SC_LOGIC_1 != bv[1] );
69
70    sc_assert ( bv[0] == '0' );
71    sc_nassert( bv[1] == '0' );
72    sc_assert ( bv[1] == '1' );
73    sc_nassert( bv[0] == '1' );
74
75    sc_nassert( bv[0] != '0' );
76    sc_assert ( bv[1] != '0' );
77    sc_nassert( bv[1] != '1' );
78    sc_assert ( bv[0] != '1' );
79
80    sc_assert ( '0' == bv[0] );
81    sc_nassert( '0' == bv[1] );
82    sc_assert ( '1' == bv[1] );
83    sc_nassert( '1' == bv[0] );
84
85    sc_nassert( '0' != bv[0] );
86    sc_assert ( '0' != bv[1] );
87    sc_nassert( '1' != bv[1] );
88    sc_assert ( '1' != bv[0] );
89
90    sc_nassert( ~bv[0] == '0' );
91    sc_assert ( ~bv[1] == '0' );
92    sc_nassert( ~bv[1] == '1' );
93    sc_assert ( ~bv[0] == '1' );
94
95    sc_assert ( ~bv[0] != '0' );
96    sc_nassert( ~bv[1] != '0' );
97    sc_assert ( ~bv[1] != '1' );
98    sc_nassert( ~bv[0] != '1' );
99
100    sc_nassert( '0' == ~bv[0] );
101    sc_assert ( '0' == ~bv[1] );
102    sc_nassert( '1' == ~bv[1] );
103    sc_assert ( '1' == ~bv[0] );
104
105    sc_assert ( '0' != ~bv[0] );
106    sc_nassert( '0' != ~bv[1] );
107    sc_assert ( '1' != ~bv[1] );
108    sc_nassert( '1' != ~bv[0] );
109
110
111
112    sc_assert( bv[0] == lv[0] );
113    sc_assert( bv[0] != lv[6] );
114
115    sc_assert( bv.or_reduce() );
116    sc_assert( !bv.nor_reduce() );
117
118    sc_assert ( lv[0] == false );
119    sc_assert ( lv[5] == SC_LOGIC_Z );
120    sc_assert ( lv[6] == SC_LOGIC_X );
121    sc_assert ( lv[6].value() == SC_LOGIC_X );
122
123    // sc_assert( ~lv[0] ); // could not convert ... to ‘bool’
124    // sc_assert( lv[7] );  // could not convert ... to ‘bool’
125    // sc_assert( !lv[0] ); // could not convert ... to ‘bool’
126
127    sc_assert( !lv[0].to_bool() );
128    sc_assert( (~lv[0]).to_bool() );
129
130    // with warnings
131    sc_assert( lv[5].to_bool() );
132    sc_assert( lv[6].to_bool() );
133  }
134
135  // bitrefs to subrefs
136  {
137    /* auto */ sc_subref_r< sc_bv_base > bv_range_r = bv.range(5,1);
138    /* auto */ sc_subref_r< sc_lv_base > lv_range_r = lv.range(6,2);
139
140    /* auto */ sc_subref< sc_bv_base >   bv_range   = bv.range(5,1);
141    /* auto */ sc_subref< sc_lv_base >   lv_range   = lv.range(6,2);
142
143#if IEEE_1666_CPLUSPLUS >= 201103L
144    sc_assert( bv_range[0] );
145#endif
146    sc_nassert( !bv_range[0] );
147    sc_assert( !bv_range[1] );
148    sc_assert( (~bv_range[1]).to_bool() );
149
150    sc_assert( bv[1] == bv_range_r[0] );
151
152    bv_range[0] = false;
153    sc_assert( !bv[1] );
154    sc_assert( bv[1] == bv_range_r[0] );
155    sc_assert( bv[1] == bv_range[0] );
156
157    bv_range[0] = SC_LOGIC_1;
158#if IEEE_1666_CPLUSPLUS >= 201103L
159    sc_assert( bv[1] );
160#endif
161    sc_assert( bv[1] == bv_range_r[0] );
162    sc_assert( bv[1] == bv_range[0] );
163
164    // sc_assert( ~lv_range[0] ); // could not convert ... to ‘bool’
165    // sc_assert( lv_range_r[7] );  // could not convert ... to ‘bool’
166
167    sc_assert( !lv_range_r[0].to_bool() );
168    sc_assert( lv_range_r[1].to_bool() );
169    sc_nassert( (~lv_range[1]).to_bool() );
170
171    // with warnings
172    sc_assert( lv_range[3].to_bool() );
173    sc_assert( lv_range_r[4].to_bool() );
174  }
175
176  // bitrefs to concrefs
177  {
178    /* auto */ sc_concref< sc_concref<sc_subref<sc_bv_base>, sc_subref<sc_bv_base> >
179                         , sc_bitref<sc_bv_base> >
180                 bv_range = ( bv.range(7,6) , bv.range(5,1), bv[1] );
181    /* auto */ sc_concref_r< sc_concref_r<sc_subref<sc_bv_base>, sc_subref<sc_lv_base> >
182                           , sc_bv_base >
183      lv_range_r = ( bv.range(7,6) , lv.range(6,2), true );
184
185#if IEEE_1666_CPLUSPLUS >= 201103L
186    sc_assert( bv_range[0] );
187#endif
188    sc_nassert( !bv_range[0] );
189    sc_assert( !bv_range[2] );
190    // sc_assert( ~bv_range[2] ); // could not convert ... to ‘bool’
191
192    sc_assert( bv[1] == bv_range[0] );
193
194    bv_range[0] = false;
195    sc_assert( !bv[1] );
196    sc_assert( bv[1] == bv_range[0] );
197
198    bv_range[0] = SC_LOGIC_1;
199#if IEEE_1666_CPLUSPLUS >= 201103L
200    sc_assert( bv[1] );
201#endif
202    sc_assert( bv[1] == bv_range[0] );
203
204    // sc_assert( ~lv_range_r[0] ); // could not convert ... to ‘bool’
205    // sc_assert( lv_range_r[7] );  // could not convert ... to ‘bool’
206    // sc_assert( !lv_range_r[0] ); // could not convert ... to ‘bool’
207
208    sc_assert( lv_range_r[0].to_bool() );
209    sc_assert( !lv_range_r[1].to_bool() );
210    sc_assert( (~lv_range_r[1]).to_bool() );
211
212    // with warnings
213    sc_assert( lv_range_r[4].to_bool() );
214    sc_assert( lv_range_r[5].to_bool() );
215  }
216
217  return 0;
218}
219