G4.8.1 Signed and Unsigned Comparator

MediumImplementIcarus Verilog + Yosys · serverReview the topic

Compare two bytes that each come as signed or unsigned, by the numbers they stand for.

Hardware context: A data logger with mixed signed and unsigned sensors, and a deadline scheduler built from comparator cells. Compare bytes that each carry their own signed or unsigned format bit.

A data logger compares a reading with a threshold. Some sensors send signed bytes and some unsigned ones, and so do the threshold registers. Each operand comes with a format bit. A plain compare in SystemVerilog goes unsigned as soon as one side is unsigned, so the logger made wrong calls.

This is an Implement problem. Write cmp_mixed. sa = 1 means a is two's complement; sa = 0 means a is unsigned. sb does the same for b. lt, eq and gt compare the numbers the two bytes stand for. So the byte ff read as −1 is less than the byte ff read as 255.

The check applies eight comparisons, one per nanosecond, and records lt, eq and gt after each. The check also synthesizes the module on the site's flow and rejects any latch.

Signals in the Timeline

SignalWidthRole
script144input of each check
Full specification
The module (keep the name and the ports)
module cmp_mixed (
  input  logic [7:0] a, b,
  input  logic       sa,         // 1: a is a signed (two's complement) byte; 0: unsigned
  input  logic       sb,         // the same for b
  output logic       lt, eq, gt  // compare the values the bytes stand for
);
  // your design
endmodule
Format bitThe byte stands for
00 to 255
1−128 to 127 (two's complement)
OutputRule
lt1 when the value of a is less than the value of b.
eq1 when the two values are equal; equal bytes in different formats can differ.
gt1 when the value of a is greater. Exactly one of lt, eq and gt is 1.

The result fields are lt_stepK, eq_stepK and gt_stepK for the steps 0 to 7.

Examples

Example 1

Both Unsigned
Input
a=01 (unsigned, 1) b=02 (unsigned, 2); a=c8 (unsigned, 200) b=64 (unsigned, 100); a=ff (unsigned, 255) b=01 (unsigned, 1); a=07 (unsigned, 7) b=07 (unsigned, 7); a=00 (unsigned, 0) b=ff (unsigned, 255); a=80 (unsigned, 128) b=7f (unsigned, 127); a=09 (unsigned, 9) b=0a (unsigned, 10); a=80 (unsigned, 128) b=80 (unsigned, 128)
Output
lt_step0 = yes, eq_step0 = no, gt_step0 = no, lt_step1 = no, eq_step1 = no, gt_step1 = yes, lt_step2 = no, eq_step2 = no, gt_step2 = yes, lt_step3 = no, eq_step3 = yes, gt_step3 = no, lt_step4 = yes, eq_step4 = no, gt_step4 = no, lt_step5 = no, eq_step5 = no, gt_step5 = yes, lt_step6 = yes, eq_step6 = no, gt_step6 = no, lt_step7 = no, eq_step7 = yes, gt_step7 = no

With both format bits 0 the bytes compare as 0 to 255: ff is greater than 01.

Example 2

Both Signed
Input
a=ff (signed, -1) b=01 (signed, 1); a=01 (signed, 1) b=ff (signed, -1); a=80 (signed, -128) b=7f (signed, 127); a=7f (signed, 127) b=80 (signed, -128); a=fb (signed, -5) b=fb (signed, -5); a=fb (signed, -5) b=fa (signed, -6); a=00 (signed, 0) b=ff (signed, -1); a=64 (signed, 100) b=63 (signed, 99)
Output
lt_step0 = yes, eq_step0 = no, gt_step0 = no, lt_step1 = no, eq_step1 = no, gt_step1 = yes, lt_step2 = yes, eq_step2 = no, gt_step2 = no, lt_step3 = no, eq_step3 = no, gt_step3 = yes, lt_step4 = no, eq_step4 = yes, gt_step4 = no, lt_step5 = no, eq_step5 = no, gt_step5 = yes, lt_step6 = no, eq_step6 = no, gt_step6 = yes, lt_step7 = no, eq_step7 = no, gt_step7 = yes

With both format bits 1, ff is −1, less than 1; and −128 is less than 127.

Example 3

The Same Byte Read Four Ways
Input
a=ff (unsigned, 255) b=ff (unsigned, 255); a=ff (signed, -1) b=ff (signed, -1); a=ff (signed, -1) b=ff (unsigned, 255); a=ff (unsigned, 255) b=ff (signed, -1); a=80 (signed, -128) b=80 (unsigned, 128); a=80 (unsigned, 128) b=80 (signed, -128); a=7f (signed, 127) b=7f (unsigned, 127); a=7f (unsigned, 127) b=7f (signed, 127)
Output
lt_step0 = no, eq_step0 = yes, gt_step0 = no, lt_step1 = no, eq_step1 = yes, gt_step1 = no, lt_step2 = yes, eq_step2 = no, gt_step2 = no, lt_step3 = no, eq_step3 = no, gt_step3 = yes, lt_step4 = yes, eq_step4 = no, gt_step4 = no, lt_step5 = no, eq_step5 = no, gt_step5 = yes, lt_step6 = no, eq_step6 = yes, gt_step6 = no, lt_step7 = no, eq_step7 = yes, gt_step7 = no

ff against ff is equal only when both have the same format. Signed ff is −1 and unsigned ff is 255, so a mixed pair is not equal.

Constraints and Contract

  • Inputs are known 0/1 values. Each comparison is held for one nanosecond and the outputs are sampled at its end.
  • Keep the module name and the ports. The design is combinational.

Hints

Hint 1

Every value an operand can stand for, from −128 to 255, fits in a signed 9-bit number. Widen both operands to that.

Hint 2

Widen a signed byte by copying its top bit into bit 8; widen an unsigned byte with a 0. The format bit chooses.

Hint 3

Declare the two 9-bit values signed. Then the ordinary operators <, == and > give the right answers.

What the grader checks: Six directed and ten seeded cases of eight steps each cover every mix of formats. lt, eq and gt are compared with a model written in JavaScript after every step. The synthesis check rejects a latch.

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

Both Unsigned

Input
a=01 (unsigned, 1) b=02 (unsigned, 2); a=c8 (unsigned, 200) b=64 (unsigned, 100); a=ff (unsigned, 255) b=01 (unsigned, 1); a=07 (unsigned, 7) b=07 (unsigned, 7); a=00 (unsigned, 0) b=ff (unsigned, 255); a=80 (unsigned, 128) b=7f (unsigned, 127); a=09 (unsigned, 9) b=0a (unsigned, 10); a=80 (unsigned, 128) b=80 (unsigned, 128)
Expected output
lt_step0 = yes, eq_step0 = no, gt_step0 = no, lt_step1 = no, eq_step1 = no, gt_step1 = yes, lt_step2 = no, eq_step2 = no, gt_step2 = yes, lt_step3 = no, eq_step3 = yes, gt_step3 = no, lt_step4 = yes, eq_step4 = no, gt_step4 = no, lt_step5 = no, eq_step5 = no, gt_step5 = yes, lt_step6 = yes, eq_step6 = no, gt_step6 = no, lt_step7 = no, eq_step7 = yes, gt_step7 = no

With both format bits 0 the bytes compare as 0 to 255: ff is greater than 01.

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

Icarus Verilog + Yosys · server