Search code examples
system-verilogmodelsim

Connections between sub modules wrong


My codes for Alu and Mux:

module alu(input logic [31:0]srca, srcb,
          input logic [2:0] alucontrol,
          output logic zero,
          output logic [31:0]aluout);

logic [31:0] addr, subr, sltr, Andr, Orr;
assign addr = srca + srcb;
assign subr = srca - srcb;
assign sltr = (srca < srcb) ? 1 : 0;
assign Andr = srca & srcb;
assign Orr = srca | srcb;
always_comb 
begin
case(alucontrol)
    3'b010: aluout = addr;
    3'b110: aluout = subr;
    3'b111: aluout = sltr;
    3'b000: aluout = Andr;
    3'b001: aluout = Orr;
    default: aluout = 32'bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;
endcase
zero = (subr == 0) ?  1 : 0;
end
endmodule


module mux2#(parameter WIDTH = 8)
                (input logic [WIDTH-1:0] d0, d1,
                 input logic                 s,
                 output logic [WIDTH-1:0] y);
always_comb
begin
y = s?d1 : d0;
end
endmodule

and their instantiation under the same top module:

alu         alu(srca, srcb, alucontrol, aluout, zero);

mux2 #(32) resmux(aluout, readdata, memtoreg, result);

When I try to connect my 2-1Mux resmux with my alu, the aluout doesn't get connected to resmux

aluout gets suspended

I could solve this by exchanging the order of aluout and zero, but could anybody explain why this happens and how to avoid? Thanks a lot!


Solution

  • There are a number of mechanisms for connecting ports in SystemVerilog. The mechanism you are using is positional. That means each signal connection has to go in the prescribed order that they appear in the module declaration of alu. The way your code is written, the signal zero in module top is connected to last port aluout declared in module alu.

    SystemVerilog also has a by_name syntax .portname(signal). You can list all your port connections in any order that way.

    alu   alu(.srca(srca), .srcb(srcb), .alucontrol(alucontrol), .alu(aluout), .zero(zero));
    

    When the signal name you are connecting matches the declared port name, you can just use .portname.

    alu alu(.alucontrol, .aluout, .zero, .srca, .srcb);
    

    And finally, if all the port names match the signal names, you can use a wildcard, .* as well as list exceptions explicitly

    alu alu(.*, .zero(my_zero));