-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS18_cpu_testbench_phase3.v
218 lines (155 loc) · 6.52 KB
/
S18_cpu_testbench_phase3.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
module cpu_ptb();
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] MemDataIn; /* Read from Memory */
wire [15:0] MemDataOut; /* Written to Memory */
wire DCacheHit;
wire ICacheHit;
wire DCacheReq;
wire ICacheReq;
wire Halt; /* Halt executed and in Memory or writeback stage */
integer inst_count;
integer cycle_count;
integer trace_file;
integer sim_log_file;
integer DCacheHit_count;
integer ICacheHit_count;
integer DCacheReq_count;
integer ICacheReq_count;
reg clk; /* Clock input */
reg rst_n; /* (Active low) Reset input */
cpu DUT(.clk(clk), .rst_n(rst_n), .pc_out(PC), .hlt(Halt)); /* Instantiate your processor */
/* Setup */
initial begin
$display("Hello world...simulation starting");
$display("See verilogsim.plog and verilogsim.ptrace for output");
inst_count = 0;
DCacheHit_count = 0;
ICacheHit_count = 0;
DCacheReq_count = 0;
ICacheReq_count = 0;
trace_file = $fopen("verilogsim.ptrace");
sim_log_file = $fopen("verilogsim.plog");
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
if (DCacheHit) begin
DCacheHit_count = DCacheHit_count + 1;
end
if (ICacheHit) begin
ICacheHit_count = ICacheHit_count + 1;
end
if (DCacheReq) begin
DCacheReq_count = DCacheReq_count + 1;
end
if (ICacheReq) begin
ICacheReq_count = ICacheReq_count + 1;
end
$fdisplay(sim_log_file, "SIMLOG:: Cycle %d PC: %8x I: %8x R: %d %3d %8x M: %d %d %8x %8x %8x",
cycle_count,
PC,
Inst,
RegWrite,
WriteRegister,
WriteData,
MemRead,
MemWrite,
MemAddress,
MemDataIn,
MemDataOut);
if (RegWrite) begin
$fdisplay(trace_file,"REG: %d VALUE: 0x%04x",
WriteRegister,
WriteData );
end
if (MemRead) begin
$fdisplay(trace_file,"LOAD: ADDR: 0x%04x VALUE: 0x%04x",
MemAddress, MemDataOut );
end
if (MemWrite) begin
$fdisplay(trace_file,"STORE: ADDR: 0x%04x VALUE: 0x%04x",
MemAddress, MemDataIn );
end
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(sim_log_file, "SIMLOG:: dcachehit_count %d\n", DCacheHit_count);
$fdisplay(sim_log_file, "SIMLOG:: icachehit_count %d\n", ICacheHit_count);
$fdisplay(sim_log_file, "SIMLOG:: dcachereq_count %d\n", DCacheReq_count);
$fdisplay(sim_log_file, "SIMLOG:: icachereq_count %d\n", ICacheReq_count);
$fclose(trace_file);
$fclose(sim_log_file);
#5;
$finish;
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 Halt = DUT.memory0.halt; //You won't need this because it's part of the main cpu interface
// Is processor halted (1 bit signal)
assign Inst = DUT.p0.instr;
//Instruction fetched in the current cycle
assign RegWrite = DUT.p0.regWrite;
// Is register file being written to in this cycle, one bit signal (1 means yes, 0 means no)
assign WriteRegister = DUT.p0.DstwithJmout;
// If above is true, this should hold the name of the register being written to. (4 bit signal)
assign WriteData = DUT.p0.wData;
// If above is true, this should hold the Data being written to the register. (16 bits)
assign MemRead = (DUT.p0.memRxout & ~DUT.p0.notdonem);
// Is memory being read from, in this cycle. one bit signal (1 means yes, 0 means no)
assign MemWrite = (DUT.p0.memWxout & ~DUT.p0.notdonem);
// Is memory being written to, in this cycle (1 bit signal)
assign MemAddress = DUT.p0.data1out;
// If there's a memory access this cycle, this should hold the address to access memory with (for both reads and writes to memory, 16 bits)
assign MemDataIn = DUT.p0.data2out;
// If there's a memory write in this cycle, this is the Data being written to memory (16 bits)
assign MemDataOut = DUT.p0.readData;
// If there's a memory read in this cycle, this is the data being read out of memory (16 bits)
assign ICacheReq = DUT.p0.icr;
// Signal indicating a valid instruction read request to cache
assign ICacheHit = DUT.p0.ich;
// Signal indicating a valid instruction cache hit
assign DCacheReq = DUT.p0.dcr;
// Signal indicating a valid instruction data read or write request to cache
assign DCacheHit = DUT.p0.dch;
// Signal indicating a valid data cache hit
/* Add anything else you want here */
endmodule