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
|
`timescale 1us/1us
`include "nandgame_types.v"
module alu_tb;
logic [15:0] in1;
logic [15:0] in2;
logic [1:0] opcode;
logic [15:0] result;
logic u, zx, sw;
alu uut (
.X(in1),
.Y(in2),
.opcode(opcode),
.RES(result),
.u(u),
.zx(zx),
.sw(sw)
);
string filename;
initial begin
`ifdef DUMP_FILE_NAME
filename=`DUMP_FILE_NAME;
`else
filename="alu.lxt2";
`endif
$dumpfile(filename); $dumpvars();
end
initial begin
zx = 0;
sw = 0;
// Arith tests
u = 1;
in1 = 13;
in2 = 17;
opcode = ARITH_PLUS;
#1
assert(result == 30)
else $error("Expected: 30, got: %d", result);
#1
in1 = 12;
in2 = 4;
opcode = ARITH_MINUS;
#1
assert(result == 8)
else $error("Expected: 8, got: %d", result);
#1
in1 = 12;
in2 = 13;
opcode = ARITH_MINUS;
#1
assert(result == 16'hffff)
else $error("Expected: 16'hffff, got: %d", result);
#1
in1 = 13;
in2 = 17;
opcode = ARITH_INC;
#1
assert(result == 14)
else $error("Expected: 14, got: %d", result);
#1
in1 = 13;
in2 = 17;
opcode = ARITH_DEC;
#1
assert(result == 12)
else $error("Expected: 12, got: %d", result);
#1
in1 = 0;
in2 = 17;
opcode = ARITH_DEC;
#1
assert(result == 16'hffff)
else $error("Expected: 16'hffff, got: %d", result);
#1
// logic tests
u = 0;
in1 = 16'b1010;
in2 = 16'b1100;
opcode = LOGIC_AND;
#1
assert(result == 16'b1000)
else $error("Expected: 16'b1000, got: %d", result);
#1
in1 = 16'b1010;
in2 = 16'b1100;
opcode = LOGIC_OR;
#1
assert(result == 16'b1110)
else $error("Expected: 16'b1110, got: %d", result);
#1
in1 = 16'b1010;
in2 = 16'b1100;
opcode = LOGIC_XOR;
#1
assert(result == 16'b0110)
else $error("Expected: 16'b0110, got: %d", result);
#1
in1 = 16'b1010101010101010;
opcode = LOGIC_NEGT;
#1
assert(result == 16'b0101010101010101)
else $error("Expected: 16'b0101010101010101, got: %d", result);
#1
// zero - tests
zx = 1;
sw = 0;
// back to logic
u = 0;
in1 = 2;
in2 = 4;
opcode = LOGIC_OR;
#1
assert(result == 4)
else $error("Expected 4, got %d", result);
#1
// back to arith
u = 1;
in1 = 2;
in2 = 4;
opcode = ARITH_PLUS;
#1
assert(result == 4)
else $error("Expected 4, got %d", result);
#1
// swap tests
zx = 0;
sw = 1;
u = 1;
in1 = 2;
in2 = 3;
opcode = ARITH_MINUS;
#1
// 3 - 2
assert(result == 1)
else $error("Expected 1, got %d", result);
// zero-swap
zx = 1;
sw = 1;
u = 1;
in1 = 2;
in2 = 3;
opcode = ARITH_PLUS;
#1
// 2 + 0
assert(result == 2)
else $error("Expected 2, got %d", result);
$finish();
end
endmodule
|