+ -- http://www.velocityreviews.com/forums/showpost.php?p=137148&postcount=5
+ function find_msb(a : std_logic_vector) return std_logic_vector is
+ function bits_to_fit(n : positive) return natural is
+ variable nn, bits : natural := 0;
+ begin
+ nn := n;
+ while nn > 0 loop
+ bits := bits + 1;
+ nn := nn/2;
+ end loop;
+ return bits;
+ end;
+
+ function or_all(p : std_logic_vector) return std_logic is
+ variable r : std_logic;
+ begin
+ r := '0';
+ for i in p'range loop
+ r := r or p(i);
+ end loop;
+ return r;
+ end;
+
+ constant wN : positive := bits_to_fit(a'length - 1);
+ constant wP : positive := 2 ** wN;
+ variable pv : std_logic_vector(wP-1 downto 0);
+ variable n : std_logic_vector(wN downto 1);