Question: testbench.sv module testCPU; reg clk , reset; wire [ 7 : 0 ] DMem; int errors = 0 ; reg [ 7 : 0 ]

testbench.sv
module testCPU;
reg clk, reset;
wire [7:0] DMem;
int errors =0;
reg [7:0] DMem_expected[4:0];
integer i, j;
top_CPU CPU0(
.D_rd_data(DMem),
.clk(clk),
.reset(reset)
);
always #5 clk = ~clk;
initial begin
$dumpfile("dump.vcd");
$dumpvars(2);
$readmemb("Dmem_expected.tv", DMem_expected, 0,4);
i =0;
j =0;
clk =1'b0;
reset =1'b1;
$display("Reset");
#26 reset =0;
end
always @(negedge clk) begin
if (!reset) begin
if ((i >7) && (i <=12)) begin
if (DMem !== DMem_expected[j]) begin
$display("# ERROR: i=%0d, j=%0d, DMem=%0d, DMem_expected=%0d", i, j, DMem, DMem_expected[j]);
errors = errors +1;
end else begin
$display("# CORRECT: i=%0d, j=%0d, DMem=%0d, DMem_expected=%0d", i, j, DMem, DMem_expected[j]);
end
j = j +1;
end
if (i >12) begin
$display("# of Errors =%d", errors);
$finish;
end
i = i +1;
end
end
endmodule
Dmem_expected.tv
0000_0001
0000_0001
0000_0010
0000_0011
0000_0101
design.sv
`include "controller.sv"
`include "registerfile.sv"
`include "ALU.sv"
`include "memories.sv"
`include "parameterized_mux.sv"
module top_CPU(
input clk,
input reset,
output [7:0] D_rd_data
);
wire RF_W_we, DMEM_we;
wire [1:0] ALUcontrol, Mux_M0, Mux_M1;
wire [11:0] instruction;
wire [7:0] alu_result, A, B, DMem;
wire [5:0] address;
// Instantiate the controller
controller ctrl(
.opcode(instruction[11:9]),// Opcode bits
.RF_W_we(RF_W_we),
.DMEM_we(DMEM_we),
.ALUcontrol(ALUcontrol),
.Mux_M0(Mux_M0),
.Mux_M1(Mux_M1)
);
// Instantiate the ALU
ALU alu(
.Result(alu_result),
.ALUcontrol(ALUcontrol),
.A(A),// Operand A
.B(B)// Operand B
);
// Instantiate the register file
register_file rf(
.clk(clk),
.reset(reset),
.R1_addr(instruction[8:6]),// Source register 1
.R2_addr(instruction[5:3]),// Source register 2
.W_addr(instruction[2:0]),// Destination register
.W_data(alu_result),// Data to write
.W_we(RF_W_we),// Write enable
.R1_data(A),// Output to ALU A
.R2_data(B)// Output to ALU B
);
// Instantiate data memory
dmem memory(
.clk(clk),
.we(DMEM_we),
.addr(address),
.wr_data(B),// Data to write comes from register file (B)
.rd_data(DMem)// Data read from memory
);
// Instruction memory
imem instruction_memory(
.addr(address[3:0]),// Instruction address
.rd_data(instruction)// Instruction output
);
// Multiplexer to select data from ALU or memory
Mux2x1 #(.N(8)) mux1(
.in0(alu_result),// ALU result
.in1(DMem),// Data from memory
.sel(Mux_M1[0]),// Select signal
.out(D_rd_data)// Output
);
// Generate address
assign address = instruction[5:0]; // Use instruction address bits
endmodule
register.sv
module register_file (
input wire clk,
input wire reset,
input wire [2:0] R1_addr,
output reg [7:0] R1_data,
input wire [2:0] R2_addr,
output reg [7:0] R2_data,
input wire [2:0] W_addr,
input wire [7:0] W_data,
input wire W_we
);
reg [7:0] rf [7:0]; //8 registers, each 8 bits wide
// Read ports
always @(*) begin
R1_data = rf[R1_addr];
R2_data = rf[R2_addr];
$display("# DEBUG: Reading R1_data=%0d, R2_data=%0d", R1_data, R2_data);
end
// Write port
always @(posedge clk or posedge reset) begin
if (reset) begin
integer i;
for (i =0; i <8; i = i +1) begin
rf[i]<=8'b0;
end
$display("# DEBUG: Resetting register file.");
end else if (W_we) begin
rf[W_addr]<= W_data;
$display("# DEBUG: Writing %0d to rf[%0d]", W_data, W_addr);
end
end
endmodule
parameterized_mux.sv
module Mux2x1 #(parameter N =8)(
input [N-1:0] in1,
input [N-1:0] in0,
input sel,
output [N-1:0] out
);
assign out = sel ? in1 : in0; // Select between in1 and in0
endmodule
Please help me to all of this code have the correct ouput like this output:Reset
# CORRECT: i=8, j=0, DMem=1, DMem_expected=1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!