• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Functional Verification
  3. Using struct and Union

Stats

  • Locked Locked
  • Replies 1
  • Subscribers 64
  • Views 13219
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using struct and Union

hipooja
hipooja over 15 years ago

Hi,

I have used class to model different parts of VE ranging from the data structure to drivers ,generator , monitor ...Class is a collection

of different data types so is a struct and a union.The difference i know is that union occupies memory equal to its largest data element unlike a struct or a class and secondly a struct can contain a union ,even a class can have a nested class object.

 My questions is ,where do we use struct and union.Have you used them in modelling data or any other element of the VE and why?

How do i differentiate as to where should i be using a class/struct/union.

 

Regards,

Pooja Vaishnav 

 

  • Cancel
  • tpylant
    tpylant over 15 years ago
    A struct is an array of data objects. it can be packed or unpacked but only packed structs can be used in synthesizable code. The following is a packed struct because it consists of only literals.

    struct packed {

      logic [15:0] addr;

      logic [31:0] data;

      bit          parity;

    } frame;

    A union allows the specification of multiple representations for the same data element.

    union {

      int        i;

      reg [31:0] r;

    } data;

    The data object holds a single 32-bit value but can have it represented as a 2-state integer or a 32-bit 4-state reg depending on whether you access it use the “I” or “r” type.

    Now you can put these two constructs together as so:

    struct {

      logic [15:0] addr;

      union {

        int        i;

        reg [31:0] r;

      } data;

      bit          parity;

    } frame;


    Tim
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information