Thursday, August 20, 2020

Union In System Verilog

 Hi Everyone !!!

In this blog we are going to see the concepts of Union in System Verilog. Structure and Union is look similar, they serve two different purposes.

Before Get into this Kindly Refresh the concepts about Structures.   https://vlsicsinfo.blogspot.com/search/label/structure

A union is a data type that represents a single piece of storage that can be accessed using one of the named member data types. Only one of the data types in the union can be used at a time. By default, a union is unpacked, meaning there is no required representation for how members of the union are stored. 
Syntax:
                union {
                            int y;
                            int unsigned x;
                            bit a;
                          } data;

Difference between Structure and Union?

The one major difference that distinguishes structure and union is that the structure has a separate memory location for each of its members whereas, the members of a union share the same memory location.


BASIS OF COMPARISON

STRUCTURE

UNION

Basic

The separate memory location is allotted to each member of the 'structure'.

All members of the 'union' share the same memory location.

Declaration

struct struct_name{
type element1;
type element2;
.
.
} variable1, variable2, ...;

union u_name{
type element1;
type element2;
.
.
} variable1, variable2, ...;

Keyword

'struct'

'union'

Size

Size of Structure= sum of size of all the data members.

Size of Union=size of the largest members.

Store Value

Stores distinct values for all the members.

Stores same value for all the members.

At a Time

A 'structure' stores multiple values, of the different members, of the 'structure'.

A 'union' stores a single value at a time for all members.

Way of Viewing

Provide single way to view each memory location.

Provide multiple way to to view same memory location.



Program Examples:
// Example for memory allocation for structure and union//
 module struct_union;
    struct {
     int a;
     bit b;
     int c;} data_s;    //Here structure size is sum of all elements (int(32)+bit(1)+int(32)=65)
     union {
     int a;
     bit b;
     int c;} data_u; //Here union size is largest of the data type. so size is 32 (By seeing output we will get a clear idea)
      initial begin
               $display(" Structure Memory =%0d Union Memory=%0d", $bits(data_s),$bits(data_u));
      end
endmodule

Note:
$bits---> System function it returns number of bits occupied by the mentioned variable/array/queue/structue/union.

OUTPUT:
Structure Memory =65 Union Memory=32
V C S S i m u l a t i o n R e p o r t

Program Examples:
// Example for string value at a time in structure and union//
 module struct_union;
    struct {
     int a;
     bit b;
     int c;} data_s;    
     union {
     int a;
     bit b;
     int c;} data_u; 
      initial begin
               data_s='{a:5,b:1,c:10}; //  structure allows to access all parameter at a time
               data_u='{a:5,b:1,c:10}; //  union allows to access a only one parameter at time. It leads to compilation error
       end
endmodule

OUTPUT

Error-[IAP] Illegal assignment pattern
testbench.sv, 14
struct_union, "'{a:5, b:1, c:10}"
Assignment pattern is illegal due to: Assignment Pattern used to assign into
unpacked union target. Make sure this context allows for expressions with no
self-determined type.

The above compilation can be removed by using below technique

module struct_union;
    struct {
     int a;
     bit b;
     int c;} data_s;    
     union {
     int a;
     bit b;
     int c;} data_u; 
      initial begin
               data_s='{a:5,b:1,c:10}; //  structure allows to access all parameter at a time
               data_u.a=10; //  union allows to access a only one parameter at time
               data_u.b=1;
               data_u.c=11; // At a time only one parameter of union can be accessed
               $display(" A=%0d B=%0d C=%0d ",data_u.a,data_u.b,data_u.c);
       end
endmodule

OUTPUT
A=11 B=1  C=11 

Note: last written will be stored in union. Since "b" is declared as bit its value shows as 1

TYPED and anonymous Union :
    A union can be defined as a type using typedef, in the same way as structures. A union that is defined as a user-defined type is referred to as a typed union. If typedef is not used, the union is referred to as an anonymous union

typedef union { int i; shortreal f; } num; // named union type
num n; // which is allowed only in typedef format 
n.f = 0.0; // set n in floating point format

typedef struct 
{ bit i;
 union { int i; shortreal f; } n; // anonymous union type 
} data;   //Named structure

UNPACKED UNION:
     By default union is unpacked.If no initial value is specified in the declaration of a variable of an unpacked union type, then the variable shall be initialized to the default initial value for variables of the type of the first member in declaration order of the union type.
      An unpacked union can contain any variable type, including real types, unpacked structures and unpacked arrays.  Un packed unions are not synthesizable.

TAGGED Union

    The qualifier tagged in a union declares it as a tagged union, which is a type-checked union. An ordinary (untagged) union can be updated using a value of one member type and read as a value of another member type, which is a potential type loophole. A tagged union stores both the member value and a tag, i.e., additional bits representing the current member name. The tag and value can only be updated together consistently using a statically type-checked tagged union expressio. The member value can only be read with a type that is consistent with the current tag value (i.e., member name). Thus, it is impossible to store a value of one type and (mis)interpret the bits as another type.

Example:
   module struct_union;
     union {
     int a;
     bit b;
     int c;} data_u; 
      initial begin
       data_u.a=5;  //Hete I am using only one variable of union but I am displaying all the variables in union. It won't produce any compilation  
          $display(" A=%0d B=%0d C=%0d", data_u.a,data_u.b,data_u.c);
       end
endmodule

OUTPUT:
A=5 B=1 C=5
V C S S i m u l a t i o n R e p o r t

The Same example can be done for tagged union
Updated union variable only allowed to read/use. If we trying to read non written value of any union variable other than written variable it leads to compilation error. By seeing below example we will get a clear idea. For this type of checking purpose tagged unions are used.
 module struct_union;
     union tagged{
     int a;
     bit b;
     int c;} data_u; 
      initial begin
       data_u= tagged a 5;  //Hete I am using only one variable of union but I am displaying all the variables in union. It  produce compilation  
          $display(" A=%0d B=%0d C=%0d", data_u.a,data_u.b,data_u.c);
       end
endmodule

OUTPUT:
Error-[TU-INVMEMUSG] Invalid member usage of a tagged union.
testbench.sv, 10
Member of a tagged union referred is not valid since a different member is
in use. The expected tag is 'a', but tag 'b' is used.
Please check which member of the tagged union is in use.

PACKED Union:

    Packed unions shall only contain members that are of integral data types. The members of a packed, untagged union shall all be the same size (in contrast to an unpacked union or a packed, tagged union, where the members can be different sizes). Thus, a union member that was written as another member can be read back. 
    A packed union differs from an unpacked union in that when a packed union appears as a primary, it shall be treated as a single vector.
    A packed union can also be used as a whole with arithmetic and logical operators, and its behavior is determined by its signedness, with unsigned being the default. One or more bits of a packed union can be selected as if it were a packed array with the range [n-1:0].
    Only packed data types and the integer data types shall be legal in packed unions.
    If a packed union contains a 2-state member and a 4-state member, the entire union is 4-state. There is an implicit conversion from 4-state to 2-state when reading and from 2-state to 4-state when writing the 2-state member.
 Syntax:
typedef union packed { // default unsigned
bit [423:0] a ;  // All size should be equal a=424, b=8*53=424 otherwise leads to compilation order
bit [52:0][7:0] b;} data; 

Packed union must have same size.
module struct_union;
    union  packed{ // Here packed union is used
     int a;
     bit b;  // Here different size of data types are used so it leads to compilation order
     int c;} data_u; 
      initial begin
               data_u.a=10;  
               data_u.b=1;
               data_u.c=11; 
endmodule

OUTPUT:
Error-[PUMSS] Incorrect packed union members size
testbench.sv, 7
"b"
Packed union members must have same size.
Member "b" has different size (1 bits) from next member (32 bits).


Hope this logic's will be useful to everyone !!!
Giving feedback is more precious than writing an article!!! 😊😊😊😊😊
Always welcome both positive and negative feedback's !!!
Feel free to post any queries related SV and UVM  

"Be yourself, who else is better qualified"
                                                                   - Frank J. Giblin




Wednesday, August 19, 2020

Handy Gvim/Vim/Vi commands

 Hi Everyone !!!

Gvim is the most common editor used by Linux Users. Here are some the gvim editor commands are given below,

Short Cut Command

Description

ZZ

Save and Quit

ZQ

Quit Without Save

CTRL+E/Y

Scrolling line up and down

CTRL+U/D

Move the half of the screen up and down

CTRL+F/B

Page up and down

s

Deletes the current character cursor will be in insert mode

S

Deletes the entire line and cursor in insert mode

H

Move the top of the screen

L

End of the screen

G

Jumps to the file or line typed before(this is the difference between L and G)

-/+

Moves the line up and down

dnw

It delete upto n words

h

Moves the cursor left direction(we can use numbers also i.e 8h)

K

Moves a cursor to upward direction(i.e 9k)

J

Moves the cursor downward direction. We can use 9j also

L

Moves the cursor right direction(we can use numbers also i.e 8l)

zt

Scroll the cursor to top

zb

Scroll the cursor to bottom

zc

Scroll the cursor at center

(

Jumps the beginning of the sentence

)

Jumps the end of the sentence

w

jump by start of words (punctuation considered words)

W

jump by words (spaces separate words)

e

jump to end of words (punctuation considered words)

E

jump to end of words (no punctuation)

0

(zero) start of line

$

Go to  the end of the line(we can use number also 9$ means cursor will go the end of 9th line)

i

start insert mode at cursor

I

insert at the beginning of the line

a

append after the cursor

A

append at the end of the line

o

open (append) blank line below current line (no need to press return)

O

open blank line above current line

Esc

exit insert mode

C

delete from cursor position to end of the line

:E

edit a file in a new buffer

:sp

open a file in a new buffer and split window

Ctrl+ws

split windows

Ctrl+ww

switch cursor between windows

Ctrl+W+

Increase the window size in split screen

Ctrl+wq

quit a window

Ctrl+w gf

Edit existing file under cursor in new tabpage

Ctrl+w f

Edit existing file under cursor in split window

gf

Edit existing file under cursor in same window

Ctrl+wv

split windows vertically

:%s/old/new/g

replace all old with new throughout file

:%s/old/new/gc

replace all old with new throughout file with confirmations

n

repeat search in same direction

N

repeat search in opposite direction

/pattern

search for pattern

:w

write (save) the file, but don’t exit

:wq

write (save) and quit

:wqall

Write and quit all

:wall

Save the changes in all file

:w!

force write to a read only file

:q!

force quit and throw away changes

yy or(Y)

yank (copy) a line

2yy

yank 2 lines

dd or D

delete (cut) a line

p

put (paste) the clipboard after cursor

P

put (paste) before cursor

dw

delete (cut) the current word

yw

yank word

~

switch case

Ctrl+v

start visual block mode

U

undo

.

repeat last command

r

replace a single character (does not use insert mode)

R

replace characters till Esc is pressed

J

join line below to the current one

:tabnew

open new tab

:tabnew %

open same file in new tab


Searching options:
 c.m- means it search the start letter as c and second letter may be anything but third letter must be m
human human human - if you type fh- cursor will move every h position( remember cursor will be in h position) Fh will search in reverse. th-one character before. same as th but in reverse direction.

Remember that vim stores the recordings in macros.Following are the steps for recording in gvim:
  • Start recording by pressing q, followed by a lower case character (a-z) to name the macro.
  • Perform any editing, actions inside Vim editor, which will be recorded.
  • Stop recording by pressing q.
  • Play the recorded macro by pressing @ followed by the macro name.
  • To repeat macros multiple times, press: N@macro name. N is a number.

Ex. 1@a (or @a) will play recording 1 time stored in buffer ‘a’, 2@a will play it 2 times and so on.

You can store 26 different recordings at a time!


Hope this logic's will be useful to everyone !!!
Giving feedback is more precious than writing an article!!! 😊😊😊😊😊
Always welcome both positive and negative feedback's !!!
Feel free to post any queries related SV and UVM  

"To Know that we know what we know, and to know that we do not know what we don not know, that is true knowledge"
                                 -Nicolaus Copernicus



`define Macro usage in System Verilog

 Hi Everyone !!! In this blog we are going to see the usage of `define macro in System Verilog. A text macro substitution facility has been ...