-
Notifications
You must be signed in to change notification settings - Fork 1
/
predefined_test.pl
executable file
·278 lines (251 loc) · 5.03 KB
/
predefined_test.pl
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env perl
# MIPS test suite
# We assemble several assembly tests into `instructions.hex` file, which is
# loaded by our Verilog MIPS implementation's simulation. We then run the
# simulation, which on finishing prints the values of $s0 and $s1.
# each function call is a string representing the test name, then a multiline
# string representing the instructions, then the expected values of $s0 and $s1.
# `$0` is always zero, and `$1` (`$at`) is initialized to one.
# Our MIPS ends the simulation once an instruction of `x`s enters the decoding
# stage. So we need to wait four cycles so that all instructions are complete;
# hence the four `nop` at the end of every test.
test("independant add 1", '
add $s0, $0, $1
add $s1, $0, $1
nop
nop
nop
nop
', 1, 1);
test("independant add 2", '
add $s0, $1, $1
add $s0, $s0, $s0
add $s0, $s0, $s0
add $s0, $s0, $s0
add $s1, $s0, $s0
nop
nop
nop
nop
', 16, 32);
test("independant sub", '
sub $s0, $0, $1
sub $s1, $1, $0
nop
nop
nop
nop
', -1, 1);
test("independant sll", '
sll $s0, $1, 1
sll $s1, $1, 12
nop
nop
nop
nop
', 2, 4096);
test("independant and 1", '
and $s0, $0, $1
and $s1, $1, $0
and $s2, $0, $1
nop
nop
nop
nop
', 0, 0);
test("independant and 2", '
and $s0, $0, $0
and $s1, $1, $1
nop
nop
nop
nop
', 0, 1);
test("independant or 1", '
or $s0, $0, $1
or $s1, $1, $0
nop
nop
nop
nop
', 1, 1);
test("independant or 2", '
or $s0, $0, $0
or $s1, $1, $1
nop
nop
nop
nop
', 0, 1);
test("dependant on branch taken", '
add $s0, $1, $0
add $s1, $1, $0
beq $s0, $s1, L
add $s0, $s1, $s0
L: sub $s1, $s1, $s0
nop
nop
nop
nop
', 1, 0);
test("dependant on branch not taken", '
add $s0, $1, $0
add $s1, $1, $1
beq $s0, $s1, L
add $s0, $s1, $s0
L: sub $s1, $s1, $s0
nop
nop
nop
nop
', 3, -1);
test("independant load and store", '
add $s0, $1, $1
add $s1, $0, $1
sw $s0, 1($1)
lw $s1, 1($1)
nop
nop
nop
nop
', 2, 2);
test("dependant sw then lw then sw then lw", '
add $s0, $1, $1
add $s1, $0, $1
sw $s0, 1($1)
lw $s1, 0($s0)
sw $s1, 0($s1)
lw $s0, 0($s1)
add $s0, $s0, $1
nop
nop
nop
nop
', 3, 2);
test("forward A 1", '
add $s0, $0, $1
add $s1, $s0, $1
nop
nop
nop
nop
', 1, 2);
test("forward A 2", '
add $t0, $0, $1
add $s0, $t0, $1
sub $s1, $t0, $1
nop
nop
nop
nop
', 2, 0);
test("forward B", '
add $t0, $0, $1
add $s0, $1, $t0
sub $s1, $1, $t0
nop
nop
nop
nop
', 2, 0);
test("add then sw; and lw then add", '
add $t0, $1, $1
sw $t0, 4($0)
lw $s0, 4($0)
add $s1, $1, $s0
nop
nop
nop
nop
', 2, 3);
test("add then lw then add", '
add $t0, $1, $1
sw $t0, 4($0)
add $t0, $1, $1
lw $s0, 2($t0)
add $s1, $1, $s0
nop
nop
nop
nop
', 2, 3);
test("add then sw then add then lw then add", '
add $t0, $1, $1
sw $t0, 4($0)
add $t0, $1, $t0
lw $s0, 1($t0)
add $s1, $t0, $s0
nop
nop
nop
nop
', 2, 5);
test("beq taken then lw", '
sw $1, 4($0)
beq $1, $1, L
lw $s0, 4($0)
L: nop
nop
nop
nop
', 'x', 'x');
test("beq not taken then lw", '
sw $1, 4($0)
beq $1, $0, L
lw $s0, 4($0)
L: nop
nop
nop
nop
', 1, 'x');
test("beq taken then sw", '
add $s0, $1, $1
beq $1, $1, L
sw $s0, 4($0)
lw $s1, 4($0)
L: nop
nop
nop
nop
', 2, 'x');
test("beq not taken then sw", '
add $s0, $1, $1
beq $1, $0, L
sw $s0, 4($0)
lw $s1, 4($0)
L: nop
nop
nop
nop
', 2, 2);
sub test {
# read function arguments
my ($name, $instructions, $expected_s0, $expected_s1) = @_;
my $asmfile = ".$name.test.asm";
my $hexfile = 'instructions.hex';
# write asm to a file
my $mipsfile = '*.v *-sim.V'; # change this to match all needed files
open ASMFILE, ">$asmfile" or die $!;
print { *ASMFILE } $instructions;
close ASMFILE;
# assemble
system('sh', '-c', "perl mas.pl '$asmfile' >$hexfile");
# compile, simulate, and run
my $result = `sh -c 'iverilog $mipsfile && ./a.out | grep ^T'`;
# check the result; here be dragons
chomp $result;
my ($result_s0, $result_s1) = $result =~ /T:\s*([-0-9Xx]+)\s*([-0-9Xx]+)\s*/;
my $res_eq_exp = (($result_s0 eq $expected_s0) and ($result_s1 eq $expected_s1));
print(($res_eq_exp ? "\e[1;32m" : "\e[1;31m") . "$name\e[m\n");
unless ($res_eq_exp) {
# print "$result\n";
print "RESULTED\tEXPECTED\n--------\t--------\n";
print "$result_s0\t\t$expected_s0\n$result_s1\t\t$expected_s1\n";
print "--------\t--------\n";
}
#print " \e[34$result\e[m\n\e[35m$expected\e[m\n";
# clean
unlink $asmfile;
unlink $hexfile;
unlink 'a.out';
}