----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date: 12/15/2021 02:06:27 PM
-- Design Name: 
-- Module Name: updateSnake - Behavioral
-- Project Name: 
-- Target Devices: 
-- Tool Versions: 
-- Description: 
-- 
-- Dependencies: 
-- 
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
-- 
----------------------------------------------------------------------------------


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
use IEEE.NUMERIC_STD.ALL;

library ourTypes;
use ourTypes.types.all;

-- Uncomment the following library declaration if instantiating
-- any Xilinx leaf cells in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity updateSnake is
    generic ( dataSize : integer);
    Port (
        clk_lente : in std_logic;
        clk_rapide : in std_logic;
        reset : in std_logic;
        
        address : out unsigned(SNAKE_ADDRESS_SIZE-1 downto 0) := (others => '0');
        dataIn : in std_logic_vector(dataSize-1 downto 0);
        dataOut : out std_logic_vector(dataSize-1 downto 0) := (others => '0');
        writeEnable : out std_logic := '1';
        
        matAddress : out unsigned(SNAKE_ADDRESS_SIZE-1 downto 0);
        matDataIn : in std_logic_vector(SNAKE_ADDRESS_SIZE-1 downto 0) := (others => '0');
        matDataOut : out std_logic_vector(SNAKE_ADDRESS_SIZE-1 downto 0) := (others => '0');
        matWriteEnable : out std_logic := '1';
        
        button_up : in STD_LOGIC;
        button_down : in STD_LOGIC;
        button_left : in STD_LOGIC;
        button_right : in STD_LOGIC;
        
        pommeCE : out std_logic := '0';
        pommeX  : in unsigned(5 downto 0);
        pommeY : in unsigned(4 downto 0);
        resetPomme : out std_logic := '1';
        
        tailIndex : out unsigned(SNAKE_ADDRESS_SIZE-1 downto 0);
        
        lost : out std_logic := '0'
        );
end updateSnake;

architecture Behavioral of updateSnake is
signal index : unsigned(SNAKE_ADDRESS_SIZE downto 0) := to_unsigned(0,SNAKE_ADDRESS_SIZE+1);
signal isUpdating: std_logic := '0';
signal updateIndex : unsigned(SNAKE_ADDRESS_SIZE-1 downto 0) := (others => '0');
signal state : unsigned(4 downto 0) := (others => '0');
signal checkLost : std_logic := '0';
signal nbOfEls : unsigned(SNAKE_ADDRESS_SIZE-1 downto 0) := to_unsigned(12,SNAKE_ADDRESS_SIZE);
begin
process(updateIndex, clk_rapide, clk_lente,reset,index) --process de reset
variable cSnake : pos; --current snake, celui qu'on met à jour
variable lSnake : pos; --last snake, sauvegarde de l'état précédent
variable pSnake : pos; --previous snake, snake precedent dans la chaine (on s'en sert aussi de stockage temp pour ajouter un el)
variable currentSnake : pos;
variable update : std_logic := '0';
variable indext : unsigned(SNAKE_ADDRESS_SIZE-1 downto 0) := to_unsigned(0,SNAKE_ADDRESS_SIZE);
variable updateIndext : unsigned(SNAKE_ADDRESS_SIZE-1 downto 0) := (others => '0');
variable addEl : std_logic := '0';
constant PROG_END : unsigned(4 downto 0) := to_unsigned(31,5);
begin

    if rising_edge(clk_lente) then
       isUpdating <= '1';
    end if;
    if rising_edge(clk_rapide) then
        indext := index(index'HIGH downto 1);
        updateIndext := MAX_SNAKE - 1 - updateIndex;
        if(reset = '0') then --il faut qu'on ai le reset sur la clk car il controle indirectement l'entrée de la RAM
            index <= (others => '0');
            writeEnable <= '1';
            matWriteEnable <= '1';
            update := '0';
            lost <= '0';
        else
            index <= index + 1;
            if(indext = MAX_SNAKE-1) then
                index <= (others => '0');
                writeEnable <= '0';
                matWriteEnable <= '0';
                update := '1';
            end if;
        end if;
        if(update = '1' and isUpdating = '1') then
            state <= state + 1;
            
            if(state = 0) then --UPDATE
                writeEnable <= '0';
                matWriteEnable <= '0';
                address <= updateIndext;
            elsif(state = 2) then
                cSnake := to_pos(dataIn);
                lSnake := to_pos(dataIn);
                if(cSnake.isDefined = '1') then
                    if(cSnake.X = 0 and cSnake.dirX = -1) then
                        --cSnake.X := to_unsigned(639,10);
                        lost <= '1';
                    elsif(cSnake.X = 639 and cSnake.dirX = 1) then
                        --cSnake.X := to_unsigned(1,10);
                        lost <= '1';
                    else
                        cSnake.X := to_unsigned(to_integer(cSnake.X) + to_integer(cSnake.dirX),10);
                    end if;
                    if(cSnake.Y = 0 and cSnake.dirY = -1) then
                        --cSnake.Y := to_unsigned(479,9);
                        lost <= '1';
                    elsif(cSnake.Y = 479 and cSnake.dirY = 1) then
                        --cSnake.Y := to_unsigned(1,9);
                        lost <= '1';
                    else
                        cSnake.Y := to_unsigned(to_integer(cSnake.Y) + to_integer(cSnake.dirY),9);
                    end if;
                    if(cSnake.X(2 downto 0) = 0  and cSnake.X(3) = '1' and cSnake.Y(2 downto 0) = 0 and cSnake.Y(3) = '1') then --si on est au centre d'une case
                        if(updateIndext = 0) then
                            if(button_up = '1' and cSnake.dirY /= 1) then
                                cSnake.dirY := to_signed(-1,2);
                                cSnake.dirX := to_signed(0,2);
                            elsif(button_down = '1' and cSnake.dirY /= -1) then
                                cSnake.dirY := to_signed(1,2);
                                cSnake.dirX := to_signed(0,2);
                            elsif(button_left = '1' and cSnake.dirX /= 1) then
                                cSnake.dirY := to_signed(0,2);
                                cSnake.dirX := to_signed(-1,2);
                            elsif(button_right = '1' and cSnake.dirX /= -1) then
                                cSnake.dirY := to_signed(0,2);
                                cSnake.dirX := to_signed(1,2);
                            end if;
                            dataOut <=  to_stdlogicvector(cSnake);
                            writeEnable <= '1';
                        else
                            address <= updateIndext-1;
                            state <= to_unsigned(9,5);
                        end if;
                    else
                        dataOut <=  to_stdlogicvector(cSnake);
                        writeEnable <= '1';
                    end if;
                    
                    matAddress <= to_unsigned(to_integer(cSnake.Y(cSnake.Y'high downto 4)) * 40 + to_integer(cSnake.X(cSnake.X'high downto 4)),SNAKE_ADDRESS_SIZE);
                else
                    state <= PROG_END; --jump end
                end if;
            
            elsif(state = 4) then -- MAJ MATRICE
                address <= unsigned(matDataIn);
                
                writeEnable <= '0';
                if(cSnake.X(cSnake.X'high downto 4) /= lSnake.X(lSnake.X'high downto 4) or cSnake.Y(cSnake.Y'high downto 4) /= lSnake.Y(lSnake.Y'high downto 4)) then --si on as changé de case
                    matWriteEnable <= '1';
                    matAddress <= to_unsigned(to_integer(cSnake.Y(cSnake.Y'high downto 4)) * 40 + to_integer(cSnake.X(cSnake.X'high downto 4)),SNAKE_ADDRESS_SIZE);
                    matDataOut <= std_logic_vector(updateIndext);
                    
                    if(cSnake.X(cSnake.X'high downto 4) = pommeX and cSnake.Y(cSnake.Y'high downto 4) = pommeY) then
                        resetPomme <= '0';
                        addEl := '1';
                    end if;
                else
                    state <= PROG_END; --jump end
                end if;
            elsif(state = 6) then
                matWriteEnable <= '0';
                matAddress <= to_unsigned(to_integer(lSnake.Y(lSnake.Y'high downto 4)) * 40 + to_integer(lSnake.X(lSnake.X'high downto 4)),SNAKE_ADDRESS_SIZE);
                
                pSnake := to_pos(dataIn);
                if(pSnake.isDefined = '1' and updateIndext = 0) then
                    lost <= '1';
                end if;
                --matDataOut <= std_logic_vector(updateIndex);
            elsif(state = 8) then
                if(matDataIn = std_logic_vector(updateIndext)) then
                    matWriteEnable <= '1';
                    matDataOut <= std_logic_vector(to_unsigned(MAX_SNAKE-1,SNAKE_ADDRESS_SIZE));
                end if;
                if(addEl = '1') then
                    state <= to_unsigned(12,5);
                    resetPomme <= '1';
                else
                    state <= PROG_END; --jump end
                end if;
                
            elsif(state = 10) then -- PROPAGATION DE LA DIRECTION
                matWriteEnable <= '0';
                pSnake := to_pos(dataIn);
                cSnake.dirX := pSnake.dirX;
                cSnake.dirY := pSnake.dirY;
                dataOut <=  to_stdlogicvector(cSnake);
                writeEnable <= '1';
                address <= updateIndext;
                state <= to_unsigned(3,5); --on peut ptet directement jump à la fin?
                
            elsif(state = 12) then
                address <= nbOfEls;
                matWriteEnable <= '0';
                nbOfEls <= nbOfEls + 1;
            elsif(state = 14) then
                pSnake := to_pos(dataIn);
                pSnake.X := to_unsigned(to_integer(pSnake.X) - 16 * to_integer(pSnake.dirX),10);
                pSnake.Y := to_unsigned(to_integer(pSnake.Y) - 16 * to_integer(pSnake.dirY),9);
                dataOut <= to_stdlogicvector(pSnake);
                address <= nbOfEls;
                matDataOut <= std_logic_vector(nbOfEls);
                matAddress <= to_unsigned(to_integer(pSnake.Y(pSnake.Y'high downto 4)) * 40 + to_integer(pSnake.X(pSnake.X'high downto 4)),SNAKE_ADDRESS_SIZE);
            elsif(state = 15) then
                writeEnable <= '1';
                matWriteEnable <= '1';
                addEl := '0';
                state <= PROG_END;
            
            --elsif(state = 16) then
            --    matWriteEnable <= '0';
            
            elsif(state = PROG_END) then --END
                matWriteEnable <= '0';
                writeEnable <= '0';
                updateIndex <= updateIndex + 1;
                state <= to_unsigned(0,5);
            end if;
        end if;
        
        if update = '0' then
            if(indext = 0) then
                nbOfEls <= to_unsigned(12,SNAKE_ADDRESS_SIZE);
                currentSnake.X := to_unsigned(8+to_integer(indext)*16,10);
                currentSnake.Y := to_unsigned(8,9);
                currentSnake.dirX := to_signed(0,2);
                currentSnake.dirY := to_signed(1,2);
                currentSnake.isDefined := '1';
            elsif(indext < 13) then
                currentSnake.X := to_unsigned(8+to_integer(indext)*16,10);
                currentSnake.Y := to_unsigned(8,9);
                currentSnake.dirX := to_signed(-1,2);
                currentSnake.dirY := to_signed(0,2);
                currentSnake.isDefined := '1';
            else
                currentSnake.X := to_unsigned(8,10);
                currentSnake.Y := to_unsigned(8,9);
                currentSnake.dirX := to_signed(0,2);
                currentSnake.dirY := to_signed(1,2);
                currentSnake.isDefined := '0';
            end if;
            dataOut <= to_stdlogicvector(currentSnake);
            matAddress <= to_unsigned(to_integer(indext),SNAKE_ADDRESS_SIZE);
            matDataOut <= std_logic_vector(to_unsigned(to_integer(indext),SNAKE_ADDRESS_SIZE));
            address <= indext;
        end if;
        
        
        if(update = '1' and isUpdating = '0') then
            pommeCE <= '1';
        else
            pommeCE <= '0';
        end if;
    end if;
    
    if(updateIndex = MAX_SNAKE) then
        isUpdating <= '0';
        updateIndex <= to_unsigned(0,SNAKE_ADDRESS_SIZE);
        state <= to_unsigned(0,5);
    end if;
end process;

tailIndex <= nbOfEls;
end Behavioral;
