Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing packed structs in System Verilog

I have a packed struct defined as shown below

typedef struct packed {
    logic bit1;
    logic [7:0] byte1;
} MyPackedStruct;

MyPackedStruct myPackedStruct;

Is there any SV built in function that I could use to print the structures similar to above but having many more fields, without having to write my own method to print each of the fields using a

$display(...,myPackedStruct.field_name)?

like image 903
Vissu Avatar asked Jul 02 '14 09:07

Vissu


People also ask

How do I print a structure in SystemVerilog?

You can use the %p formatting element.

What is a packed struct in SystemVerilog?

Packed Structures A packed structure is a mechanism for subdividing a vector into fields that can be accessed as members and are packed together in memory without gaps. The first member in the structure is the most significant and subsequent members follow in decreasing order of significance.

What is struct and union in SystemVerilog?

A structure is a hierarchical collection of data of possibly different datatypes, while a union is set of different datatypes for accessing the same data. In SystemVerilog, an unpacked structure gives you the convenience of working with a collection of data as an aggregate.

Does Verilog have structs?

The SystemVerilog struct groups the data types of multiple types. The entire group can be referenced as a whole, or the individual data type can be referenced by name.


2 Answers

You can use the %p formatting element.

$display("%p", myPackedStruct);

Output from Modelsim:

 # '{bit1:x, byte1:x}

See section 21.2.1.7 Assignment pattern format in the IEEE 1800-2012 SystemVerilog language spec.

like image 183
dwikle Avatar answered Oct 17 '22 06:10

dwikle


You can use %p - pretty print:

$displayb("%p",myPackedStruct);

'{bit1:x, byte1:xxxxxxxx}

which will print it as an assignment pattern, but you will not be able choose the fields or their ordering, or apply any other individual formating. %p is good for quick and easy displays, but most people eventually wind up writing a method to format it exactly the way they want it.

like image 7
dave_59 Avatar answered Oct 17 '22 06:10

dave_59