13 Finite State Machines
Fsm1
这里需要实现一个简单的摩尔状态机,即输出只与状态有关的状态机。
我这里代码看上去比长一点,答案用的case和三目运算符,结果是一样的。
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; always @(*) begin // This is a combinational always block if(state ==A)begin if(in==1) next_state=A; else next_state=B; end else if(state ==B)begin if(in==1) next_state=B; else next_state=A; end end always @(posedge clk, posedge areset) begin // This is a sequential always block if(areset) state <= B; else state <= next_state; end // Output logic assign out = (state == B); endmodule