G4.9.3 One Funnel, Four Shifts

MediumImplementIcarus · browser samples / server checksReview the topic

Make one funnel shifter give logical right, arithmetic right, rotate right and rotate left shifts.

Hardware context: A cipher round that rotates a word by a key-dependent amount, and a sensor filter that scales signed samples. Drive four shift instructions through one funnel shifter.

A small processor has one funnel shifter. It takes two 8-bit words, hi and lo, shifts {hi, lo} right by n and keeps the low eight bits. Four shift instructions must all use it.

This is an Implement problem. Write shifter8 around one instance of the read-only funnel8, named u_fun. mode picks the shift by n places: 0 logical right, 1 arithmetic right, 2 rotate right, 3 rotate left. x is two's complement. Choose what goes into hi and how far the funnel shifts.

The check applies eight shifts per case. After each it records y, and it forces the funnel's output to check that y comes from it. Shift and compare operators are refused, and the funnels are counted.

Signals in the Timeline

SignalWidthRole
script104input of each check
Full specification
The module (keep the name and the ports)
module shifter8 (
  input  logic [7:0] x,
  input  logic [2:0] n,          // 0 to 7 places
  input  logic [1:0] mode,       // 0 logical right, 1 arithmetic right, 2 rotate right, 3 rotate left
  output wire  [7:0] y
);
  // your design
endmodule
funnel8.sv: the funnel (read-only)
// funnel8.sv: the funnel shifter (read-only). Instantiate it once, as u_fun.
// y is the low 8 bits of {hi, lo} shifted right by n. Every copy adds 1 to tb.n_fun.
module funnel8 (
  input  logic [7:0] hi, lo,
  input  logic [2:0] n,
  output wire  [7:0] y
);
  wire [15:0] w = {hi, lo} >> n;
  assign y = w[7:0];
  initial tb.n_fun++;
endmodule

modey
0x shifted right by n, zeros in
1x shifted right by n, copies of x[7] in
2x rotated right by n
3x rotated left by n

The result fields are y_stepK (in binary) and from_funnel_stepK for the steps 0 to 7, and funnel_count_wrong.

Examples

Example 1

Each Mode by 2
Input
logical right of 10110010 by 2; arithmetic right of 10110010 by 2; rotate right of 10110010 by 2; rotate left of 10110010 by 2; logical right of 01110010 by 2; arithmetic right of 01110010 by 2; rotate right of 01110010 by 2; rotate left of 01110010 by 2
Output
y_step0 = 'b00101100, from_funnel_step0 = yes, y_step1 = 'b11101100, from_funnel_step1 = yes, y_step2 = 'b10101100, from_funnel_step2 = yes, y_step3 = 'b11001010, from_funnel_step3 = yes, y_step4 = 'b00011100, from_funnel_step4 = yes, y_step5 = 'b00011100, from_funnel_step5 = yes, y_step6 = 'b10011100, from_funnel_step6 = yes, y_step7 = 'b11001001, from_funnel_step7 = yes, funnel_count_wrong = no

Shifted right by 2, 10110010 gets zeros in logical mode and copies of its sign in arithmetic mode. Rotated, the two bits that leave come back on the other side.

Example 2

Shift by 0 in Every Mode
Input
logical right of 10110010 by 0; arithmetic right of 10110010 by 0; rotate right of 10110010 by 0; rotate left of 10110010 by 0; logical right of 00000001 by 0; arithmetic right of 10000000 by 0; rotate right of 10000001 by 0; rotate left of 10000001 by 0
Output
y_step0 = 'b10110010, from_funnel_step0 = yes, y_step1 = 'b10110010, from_funnel_step1 = yes, y_step2 = 'b10110010, from_funnel_step2 = yes, y_step3 = 'b10110010, from_funnel_step3 = yes, y_step4 = 'b00000001, from_funnel_step4 = yes, y_step5 = 'b10000000, from_funnel_step5 = yes, y_step6 = 'b10000001, from_funnel_step6 = yes, y_step7 = 'b10000001, from_funnel_step7 = yes, funnel_count_wrong = no

A shift by 0 leaves x unchanged in every mode, rotate left included.

Example 3

Shift by 7 in Every Mode
Input
logical right of 10000000 by 7; arithmetic right of 10000000 by 7; rotate right of 10000000 by 7; rotate left of 10000000 by 7; logical right of 11111111 by 7; arithmetic right of 01111111 by 7; rotate right of 00000001 by 7; rotate left of 00000001 by 7
Output
y_step0 = 'b00000001, from_funnel_step0 = yes, y_step1 = 'b11111111, from_funnel_step1 = yes, y_step2 = 'b00000001, from_funnel_step2 = yes, y_step3 = 'b01000000, from_funnel_step3 = yes, y_step4 = 'b00000001, from_funnel_step4 = yes, y_step5 = 'b00000000, from_funnel_step5 = yes, y_step6 = 'b00000010, from_funnel_step6 = yes, y_step7 = 'b10000000, from_funnel_step7 = yes, funnel_count_wrong = no

By 7, only one bit of x stays in the word for a shift; a rotate keeps them all.

Constraints and Contract

  • Inputs are known 0/1 values. Each shift is held for 3 ns: y is sampled after 1 ns, then the check briefly forces the funnel's output.
  • Keep the module name and the ports, and name the funnel instance u_fun. The < and > characters are refused, so no shift operator can stand in for the funnel.

Hints

Hint 1

The funnel shifts {hi, lo} right. With x on lo, the bits that come in from the left are the low bits of hi. Pick hi to make each kind of fill.

Hint 2

For a rotate, the bits that leave on the right must come back on the left. Which word has those bits?

Hint 3

A rotate left by n is a rotate right by 8 − n. In three bits, 8 − n is the two's-complement negative of n.

What the grader checks: Six directed and eight seeded cases of eight shifts each cover every mode and amount. y is compared with a model written in JavaScript after every step. The check forces the funnel's output to see that y follows it, and counts the funnels. The < and > characters are refused before the run.

14 checks on Icarus · browser samples / server checks. 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 3 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

Each Mode by 2

Input
logical right of 10110010 by 2; arithmetic right of 10110010 by 2; rotate right of 10110010 by 2; rotate left of 10110010 by 2; logical right of 01110010 by 2; arithmetic right of 01110010 by 2; rotate right of 01110010 by 2; rotate left of 01110010 by 2
Expected output
y_step0 = 'b00101100, from_funnel_step0 = yes, y_step1 = 'b11101100, from_funnel_step1 = yes, y_step2 = 'b10101100, from_funnel_step2 = yes, y_step3 = 'b11001010, from_funnel_step3 = yes, y_step4 = 'b00011100, from_funnel_step4 = yes, y_step5 = 'b00011100, from_funnel_step5 = yes, y_step6 = 'b10011100, from_funnel_step6 = yes, y_step7 = 'b11001001, from_funnel_step7 = yes, funnel_count_wrong = no

Shifted right by 2, 10110010 gets zeros in logical mode and copies of its sign in arithmetic mode. Rotated, the two bits that leave come back on the other side.

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

Icarus · browser samples / server checks