R10.1.1 Sign-Extend and Pack

EasyImplementIcarus Verilog + Yosys · serverReview the topic

Pack two signed deltas of different widths, a tag, two buttons and a flag into one 32-bit report. Use only wiring and one gate.

Hardware context: A 32-bit motion report packed from two signed deltas, a tag and two buttons; and a parity generator and checker at six widths. Pack the motion report from its layout table.

A pointing device sends a motion report every millisecond. The sensor gives a 5-bit signed dx and a 7-bit signed dy. The host wants both as 12-bit signed fields in one 32-bit word. The tag, the two buttons, an any-button flag and a fixed marker sit above them.

This is an Implement problem: write the body of pack_report. The fixture applies two input sets per case, one per nanosecond, and records word after each.

Brief: combinational, no clock and no reset. The layout is in the specification. A negative delta must read as the same negative number in its 12-bit field. 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

SignalWidthRole
sets34input of each check
Full specification
The module (keep the name and the ports)
module pack_report (
  input  logic [4:0]  dx,       // signed, -16 to 15
  input  logic [6:0]  dy,       // signed, -64 to 63
  input  logic [2:0]  tag,
  input  logic [1:0]  btn,
  output logic [31:0] word
);
  // your design
endmodule
Bits of wordFieldValue
31:29tagthe tag input
28:27btnthe two buttons
26any1 when at least one button is 1
25:24markeralways binary 10
23:12dx12dx as a 12-bit signed number
11:0dy12dy as a 12-bit signed number

The result shows the fields of both words: tag_0 to dy12_0 for the first input set, tag_1 to dy12_1 for the second.

Examples

Example 1

Small Positive Deltas
Input
tag=1 btn=00 dx=3 dy=5; tag=2 btn=00 dx=15 dy=63
Output
tag_0 = 1, btn_0 = 'b00, any_0 = no, marker_0 = 'b10, dx12_0 = 3, dy12_0 = 5, tag_1 = 2, btn_1 = 'b00, any_1 = no, marker_1 = 'b10, dx12_1 = 15, dy12_1 = 63

Positive deltas get zeros on top. No button is down, so any is 0. The marker is 10 in both words.

Example 2

Negative Deltas
Input
tag=5 btn=01 dx=-1 dy=-1; tag=7 btn=10 dx=-16 dy=-64
Output
tag_0 = 5, btn_0 = 'b01, any_0 = yes, marker_0 = 'b10, dx12_0 = -1, dy12_0 = -1, tag_1 = 7, btn_1 = 'b10, any_1 = yes, marker_1 = 'b10, dx12_1 = -16, dy12_1 = -64

-1 stays -1 and -16 stays -16 in 12 bits: the bits above the value repeat its sign bit.

Example 3

Mixed Signs
Input
tag=0 btn=11 dx=-7 dy=9; tag=3 btn=01 dx=12 dy=-33
Output
tag_0 = 0, btn_0 = 'b11, any_0 = yes, marker_0 = 'b10, dx12_0 = -7, dy12_0 = 9, tag_1 = 3, btn_1 = 'b01, any_1 = yes, marker_1 = 'b10, dx12_1 = 12, dy12_1 = -33

Each delta is extended by its own sign bit. dx = -7 with dy = 9 must not make dy negative.

Constraints and Contract

  • Inputs are known 0/1 values. Each input set is held for one nanosecond and word is sampled at its end.
  • Keep the module name and the ports.

Hints

Hint 1

Add up the widths in the table first: 3 + 2 + 1 + 2 + 12 + 12 must be 32.

Hint 2

A 12-bit signed copy of a 5-bit number needs seven more bits on top. What must those seven bits be for a negative number?

Hint 3

Count the copies for each delta separately: 12 - 5 for dx and 12 - 7 for dy. If the braces hold fewer than 32 bits, every field above the short one moves down.

What the grader checks: Five directed and eleven seeded cases apply two input sets each. An independent model checks all six fields of each word. Passing shows the layout and both sign extensions are right on these inputs; the synthesis check adds the latch count.

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.

16 checks on Icarus Verilog + 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

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Small Positive Deltas

Input
tag=1 btn=00 dx=3 dy=5; tag=2 btn=00 dx=15 dy=63
Expected output
tag_0 = 1, btn_0 = 'b00, any_0 = no, marker_0 = 'b10, dx12_0 = 3, dy12_0 = 5, tag_1 = 2, btn_1 = 'b00, any_1 = no, marker_1 = 'b10, dx12_1 = 15, dy12_1 = 63

Positive deltas get zeros on top. No button is down, so any is 0. The marker is 10 in both words.

Run samples executes all 3 public checks. Check all tests runs 16 checks on the server.

Icarus Verilog + Yosys · server