VHDL help needed

Discussion in 'Hardware' started by TomboT, Nov 11, 2006.

  1. TomboT

    TomboT

    Joined:
    Nov 11, 2006
    Messages:
    2
    Likes Received:
    0
    Hi all,

    I regret that I am having to post this question as I am sure many have posted questions like this before, but I have reached desperation point!

    I am at the university of Liverpool undergoing a module in VHDL which is currently being taught very poorly! my first assignment is due in a week and I am just at a loss, please can someone help point me in the right direction....

    Here is the code that I have created to test and simulate one of my design blocks. I am creating an electronic safe, and this part reads an input from a keypad scanner and outputs to 2 seven segment decoders and to 2 LED's. I have designed this part using 'One Hot'.

    Code:
    ENTITY safe_test IS
    	-- {{ALTERA_IO_BEGIN}} DO NOT REMOVE THIS LINE!
    	PORT
    	(
    		clk : IN STD_LOGIC;
    		code : IN STD_LOGIC_VECTOR(3 downto 0);
    		H : IN STD_LOGIC;
    		reset : IN STD_LOGIC;
    		LED1 : OUT STD_LOGIC;
    		LED2 : OUT STD_LOGIC;
    		LED3 : OUT STD_LOGIC;
    		SS1 : OUT STD_LOGIC_VECTOR(3 downto 0);
    		SS2 : OUT STD_LOGIC_VECTOR(3 downto 0);
    		stateout: out STD_LOGIC_VECTOR(8 downto 0)
    	);
    	-- {{ALTERA_IO_END}} DO NOT REMOVE THIS LINE!
    	
    END safe_test;
    
    
    --  Architecture Body
    
    ARCHITECTURE SafeLogioc_architecture OF safe_test IS
    
    	
    --State Assignments
    constant SL0: integer := 0;
    constant SL1: integer := 1;
    constant SL2: integer := 2;
    constant SL3: integer := 3;
    constant SL4: integer := 4;
    constant SL5: integer := 5;
    constant SL6: integer := 6;
    constant SL7: integer := 7;
    constant SL8: integer := 8;
    
    signal SLp_state, SLn_state: std_logic_vector(8 downto 0);
    
    
    begin
    
    stateout <= SLp_state;
    
    SLstatereg: process(reset,clk,SLn_state)
    	begin
    		if(reset='0')then
    			SLp_state<= (SL0 =>'1',others=>'0');
    					
    		elsif(clk'event and clk='1')then
    			SLp_state<=SLn_state;
    			
    		end if;
    end process SLstatereg;
    
    SLcomb: process(SLp_state,H)
    
    variable program : std_logic := '0';
    variable Test : std_logic;
    variable locked : std_logic := '1';
    variable Key0: std_logic_vector(3 downto 0) := "0010";
    variable Key1: std_logic_vector(3 downto 0) := "0110";
    variable Key2: std_logic_vector(3 downto 0) := "1001";
    variable Key3: std_logic_vector(3 downto 0) := "0010";
    variable CodeStore: std_logic_vector(3 downto 0) := "1011";
    
    begin
    
    	SLn_state <= SLp_state;
    	
    	if(SLp_state(SL0) = '1')then
    			SS1 <= "0001";
    			SS2 <= "1011";
    			if(locked='0')then
    				LED1 <= '0';
    				else LED1 <= '1';
    			end if;
    			if(program='0')then
    				LED2 <= '0';
    				else LED2 <= '1';
    			end if;
    			Test:='1';
    			LED3<=Test;
    			if(H='1')then
    				CodeStore:=code;
    				
    				SLn_state(SL0) <= '0';SLn_state(SL1) <= '1';
    							
    			end if;
    	end if;
    	
    	if(SLp_state(SL1) = '1')then
    		
    		if(H='0')then
    			if(CodeStore="1111" and locked='0' and program='0')then
    				program := '1';
    				LED2 <= '1';
    				LED1 <= '0';
    				LED3 <= Test;
    				SS1 <= "0001";
    				SS2 <= "1011";
    				SLn_state(SL1) <= '0';SLn_state(SL0) <= '1';
    			elsif(program='1') then
    				LED2 <= '1';
    				LED1 <= '0';
    				LED3 <= Test;
    				SS1 <= "0001";
    				SS2 <= CodeStore;
    				key0:=CodeStore;
    				SLn_state(SL1) <= '0';SLn_state(SL2) <= '1';
    			else
    				if(locked='0')then
    				LED1 <= '0';
    				else LED1 <= '1';
    				end if;
    				LED2 <= '0';
    				LED3 <= Test;
    				SS2 <= CodeStore;
    				SS1 <= "0001";
    				if(CodeStore/=key0)then
    					Test:= (Test and '1');
    					LED3 <= Test;
    				end if;
    				SLn_state(SL1) <= '0';SLn_state(SL2) <= '1';
    			end if;
    		end if;
    	end if;
    	
    	if(SLp_state(SL2) = '1')then
    			SS1 <= "0010";
    			SS2 <= CodeStore;
    			if(locked='0')then
    				LED1 <= '0';
    				else LED1 <= '1';
    			end if;
    			LED2<='0';
    			LED3<=Test;
    			if(H='1')then
    				CodeStore:=code;
    				
    				SLn_state(SL2) <= '0';SLn_state(SL3) <= '1';
    			end if;
    	end if;
    		
    	if(SLp_state(SL3) = '1')then	
    			SS1 <= "0010";
    			SS2 <= CodeStore;
    		if(H='0')then	
    				if(program='1')then
    					key1:=CodeStore;
    					LED2 <= '1';
    					LED1 <= '0';
    					LED3 <= Test;
    					SS1 <= "0010";
    					SS2 <= CodeStore;		
    					SLn_state(SL3) <= '0';SLn_state(SL4) <= '1';
    				else
    					if(locked='0')then
    					LED1 <= '0';
    					else LED1 <= '1';
    					end if;
    					LED2 <= '0';
    					LED3 <= Test;
    					SS2 <= CodeStore;
    					SS1 <= "0010";
    					if(CodeStore/=key1)then
    						Test:= (Test and '1');					
    						LED3 <= Test;
    					end if;	
    					SLn_state(SL3) <= '0';SLn_state(SL4) <= '1';
    				
    			end if;
    		end if;	
    	end if;
    		
    	if(SLp_state(SL4) = '1')then
    			SS1 <= "0011";
    			SS2 <= CodeStore;
    			if(locked='0')then
    				LED1 <= '0';
    				else LED1 <= '1';
    			end if;
    			LED2<='0';
    			LED3<=Test;
    			if(H='1')then
    				CodeStore:=code;
    				SLn_state(SL4) <= '0';SLn_state(SL5) <= '1';
    			end if;
    	end if;
    	
    	if(SLp_state(SL5) = '1')then
    		SS1 <= "0011";
    		SS2 <= CodeStore;
    		if(H='0')then
    			if(program='1')then
    				LED2 <= '1';
    				LED1 <= '0';
    				LED3 <= Test;
    				SS1 <= "0011";
    				SS2 <= CodeStore;
    				key2:=CodeStore;		
    				SLn_state(SL5) <= '0';SLn_state(SL6) <= '1';
    			else
    				if(locked='0')then
    				LED1 <= '0';
    				else LED1 <= '1';
    				end if;
    				LED2 <= '0';
    				LED3 <= Test;
    				SS2 <= CodeStore;
    				SS1 <= "0011";
    				if(CodeStore/=key2)then
    					Test:= (Test and '1');
    					LED3 <=  Test;
    				end if;
    				SLn_state(SL5) <= '0';SLn_state(SL6) <= '1';
    			end if;
    		end if;			
    	end if;
    	
    	if(SLp_state(SL6) = '1')then
    			SS1 <= "0100";
    			SS2 <= CodeStore;
    			if(locked='0')then
    				LED1 <= '0';
    				else LED1 <= '1';
    			end if;
    			LED2<='0';
    			LED3<=Test;
    			if(H='1')then
    				CodeStore:=code;
    				SLn_state(SL6) <= '0';SLn_state(SL7) <= '1';
    			end if;
    	end if;	
    		
    	if(SLp_state(SL7) = '1')then
    		SS1 <= "0100";
    		SS2 <= CodeStore;	
    		if(H='0')then
    			if(program='1')then
    				LED2 <= '1';
    				LED1 <= '0';
    				LED3 <= Test;
    				SS1 <= "0100";
    				SS2 <= CodeStore;
    				key3:=CodeStore;
    				SLn_state(SL7) <= '0';SLn_state(SL8) <= '1';
    			else
    				if(locked='0')then
    				LED1 <= '0';
    				else LED1 <= '1';
    				end if;
    				LED2 <= '0';
    				LED3 <= Test;
    				SS2 <= CodeStore;
    				SS1 <= "0100";
    				SLn_state(SL7) <= '0';SLn_state(SL8) <= '1';
    				if(CodeStore/=key3)then
    					Test:= (Test and '1');
    				end if;
    				if(Test = '1' and locked='1') then
    					LED1 <= '0';
    					locked:='0';
    				elsif(Test = '1' and locked='0') then
    					LED1 <= '1';
    					locked := '1';
    				end if;
    			end if;
    		end if;
    	end if;
    	
    	if(SLp_state(SL8) = '1')then
    			if(locked='0')then
    				LED1 <= '0';
    				else LED1 <= '1';
    			end if;
    			LED2<='0';
    			LED3<=Test;
    			SS1 <= "0001";
    			SS2 <= "1011";
    			if(H='1')then
    				SLn_state(SL8) <= '0';SLn_state(SL0) <= '1';
    			end if;
    	end if;
    
    end process SLcomb;
    	
    END architecture SafeLogioc_architecture;
    
    I have added a third test LED (LED3) to check what my test variable is doing.... so far I have succesfully got the design to change states whenever the keypad scanner reports a button is being pressed (signal H). However the variable Test does not appear to be doing what it should when I simulate correct codes or incorrect codes, nor do the other LED's.

    I feel that my biggest problem with being new to this is that I expect everything to work like when programming in C... but of course it doesn't, and I do not yet understand why not fully. I read somewhere that I must always re-assign variables for every possible path through the code and so have done this.

    Please could someone tell me where I am going wrong, I have been at this for ages and just do not understand why it won't work the way I think it should.

    Many thanks in advance!

    Tom
     
    TomboT, Nov 11, 2006
    #1
    1. Advertisements

  2. TomboT

    TomboT

    Joined:
    Nov 11, 2006
    Messages:
    2
    Likes Received:
    0
    Sorry quick addition:

    The main warnings that I get are,

    Pins stuck at GND or VCC
    Latches have been inferred
    Latch has unsafe behaviour


    In respect to my code, please could you tell me why this is the case?

    Thanks
     
    TomboT, Nov 11, 2006
    #2
    1. Advertisements

  3. TomboT

    nhendon

    Joined:
    Nov 21, 2006
    Messages:
    1
    Likes Received:
    0
    A bit too late for the reply. But for future reference...
    "SLstatereg: process(reset,clk,SLn_state)"
    --remove the SLn_state from the sensitivity list.
    --More likely to cause the latch.

    You also use a lot of variables.
    One of your variable, 'program' only have one driver to assign it to '1'. You put initial value on it which will be ignored.

    I'm afraid there's a lot of work need to be done on this.
     
    nhendon, Nov 21, 2006
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.