library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

package types is
    constant MAX_SNAKE : integer := 1200;
    constant SNAKE_ADDRESS_SIZE : integer :=11;
    type coord is array(0 to 39, 0 to 29) of unsigned(SNAKE_ADDRESS_SIZE-1 downto 0);
    type pos is record
        X: unsigned(9 downto 0);
        Y: unsigned(8 downto 0);
        dirX: signed(1 downto 0);
        dirY: signed(1 downto 0);
        isDefined: std_logic;
    end record;
    function to_stdlogicvector(snake : pos) return std_logic_vector;
    function to_pos(input : std_logic_vector) return pos;
end package;

package body types is
    function to_stdlogicvector(snake : pos) return std_logic_vector is
    --variable sortie : std_logic_vector(pos.X'length+pos.Y'length+pos.dirX'length+pos.dirY'length downto 0);
    begin
        return std_logic_vector(std_logic_vector(snake.X) & std_logic_vector(snake.Y) & std_logic_vector(snake.dirX) & std_logic_vector(snake.dirY) & snake.isDefined);
    end to_stdlogicvector;
    
    function to_pos(input : std_logic_vector) return pos is
    variable sortie : pos;
    variable offset : integer;
    begin
        --si on trouve une facon plus simple de deserialiser je suis preneur
        offset := 0;
        sortie.X := unsigned(input(pos.X'length-1 downto 0));
        offset := offset+pos.X'length;
        sortie.Y := unsigned(input(offset+pos.Y'length-1 downto offset));
        offset := offset+pos.Y'length;
        sortie.dirX := signed(input(offset+pos.dirX'length-1 downto offset));
        offset := offset+pos.dirX'length;
        sortie.dirY := signed(input(offset+pos.dirY'length-1 downto offset));
        offset := offset+pos.dirY'length;
        sortie.isDefined := input(offset);
        return sortie;
    end to_pos;
end package body;
