Know this already? Skip to the exercise: Repack the Telemetry Word
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:
| Positions | Meaning | Width |
|---|---|---|
| Bit 7 | Charging flag | 1 bit |
| Bits 6 through 4 | Number of cells | 3 bits |
| Bits 3 through 0 | Charge-level code | 4 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
Question 1 of 1
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.
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.
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.
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.