HDLbits刷题继续

image.png
module top_module(
    input clk,
    input areset,    // Asynchronous reset to OFF
    input j,
    input k,
    output out); //  

    parameter OFF=0, ON=1; 
    reg state, next_state;

    always @(*) begin
        case(state)
            ON:
                if(k) next_state = OFF;
                else if(~k) next_state = ON;
                else next_state = ON;
            OFF: 
                if(j) next_state = ON;
                else if(~j) next_state = OFF;
                else next_state = OFF;
            
                    // State transition logic
        endcase
    end

    always @(posedge clk, posedge areset) begin
        if(areset)
            state <= OFF;
        else
            state <= next_state;// State flip-flops with asynchronous reset
    end

    // Output logic
    assign out = (state == OFF)? 0 : 1;
    // assign out = (state == ...);

endmodule

-----------------------------官网参考答案--------------------------------
module top_module (
    input clk,
    input j,
    input k,
    input areset,
    output out
);
    parameter A=0, B=1;
    reg state;
    reg next;
    
    always_comb begin
        case (state)
            A: next = j ? B : A;   //这里采用三目运算符简化 了代码量
            B: next = k ? A : B;
        endcase
    end
    
    always @(posedge clk, posedge areset) begin
        if (areset) state <= A;
        else state <= next;
    end
        
    assign out = (state==B);

    
endmodule
image.png
module top_module(
    input clk,
    input reset,    // Synchronous reset to OFF
    input j,
    input k,
    output out); //  

    parameter OFF=0, ON=1; 
    reg state, next_state;

    always @(*) begin
        case(state)
            ON: next_state = (k==0)? ON:OFF;
            OFF: next_state = (j==0)? OFF:ON;
        endcase
                // State transition logic
    end

    always @(posedge clk) begin
        if(reset)
            state <= OFF;
        else 
            state <= next_state;// State flip-flops with synchronous reset
    end

    // Output logic
    assign out = (state== OFF) ? 0:1;
    // assign out = (state == ...);

endmodule
          
  • The following is the state transition table for a Moore state machine with one input, one output, and four states. Use the following state encoding: A=2'b00, B=2'b01, C=2'b10, D=2'b11.
    **Implement only the state transition logic and output logic **(the combinational logic portion) for this state machine. Given the current state (state), compute the next_state and output (out) based on the state transition table.


    image.png
module top_module(
    input in,
    input [1:0] state,
    output [1:0] next_state,
    output out); //

    parameter A=0, B=1, C=2, D=3;
    
    always@(*)
        case(state)
            A: next_state = (in)?B:A;
            B: next_state = (in)?B:C;
            C: next_state = (in)?D:A;
            D: next_state = (in)?B:C;
        endcase
    // State transition logic: next_state = f(state, in)
    assign out = (state==D)?1:0;
    // Output logic:  out = f(state) for a Moore state machine

endmodule
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容