Skip to content

Use templated functions for the Verilog API #11

@SeanMcLoughlin

Description

@SeanMcLoughlin

longint is the sole type that is used in the Verilog API. This makes sense given the limitations of the DPI, but I often end up with compiler warnings due to automatic casts from longint <-> whatever the type of my knob is.

For example, bit types are common for boolean knobs, and the implied casting gives several warnings on this line:

bit my_bit = 0;
// WARNING 1. get_value casts longint -> bit
// WARNING 2. default_value param casts bit -> longint
my_bit = sknobs::get_value("my_value", my_bit); 

In order to avoid these warnings, I have to manually cast everything in a very ugly manner:

bit my_bit = bit'(sknobs::get_value("my_value", longint'(my_bit)));

What I'm proposing is to wrap the API in a templated class, and have the API do the casting internally.

// sknobs.sv
class sknobs #(type T=longint);
  static function T get_value(string name, T default_value=0);
    get_value = T'(sknobs_get_value(name, longint'(default_value)));
  endfuncion
endclass : sknobs

That way, my ugly casting example can become the following in user code:

bit my_bit = 0;
my_bit = sknobs#(bit)::get_value("my_value", my_bit);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions