其他分享
首页 > 其他分享> > HDLBits刷题记录——FSMQ3a

HDLBits刷题记录——FSMQ3a

作者:互联网

导言

Q3a这题要求使用尽量少的状态,那就按照状态图给的A和B两种状态来设计(虽然我感觉多加几个状态,这个电路的设计能够更简洁一些)。

思路

代码

module top_module (
    input clk,
    input reset,   // Synchronous reset
    input s,
    input w,
    output z
);
    parameter A=1'b0,B=1'b1;
    reg state,next;
    reg [1:0] cyc_cout,w_cout;
    
    always@(posedge clk) begin
        if(reset) begin
            state <= A;
        end
        else begin
            state <= next;
        end
    end
    
    always@* begin
        case(state)
            A: next = s ? B:A;
            B: next = B;
        endcase
    end
    
    always@(posedge clk) begin
        if(reset) begin
            cyc_cout <= 2'b0;
        end
        else if(state == B) begin
            if(cyc_cout == 2'd2) begin
                cyc_cout <= 2'd0;
            end
            else begin
                cyc_cout <= cyc_cout + 1'd1;
            end
        end
        else begin
            cyc_cout <= 2'b0;
        end
    end
    
    always@(posedge clk) begin
        if(reset) begin
            w_cout <= 2'd0;
        end
        else if(state == B) begin
            if(cyc_cout == 2'd0) begin
                w_cout <= w ? 2'd1:2'd0; //key point
            end
            else if(w == 1'b1) begin
                w_cout <= w_cout + 1'd1;
            end
            else begin
                w_cout <= w_cout;
            end
        end
        else begin
            w_cout <= 2'd0;
        end
    end
    
    always@* begin
        case(state)
            A: z = 1'b0;
            B: z = (cyc_cout == 2'd0 && w_cout == 2'd2);
        endcase
    end

endmodule

小结

标签:reset,FSMQ3a,cout,HDLBits,状态,个数,cyc,input,刷题
来源: https://blog.csdn.net/JJJJJJJerry/article/details/112985185