-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject-phase1-testbench.v.bak
192 lines (138 loc) · 5.73 KB
/
project-phase1-testbench.v.bak
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
module cpu_tb();
wire [15:0] PC;
wire [15:0] Inst; /* This should be the 15 bits of the FF that
stores instructions fetched from instruction memory
*/
wire RegWrite; /* Whether register file is being written to */
wire [3:0] WriteRegister; /* What register is written */
wire [15:0] WriteData; /* Data */
wire MemWrite; /* Similar as above but for memory */
wire MemRead;
wire [15:0] MemAddress;
wire [15:0] MemData, MemDataRead;
wire Halt; /* Halt executed and in Memory or writeback stage */
integer inst_count;
integer cycle_count;
integer trace_file;
integer sim_log_file;
reg clk; /* Clock input */
reg rst_n; /* (Active low) Reset input */
cpu DUT(.clk(clk), .rst_n(rst_n), .pc(PC), .hlt(Halt)); /* Instantiate your processor */
/* Setup */
initial begin
$display("Hello world...simulation starting");
$display("See verilogsim.log and verilogsim.trace for output");
inst_count = 0;
trace_file = $fopen("verilogsim.trace");
sim_log_file = $fopen("verilogsim.log");
end
/* Clock and Reset */
// Clock period is 100 time units, and reset length
// to 201 time units (two rising edges of clock).
initial begin
$dumpvars;
cycle_count = 0;
rst_n = 0; /* Intial reset state */
clk = 1;
#201 rst_n = 1; // delay until slightly after two clock periods
end
always #50 begin // delay 1/2 clock period each time thru loop
clk = ~clk;
end
always @(posedge clk) begin
cycle_count = cycle_count + 1;
if (cycle_count > 100000) begin
$display("hmm....more than 100000 cycles of simulation...error?\n");
$finish;
end
end
/* Stats */
always @ (posedge clk) begin
if (rst_n) begin
if (Halt || RegWrite || MemWrite) begin
inst_count = inst_count + 1;
end
$fdisplay(sim_log_file, "SIMLOG:: Cycle %d PC: %8x I: %8x R: %d %3d %8x M: %d %d %8x %8x",
cycle_count,
PC,
Inst,
RegWrite,
WriteRegister,
WriteData,
MemRead,
MemWrite,
MemAddress,
MemData);
if (RegWrite) begin
if (MemRead) begin
// ld
$fdisplay(trace_file,"INUM: %8d PC: 0x%04x REG: %d VALUE: 0x%04x ADDR: 0x%04x",
(inst_count-1),
PC,
WriteRegister,
WriteData,
MemAddress);
end else begin
$fdisplay(trace_file,"INUM: %8d PC: 0x%04x REG: %d VALUE: 0x%04x",
(inst_count-1),
PC,
WriteRegister,
WriteData );
end
end else if (Halt) begin
$fdisplay(sim_log_file, "SIMLOG:: Processor halted\n");
$fdisplay(sim_log_file, "SIMLOG:: sim_cycles %d\n", cycle_count);
$fdisplay(sim_log_file, "SIMLOG:: inst_count %d\n", inst_count);
$fdisplay(trace_file, "INUM: %8d PC: 0x%04x",
(inst_count-1),
PC );
$fclose(trace_file);
$fclose(sim_log_file);
$finish;
end else begin
if (MemWrite) begin
// st
$fdisplay(trace_file,"INUM: %8d PC: 0x%04x ADDR: 0x%04x VALUE: 0x%04x",
(inst_count-1),
PC,
MemAddress,
MemData);
end else begin
// conditional branch or NOP
// Need better checking in pipelined testbench
inst_count = inst_count + 1;
$fdisplay(trace_file, "INUM: %8d PC: 0x%04x",
(inst_count-1),
PC );
end
end
end
end
/* Assign internal signals to top level wires
The internal module names and signal names will vary depending
on your naming convention and your design */
// Edit the example below. You must change the signal
// names on the right hand side
// assign PC = DUT.fetch0.pcCurrent; //You won't need this because it's part of the main cpu interface
assign Inst = DUT.fetch0.instruction;
assign RegWrite = DUT.regwrite;
// Is memory being read, one bit signal (1 means yes, 0 means no)
assign WriteRegister = DUT.writereg;
// The name of the register being written to. (4 bit signal)
assign WriteData = DUT.output_value;
// Data being written to the register. (16 bits)
assign MemRead = DUT.memread;
// Is memory being read, one bit signal (1 means yes, 0 means no)
assign MemWrite = (DUT.memwrite);
// Is memory being written to (1 bit signal)
assign MemAddress = DUT.alu_out;
// Address to access memory with (for both reads and writes to memory, 16 bits)
assign MemData = DUT.register2;
// Data to be written to memory for memory writes (16 bits)
assign MemData = DUT.register2;
assign MemDataRead = DUT.mem_out;
// assign Halt = DUT.memory0.halt; //You won't need this because it's part of the main cpu interface
// Is processor halted (1 bit signal)
/* Add anything else you want here */
$display("\nCompleted Successfully\n");
endmodule