is not a constant” error in if-statement

前端 未结 2 412
刺人心
刺人心 2021-01-18 16:54

I am trying to write a simple module to output a 14-bit number based on the value of four input signals. My attempt is shown below.

module select_size(
    i         


        
2条回答
  •  误落风尘
    2021-01-18 16:57

    As @Tim has already answered, using reg types inside always blocks or wire with assign.

    @Tim has also described the nested ternary assignments, while in the example are written very well, they are generally seen as bad practice. They imply a very long combinatorial path and can be hard to maintain. The combinatorial path may be optimised by synthesis which should imply a mux with optimised selection logic.

    Easier to maintain code will have a lower cost of ownership, and as long as it does not lead to a larger synthesised design it is normally preferred.

    My implementation would be to use a casez, (? are don't cares). I find the precedence of each value easier to see/debug.

    module select_size(
      input a,
      input b,
      input c,
      input d,
      output logic [13:0] size //logic (SystemVerilog) or reg type
    );
    
    always @* begin
      casez ({a,b,c})
        3'b1?? : size = 14'h2222 ;
        3'b01? : size = 14'h1111 ;
        3'b001 : size = 14'h0777 ;
        3'b000 : size = 14'h0333 ;
        default: size = 'bx      ;
      endcase
    end
    
    endmodule
    

提交回复
热议问题