|
楼主 |
发表于 2011-12-31 13:42:30
|
显示全部楼层
本帖最后由 xilinx_zhao 于 2011-12-31 13:48 编辑
模块代码如下:
module simple_fsm (clk, rst_n,w_i,z_o);
input clk;
input rst_n;
input w_i;
output z_o;
reg z_o;
reg [1:0]current_state;
reg [1:0] next_state;
parameter idle=2'b01;
parameter s0 =2'b10;
parameter s1 =2'b11;
always@(posedge clk)
begin
if (rst_n==1'b0)
current_state<=idle;
else
current_state<= next_state;
end
always@(*)
begin
next_state=current_state;
case (current_state)
idle: if (w_i==1'b1) next_state=s0;
else next_state=idle;
s0 : if (w_i==1'b1) next_state=s1;
else next_state=idle;
s1 : if (w_i==1'b1) next_state=s1;
else next_state=idle;
default : next_state=idle;
endcase
end
always@(posedge clk)
begin
if (!rst_n) z_o=1'b0;
else
case (current_state)
idle: z_o=1'b0;
s0 : z_o=1'b0;
s1 : z_o=1'b1;
default : z_o=1'b0;
endcase
end
endmodule
Testbench 如下,请把红色的改成非阻塞,仿真结果不一样,这是怎么回事:
module simple_fsm_tb ();
reg clk;
reg rst_n;
reg w_i;
integer i;
wire z_o;
event start;
reg simarray [6:0] ;
always
#10 clk=~clk;
initial
begin
clk=1'b0;
rst_n=1'b1;
#6 rst_n=~rst_n;
#6 rst_n=~rst_n;
-> start;
end
initial $readmemb("simarray.txt", simarray);
initial begin
@(start);
for (i=6;i>=0;i=i-1) begin @(posedge clk); w_i=simarray [ i ];end ;
//@(posedge clk); w_i<=1'b1;
//@(posedge clk); w_i<=1'b0;
//@(posedge clk); w_i<=1'b1;
//repeat(2) @(posedge clk) w_i=1'b0;
//repeat(2) @(posedge clk) w_i=1'b1;
//@(posedge clk); w_i<=1'b0;
end
initial $display ("Test control");
always begin
@(posedge clk); if (z_o==1'b1) $display("got one");
end
simple_fsm simple_fsm_0 (
.clk (clk),
.rst_n(rst_n),
.w_i(w_i),
.z_o(z_o)
);
endmodule |
|