Translate page

Thursday, February 3, 2022

Verilog HDL - Lexical Conventions


 

HDLs provide ways to represent the digital circuits in the textual form. Verilog HDL is a HARDWARE DESCRIPTION LANGUAGE to model the digital circuits, this source code is written in a text file with the extension [ *.v ]. 


Any source code is created with the combination of characters and words which are called KEYWORDS and SYNTAX/SEMANTICS. These are globally called lexical tokens. Combination of more than one lexical tokens originate LEXICAL CONVENTIONS.



1.  LEXICAL CONVENTIONS

Lexical tokens in Verilog HDL are given below:

  • white space
  • comment
  • operator
  • number
  • string
  • identifier
  • keyword

1.1  white space or blank spaces

White spaces are used in the strings which can be incorporated using the double quotes (“). Follow the below table containing the semantics required to provide the white spaces in the string sequence:

Escape String

Character produced by escape string

\n

New line character

\t

Tab character

\\

\ character

\”

“ character

\ddd

A character specified in 1-3 octal digits (0 <= d <= 7)


If less than three characters are used, the following character shall not be an octal digit. 

Implementations may issue an error if the character represented is greater than \377.


1.2  comments

Comments are used to increase the readability of code. Comments are ignored by the Simulator. Verilog HDL has two forms to introduce comments in the source code which are given below:

1.2.1.  Single Line Comment

// This line is commented.

1.2.2.  Multiple-Line Comments

     /* This line is commented.

          This line is commented on too. */


1.3  operators

The operation which is to be performed is decided by operators. There are three kinds of operators available in Verilog HDL which are given below:

1.3.1.  Unary Operator: work on single operands.

1.3.2.  Binary Operator: work on two operands.

1.3.3.  Ternary Operator: work on three operands.

1.4  numbers

Constant numbers are represented in two ways:

  • Integer Constants

  • Real Constants


1.4.1  Integer Constants

Integer constants can be specified in decimal, hexadecimal, octal, or binary format.

There are two forms to express integer constants which are given below:

  • A.  Simple decimal number, which shall be specified as a sequence of digits 0 through 9, optionally starting with a plus or minus unary operator.
  • B.  Based constant, which shall be composed of up to three tokens.
    • an optional size constant, shall specify the size of the constant in terms of its exact number of bits.
    • an apostrophe character (', ASCII 0x27) followed by a base format character, shall consist of a case insensitive letter specifying the base for the number. Legal base specifications are d, D, h, H, o, O, b, or B, for the bases decimal, hexadecimal, octal, and binary respectively. The apostrophe character and the base format character shall not be separated by any white space.
    • the digits representing the value of the number.

It shall be legal to macro substitute these three tokens.

Negative Constant Numbers

Simple decimal numbers without the size and the base format shall be treated as signed integers,

whereas the numbers specified with the base format shall be treated as signed integers if the s designator is included or as unsigned integers if the base format only is used.

The s designator does not affect the bit pattern specified, only its interpretation.

A plus or minus operator preceding the size constant is a unary plus or minus operator.

e.g., -4 or +4

A plus or minus operator between the base format and the number is an illegal syntax.

-4'd7 // Used for performing unsigned integer math, will be stored as 2's complement of 7

-4'sd7 // Used for performing signed integer math

4'd-7 // Illegal

Unsized Constant Numbers

Numbers specified without a base format are considered as decimal numbers by default. Numbers that are written without size are simulator/machine specific but must be at least 32 bits.

659 // is a decimal number

'h 837FF // is a hexadecimal number

'o7460 // is an octal number

4af // is illegal (hexadecimal format requires 'h)

Sized Constant Numbers

4'b1001 // is a 4-bit binary number

'D 3 // is a 5-bit decimal number (Simulator/Machine Specific)

X or Z Values

3'b01x // is a 3-bit number with the least

// significant bit unknown

12'hx // is a 12-bit unknown number

16'hz // is a 16-bit high-impedance number

16'sd? // the same as 16'sbz

The default length of x and z is the same as the default length of an integer.

1.4.2  Real Constants

Real numbers are specified as given below:

  • decimal notation (e.g., 14.72)

  • scientific notation (e.g., 39e8)

  • scaled notation (e.g., 24.7K)

Real numbers expressed with a decimal point shall have at least one digit on each side of the decimal point.

Underscore Character

The underscore character is legal anywhere in an integer constant.

The underscore character is legal anywhere in a real constant except as the first character of the constant or the first character after the decimal point. The underscore character is ignored by the simulator.

E,g., 16’b1011_1000_1111_1010 // Underscore increase the readability of a number constant


1.5  string

A string is a sequence of characters enclosed by double quotes ( " ) and contained on a single line. Strings used as operands in expressions and assignments shall be treated as unsigned integer constants represented by a sequence of 8-bit ASCII values, with one 8-bit ASCII value representing one character.


1.6  Identifier

Identifier shall be used to give an object a unique name so it can be referenced. An identifier shall either be a simple identifier or an escaped identifier.

Simple Identifier

It shall be any sequence of letters,digits, dollar signs ($), and the underscore characters (_).

The first character of an identifier shall not be a digit or $; it can be a letter or an underscore. Identifiers shall be case sensitive.

e.g.,

    shiftreg_a

    busa_index

    error_condition

    merge_ab

    _bus3

    n$657

Implementations may set a limit on the maximum length of identifiers, but they shall be at least 1024 characters. If an identifier exceeds the implementation-specified length limit, an error shall be reported.

Escaped Identifiers

Escaped identifiers shall start with the backslash character (\) and end with white space (space, tab, newline, or formfeed).

They provide a means of including any of the printable ASCII characters in an identifier (the decimal values 33 through 126 or 21 through 7E in hexadecimal).

Neither the leading backslash character nor the terminating white space is considered to be part of the identifier. Therefore, an escaped identifier \cpu3 is treated the same as a non-escaped identifier cpu3.

e.g.,

    \busa+index

    \-clock

    \***error-condition***

    \net1/\net2

    \{a,b}

    \a*(b+c)



1.7  Keyword

Keywords are special identifiers reserved to define the language constructs. Keywords are in lowercase.

Friday, January 28, 2022

Unix For VLSI Industry - Part 4a- TASK 1 - SHELL Scripting


SHELL Scripting Task - 1


In the previous articles or say Assignment 1 and Assignment 2, I have captured few of the practice question related to debugging of SHELL Scripting. We know very well how much this is important from Industry point of view. By now you are very much expert in the UNIX Command and specially writing any Kind of Script using the SHELL Scripting. I am now going to give you few small-small TASK which you can do yourself and these can be used directly in Industry with small modification as per the requirement.

This is the based on script or say automation, we normally use during regression-runs. If you don't know what's regressions - basically engineers run a set of code/tool on large set of small designs (known as Test cases) and analysis the result with respect to each test cases. Few test case are designed to be failed and few suppose to a pass (means should give expected result). If after running the code - result is different from the expected one - We consider regression testcase fail. :)
As you can see that it's looks like Interesting first time but it's huge in numbers and that's the reason there is a lot of automation happen around this using different type of scripting language. Today, we will discuss about the same but in SHELL scripting.

Task 1a -: There is a directory named “regression” inside which there are more than a thousand sub-directories. Each sub-directory has two more sub-directories which are “golden_dir” and “current_dir”. Inside these sub-directories, a file with different versions is present. 
  • Write a script to diff files present in “golden_dir” and “current_dir”. If diff is valid then update the “golden_dir” sub-directory with the new version of the file.

Task 1b -: 
  • Step 1: Write a script named “parent.sh” which run another script with “*.sh” extension only. 
  • If the user has a directory including 100 script files with different extensions in it. Write a script “extension_converter.sh” which can convert any extension of file to “*.sh” extension and call this script inside “parent.sh” script.

Task 1c :- 
  • There is a directory containing more than 100 script files written in python, perl, and TCL syntax. 
  • Each python script has first line i.e., /usr/bin/python
  • Each Perl script has first line i.e., /usr/bin/perl
  • Each tcl script has first line i.e., /usr/bin/tclsh
Write a script to dump python scripts to “python” directory, perl scripts to “perl” directory, and TCL scripts to “TCL” directory.


Task 1d :A directory contains thousands of files created in 2020 and 2021. 
  • Write a script to create a log file which contains the list of all files created in June 2020 along with the modified time.


Try these task and I am sure if you have prepared them today - Tomorrow you can use these in Industry with slight modification.
Stay Tune and Happy Learning.

Tuesday, January 25, 2022

Unix For VLSI Industry - Part 3b - Assignment 2 - SHELL Scripting


SHELL Scripting Debugging Assignment - 2


In the previous assignment, I have captured few of the practice question related to debugging part. We know very well how much this is important from Industry point of view. Like I said in last article - Understanding the Scripting syntax and using them in automation - these are 2 different things. Debugging the existing code OR say enhancing that as per the requirement is completely different. For that first you need to understand the different error messages and then you should know how to fix that part.
In this part also - there are list of questions which can help you to do practice.

  1. Correct the below shell script to split the given string.

#! /bin/bash

string="abc,xyz,jkl,raw"

IFS=' '

read -r arr <<< "$string"

for val in "${arr[@]}";

do

  printf "name = $val\n"

done


  1. Correct the below shell script to match the two input strings.

read a

read b

if ["$a"=!"$b"] then

    echo "Matched"

elif

    echo "Not Matching"

fi


  1. Correct the below shell script to create a shell function library.

function square(){

    v1=$10

    $n = $((v1*v1))

    echo $n

}


  1. Correct the below shell script to create a shell function library to calculate the factorial of a given number.

function factorial(){

    v1=$2

    n=1

    while [[ $v1 -gt 0 ]]; 

do

v1=$(($v1 - 1))

done

    echo $n

}


  1. Correct the below shell script to calculate the number of users.

read userInput

case [$userInput] [in]

    {1} lslogins -o USER ;

    {2} who --count | grep users ;

    {3} who -q | grep -v users ;

    {4} groups ;

    {*} echo -e "Please Enter Correct Input \n" ;

esac


  1. Correct the below shell script to find out how many terminals a user has logged-in.

read input

if [[ $input ]] and [ $input eq $input 2>\dev\null ]

then

echo "Number of terminals are : "

cat /etc/passwd | grep $input -c 

else

cat /etc/passwd>userlist

echo "Number of terminals are : "

grep -c $input userlist

fi


  1. Correct the shell script to swap two numbers.

first = 5

second = 10

$temp = first

$first = second

$second = temp


  1. Correct the shell script to find out the largest number among the arguments in the script.

max=$1

for arg in "$#"

do

    if [ "arg" gt "max" ]

    then

        $max=arg

    fi

done

echo "Largest value is: $max"


  1. Correct the below script to calculate the sum of digits of the given number.

Num=123

g=$Num

s=0

while { $Num gt 0 }

do

    k = $Num % 10

    $Num = $Num / 10

    $s = $s + $k 

done

echo  "sum of digits of $g is : $s"


  1. Correct the below script to display all words of a given file in ascending order.

read filename

if [ ! -f $filename ]

then

echo "File does not exist"

else

for i in $(cat $filename)

do

echo $i > "TEMP"

done

echo "$(sort "$TEMP")"

fi

if [ -f "$TEMP" ]

then

rm "$TEMP"

fi


This assignment is for your practice - We have inserted Errors for you, so that you can understand the error messages and fix accordingly. 
I can understand that you may be in need to solution - And Solution is that it should run error free. :) 
Again - It's for your Practice, not just to copy paste the code and use it anywhere. 
   
  Stay Tune and Happy Learning. We discuss more in next Article.:)

Saturday, January 22, 2022

Unix For VLSI Industry - Part 3a - Assignment 1 - SHELL Scripting


SHELL Scripting Debugging Assignment - 1

Understanding the Scripting syntax and using them in automation - these are 2 different things. A lot of times people use google to check the syntax and they have some logic to capture them but when they compile - they face a lot of error. And that's the point they stuck because debugging itself is a Skill and which always come by experience or say practice. In next couple of assignments, you will get few practice questions where you have to debug and find out the error and fix the script.

  1. Find the errors in the below shell script for calculating the factorial of a given number:

factorial() {

product=$1

if((product <= 2)); then

echo $product

else

f=$((product -1))

f = $(factorial $f)

f = $((f*product))

echo $f

fi

}

echo "Enter the number:"   

read num

if(num == 0); then   

echo 1

else

factorial $num

fi


  1. Correct the below shell script for a counter using a while loop.

#!/bin/bash

valid=true

count=1

while [ $valid ]

do

echo $count

if [ $count -eq 5 ];

then

break

fi

(count++)

done


  1. Find out the errors in the below shell script for a down counter.

#!/bin/bash

for (( counter=10; counter>0; counter-- ))

do

echo -n "$counter "

printf "\n"


  1. Correct the errors in the below shell script.

#!/bin/bash

echo "Enter your lucky number"

read n

case $n in

1)

echo "Gold Winner" ;

2)

echo "Silver Winner" ;

3)

echo "Bronze Winner" ;

*)

echo "Better Luck Next Time" ;

esac


  1. Correct the below shell script to return the string for the function.

#!/bin/bash

function func_name() {

str="VSLI Expert, $input"

echo $str

}

echo "Enter your name"

read input

val=$((func_name))

echo "Return value of the function is $val"


  1. Change the below script to print Fibonacci series in vertical order.

read N

x=0

y=1

echo "The Fibonacci series is : "

for (( i=0; i<N; i++ ))

do

    echo -n "$x "

    sum=$((x + y))

    x=$y

    y=$sum

done


  1. Correct the below script to find out the prime number.

read num

i=2  

f=0

while test $i -le ((expr $num / 2)) 

do

if test "expr $num % $i" -eq 0 

then

f=1

fi

i=`expr $i + 1`

done

if test $f -eq 1 

then

echo "Given number is not Prime number"

else

echo "Given number is Prime number"

fi


  1. Correct the below shell script for sorting the list.

arr=(100 40 220 1100 102)

echo ${arr[*]}

for ((i = 0; i<5; i++))

do

    for((j = 0; j<5-i-1; j++))

    do

        if [ ${arr[$j]} -gt ${arr[$((j+1))]} ]

        then

            temp=${arr[j]}

            arr[j]=${arr[$((j+1))]}  

            arr[$((j+1))]=temp

        fi

    done

done

echo ${arr[*]}


  1. Correct the below shell script to traverse a given list of numbers.

arr=(10 12 63 34 58)

for i in "${arr[$?]}"

do

    echo $i

done


  1. Correct the below script to generate the password with the given length by user.

read  PASS_LENGTH

for p in $(seq 1 5);                                   

do 

    openssl rand -base64 48 ; cut -c1-$PASS_LENGTH 

done


 This assignment is for your practice - you can practice on these scripting questions and later it will help you a lot in the Industry.
   
  Stay Tune and Happy Learning. We discuss more in next Assignment.:)

Must Read Article

Related Posts Plugin for WordPress, Blogger...