Saturday, July 17, 2021

How to open the .gz file in gvim editor ?

 

    Right now, we can open session.log.gz file in gvim from the command line and it will unzip automatically & will show correct contents of the session.log  whereas if you try to open the same file from gvim editor itself, it won’t unzip.

 

If you add the following code to your .vimrc, it will fix that problem.

 

 

:augroup gzip

  :  autocmd!

  :  autocmd BufReadPre,FileReadPre      *.gz set bin

  :  autocmd BufReadPost,FileReadPost  *.gz '[,']!gunzip

  :  autocmd BufReadPost,FileReadPost  *.gz set nobin

  :  autocmd BufReadPost,FileReadPost  *.gz execute ":doautocmd BufReadPost " . expand("%:r")

  :  autocmd BufWritePost,FileWritePost *.gz !mv <afile> <afile>:r

  :  autocmd BufWritePost,FileWritePost *.gz !gzip <afile>:r

 

  :  autocmd FileAppendPre                         *.gz !gunzip <afile>

  :  autocmd FileAppendPre                         *.gz !mv <afile>:r <afile>

  :  autocmd FileAppendPost                       *.gz !mv <afile> <afile>:r

  :  autocmd FileAppendPost                       *.gz !gzip <afile>:r

  :augroup END

Wednesday, May 13, 2020

Printing any UVM register in nice table format during write & read operation


In higher verbosity, we can use a uvm_reg_callback to print the register info in a nice format(shown below) during write/read.  This will make our debug easier from the logs.  
It's a generic callback and you can plug-in to any env. 
This will print the information for any uvm_reg write/read,









We need to do 2 things.
1. Create or Copy-paste the following reg_callback class in a file. In the following code, I've written the code to print the reg info before reg write(pre_write) and after reg read(post_read). You can edit it the way you want.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class global_reg_callback extends uvm_reg_cbs;
  `uvm_object_utils( global_reg_callback )

  uvm_reg u_reg;

  function new( string name = "global_reg_callback" );
    super.new( name );
  endfunction: new

  virtual task pre_write( uvm_reg_item rw );
    bit                        sour;
    $cast(u_reg, rw.element);
    `uvm_info( "pre_write", $sformatf( "%s ", u_reg.sprint()), UVM_HIGH )

  endtask: pre_write

  virtual task post_read( uvm_reg_item rw );
    bit                        sour;
    $cast(u_reg, rw.element);
    `uvm_info( "post_read", $sformatf( "%s ", u_reg.sprint()), UVM_HIGH )

  endtask: post_read

endclass: global_reg_callback
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

2. Add the following code to your env class.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
global_reg_callback              global_reg_cbs;

function void env::build_phase(uvm_phase phase);
   super.build_phase(phase);
   global_reg_cbs = global_reg_callback::type_id::create("global_reg_cbs", this);
endfunction

function void env::start_of_simulation_phase(uvm_phase phase);
   super.start_of_simulation_phase(phase);
   //Add the callback to print the reg info in the table format during write/read
   uvm_callbacks#(uvm_reg, uvm_reg_cbs)::add(null, global_reg_cbs);

endfunction : start_of_simulation_phase
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Happy debugging !!!

Wednesday, April 4, 2018

Flipping only 4 bits between adjacent elements in an array


Countones method simplify some constraints.. Here is another example.. The program is to constrain such a way that adjacent elements in an array should change only by 4 bits.



class b;
rand bit [7:0] b_v[32];
constraint b_c { 
   foreach(b_v[i]) {
      if(i!=0) {
         $countones(b_v[i] ^ b_v[i-1]) == 4; 
      }
   }
  }
function new();
endfunction

function void post_randomize();
   foreach(b_v[i]) begin
      $display("post_randomize b_v[%0d]=%b",i, b_v[i]);
   end
endfunction
endclass : b


module flip_only_4bits;
reg a;
b B = new();

initial
begin
 
  $display("a=%0d",a);
  for(int i=0;i<1;i++)
  begin
    B.randomize();
  end
end
endmodule




Output:-
post_randomize b_v[0]=00011000
post_randomize b_v[1]=01110100
post_randomize b_v[2]=01101111
post_randomize b_v[3]=00111001
post_randomize b_v[4]=01010000
post_randomize b_v[5]=11110110
post_randomize b_v[6]=00110101
post_randomize b_v[7]=10010110
post_randomize b_v[8]=10101100
post_randomize b_v[9]=10110001
post_randomize b_v[10]=10101111
post_randomize b_v[11]=01001110
post_randomize b_v[12]=11000000
post_randomize b_v[13]=01011001
post_randomize b_v[14]=01101111
post_randomize b_v[15]=00011101
post_randomize b_v[16]=01110001
post_randomize b_v[17]=00100111
post_randomize b_v[18]=01000001
post_randomize b_v[19]=11001010
post_randomize b_v[20]=00101011
post_randomize b_v[21]=10011001
post_randomize b_v[22]=11011110
post_randomize b_v[23]=10100110
post_randomize b_v[24]=01100011
post_randomize b_v[25]=10100101
post_randomize b_v[26]=01111101
post_randomize b_v[27]=00100001
post_randomize b_v[28]=01011001
post_randomize b_v[29]=00000011
post_randomize b_v[30]=01001110
post_randomize b_v[31]=10111110

Tuesday, January 23, 2018

Linux find cmd examples

25+ examples of Linux find command – search files from command line

Linux find command

The Linux find command is a very useful and handy command to search for files from the command line. It can be used to search for files based on various criterias like permissions, user ownership, modification date/time, size etc. In this post we shall learn to use the find command along with various options that it supports.
The examples are broken down into discrete examples making it easy to learn and comprehend. The find command is available on most linux distros by default so you do not have to install any package. This is a command you must master, if you want to get comfortable with your linux system.
So lets begin with the command.
The basic format of the syntax is like this
find where-to-look criteria what-to-look-for

Basic examples

1. List all files in current and sub directories

This command lists out all the files in the current directory as well as the subdirectories in the current directory.
$ find
.
./abc.txt
./subdir
./subdir/how.php
./cool.php
The command is same as the following
$ find .
$ find . -print

2. Search specific directory or path

The following command will look for files in the test directory in the current directory. Lists out all files by default.
$ find ./test
./test
./test/abc.txt
./test/subdir
./test/subdir/how.php
./test/cool.php
The following command searches for files by their name.
$ find ./test -name abc.txt
./test/abc.txt
We can also use wildcards
$ find ./test -name *.php
./test/subdir/how.php
./test/cool.php
Note that all sub directories are searched recursively. So this is a very powerful way to find all files of a given extension.
Trying to search the "/" directory which is the root, would search the entire file system including mounted devices and network storage devices. So be careful. Of course you can press Ctrl + c anytime to stop the command.
Ignore the case
It is often useful to ignore the case when searching for file names. To ignore the case, just use the "iname" option instead of the "name" option.
$ find ./test -iname *.Php
./test/subdir/how.php
./test/cool.php

3. Limit depth of directory traversal

The find command by default travels down the entire directory tree recursively, which is time and resource consuming. However the depth of directory travesal can be specified. For example we don't want to go more than 2 or 3 levels down in the sub directories. This is done using the maxdepth option.
$ find ./test -maxdepth 2 -name *.php
./test/subdir/how.php
./test/cool.php

$ find ./test -maxdepth 1 -name *.php
./test/cool.php
The second example uses maxdepth of 1, which means it will not go lower than 1 level deep, either only in the current directory.
This is very useful when we want to do a limited search only in the current directory or max 1 level deep sub directories and not the entire directory tree which would take more time.
Just like maxdepth there is an option called mindepth which does what the name suggests, that is, it will go atleast N level deep before searching for the files.

4. Invert match

It is also possible to search for files that do no match a given name or pattern. This is helpful when we know which files to exclude from the search.
$ find ./test -not -name *.php
./test
./test/abc.txt
./test/subdir
So in the above example we found all files that do not have the extension of php, either non-php files. The find command also supports the exclamation mark inplace of not.
find ./test ! -name *.php

5. Combine multiple search criterias

It is possible to use multiple criterias when specifying name and inverting. For example
$ find ./test -name 'abc*' ! -name '*.php'
./test/abc.txt
./test/abc
The above find command looks for files that begin with abc in their names and do not have a php extension. This is an example of how powerful search expressions can be build with the find command.
OR operator
When using multiple name criterias, the find command would combine them with AND operator, which means that only those files which satisfy all criterias will be matched. However if we need to perform an OR based matching then the find command has the "o" switch.
$ find -name '*.php' -o -name '*.txt'
./abc.txt
./subdir/how.php
./abc.php
./cool.php
The above command search for files ending in either the php extension or the txt extension.

6. Search only files or only directories

Sometimes we want to find only files or only directories with a given name. Find can do this easily as well.
$ find ./test -name abc*
./test/abc.txt
./test/abc

Only files

$ find ./test -type f -name abc*
./test/abc.txt

Only directories

$ find ./test -type d -name abc*
./test/abc
Quite useful and handy!

7. Search multiple directories together

So lets say you want to search inside 2 separate directories. Again, the command is very simple
$ find ./test ./dir2 -type f -name abc*
./test/abc.txt
./dir2/abcdefg.txt
Check, that it listed files from 2 separate directories.

8. Find hidden files

Hidden files on linux begin with a period. So its easy to mention that in the name criteria and list all hidden files.
$ find ~ -type f -name ".*"

Search based on permissions

9. Find files with certain permissions

The find command can be used to find files with a specific permission using the "perm" option. The following command searches for files with the permission 0664
$ find . -type f -perm 0664
./abc.txt
./subdir/how.php
./abc.php
./cool.php
This can be useful to find files with wrong permissions which can lead to security issues. Inversion can also be applied to permission checking.
$ find . -type f ! -perm 0777
./abc.txt
./subdir/how.php
./abc.php
./cool.php

10. Find files with sgid/suid bits set

The "perm" option of find command accepts the same mode string like chmod. The following command finds all files with permission 644 and sgid bit set.
# find / -perm 2644
Similarly use 1664 for sticky bit. The perm option also supports using an alternative syntax instead of octal numbers.
$ find / -maxdepth 2 -perm /u=s 2>/dev/null
/bin/mount
/bin/su
/bin/ping6
/bin/fusermount
/bin/ping
/bin/umount
/sbin/mount.ecryptfs_private
Note that the "2>/dev/null" removes those entries that have an error of "Permission Denied"

11. Find readonly files

Find all Read Only files.
$ find /etc -maxdepth 1 -perm /u=r
/etc
/etc/thunderbird
/etc/brltty
/etc/dkms
/etc/phpmyadmin
... output truncated ...

12. Find executable files

The following command will find executable files
$ find /bin -maxdepth 2 -perm /a=x
/bin
/bin/preseed_command
/bin/mount
/bin/zfgrep
/bin/tempfile
... output truncated ...

Search Files Based On Owners and Groups

13. Find files belonging to particular user

To find all or single file called tecmint.txt under /root directory of owner root.
$ find . -user bob
.
./abc.txt
./abc
./subdir
./subdir/how.php
./abc.php
We could also specify the name of the file or any name related criteria along with user criteria
$ find . -user bob -name '*.php'
Its very easy to see, how we can build up criteria after criteria to narrow down our search for matching files.

14. Search files belonging to group

Find all files that belong to a particular group.
# find /var/www -group developer
Did you know you could search your home directory by using the ~ symbol ?
$ find ~ -name hidden.php
Easy!!

Search file and directories based on modification date and time

Another great search criteria that the find command supports is modification and accessed date/times. This is very handy when we want to find out which files were modified as a certain time or date range. Lets take a few examples

15. Find files modified N days back

To find all the files which are modified 50 days back.
# find / -mtime 50

16. Find files accessed in last N days

Find all files that were accessed in the last 50 days.
# find / -atime 50

17. Find files modified in a range of days

Find all files that were modified between 50 to 100 days ago.
# find / -mtime +50 –mtime -100

18. Find files changed in last N minutes.

Find files modified within the last 1 hour.
$ find /home/bob -cmin -60

19. Files modified in last hour

To find all the files which are modified in last 1 hour.
# find / -mmin -60

20. Find Accessed Files in Last 1 Hour

To find all the files which are accessed in last 1 hour.
# find / -amin -60

Search files and directories based on size

21. Find files of given size

To find all 50MB files, use.
# find / -size 50M

22. Find files in a size range

To find all the files which are greater than 50MB and less than 100MB.
$ find / -size +50M -size -100M

23. Find largest and smallest files

The find command when used in combination with the ls and sort command can be used to list out the largest files.
The following command will display the 5 largest file in the current directory and its subdirectory. This may take a while to execute depending on the total number of files the command has to process.
$ find . -type f -exec ls -s {} \; | sort -n -r | head -5
Similary when sorted in ascending order, it would show the smallest files first
$ find . -type f -exec ls -s {} \; | sort -n | head -5

24. Find empty files and directories

The following command uses the "empty" option of the find command, which finds all files that are empty.
# find /tmp -type f -empty
To file all empty directories use the type "d".
$ find ~/ -type d -empty
Really very simple and easy

Some advanced operations

The find command not only finds files based on a certain criteria, it can also act upon those files using any linux command. For example, we might want to delete some files.
Here are some quick examples

25. List out the found files

Lets say we found files using find command, and now want to list them out as the ls command would have done. This is very easy.
$ find . -exec ls -ld {} \;
drwxrwxr-x 4 enlightened enlightened 4096 Aug 11 19:01 .
-rw-rw-r-- 1 enlightened enlightened 0 Aug 11 16:25 ./abc.txt
drwxrwxr-x 2 enlightened enlightened 4096 Aug 11 16:48 ./abc
drwxrwxr-x 2 enlightened enlightened 4096 Aug 11 16:26 ./subdir
-rw-rw-r-- 1 enlightened enlightened 0 Aug 11 16:26 ./subdir/how.php
-rw-rw-r-- 1 enlightened enlightened 29 Aug 11 19:13 ./abc.php
-rw-rw-r-- 1 enlightened enlightened 0 Aug 11 16:25 ./cool.php

26. Delete all matching files or directories

The following command will remove all text files in the tmp directory.
$ find /tmp -type f -name "*.txt" -exec rm -f {} \;
The same operating can be carried out with directories, just put type d, instead of type f.
Lets take another example where we want to delete files larger than 100MB
$ find /home/bob/dir -type f -name *.log -size +10M -exec rm -f {} \;

Tuesday, December 5, 2017

regular expression matching in System Verilog/UVM

Some simulators already support a set of SystemVerilog string method extensions that handle regular expressions such as str.match() and str.search().
For example,
string str = "Thirumoorthy";
result = str.match(“Thiru.*”); // returns true ( i.e if the pattern is matched with the str).

If you are using the UVM, there is a DPI routine that does the same thing
result = uvm_pkg::uvm_re_match(“pattern”,str); //returns 0 if matches

With uvm_re_match, sometimes regular expressions might have problem if you are not careful with the expression.
For example,
result = uvm_pkg::uvm_re_match(“Thiru.*”,str); //returns 1 which is a mismatch
here . is matched as dot instead of wildcard

whereas
result = uvm_pkg::uvm_re_match(“Thiru*”,str); //returns 0 which is a match


But SV regular expression works consistently. 

Monday, June 5, 2017

Difference between copy and clone method in UVM

I've been confused for a while about how copy and clone method works in UVM.
The confusion is about whether clone = create+deep copy?
In my code, when I used clone method, it was not deep copy by default. I've to explicitly write do_copy method.

Today I spent some time to explore about it and found the answer.

Please note that if you implement do_copy in your derived object, clone will have deep copy else it only copies the fields mentioned in `uvm_object_utils*.

Here is the mimicking behavior of copy and clone method of UVM
typedef base_class;
typedef derived_class;
function base_class create();
  derived_class tmp;
  tmp = new();
  return tmp;
endfunction
class base_class;
  int A = 7;
  virtual function do_copy(base_class rhs);
  endfunction
  virtual function copy(base_class rhs);
    do_copy(rhs);
  endfunction
  virtual function base_class clone();
    base_class base;
    base = create();
    base.copy(this);
    return (base);
  endfunction
endclass
class derived_class extends base_class;
  int A = 5;
  function do_copy(base_class rhs);
    derived_class derived;
    $cast(derived,rhs);
    super.do_copy(rhs);
    A = derived.A;
  endfunction
endclass

module test();
  derived_class d1,d2,d3;
  initial
  begin
    d1 = new();
    d1.A = 10;
    d3 = new();
    d3.copy(d1);
    $cast(d2,d1.clone());
    $display("A in d1 is %0d",d1.A);
    $display("A in d2 is %0d",d2.A);
    $display("A in d3 is %0d",d3.A);
    d1.A = 20;
    $display("A in d1 is %0d",d1.A);
    $display("A in d2 is %0d",d2.A);
    $display("A in d3 is %0d",d3.A);
  end 
endmodule
Output : 
A in d1 is 10 A in d2 is 10 A in d3 is 10 A in d1 is 20 A in d2 is 10A in d3 is 10
There are two function used here copy and clone.
copy : d3.copy(d1); which is simply deep copy of d1 to d3. It works as below.
1. d3 calls copy function which is defined in base_class.
2. copy function calls do_copy, as do_copy is virtual method, always latest definition of function will be executed. There is only one implementation of a virtual method per class hierarchy, and it is always the one in the latest derived class. So do_copy from "derived class" will be called.

clone : $cast(d2,d1.clone()); This method works as below.
1. d1.clone() calls definition of clone from base class.
2. clone calls create method which, returns handle of type derived_class.
3. A child class handle can be directly assign to base class.
4. base.copy(this); This is basically deep copy of this(here d1) to base.
5. So clone method return class variable of type "base_class" which we need to cast in derived_class, then only we can access member of class.

If you have noticed, here there is one dilemma. 
base.copy(this); calls copy function from base class , which call do_copy of derived class. So in turn do_copy is called from handle of object of type base_class. So how in do_copy it can access variable A, since it is not defined in base_class.
Reason is that "A" is being used by do_copy function which is in derived class. So context of A will be derived_class. 

Friday, October 14, 2016

vim format for system verilog and matching keywords

For System Verilog color highlighting, 

augroup filetype
  " Verilog HDL
  au BufNewFile,BufRead *.bv,*.sv     set ft=verilog_systemverilog
  au BufNewFile,BufRead *.vr,*.vrh,*vri  set ft=vera
augroup END

If you want to match begin-end, case-endcase, task-endtask etc in a nested code in vim, 
here is the code required in .vimrc and enjoy vimming.
You have to download matchit.vim from online.

source /home/username/matchit.vim

:let b:match_ignorecase = 1

:let b:match_words = '\<begin\>:\<end\>'