-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cpu.sv
128 lines (108 loc) · 3.91 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
`timescale 1 ns / 1 ps
module test_cpu;
parameter ADDR_WIDTH = 14;
parameter DATA_WIDTH = 16;
reg osc;
localparam period = 10;
wire clk;
assign clk = osc;
reg cs;
reg we;
reg oe;
integer i;
reg [ADDR_WIDTH-1:0] MAR;
wire [DATA_WIDTH-1:0] data;
reg [DATA_WIDTH-1:0] testbench_data;
assign data = !oe ? testbench_data : 'hz;
single_port_sync_ram_large #(.DATA_WIDTH(DATA_WIDTH)) ram
( .clk(clk),
.addr(MAR),
.data(data[DATA_WIDTH-1:0]),
.cs_input(cs),
.we(we),
.oe(oe)
);
reg [15:0] A;
reg [15:0] B;
reg [15:0] ALU_Out;
reg [1:0] ALU_Sel;
alu alu16(
.A(A),
.B(B), // ALU 16-bit Inputs
.ALU_Sel(ALU_Sel),// ALU Selection
.ALU_Out(ALU_Out) // ALU 16-bit Output
);
reg [15:0] PC = 'h100;
reg [15:0] IR = 'h0;
reg [15:0] MBR = 'h0;
reg [15:0] AC = 'h0;
initial osc = 1; //init clk = 1 for positive-edge triggered
always begin // Clock wave
#period osc = ~osc;
end
initial begin
$dumpfile("dump.vcd");
$dumpvars;
// Multiplication by addition program
@(posedge clk) MAR <= 'h100; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h110C;
@(posedge clk) MAR <= 'h101; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h210E;
@(posedge clk) MAR <= 'h102; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h110D;
@(posedge clk) MAR <= 'h103; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h310B;
@(posedge clk) MAR <= 'h104; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h210D;
@(posedge clk) MAR <= 'h105; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h110E;
@(posedge clk) MAR <= 'h106; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h310F;
@(posedge clk) MAR <= 'h107; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h210E;
@(posedge clk) MAR <= 'h108; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h8400;
@(posedge clk) MAR <= 'h109; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h9102;
@(posedge clk) MAR <= 'h10A; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h7000;
@(posedge clk) MAR <= 'h10B; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h0005;
@(posedge clk) MAR <= 'h10C; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h0007;
@(posedge clk) MAR <= 'h10D; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h0000;
@(posedge clk) MAR <= 'h10E; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'h0000;
@(posedge clk) MAR <= 'h10F; we <= 1; cs <= 1; oe <= 0; testbench_data <= 'hFFFF;
@(posedge clk) PC <= 'h100;
for (i = 0; i < 62; i = i+1) begin
// Fetch
@(posedge clk) MAR <= PC; we <= 0; cs <= 1; oe <= 1;
@(posedge clk) IR <= data;
@(posedge clk) PC <= PC + 1;
// Decode and execute
case(IR[15:12])
4'b0001: begin
@(posedge clk) MAR <= IR[11:0];
@(posedge clk) MBR <= data;
@(posedge clk) AC <= MBR;
end
4'b0010: begin
@(posedge clk) MAR <= IR[11:0];
@(posedge clk) MBR <= AC;
@(posedge clk) we <= 1; oe <= 0; testbench_data <= MBR;
end
4'b0011: begin
@(posedge clk) MAR <= IR[11:0];
@(posedge clk) MBR <= data;
@(posedge clk) ALU_Sel <= 'b01; A <= AC; B <= MBR;
@(posedge clk) AC <= ALU_Out;
end
4'b0111: begin
@(posedge clk) PC <= PC - 1;
end
4'b1000: begin
@(posedge clk)
if(IR[11:10]==2'b01 && AC == 0) PC <= PC + 1;
else if(IR[11:10]==2'b00 && AC < 0) PC <= PC + 1;
else if(IR[11:10]==2'b10 && AC > 0) PC <= PC + 1;
end
4'b1001: begin
@(posedge clk) PC <= IR[11:0];
end
4'b1010: begin
@(posedge clk) AC <= 0;
end
endcase
end
@(posedge clk) MAR <= 'h10D; we <= 0; cs <= 1; oe <= 1;
@(posedge clk)
#20 $finish;
end
endmodule