Help with variables and 'for' loops

Apr 29, 2012
Reaction score
I am working on a design for a game of "snake". To determine the next position of the snake, I have a section of code that uses arrays to store the position of the snake and variables to store the index values of the head and tail of the snake. Then I have a 'for' loop that goes through every element of the array to determine the next position of the snake based on the value of the "direction" signal

My problem is that the positions aren't updating. Now I'm not very familiar with using loops in VHDL but I have a feeling it may have something to do with the way I compare the index values of the loop to values stored in the variables. Any insight you all can give me would be greatly appreciated. Here is part of the code:

    snake_motion : process(clk, reset, dir, mov_en, snake_pos, snake_dir)
        variable r_head_next : integer;
        variable c_head_next : integer;
        variable r_tail_next : integer;
        variable c_tail_next : integer;
        variable r_head : integer;
        variable c_head : integer;
        variable r_tail : integer;
        variable c_tail : integer;
        if reset = '1' then                            -- assign default values upon reset
            snake_pos <= snake_pos_def;
            snake_pos_next <= snake_pos_def;
            snake_dir <= snake_dir_def;
            snake_dir_next <= snake_dir_def;
            r_head := r_head_def;
            r_tail := r_tail_def;
            c_head := c_head_def;
            c_tail := c_tail_def;
        elsif (clk'EVENT and clk = '0') then
            mov_en <= '1';
        elsif (clk'EVENT and clk = '1') then
            snake_pos <= snake_pos_next;
            snake_dir <= snake_dir_next;
            r_head := r_head_next;
            r_tail := r_tail_next;
            c_head := c_head_next;
            c_tail := c_tail_next;
        end if;
        if mov_en = '1' then
            for r in 0 to max_row loop
                for c in 0 to max_col loop
                    if ((r = r_head) and (c = c_head)) then        -- head of snake
                        snake_dir_next(r,c) <= dir;
                        if dir = "000" then
                            if r = max_row then
                                r_head_next := 0;
                                snake_pos_next(0,c) <= '1';
                                r_head_next := (r+1);
                                snake_pos_next((r+1),c) <= '1';
                            end if;
                        elsif dir = "001" then
It should be noted that I currently have the direction fixed to "000"


Mar 10, 2008
Reaction score
Will this be VHDL code for simulation or synthezing (and implementation in a FPGA)


Apr 29, 2012
Reaction score
I'm trying to implement it onto an FPGA. I'm using a Cyclone II DE2-70. I actually scrapped that design to try another method without using 'for' loops, but now I'm not getting any video output. Here is the section of code I'm working with:

    type array1 is array(0 to 14, 0 to 19) of std_logic;
    signal snake_pos_def    : array1 := (    0 => (0 => '1', others => '0'), 
                                            1 => (0 => '1', others => '0'),
                                            2 => (0 => '1', others => '0'),
                                            others => (others => '0'));
    snake_pos <= snake_pos_def;
    snake_display : process(pix_row, pix_col, snake_pos)
        case pix_row(9 downto 5) is
            when "00000" =>
                snake_on <= snake_pos(0, conv_integer(pix_col(9 downto 5)));
            when "00001" =>
                snake_on <= snake_pos(1, conv_integer(pix_col(9 downto 5)));
            when "00010" =>
                snake_on <= snake_pos(2, conv_integer(pix_col(9 downto 5)));
            when "00011" =>
                snake_on <= snake_pos(3, conv_integer(pix_col(9 downto 5)));
            when others =>
                snake_on <= '0';
        end case;
    end process snake_display;

Right now I'm not going for motion, just trying to see a display

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. After that, you can post your question and our members will help you out.

Ask a Question