⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 v14_2.vhd

📁 台湾全华科技VHDL教材实例
💻 VHD
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity PinCheck is
    port(InD    	: in  	std_logic_vector(3 downto 0);
         InPinN     : in    std_logic;
         InData     : in    std_logic;
    	 nMatch    	: out 	std_logic;
         DEnter 	: in  	std_logic;
         Reset   	: in  	std_logic;
         RstPin   	: in  	std_logic;
         Clk     	: in  	std_logic);
end PinCheck;

architecture A_PinCheck of PinCheck is

	type MState  is (Idle,LoadPin,LoadOldPin,InPin,CheckPin);
	type PinArray is array(0 to 3) of std_logic_vector(InD'range);
	signal PinDigit     : PinArray;
	signal InputDigit   : PinArray;
	Signal PresentState : MState;
    signal PinCnt       : integer range 0 to 4;
    signal InputCnt     : integer range 0 to 4;
    signal nMatchi      : std_logic;
    signal nMatchi1     : std_logic;

begin
	
	process(RstPin,Clk)
	begin
		if RstPin = '0' then
			for i in 0 to 3 loop
				PinDigit(i) <= (others => '0');
			end loop;
		elsif Clk = '1' and Clk'event then 
			if (PresentState = LoadPin and PinCnt < 4) then
			    PinDigit(CONV_INTEGER(PinCnt)) <= InD;
			end if;
		end if;
	end process;

	process(Reset,Clk)
	begin
		if Reset = '0' then
			PresentState <= idle;
		elsif Clk = '1' and Clk'event then
			case PresentState is
				when idle =>
					if InPinN = '0' then
						PresentState <= LoadOldPin;
					elsif InData = '0' then
						PresentState <= InPin;
					end if;
				when LoadOldPin => 
					if DEnter = '0' then
						if nMatchi1 = '0' then
							PresentState <= LoadPin;
						else
							PresentState <= idle;
						end if;
					end if;
				when LoadPin => 
					if DEnter = '0' then
						PresentState <= idle;
					end if;
				when InPin => 
					if InputCnt = 3 then
						PresentState <= CheckPin;
					end if;
				when CheckPin => 
					if DEnter = '0' then
						PresentState <= idle;
					end if;
				when others => 
					PresentState <= idle;
			end case; 
		end if;
	end process;
	
	process(Reset,PresentState,Clk)
	begin
		if Reset = '0' or PresentState = idle then
		    PinCnt <= 0;
			InputCnt <= 0;
		elsif Clk = '1' and Clk'event then 
			if (PresentState = LoadPin and PinCnt < 4) then
			    PinCnt <= PinCnt + 1;
			end if;
			if (PresentState = InPin and InputCnt < 4) or 
			   (PresentState = LoadOldPin and InputCnt < 4) then
			    InputCnt <= InputCnt + 1;
			    InputDigit(CONV_INTEGER(InputCnt)) <= InD;
			end if;
		end if;
	end process;
	
	process(Reset,PresentState)
	begin
		if Reset = '0' or PresentState = Idle then
			nMatchi <= '1';
		elsif (PresentState = CheckPin) then 
			for i in 0 to 3 loop
				if InputDigit(i) = PinDigit(i) then
					nMatchi <= '0';
				else
					nMatchi <= '1';
					exit;
				end if;
			end loop;
		end if;
	end process;
	
	process(Reset,Clk)
	begin
		if Reset = '0' then
			nMatchi1 <= '1';
		elsif Clk = '0' and Clk'event then
		    if (PresentState = LoadOldPin) and (InputCnt = 4) then 
				for i in 0 to 3 loop
					if InputDigit(i) = PinDigit(i) then
						nMatchi1 <= '0';
					else
						nMatchi1 <= '1';
						exit;
					end if;
				end loop;
			else
				nMatchi1 <= '1';
			end if;
		end if;
	end process;
	
	nMatch <= '0' when nMatchi = '0' and DEnter = '0' else
	          '1';
    
end A_PinCheck;    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -