Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Last active June 12, 2020 06:09
Show Gist options
  • Save pervognsen/28c9b68815bb302d356cc0cd73011fdd to your computer and use it in GitHub Desktop.
Save pervognsen/28c9b68815bb302d356cc0cd73011fdd to your computer and use it in GitHub Desktop.

From this paper: http://fpgacpu.ca/fpga/hdl/Tumbush%20DVCon%2005.pdf

  1. If any operand in an operation is unsigned the entire operation is unsigned.
  2. Investigate fully all "signed to unsigned conversion occurs" synthesis warnings. These point to incorrect functionality.
  3. All signed operands will be sign-extended to match the size of the largest signed operand.
  4. Type casting using $unsigned will make the operation unsigned. The operand will be extended with 0’s if necessary.
  5. Type casting using $signed makes the operand signed. The operand will be sign-extended with 1's if necessary. Pad the operand with a single 0 bit before the cast if this is not desired.
  6. Expression type depends only on the operands or operation; it does not depend on the LHS of the expression.

Additional points based on feedback from Owen Shepherd:

  1. In SystemVerilog, signed' and unsigned' are the preferred way of doing things.
  2. Do explicit widening with N'(x) rather than relying on implicit widening. (This is a SystemVerilog only feature.)
  3. Instead of 0 padding as in the paper's point 5, you can use explicit pre-widening, e.g. $signed(32'(x)).
@tommythorn
Copy link

tommythorn commented Jun 12, 2020

$ verilator --lint-only top.v
%Warning-WIDTH: top.v:2: Operator ASSIGNW expects 32 bits on the Assign RHS, but Assign RHS's UNSIGNED generates 12 bits.
                       : ... In instance top
   assign larger = $unsigned(small12);
                 ^
                ... Use "/* verilator lint_off WIDTH */" and lint_on around source to disable this message.
%Error: Exiting due to 1 warning(s)

However it was a good thing I made this example because I hadn't noticed that there were a way to turn off this BS!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment