Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 37 ยท Data Types and Signals

Vectors, Arrays and Slices

Work with VHDL buses: std_logic_vector ranges, indexing, slices, concatenation, aggregates with others, array types, 2D arrays and attributes like 'range and 'length.

Real hardware is full of buses: eight data lines, a 32-bit address, sixteen LEDs. In VHDL a bus is an array: a numbered collection of elements of one type. std_logic_vector is an array of std_logic. This lesson shows how to declare, index, slice and initialize arrays, and how to ask an array about its size.

Vector ranges

VHDL
signal data  : std_logic_vector(7 downto 0);   -- bit 7 is leftmost (MSB)
signal flags : std_logic_vector(0 to 3);       -- bit 0 is leftmost

The direction decides which index sits on the left. With 7 downto 0, the literal "10000001" puts '1' in index 7 and index 0. With 0 to 3, the literal "1100" puts '1' in index 0 and index 1. Using downto for numeric values keeps bit numbers equal to weights, which is why it is the norm.

Indexing and slicing

VHDL
data(0) <= '1';                       -- one element: std_logic
data(3 downto 0) <= "1010";           -- a slice: std_logic_vector
data(7 downto 4) <= data(3 downto 0); -- copy low nibble to high nibble

A slice must use the same direction as the declaration: data(0 to 3) on a downto vector is an error (or a null slice). Both sides of an assignment must have the same length, otherwise the tool reports a length mismatch.

Concatenation and aggregates

The & operator joins vectors and single bits:

VHDL
byte  <= nibble_hi & nibble_lo;           -- 4 bits & 4 bits = 8 bits
shift <= data(6 downto 0) & '0';          -- shift left by one
rotl  <= data(6 downto 0) & data(7);      -- rotate left by one

An aggregate lists values for each element, by position or by index:

VHDL
data <= (others => '0');                  -- all zeros: works for any width
data <= (7 => '1', others => '0');        -- bit 7 set
data <= (0 => '1', 7 => '1', others => '0');

(others => '0') is the standard way to say "all zeros" independent of width, and it is safer than typing 8 zeros. VHDL-2008 also permits data <= (others => '1'); in more places, such as in function calls and conditions.

Attributes: asking about size

Attributes are queries written with an apostrophe:

Attribute Meaning For std_logic_vector(7 downto 0)
'left / 'right Leftmost / rightmost index 7 / 0
'high / 'low Largest / smallest index 7 / 0
'length Number of elements 8
'range The index range itself 7 downto 0
'reverse_range Opposite direction 0 to 7

They let you write code that does not hard-code a width:

VHDL
for i in data'range loop
  parity := parity xor data(i);
end loop;

Array types

You can declare your own array types:

VHDL
type byte_array_t is array (0 to 15) of std_logic_vector(7 downto 0);
signal mem : byte_array_t;

type matrix_t is array (0 to 2, 0 to 2) of integer range 0 to 255;
signal m : matrix_t;

mem(3)      <= x"A5";
mem(3)(7)   <= '0';        -- bit 7 of element 3
m(1, 2)     <= 42;

An array of vectors is how memories and register files are modeled. A true 2D array uses m(row, col); an array of arrays uses mem(i)(j). Both synthesize, but large mem arrays are best written in a way the tool recognizes as block RAM (a clocked read and write pattern, see chapter 7).

An unconstrained array type leaves the range open: type word_array_t is array (natural range <>) of std_logic_vector(31 downto 0);. The range is then set where you declare a signal of that type. This is how std_logic_vector itself is defined.

Slicing and concatenation in C

C has no slices, but bit-fields can be extracted and joined with shifts and masks. This stand-in mimics data(7 downto 4), data(3 downto 0), concatenation and a left rotate on an 8-bit value.

C Stand-in (C): slices and concatenation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

A register file

Arrays of vectors model memories. This stand-in is an 8-entry, 8-bit register file with one write and two read ports, similar to what you would build from type reg_file_t is array (0 to 7) of std_logic_vector(7 downto 0).

C Stand-in (C): a small register file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Length mismatches are a frequent compile error: y <= "101"; for an 8-bit y does not pad automatically. Use (others => '0') or resize (Numeric Arithmetic) when widths differ.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. For signal v : std_logic_vector(7 downto 0); what is v'length?

    Indexes 7 down to 0 contain eight elements.
  2. 2. What does v <= (others => '0'); do?

    An aggregate with others fills all elements and does not depend on the width.
  3. 3. Which expression shifts an 8-bit vector d(7 downto 0) left by one, filling with zero?

    Drop the top bit (index 7) by taking d(6 downto 0), then append a zero at the right.
  4. 4. Which is valid access to bit 2 of element 5 of mem (array of 8-bit vectors)?

    For an array of arrays, use successive index parentheses.

Summary

  • A bus is an array; downto ranges keep indexes equal to bit weights.
  • Slices must follow the declared direction; widths on both sides must match.
  • & concatenates; aggregates such as (others => '0') initialize any width.
  • Attributes 'length, 'range, 'high, 'low avoid hard-coded widths.
  • Array types model memories and register files; use (i)(j) for arrays of vectors.

Next: Numeric Arithmetic with numeric_std