Sign in

Learn

0/8
0/6
0/6
0/6

Practice

Know this already? Skip to the exercise: Repack the Telemetry Word

Vectors and Bit Indexing

A vector groups several bits under one name. Indexing selects one bit and slicing selects a field. Start from the format you need to decode, and the indices follow from it.

A battery status byte has this layout:

PositionsMeaningWidth
Bit 7Charging flag1 bit
Bits 6 through 4Number of cells3 bits
Bits 3 through 0Charge-level code4 bits

Using binary and hexadecimal, B4 is 1011_0100. Split it as 1 | 011 | 0100: charging is one, the cell count is three, and the level code is four.

Quick check

Which field changed?

Question 1 of 1

Declare the bit positions

signal status_i : std_logic_vector(7 downto 0);

Both declarations describe eight positions, 7 through 0 inclusive, and the logic type applies to each position separately. The course uses descending ranges, with the most significant bit on the left.

For this zero-based unsigned layout, bit 7 has weight 128 and bit 0 has weight 1. An index is a position rather than a count, so positions 6 through 4 include three bits.

Select a bit or a slice

The example decodes the charging flag and cell field with:

charging_o <= status_i(7);
cells_o    <= status_i(6 downto 4);

A single-bit selection produces one bit, while the cell slice produces a three-bit vector, so cells_o has width three. The level output similarly selects bits 3 through 0.

Use downto for slices of this descending vector, and make sure the assignment's source and destination widths match.

Slicing a fixed field only selects connections; it performs no arithmetic and adds no clock cycle. A field's meaning comes from the format rather than from its HDL name.

Assemble a smaller summary

The summary keeps the charging flag followed by the three cell-count bits and omits the level field entirely.

summary_o(3)          <= status_i(7);
summary_o(2 downto 0) <= status_i(6 downto 4);

These assignments drive disjoint output slices, so each output bit has one driver. The & concatenation operator can also assemble fields; the next topic develops that form.

For B4 the summary is 1011, and for 34 it is 0011. A change from 34 to 3C must not affect the summary because both words share the retained fields.

CAUTION

Common Mistake: overlapping output slices creates multiple drivers on the shared bits. Cover every output bit once when assembling a word.

Check the wiring in the waveform

Open Battery Status Word in the example panel on the right. The battery_status source implements the layout above, and the waveform steps through these transitions:

  • B4 to 34 changes only bit 7. Charging and the summary's charging bit change.
  • 34 to 3C changes only the level field. Cells, charging, and summary hold.
  • 3C to FC changes charging and cell count while the level stays at twelve.

Before reading the corresponding outputs, decide which traces should remain unchanged. A trace that holds still confirms the wiring just as much as one that moves.

The word-repack exercise gives you a different layout to assemble, and it is worth drawing its field boundaries before writing any syntax.

Key Takeaways

  • Derive indices from the bit-field layout, including both endpoints of a slice.
  • Fixed slices select parts of a value without changing the other fields.
  • Test one changing field at a time, and check which outputs must remain unchanged.
Loading editor...
No waveform is available for this example. You can continue with the lesson and example code.