is not a constant” error in if-statement

前端 未结 2 404
刺人心
刺人心 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
    
    0 讨论(0)
  • 2021-01-18 17:11

    Two problems:

    1) You need to put if statements inside an always block.

    If you use verilog-2001, you can use

    always @*
       if ....
       end
    end
    

    Otherwise specify all the inputs in the sensitivity list:

    always @(a or b or c or d)
       if ....
       end
    end
    


    2) Constant assignments are not allowed inside if statements.

    Remove the assign keyword from any statements inside the if block:

    if (a) begin
        size = 14'h2222;
    end
    

    You will also have to declare size as a reg type.

    However my preference would be to rewrite the entire module with conditional operator, I find it much preferrable to read. This following module achieves the same result:

    module select_size(
        input a,
        input b,
        input c,
        input d,
        output [13:0] size
    );
    
        assign size = a ? 14'h2222 :
                      b ? 14'h1111 :
                      c ? 14'h0777 : 
                          14'h0333 ;
    
    endmodule
    
    0 讨论(0)
提交回复
热议问题