RTL Design #66 · 2026-11-28 · Width conversion · Medium · about 25 min

Pack bytes into words with a last flag

Build byte_pack, which collects a stream of bytes into 32-bit words. Packets end with in_last, so a word may be only partly filled.

Requirements

  1. A byte is accepted on a clock edge where in_valid and in_ready are both 1. The first byte of a word goes to bits [7:0] of out_data, the second to [15:8], and so on.
  2. A word is complete at the edge that accepts its fourth byte, or any byte with in_last 1 (a short word). At that edge out_valid becomes 1 and holds, with out_data, out_keep and out_last stable, until an edge where out_ready is 1.
  3. out_keep[i] is 1 when byte i of the word holds a byte; for a short word it is 0001, 0011 or 0111, otherwise 1111. out_last is the in_last of the final byte of the word. The bytes of out_data whose out_keep bit is 0 are don't-care.
  4. The next word starts empty after a word completes, whatever the reason. A byte that arrives after an in_last byte is the first byte of a new word.
  5. in_ready is !out_valid: bytes are not accepted while a finished word waits to be taken.
  6. rst_n is a synchronous, active-low reset. It drops any partly collected word and any word waiting at the output.

Example (out_ready always 1): bytes 11 22 33 44 accepted on consecutive edges give, after the fourth edge, out_valid 1, out_data 44332211, out_keep 1111, out_last 0. Then AA and BB with in_last on BB give, after the second edge, out_valid 1, out_data[15:0] BBAA, out_keep 0011, out_last 1.

Write and run your answer in the browser. The hidden tests check it, and it’s free.