这是一个 Verilog 示例,展示了状态机的实现。第一个 CASE 语句定义了取决于状态机变量状态值的输出。第二个 CASE 语句定义了状态机的转换以及控制转换的条件。
有关在项目中使用此示例的更多信息,请参阅 Verilog 网页上的“如何使用 Verilog HDL 示例”部分。
statem.v
module statem(clk, in, reset, out); input clk, in, reset; output [3:0] out; reg [3:0] out; reg [1:0] state; parameter zero=0, one=1, two=2, three=3; always @(state) begin case (state) zero: out = 4'b0000; one: out = 4'b0001; two: out = 4'b0010; three: out = 4'b0100; default: out = 4'b0000; endcase end always @(posedge clk or posedge reset) begin if (reset) state = zero; else case (state) zero: state = one; one: if (in) state = zero; else state = two; two: state = three; three: state = zero; endcase end endmodule