[go: up one dir, main page]

0% found this document useful (0 votes)
23 views10 pages

Structures and Union

The document explains user-defined data types in System Verilog, including typedefs for creating new types like uint8 and word. It covers structures and unions, detailing how to group non-homogeneous data types, initialize, and access them, as well as the differences between packed and unpacked structures. Additionally, it highlights the memory allocation differences between structures and unions, emphasizing that unions share memory among their members.

Uploaded by

abishek2003jothi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views10 pages

Structures and Union

The document explains user-defined data types in System Verilog, including typedefs for creating new types like uint8 and word. It covers structures and unions, detailing how to group non-homogeneous data types, initialize, and access them, as well as the differences between packed and unpacked structures. Additionally, it highlights the memory allocation differences between structures and unions, emphasizing that unions share memory among their members.

Uploaded by

abishek2003jothi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

User Defined

⚫ System Verilog allows user to define new data types


using typedef keyword.

typedef byte unsigned uint8; //Defining uint8


typedef bit [15:0] word; //Defining word

uint8 a, b;
word c, d;

a=8’d10;
c=16’d25;
Futurewiz
www.futurewiz.co.in
Structures

⚫ Structure and Unions are used to group non-


homogenous data types.
⚫ By default structure are unpacked.
⚫ Unpacked structure can contain any data type.

Declaration :
struct { bit [7:0] opcode; struct {bit [7:0] r, g, b;} pixel;
bit [15:0] addr; } IR;

struct {int a, b; real b;} mix;

Futurewiz
www.futurewiz.co.in
Structures

Initializing : Accessing :

IR=‘{opcode : 7’d8, addr : 15’d1}; int x;


pixel=‘{ 128, 255, 100}; bit [7:0] y;
pixel=‘{ r :128, g : 255, b :100}; pixel.r=200;
pixel=‘{ int :0}; mix.a=3;
mix=‘{ 3, 5, 5.6}; mix.c=4.5;
mix=‘{ int : 1, real : 1.0}; x=mix.b;
mix=‘{ default : 0}; y=pixel.g;

Futurewiz
www.futurewiz.co.in
Packed Structures

⚫ Packed Structure is made up of bit fields which are


packed together in memory without gaps.

⚫ A packed structure can be used as a whole to perform


arithmetic and logical operations.

⚫ First member of packed array occupies MSB and


subsequent members follow decreasing significance.

⚫ Structures can be packed by writing packed keyword


which can be followed by signed or unsigned keyword.

Futurewiz
www.futurewiz.co.in
Packed Structures

Example :
typedef struct packed signed { shortint a; //16-bits [31:16]
byte b; //8-bits [15:8]
bit [7:0] c; //8-bits [7:0]
} exam_st;
exam_st pack1;
bit [7:0] a, b, c;
pack1=‘{a: ’1, b: -10, c: 8’b1001_0101};
a=pack1.b;
b=pack1.c;
c=pack1[9:2];
Futurewiz
www.futurewiz.co.in
Packed Structures

⚫ Only packed data type and integer data types are allowed
inside packed structures

struct packed // default unsigned


{ bit [3:0] a;
bit [7:0] b;
bit [15:0] c [7:0] ; } pack2;

Compilation Error packed structure cannot have unpacked


element
Futurewiz
www.futurewiz.co.in
Packed vs Unpacked Structures

struct { bit [7:0] a; struct packed { bit [7:0] a;


bit [15:0] b; bit [15:0] b;
int c; int c;
} str1; } str2;

31:24 23:16 15:8 7:0


Unused a 55:48 47:32 31:0
Unused b a b c
c

Futurewiz
www.futurewiz.co.in
Unions

⚫ Union represents a single piece of storage element that


can be accessed by any of its member.

⚫ Only one data types in union can be used at a time.

Example :

union union packed


{ real a; { real a;
int b; int b;
bit [7:0] c; } exam1; bit [7:0] c; }
exam2;
Futurewiz
www.futurewiz.co.in
Unions

Example :
typedef union
{ shortint a;
00 00 00 00
int b;
bit [7:0] c; } my_un;
my_un un1; 00 00 F0 F0
un1.a=16’hf0f0;
$displayh(un1.b);
00 00 F0 AA
un1.c=8’b1010_1010;
$displayh(un1.b);
Futurewiz
www.futurewiz.co.in
Structures vs Unions

Structure Union

Memory is allocated to each Common memory is allocated


and every element. for all the members.

Size of structure is sum of size Size of union is equal to size of


of each member or more. largest member

First member is at offset 0. All member have 0 offset.

Modifying value of one member Modifying value of one member


has no effect on other members modifies value of all members

Futurewiz
www.futurewiz.co.in

You might also like