Advertisement
fsmiro

PART B

Jan 30th, 2022 (edited)
1,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 2.92 KB | None | 0 0
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4.  
  5. entity my_pass_setter is
  6.     generic (
  7.         g_max_attempt       :   integer                 := 16;
  8.         g_password_width    :   positive range 4 to 16  := 16;
  9.         g_sys_freq          :   integer                 := 100000000
  10.     );
  11.     port (
  12.         clk : in std_logic;
  13.         reset : in std_logic;
  14.        
  15.         btn_down    :   in  std_logic;
  16.         sw          :   in  std_logic_vector(15 downto 0);
  17.  
  18.         pass        :   out std_logic_vector(15 downto 0);
  19.         new_pass    :   out std_logic;
  20.         leds        :   out std_logic_vector(15 downto 0)
  21.     );
  22. end my_pass_setter;
  23.  
  24. architecture behavioral of my_pass_setter is
  25.  
  26.     constant    c_one_second_count      :   integer                         := g_sys_freq;
  27.     constant    c_three_second_count    :   integer                         := 3* c_one_second_count;
  28.     constant    c_default_psw           :   std_logic_vector(pass'range)    := std_logic_vector(to_unsigned(1,pass'length));
  29.    
  30.     constant    c_debug_ns_count        :   integer                         := 300 ;
  31.     constant    c_debug_count_ccycles   :   integer                         := (c_debug_ns_count/10**2)*(g_sys_freq/(10**7));
  32.  
  33.     type state_type is (IDLE,
  34.                         CHECK_BTN,
  35.                         READ_SW_SET_PASS,
  36.                         LED_ON_1_SEC
  37.                         );
  38.    
  39.    
  40.     signal state : state_type   := IDLE;
  41.     --signal    r_switch    :   std_logic_vector(sw'RANGE)  := (others => '0') ;
  42.     signal  r_counter       :   integer                     := 0;
  43.     signal  r_new_pass      :   std_logic_vector(pass'range):= c_default_psw;
  44.     signal  r_leds          :   std_logic_vector(leds'range):= c_default_psw;
  45.     signal  r_new_psw_flag  :   std_logic                   := '0';
  46.  
  47.     begin
  48.  
  49.     pass        <= r_new_pass;
  50.     leds        <= r_leds;
  51.     new_pass    <= r_new_psw_flag;
  52.  
  53.     FSM_PROC : process(clk)
  54.     begin
  55.         if rising_edge(clk) then
  56.             if reset = '1' then
  57.                 state       <= IDLE;
  58.                 r_new_pass  <= c_default_psw;
  59.                 r_leds      <= c_default_psw;
  60.             else
  61.                 case state is
  62.    
  63.                     when IDLE =>
  64.                                     state   <= CHECK_BTN;
  65.                     when CHECK_BTN  =>
  66.  
  67.                                     if btn_down = '1' then
  68.                                         r_counter <= r_counter + 1;
  69.                                         if r_counter = c_debug_count_ccycles-1 then
  70.                                             state       <= READ_SW_SET_PASS;
  71.                                             r_new_pass  <= (others => '0'); -- devo settarle entrambe a 0 altrimenti ho
  72.                                             r_leds      <= (others => '0');  -- la parte della vecchia password in mezzo alle palle
  73.                                             r_counter   <= 0;
  74.                                         end if;
  75.                                     else
  76.                                         r_counter <= 0;
  77.                                     end if;
  78.  
  79.                     when READ_SW_SET_PASS   =>
  80.  
  81.                                     r_new_pass      <=  r_new_pass(r_new_pass'high downto r_new_pass'low + g_password_width  )  & sw(g_password_width-1 downto 0);
  82.                                     r_leds          <=  r_leds(r_new_pass'high downto r_new_pass'low + g_password_width )           & sw(g_password_width-1 downto 0);
  83.                                     r_new_psw_flag  <= '1';
  84.                                     state           <= LED_ON_1_SEC;
  85.                                    
  86.                     when LED_ON_1_SEC   =>
  87.  
  88.                                     r_new_psw_flag  <= '0';
  89.                                    
  90.                                     if r_counter = c_one_second_count-1 then
  91.                                         r_counter   <=  0;
  92.                                         r_leds      <=  (others => '0');
  93.                                         state       <=  CHECK_BTN;
  94.                                     else
  95.                                         r_counter   <= r_counter + 1;
  96.                                     end if;
  97.  
  98.                 end case;
  99.    
  100.             end if;
  101.         end if;
  102.     end process;
  103.  
  104.  
  105.    
  106. end architecture;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement