Recherche…


Introduction

La récursivité est une méthode de programmation où les sous-programmes s'appellent eux-mêmes. Il est très pratique de résoudre certains types de problèmes de manière élégante et générique. VHDL prend en charge la récursivité. La plupart des synthétiseurs logiques le prennent également en charge. Dans certains cas, le matériel inféré est encore meilleur (plus rapide, même taille) que la description équivalente en boucle.

Calcul du poids de Hamming d'un vecteur

-- loop-based version
function hw_loop(v: std_logic_vector) return natural is
  variable h: natural;
begin
  h := 0;
  for i in v'range loop
    if v(i) = '1' then
      h := h + 1;
    end if;
  end loop;
  return h;
end function hw_loop;

-- recursive version
function hw_tree(v: std_logic_vector) return natural is
  constant size: natural := v'length;
  constant vv: std_logic_vector(size - 1 downto 0) := v;
  variable h: natural;
begin
  h := 0;
  if size = 1 and vv(0) = '1' then
    h := 1;
  elsif size > 1 then
    h := hw_tree(vv(size - 1 downto size / 2)) + hw_tree(vv(size / 2 - 1 downto 0));
  end if;
  return h;
end function hw_tree;


Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow