signal w_valid : boolean;w_valid <= true;w_valid <= (r_counter = to_unsigned(0, 8)); -- comparaison retourne booleanif w_valid then -- ...end if;
Tableau récapitulatif des conversions
De → Vers
std_logic_vector
unsigned
signed
integer
std_logic_vector
-
unsigned(x)
signed(x)
to_integer(unsigned(x))
unsigned
std_logic_vector(x)
-
signed(x)
to_integer(x)
signed
std_logic_vector(x)
unsigned(x)
-
to_integer(x)
integer
std_logic_vector(to_unsigned(x,N))
to_unsigned(x,N)
to_signed(x,N)
-
Types énumérés (enumeration)
Les types énumérés permettent de nommer des états ou des codes de façon explicite. C'est le type utilisé dans les machines à états.
-- Déclaration dans la zone architecturaletype t_state is (IDLE, INIT, RUN, DONE, ERROR);type t_couleur is (ROUGE, ORANGE, VERT);type t_direction is (NORD, SUD, EST, OUEST);signal r_state : t_state := IDLE;signal r_couleur : t_couleur := ROUGE;
-- Utilisation dans un processprocess(i_clk)begin if rising_edge(i_clk) then case r_state is when IDLE => r_state <= INIT; when INIT => r_state <= RUN; when RUN => if i_done = '1' then r_state <= DONE; end if; when others => r_state <= IDLE; end case; end if;end process;
Les types énumérés sont automatiquement encodés par le synthétiseur (binaire ou one-hot selon les outils). Il est possible de forcer un encodage particulier via des attributs de synthèse.
Types tableaux définis par l'utilisateur
VHDL permet de définir des tableaux multi-dimensionnels ou des tableaux de types personnalisés.
-- Tableau de 8 octets (ROM simple)type t_rom_8x8 is array (0 to 7) of std_logic_vector(7 downto 0);constant c_ROM : t_rom_8x8 := ( x"00", x"1F", x"3C", x"7E", x"FF", x"7E", x"3C", x"1F");-- Tableau 2D (matrice 4 × 4 de bits)type t_matrice is array (0 to 3, 0 to 3) of std_logic;-- Tableau de std_logic_vector (RAM inférée)type t_ram is array (0 to 255) of std_logic_vector(7 downto 0);signal r_mem : t_ram;
-- RAM synchrone inférée à partir d'un tableauprocess(i_clk)begin if rising_edge(i_clk) then if i_we = '1' then r_mem(to_integer(unsigned(i_addr))) <= i_data; end if; o_data <= r_mem(to_integer(unsigned(i_addr))); end if;end process;
Tableaux non contraints
Un tableau non contraint définit la forme générale du type, mais laisse la taille exacte au moment où le signal, la constante ou le port est déclaré.
subtype t_byte is std_logic_vector(7 downto 0);type t_byte_array is array (natural range <>) of t_byte;signal r_window : t_byte_array(0 to 3);signal r_fifo : t_byte_array(0 to 15);
Le subtype n'est pas obligatoire : il sert surtout à nommer proprement "un octet" et à réutiliser cette définition ailleurs.
type t_byte_array is array (natural range <>) of std_logic_vector(7 downto 0);
En revanche, type t_byte is std_logic_vector(7 downto 0); n'est pas la bonne forme : pour donner un nom à une version contrainte de std_logic_vector, on utilise un subtype.
t_byte_array peut donc servir pour une petite fenêtre de 4 octets, une FIFO de 16 octets ou une table plus grande. Le type reste le même, seule la plage change.