Know this topic? Give it a go. The lesson is here if you need it.
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:
| 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.
Question 1 of 1
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.
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.
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.
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.