0
\$\begingroup\$

I've coded this state in VHDL, but I am having problem getting into a certain state.

 architecture Behavioral of game is
type LIST is ARRAY (11 downto 0) of std_logic_vector(3 downto 0);
Constant LISTEN: LIST := ("0010","0100","1000","0001","0100","0010","0010","0010","1000","0001","0010","0001"); 
begin
process(CLK)
variable prescaler: integer range 0 to 50000000;
Variable control: std_logic := '0';
variable pointer: integer range 0 to 11:=0;
variable test: std_logic:='0';
variable error: std_logic:='0';
variable top_pointer: integer range 0 to 11 := 0;
variable buttontimer: integer range 0 to 50000000 := 0;
begin
if rising_edge (clk) then 
 if control = '0' and error = '0' then 
 winLEd <= "01";
 if(prescaler < 49999999/1) then 
 prescaler := prescaler + 1; 
 else
 prescaler := 0;
 top_pointer := top_pointer + 1; -- Top pointer increment
 control := '1'; --- trigger event to get to new state
 --test := '0';
 end if;
 end if; 
 if control = '1' and error = '0' then 
 winLEd <= "11";
 -- test := '0';
 if pointer > (top_pointer) then -- If pointer is higher than toppointer, it means the sequence has to be incrementet. 
 LED <= "0000";
 control := '0';
 pointer := 0;
 test := '0';
 error:= '0';
 end if;
 --if switch = "0000" then
 if switch = LISTEN(pointer-1) and pointer<= top_pointer then -- Checks if the switch match the sequence. 
 test := '1';
 buttontimer := buttontimer + 1;
 errorLed <= "01"; 
 if switch = "0000" and test = '1' and buttontimer < 49999999/1 then
 pointer:= pointer +1;
 test := '0';
 errorLed <= "10"; 
 buttontimer:= 0;
 end if; 
 end if;
 if switch /= LISTEN(pointer-1) and switch /= "0000" then -- if it doesn not match, error will be set high => trigger an new state. 
 error:= '1'; 
 end if; 
 if switch = "0000" then 
 Led <= LISTEN(pointer-1); 
 errorLed <= "11";
 error:= '0';
 control := '1';
 end if;
 --end if; 
 end if; 
 if error = '1' then -- stays here infinetly until board is reset. 
 winLED <= "10";
 errorLed <= "00"; 
 Led <= switch;
 end if; 
end if; 
end process;
end Behavioral;

I am having problem incrementing pointer here. Listen is an array, containing different 4 bit patterns. The Switch is the switches on my FGPA, I've used errorLED to see where I am in code, I see that I am entering this if statement, when the switches mathes the 4 bit pattern.

asked May 3, 2014 at 13:38
\$\endgroup\$
1
  • \$\begingroup\$ Does it work in simulation? \$\endgroup\$ Commented May 6, 2014 at 10:02

1 Answer 1

2
\$\begingroup\$

test only is 1 when this code is entered:

if switch = LISTEN(pointer-1) and pointer<= top_pointer and test = '0' then -- Checks if the switch match the sequence. 
 test := '1';
 buttontimer := buttontimer + 1;
 errorLed <= "01"; 
end if;

In this if statement, switch has a value of LISTEN(pointer-1). Then further down in the same process, switch needs to be "0000" and test = '1' in order to hit this code.

if switch = "0000" and test = '1' and buttontimer > 1 then
 pointer:= pointer +1;
 test := '0';
 errorLed <= "10"; 
 buttontimer:= 0;
end if;

So, the above code is only going to be entered when switch = LISTEN(pointer-1) = "0000", and buttontimer needs to meet it's requirement too, and pointer needs to hit it's requirement too (so that test can be 1).

Another note is that since you reset the value of test to 0 at the beginning of the clock cycle, that the value of test isn't going to carry over from one clock cycle to the next.

if control = '1' and error = '0' then 
 winLEd <= "11";
 Led <= LISTEN(pointer-1); 
 test := '0'; << remove this line
 ...
end if;

EDIT: In order to remember test, in the first block of code, remove the test := '0' at the beginning. You are already setting test to zero when the switch is de-asserted.

The FPGA is operating a lot faster than your finger can move. it's going to have run lots of clock cycles between you asserting the switch and de-asserting it. for that reason, you're going to need to remember the value of test, so that when the switch is de-asserted, the FPGA will remember that the switch was pressed. This is why I'm suggesting the removal of the test := '0'.

answered May 3, 2014 at 14:21
\$\endgroup\$
13
  • \$\begingroup\$ So you want me to insert it inside first if statement?? \$\endgroup\$ Commented May 3, 2014 at 14:43
  • \$\begingroup\$ Please look at my edit \$\endgroup\$ Commented May 3, 2014 at 14:46
  • \$\begingroup\$ what do you want to achieve? \$\endgroup\$ Commented May 3, 2014 at 14:59
  • \$\begingroup\$ I want to increment the pointer when what i press is equal to the pattern from the array, if not, it has to set error 1. \$\endgroup\$ Commented May 3, 2014 at 15:09
  • 1
    \$\begingroup\$ you can write a testbench which generates a test switch signal. See seas.upenn.edu/~ese171/vhdl/VHDLTestbench.pdf and vhdlguru.blogspot.com/2010/03/how-to-write-testbench.html. Since you have access to all the signals during every clock cycle, it's easier to see exactly where your code is going wrong. \$\endgroup\$ Commented May 3, 2014 at 16:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.