Search code examples
vhdl

Split n long bit logic vector to n individual binary values


I was wondering if there was a method where you can split a n bit long vector for example:

10100111

Into n individual binary units to be used later on? I'm trying to incorporate a method where i can get a 8 bit long vector and have 8 LED's light up depending if that n'th value is 0 or 1. Googling the question returns people cutting up a larger vector into smaller ones, but if we were to have 16 bits for example then i'd have to make 16 separate variables for it work using:

entity test is
port (
 myVector : in std_logic_vector(16 downto 0);
 LED : out std_logic_vector(16 downto 0)
);
END test

architecture behavior of test is
 SIGNAL led1 : std_logic;
 ...
 SIGNAL led16 : std_logic;
BEGIN

 led1 <= myVector(0);
 ...
 led16 <= myVector(16);

 LED(1) <= '1' when led1 = '1' else '0';
 ...
 LED(16) <= '1' when led16 = '1' else '0';
END behavior

Doesn't seem tidy when it needs to be repeated several times in the code.


Solution

  • If you want to declare and assign to identifiers with different names, then you have to make a line for each name, since there is no loop statement for identifier name manipulation in VHDL.

    But VHDL provides arrays, where you can loop over the entries, like with this example code:

    ...
       signal led_sig  : std_logic_vector(16 downto 0);
    begin
      loop_g: for idx in myVector'range generate
        led_sig(idx) <= myVector(idx);
        LED(idx)     <= '1' when led_sig(idx) = '1' else '0';
      end generate;
    ...
    

    Where the assigns are equivalent to the shorter:

    led_sig <= myVector;
    LED     <= led_sig;