VHDL 有限状态机(FSM) 代码示例

示例一 "10"检测器

library ieee;
use ieee.std_logic_1164.all;
entity FSM is
	port(Din,CLK,Reset:in std_logic;Dout:out std_logic);
end FSM;
architecture behave of FSM is
	type state is(S0,S1,S2);
	signal currentstate: state:=S0;
	signal nextstate:state:=S0;
begin
	state_trans:process(Din,currentstate)
	begin
		case currentstate is
			when S0=>if Din='1' then nextstate<=S1;else nextstate<=S0;end if;
			when S1=>if Din='0' then nextstate<=S2;else nextstate<=S1;end if;
			when S2=>if Din='0' then nextstate<=S0;else nextstate<=S1;end if;
		end case;
	end process;
	state_latch:process(CLK,reset)
	begin
		if reset='0' then
			currentstate<=S0;
		else
			if CLK'event and CLk='1' then
				currentstate<=nextstate;
			end if;
		end if;
	end process;
	Dout<='1' when currentstate=S2 else '0';
end behave;

在这里插入图片描述

示例二 十进制计数器

library ieee;
use ieee.std_logic_1164.all;
entity FSM is
	port(CLK,Reset,enable:in std_logic;Y:out std_logic_vector(3 downto 0);carry:out std_logic);
end FSM;
architecture behave of FSM is
	type state is(S0,S1,S2,S3,S4,S5,S6,S7,S8,S9);
	signal currentstate:state:=S0;
	signal nextstate:state:=S0;
begin
	process(currentstate)
	begin
		case currentstate is
			when S0=>if enable='1' then nextstate<=S1;else nextstate<=S0;end if;
			when S1=>if enable='1' then nextstate<=S2;else nextstate<=S1;end if;
			when S2=>if enable='1' then nextstate<=S3;else nextstate<=S2;end if;
			when S3=>if enable='1' then nextstate<=S4;else nextstate<=S3;end if;
			when S4=>if enable='1' then nextstate<=S5;else nextstate<=S4;end if;
			when S5=>if enable='1' then nextstate<=S6;else nextstate<=S5;end if;
			when S6=>if enable='1' then nextstate<=S7;else nextstate<=S6;end if;
			when S7=>if enable='1' then nextstate<=S8;else nextstate<=S7;end if;
			when S8=>if enable='1' then nextstate<=S9;else nextstate<=S8;end if;
			when S9=>if enable='1' then nextstate<=S0;else nextstate<=S9;end if;
		end case;
	end process;
	process(clk,reset)
	begin
		if reset='0' then
			currentstate<=S0;
		elsif CLK'event and CLK='1' then
			if enable='1' then
				Currentstate<=nextstate;
			end if;
		end if;
	end process;
	with currentstate select
		Y<="0000" when S0,
			"0001" when S1,
			"0010" when S2,
			"0011" when S3,
			"0100" when S4,
			"0101" when S5,
			"0110" when S6,
			"0111" when S7,
			"1000" when S8,
			"1001" when S9;
	carry<='1' when currentstate=S9 else '0';
end behave;

在这里插入图片描述

By-Round Moon

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Round Moon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值