在用Verilog HDL描述狀態機下,一般建議使用三段式。如下:
reg[4:0] curr_state,next_state;
always@(posedge clk or posedge rst)
begin
if(rst)
curr_state <= STATE_RESET;
else
curr_state <= next_state;
end
always@(clk,rst,cnt_delay,cnt_bit,start,curr_state)
begin
if(rst)
next_state = STATE_RESET;
else
begin
case(curr_state)
STATE_RESET:
next_state = STATE_IDLE;
STATE_IDLE:
if(start)
next_state = STATE_START;
狀態的切換操作
default: next_state = STATE_IDLE;
endcase
end
end
always@(posedge clk)
begin
case(curr_state)
STATE_RESET:
readData <= 16'd0;
STATE_DONE:
readData <= readDataBuf;
default:
readData <= readData;
endcase
end
等後續各種賦值操作。
在開始使用時,在next_state的切換時,使用的是非阻塞賦值。前兩天通過看例子,發現在next_state的切換時,都是使用的阻塞賦值,有了這個思考。
一直都知道,在時序邏輯中使用非阻塞賦值,在組合邏輯中使用阻塞賦值。所以在這個基礎上,在如下的always中,敏感信號是沿信號,所以是描述的時序電路,使用非阻塞賦值。
always@(posedge clk)
而在next_state的切換中,always的 敏感信號是多個電平信號(非沿信號),則說明該程序塊描述的組合邏輯電路,所以應該使用阻塞賦值。
今天在跟同事講這個的時候,突然想到這裏,頓悟了。
很簡單的一個“next_state的狀態轉換應該使用阻塞賦值還是非阻塞賦值“問題,在知道使用阻塞賦值,但並說不清楚爲什麼使用阻塞賦值的狀況下,終於想明白了。