HDLbits--Fsm1/Fsm1s

参考有限状态机

1.定义状态,定义状态寄存器(state,nextstate)

2.定义组合逻辑--状态转换逻辑

3.定义时序逻辑--何时转换状态/ 描述输出?

4.定义输出--应该可以放在3中?

module top_module(
    input clk,
    input areset,    // Asynchronous reset to state B
    input in,
    output out);//  

    parameter A=0, B=1; 
    reg state, next_state;

    always @(*) begin    // This is a combinational always block
        // State transition logic
        state<=next_state;
    end

    always @(posedge clk, posedge areset) begin    // This is a sequential always block
        // State flip-flops with asynchronous reset
        if(areset)
            next_state<=B;
        else
        case(state)
            A:begin
                if(in)
                    begin
                        next_state<=A;
                    end
                else
                    begin
                        next_state<=B;
                    end
            end
            B:begin
                if(in)
                    begin
                        next_state<=B;
                    end
                else
                    begin
                        next_state<=A;
                    end
            end
        endcase
                    
            
        
    end

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

endmodule

参考答案: 

一个状态机主要要包括三个部分

1.状态转换逻辑(组合逻辑)

2.状态触发(时序逻辑)

3.输出逻辑(组合逻辑)

module top_module (
	input clk,
	input in,
	input areset,
	output out
);

	// Give state names and assignments. I'm lazy, so I like to use decimal numbers.
	// It doesn't really matter what assignment is used, as long as they're unique.
	parameter A=0, B=1;
	reg state;		// Ensure state and next are big enough to hold the state encoding.
	reg next;
    
    
    // A finite state machine is usually coded in three parts:
    //   State transition logic
    //   State flip-flops
    //   Output logic
    // It is sometimes possible to combine one or more of these blobs of code
    // together, but be careful: Some blobs are combinational circuits, while some
    // are clocked (DFFs).
    
    
    // Combinational always block for state transition logic. Given the current state and inputs,
    // what should be next state be?
    // Combinational always block: Use blocking assignments.
    always@(*) begin
		case (state)
			A: next = in ? A : B;
			B: next = in ? B : A;
		endcase
    end
    
    
    
    // Edge-triggered always block (DFFs) for state flip-flops. Asynchronous reset.
    always @(posedge clk, posedge areset) begin
		if (areset) state <= B;		// Reset to state B
        else state <= next;			// Otherwise, cause the state to transition
	end
		
		
		
	// Combinational output logic. In this problem, an assign statement is the simplest.
	// In more complex circuits, a combinational always block may be more suitable.
	assign out = (state==B);

	
endmodule

// Note the Verilog-1995 module declaration syntax here:
module top_module(clk, reset, in, out);
    input clk;
    input reset;    // Synchronous reset to state B
    input in;
    output out;//  
    reg out;

    // Fill in state name declarations
    parameter A = 0,B = 1;

    reg present_state, next_state;

    always @(posedge clk) begin
        if (reset) begin  
            // Fill in reset logic
            next_state=B;
        end else begin
            case (present_state)
                // Fill in state transition logic
                A:
                    next_state=in?A:B;
                B:
                    next_state=in?B:A;
                        
            endcase
        end

            // State flip-flops
            present_state = next_state;   

            case (present_state)
                // Fill in output logic
                A:
                    out=1'b0;
                B:
                    out=1'b1;
            endcase
        //end
    end

endmodule

没搞懂的:end的位置?阻塞赋值? 

Verilog FSM(Finite State Machine)是用Verilog语言编写的有限状态机。通过组合逻辑和时序逻辑的描述来实现对具有逻辑顺序事件的控制。在Verilog中,FSM的编写可以遵循一定的规范和推荐写法。 在编写Verilog FSM时,通常会使用always块来描述时钟上升沿或复位信号的触发条件。在这个always块中,可以使用if-else语句来处理复位信号,并根据当前状态和输入信号进行状态转移。 另外,为了使代码结构规范清晰,通常会使用三段式状态机的写法。第一个always块用来描述状态转移的触发条件,第二个always块用来描述下一状态的判断,第三个always块用来描述各状态的输出。这样的写法可以将组合逻辑和时序逻辑分开,易于综合。 在Verilog中,状态的编码可以使用二进制、格雷码或独热码。二进制编码简便,适用于小型设计。格雷码需要状态顺序跳变才能利用其特性,而独热码则常用于状态机设计中,因为它的译码简单,节省组合逻辑,并且时序更快,还能减少毛刺产生的概率。 下面是一个示例的Verilog FSM代码,其中使用了独热码编码状态和状态转移的逻辑: ```verilog localparam S0 = 4'b0001; localparam S1 = 4'b0010; localparam S2 = 4'b0100; localparam S3 = 4'b1000; reg [3:0 current_state; reg [3:0 next_state; // 状态转移 always @(posedge clk or negedge rst_n) begin if(!rst_n) current_state <= S0; else current_state <= next_state; end // 下一状态判断 always @ (*) begin case(current_state) S0 : next_state = S1; S1 : next_state = S2; S2 : next_state = S3; S3 : next_state = S0; default: next_state = S0; endcase end // 状态输出 always @(posedge clk or negedge rst_n) begin if(!rst_n) begin // reset condition end else begin case(current_state) S0 : begin // state S0 output end S1 : begin // state S1 output end S2 : begin // state S2 output end S3 : begin // state S3 output end default : ; endcase end end ``` 这个示例代码展示了一个简单的Verilog FSM,使用独热码编码了四个状态,并根据时钟信号和复位信号实现了状态转移和状态输出逻辑。你可以根据具体的需求修改状态和状态转移的逻辑,以及每个状态的输出逻辑。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [verilog FSM 范例](https://download.csdn.net/download/u013560111/6884151)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【Verilog】FSM设计](https://blog.csdn.net/m0_52840978/article/details/123390136)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值