Advertisement
nontawat1996

Assignment-VHDL-2

Feb 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 3.70 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 using
  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.            SingA : in  STD_LOGIC;
  36.            SingB : in  STD_LOGIC;
  37.               Operator : in STD_LOGIC;
  38.               clk : in STD_LOGIC;
  39.               seg : out STD_LOGIC_VECTOR(6 downto 0);
  40.               com : out STD_LOGIC_VECTOR(2 downto 0);
  41.            C : out  STD_LOGIC_VECTOR (4 downto 0));
  42. end addAndSub;
  43.  
  44. architecture Behavioral of addAndSub is
  45.     signal x : integer := 0;
  46.     signal count : integer := 0;
  47.     signal comtemp : std_logic_vector(2 downto 0) := "000";
  48.     signal segtemp : std_logic_vector(6 downto 0) := "0000000";
  49. begin
  50.    
  51.     process(clk, count)
  52.         variable AInt, BInt, AB, digit, ten: integer;
  53.     begin
  54.             if clk'event and clk = '1' then
  55.                 AInt := conv_integer(A(2 downto 0));
  56.                 BInt := conv_integer(B(2 downto 0));
  57.                
  58.                 if A(3) = '1' then AInt := AInt*(-1);
  59.                 end if;
  60.                
  61.                 if B(3) = '1' then BInt := BInt*(-1);
  62.                 end if;
  63.                
  64.                 AB := AInt + BInt;
  65.                 C <= conv_std_logic_vector(AB, 4);
  66.  
  67.                 count <= count+1;
  68.  
  69.                 if count = 100000 then --digit
  70.                     comtemp <= "110";
  71.                     segtemp <= "0000000";
  72.  
  73.                     if AB < 0 then
  74.                         AB := AB*(-1);
  75.                     end if ;
  76.  
  77.                     if AB >= 30 then
  78.                         AB := AB - 30;
  79.                     elsif AB >= 20 then
  80.                         AB := AB - 20;
  81.                     elsif AB >= 10 then
  82.                         AB := AB - 10;
  83.                     else
  84.                         AB := AB;
  85.                     end if ;
  86.  
  87.                     digit := AB;
  88.  
  89.                     if digit = 0 then
  90.                         segtemp <= "0111111";
  91.                     elsif digit = 1 then
  92.                         segtemp <= "0000110";
  93.                     elsif digit = 2 then
  94.                         segtemp <= "1011011";
  95.                     elsif digit = 3 then
  96.                         segtemp <= "1001111";
  97.                     elsif digit = 4 then
  98.                         segtemp <= "1100110";
  99.                     elsif digit = 5 then
  100.                         segtemp <= "1101101";
  101.                     elsif digit = 6 then
  102.                         segtemp <= "1111101";
  103.                     elsif digit = 7 then
  104.                         segtemp <= "0000111";
  105.                     elsif digit = 8 then
  106.                         segtemp <= "1111111";
  107.                     elsif digit = 9 then
  108.                         segtemp <= "1101111";
  109.                     else
  110.                         segtemp <= "0000000";
  111.                     end if ;
  112.                 end if;
  113.                 if count = 200000 then --ten
  114.                     comtemp <= "101";
  115.                     segtemp <= "0000000";
  116.  
  117.                     if AB < 0 then
  118.                         AB := AB*(-1);
  119.                     end if ;
  120.  
  121.                     if AB >= 30 then
  122.                         digit := 3;
  123.                     elsif AB >= 20 then
  124.                         digit := 2;
  125.                     elsif AB >= 10 then
  126.                         digit := 1;
  127.                     else
  128.                         digit := 0;
  129.                     end if ;
  130.  
  131.                     if digit = 0 then
  132.                         segtemp <= "0111111";
  133.                     elsif digit = 1 then
  134.                         segtemp <= "0000110";
  135.                     elsif digit = 2 then
  136.                         segtemp <= "1011011";
  137.                     elsif digit = 3 then
  138.                         segtemp <= "1001111";
  139.                     else
  140.                         segtemp <= "0000000";
  141.                     end if ;
  142.                 end if;
  143.                 if count = 300000 then
  144.                     comtemp <= "011";
  145.  
  146.                     if AB < 0 then
  147.                         segtemp <= "1000000";
  148.                     else
  149.                         segtemp <= "0000000";
  150.                     end if ;
  151.  
  152.                 count <= 0;
  153.                 end if;
  154.            
  155.             end if;
  156.     end process;
  157.     com <= comtemp;
  158.     seg <= segtemp;
  159.  
  160. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement