-
Notifications
You must be signed in to change notification settings - Fork 0
/
tb.v
executable file
·202 lines (169 loc) · 4.81 KB
/
tb.v
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
`timescale 1 ns/10 ps
`define CYCLE 11
`define SDFFILE "./SingleCycleMIPS_syn.sdf" // Modify your sdf file name
`include "HSs18n_128x32.v"
`define IGLD_INIT "golden/inst_golden.txt"
`ifdef Baseline
`define IMEM_INIT "inst/inst.txt"
`define DMEM_INIT "data/mem_data.txt"
`define GOLD_INIT "golden/golden.txt"
`endif
`ifdef FPU
`define DMEM_INIT "data/fp_mem_data.txt"
`ifdef Single
`define IMEM_INIT "inst/fp_inst.txt"
`define GOLD_INIT "golden/fp_golden.txt"
`endif
`ifdef Double
`define IMEM_INIT "inst/fp_inst_double.txt"
`define GOLD_INIT "golden/fp_golden_double.txt"
`endif
`endif
module SingleCycle_tb;
reg clk;
reg rst_n;
wire [31:0] IR_addr;
wire [31:0] IR;
wire [31:0] ReadDataMem;
wire CEN;
wire WEN;
wire [6:0] A;
wire [31:0] Data2Mem;
wire OEN;
reg [31:0] ans[0:100];
reg [7:0] inst_ans[0:2700];
integer error;
integer i, check_count;
// Instruction memory
ROM128x32 i_rom(
.addr(IR_addr[8:2]),
.data(IR)
);
SingleCycleMIPS SingleCycleMIPS(
clk,
rst_n,
IR_addr,
IR,
ReadDataMem,
CEN,
WEN,
A,
Data2Mem,
OEN
);
HSs18n_128x32 Data_memory(
ReadDataMem,
~clk,
CEN,
WEN,
A,
Data2Mem,
OEN
);
`ifdef SDF
initial $sdf_annotate(`SDFFILE, SingleCycleMIPS);
`endif
// Initialize the data memory
initial begin
$readmemh (`DMEM_INIT, Data_memory.mem);
$readmemh (`GOLD_INIT, ans);
$readmemb (`IGLD_INIT, inst_ans);
end
// Dump waveform file
initial begin
// $fsdbDumpfile("MIPS.fsdb");
// $fsdbDumpvars(0,SingleCycle_tb,"+mda"); //This command is for dumping 2D array
// $fsdbDumpvars;
$dumpfile("MIPS.vcd");
// $dumpvars(0, SingleCycle_tb, "+mda");
$dumpvars;
end
//initial rst_n, value
initial begin
clk = 0;
error = 0;
i = 0;
rst_n = 1'b1;
#(`CYCLE*0.2) rst_n = 1'b0;
#(`CYCLE*2) rst_n = 1'b1;
end
// clk generation
always begin
#(`CYCLE*0.5) clk = ~clk;
end
// check correctness
`ifdef Baseline
always@(negedge clk)begin
if (IR_addr >= 8'd4 && rst_n == 1)begin
if (IR_addr[7:0] !== inst_ans[i])
error = 1;
i <= i+1;
end
end
`endif
always@(*)begin
if (ReadDataMem == 32'h7fffffff && IR_addr == 8'd8)begin
if (error==1)
$display("\norder of Single cycle MIPS instructions error!!!\n");
`ifdef SDF
$display("using SDF File %s for this simulation.", `SDFFILE);
`endif
for (i=1; i<=ans[0]; i=i+1)begin
if (Data_memory.mem[i] !== ans[i])begin
$display("\nerror in memory%d, expected: %h, your: %h", i, ans[i], Data_memory.mem[i]);
error = error + 1;
end
end
$display(error);
if (error == 0)begin
$display("=======================The test result is ..... PASS=========================");
$display("\n");
`ifdef FPU
$display(" FPU Version \n");
`endif
$display(" ***************************************************** ");
$display(" ** ** /|__/|");
$display(" ** Congratulations !! ** / O,O \\");
$display(" ** ** /_____ \\");
$display(" ** All instructions have been done successfully! ** /^ ^ ^ \\ |");
$display(" ** ** |^ ^ ^ ^ |w|");
$display(" ***************************************************** \\m___m__|_|");
$display("\n");
$display("============================================================================");
end
else begin
$display("-----------------------------------------------------\n");
$display(" Error!!! \n");
$display(" There are %d error with your code! \n", error);
$display("--------The test result is .....FAIL ----------------\n");
$display("-----------------------------------------------------\n");
end
$finish;
end
end
initial begin
#(`CYCLE*5000)
$display("-----------------------------------------------------\n");
$display(" Error!!! Running out of time! \n");
$display(" There is something wrong with your code! \n");
$display("--------The test result is .....FAIL ----------------\n");
$display("-----------------------------------------------------\n");
$finish;
end
endmodule
module ROM128x32 (
addr,
data
);
input [6:0] addr;
output [31:0] data;
reg [31:0] data;
reg [31:0] mem [0:127];
integer i;
initial begin
// Initialize the instruction memory
$readmemh (`IMEM_INIT, mem);
$display("Reading instruction memory......");
end
always @(addr) data = mem[addr];
endmodule