1.2.1 Byte-lane extraction

EasyImplementIcarus Verilog · browserReview the topic

Pick the byte that travels in a given position, for either byte order, without losing unknown bits.

Hardware context: Packet bus width adapter with byte lanes and backpressure. Extract memory or stream bus lanes into a monitor transaction without reversing byte order.

A bridge test fails on every write: the DUT stores 32'hEFBEADDE where the test sent 32'hDEADBEEF. The bytes travel most significant first, and somewhere a byte number was used as a lane number.

Write byte_at(word, index, msb_first). It returns the byte that travels in position index, where 0 is the first byte on the bus. When msb_first is 1, the first byte is the most significant one, word[31:24]. When msb_first is 0, the first byte is word[7:0].

For word = 32'hDEADBEEF and index = 0, the answer is 8'hDE most significant first and 8'hEF least significant first.

A word has only four bytes. For any other index, return 8'bx, the same value you get when you read past the end of a logic array. Unknown bits in word must come through unchanged.

Signals in the timeline

SignalWidthRole
word32input of each check
index32input of each check
msb_first1input of each check
Full specification
Signature (keep it)
function automatic logic [7:0] byte_at(input logic [31:0] word, input int index, input bit msb_first);
indexmsb_first = 1msb_first = 0
0word[31:24]word[7:0]
1word[23:16]word[15:8]
2word[15:8]word[23:16]
3word[7:0]word[31:24]
anything else8'bx8'bx

word may hold x and z in any bit; the returned byte must keep them exactly.

Examples

Example 1

Most significant byte first
Input
word = 32'hDEADBEEF, index = 0, msb_first = 1
Output
byte = 11011110 (0xde)

The first byte on the bus is the top byte, word[31:24] = DE.

Example 2

Least significant byte first
Input
word = 32'hDEADBEEF, index = 0, msb_first = 0
Output
byte = 11101111 (0xef)

Now the first byte is lane 0, word[7:0] = EF.

Example 3

Past the last byte
Input
word = 32'hDEADBEEF, index = 4, msb_first = 1
Output
byte = xxxxxxxx

A 32-bit word has bytes 0 to 3 only, so the result is all x.

Constraints and contract

  • index is any signed int; only 0 to 3 name a byte.
  • word is four-state. Keep every x and z in the byte you return.
  • Keep the function name, argument types and 8-bit return type.

Hints

Hint 1

Two numbers are in play: the position on the bus (index) and the lane in the packed word, where lane 0 is the least significant byte. Write down which lane each index maps to for both byte orders.

Hint 2

A part-select of the form word[base +: 8] takes eight bits upward from base. What is base for lane L?

Hint 3

Reject an index outside 0 to 3 before you compute a lane. Return the part-select directly from word, so no two-state variable ever holds the byte.

What this judge verifies: Your function is called for indexes -1 to 4 and 100, in both byte orders, on directed words and seeded four-state words. Passing shows the right byte comes back, with its x and z bits intact.

199 checks on Icarus Verilog · browser. Run the examples first, then check the full regression. The suite is local and inspectable. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 5 steps and the final capstone
Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Most significant byte first

Input
word = 32'hDEADBEEF, index = 0, msb_first = 1
Expected output
byte = 11011110 (0xde)

The first byte on the bus is the top byte, word[31:24] = DE.

Run samples executes all 3 example checks. Check all tests runs 199 checks.

Icarus Verilog · browser