Advertisement
nontawat1996

Assignment-VHDL-3-Last

Feb 27th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.57 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer:
  4. --
  5. -- Create Date:    23:26:17 02/27/2016
  6. -- Design Name:
  7. -- Module Name:    addAndSub - Behavioral
  8. -- Project Name:
  9. -- Target Devices:
  10. -- Tool versions:
  11. -- Description:
  12. --
  13. -- Dependencies:
  14. --
  15. -- Revision:
  16. -- Revision 0.01 - File Created
  17. -- Additional Comments:
  18. --
  19. ----------------------------------------------------------------------------------
  20. library IEEE;
  21. use IEEE.STD_LOGIC_1164.ALL;
  22. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  23. use IEEE.std_logic_arith.ALL;
  24. -- Uncomment the following library declaration if uSign
  25. -- arithmetic functions with Signed or Unsigned values
  26. --use IEEE.NUMERIC_STD.ALL;
  27.  
  28. -- Uncomment the following library declaration if instantiating
  29. -- any Xilinx primitives in this code.
  30. --library UNISIM;
  31. --use UNISIM.VComponents.all;
  32.  
  33. entity addAndSub is
  34.     Port ( A,B : in  STD_LOGIC_VECTOR (3 downto 0);
  35.             SignA : in  STD_LOGIC;
  36.             SignB : in  STD_LOGIC;
  37.             Operator : in STD_LOGIC;
  38.             OutLEDSign : out std_logic_vector(2 downto 0);
  39.             clk : in STD_LOGIC;
  40.             seg : out STD_LOGIC_VECTOR(6 downto 0);
  41.             com : out STD_LOGIC_VECTOR(2 downto 0);
  42.             C : out  STD_LOGIC_VECTOR (4 downto 0));
  43. end addAndSub;
  44.  
  45. architecture Behavioral of addAndSub is
  46.     signal delay : integer := 0;
  47.     signal countDelay : integer := 0;
  48.     signal count : integer := 0;
  49.     signal comtemp : std_logic_vector(2 downto 0) := "000";
  50.     signal segtemp : std_logic_vector(6 downto 0) := "0000000";
  51.     signal tempSignA : std_logic := '0';
  52.     signal tempSignB : std_logic := '0';
  53.     signal tempOperator : std_logic := '0';
  54. begin
  55.  
  56.     process(clk, count)
  57.         variable AInt, BInt, AB, digit, ten: integer;
  58.     begin
  59.             if clk'event and clk = '1' then
  60.                 -----------------------------
  61.                 if delay = 0 then
  62.                     countDelay <= 0;
  63.  
  64.                     if SignA = '1' then
  65.                         delay <= 1;
  66.                         tempSignA <= NOT tempSignA;
  67.                     end if ;
  68.  
  69.                     if SignB = '1' then
  70.                         delay <= 1;
  71.                         tempSignB <= NOT tempSignB;
  72.                     end if ;
  73.  
  74.                     if Operator = '1' then
  75.                         delay <= 1;
  76.                         tempOperator <= NOT tempOperator;
  77.                     end if ;
  78.  
  79.                 else
  80.                     countDelay <= countDelay + 1;
  81.  
  82.                     if countDelay = 5000000 then
  83.                         delay <= 0;
  84.                         countDelay <= 0;
  85.                     end if ;
  86.                 end if ;
  87.  
  88.  
  89.                 -----------------------------
  90.                 AInt := conv_integer(A(3 downto 0));
  91.                 BInt := conv_integer(B(3 downto 0));
  92.                
  93.                 if tempSignA = '1' then AInt := AInt*(-1);
  94.                 end if;
  95.                
  96.                 if tempSignB = '1' then BInt := BInt*(-1);
  97.                 end if;
  98.  
  99.                 if tempOperator = '1' then BInt := BInt*(-1);
  100.                 end if;
  101.  
  102.                 OutLEDSign <= tempSignA&tempOperator&tempSignB;
  103.                
  104.                 AB := AInt + BInt;
  105.                 C <= conv_std_logic_vector(AB, 5);
  106.  
  107.                 count <= count+1;
  108.  
  109.                 if count = 100000 then --digit
  110.                     comtemp <= "110";
  111.  
  112.                     if AB < 0 then
  113.                         AB := AB*(-1);
  114.                     end if ;
  115.  
  116.                     if AB >= 30 then
  117.                         digit := AB - 30;
  118.                     elsif AB >= 20 then
  119.                         digit := AB - 20;
  120.                     elsif AB >= 10 then
  121.                         digit := AB - 10;
  122.                     else
  123.                         digit := AB;
  124.                     end if ;
  125.  
  126.                     if digit = 0 then
  127.                         segtemp <= "0111111";
  128.                     elsif digit = 1 then
  129.                         segtemp <= "0000110";
  130.                     elsif digit = 2 then
  131.                         segtemp <= "1011011";
  132.                     elsif digit = 3 then
  133.                         segtemp <= "1001111";
  134.                     elsif digit = 4 then
  135.                         segtemp <= "1100110";
  136.                     elsif digit = 5 then
  137.                         segtemp <= "1101101";
  138.                     elsif digit = 6 then
  139.                         segtemp <= "1111101";
  140.                     elsif digit = 7 then
  141.                         segtemp <= "0000111";
  142.                     elsif digit = 8 then
  143.                         segtemp <= "1111111";
  144.                     else
  145.                         segtemp <= "1101111";
  146.                     end if ;
  147.                 elsif count = 200000 then --ten
  148.                     comtemp <= "101";
  149.  
  150.                     if AB < 0 then
  151.                         AB := AB*(-1);
  152.                     end if ;
  153.  
  154.                     if AB >= 30 then
  155.                         digit := 3;
  156.                     elsif AB >= 20 then
  157.                         digit := 2;
  158.                     elsif AB >= 10 then
  159.                         digit := 1;
  160.                     else
  161.                         digit := 0;
  162.                     end if ;
  163.  
  164.                     if digit = 0 then
  165.                         segtemp <= "0111111";
  166.                     elsif digit = 1 then
  167.                         segtemp <= "0000110";
  168.                     elsif digit = 2 then
  169.                         segtemp <= "1011011";
  170.                     elsif digit = 3 then
  171.                         segtemp <= "1001111";
  172.                     else
  173.                         segtemp <= "0000000";
  174.                     end if ;
  175.                 elsif count = 300000 then
  176.                     comtemp <= "011";
  177.  
  178.                     if AB < 0 then
  179.                         segtemp <= "1000000";
  180.                     else
  181.                         segtemp <= "0000000";
  182.                     end if ;
  183.  
  184.                 count <= 0;
  185.                 end if;
  186.            
  187.             end if;
  188.     end process;
  189.     com <= comtemp;
  190.     seg <= segtemp;
  191.  
  192. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement