R9.1.2 A Mask for Every Width

EasyImplementIcarus Verilog + Yosys · serverReview the topic

Write a mask generator that is right at every width from 1 to 64 bits, with no literal that assumes a size.

Hardware context: A watchdog counter that reloads 12 and barks at 0; and a mask generator with a WIDTH parameter. Write the masks and the all-ones flag so that they are right at every width.

A bus library needs one mask block for every bus it builds: 8-bit sensors, a 33-bit tagged pointer, a 64-bit data path. The first version worked on the 8-bit bus it was tested on. On the 48-bit bus its "all lanes" mask enabled only the low byte.

This is an Implement problem: write the body of lane_mask. The fixture holds one copy at each of eight widths from 1 to 64. Each test reads the mask and the full flag of one copy.

Brief: parameter WIDTH (1 to 64); mode, k and d in; mask and full out; combinational. Mode 0 gives no bits and mode 1 gives all WIDTH bits. Mode 2 gives bit k alone and mode 3 gives bits k down to 0. full is 1 exactly when all WIDTH bits of d are 1. 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
width8input of each check
mode2input of each check
k6input of each check
Full specification
The module (keep the name, the parameter and the ports)
module lane_mask #(parameter int WIDTH = 8) (
  input  logic [1:0]       mode,     // 0 none, 1 all, 2 one bit, 3 low bits
  input  logic [5:0]       k,        // a bit number, 0 to WIDTH - 1
  input  logic [WIDTH-1:0] d,
  output logic [WIDTH-1:0] mask,
  output logic             full      // 1 when every bit of d is 1
);
  // your design
endmodule
modemaskAt WIDTH = 8, k = 2
0no bit set00000000
1all WIDTH bits set11111111
2bit k set, the others 000000100
3bits k down to 0 set00000111

full depends only on d, whatever mode and k are. It is 1 when every one of the WIDTH bits of d is 1.

The widths under test are 1, 3, 8, 13, 32, 33, 48, 64. The result shows mask (extended with zeros to 64 bits) and full.

Examples

Example 1

All Ones at 8 Bits
Input
WIDTH=8 mode=1 (all) k=0 d=8'hff
Output
mask = 0xff, full = yes

Mode 1 sets all eight bits, and d = ff is a full bus.

Example 2

All Ones at 48 Bits
Input
WIDTH=48 mode=1 (all) k=0 d=48'hffffffffffff
Output
mask = 0xffffffffffff, full = yes

The same mode on a 48-bit copy must set 48 bits. A constant with a size of its own gives fewer.

Example 3

One Bit at 64 Bits
Input
WIDTH=64 mode=2 (one bit) k=63 d=64'hfffffffffffffffe
Output
mask = 0x8000000000000000, full = no

Bit 63 alone is set. d has every bit but bit 0, so full is 0.

Constraints and Contract

  • Inputs are known 0/1 values, and k is always below WIDTH.
  • Keep the module name, the parameter and the ports. The design must be combinational.

Hints

Hint 1

Write each constant so that it has no size of its own. A number with digits has one, even when you leave the size out.

Hint 2

Check every operand of your compare for full. Both sides are extended to the wider one before the compare, and a narrow side gets zeros or sign bits on top.

Hint 3

For the low-bits mask, loop over the WIDTH bits and set bit i when i is at most k. Synthesis unrolls the loop.

What the grader checks: Twenty-two directed and eighteen seeded inputs are applied to copies at eight widths from 1 to 64. An independent model checks mask and full of one copy per input. Passing shows the block is right at these widths on these inputs. The synthesis check builds the default width and 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.

40 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

All Ones at 8 Bits

Input
WIDTH=8 mode=1 (all) k=0 d=8'hff
Expected output
mask = 0xff, full = yes

Mode 1 sets all eight bits, and d = ff is a full bus.

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

Icarus Verilog + Yosys · server