Verilog and SystemVerilog Conversion Functions Explained

Conversion functions play a crucial role in digital design with Verilog and SystemVerilog. They help in handling and converting different data types seamlessly and efficiently. In this tutorial, we'll go over several important conversion functions that will make your life as a designer much easier.

Real Number Conversion Functions

In Verilog and SystemVerilog, several system functions can handle real number values (the real and shortreal types). These functions allow you to convert values to and from real number values and make signed or unsigned values conversions.

The following real number conversion functions are commonly used:

  1. $rtoi: Converts a real value to an integer by truncating it.
  2. $itor: Converts an integer value to a real value.
  3. $realtobits: Converts a real value to a 64-bit vector representation.
  4. $bitstoreal: Converts a 64-bit vector representation back to a real value.
  5. $shortrealtobits: Converts a shortreal value to a 32-bit vector representation.
  6. $bitstoshortreal: Converts a 32-bit vector representation back to a shortreal value.

For more information on real number data types, refer to the post Real, Shortreal, and Realtime Data Types in Verilog and SystemVerilog.

Practical Usage of Real Number Conversion Functions

Let's look at a simple example to better understand real number conversion functions. Suppose we have two modules, DataSender and DataReceiver, which communicate real values with each other. Here's how you could use $realtobits and $bitstoreal functions to convert real values for data transmission:

module DataSender(output logic [63:0] realData);
  real temperature = 25.5;
  assign realData = $realtobits(temperature);
endmodule

module DataReceiver(input logic [63:0] realData);
  real temperature;
  assign temperature = $bitstoreal(realData);
endmodule

In this example, the DataSender module uses $realtobits to convert the temperature real value into a 64-bit vector representation. The DataReceiver module then uses $bitstoreal to convert the 64-bit vector representation back to a real value.

Signedness Conversion Functions

In some scenarios, you may need to convert the signedness of expressions without changing the actual data type. For such situations, you can use $signed and $unsigned functions:

  • $signed: Returns a signed value
  • $unsigned: Returns an unsigned value

Here's an example to demonstrate the usage of signedness conversion functions:

module SignedUnsignedExample;
  logic          [7:0] data = 8'b1010_1010;
  logic signed   [7:0] signedData;
  logic unsigned [7:0] unsignedData;

  initial begin
    signedData   = $signed(data);
    unsignedData = $unsigned(data);

    $display("Original data: %0h", data);
    $display("Signed data: %0h", signedData);
    $display("Unsigned data: %0h", unsignedData);
  end
endmodule

In this example, the $signed function is used to convert the 8-bit data to a signed value, and the $unsigned function is used to convert it to an unsigned value.

Dynamic Casting with $cast Function

Dynamic casting using the $cast function is a powerful feature in SystemVerilog that allows you to assign values to variables that might not ordinarily be valid due to differing data types. The $cast function can be used in two modes - as a task and as a function. Here's how the syntax looks for both modes:

function int $cast(singular destVar, singular sourceExp);

or:

task $cast(singular destVar, singular sourceExp);

destVar is the destination variable to which the assignment is made, while sourceExp is the expression that will be assigned to the destination variable.

Handling Invalid Assignments with $cast

The choice between using $cast as a function or as a task determines how invalid assignments are handled:

  1. As a task: If the assignment is invalid, a run-time error occurs, and the destination variable is left unchanged.
  2. As a function: If the assignment is invalid, the function returns 0, the destination variable is left unchanged, and no run-time error occurs.

Here's a simple example to demonstrate how $cast can be used:

module DynamicCastingExample;
  typedef enum {Red, Green, Blue} Color;
  Color myColor;
  int colorCode;

  initial begin
    colorCode = 1;
    if ($cast(myColor, colorCode)) begin
      $display("Color code: %0d => Color: %s", colorCode, myColor.name());
    end
    else begin
      $display("Invalid color code: %0d", colorCode);
    end
  end
endmodule

In this example, the $cast function is used to check if the colorCode integer can be assigned to the Color enumerated type. If the cast is successful, the assigned color is displayed. If the cast fails, an error message is shown.


Conversion functions in Verilog and SystemVerilog offer designers a convenient way to manage and convert different data types in their designs. We've discussed essential functions for real number conversions, signedness conversions, and dynamic casting. These functions can help simplify your code and enable seamless communication between different modules and data types in your design.