VHDL实现序列检测机

VHDL实现序列检测机,所检测的序列为"01111110",序列发生器请看上一篇文章

library ieee;
use ieee.std_logic_1164.all;

entity detect is
port( datain:in std_logic;
		clk:in std_logic;
		q:out std_logic
		);
end entity;

architecture one of detect is

type statetype is(s0,s1,s2,s3,s4,s5,s6,s7,s8);

begin
	process
		variable present_state:statetype;
	begin
		q<='0';
		case present_state is
			when s0 =>
				if datain='0' then present_state:=s1;
				else present_state:=s0;end if;
			when s1 =>
				if datain='1' then present_state:=s2;
				else present_state:=s1;end if;
			when s2 =>
				if datain='1' then present_state:=s3;
				else present_state:=s1;end if;
				--由于我们检测的序列是“01111110”,所以当中途出现0的时候,就需要重新检测
				--因此跳转到s1重新开始,s0本来检测的就是0,所以不需要从0开始
			when s3 =>
				if datain='1' then present_state:=s4;
				else present_state:=s1;end if;
			when s4 =>
				if datain='1' then present_state:=s5;
				else present_state:=s1;end if;
			when s5 =>
				if datain='1' then present_state:=s6;
				else present_state:=s1;end if;
			when s6 =>
				if datain='1' then present_state:=s7;
				else present_state:=s1;end if;
			when s7 =>
				if datain='0' then present_state:=s8;q<='1';
				else present_state:=s0;end if;
			when s8 =>
				if datain='0' then present_state:=s1;
				else present_state:=s2;end if;
		end case;
		wait until clk='1';--时钟上升沿
	end process;
	
end architecture;

但是,上面的序列检测机较为繁琐,下面展示一种更为简洁的序列检测机

library ieee;
use ieee.std_logic_1164.all;

entity detect_s is
port( clk,datain:in std_logic;
		q:out std_logic
		);
end entity;

architecture one of detect_s is

signal	reg:std_logic_vector(7 downto 0);

begin
	process(clk)
	begin
		if clk'event and clk='1' then
			reg(0)<=datain;
			reg(7 downto 1)<=reg(6 downto 0);
		end if;
		if reg="01111110" then
			q<='1';
		else
			q<='0';
		end if;
	end process;
	
end architecture;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值