【组合和时序逻辑杂记(1)】
【1】. Verilog 代码中,if 语句注意事项;
(1)
always @(posedge clk_i) begin
if (A) B <= 16'h0;
C <= C + 16'h1;
end
(2)
always @(posedge clk_i) begin
if(A)
B <= 16'h0;
C <= C+ 16'h1;
end
上述(1)中,表示:
1)C 每个时钟上升沿累加1;2)A 非0时,B 清零。
上述(2)中,表示:
1)A 非0时,B清零;同时 C累加数值1。
2)一般情况上述(2)写法会报错;需更改为:
always @(posedge clk_i)begin
if(A) begin
B <= 16'h0;
C <= C + 16'h1;
end
end
小结:verilog书写规范会影响代码的解读。
即:if 语句后面没有接begin,仅执行第1个分号前的语句。
【2】时序逻辑中阻塞赋值,(含同时赋值)
(1) always @(posedge clk_i)begin
test_bit = test_bit + 1'b1;
end
(2) always @(posedge clk_i)begin
test_bit <= test_bit + 1'b1;
end
小结:示例(1)和(2)执行相同的时序,即下一拍更新数值。
(3) always @(posedge clk_i)begin
test_bit = test_bit << 1'b1;
if(A) test_bit = test_bit + 1'b1;
end
(4) always @(posedge clk_i)begin
test_bit = test_bit << 1'b1;
if(A) test_bit <= test_bit + 1'b1;
end
小结:示例(3)和(4)执行相同的时序,即A为假时,test_bit移位操作;A为真时,test_bit 组合逻辑执行移位和加1操作,并下一拍更新数值输出。
(5) always @(posedge clk_i)begin
test_bit <= test_bit << 1'b1;
if(A) test_bit <= test_bit + 1'b1;
end
(6) always @(posedge clk_i)begin
test_bit <= test_bit << 1'b1;
if(A) test_bit = test_bit + 1'b1;
end
小结:示例(5),在A为假时,test_bit移位操作;A为真时,test_bit执行加1操作。(有时仿真会报错)
示例(6),在A为假时,test_bit移位操作;A为真时,test_bit无操作(程序异常)。
Note:
建议:
(1)不要在时序逻辑中,进行阻塞赋值,会造成程序异常或者未知状态的出现。
(2)不要在always 语句里面,多个并行语句同时赋值。

浙公网安备 33010602011771号