Showing posts with label unpacked array. Show all posts
Showing posts with label unpacked array. Show all posts

Saturday, June 27, 2020

System Verilog Concepts

1. What is the difference between Module based and Class based Tb.


An instance of a module is created at compilation time. It is a static object. Its lifetime is valid throughout the simulation. Whereas instance of an object is created during simulation time. Its an dynamic object. Its lifetime is not valid throughout the simulation.

2. What is a 4-state and a 2-state data type? Which datatypes are called 4-state and 2-state Datatypes?

If a variable is capable of holding '1' '0' 'X' 'Z'  values, then it is called 4-state datatype.
Ex: logic, integer, time.

If a variable is capable of holding '1'  and '0'   values, then it is called a 2-state datatype.
Ex: bit,byte, int, longint, shortint.

3. What is Packed array and Unpacked array?
Packed array is used to represent an array which has its dimensions defined before the variable name. Packed array can be declared with limited datatypes such as bit, logic, reg and other net types.

Ex: bit [31:0] packed_addr;

If this address has to be divided into 4 sub groups with 8 bits in each group then it can be declared as shown below. 

bit[3:0][7:0] packed_addr;

The index next to data type is called packed dimension. All groups will be packed together into a single array memory of 32 bits. packed_array[2] will represent 3rd set of 8 bits in packed_addr array as shown below.

Unpacked array is used to represent an array, which has its dimensions defined after the variable name. Unpacked array can be declared with any data type such as string, user defined data types such as class handles etc.,

Ex: bit [1:0][3:0] address[4];

These arrays follow addressing technique used by all regular arrays, as shown in the below figure.

1

0

Unpacked dimension

3

2

1

0

3

2

1

0

address[0]

3

2

1

0

3

2

1

0

address[1]

3

2

1

0

3

2

1

0

address[2]

3

2

1

0

3

2

1

0

address[3]

 
To represent 1st set in 3rd array, array can be declared as address[2][0]. Indexing always starts from unpacked followed by packed from left to right. Here index [2] represents address[2] and index [0] is first 4 bits set in address[2] which is a packed dimension.
Advantage of Packed array is, it saves memory as multiple bytes/multiple nibbles are packed onto single segment.
Disadvantage of Packed array is, its memory is allocated at compilation time, therefore memory cannot be allocated dynamically. This drawback overcomes its advantage of memory saving

4. What is a static Array and a Dynamic Array?
Static array is an array, whose size is known during compilation time 
ex: packed and unpacked arrays
Dynamic array is an array, whose size is not known during compilation time. It is defined during run time.
 ex: Dynamic array, Queue, Associative array.

5. What is Dynamic Array, Queue and Associative array?
Dynamic Array: A dynamic array is a one dimensional unpacked array, whose memory is allocated during run time. It is declared by appending '[ ]' at the end of array as shown below.
data_type dyn_array[];
here data_type is datatype of the array elements. Ex:- bit dyn_array[] or int dyn_array.
The memory is allocated with the help of new[] method. The value given inside new method decides the space allocated for array. 
int dyn_array[]
dyn_array= new[10]; // An array with 10 elements of int type is declared.
To find the size of dynamic array size() method is used. 
Ex: dyn_array.size();
To delete the entire array delete() method is used. 
Ex: dyn_array.delete();

Queue: Queue is a Variable-sized, ordered collection of identical elements. The elements can be inserted or removed from any position of the queue. Each element is indexed based on its position. First element has a index of '0' and last element has a index of '$'. Queue is represented by appending'[$]' at the end of variable as shown below. 
data_type queue[$];
Ex: int queue[$]; //queue of  type int
       string queue[$] ; // Queue of  type String
       bit queue[$:63]; // Queue of type bit with 64 entries(bounded)
A queue is comparable to one dimensional unpacked array, which grows and shrinks automatically.
Queue can be bounded or unbounded. An empty queue is a valid queue unlike arrays.
Queue Methods:
The size() method returns the number of items in the queue. If the queue is empty, it returns 0.
Ex: queue.size(); //returns size of queue
The insert() method inserts the given item at the specified index position.
Ex: queue.insert(index,item) // item will be inserted to that particular index.
The delete() method deletes the item from the specified index position.
Ex: queue.delete(index) // deletes the particular element present in  the index
The push_front() method inserts the given element at the front of the queue.
Ex: queue. push_front(item)// pushes the item in the front.
The push_back() method inserts the given element at the end of the queue.
Ex: queue.push_back(item) // pushes item at the end of queue
The pop_front() method removes and returns the first element of the queue.
Ex: item = queue.pop_front()// First element of the queue is removed and returned to item
The pop_back() method removes and returns the last element of the queue.
Ex: item = queue.pop_back() // last element is removed and returned to item.
Note: item's data type will be same as that of queue.
Associative Array: An associative array is non contiguous in nature. The memory is allocated only when it is in use. It is generally used when size is unknown or data space is sparse. The index can be of any datatype, it is not restricted to int as in dynamic array. 
data_type array[index_type]
here index_type can be of any datatype.
Ex: int array[*] // array with int elements with unspecified length
       bit[7:0]array[string] // array elements of 8-bits with string index.
The num() method returns the number of entries in the associative array. If the array is empty, it returns 0.  Ex: length = array.num()
The delete() method removes the element present at the specified index. If the index is not present it will delete entire array.
Ex: array.delete(index); // deletes the item present in that index.
The exists() function checks if an element exists at the specified index within the given array. It returns 1 if the element exists, otherwise it returns 0.
Ex: if(array.exists(index))
          $display("element exists");
      else
          $display("element does not exist");
The first() method assigns the index of the first element to the argument present inside the method. it returns 0 if there is no first element.
Ex: if(array.first(first_index))
          $display("First entry is :%0d",array[first_index]);
      else
          $display("element does not exist");
similarly we have last(),next() and prev() methods which gives last element index, next element index and previous element index respectively.

6. What is Static casting and Dynamic casting?
Static cast:A data type of a variable can be changed by using a cast " ' " operator. In a static cast, the expression to be cast shall be enclosed in parentheses, and it is prefixed with the casting data type, followed by an apostrophe ( ' ).

    Ex: 1. int ' (3.0*7.1) ; // output here is 21. the fraction value  casts to int type.

Static casting is checked during compilation time, so there will be no simulation errors if casting fails. static casting is not related to OOP. It can be used with value, variable or expression.

Dynamic Cast : Dynamic casting is performed using $cast method. It can behave like both task and function. The casting is performed during run time. it will check whether the given data type casts to its predefined datatype first and then proceeds with the casting process.

Ex: typedef enum {apple, orange, banana, mango, guava }fruits;

      fruits f_h;

      $cast(f_h, 4)   // here guava is assigned to f_h as position of guava is 4.

      if(! $cast(_h,7))         // casting fails, as there is no element at position 7  

          $display("error"); 

       f_h'(7) //  here f_h = banana 

Beginner's Verilog Code

******** Simple Logic circuits using Data flow (assign logic) ******** 1. RTl code: 2:1 Mux module mux_2x1 ( a , b , sel , y );    input  ...