UFP 发表于 2010-6-26 01:57:35

状态机有问题吗

本帖最后由 fpgaw 于 2010-11-18 16:22 编辑

module bingcuanfan(clk,rst,a,b,c);
input clk;
input rst;
input a;
output b;
output c;
regtemp;
reg b;
reg state;
//regnextstate;
parameter idle=11'b00000000001,st0=11'b00000000010,st1=11'b00000000100,st2=11'b00000001000,st3=11'b00000010000,st4=11'b00000100000,st5=11'b00001000000,
    st6=11'b00010000000,st7=11'b00100000000,st8=11'b01000000000,st9=11'b10000000000;
assign c=3'b111;
always @(negedge clk)
begin
if(rst)
begin
state<=idle;
temp<=8'b0;
b<=8'b1;
end
elsebegin
case(state)
idle: state<=st0;
st0:begin
temp<={temp,a};
state<=st1;
end
st1:begin
temp<={temp,a};
state<=st2;
end
st2:begin
temp<={temp,a};
state<=st3;
end
st3:begin
temp<={temp,a};
state<=st4;
end
st4:begin
temp<={temp,a};
state<=st5;
end
st5:begin
temp<={temp,a};
state<=st6;
end
st6:begin
temp<={temp,a};
state<=st7;
end
st7:begin
temp<={temp,a};
state<=st8;
end
st8:begin
b<=temp;
state<=st9;
end
st9:begin
state<=idle;
end
default:begin
state<=idle;
b<=8'b1;
temp<=8'b0;
end
endcase         
end
end
endmodule

longt 发表于 2010-6-26 02:19:50

状态机的写法有好几个的,你的写法来说确实是不是很好的写法,<br>
一般推荐的写法是,至少要有两个进程的,一个是时钟进程,一个是组合进程,在时钟进程里面一般做的工作是reset时状态的复位,还有就是将下一个状态的值付给当前状态

ngtim 发表于 2010-6-26 02:53:49

就是说我这种写发一般会产生什么不好的后果,谢谢指教,他们中和出来的电路不一样吗

UFO 发表于 2010-6-26 04:13:15

另一个时一个组合进程,是根据当前状态给下一个状态赋值,例如:<br>
process(reset,clk)<br>
if reset='0' then<br>
&nbsp; &nbsp;state&lt;="0000";<br>
elsif clk='1' and clk'event then<br>
&nbsp; &nbsp;state&lt;=state_next;<br>
end if;<br>
end process;<br>
process(state)<br>
begin<br>
case (state )is<br>
&nbsp;&nbsp;when "0001"=&gt;<br>
&nbsp; &nbsp;&nbsp; &nbsp;state_next&lt;="0010";<br>
when "0010"=&gt;<br>
<br>

ATA 发表于 2010-6-26 05:51:27

&nbsp;&nbsp;state_next&lt;="0011";<br>
when others=&gt;<br>
&nbsp;&nbsp;state_next&lt;="0000";<br>
end case;<br>
end process;<br>
我不过是举了个例子,状态机要写好还是要花不少功夫的

CHANG 发表于 2010-6-26 07:45:33

结果会有一些的不一样,分开两个进程来写的话,第一个就是比较清晰,便于代码维护;其次综合出来的结果还是会有一些不同的

usd 发表于 2010-6-26 08:33:31

"其次综合出来的结果还是会有一些不同的"我也觉得会不一样,但就是不太清楚何时会产生错的结果

AAT 发表于 2010-6-26 09:25:12

为什么会有不同呢?我觉得如果代码写的没有歧异,二者综合出来的结果应该是相同的。

UFO 发表于 2010-6-26 09:54:36

有时会有些我们想不到的结果的!

ICE 发表于 2010-6-26 11:17:34

要看综合工具是怎么综合的,我以前看到说综合工具是以一个process做为一个单位来综合的,所以一个process写的太大是不好的
页: [1] 2 3
查看完整版本: 状态机有问题吗