1

I am using EDA Playground with Aldec Riviera simulator, and I have this module here:

module alu(input  logic [31:0] a, b,
           input  logic [2:0]  alucontrol,
           output logic [31:0] result,
           output logic        zero);
  
  logic [31:0] condinvb, sum;

  assign condinvb = alucontrol[2] ? ~b : b;
  assign sum = a + condinvb + alucontrol[2];
 
  always_comb
    case (alucontrol[1:0])
      2'b00: result = a & b;
      2'b01: result = a | b;
      2'b10: result = sum;
      2'b11: result = sum[31];
    endcase

  assign zero = (result == 32'b0);
endmodule

and my testbench is here:

module alu_testbench();
  logic [31:0] a,b;
  logic [2:0] alucontrol;
  logic [31:0] result;
  logic zero, clk;
  
  alu test_alu(a,b,alucontrol);
  
  initial begin
    $dumpfile("dump.vcd"); $dumpvars(1);
    clk = 0;
    a = 'hdead0000; b = 'h0000beef; alucontrol = 'b010; #1;
    a = 'hc0debabe; b = 'h0000ffff; alucontrol = 'b000; #1;
    a = 'hc0de0000; b = 'h0000babe; alucontrol = 'b001; #1;
    a = 'hc0debabe; b = 'h0000babe; alucontrol = 'b100; #1;
  end
  
  always begin
    #1; clk = ~clk;
  end
endmodule 

When I run the testbench and look at the generated waveform, I do not see result being updated. Instead, it stays as XXXX_XXXX. What am I doing wrong in the testbench?

toolic
  • 57,801
  • 17
  • 75
  • 117

1 Answers1

2

In the testbench, you declared the result signal, but it is not connected to anything. You probably intended it to be driven by the alu output of the same name. In that case, you should connect it to the instance:

Change:

  alu test_alu(a,b,alucontrol);

to:

  alu test_alu(a,b,alucontrol,result,zero);

Try your code on other simulators on edaplayground; you should get warnings about unconnected ports.

With your code, if you looked inside the alu instance, the result signal would not be X.


Another equivalent way to code the instance is to use connections by name:

alu test_alu (
    .a           (a),
    .b           (b),
    .alucontrol  (alucontrol),
    .result      (result),
    .zero        (zero)
);

This makes it easier to avoid common connection errors.

toolic
  • 57,801
  • 17
  • 75
  • 117