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
- A byte is accepted on a clock edge where
in_validandin_readyare both 1. The first byte of a word goes to bits[7:0]ofout_data, the second to[15:8], and so on. - A word is complete at the edge that accepts its fourth byte, or any byte with
in_last1 (a short word). At that edgeout_validbecomes 1 and holds, without_data,out_keepandout_laststable, until an edge whereout_readyis 1. out_keep[i]is 1 when byteiof the word holds a byte; for a short word it is0001,0011or0111, otherwise1111.out_lastis thein_lastof the final byte of the word. The bytes ofout_datawhoseout_keepbit is 0 are don't-care.- The next word starts empty after a word completes, whatever the reason. A byte that arrives after an
in_lastbyte is the first byte of a new word. in_readyis!out_valid: bytes are not accepted while a finished word waits to be taken.rst_nis 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.