HDLBits Exams/2014 q3fsm

 

 

module top_module (
    input clk,
    input reset,   // Synchronous reset
    input s,
    input w,
    output z
);
    parameter A=0,B=1;
    reg state,next_state;
    reg z_next;
    reg [1:0]count,next_count,next_ocount,ocount;//count 用来对B的周期计数,ocount用于计算1的总个数
    always@(*)begin
        next_state=state;
        z_next=z;
        next_ocount=ocount;
        next_count=count;
        if(reset)begin
            next_state=A;
            next_ocount=0;
            z_next=0;
            next_count=0;
        end
        else begin
            case(state)
                A:begin 
                    if(s)begin 
                        next_state=B;
                        next_count=0;
                        next_ocount=0;
                    end
                end

                B:begin
                    z_next=0;
                    next_count=count+1;
                    if(w)begin
                        next_ocount=ocount+1;
                    end
                    if(count==2)begin
                        next_count=0;
                        if(next_ocount==2)begin
                            z_next=1;
                        end
                        next_ocount=0;
                    end
                end
            endcase
        end
    end
    
    always@(posedge clk)begin
        state=next_state;
        count=next_count;
        ocount=next_ocount;
        z=z_next;
    end
    
    
endmodule

结果

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值