Advertisement
nontawat1996

Assignment-VHDL-Passed-Code

Feb 28th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.93 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.             CSum : out STD_LOGIC_VECTOR(4 downto 0);
  43.             OutLEDA : out  STD_LOGIC_VECTOR (3 downto 0);
  44.             OutLEDB : out  STD_LOGIC_VECTOR (3 downto 0));
  45. end addAndSub;
  46.  
  47. architecture Behavioral of addAndSub is
  48.     signal delay : integer := 0;
  49.     signal countDelay : integer := 0;
  50.     signal count : integer := 0;
  51.     signal comtemp : std_logic_vector(2 downto 0) := "000";
  52.     signal segtemp : std_logic_vector(6 downto 0) := "0000000";
  53.     signal tempSignA : std_logic := '0';
  54.     signal tempSignB : std_logic := '0';
  55.     signal tempOperator : std_logic := '0';
  56.     signal tempA : std_logic_vector(3 downto 0) := "0000";
  57.     signal tempB : std_logic_vector(3 downto 0) := "0000";
  58. begin
  59.  
  60.     process(clk, count, A, B)
  61.         variable AInt, BInt, AB, digit, ten: integer;
  62.     begin
  63.             if clk'event and clk = '1' then
  64.                 -----------------------------
  65.                 if delay = 0 then
  66.                     countDelay <= 0;
  67.  
  68.                     if SignA = '1' then
  69.                         delay <= 1;
  70.                         tempSignA <= NOT tempSignA;
  71.                     end if ;
  72.  
  73.                     if SignB = '1' then
  74.                         delay <= 1;
  75.                         tempSignB <= NOT tempSignB;
  76.                     end if ;
  77.  
  78.                     if Operator = '1' then
  79.                         delay <= 1;
  80.                         tempOperator <= NOT tempOperator;
  81.                     end if ;
  82.  
  83.                 else
  84.                     countDelay <= countDelay + 1;
  85.  
  86.                     if countDelay = 5000000 then
  87.                         delay <= 0;
  88.                         countDelay <= 0;
  89.                     end if ;
  90.                 end if ;
  91.  
  92.  
  93.                 -----------------------------
  94.                 tempA <= NOT A;
  95.                 tempB <= NOT B;
  96.                 OutLEDA <= tempA;
  97.                 OutLEDB <= tempB;
  98.  
  99.                 AInt := conv_integer(tempA(3 downto 0));
  100.                 BInt := conv_integer(tempB(3 downto 0));
  101.                
  102.                 if tempSignA = '1' then AInt := AInt*(-1);
  103.                 end if;
  104.                
  105.                 if tempSignB = '1' then BInt := BInt*(-1);
  106.                 end if;
  107.  
  108.                 if tempOperator = '1' then BInt := BInt*(-1);
  109.                 end if;
  110.  
  111.                 OutLEDSign <= NOT tempSignA& NOT tempOperator& NOT tempSignB;
  112.                
  113.                 AB := 0;
  114.  
  115.                 AB := AInt + BInt;
  116.                 CSum <= conv_std_logic_vector(AB, 5);
  117.  
  118.                 count <= count+1;
  119.  
  120.                 if count = 100000 then --digit
  121.                     comtemp <= "110";
  122.  
  123.                     if AB < 0 then
  124.                         AB := AB*(-1);
  125.                     end if ;
  126.  
  127.                     if AB >= 30 then
  128.                         digit := AB - 30;
  129.                     elsif AB >= 20 then
  130.                         digit := AB - 20;
  131.                     elsif AB >= 10 then
  132.                         digit := AB - 10;
  133.                     else
  134.                         digit := AB;
  135.                     end if ;
  136.  
  137.                     if digit = 0 then
  138.                         segtemp <= "0111111";
  139.                     elsif digit = 1 then
  140.                         segtemp <= "0000110";
  141.                     elsif digit = 2 then
  142.                         segtemp <= "1011011";
  143.                     elsif digit = 3 then
  144.                         segtemp <= "1001111";
  145.                     elsif digit = 4 then
  146.                         segtemp <= "1100110";
  147.                     elsif digit = 5 then
  148.                         segtemp <= "1101101";
  149.                     elsif digit = 6 then
  150.                         segtemp <= "1111101";
  151.                     elsif digit = 7 then
  152.                         segtemp <= "0000111";
  153.                     elsif digit = 8 then
  154.                         segtemp <= "1111111";
  155.                     else
  156.                         segtemp <= "1101111";
  157.                     end if ;
  158.                 elsif count = 200000 then --ten
  159.                     comtemp <= "101";
  160.  
  161.                     if AB < 0 then
  162.                         AB := AB*(-1);
  163.                     end if ;
  164.  
  165.                     if AB >= 30 then
  166.                         digit := 3;
  167.                     elsif AB >= 20 then
  168.                         digit := 2;
  169.                     elsif AB >= 10 then
  170.                         digit := 1;
  171.                     else
  172.                         digit := 0;
  173.                     end if ;
  174.  
  175.                     if digit = 0 then
  176.                         segtemp <= "0111111";
  177.                     elsif digit = 1 then
  178.                         segtemp <= "0000110";
  179.                     elsif digit = 2 then
  180.                         segtemp <= "1011011";
  181.                     elsif digit = 3 then
  182.                         segtemp <= "1001111";
  183.                     else
  184.                         segtemp <= "0000000";
  185.                     end if ;
  186.                 elsif count = 300000 then
  187.                     comtemp <= "011";
  188.  
  189.                     if AB < 0 then
  190.                         segtemp <= "1000000";
  191.                     else
  192.                         segtemp <= "0000000";
  193.                     end if ;
  194.  
  195.                 count <= 0;
  196.                 end if;
  197.            
  198.             end if;
  199.     end process;
  200.     com <= comtemp;
  201.     seg <= segtemp;
  202.  
  203. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement