-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalu_tb.cpp
223 lines (166 loc) · 4.34 KB
/
alu_tb.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Verifies the results of the ALU and exits with a 0 on success
* Author: Noam Weitzman <nw521@ic.ac.uk>
*/
#include "base_testbench.h"
#define NAME "alu"
#define OPCODE_ADD 0b0000
#define OPCODE_SUB 0b0001
#define OPCODE_AND 0b0010
#define OPCODE_OR 0b0011
#define OPCODE_XOR 0b0100
#define OPCODE_LSL 0b0101
#define OPCODE_LSR 0b0110
#define OPCODE_ASR 0b0111
#define OPCODE_SLT 0b1000
#define OPCODE_SLTU 0b1001
#define OPCODE_B 0b1010
class ALUTestbench : public BaseTestbench
{
protected:
void initializeInputs() override
{
top->a = 0;
top->b = 0;
top->ALUctrl = 0;
// output logic EQ,
// output logic [WIDTH - 1:0] ALUout
}
};
TEST_F(ALUTestbench, AdditionTest)
{
int op1 = 0x55555555;
int op2 = 0xAAAAAAAA;
//inputs for addition operation
top->a = op1;
top->b = op2;
top->ALUctrl = OPCODE_ADD;
top->eval();
//check the ALU output and EQ signal for addition
EXPECT_EQ(top->ALUout, op1 + op2);
EXPECT_EQ(top->EQ, op1 + op2 == 0);
}
TEST_F(ALUTestbench, SubtractionTest)
{
int op1 = 0xAAAAAAAA;
int op2 = 0x55555555;
//inputs for subtraction operation
top->a = op1;
top->b = op2;
top->ALUctrl = OPCODE_SUB;
top->eval();
//check the ALU output and EQ signal for subtraction
EXPECT_EQ(top->ALUout, op1 - op2);
EXPECT_EQ(top->EQ, op1 - op2 == 0);
}
TEST_F(ALUTestbench, BinaryAndTest)
{
int op1 = 0b0110;
int op2 = 0b0101;
//inputs for binary AND operation
top->a = op1;
top->b = op2;
top->ALUctrl = OPCODE_AND;
top->eval();
//check the ALU output and EQ signal for AND
EXPECT_EQ(top->ALUout, op1 & op2);
EXPECT_EQ(top->EQ, (op1 & op2) == 0);
}
TEST_F(ALUTestbench, BinaryOrTest)
{
int op1 = 0b0110;
int op2 = 0b0101;
//inputs for binary OR operation
top->a = op1;
top->b = op2;
top->ALUctrl = OPCODE_OR;
top->eval();
//check the ALU output and EQ signal for OR
EXPECT_EQ(top->ALUout, op1 | op2);
EXPECT_EQ(top->EQ, (op1 | op2) == 0);
}
TEST_F(ALUTestbench, BinaryXorTest)
{
int op1 = 0b0110;
int op2 = 0b0101;
//inputs for binary XOR operation
top->a = op1;
top->b = op2;
top->ALUctrl = OPCODE_XOR;
top->eval();
//check the ALU output and EQ signal for XOR
EXPECT_EQ(top->ALUout, op1 ^ op2);
EXPECT_EQ(top->EQ, (op1 ^ op2) == 0);
}
TEST_F(ALUTestbench, LogicalShiftLeftTest)
{
int op1 = 0b0110;
int op2 = 4;
//inputs for LSL operation
top->a = op1;
top->b = op2;
top->ALUctrl = OPCODE_LSL;
top->eval();
//check the ALU output and EQ signal for LSL
EXPECT_EQ(top->ALUout, op1 << op2);
EXPECT_EQ(top->EQ, (op1 << op2) == 0);
}
TEST_F(ALUTestbench, LogicalShiftRightTest)
{
unsigned int op1 = 0b0110;
unsigned int op2 = 4;
//inputs for LSR operation
top->a = op1;
top->b = op2;
top->ALUctrl = OPCODE_LSR;
top->eval();
//check the ALU output and EQ signal for LSR
EXPECT_EQ(top->ALUout, op1 >> op2);
EXPECT_EQ(top->EQ, (op1>> op2) == 0);
}
TEST_F(ALUTestbench, ASRTest)
{
int op1 = 0xffffd8f1;
int op2 = 0x5;
top->a = op1;
top->b = op2;
top->ALUctrl = OPCODE_ASR;
top->eval();
EXPECT_EQ((int)top->ALUout, op1 >> op2);
EXPECT_EQ(top->EQ, op2 == 0);
}
TEST_F(ALUTestbench, SetIfLessThanTest)
{
int op1 = 0b0110;
int op2 = 0b0101;
//inputs for binary SLT operation
top->a = op1;
top->b = op2;
top->ALUctrl = OPCODE_SLT;
top->eval();
//check the ALU output and EQ signal for SLT
EXPECT_EQ(top->ALUout, (op1 < op2) ? 1 : 0);
EXPECT_EQ(top->EQ, (op1 < op2) == 0);
}
TEST_F(ALUTestbench, LoadUpperImmTest)
{
int op1 = 0b0110;
int op2 = 0b1101;
//inputs for LUI operation
top->a = op1;
top->b = op2;
top->ALUctrl = OPCODE_B;
top->eval();
//check the ALU output and EQ signal for LUI
EXPECT_EQ(top->ALUout, op2);
EXPECT_EQ(top->EQ, op2 == 0);
}
int main(int argc, char **argv)
{
Verilated::commandArgs(argc, argv);
testing::InitGoogleTest(&argc, argv);
Verilated::mkdir("logs");
auto res = RUN_ALL_TESTS();
VerilatedCov::write(("logs/coverage_" + std::string(NAME) + ".dat").c_str());
return res;
}