-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_cpu.sv
106 lines (92 loc) · 3.42 KB
/
test_cpu.sv
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
module test_cpu;
timeunit 1ns;
timeprecision 100ps;
import opcodes::*;
logic clk, alu_clk, control_clk, clk2, fetch, halt;
logic rst, load_ir;
opcode_t topcode;
logic [12*8:1] testfile;
logic [31:0] test_number;
logic [3:0] count;
`define PERIOD 10
logic master_clk = 1'b1;
always
#(`PERIOD/2) master_clk = ~master_clk;
always @(posedge master_clk or negedge rst)
if (~rst)
count <= 3'b0;
else
count <= count + 1;
assign control_clk = ~count[0];
assign clk = count[1];
assign fetch = ~count[3];
assign alu_clk = ~(count == 4'hc);
cpu cpu_test (
.halt,
.load_ir,
.clk,
.alu_clk,
.control_clk,
.fetch,
.rst
);
initial
$timeformat ( -9, 1," ns", 12);
initial
forever begin
$display ( "Enter ' deposit test_number # ; run' \n" );
$stop;
if ( test_number > 3 )
begin
$display ( "Test number %d is invalid", test_number );
end
else
begin
case ( test_number )
1: begin
$display ( "CPUtest1 - BASIC CPU DIAGNOSTIC PROGRAM \n" );
$display ( "THIS TEST SHOULD HALT WITH THE PC AT 17 hex\n" );
end
2: begin
$display ( "CPUtest2 - ADVANCED CPU DIAGNOSTIC PROGRAM\n" );
$display ( "THIS TEST SHOULD HALT WITH THE PC AT 10 hex\n" );
end
3: begin
$display ( "CPUtest3 - FIBONACCI NUMBERS to 144\n" );
$display ( "THIS TEST SHOULD HALT WITH THE PC AT 0C hex\n" );
end
endcase
testfile = { "CPUtest", 8'h30+test_number[7:0], ".dat" };
$readmemb (testfile, cpu_test.memory1.memory);
rst_ = 1;
repeat (2) @(negedge master_clk);
rst_ = 0;
repeat (2) @(negedge master_clk);
rst_ = 1;
$display(" TIME PC INSTR OP ADR DATA\n");
$display(" ---------- -- ----- -- --- ----\n");
while (!halt)
@( posedge clk )
if (load_ir)
begin
#(`PERIOD/2)
topcode = cpu_test.opcode;
$display ( "%t %h %s %h %h %h %h",
$time,cpu_test.pc_out, topcode.name(), cpu_test.opcode,
cpu_test.mux_addr, cpu_test.alu_out, cpu_test.data);
if ((test_number == 3) && (topcode == JMP))
$display ( "Next Fibonacci number is %d",
cpu_test.memory1.memory[5'h1B] );
end
if ( test_number == 1 && cpu_test.pc_out !== 5'h17
|| test_number == 2 && cpu_test.pc_out !== 5'h10
|| test_number == 3 && cpu_test.pc_out !== 5'h0C
|| cpu_test.pc_out === 5'hXX )
begin
$display ( "CPU TEST %0d FAILED with pc_out at %h",test_number , cpu_test.pc_out);
$finish;
end
$display ( "\nCPU TEST %0d PASSED",test_number);
end
end
endmodule