I understand when using case syntax in systemverilog, we need to fully describe all combinations or add a default to avoid latches.
Here is my example code, no latches are generated:
module test(
input logic[2:0] op,
output logic a,b,c
);
always_comb
begin
case(op)
0: {a,b,c} = {1'b1,1'b1,1'b0};
1: {a,b,c} = {1'b1,1'b0,1'b0};
2: {a,b,c} = {1'b0,1'b1,1'b0};
default: {a,b,c} = {1'b0,1'b0,1'b0};
endcase
end
endmodule
As I said in the beginning, if add a default, not latches are generated. Please look the second code, which is a ALU design:
module ALU(
output logic[31:0] Result,
output logic Zero, Overflow, Negative, Carryout,
input logic [5:0]ALUOp_i,
input logic [31:0] ALU_A_i, ALU_B_i,
input logic [4:0] Shamt
);
logic [31:0] adder_b;
always_comb
begin
casez(ALUOp_i)
/*Add_trap*/ 0,1: {Carryout,Result} = {ALU_A_i[31],ALU_A_i} + {ALU_B_i[31],ALU_B_i};
/*Add_notrap*/
/*Subtrap*/ 2,3:
/*Sub_notrap*/ begin
adder_b = ALU_B_i ^ {32{1'b1}};
{Carryout,Result} = {ALU_A_i[31],ALU_A_i} + {adder_b[31],adder_b} + 1;
end
/*SLL*/ 8: Result = ALU_B_i << Shamt;
/*SLLV*/ 9: Result = ALU_B_i << ALU_A_i;
/*SRA*/ 10: Result = ALU_B_i >>> Shamt;
/*SRAV*/ 11: Result = ALU_B_i >>> ALU_A_i;
/*SRL*/ 12: Result = ALU_B_i >> Shamt;
/*SRLV*/ 13: Result = ALU_B_i >> ALU_A_i;
/*AND*/ 14: Result = ALU_A_i && ALU_B_i;
/*OR*/ 15: Result = ALU_A_i || ALU_B_i;
/*XOR*/ 16: Result = ALU_A_i ^^ ALU_B_i;
default:
begin
Result = 0;
Carryout = 0;
adder_b = 0;
end
endcase
end
endmodule
The code above will generated latches, here is the result given by Quartus II:
Warning (10240): Verilog HDL Always Construct warning at ALU.sv(16): inferring latch(es) for variable "Carryout", which holds its previous value in one or more paths through the always construct
Warning (10240): Verilog HDL Always Construct warning at ALU.sv(16): inferring latch(es) for variable "adder_b", which holds its previous value in one or more paths through the always construct
Error (10166): SystemVerilog RTL Coding error at ALU.sv(16): always_comb construct does not infer purely combinational logic.
I did added a default in the end of the case, can some one explain what is going on? Many thanks.
ALU_B_i
? Your synthesis tool should be able to figure out how to do a subtract operation. – nguthrie