Reverse the byte order of a write word for any number of bytes, and move each write strobe with its byte.
Hardware context: A rotate-left block for words of 8 to 32 bits with an amount that can pass the width; and an endian swap for a write channel with byte strobes. Design the endian swap that moves each write strobe with its byte.
A network block keeps the first byte of a header in the top bits of its word. The bus it writes to wants that byte in the bottom bits. A small block between them reverses the byte order when a mode bit asks for it.
The data is not the whole job. Each byte travels with a write strobe that says whether the byte is valid. When byte 0 moves to the top, its strobe must move with it, or the bus writes the wrong bytes.
This is an Implement problem: write the body of endian_swap. The tests use a 4-byte and an 8-byte copy. Brief: parameter BYTES. Ports swap, d[8*BYTES-1:0] and strb[BYTES-1:0] in, q and qstrb out. The block is combinational. With swap = 0 both pass unchanged. With swap = 1, byte k of d becomes byte BYTES-1-k of q, and strb[k] becomes qstrb[BYTES-1-k]. Synthesis check: the design must build on the site's synthesis flow with no latch. The cell and level counts come from an open-source flow and are a proxy for area and depth, not a foundry result.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
data | 64 | input of each check |
strobes | 8 | input of each check |
Full specification
module endian_swap #(parameter int BYTES = 4) (
input logic swap, // 1: reverse the byte order
input logic [8*BYTES-1:0] d, // write data; byte 0 is bits 7:0
input logic [BYTES-1:0] strb, // strb[k] = 1: byte k of d is valid
output logic [8*BYTES-1:0] q,
output logic [BYTES-1:0] qstrb // qstrb[k] = 1: byte k of q is valid
);
// your design
endmodule
| swap | q | qstrb |
|---|---|---|
| 0 | d | strb |
| 1 | the bytes of d in the opposite order; the bits inside each byte keep their order | the bits of strb in the opposite order |
Byte k is bits 8k+7 to 8k. Each test sets swap, a 64-bit data word and 8 strobe bits, and reads one of the two copies. The 4-byte copy takes the low 32 bits and the low 4 strobes.
Simulator note: This problem runs on Verilator 5.052 through the judge, because Icarus 12 refuses streaming operators. You may solve it with or without them.
Examples
Example 1
A 32-bit Word, Swapped- Input
BYTES=4 swap=1 d=0x11223344 strb=1111
- Output
q = 0x44332211, qstrb = 'b1111
The four bytes 11, 22, 33, 44 leave as 44, 33, 22, 11. All four strobes are 1, so they look unchanged.
Example 2
One Valid Byte- Input
BYTES=4 swap=1 d=0x000000a5 strb=0001
- Output
q = 0xa5000000, qstrb = 'b1000
Byte 0 holds a5 and only strb[0] is 1. After the swap the byte is at the top, and so is its strobe: 1000.
Example 3
A 64-bit Word, Swapped- Input
BYTES=8 swap=1 d=0x0102030405060708 strb=11111111
- Output
q = 0x807060504030201, qstrb = 'b11111111
The eight bytes change places end for end. The same source line does it, because the block size is a byte at any width.
Constraints and Contract
- Inputs are known 0/1 values, held one nanosecond; q and qstrb are read at its end.
- Keep the module name, the parameter and the ports. The synthesis check uses the default BYTES of 4.
Hints
Hint 1
A byte swap moves 8-bit blocks. A streaming operator with a block size of 8 does it for any width.
Hint 2
The strobes are one bit per byte. Ask what block size reverses them.
Hint 3
Give q and qstrb their pass-through values first, then overwrite both when swap is 1. A streaming operator must be assigned to a variable of the right width.
What the grader checks: Nine directed and twelve seeded words are applied to a 4-byte and an 8-byte copy, with swap at 0 and 1. q and qstrb are compared with an independent model. Passing shows the block follows the brief at these two widths; the synthesis check adds the latch count at BYTES = 4.
The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.
21 checks on Verilator + Yosys · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 2 steps and the final capstone