RTL code writing specification

(1) Use non-blocking assignment to write sequential logic code

  • All variables need to wait for the trigger edge to arrive before assigning
  • When calculating the signal on the right side of the assignment number, all variables are the values ​​before the trigger edge.
  • The statements of the procedure block are executed in parallel

(2) Write combinational logic block code with always block to use blocking assignment

  • The sensitive list in the always block should be level-triggered

(3) You cannot use both blocking assignment and non-blocking assignment in the same always block

(4) Only one variable can be assigned in an always block

  • always blocks are executed in parallel
  • Assignment to the same variable in multiple always blocks is strictly prohibited
  • Assignment to multiple variables in an always block is not recommended

(5) In the asynchronous reset program, if the if statement appears in the always statement, the sensitive control signal (usually the reset signal) other than the clock signal must be judged in the first if statement . If it is placed in the following else if statement, the compile error

  • Common mistake: cannot match operand(s) in the condition to the corresponding edges in the enclosing event control of the always construct. The corresponding code block is:
always @(posedge sys_clk or negedge sys_rst_n) begin
    if (count == COUNT_MAX)            // 
        led_out <= ~led_out;         // 时序逻辑块中用非阻塞赋值
    else if(sys_rst_n == 1'b0)
        led_out <= 1'b0;
    else 
        led_out <= led_out;
end

Obviously, the reset signal is not judged in the first if statement, so there is an error!

  • Modification method: Put sensitive control signals other than the clock signal in the first if statement
always @(posedge sys_clk or negedge sys_rst_n) begin
    if (sys_rst_n == 1'b0)            // 
        led_out <= 1'b0;
    else if(count == COUNT_MAX)
        led_out <= ~led_out;         // 时序逻辑块中用非阻塞赋值
    else 
        led_out <= led_out;
end

(6) The name of the parameter parameter is capitalized to distinguish other variables

parameter COUNT_MAX = 25'd24_999_999

(7) When writing longer numbers, separate them with _ (underscore) to enhance readability

parameter MIN = 25'd10_999_999,
parameter MAX = 25'd24_999_999

Guess you like

Origin blog.csdn.net/jac_chao/article/details/124158253