Store multiple variables as a single hex string - verilog

I am looking for a way to store a few variables in one variable so I can output a string. For example, I have variables:
int flow_val = "128";
int numb_val = "104";
int size_val = "256";
I can put them together using $display like this:
$display("32'h%0h%0h_%4h", flow_val, numb_val, size_val);
to print out:
32'h8068_0100
Is there a way to get that output and put it into a variable such as hex_val?

$sformatf can be used. Refer to IEEE Std 1800-2012, section 21.3.3 Formatting data to a string.
module tb;
int flow_val = 128;
int numb_val = 104;
int size_val = 256;
string hex_val;
initial begin
hex_val = $sformatf("32'h%0h%0h_%4h", flow_val, numb_val, size_val);
$display(hex_val);
end
endmodule
Output:
32'h8068_0100

Related

UVM indexing into array by get_type_name

Is this possible? Get_type_name is a string. Can't I have an int array and use the name to index in? I get index expression type of illegal.
Obj n1;
int number[100];
n1 = new();
number[n1.get_type_name] = 1;
Long day. Should have declared int number[string]

In python: unable to convert variable with uint32_t, uint8_t, int16_t value to hexadecimal

I have a .ini file with values as below
[Value1]
data_type = uint16_t
value = 0x0001U
[Value2]
data_type = uint32_t
value = 0x00000002UL
[Value4]
data_type = uint8_t
value = 5U
I am unable to convert these values to hexadecimal as below
Comment: I am easily able to read .ini file using configparser. Let assume i have value as string in variable var and I want to convert that string variable to hex form
print (hex(var)) #this should print the hexadecimal value
This doesn't work:
var = '0x00000002UL'
hex(var)
Because hex() is meant to convert in the opposite direction. Instead, try this:
var = '0x00000002UL'
int(var[:-2], 16)
Note you need to skip the UL on the end because that's not Python syntax.

QT. Is any way to multiplicate string several times?

In python i can write
s = "dad" * 3
Result will be: s = "daddaddad"
I want to append "tabs" to my string. Something like:
QString tabs = "\t" * count;
What would be a simple, idiomatic way to do it?
You can do it quite simply with a loop:
QString mystring("somestring");
QString output;
for (int i = 0; i < 3; ++i)
output.append(mystring);
//'output' will contain the result string
Please note that the code I provide is in C++, not Python, but the concept still applies (and should be easily ported).
EDIT:
If you need to concatenate single characters, you could do it more easily like this:
int size = 5;
QString output(size, QChar('\t'));
//'output' contains 5 tab characters
Or, if you need to assign to another string (output is already created):
int size = 5;
output.fill(QChar('\t'), size);
//'output' contains 5 tab characters
#include <QString>
QString s;
for(int i = 0; i < 3; ++i)
{
s << "dad";
}

Parameterized Bit-fields in verilog

Is it possible to parameterize a bit-field in verilog? Essentially I want to use a parameter or alternative to define a bit-range. The only way I can think of doing this is with a `define as shown below but it seems like there should be a better way.
`define BITFIELD_SELECT 31:28
foo = bar[BITFIELD_SELECT]
Parameters are nicer(safer) than defines since the namespace is not global to the project. You should be able to do this with two parameters.
parameter BITFIELD_HIGH = 31;
parameter BITFIELD_LOW = 28;
assign foo = bar[BITFIELD_HIGH:BITFIELD_LOW];
Alternatively
parameter BITFIELD_HIGH = 31;
localparam BITFIELD_LOW = BITFIELD_HIGH-3;
assign foo = bar[BITFIELD_HIGH:BITFIELD_LOW];
If you use macros (define) include the "`" when call the macro
`define BITFIELD_SELECT 31:28
foo = bar[`BITFIELD_SELECT]; // `BITFIELD_SELECT

stick integer to string and char*

How can I add an integer variable to a string and char* variable? for example:
int a = 5;
string St1 = "Book", St2;
char *Ch1 = "Note", Ch2;
St2 = St1 + a --> Book5
Ch2 = Ch1 + a --> Note5
Thanks
The C++ way of doing this is:
std::stringstream temp;
temp << St1 << a;
std::string St2 = temp.str();
You can also do the same thing with Ch1:
std::stringstream temp;
temp << Ch1 << a;
char* Ch2 = new char[temp.str().length() + 1];
strcpy(Ch2, temp.str().c_str());
for char* you need to create another variable that is long enough for both, for instance. You can 'fix' the length of the output string to remove the chance of overrunning the end of the string. If you do that, be careful to make this large enough to hold the whole number, otherwise you might find that book+50 and book+502 both come out as book+50 (truncation).
Here's how to manually calculate the amount of memory required. This is most efficient but error-prone.
int a = 5;
char* ch1 = "Book";
int intVarSize = 11; // assumes 32-bit integer, in decimal, with possible leading -
int newStringLen = strlen(ch1) + intVarSize + 1; // 1 for the null terminator
char* ch2 = malloc(newStringLen);
if (ch2 == 0) { exit 1; }
snprintf(ch2, intVarSize, "%s%i", ch1, a);
ch2 now contains the combined text.
Alternatively, and slightly less tricky and also prettier (but less efficient) you can also do a 'trial run' of printf to get the required length:
int a = 5;
char* ch1 = "Book";
// do a trial run of snprintf with max length set to zero - this returns the number of bytes printed, but does not include the one byte null terminator (so add 1)
int newStringLen = 1 + snprintf(0, 0, "%s%i", ch1, a);
char* ch2 = malloc(newStringLen);
if (ch2 == 0) { exit 1; }
// do the actual printf with real parameters.
snprintf(ch2, newStringLen, "%s%i", ch1, a);
if your platform includes asprintf, then this is a lot easier, since asprintf automatically allocates the correct amount of memory for your new string.
int a = 5;
char* ch1 = "Book";
char* ch2;
asprintf(ch2, "%s%i", ch1, a);
ch2 now contains the combined text.
c++ is much less fiddly, but I'll leave that to others to describe.
You need to create another string large enough to hold the original string followed by the number (i.e. append the character corresponding to each digit of the number to this new string).
Try this out:
char *tmp = new char [ stelen(original) ];
itoa(integer,intString,10);
output = strcat(tmp,intString);
//use output string
delete [] tmp;

Resources