实现下图所示的摩尔状态机,复位为异步复位。
代码实现:
module top_module(
input clk,
input areset, // Asynchronous reset to state B
input in,
output out);//
parameter A=0, B=1;
reg state, next;
// State transition logic
always@(*)begin // This is a combinational always block
case(state)
A: next = in ? A : B;
B: next = in ? B : A;
endcase
end
// State flip-flops with asynchronous reset
always @(posedge clk or posedge areset) begin // This is a sequential always block
if(areset == 1'b1)begin
state <= B;
end else begin
state <= next;
end
end
// Output logic
assign out = (state == B);
endmodule