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

你可能感兴趣的:(HDLBits,-,Verilog在线实战,物联网)