一段式、两段式和三段式状态机

29 篇文章 28 订阅

这里写自定义目录标题

在这里插入图片描述
这张图片是mealy型状态机的结构,Moore型类似,输出与输入无关。

一段式

module moduleName (
	input clk,
	input rst_n,
	input in,

	output reg [3:0]	out
);

localparam s1=4'd0, s2=4'd1, s3=4'd2, s4=4'd3, s5=4'd4;
reg [3:0] cur_state;
reg [3:0] next_state;

always @(posedge clk) begin
	if(!rst_n) begin
		cur_state <= s1;
	end
	else	begin
		case (cur_state)
			s1:begin
				if(in)
					 cur_state <= s2;
				out <= cur_state;
			end
			s2:begin
				if(~in)
					 cur_state <= s3;
				out <= cur_state;
			end
			s3:begin
				if(in)
					 cur_state <= s4;
				out <= cur_state;
			end
			s4:begin
				if(~in)
					 cur_state <= s5;
				out <= cur_state;
			end
			s5:begin
				if(in)
					 cur_state <= s1;
				out <= cur_state;
			end
			default: begin
				cur_state <= s1;
				out <= cur_state;
			end
		endcase
	end
end

endmodule

如果将输出单独用组合逻辑表示,本质上也是一段式状态机
在这里插入图片描述
缺点是,输出比状态晚一个时钟

两段式

module moduleName (
	input clk,
	input rst_n,
	input in,

	output reg [3:0]	out
);

localparam s1=4'd0, s2=4'd1, s3=4'd2, s4=4'd3, s5=4'd4;
reg [3:0] cur_state;
reg [3:0] next_state;

// ========================================
// 二段式
// ========================================
always @(posedge clk) begin
	if(!rst_n)	begin
		cur_state <= s1;
	end
	else	begin
		cur_state <= next_state;
	end
end

always @(*) begin
	case (cur_state)
			s1:begin
				if(in)
					 cur_state = s2;
				out = cur_state;
			end
			s2:begin
				if(~in)
					 cur_state = s3;
				out = cur_state;
			end
			s3:begin
				if(in)
					 cur_state = s4;
				out = cur_state;
			end
			s4:begin
				if(~in)
					 cur_state = s5;
				out = cur_state;
			end
			s5:begin
				if(in)
					 cur_state = s1;
				out = cur_state;
			end
			default: begin
				cur_state = s1;
				out = cur_state;
			end
		endcase
end

如果将输出单独用组合逻辑表示,本质上也是二段式状态机

在这里插入图片描述
输出和状态的时序是一致的

三段式

module moduleName (
	input clk,
	input rst_n,
	input in,

	output reg [3:0]	out
);

localparam s1=4'd0, s2=4'd1, s3=4'd2, s4=4'd3, s5=4'd4;
reg [3:0] cur_state;
reg [3:0] next_state;


// ========================================
// 三段式
// ========================================

always @(posedge clk) begin
	if(!rst_n)	begin
		cur_state <= s1;
	end
	else	begin
		cur_state <= next_state;
	end
end


always @(*) begin
	case (cur_state)
		s1:begin
			if(in)
				next_state = s2;
		end
		s2:begin
			if(~in)
				next_state = s3;
		end
		s3:begin
			if(in)
				next_state = s4;
		end
		s4:begin
			if(~in)
				next_state = s5;
		end
		s5:begin
			if(in)
				next_state = s1;
		end
		default: begin
			next_state = s1;
		end
	endcase
end

`ifdef SEQUENTIAL_LOGIC
	always @(posedge clk) begin
		if(!rst_n)	begin
			out <= 4'd0;
		end
		else	begin
			case (next_state)
				s1:begin
					out <= s1;
				end
				s2:begin
					out <= s2;
				end
				s3:begin
					out <= s3;
				end
				s4:begin
					out <= s4;
				end
				s5:begin
					out <= s5;
				end
				default: begin
					out <= s1;
				end
			endcase
		end
	end
`else
	always @(*) begin
	if(!rst_n)	begin
		out <= 4'd0;
	end
	else	begin
		case (cur_state)
			s1:begin
				out <= s1;
			end
			s2:begin
				out <= s2;
			end
			s3:begin
				out <= s3;
			end
			s4:begin
				out <= s4;
			end
			s5:begin
				out <= s5;
			end
			default: begin
				out <= s1;
			end
		endcase
	end
end

`endif

在这里插入图片描述


都看到这儿了,点个赞呗
||
\/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值