【Verilog】HDLBits之FSM(一)

写自己动手写cpu时写到除法器部分感觉对状态机还不是比较熟悉,做题家还是要多刷题巩固,少动脑子多花时间。

一: FSM1(异步复位)

This is a Moore state machine with two states, one input, and one output. Implement this state machine. Notice that the reset state is B.
This exercise is the same as fsm1s, but using asynchronous reset.
在这里插入图片描述

/*This is a Moore state machine with two states, one input, and one output. 
Implement this state machine. Notice that the reset state is B.*/
module fsm1(
input clk,
input areset,
input in,
output out
);
parameter A=1'b0,B=1'b1;//一共两个状态
reg state,next_state;
//两段式

	/*always @ (posedge clk,posedge areset)begin
		if(areset)begin
			state <= B;
		end
		else begin
			state <= nextstate;
		end
	end*/

always @ (*) begin
	case(state)
		B:begin
			if(in == 1'b1)begin
				next_state <= B;
			end
			else begin
				next_state <= A;
			end
		end
		A:begin
			if(in == 1'b1)begin
				next_state <= A;
			end
			else begin
				next_state <= B;
			end
		//end
			//default:begin
            //end
		
        end
	endcase
end
    
always @ (posedge clk,posedge areset)begin
		if(areset)begin
			state <= B;
		end
		else begin
			state <= next_state;
		end
	end
    
always @ (*) begin
	if(state == B)begin
		out = 1'b1;
	end
	else begin
		out = 1'b0;
	end
end
endmodule

二:FSM2(异步复位)

This is a Moore state machine with two states, two inputs, and one output. Implement this state machine.
This exercise is the same as fsm2s, but using asynchronous reset.
在这里插入图片描述

module fsm2(
input clk,
input areset,
input j,
input k,
output out
);
parameter OFF=0,ON=1;
reg state,next_state;

always @ (posedge clk,posedge areset) begin
    if(areset) begin
	state <= OFF;
end
else begin
	state <= next_state;
end
end

always @ (*) begin
	case(state)
		OFF:begin
			if(j == 0)begin
				next_state <= OFF;
			end
			else begin
				next_state <= ON;
			end
		end
		ON:begin
			if(k == 0)begin
				next_state <= ON;
			end
			else begin
				next_state <= OFF;
			end
		end
	endcase
end

always @ (*) begin
	if(state == OFF)begin
		out <= 1'b0;
	end
	else begin
		out <= 1'b1;
	end
end

endmodule	

同步复位将areset改成reset即可

三:Fsm3comb

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.
在这里插入图片描述
上图是状态转移表

module fsm3comb(
 input in,
    input [1:0] state,
    output [1:0] next_state,
    output out); //

    parameter A=0, B=1, C=2, D=3;

   // reg state[1:0];
    //reg next_state[1:0];

always @ (*) begin
	case(state)
		A:begin
			if(in == 1'b0)begin
				next_state <= A;
			end
			else begin
				next_state <= B;
			end
		end
		B:begin
			if(in == 1'b0)begin
				next_state <= C;
			end
			else begin
				next_state <= B;
			end
		end
		C:begin
			if(in == 1'b0)begin
				next_state <= A;
			end
			else begin
				next_state <= D;
			end
		end
		D:begin
			if(in == 0)begin
				next_state <= C;
			end
			else begin
				next_state <= B;
			end
		end
	endcase
end

always @ (*) begin
	if(state == A)begin
		out = 0;
	end else if(state == B)begin
		out = 0;
	end else if(state == C)begin
		out = 0;
	end else begin
		out = 1;
	end
end

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值