risc-processor-211 / CPEN Lab 6 / alu_tb.v
alu_tb.v
Raw
module ALU_tb();

reg err;
reg clk;
reg [15:0] Ain, Bin;
reg [1:0] ALUop;
wire [15:0] out;
wire [2:0] Z;

ALU DUT(Ain, Bin, ALUop, out, Z);

task alucheck;
    input [15:0] expected_out;
    input [2:0] expected_Z;
begin
    if(ALU_tb.DUT.out !== expected_out) begin
        $display("ERROR: output is %b, expected %b",
        ALU_tb.DUT.out, expected_out);
        err = 1'b1; end

    if(ALU_tb.DUT.Z !== expected_Z) begin
        $display("ERROR: Z is %b, expected %b",
        ALU_tb.DUT.Z, expected_Z);
        err = 1'b1; end
end 
endtask

initial begin
    clk = 1'b0; #5;
    forever begin
        clk = 1'b1; #5;
        clk = 1'b0; #5;
    end
end

initial begin
    $display("Checking addition of 6+3");
    Ain = 16'b0000000000000110;  
    Bin = 16'b0000000000000011;
    ALUop = 2'b00; err = 1'b0; #10
    alucheck(16'b0000000000001001, 3'b000);

    $display("Checking subtraction of 7-1");
    Ain = 16'b0000000000000111;  
    Bin = 16'b0000000000000001;
    ALUop = 2'b01; #10
    alucheck(16'b0000000000000110, 3'b000);

    $display("Checking subtraction of 8-8");
    Ain = 16'b0000000000001000;  
    Bin = 16'b0000000000001000;
    ALUop = 2'b01; #10
    alucheck(16'b0000000000000000, 3'b001); //zero flag

    $display("Checking anding of inputs 5 and 3");
    Ain = 16'b0000000000000101;  
    Bin = 16'b0000000000000011;
    ALUop = 2'b10; #10
    alucheck(16'b0000000000000001, 3'b000); 

    $display("Checking the inverse of input 1"); 
    Ain = 16'b0100000000000000;
    Bin = 16'b0100000000000001;
    ALUop = 2'b11; #10
    alucheck(16'b1011111111111110, 3'b100); //overflow and negative flag

    if(~err) $display("PASSED");
    else $display("FAILED");
    $stop;
#450;

end
endmodule