Sunday 29 March 2015

Difference between pass by ref and pass by value?

Pass by value is the default method through which arguments are passed into functions and tasks. Each subroutine retains a local copy of the argument. If the arguments are changed within the subroutine declaration, the changes do not affect the caller.

In pass by reference functions and tasks directly access the specified variables passed as arguments.Its like passing pointer of the variable.

example:
task pass(int i)    //  task pass(var int i) pass by reference 
{
delay(10);
i = 1;
printf(" i is changed to %d at %d\n",i,get_time(LO) );
delay(10);
i = 2;
printf(" i is changed to %d at %d\n",i,get_time(LO) );
}





In verilog,method arguments takes as pass by value.The inputs are copyed when the method is called and the outputs are assigned to outputs when exiting the method.In SystemVerilog ,methods can have pass by reference.Arguments passed by reference are not copied into the subroutine area, rather, a reference to the original argument is passed to the subroutine. The subroutine can then access the argument data via the reference.

In the following example, variable a is changed at time 10,20 and 30. The method pass_by_val , copies only the value of the variable a, so the changes in variable a which are occurred after the task pass_by_val call, are not visible to pass_by_val. Method pass_by_ref is directly referring to the variable a. So the changes in variable a are visible inside pass_by_ref.


EXAMPLE:

program main();
  int a;

  initial begin
    #10 a = 10;
    #10 a = 20;
    #10 a = 30;
    #10 $finish;
  end

  task pass_by_val(int i);

    forever
      @i $display("pass_by_val: I is %0d",i);
  endtask


  task pass_by_ref(ref int i);
    forever begin
      @i $display("pass_by_ref: I is %0d",i);

    end
  endtask

  initial begin
    pass_by_val(a);

  end

  initial begin

    pass_by_ref(a);
  end

endprogram

RESULT:

pass_by_ref: I is 10
pass_by_ref: I is 20
pass_by_ref: I is 30



By default, SystemVerilog passes arrays by value, copying the entire array.
It is recommended to pass arrays by reference whenever possible for performance reasons.

If you want your function to modify the array, use ref.
If you want your function to read the array, use const ref.
Example:


module automatic test;
 
  initial run();
 
  function void run();
    // Create some SystemVerilog arrays and populate them
    int array[5];
    int queue[$];
    int assoc[int];
    for (int i = 0; i < 5; i++) begin
      array[i] = i;
      queue[i] = i;
      assoc[i] = i;
    end  
   
    pass_by_value(array, queue, assoc);
    $display("After pass by value:");
    for (int i = 0; i < 5; i++) begin
      $display("array[%0d]:%0d", i, array[i]);
      $display("queue[%0d]:%0d", i, queue[i]);
      $display("assoc[%0d]:%0d", i, assoc[i]);
    end  

    pass_by_ref(array, queue, assoc);
    $display("After pass by reference:");
    for (int i = 0; i < 5; i++) begin
      $display("array[%0d]:%0d", i, array[i]);
      $display("queue[%0d]:%0d", i, queue[i]);
      $display("assoc[%0d]:%0d", i, assoc[i]);
    end  
   
  endfunction

   // These functions try to modify the arrays.

   // Default.
   // A copy of the arrays is made in this function

   function void pass_by_value(int array[5], int queue[$], int assoc[int]);
    array[2] = 100;
    queue[2] = 100;
    assoc[2] = 100;
  endfunction


   // Original arrays are being referenced
   function void pass_by_ref(ref int array[5], ref int queue[$], ref int assoc[int]);
    array[3] = 100;
    queue[3] = 100;
    assoc[3] = 100;   
  endfunction
  


   // Original arrays are being referenced
   // And they can be read but cannot be modified in this function 

   function void pass_by_const_ref(const ref int array[5],
                                  const ref int queue[$],
                                  const ref int assoc[int]);
    // Arrays cannot be modified in this function
    // array[3] = 100;
    // queue[3] = 100;
    // assoc[3] = 100;   
  endfunction
 
endmodule

Difference between Dynamic Array and Assosicate Array in SystemVerilog

With a regular array, you must specify its size when you declare it

bit my_array[10];
With a dynamic array you can allocate the size of the array during runtime (hence the term "dynamic").
bit my_dynamic_array[];  // Note size is not specified
...
my_dynamic_array = new[size];  // size is determined at run-time


Also, the array can be "re-sized" at a later point:
my_dynamic_array = new[new_size](my_dynamic_array);

In this case, new memory is allocated, and the old array values are copied into the new memory, giving the effect of resizing the array.

The main characteristic of an associative array is that the index type can be any type - you are not restricted to just integer values. For example, you can use a string as the index to look up an "associated" value.

bit my_assoc_array[string];  // Array stores bits, using a string as an index
...
my_assoc_array["testname"] = 1;  //Stores a 1 into the array element indexed by the value "testname"
$display(my_assoc_array["testname"]); // Displays 1

An associative array is also "dynamic", in the sense that it does not have a pre-determined size. However, you do not have to allocate the size - it grows as you add more elements into it.


Another answer is:

    Dynamic arrays are useful for dealing with contiguous collections of variables whose number changes dynamically.
    e.g.            int array[];
    When the size of the collection is unknown or the data space is sparse (scattered-throw in various random directions.), an associative array is a better option. In associative array, it uses the transaction names as the keys in associative array.
   e.g.            int array[string];

Different types of Arrays in SystemVerilog

Dynamic Array: 
  • We use dynamic array when we have no idea about the size of the array during compile time and we have to allocate its size for storage during run time.
  • We basically use this array when we have to store a contiguous or Sequential collection of data.
  • The array indexing should be always integer type.
  • To allocate size of a dynamic array, we have to use new[] operator.
            Example: 
               int my_array [];
               initial
                   begin
                         my_array = new[4];                              //Allocated 4 elements 
                    end 
  • To resize a dynamic array, we have to do as follows:
             Example:               
               initial 
                  begin 
                       my_array = new[18](my_array);     //Resize the Array and Copy 
                  end 

  • To know the size of the array, we have to use size() operator.
             Example: 
              my_array.size();                    //Returns the current size of the array, my_array as an integer. 

  • To delete an element from a dynamic array, we have to use delete() operator.
             Example: 
              initial 
                  begin 
                       my_array.delete();    //All the elements of array, my_array will be deleted. 
                   end


Associative Array: 
  • It is also allocated during run time.
  • This is the array, where data stored in random fashion.
  • It is used when we don’t have to allocate contiguous collection of data, or data in a proper sequence or index.
  • In associative array, the index itself associates the data. So it is called so.
  • Indexing is not regular, can be accessed using indexing like integer or string type or any scalar. 
Example: 
my_array ["name"];           // “name”, Index type is a string
my_array[address];           // address, Index type is an integer (here address is an integer).
my_array[my_class];       // my_class, Index type is a class.
my_array[s_array];          // s_array, Index type is an array.
  • It is better to use associative array, when size of the array is unknown & data space is random or irregular or sparse.
  • Following are the methods associated with Associative array. 
          num()  returns the number of entries in the Associative array 
Eg: my_array.num() 
first()  assigns the value of the first index in the Associative array to the given index variable Eg:my_array.first(i); 
last()  assigns the value of the last index in the Associative array to the given index variable Eg:my_array.last(i); 
next() — assigns the value of the next index in the Associative array to the given index variable Eg:my_array.next(i); 
prev()  assigns the value of the previous index in the Associative array to the given index variable Eg:my_array.prev(i); 
delete()  removes all the elements in the Associative array. 
Eg: my_array.delete(i); If the index is specified, then the element at the specified index “i”is deleted 
exists()  checks if element exists at the specified index “i” in the Associative arrayEg:my_array.exists(i);



 Queue: 
  • Queue is a variable size, ordered collection of Homogenous Data.
  • It is flexible, as it is variable in size and analogous to an 1-dimensional Unpacked array that can shrink & grow automatically and can be of size zero.
  • The main advantage of queue over dynamic array is that, we don’t need new[] operatorto allocate storage space for a queue.
  • The other advantages of queue over dynamic array is that we can manipulate the queue using various queue methods like: push, pop, delete, insert, size.
Example: 
module test_example ; 
int my_queue[$] = { 1, 2, 3 }; 
string s_queue [$] = {"first","second","third","fourth"}; 
string store;
initial 
begin
// Use of the size() method/operator 
$display("\n size() operator used"); 
for (int i = 0 ; i < my_queue.size(); i++ ) 
$display (my_queue[i]); 
$display("\n\n Elements of s_queue is :"); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," "); 
// Use of insert() method/operator 
s_queue.insert(1,"next"); // Previous element 1 is now turned to element 2. 
s_queue.insert(2,"somewhere"); 
$display("\n\n insert() operator used"); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," "); 
// Use of delete() method/operator 
s_queue.delete(1); // delete the element 1 
s_queue.delete(3); // delete the element 3 
$display("\n\n delete() operator used"); 
for (int i = 0; i < s_queue.size; i++) 
$write(string_queue[i]," "); 
// Use of pop_front() method/operator (it deletes the front of the queue) 
store = s_queue.pop_front(); 
$display("\n\n pop_front() operator used"); 
$display(" %s",store); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," "); 
// Use of pop_back() method/operator (it deletes the back of the queue) 
store= s_queue.pop_back(); 
$display("\n\n pop_back() operator used"); 
$display(" %s",store); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," "); 
// Use of push_front() and push_back() method/operator 
s_queue.push_front("in-front"); 
s_queue.push_back("in-back"); 
$display("\n\n push_front() and push_back() operator used"); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," \n "); 
end 
endmodule
 
Result: 
size() operator used 
1 
2 
3 
 
Elements of s_queue[$] is: 
first second third fourth 
 
insert() operator used 
first next somewhere second third fourth 
 
delete() operator used 
first somewhere second fourth 
 
pop_front() operator used 
first 
somewhere second fourth 
 
pop_back() operator used 
fourth 
somewhere second 
 
push_front() and push_back() operator used 
in-front 
somewhere 
second 
in-back

Saturday 28 March 2015

Find command in Linux (Examples)

The find command is used to locate files on a Unix or Linux system. Find will search any set of directories you specify for files that match the supplied search criteria. You can search for files by name, owner, group, type, permissions, date, and other criteria. The search is recursive in that it will search all sub-directories too. The syntax looks like this: 

find <where-to-look> <criteria> <what-to-do>


All arguments to find are optional, and there are defaults for all parts.  (This may depend on which version of find is used.  Here we discuss the freely available Gnu version of find
For example,
where-to-look  defaults to . (that is, the current working directory),
criteria defaults to none (that is, select all files),
what-to-do
(known as the find action) defaults to ‑print (that is, display the names of found files to standard output).

For example: 
1) find 
will display the path-names of all files in the current directory and all sub-directories.  The commands 
find . -print 
find -print 
find . 
do the exact same thing.

2) Here's an example find command using a search criterion and the default action:
find / -name foo
This will search the whole system for any files named foo and display their path-names.  Here we are using the criterion ‑name with the argument foo to tell find to perform a name search for the file-name foo.  The output might look like this:
/home/wpollock/foo
/home/ua02/foo
/tmp/foo
If find doesn't locate any matching files, it produces no output.
The above example said to search the whole system, by specifying the root directory (“/”) to search.  If you don't run this command as root, find will display a error message for each directory on which you don't have read permission.  This can be a lot of messages, and the matching files that are found may scroll right off your screen.  A good way to deal with this problem is to redirect the error messages so you don't have to see them at all:
find / -name foo 2>/dev/null
You can specify as many places to search as you wish:
find /tmp /var/tmp . $HOME -name foo

3) How to run the last executed find command?
!find 
It also displays the last find command executed along with the result on the terminal.

4) How to find for a file using name?
find -name "sum.sv" 
This will find all the files with name "sum.sv" in the current directory and sub-directories. The output might look like this:
./bkp/sum.sv
./sum.sv

5) How to find for files using name and ignoring case?
find -iname "sum.sv"
This will find all the files with name "sum.sv" while ignoring the case in the current directory and sub-directories. The output might look like this:
./SUM.sv
./bkp/sum.sv
./sum.sv

6) How to find for a file in the current directory only?
find -maxdepth 1 -name "test.sv"
This will find for the file "test.sv" in the current directory only. The output might look like this:
./test.sv

7) How to find for files containing a specific word in its name?
find -name "*sv*"
It displayed all the files which have the word "sv" in the filename. The output might look like this:
./SUM.sv
./bkp/sum.sv
./sum.sv
./multiply.sv

8) How to find for files in a specific directory?
find  /etc -name "*.sv"
This will look for the files in the /etc directory with ".sv" in the filename.

9) How to find the files whose name are not "test.sv"?
find -not -name "test.sv"
This is like inverting the match. It prints all the files except the given file "test.sv". The output might look like this: 
./TEST.sv
./bkp
./multiply.sv

10) How to limit the file searches to specific directories?
find -name "test.sv"
You can see here the find command displayed all the files with name "test.sv" in the current directory and sub-directories.
. /tmp/test.sv
./bkp/var/tmp/files/test.sv
./bkp/var/tmp/test.sv
./bkp/var/test.sv
./bkp/test.sv 
./test.sv

a) How to print the files in the current directory and one level down to the current directory?
find -maxdepth 2 -name "test.sv"
./tmp/test.sv
./bkp/test.sv
./test.sv

b) How to print the files in the current directory and two levels down to the current directory? 
find -maxdepth 3 -name "test.sv"
./tmp/test.sv
./bkp/var/test.sv
./bkp/test.sv
./test.sv

c) How to print the files in the subdirectories between level 1 and 4? 
find -mindepth 2 -maxdepth 5 -name "test.sv"
./tmp/test.sv
./bkp/var/tmp/files/test.sv
./bkp/var/tmp/test.sv
./bkp/var/test.sv
./bkp/test.sv

11) How to find the empty files in a directory? 
find . -maxdepth 1 -empty ./empty_file

12) How to find the largest file in the current directory and sub directories 
find . -type f -exec ls -s {} \; | sort -n -r | head -1
The find command "find . -type f -exec ls -s {} \;" will list all the files along with the size of the file. Then the sort command will sort the files based on the size. The head command will pick only the first line from the output of sort.

13) How to find the smallest file in the current directory and sub directories 
find . -type f -exec ls -s {} \; | sort -n -r | tail -1
Another method using find is 
find . -type f -exec ls -s {} \; | sort -n | head -1

14) How to find files based on the file type? 
a) Finding socket files 
find . -type s
b) Finding directories 
find . -type d
c) Finding hidden directories
find -type d -name ".*"
d) Finding regular files
find . -type f
e) Finding hidden files
find . -type f -name ".*" 


15) How to find files based on the size?
a) Finding files whose size is exactly 10M
find . -size 10M
b) Finding files larger than 10M size
find . -size +10M
c) Finding files smaller than 10M size
find . -size -10M

16) How to find the files which are modified after the modification of a give file.
find -newer "test.sv"
This will display all the files which are modified after the file "test.sv"

17) Display the files which are accessed after the modification of a give file.
find -anewer "test.sv"

18) Display the files which are changed after the modification of a give file.
find -cnewer "test.sv"

19) How to find the files based on the file permissions?
find . -perm 777
This will display the files which have read, write, and execute permissions. To know the permissions of files and directories use the command "ls -l".

20) Find the files which are modified within 30 minutes.
find . -mmin -30

21) Find the files which are modified within 1 day.
find . -mtime -1

22) How to find the files which are modified 30 minutes back
find . -not -mmin -30

23) How to find the files which are modified 1 day back.
find . -not -mtime -1

24) Print the files which are accessed within 1 hour.
find . -amin -60

25) Print the files which are accessed within 1 day.
find . -atime -1

26) Display the files which are changed within 2 hours.
find . -cmin -120

27) Display the files which are changed within 2 days.
find . -ctime -2

28) How to find the files which are created between two files.
find . -cnewer f1 -and ! -cnewer f2

So far we have just find the files and displayed on the terminal. Now we will see how to perform some operations on the files.

1) How to find all 777 permission directories and use chmod command to set permissions to 755.
find ./ -type d -perm 777 -print -exec chmod 755 {} \;
Another method using xargs is
find ./ -type d -perm 777 - print | xargs chmod 755

2) How to find a single file called tecmint.txt and remove it.
find . -type f -name "tecmint.txt" -exec rm {} \;
Another method using xargs is
find ./ -type d -perm 777 - print | xargs rm

3) Find the files which have the name ".sv" in it and then display only the files which have "class" word in them?
find -name "*.sv" -exec grep -H class {} \;
Another method using xargs is
find -name "*.sv" | xargs grep "class"

4) How to find  files which have the name ".svi" or ".svh" in it?
find ./ -type f  \( -name "*.svi" -o -name "*.svh" \)
Explanation from the POSIX spec:
! ( expression ) : Negation of a primary; the unary NOT operator.
( expression ): True if expression is true.
expression -o expression: Alternation of primaries; the OR operator. The second expression shall not be evaluated if the first expression is true.
Note that parenthesis, both opening and closing, are prefixed by a backslash (\) to prevent evaluation by the shell. 
Another method is
find /path/ -name '*.png' -or -name '*.jpg'
which will find the files with .png or .jpg extensions.

5) How to find  files whose name are not ".swp" or ".swo"?
find ./ -type f ! \( -name "*.swp" -o -name "*.swo" \)