Sign in

Learn

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

Practice

Try the challenge first: Repack the Telemetry Word

Know this topic? Give it a go. The lesson is here if you need it.

Vectors and Bit Indexing

A vector groups several bits under one name. Indexing selects a bit; slicing selects a field. Start with the format you need to decode, then choose the indices.

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.

Which field actually 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. 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, not a count: 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. 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. The assignment's source and destination widths must match.

Slicing a fixed field selects connections; it does not perform arithmetic or add a clock cycle. The field meaning comes from the format, not its HDL name.

Assemble a smaller summary

The summary keeps the charging flag followed by the three cell-count bits. It 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. 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. 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.

Find evidence for the wiring

Open Battery Status Word in the example panel on the right. The battery_status source implements the layout above. Follow these waveform 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, predict which traces should remain unchanged. An unchanged field is useful evidence too.

The word-repack exercise gives you a different layout to assemble. Draw its field boundaries before writing syntax.

Key Takeaways

  • Derive indices from the bit-field contract, 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.