Advertisement
Guest User

Untitled

a guest
Jan 6th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.49 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.STD_LOGIC_ARITH.ALL;
  4.  
  5. entity GestionePulsanti is
  6.     Port ( rst : in  STD_LOGIC;
  7.            clk : in  STD_LOGIC;
  8.            P1 : in  STD_LOGIC;
  9.            P2 : in  STD_LOGIC;
  10.            seg_out : out  STD_LOGIC_VECTOR (0 to 3);
  11.        state_debug : out STD_LOGIC_VECTOR (0 to 2);
  12.            count_debug : out STD_LOGIC_VECTOR (0 to 3));
  13. end GestionePulsanti;
  14.  
  15. architecture Behavioral of GestionePulsanti is
  16. type state is (PrecP1, Win1_Prec1, Win2_Prec1, PrecP2, Win1_Prec2, Win2_Prec2);
  17. signal CurrentState, NextState: state;
  18. signal count, new_count: integer range 0 to 10;
  19. signal temp_unlock: std_logic:= '0';
  20. signal timeover: std_logic:= '0';
  21.  
  22. begin
  23.  
  24. Memorizzazione: process(clk, rst) -- reset di tipo asincrono
  25. begin
  26.     if(rst='1')
  27.     -- se il reset  alto, resetto la macchina riportandola allo stato iniziale
  28.         then CurrentState <= PrecP1;
  29.               count <= 0;
  30.     elsif(rising_edge(clk))
  31.     -- altrimenti, commuto di stato sul fronte di salita del clock
  32.         then CurrentState <= NextState;
  33.               count <= new_count;
  34.     end if;
  35. end process;
  36.  
  37. TransizioneStato: process(CurrentState, P1, P2, timeover)
  38. variable Ingressi: std_logic_vector (0 to 1);
  39. begin
  40.     Ingressi:=P2&P1;
  41.     case (CurrentState) is
  42.         when PrecP1 => temp_unlock<='0';
  43.                    state_debug<="000";
  44.                    case Ingressi is
  45.                     when "00" => NextState <= PrecP1;
  46.                     when "01" => NextState <= Win1_Prec1;
  47.                     when "10" => NextState <= Win2_Prec1;
  48.                     when "11" => NextState <= Win1_Prec2;
  49.                     when others => null;
  50.                 end case;
  51.         when Win1_Prec1 => temp_unlock<='1';   
  52.                        state_debug<="001";
  53.                    if (timeover='1') then
  54.                     case Ingressi is
  55.                         when others => NextState <= PrecP1;
  56.                         -- torna nello stato PrecP1 quando ha finito di contare
  57.                         end case;
  58.                    elsif (timeover='0') then
  59.                     case Ingressi is
  60.                         when others => NextState <= Win1_Prec1;
  61.                         -- rimane nello stato corrente
  62.                     end case;
  63.                    end if;
  64.         when Win2_Prec1 => temp_unlock<='1';   
  65.                    state_debug<="010";
  66.                    if (timeover='1') then
  67.                     case Ingressi is
  68.                         when others => NextState <= PrecP1;
  69.                         -- torna nello stato PrecP1 quando ha finito di contare
  70.                     end case;
  71.                    elsif (timeover='0') then
  72.                     case Ingressi is
  73.                         when others => NextState <= Win2_Prec1;
  74.                         -- rimane nello stato corrente
  75.                     end case;
  76.                    end if;
  77.         when PrecP2 => temp_unlock<='0';
  78.                    state_debug<="011";
  79.                    case Ingressi is
  80.                         when "00" => NextState <= PrecP2;
  81.                     when "01" => NextState <= Win1_Prec2;
  82.                     when "10" => NextState <= Win2_Prec2;
  83.                     when "11" => NextState <= Win2_Prec1;
  84.                     when others => null;
  85.                 end case;
  86.         when Win1_Prec2 => temp_unlock<='1';   
  87.                    state_debug<="100";
  88.                        if (timeover='1') then
  89.                     case Ingressi is
  90.                         when others => NextState <= PrecP2;
  91.                         -- torna nello stato PrecP1 quando ha finito di contare
  92.                     end case;
  93.                        elsif (timeover='0') then
  94.                     case Ingressi is
  95.                         when others => NextState <= Win1_Prec2;
  96.                         -- rimane nello stato corrente
  97.                     end case;
  98.                    end if;
  99.         when Win2_Prec2 => temp_unlock<='1';
  100.                    state_debug<="101";
  101.                    if (timeover='1') then
  102.                     case Ingressi is
  103.                         when others => NextState <= PrecP2;
  104.                         -- torna nello stato PrecP1 quando ha finito di contare
  105.                     end case;
  106.                    elsif (timeover='0') then
  107.                     case Ingressi is
  108.                         when others => NextState <= Win2_Prec2;
  109.                         -- rimane nello stato corrente
  110.                     end case;
  111.                    end if;
  112.     end case;
  113. end process;
  114.  
  115. Uscite: process(CurrentState)
  116. begin
  117.     case CurrentState is
  118.         when PrecP1 => seg_out <= "1111"; -- display spento
  119.         when Win1_Prec1 => seg_out <= "0001"; -- vince il giocatore 1
  120.         when Win2_Prec1 => seg_out <= "0010"; -- vince il giocatore 2
  121.         when PrecP2 => seg_out <= "1111"; -- display spento
  122.         when Win1_Prec2 => seg_out <= "0001"; -- vince il giocatore 1
  123.         when Win2_Prec2 => seg_out <= "0010"; -- vince il giocatore 2
  124.     end case;
  125. end process;
  126.  
  127. Contatore: process(temp_unlock, count)
  128. begin
  129.     if temp_unlock = '1' then -- questo segnale vale 1 quando sto in uno stato di conteggio
  130.         if count = 10 then -- se ho finito di contare, azzero new_count ed imposto timeover ad 1
  131.             new_count <= 0;
  132.             timeover <= '1';
  133.         else -- altrimenti aggiorno il contatore di 1
  134.             new_count <= count + 1;
  135.             timeover <= '0';
  136.         end if;
  137.     else
  138.         new_count <= 0;
  139.         timeover <= '0';
  140.     end if;
  141. end process;
  142.  
  143. count_debug <= conv_std_logic_vector(count, 4);
  144.  
  145. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement