两段式状态机不可能完成的任务
兩段式狀態(tài)機(jī)不可能完成的任務(wù)(特權(quán))
???????? 最近折騰狀態(tài)機(jī),發(fā)現(xiàn)一個(gè)小任務(wù)對(duì)于兩段式狀態(tài)機(jī)寫法是不可能完成的。這個(gè)小任務(wù)很簡(jiǎn)單,先看用一段式狀態(tài)機(jī)實(shí)現(xiàn)的代碼:
module test(
??????????? clk,rst_n,
??????????? din,dout
??????? );
?
input clk;
input rst_n;???
input din;
output[3:0] dout;??
?
parameter IDLE? = 3'd0;
parameter STA1? = 3'd1;
?
//一段式寫法
reg[2:0] cstate;
reg[3:0] cnt;
?
always @(posedge clk or negedge rst_n)
??? if(!rst_n) cstate <= IDLE;
??? else begin
??????? case(cstate)
??????????? IDLE: begin
??????????????????? cnt <= 4'd0;
??????????????????? if(din) cstate <= STA1;
??????????????????? else cstate <= IDLE;???????
??????????????? end
??????????? STA1: begin
??????????????????? cnt <= cnt+1'b1;
??????????????????? if(cnt == 4'd10) cstate <= IDLE;
??????????????????? else cstate <= STA1;
??????????????? end
??????????? default: cstate <= IDLE;
??????? endcase
??? end
?
assign dout = cnt;
?
endmodule
???????? 同樣的,用三段式狀態(tài)機(jī)也能夠?qū)崿F(xiàn)這個(gè)功能:
//三段式寫法
reg[2:0] cstate,nstate;
reg[3:0] cnt;
?
always @(posedge clk or negedge rst_n)
??? if(!rst_n) cstate <= IDLE;
??? else cstate <= nstate;
?
always @(cstate or din or cnt) begin
??? case(cstate)
??????? IDLE: ? if(din) nstate = STA1;
??????????????? else nstate = IDLE;????
??????? STA1: ? if(cnt == 4'd10) nstate = IDLE;
??????????????? else nstate = STA1;
??????? default: nstate = IDLE;
??? endcase
end
?
always @(posedge clk or negedge rst_n)
??? if(!rst_n) cnt <= 4'd0;
??? else begin
??????? case(nstate)
??????????? IDLE: ? cnt <= 4'd0;
??????????? STA1: ? cnt <= cnt+1'b1;
??????????? default: ;
??????? endcase
??? end
???????? 嚴(yán)格來看,上面的三段式狀態(tài)機(jī)相比于一段式會(huì)滯后一個(gè)時(shí)鐘周期。但是我們的重點(diǎn)不在這里,大家大可以不必鉆這個(gè)牛角尖。另外,這個(gè)實(shí)例實(shí)現(xiàn)的功能本身也沒 有什么意義,當(dāng)然也是可以用別的更簡(jiǎn)單(不需要狀態(tài)機(jī))的方式實(shí)現(xiàn),但是你可以想象成這是實(shí)際應(yīng)用中狀態(tài)機(jī)各種復(fù)雜輸出的一部分。
???????? 而如果大家希望用兩段式狀態(tài)機(jī)實(shí)現(xiàn)這個(gè)功能,或許會(huì)這么寫:
//兩段式寫法
reg[2:0] cstate,nstate;
reg[3:0] cnt;
?
always @(posedge clk or negedge rst_n)
??? if(!rst_n) cstate <= IDLE;
??? else cstate <= nstate;
?
always @(cstate or din or cnt) begin
??? case(cstate)
??????? IDLE: begin
??????????????? cnt = 4'd0;
??????????????? if(din) nstate = STA1;
??????????????? else nstate = IDLE;????
??????????? end
??????? STA1: begin
??????????????? cnt = cnt+1'b1;
??????????????? if(cnt == 4'd10) nstate = IDLE;
??????????????? else nstate = STA1;
??????? ??? end
??????? default: nstate = IDLE;
??? endcase
end
???????? 如果大家有興趣對(duì)三中代碼方式都做一下仿真,會(huì)發(fā)現(xiàn)一些有意思的問題,尤其兩段式狀態(tài)機(jī)最終根本無法退出STA1,計(jì)數(shù)器cnt也會(huì)死在那里。究其根本原因,可大有學(xué)問。在編譯工程后,出現(xiàn)了數(shù)條類似下面的warning:
Warning: Found combinational loop of 2 nodes
??? Warning: Node "Add0~2"
??? Warning: Node "cnt~9"
??????? 何為combinational loop?讓handbook來解釋吧,看不懂英文的可別怪我~_~
Combinational loops are among the most common causes of instability and unreliability in digital designs. They should be avoided whenever possible. In a synchronous design, feedback loops should include registers. Combinational loops generally violate synchronous design principles by establishing a direct feedback loop that contains no registers. For example, a combinational loop occurs when the left-hand side of an arithmetic expression also appears on the right-hand side in HDL code. A combinational loop also occurs when you feed back the output of a register to an asynchronous pin of the same register through combinational logic, as shown in Figure 5–1.
? ? ? ? 沒有寄存器打一拍的這種combinational loop(組合環(huán))是一種不推薦的設(shè)計(jì)方式,就如兩段式狀態(tài)機(jī)所實(shí)現(xiàn)的效果,甚至最終無法實(shí)現(xiàn)功能要求。同樣的功能,一段式和三段式狀態(tài)機(jī)之所以能夠解決 這個(gè)問題,就是避免了在純組合邏輯中涉及這個(gè)反饋邏輯。在初學(xué)verilog時(shí),我們常提的latch(鎖存器),其實(shí)也是combinational loop的一個(gè)特例。
轉(zhuǎn)載于:https://www.cnblogs.com/lueguo/p/3582250.html
總結(jié)
以上是生活随笔為你收集整理的两段式状态机不可能完成的任务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js aop 拦载实现
- 下一篇: C# Windows Phone 8 W