forked from Fernsicles/RV32-verilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALUtest.cpp
203 lines (190 loc) · 3.99 KB
/
ALUtest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "obj_dir/VALU.h"
#include "verilated.h"
#include <string>
#include <random>
#include <iostream>
using namespace std;
bool add_sub(VALU *tb) {
random_device rand;
tb->i_op = 0b000;
tb->i_op2 = 0b0;
for(int i = 0; i < 10000; i++) {
int x = rand();
int y = rand();
tb->i_x = x;
tb->i_y = y;
tb->eval();
if(tb->o_result != x + y) {
cout << "Failed: Add\t" << x << " + " << y << " = " << tb->o_result << endl;
cout << "Expected: " << x + y << endl;
return false;
}
}
cout << "Passed: Add" << endl;
tb->i_op2 = 0b1;
for(int i = 0; i < 10000; i++) {
int x = rand();
int y = rand();
tb->i_x = x;
tb->i_y = y;
tb->eval();
if(tb->o_result != x - y) {
cout << "Failed: Sub\t" << x << " - " << y << " = " << tb->o_result << endl;
cout << "Expected: " << x - y << endl;
return false;
}
}
cout << "Passed: Sub" << endl;
return true;
}
bool shift_left(VALU *tb) {
random_device rand;
tb->i_op = 0b001;
for(int i = 0; i < 10000; i++) {
int x = rand();
int y = rand() % 32;
tb->i_x = x;
tb->i_y = y;
tb->eval();
int res = x << y;
if(tb->o_result != res) {
cout << "Failed: Sll\t" << x << " << " << y << " = " << tb->o_result << endl;
cout << "Expected: " << res << endl;
return false;
}
}
cout << "Passed: Sll" << endl;
return true;
}
bool set_less(VALU *tb) {
random_device rand;
tb->i_op = 0b010;
for(int i = 0; i < 10000; i++) {
int x = rand();
int y = rand();
tb->i_x = x;
tb->i_y = y;
tb->eval();
int res = x < y;
if(tb->o_result != res) {
cout << "Failed: Slt\t" << x << " < " << y << " = " << tb->o_result << endl;
cout << "Expected: " << res << endl;
return false;
}
}
cout << "Passed: Slt" << endl;
return true;
}
bool set_less_unsigned(VALU *tb) {
random_device rand;
tb->i_op = 0b011;
for(int i = 0; i < 10000; i++) {
unsigned int x = rand();
unsigned int y = rand();
tb->i_x = x;
tb->i_y = y;
tb->eval();
int res = x < y;
if(tb->o_result != res) {
cout << "Failed: Sltu\t" << x << " < " << y << " = " << tb->o_result << endl;
cout << "Expected: " << res << endl;
return false;
}
}
cout << "Passed: Sltu" << endl;
return true;
}
bool exclusive_or(VALU *tb) {
random_device rand;
tb->i_op = 0b100;
for(int i = 0; i < 10000; i++) {
unsigned int x = rand();
unsigned int y = rand();
tb->i_x = x;
tb->i_y = y;
tb->eval();
int res = x ^ y;
if(tb->o_result != res) {
cout << "Failed: Xor\t" << x << " ^ " << y << " = " << tb->o_result << endl;
cout << "Expected: " << res << endl;
return false;
}
}
cout << "Passed: Xor" << endl;
return true;
}
bool shift_right(VALU *tb) {
random_device rand;
tb->i_op = 0b101;
tb->i_op2 = 0b0;
for(int i = 0; i < 10000; i++) {
unsigned int x = rand();
unsigned int y = rand() % 32;
tb->i_x = x;
tb->i_y = y;
tb->eval();
unsigned int res = x >> y;
if(tb->o_result != res) {
cout << "Failed: Srl\t" << x << " >> " << y << " = " << tb->o_result << endl;
cout << "Expected: " << res << endl;
return false;
}
}
cout << "Passed: Srl" << endl;
tb->i_op2 = 0b1;
for(int i = 0; i < 10000; i++) {
int x = rand();
int y = rand() % 32;
tb->i_x = x;
tb->i_y = y;
tb->eval();
int res = x >> y;
if(tb->o_result != res) {
cout << "Failed: Sra\t" << x << " >> " << y << " = " << tb->o_result << endl;
cout << "Expected: " << res << endl;
return false;
}
}
cout << "Passed: Sra" << endl;
return true;
}
int main(int argc, char **argv) {
Verilated::commandArgs(argc, argv);
VALU tb;
int op;
if(argc == 1) {
op = -1;
} else {
op = stoi(argv[1]);
}
switch(op) {
case 0b000:
add_sub(&tb);
break;
case 0b001:
shift_left(&tb);
break;
case 0b010:
set_less(&tb);
break;
case 0b011:
set_less_unsigned(&tb);
break;
case 0b100:
exclusive_or(&tb);
break;
case 0b101:
shift_right(&tb);
break;
case 0b110:
case 0b111:
default:
add_sub(&tb);
shift_left(&tb);
set_less(&tb);
set_less_unsigned(&tb);
exclusive_or(&tb);
shift_right(&tb);
break;
}
}