HDLBits在线练习题之Exams/2014 q3fsm

地址:HDLBits - Exams/2014 q3fsm
介绍:仅记录代码

module top_module (
    input clk,
    input reset,   // Synchronous reset
    input s,
    input w,
    output z
);

    localparam A=1'b0,B=1'b1;
    reg current_state,next_state;
    
    always @(*) begin
        next_state = 1'bx;
        case (current_state) 
            A: next_state = s?B:A;
            B: next_state = B;
            default: next_state = A;
        endcase
    end
    always @(posedge clk) begin
        if (reset) 
            current_state <= A;
        else 
            current_state <= next_state;
    end
    
    reg [1:0] clk_cnt,w_cnt;
    always @(posedge clk) begin
        if (reset)
            clk_cnt <= 2'd0;
        else if (next_state==B)
            if (clk_cnt==2'd3)
                clk_cnt <= 2'd1;
        	else 
                clk_cnt <= clk_cnt+2'd1;
        else 
            clk_cnt <= 2'd0;
    end
    
    always @(posedge clk) begin
        if (reset)
            w_cnt <= 2'd0;
        else if (w && current_state==B)
            if (clk_cnt==2'd1)
                w_cnt <= 2'd1;
        	else
            	w_cnt <= w_cnt+2'd1;
        else
            if (clk_cnt==2'd1)
                w_cnt <= 2'd0;
            else
                w_cnt <= w_cnt;
    end
    
    always @(*) begin
        if (clk_cnt==2'd1 && w_cnt==2'd2)
            z <= 1'b1;
        else 
            z <= 1'b0;
    end
endmodule
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值