该题的状态机共2输入2输出;当复位信号撤销时,在下一个周期内将f输出为1,需留意f为1持续一个周期;然后状态机取决于x的值,当x在连续的三个周期中产生值为1、0、1时,下一周期将g输出为1,在保持g为1时判断y的输入,如果y在两个周期中有任意一个周期为1了,那么g永久保持1;如果两个周期都没有1,那么g将永久保持0。
module top_module (
input clk,
input resetn, // active-low synchronous reset
input x,
input y,
output f,
output g
);
parameter a=4'd0,b=4'd1,c=4'd2,d=4'd3,e=4'd4,F=4'd5,G=4'd6,h=4'd7;
reg [3:0]state,next_state;
reg rstn1,rstn2;
always@(posedge clk)begin
if(!resetn)
state<=a;
else
state<=next_state;
end
always@(*)begin
if(!resetn)begin
f=0;
g=0;
end
else begin
case(state)
a:next_state=resetn?b:a;
b:begin
f=1;
g=0;
next_state=x?c:b;
end
c:begin
f=0;
g=0;
next_state=x?c:d;
end
d:begin
f=0;
g=0;
next_state=x?e:b;
end
e:begin
f=0;
g=1;
next_state=y?F:G;
end
F:begin
f=0;
g=1;
next_state=F;
end
G:begin
f=0;
g=1;
next_state=y?F:h;
end
h:begin
f=0;
g=0;
next_state=h;
end
endcase
end
end
endmodule
上面是最开始我的想法,但这样不太好设计 f 的输出(上述代码中f的输出逻辑是错误的),解决方法是多加一个状态专门用来进行 f 的输出,称为fout
module top_module (
input clk,
input resetn, // active-low synchronous reset
input x,
input y,
output f,
output g
);
parameter a=4'd0,b=4'd1,c=4'd2,d=4'd3,e=4'd4,F=4'd5,G=4'd6,h=4'd7,fout=4'd8;
reg [3:0]state,next_state;
reg rstn1,rstn2;
always@(posedge clk)begin
if(!resetn)
state<=a;
else
state<=next_state;
end
always@(*)begin
case(state)
a:next_state=resetn?fout:a;
fout:next_state=b;
b:next_state=x?c:b;
c:next_state=x?c:d;
d:next_state=x?e:b;
e:next_state=y?F:G;
F:next_state=F;
G:next_state=y?F:h;
h:next_state=h;
endcase
end
assign f=state==fout;
assign g=state==e|state==F|state==G;
endmodule