• HDLBis-Fsm3s


    See also: State transition logic for this FSM

    The following is the state transition table for a Moore state machine with one input, one output, and four states. Implement this state machine. Include a synchronous reset that resets the FSM to state A. (This is the same problem as Fsm3 but with a synchronous reset.)

    StateNext stateOutput
    in=0in=1
    AAB0
    BCB0
    CAD0
    DCB1

    Module Declaration

    module top_module(
        input clk,
        input in,
        input reset,
        output out); 

    自己写的代码:

    1. module top_module(
    2. input clk,
    3. input in,
    4. input reset,
    5. output out);
    6. //采用同步置位reset
    7. reg[3:0] state;
    8. parameter A = 0, //0001
    9. B = 1, //0010
    10. C = 2, //0100
    11. D = 3; //1000
    12. // State transition logic
    13. always @(posedge clk) begin
    14. if(reset)begin
    15. state <= 4'b0001;
    16. end
    17. else begin
    18. state[A] <= state[A]&(~in)|state[C]&(~in);
    19. state[B] <= state[A]&in | state[B]&in |state[D]∈
    20. state[C] <= state[B]&(~in) | state[D]&(~in);
    21. state[D] <= state[C]∈
    22. end
    23. end
    24. // State flip-flops with synchronous reset
    25. // Output logic
    26. assign out = state[D];
    27. endmodule

    标准的写法是在always @(posedge clk)模块中写

     if(reset)begin

                state <= 4'b0001;

            end

            else begin

               state <= nextstate;

            end

    通过组合逻辑对nextstate的状态进行改变

    下面是标准的代码:

    1. module top_module(
    2. input clk,
    3. input in,
    4. input reset,
    5. output out);
    6. //采用同步置位reset
    7. reg[3:0] state,nextstate;
    8. parameter A = 0, //0001
    9. B = 1, //0010
    10. C = 2, //0100
    11. D = 3; //1000
    12. // State transition logic
    13. always @(*)begin
    14. nextstate[A] <= state[A]&(~in)|state[C]&(~in);
    15. nextstate[B] <= state[A]&in | state[B]&in |state[D]∈
    16. nextstate[C] <= state[B]&(~in) | state[D]&(~in);
    17. nextstate[D] <= state[C]∈
    18. end
    19. // State flip-flops with synchronous reset
    20. always @(posedge clk) begin
    21. if(reset)begin
    22. state <= 4'b0001;
    23. end
    24. else begin
    25. state <= nextstate;
    26. end
    27. end
    28. // Output logic
    29. assign out = state[D];
    30. endmodule

  • 相关阅读:
    【FPGA教程案例87】加解密1——基于FPGA的AES加解密算法verilog实现
    java之Object类
    头条号如何获得收益、怎样获得收益?
    java线程生命周期
    【笔试刷题训练】day_12
    传奇服务器配置如何搭建
    云原生之深入解析K8S中Ceph的部署与功能测试
    leetcode - 780. Reaching Points
    Spring事务的实现方式和实现原理
    华为OD机试 - 最远足迹(2022Q4 100分)
  • 原文地址:https://blog.csdn.net/Kai666888/article/details/133647882