Advertisement
ARayofLight

Untitled

Nov 15th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 6.54 KB | None | 0 0
  1. --------------HALF ADDER---------------------
  2. library IEEE;
  3. use IEEE.STD_LOGIC_1164.ALL;
  4.  
  5. entity HalfAdder is
  6.   Port ( A, B : in STD_LOGIC;
  7.          Sum, Carry : out STD_LOGIC);
  8. end HalfAdder;
  9.  
  10. architecture Behavioral of HalfAdder is
  11. begin
  12.   process(A, B)
  13.   begin
  14.     Sum <= A XOR B;    -- XOR gives the sum bit
  15.     Carry <= A AND B;  -- AND gives the carry bit
  16.   end process;
  17. end Behavioral;
  18.  
  19.    
  20.  
  21.  
  22. ------------------ALU--------------------------
  23. library IEEE;
  24. use IEEE.STD_LOGIC_1164.ALL;
  25. use IEEE.STD_LOGIC_unsigned.ALL;
  26.  
  27. entity FourBitALU is
  28.   Port ( A, B : in STD_LOGIC_VECTOR(3 downto 0);
  29.          opcode : in STD_LOGIC_VECTOR(2 downto 0);
  30.          result : out STD_LOGIC_VECTOR(3 downto 0));
  31. end FourBitALU;
  32.  
  33. architecture Behavioral of FourBitALU is
  34.   signal temp_result : STD_LOGIC_VECTOR(4 downto 0); -- To handle overflow
  35. begin
  36.  
  37.   process(A, B, opcode)
  38.   begin
  39.     case opcode is
  40.       when "000" => -- ADD
  41.         temp_result <= ('0' & A) + ('0' & B);
  42.       when "001" => -- SUBTRACT
  43.         temp_result <= ('0' & A) - ('0' & B);
  44.       when "010" => -- AND
  45.         temp_result <= A AND B;
  46.       when "011" => -- OR
  47.         temp_result <= A OR B;
  48.       when "100" => -- XOR
  49.         temp_result <= A XOR B;
  50.       when "101" => -- NAND
  51.         temp_result <= A NAND B;
  52.       when "110" => -- NOR
  53.         temp_result <= A NOR B;
  54.       when "111" => -- NOT
  55.         temp_result <= not A;
  56.       when others =>
  57.         temp_result <= (others => '0');
  58.     end case;
  59.  
  60.     result <= temp_result(3 downto 0); -- Extract the 4-bit result
  61.   end process;
  62.  
  63. end Behavioral;
  64.  
  65.        
  66.        
  67.        
  68.        
  69.        
  70. -------------------COUNTER--------------------------
  71. library IEEE;
  72. use IEEE.STD_LOGIC_1164.ALL;
  73. use IEEE.STD_LOGIC_unsigned.ALL;
  74.  
  75. entity UpDownCounter is
  76.   Port ( clk : in STD_LOGIC;
  77.          rst : in STD_LOGIC;
  78.          up_down : in STD_LOGIC;
  79.          count : out STD_LOGIC_VECTOR(3 downto 0));
  80. end UpDownCounter;
  81.  
  82. architecture Behavioral of UpDownCounter is
  83.   signal counter : STD_LOGIC_VECTOR(3 downto 0) := "0000";
  84. begin
  85.  
  86.   process(clk, rst)
  87.   begin
  88.     if rst = '1' then
  89.       counter <= "0000"; -- Reset the counter to 0
  90.     elsif rising_edge(clk) then
  91.       if up_down = '1' then
  92.         -- Up counter
  93.         counter <= counter + 1;
  94.       else
  95.         -- Down counter
  96.         counter <= counter - 1;
  97.       end if;
  98.     end if;
  99.   end process;
  100.  
  101.   count <= counter;
  102.  
  103. end Behavioral;
  104.          
  105.          
  106.          
  107.          
  108.          
  109.          
  110. --------------Seven Segment------------------
  111. library IEEE;
  112. use IEEE.STD_LOGIC_1164.ALL;
  113. use IEEE.STD_LOGIC_unsigned.ALL;
  114.  
  115. entity SevenSegmentDisplay is
  116.   Port ( clk : in STD_LOGIC;
  117.          rst : in STD_LOGIC;
  118.          seg : out STD_LOGIC_VECTOR(6 downto 0);
  119.          anode : out STD_LOGIC);
  120. end SevenSegmentDisplay;
  121.  
  122. architecture Behavioral of SevenSegmentDisplay is
  123.   signal counter : INTEGER range 0 to 9 := 0;
  124.   signal display_pattern : STD_LOGIC_VECTOR(6 downto 0);
  125.  
  126. begin
  127.  
  128.   process(clk, rst)
  129.   begin
  130.     if rst = '1' then
  131.       counter <= 0;
  132.       display_pattern <= "0000000"; -- Reset to display pattern for 0
  133.     elsif rising_edge(clk) then
  134.       if counter < 9 then
  135.         counter <= counter + 1;
  136.       else
  137.         counter <= 0;
  138.       end if;
  139.  
  140.       case counter is
  141.         when 0 => display_pattern <= "0000001"; -- 0
  142.         when 1 => display_pattern <= "1001111"; -- 1
  143.         when 2 => display_pattern <= "0010010"; -- 2
  144.         when 3 => display_pattern <= "0000110"; -- 3
  145.         when 4 => display_pattern <= "1001100"; -- 4
  146.         when 5 => display_pattern <= "0100100"; -- 5
  147.         when 6 => display_pattern <= "0100000"; -- 6
  148.         when 7 => display_pattern <= "0001111"; -- 7
  149.         when 8 => display_pattern <= "0000000"; -- 8
  150.         when 9 => display_pattern <= "0000100"; -- 9
  151.         when others => display_pattern <= "1111111"; -- All segments off for invalid input
  152.       end case;
  153.     end if;
  154.   end process;
  155.  
  156.   seg <= display_pattern;
  157.   anode <= '1'; -- Anode control signal (common anode display)
  158.  
  159. end Behavioral;
  160.  
  161.  
  162.          
  163.          
  164.          
  165.          
  166. -----------------FIFO-------------------------
  167. library IEEE;
  168. use IEEE.STD_LOGIC_1164.ALL;
  169. use IEEE.STD_LOGIC_unsigned.ALL;
  170.  
  171. entity FourBitFIFO is
  172.   Port ( clk : in STD_LOGIC;
  173.          rst : in STD_LOGIC;
  174.          wr_en : in STD_LOGIC;
  175.          rd_en : in STD_LOGIC;
  176.          data_in : in STD_LOGIC_VECTOR(3 downto 0);
  177.          data_out : out STD_LOGIC_VECTOR(3 downto 0);
  178.          empty : inout STD_LOGIC;
  179.          full : inout STD_LOGIC);
  180. end FourBitFIFO;
  181.  
  182. architecture Behavioral of FourBitFIFO is
  183.   type MemoryArray is array (0 to 7) of STD_LOGIC_VECTOR(3 downto 0);
  184.   signal fifo_memory : MemoryArray := (others => "0000");
  185.   signal read_ptr, write_ptr : integer range 0 to 7 := 0;
  186. begin
  187.  
  188.   process(clk, rst)
  189.   begin
  190.     if rst = '1' then
  191.       read_ptr <= 0;
  192.       write_ptr <= 0;
  193.       fifo_memory <= (others => "0000");
  194.     elsif rising_edge(clk) then
  195.       -- Write operation
  196.       if wr_en = '1' and not full = '1' then
  197.         fifo_memory(write_ptr) <= data_in;
  198.         write_ptr <= write_ptr + 1;
  199.       end if;
  200.  
  201.       -- Read operation
  202.       if rd_en = '1' and not empty = '1' then
  203.         data_out <= fifo_memory(read_ptr);
  204.         read_ptr <= read_ptr + 1;
  205.       end if;
  206.     end if;
  207.   end process;
  208.  
  209.   empty <= '1' when read_ptr = write_ptr else '0';
  210.   full <= '1' when ((write_ptr + 1) mod 8) = read_ptr else '0';
  211.  
  212. end Behavioral;
  213.  
  214.  
  215.        
  216.        
  217.        
  218. -----------------UNIVERSAL SHIFT REGISTER-----------------------
  219. library IEEE;
  220. use IEEE.STD_LOGIC_1164.ALL;
  221.  
  222. entity UniversalShiftRegister is
  223.   Port ( clk : in STD_LOGIC;
  224.          load : in STD_LOGIC;
  225.          shift_left : in STD_LOGIC;
  226.          shift_right : in STD_LOGIC;
  227.          data_in : in STD_LOGIC_VECTOR(3 downto 0);
  228.          data_out : out STD_LOGIC_VECTOR(3 downto 0));
  229. end UniversalShiftRegister;
  230.  
  231. architecture Behavioral of UniversalShiftRegister is
  232.   signal shift_register : STD_LOGIC_VECTOR(3 downto 0) := "0000";
  233. begin
  234.  
  235.   process(clk)
  236.   begin
  237.     if rising_edge(clk) then
  238.       if load = '1' then
  239.         shift_register <= data_in; -- Parallel load
  240.       else
  241.         if shift_left = '1' then
  242.           shift_register <= '0' & shift_register(3 downto 1); -- Left shift
  243.         elsif shift_right = '1' then
  244.           shift_register <= shift_register(2 downto 0) & '0'; -- Right shift
  245.         end if;
  246.       end if;
  247.     end if;
  248.   end process;
  249.  
  250.   data_out <= shift_register;
  251.  
  252. end Behavioral;
  253.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement