p6p
11/9/2015 - 8:25 AM

keyboardpad.vhd

library ieee;
use ieee.std_logic_1164.all;

entity P4 is
port(ctrl_74138:out std_logic_vector(3 downto 1);
	 keyboard_in:in std_logic_vector(3 downto 1);
	 led7:out std_logic_vector(7 downto 0);
	 clk:in std_logic);
end P4;

--keyborad
--key down is '0'
--rk> 1 2 3     keyboard_f
--000 1 2 3		1  2  3
--001 4 5 6		4  5  6
--010 7 8 9		7  8  9
--011 * 0 #		10 0  11
architecture a of P4 is
	type keyboard_f is array(3 downto 0) of std_logic_vector(9 downto 1);
	type led_7_data is array (-3 to 9) of std_logic_vector(7 downto 0);
	type int_array is array(0 to 5) of integer;
begin
	process(clk)
	variable f, flag:integer:=0;
	--led7*6  _ _ _ _ _ _
	--number  0 1 2 3 4 5
	variable number:int_array:=(-1,-1,-1,-1,-1,-1);   -- 
	variable keyboard_flag:keyboard_f:=("111111111","111111111","111111111","111111111");
	--"=","-",null,"0","1","2","3","4","5","6","7","8","9"
	variable data_7:led_7_data:=("10000010","00000010","00000000","11111100","01100000","11011010","11110010","01100110","10110110","10111110","11100100","11111110","11110110");
	variable sum: integer;
	begin
		if clk'event and clk = '1' then
			if f = 10000 then
				f:=0;
				if flag = 0 or flag = 2 then
					for i in 1 to 9 loop --0,10,11 don't care
						if keyboard_flag(flag)(i) = '0' then
							number(flag) := i;
						end if;
					end loop;
				end if;
			else
				if f = 0 then 
					ctrl_74138 <= "000";
					led7 <= data_7(number(0));
				elsif f = 1000 then 
					keyboard_flag(flag)(3 downto 1) := keyboard_in;
				elsif f = 2000 then 
					ctrl_74138 <= "001";
					led7 <= data_7(number(1));--"+"->"-"
				elsif f = 3000 then 
					keyboard_flag(flag)(6 downto 4) := keyboard_in;
				elsif f = 4000 then 
					ctrl_74138 <= "010";
					led7 <= data_7(number(2));
				elsif f = 5000 then 
					keyboard_flag(flag)(9 downto 7) := keyboard_in;
				elsif f = 6000 then 
					ctrl_74138 <= "011";
					led7 <= data_7(number(3));
				elsif f = 7000 then
					if keyboard_in(1)='0' then
						number(1) := -2;
						flag := 2;
					end if;
					if keyboard_in(2)='0' then
						flag := 0;
						for i in 0 to 5 loop
							number(i) := -1;
						end loop;
						
					end if;
					if keyboard_in(3)='0' then
						number(3) := -3;
						sum := number(0) + number(2);
						if sum >= 10 then
							number(4) := 1;
							number(5) := sum - 10;
						else
							number(4) := sum;
						end if;
					end if;
				elsif f = 8000 then
					ctrl_74138 <= "100";
					led7 <= data_7(number(4));
				elsif f = 9000 then
					ctrl_74138 <= "101";
					led7 <= data_7(number(5));
				end if;
				f := f+1;
			end if;
			
		end if;
		

		
		
	end process;
	
	
end a;