7 - 简单状态机代码设计

7 - 简单状态机代码设计

三角波发生器

image-20211121194545898

代码:

//2021.11.21 lyw
//The simplest state machine——triangle wave generator

`timescale 1ns/10ps
module tri_gen (
                clk,
                res,
                d_out
);
input           clk;
input           res;
output[8:0]     d_out;      //299;2^8=256

reg             state;      //Main State Machine Register; 2 states need 1 bit
reg[8:0]        d_out;

always @(posedge clk or negedge res) begin
    if(~res)begin
        state=0;
        d_out=0;
    end
    else begin
        case(state)
        0://rise
        begin
              d_out<=d_out+1;
            if (d_out==299) begin
                state<=1;
            end
        end
        1://drop
        begin
            d_out<=d_out-1;
            if (d_out==1) begin
                state<=0;
            end
        end

        endcase

    end
end


endmodule

//-----testbench of tri_gen------
module tri_gen_tb;

reg             clk,res;
wire [8:0]      d_out;   
tri_gen U1 (
                .clk(clk),
                .res(res),
                .d_out(d_out)
);
initial begin
            clk<=0;
            res<=0;
    #17     res<=1;
    #8000   $stop;
end

always #5 clk=~clk;


endmodule

仿真波形:

image-20211121212723999

用模拟方式观察:

image-20211121213035530

注意:判断 d_out=299 和 d_out+1 是同时进行的,此时 d_out 按照 298 来判断。到下一个时钟上升沿时,d_out=299,state=0,这个周期跑完以后 d_out=300,state=1。

image-20211121223137525

梯形波发生器:

如果想把三角波换成梯形波:增加一个状态(上升、下降、平持三个状态)

代码:

//2021.11.21 lyw
//The simplest state machine——triangle wave generator

`timescale 1ns/10ps
module tri_gen (
                clk,
                res,
                d_out
);
input           clk;
input           res;
output[8:0]     d_out;      //299;2^8=256

reg[1:0]        state;      //Main State Machine Register; 2 states need 1 bit
reg[8:0]        d_out;
reg [7:0]       con;
always @(posedge clk or negedge res) begin
    if(~res)begin
        state=0;
        d_out=0;
        con<=0;
    end
    else begin
        case(state)
        0://rise
        begin
            d_out<=d_out+1;
            if (d_out==299) begin
                state<=1;
            end
        end
        1://flat
        begin
            if (con==200) begin
                state<=2;
                con<=0;
            end
            else begin
                con<=con+1;
            end
        end
        2://drop
        begin
            d_out<=d_out-1;
            if (d_out==1) begin
                state<=0;
            end
        end
        default:
        begin
            state<=0;
            con<=0;
            d_out<=0;
        end

        endcase

    end
end


endmodule

//-----testbench of tri_gen------
module tri_gen_tb;

reg             clk,res;
wire [8:0]      d_out;   
tri_gen U1 (
                .clk(clk),
                .res(res),
                .d_out(d_out)
);
initial begin
            clk<=0;
            res<=0;
    #17     res<=1;
    #20000   $stop;
end

always #5 clk=~clk;


endmodule

注意:用两位来表示三种状态还有剩余,需要一个 default。

仿真波形:

image-20211122140429819

如果继续改改成【上-平-下-平】格式

代码:

//2021.11.21 lyw
//The simplest state machine——triangle wave generator

`timescale 1ns/10ps
module tri_gen (
                clk,
                res,
                d_out
);
input           clk;
input           res;
output[8:0]     d_out;      //299;2^8=256

reg[1:0]        state;      //Main State Machine Register; 2 states need 1 bit
reg[8:0]        d_out;
reg [7:0]       con;
always @(posedge clk or negedge res) begin
    if(~res)begin
        state=0;
        d_out=0;
        con<=0;
    end
    else begin
        case(state)
        0://rise
        begin
            d_out<=d_out+1;
            if (d_out==299) begin
                state<=1;
            end
        end
        1://flat
        begin
            if (con==200) begin
                state<=2;
                con<=0;
            end
            else begin
                con<=con+1;
            end
        end
        2://drop
        begin
            d_out<=d_out-1;
            if (d_out==1) begin
                state<=3;
            end
        end
        /*
        default:
        begin
            state<=0;
            con<=0;
            d_out<=0;
        end
        */
        3://flat    
        begin
            if (con==200) begin
                state<=0;
                con<=0;
            end
            else begin
                con<=con+1;
            end
        end
        endcase

    end
end


endmodule

//-----testbench of tri_gen------
module tri_gen_tb;

reg             clk,res;
wire [8:0]      d_out;   
tri_gen U1 (
                .clk(clk),
                .res(res),
                .d_out(d_out)
);
initial begin
            clk<=0;
            res<=0;
    #17     res<=1;
    #20000   $stop;
end

always #5 clk=~clk;


endmodule

仿真波形:

image-20211122142332747

写状态机曾犯过的错误:

  1. 增加了状态,但 state 位数却没有随之增加。
  2. 当状态不够多时,要写 default。
  3. 增加了新状态,但跳转关系没改好。

PS:内容总结自网络,状态机好有意思啊(* ̄▽ ̄*)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单Verilog状态机的示例代码: ```verilog module state_machine ( input clk, input rst, input start, output reg [2:0] state ); localparam IDLE = 3'b000; localparam STATE1 = 3'b001; localparam STATE2 = 3'b010; localparam STATE3 = 3'b011; always @(posedge clk or posedge rst) begin if (rst) begin state <= IDLE; end else begin case (state) IDLE: begin if (start) begin state <= STATE1; end end STATE1: begin // State transition conditions and actions if (/* condition */) begin state <= STATE2; end else if (/* condition */) begin state <= STATE3; end end STATE2: begin // State transition conditions and actions if (/* condition */) begin state <= STATE1; end else if (/* condition */) begin state <= STATE3; end end STATE3: begin // State transition conditions and actions if (/* condition */) begin state <= STATE1; end else if (/* condition */) begin state <= STATE2; end end endcase end end endmodule ``` 上述示例代码中,定义了一个简单的3位状态机,包含了四个状态:IDLE、STATE1、STATE2和STATE3。时钟信号`clk`和复位信号`rst`用于控制状态机的操作。`start`信号被用作状态迁移条件之一,根据具体需求可以添加其他条件。 在`always @(posedge clk or posedge rst)`块中,根据当前状态`state`的值进行状态迁移操作。每个状态的具体操作和状态迁移条件可以根据实际需求进行定义和修改。 请注意,这只是一个简单的示例,实际的状态机设计可能更加复杂,并且具体的状态和迁移条件取决于您的应用需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值