test01.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  test01.cpp --
23
24  Original Author: Joe Buck, Synopsys, Inc., 2002-02-15
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#include "systemc.h"
39
40void cover_sc_bit()
41{
42    sc_bit bdef;
43    sc_bit bf(false);
44    sc_bit bt(true);
45    sc_bit b0(0);
46    sc_bit b1(1);
47    try {
48	sc_bit foo(2);
49    }
50    catch (sc_report) {
51	cout << "Caught exception for sc_bit(2)\n";
52    }
53    sc_bit bc0('0');
54    sc_bit bc1('1');
55    try {
56	sc_bit foo('2');
57    }
58    catch (sc_report) {
59	cout << "Caught exception for sc_bit('2')\n";
60    }
61    sc_bit blc0(sc_logic('0'));
62    sc_bit blc1(sc_logic('1'));
63    sc_bit blcx(sc_logic('X'));
64    sc_bit bcop(bt);
65    cout << bdef << bf << bt << b0 << b1 << bc0 << bc1 << blc0 << blc1
66	 << blcx << bcop << endl;
67    sc_bit b;
68    b = bt;
69    sc_assert(b);
70    b = 0;
71    sc_assert(!b);
72    b = true;
73    sc_assert(b.to_bool());
74    b = '0';
75    sc_assert(!b.to_bool());
76    b = sc_logic('1');
77    sc_assert(b.to_char() == '1');
78    b = bf;
79    sc_assert(~b);
80    b |= bt;
81    sc_assert(b);
82    b &= bf;
83    sc_assert(!b);
84    b |= 1;
85    sc_assert(b);
86    b &= 0;
87    sc_assert(!b);
88    b |= '1';
89    sc_assert(b);
90    b &= '0';
91    sc_assert(!b);
92    b |= true;
93    sc_assert(b);
94    b &= false;
95    sc_assert(!b);
96    b ^= bt;
97    sc_assert(b);
98    b ^= 1;
99    sc_assert(!b);
100    b ^= '1';
101    sc_assert(b);
102    b ^= true;
103    sc_assert(!b);
104
105    sc_assert(b == bf);
106    sc_assert(b == 0);
107    sc_assert(b == '0');
108    sc_assert(b == false);
109    b = 1;
110    sc_assert(b == bt);
111    sc_assert(b == 1);
112    sc_assert(b == '1');
113    sc_assert(b == true);
114    sc_assert(1 == b);
115    sc_assert('1' == b);
116    sc_assert(true == b);
117    sc_assert(equal(b, bt));
118    sc_assert(equal(b, 1));
119    sc_assert(equal(b, '1'));
120    sc_assert(equal(b, true));
121    sc_assert(equal(1, b));
122    sc_assert(equal('1', b));
123    sc_assert(equal(true, b));
124    b = 0;
125    sc_assert(b != bt);
126    sc_assert(b != 1);
127    sc_assert(b != '1');
128    sc_assert(b != true);
129    sc_assert(1 != b);
130    sc_assert('1' != b);
131    sc_assert(true != b);
132    sc_assert(not_equal(b, bt));
133    sc_assert(not_equal(b, 1));
134    sc_assert(not_equal(b, '1'));
135    sc_assert(not_equal(b, true));
136    sc_assert(not_equal(1, b));
137    sc_assert(not_equal('1', b));
138    sc_assert(not_equal(true, b));
139
140    // the following assertion is incorrect, because the b_not() method
141    // is destructive, i.e., it implements something like b ~= void.
142    /// sc_assert(b == b_not(b.b_not()));
143    b.b_not();
144    sc_assert(b);
145    sc_bit bx;
146    b_not(bx, b0);
147    sc_assert(bx);
148    b_not(bx, b1);
149    sc_assert(!bx);
150
151    cout << (b0|b0) << (b0|b1) << (b1|b0) << (b1|b1) << endl;
152    cout << (b0&b0) << (b0&b1) << (b1&b0) << (b1&b1) << endl;
153    cout << (b0^b0) << (b0^b1) << (b1^b0) << (b1^b1) << endl;
154
155    cout << (b0|0) << (b0|1) << (b1|0) << (b1|1) << endl;
156    cout << (b0&0) << (b0&1) << (b1&0) << (b1&1) << endl;
157    cout << (b0^0) << (b0^1) << (b1^0) << (b1^1) << endl;
158
159    cout << (b0|'0') << (b0|'1') << (b1|'0') << (b1|'1') << endl;
160    cout << (b0&'0') << (b0&'1') << (b1&'0') << (b1&'1') << endl;
161    cout << (b0^'0') << (b0^'1') << (b1^'0') << (b1^'1') << endl;
162
163    cout << (b0|true) << (b0|false) << (b1|true) << (b1|false) << endl;
164    cout << (b0&true) << (b0&false) << (b1&true) << (b1&false) << endl;
165    cout << (b0^true) << (b0^false) << (b1^true) << (b1^false) << endl;
166
167    cout << (0|b0) << (0|b1) << (1|b0) << (1|b1) << endl;
168    cout << (0&b0) << (0&b1) << (1&b0) << (1&b1) << endl;
169    cout << (0^b0) << (0^b1) << (1^b0) << (1^b1) << endl;
170
171    cout << ('0'|b0) << ('0'|b1) << ('1'|b0) << ('1'|b1) << endl;
172    cout << ('0'&b0) << ('0'&b1) << ('1'&b0) << ('1'&b1) << endl;
173    cout << ('0'^b0) << ('0'^b1) << ('1'^b0) << ('1'^b1) << endl;
174
175    cout << (false|b0) << (false|b1) << (true|b0) << (true|b1) << endl;
176    cout << (false&b0) << (false&b1) << (true&b0) << (true&b1) << endl;
177    cout << (false^b0) << (false^b1) << (true^b0) << (true^b1) << endl;
178
179    cout << b_or(b0,b0) << b_or(b0,b1) << b_or(b1,b0) << b_or(b1,b1) << endl;
180    cout << b_and(b0,b0) << b_and(b0,b1) << b_and(b1,b0) << b_and(b1,b1)
181         << endl;
182    cout << b_xor(b0,b0) << b_xor(b0,b1) << b_xor(b1,b0) << b_xor(b1,b1)
183         << endl;
184
185    cout << b_or(b0,0) << b_or(b0,1) << b_or(b1,0) << b_or(b1,1) << endl;
186    cout << b_and(b0,0) << b_and(b0,1) << b_and(b1,0) << b_and(b1,1) << endl;
187    cout << b_xor(b0,0) << b_xor(b0,1) << b_xor(b1,0) << b_xor(b1,1) << endl;
188
189    cout << b_or(b0,'0') << b_or(b0,'1') << b_or(b1,'0') << b_or(b1,'1')
190         << endl;
191    cout << b_and(b0,'0') << b_and(b0,'1') << b_and(b1,'0') << b_and(b1,'1')
192         << endl;
193    cout << b_xor(b0,'0') << b_xor(b0,'1') << b_xor(b1,'0') << b_xor(b1,'1')
194         << endl;
195
196    cout << b_or(b0,false) << b_or(b0,true) << b_or(b1,false) << b_or(b1,true)
197         << endl;
198    cout << b_and(b0,false) << b_and(b0,true) << b_and(b1,false)
199         << b_and(b1,true) << endl;
200    cout << b_xor(b0,false) << b_xor(b0,true) << b_xor(b1,false)
201         << b_xor(b1,true) << endl;
202
203    cout << b_or(0,b0) << b_or(0,b1) << b_or(1,b0) << b_or(1,b1) << endl;
204    cout << b_and(0,b0) << b_and(0,b1) << b_and(1,b0) << b_and(1,b1) << endl;
205    cout << b_xor(0,b0) << b_xor(0,b1) << b_xor(1,b0) << b_xor(1,b1) << endl;
206
207    cout << b_or('0',b0) << b_or('0',b1) << b_or('1',b0) << b_or('1',b1)
208         << endl;
209    cout << b_and('0',b0) << b_and('0',b1) << b_and('1',b0) << b_and('1',b1)
210         << endl;
211    cout << b_xor('0',b0) << b_xor('0',b1) << b_xor('1',b0) << b_xor('1',b1)
212         << endl;
213
214    cout << b_or(false,b0) << b_or(false,b1) << b_or(true,b0) << b_or(true,b1)
215         << endl;
216    cout << b_and(false,b0) << b_and(false,b1) << b_and(true,b0)
217         << b_and(true,b1) << endl;
218    cout << b_xor(false,b0) << b_xor(false,b1) << b_xor(true,b0)
219         << b_xor(true,b1) << endl;
220
221    b_or(b, b0, b1);
222    sc_assert(b);
223    b_and(b, b0, b1);
224    sc_assert(!b);
225    b_xor(b, b0, b1);
226    sc_assert(b);
227}
228
229int
230sc_main(int, char*[])
231{
232    cover_sc_bit();
233    return 0;
234}
235
236