Advertisement
Maleklobaski

zbytok

Dec 4th, 2023 (edited)
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 10.93 KB | None | 0 0
  1. //////// johnson counter testbench //////////
  2. LIBRARY ieee;
  3. USE ieee.std_logic_1164.ALL;
  4.  
  5. ENTITY Tb_Johnson_counter IS
  6. END Tb_Johnson_counter;
  7.  
  8. ARCHITECTURE behavior OF Tb_Johnson_counter IS
  9.  
  10.     -- Component Declaration for the Unit Under Test (UUT)
  11.  
  12.     COMPONENT Johnson_counter
  13.     PORT(
  14.          clk : IN  std_logic;
  15.          rst : IN  std_logic;
  16.          Q : OUT  std_logic_vector(3 downto 0)
  17.         );
  18.     END COMPONENT;
  19.      
  20.  
  21.    --Inputs
  22.    signal clk : std_logic := '0';
  23.    signal rst : std_logic := '0';
  24.  
  25.     --Outputs
  26.    signal Q : std_logic_vector(3 downto 0);
  27.  
  28.    -- Clock period definitions
  29.    constant clk_period : time := 10 ns;
  30.  
  31. BEGIN
  32.  
  33.     -- Instantiate the Unit Under Test (UUT)
  34.    uut: Johnson_counter PORT MAP (
  35.           clk => clk,
  36.           rst => rst,
  37.           Q => Q
  38.         );
  39.  
  40.    -- Clock process definitions
  41.    clk_process :process
  42.    begin
  43.         clk <= '0';
  44.         wait for clk_period/2;
  45.         clk <= '1';
  46.         wait for clk_period/2;
  47.    end process;
  48.  
  49.  
  50.    -- Stimulus process
  51.    stim_proc: process
  52.    begin      
  53.       -- hold reset state for 100 ns.
  54.       wait for 100 ns;
  55.  
  56.         rst <= '1';
  57.          
  58.       wait for 100 ns;
  59.          
  60.         rst <= '0';
  61.  
  62.       wait;
  63.    end process;
  64.  
  65. END;
  66. ///////////////////////////////////////////
  67.  
  68.  
  69. ////////roboti test bench ////////////////
  70.  
  71. library IEEE;
  72. use IEEE.STD_LOGIC_1164.ALL;
  73.  
  74. entity robot_mealy_tb is
  75. --  Port ( );
  76. end robot_mealy_tb;
  77.  
  78. architecture Behavioral of robot_mealy_tb is
  79.  
  80.     -- Component Declaration for the Unit Under Test (UUT)
  81.     component robot_mealy is
  82.         Port ( clk : in STD_LOGIC;
  83.                rst : in STD_LOGIC;
  84.                din : in STD_LOGIC;
  85.                dout : out STD_LOGIC);
  86.     end component robot_mealy;
  87.  
  88.     --Output
  89.    signal dout : std_logic;
  90.  
  91.    --Inputs
  92.    signal rst : std_logic := '0';
  93.    signal clk : std_logic := '0';
  94.    signal din : std_logic := '0';
  95.  
  96.    -- Clock period definition
  97.    constant clk_period : time := 50 ns;
  98.    constant tester : std_logic_vector(0 to 15) := "0010100000100101";
  99.  
  100. begin
  101.  
  102.     -- Instantiate the Unit Under Test (UUT)
  103.     UUT: robot_mealy port map (
  104.         clk => clk,
  105.         rst => rst,
  106.         din => din,
  107.         dout => dout
  108.     );
  109.    
  110.     -- Clock process definition
  111.     clk_process : process (clk)
  112.     begin
  113.         clk <= not clk after clk_period/2;
  114.     end process;
  115.        
  116.     -- Stimulus process
  117.     stim_proc: process
  118.     begin      
  119.         -- Hold reset state for 100 ns.
  120.         rst <= '1', '0' after 100 ns;
  121.        
  122.         wait until rst'event and rst = '0';
  123.        
  124.         for i in 0 to 15 loop
  125.             wait until clk'event and clk = '1';
  126.             din <= tester(i);  
  127.         end loop;
  128.        
  129.         wait;
  130.     end process;
  131.  
  132. end Behavioral;
  133. /////////////////////////////////////////
  134.    
  135. /////single port ram//////
  136. library IEEE;
  137. use IEEE.STD_LOGIC_1164.ALL;
  138. use IEEE.NUMERIC_STD.ALL;
  139.  
  140. entity single_port_RAM is
  141.     generic (depth : positive := 2; width : positive := 3);
  142.     Port ( clk : in STD_LOGIC;
  143.            we : in STD_LOGIC;
  144.            Addr : in STD_LOGIC_VECTOR(depth-1 downto 0);
  145.            Din : in STD_LOGIC_VECTOR (width-1 downto 0);
  146.            Dout : out STD_LOGIC_VECTOR (width-1 downto 0));
  147. end single_port_RAM;
  148.  
  149. /*
  150. architecture Behavioral_package of single_port_RAM is
  151.  
  152.     package mem is new work.memories generic map (depth => depth, width => width);
  153.    
  154. begin
  155.  
  156.     process(clk) is
  157.         variable RAM : mem.RAM_type;
  158.  
  159.         attribute ram_style : string;
  160.         attribute ram_style of RAM : variable is "block";
  161.     begin
  162.         if rising_edge(clk) then
  163.             if we = '1' then
  164.                 mem.write_RAM(RAM, Addr, Din);
  165.             else
  166.                 mem.read_RAM(RAM, Addr, Dout);    
  167.             end if;
  168.         end if;
  169.     end process;
  170.  
  171. end Behavioral_package;
  172. */
  173.  
  174. architecture Behavioral of single_port_RAM is
  175.  
  176.     type RAM_type is array (2**depth-1 downto 0) of std_logic_vector (width-1 downto 0);
  177.     signal RAM : RAM_type;
  178.  
  179.  
  180. begin
  181.  
  182.     process(clk) is
  183.     begin
  184.         if rising_edge(clk) then
  185.             if we = '1' then
  186.                 RAM(to_integer(unsigned(Addr))) <= Din;
  187.             end if;  
  188.         end if;
  189.     end process;
  190.    
  191.     Dout <= RAM(to_integer(unsigned(Addr)));
  192.  
  193. end Behavioral;
  194. ////////////////////////////
  195.                
  196. /////////////dual port ram////////////////
  197. library IEEE;
  198. use IEEE.STD_LOGIC_1164.ALL;
  199.  
  200.  
  201. use IEEE.NUMERIC_STD.ALL;
  202.  
  203.  
  204. entity dual_port_RAM is
  205.     generic (depth : positive := 2; width : positive := 3);
  206.     Port ( clk : in STD_LOGIC;
  207.            we : in STD_LOGIC;
  208.            rAddr : in STD_LOGIC_VECTOR(depth-1 downto 0);
  209.            wAddr : in STD_LOGIC_VECTOR(depth-1 downto 0);
  210.            Din : in STD_LOGIC_VECTOR (width-1 downto 0);
  211.            Dout : out STD_LOGIC_VECTOR (width-1 downto 0));
  212. end dual_port_RAM;
  213.  
  214. architecture Behavioral of dual_port_RAM is
  215.  
  216.     type RAM_type is array (2**depth-1 downto 0) of std_logic_vector (width-1 downto 0);
  217.     signal RAM : RAM_type;
  218.  
  219. begin
  220.  
  221.     process(clk) is
  222.     begin
  223.         if rising_edge(clk) then
  224.             if we = '1' then
  225.                 RAM(to_integer(unsigned(wAddr))) <= Din;
  226.             end if;  
  227.         end if;
  228.     end process;
  229.    
  230.     Dout <= RAM(to_integer(unsigned(rAddr)));
  231.  
  232. end Behavioral;
  233. //////////////////////
  234. ////////////debouncer///////////////////
  235. library IEEE;
  236. use IEEE.STD_LOGIC_1164.ALL;
  237.  
  238. entity debouncer is
  239.     generic (sys_freq       : natural := 100_000_000; -- in Hz
  240.              stable_time    : natural := 10);  -- in ms
  241.     Port ( clk      : in STD_LOGIC;
  242.            btn      : in STD_LOGIC;
  243.            q        : out STD_LOGIC);
  244. end debouncer;
  245.  
  246. architecture Structural of debouncer is
  247.  
  248.     -- Flip-flop D type
  249.     component dff is
  250.         Port ( clk  : in  STD_LOGIC;
  251.                d    : in  STD_LOGIC;
  252.                q    : out  STD_LOGIC);
  253.     end component dff;
  254.  
  255.     -- Flip-flop D type with enable input
  256.     component dff_en is
  257.         Port ( clk  : in  STD_LOGIC;
  258.                rst  : in  STD_LOGIC;
  259.                d    : in  STD_LOGIC;
  260.                en   : in  STD_LOGIC;
  261.                q    : out  STD_LOGIC);
  262.     end component dff_en;
  263.  
  264.     component counter is
  265.         generic (up_counter : boolean := TRUE;
  266.                  width      : positive := 8;
  267.                  init_value : natural := 0);
  268.         Port ( clk  : in STD_LOGIC;
  269.                rst  : in STD_LOGIC;
  270.                en   : in STD_LOGIC;
  271.                Y    : out STD_LOGIC_VECTOR (width-1 downto 0);
  272.                o    : out STD_LOGIC);
  273.     end component counter;
  274.  
  275.     signal q_ff1, q_ff2 : std_logic;
  276.     signal en : std_logic;
  277.     signal counter_rst : std_logic;
  278.     signal detect_zero : std_logic;
  279.    
  280. begin
  281.  
  282.     FF1 : dff port map (clk => clk, d => btn, q => q_ff1);
  283.     FF2 : dff port map (clk => clk, d => q_ff1, q => q_ff2);
  284.  
  285.     counter_rst <= q_ff1 xor q_ff2;
  286.  
  287.     en <= not detect_zero;
  288.     CNT : counter generic map (up_counter => false, init_value => ((sys_freq*stable_time)/1000), width => 8) port map (clk => clk, rst => counter_rst, en => en, o => detect_zero);
  289.  
  290.     FF3 : dff_en port map (clk => clk, rst => counter_rst, d => q_ff2, en => detect_zero, q => q);
  291.    
  292. end Structural;
  293. /////////////////////////////
  294.        
  295. /////////clock divider////////////////////////
  296.  
  297. library IEEE;
  298. use IEEE.STD_LOGIC_1164.ALL;
  299.  
  300. use IEEE.NUMERIC_STD.ALL;
  301.  
  302.  
  303. entity clock_divider is
  304.     generic (divider : natural := 100_000_000); -- 1 pulse in every one second
  305.     Port ( sys_clk : in STD_LOGIC;              -- The base frequency for Basys 3 board (xc7a35tcpg236-1 chip) is 100 MHz
  306.            rst : in STD_LOGIC;
  307.            clk : out STD_LOGIC);
  308. end clock_divider;
  309.  
  310. architecture Behavioral of clock_divider is
  311.     signal internal_clock : std_logic;
  312. begin
  313.  
  314.     process (rst, sys_clk)
  315.         variable clk_divider : natural;
  316.     begin
  317.         if (rst = '1') then
  318.             clk_divider := 0;
  319.             internal_clock <= '0';
  320.         elsif(rising_edge(sys_clk)) then
  321.             if (clk_divider < (divider / 2)) then
  322.                 clk_divider := clk_divider + 1;
  323.                 internal_clock <= '0';
  324.             elsif (clk_divider = divider-1) then
  325.                 clk_divider := 0;
  326.                 internal_clock <= '1';
  327.             else
  328.                 clk_divider := clk_divider + 1;
  329.                 internal_clock <= '1';
  330.             end if;
  331.         end if;
  332.     end process;
  333.    
  334.     clk <= internal_clock;
  335.        
  336. end Behavioral;
  337. ///////////////////////////////////////
  338.  
  339. ////////////comparator //////////////
  340.  
  341. library IEEE;
  342. use IEEE.STD_LOGIC_1164.ALL;
  343.  
  344. entity comparator is
  345.     generic ( width : positive := 3);
  346.     Port ( A, B : in STD_LOGIC_VECTOR (width-1 downto 0);
  347.            lt, eq, gt : out STD_LOGIC);
  348. end comparator;
  349.  
  350. architecture Behavioral_v1 of comparator is
  351.  
  352. begin
  353.  
  354.     eq <= '1' when A = B else '0';
  355.     lt <= '1' when A < B else '0';
  356.     gt <= '1' when A > B else '0';
  357.  
  358. end Behavioral_v1;
  359. /////////////////////////////////
  360.  
  361. //////////one_hot_encoder///////////
  362.  
  363. library IEEE;
  364. use IEEE.STD_LOGIC_1164.ALL;
  365.  
  366. use IEEE.NUMERIC_STD.ALL;
  367.  
  368.  
  369. entity one_hot_encoder is
  370.     generic ( width : natural := 4);
  371.     Port ( D : in STD_LOGIC_VECTOR (width-1 downto 0);
  372.            Y : out STD_LOGIC_VECTOR (width-1 downto 0);
  373.            v : out STD_LOGIC);
  374. end one_hot_encoder;
  375.  
  376. architecture Behavioral of one_hot_encoder is
  377.  
  378. begin
  379.  
  380.     process(D)
  381.         variable OneHot : integer := 0;
  382.     begin        
  383.         Y <= (others => '0');
  384.         if unsigned(D) > 0 then
  385.             for i in width-1 downto 0 loop
  386.                 OneHot := i;
  387.                 exit when D(i) = '1';                        
  388.             end loop;
  389.             Y(OneHot) <= '1';
  390.             v <= '1';    
  391.         else
  392.             v <= '0';
  393.         end if;        
  394.     end process;
  395.  
  396. end Behavioral;
  397. /////////////////////////////////////////
  398.            
  399. /////////priority encoder //////////////
  400.  
  401. library IEEE;
  402. use IEEE.STD_LOGIC_1164.ALL;
  403.  
  404. use IEEE.NUMERIC_STD.ALL;
  405.  
  406.        
  407. entity priority_encoder is
  408.     generic ( width : positive := 3);
  409.     Port ( D : in STD_LOGIC_VECTOR (2**width-1 downto 0);
  410.            Y : out STD_LOGIC_VECTOR (width-1 downto 0);
  411.            v : out STD_LOGIC);
  412. end priority_encoder;
  413.  
  414. architecture Behavioral of priority_encoder is
  415.  
  416. begin
  417.  
  418.     process(D)
  419.         variable OneHot : integer := 0;
  420.     begin            
  421.    
  422.         Y <= (others => '0');
  423.         if unsigned(D) > 0 then
  424.             for i in 2**width-1 downto 0 loop
  425.                 OneHot := i;
  426.                 exit when D(i) = '1';                        
  427.             end loop;
  428.  
  429.             Y <= std_logic_vector(to_unsigned(OneHot, width));    
  430.             v <= '1';    
  431.         else
  432.             v <= '0';
  433.         end if;                  
  434.     end process;
  435.  
  436. end Behavioral;
  437. ////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement