Wednesday, November 14, 2018

TCL Practice Task 1 (Scripting Language)

VLSI Industry has requirement of TCL (a scripting language). Lot of Institutes helping students to learn this language. But somehow students don't know much about the use of this in real VLSI World. In this article, I am listing down few of the small projects or say programs or exercise which you should try at least once before entering into the VLSI Industry. If you are able to automate below few task, more then 50% of work (based on TCL) can be done easily.

Task 1:- Input Output File Handling & Rearranging Data

Step 1: Create a file and named it "file_input1.txt" (Content of "file_input1.txt" is given below - Remember, you have create file exactly same as given. All spaces and format should be in same manner)

Input File: “file_input1.txt”

    Name        Delay    Trans    Load
    ---------------------------------------------
    AND1_2X      1.50ns    0.1    1.25ff
    AND1_3X      1.75ns    0.2    1.98ff
    AND2_3X      2.37ns    0.3    2.27ff
    AND2_4X      1.82ns    0.5    2.54ff
    NAND1_2X    2.14ns    0.2    1.69ff
    NAND2_3X    2.48ns    0.3    2.11ff

Step 2: Create a TCL based program which will read input file ("file_input1.txt) and rearrange the data as per below format.

    Name        Trans    Load    Delay
    ---------------------------------------------
    AND1_2X      0.1    1.25ff    1.50ns
    AND1_3X      0.2    1.98ff    1.75ns
    AND2_3X      0.3    2.27ff    2.37ns
    AND2_4X      0.5    2.54ff    1.82ns
    NAND1_2X    0.2    1.69ff    2.14ns
    NAND2_3X    0.3    2.11ff    2.48ns

Step 3: Print the above formated data onto the terminal & in output file "file_output.txt"

Learning after completion of this task:
  • How to Open and Close file
  • How to Read or Write from/in a file
  • How to save data in List and do different operation on that
  • Different commands usemodel
    • list, lindex, lappend
    • Foreach loop, While loop, If-else loop
    • gets and puts
    • split
  • How to manage single space, multiple space
  • If you are using regular expression - then things will be different but as such this program can be written without any regular expression.
  • If you are new to Linus environment then few more learning after this task
    • How to write a file using any unix editor
    • How to source a file and execute a program
  • If you want to use Procedure (equivalent to functions in C)
    • How to write a Procedure
    • How to pass a value to Procedure
    • How to call a Procedure

Industrial Use of this Task:
Most of the time, we work on automation of data in Industry.This data present in the form of report file (.rpt file) or output file or may be logfile. After reading data, we have to rearrange that data as per required format or say template (may be because of any other tool requirement or other scripting requirement or creating graphs/charts etc.)

I am sure, it will help you. In the Next article, we will discuss another Task with multiple file handling and data comparing concepts.

-By Rajat Bansal
(Btech-EC:- 2019 Passout)
(ABES Engineering College)


-Supervised By Puneet Mittal
(Founder & Director)
(VLSI Expert Private Limited)

11 comments:

  1. Please share the TCL program file. I cannot find it on the page

    ReplyDelete
  2. I wrote the script like this. I am a beginner. Suggestions and modifications are welcome.


    #Abhishek Sharma
    #!/usr/bin/tclsh
    #set fileid [open "file_input1.txt" a]
    #puts $fileid
    if {[catch {open "file_input1.txt"} fileid]} {
    puts stderr "error: $fileid"
    } else {
    gets $fileid line
    set var1 [lsearch -inline $line N*]
    set extracted_var [lrange $line 1 3]
    #puts $extracted_var
    set var2 [lsort -decreasing $extracted_var]
    #puts $var2
    set var3 [lindex $var2 0]
    set var4 [lindex $var2 1]
    set var5 [lindex $var2 2]
    puts [format "%-12s %-9s %-9s %-9s" $var1 $var3 $var4 $var5]
    gets $fileid line ;#------------
    puts $line
    while {[gets $fileid line]>=0} {
    regexp {[A-Z0-9_]*} $line match sub1
    #puts $match
    set ex [lrange $line 1 3]
    set var5 [lsort $ex]
    set var6 [lindex $ex 0]
    set var7 [lindex $ex 1]
    set var8 [lindex $ex 2]
    puts [format "%-12s %-9s %-9s %-9s" $match $var6 $var7 $var8]
    } ;#while
    } ;#if

    ReplyDelete
  3. MODIFIED SCRIPT.



    if {[catch {open "file_input1.txt"} file_input1_id]} {
    puts stderr "error : $file_input1_id"
    } else {
    #read data in a variable file_data
    set fp_infile [open "file_input1.txt" r]
    set file_data [read $fp_infile]
    close $fp_infile

    # take each line in as a list element

    set data [split $file_data "\n"]

    #firt row modification

    set first_row [lindex $data 0]
    set name [lindex $first_row 0]
    set sort_first_row [lrange $first_row 1 3]
    set sorted_first_row [lsort -decreasing $sort_first_row]
    set Trans [lindex $sorted_first_row 0]
    set Load [lindex $sorted_first_row 1]
    set Delay [lindex $sorted_first_row 2]
    set display_first_row [format "%-12s %-9s %-9s %-9s" $name $Trans $Load $Delay]
    puts $display_first_row

    #second row
    puts [lindex $data 1]

    #data for different gates
    set list_index 2
    set i 0
    while {($list_index != 8) && ($i<6)} {
    set gate_data [lindex $data $list_index]
    set list_index [expr ($list_index +1)]


    #sort and modify the data now
    regexp {[A-Z0-9_]*} $gate_data name_match
    set sort_values [lrange $gate_data 1 3]
    set delay_value [lsearch -all -inline -regexp $sort_values "ns*"]
    regexp {[0-9.]*} $sort_values trans_value
    set load_value [lsearch -all -inline -regexp $sort_values "ff*"]
    set sorted_values [lsort $sort_values]


    set display_gate_values [format "%-12s %-9s %-9s %-9s" $name_match $trans_value $load_value $delay_value]
    puts $display_gate_values

    set row($i) "$display_gate_values"
    set i [expr ($i+1)]
    };#while

    #print in file_output.txt

    set fp_outfile [open "file_ouput.txt" w]
    puts $fp_outfile $display_first_row
    puts $fp_outfile [lindex $data 1]
    set list_index 2
    while {$list_index != 8} {
    set gate_data [lindex $data $list_index]
    set list_index [expr ($list_index +1)]

    #sort and modify the data now
    regexp {[A-Z0-9_]*} $gate_data name_match
    set sort_values [lrange $gate_data 1 3]
    set sorted_values [lsort $sort_values]

    set g_Trans [lindex $sorted_values 0]
    set g_Load [lindex $sorted_values 1]
    set g_Delay [lindex $sorted_values 2]

    set display_gate_values [format "%-12s %-9s %-9s %-9s" $name_match $g_Trans $g_Load $g_Delay]
    puts $fp_outfile $display_gate_values
    } ;#outfile while
    };#else

    ReplyDelete
  4. set fh [open file_input1.txt r]
    set fp [open file_output.txt w]

    while {[gets $fh line]>=0} {
    if {[regexp "^-" $line]} {
    puts $fp $line
    } else {
    puts $fp "[lindex $line 0]\t[lindex $line 2]\t[lindex $line 3]\t[lindex $line 1]"
    }
    }
    close $fh
    close $fp

    ReplyDelete

  5. set b [open file_input1.txt r+]

    while { [gets $b line] != -1} {


    puts "[lindex $line 0] [lindex $line 2] [lindex $line 3] [lindex $line 1]"

    }

    ReplyDelete
  6. puts "Name Tran Load Delay"
    puts "-------------------------------"
    foreach line $list1 {
    regexp {(([A-Z]*\d_\dX) *(\d.\d*ns) *(\d.\d) *(\d.+))} $line can \
    be done with regexp only;
    puts "$done $regexp\t$only \t $with"
    }

    ReplyDelete
  7. I wrote script as shown below (suggestions are most welcome so do let me know)
    #!/usr/bin/tclsh

    set filename "input-file.txt"
    set fhandle [open $filename r]
    set fileout "output-file.txt"
    set outwrite [open $fileout w+]
    while { [gets $fhandle data] >=0 } {
    set indata4 [lindex $data [expr [llength $data] -4]];#0 index
    set indata1 [lindex $data [expr [llength $data] -3]];#1 index
    set indata2 [lindex $data [expr [llength $data] -2]];#2 index
    set indata3 [lindex $data [expr [llength $data] -1]];#3 index

    set dataout "$indata4 $indata2 $indata3 $indata1"
    puts $dataout
    puts $outwrite $dataout

    }


    close $fhandle
    close $outwrite

    ReplyDelete
  8. set fread [open file_input1.txt r]
    set fwrite [open read_1.txt w]

    proc element {x} {
    for {set j 0 } {$j < [llength $x]} {incr j} {
    set ele$j [lindex $x $j]
    }

    set ele0 [concat $ele0 $ele2 $ele3 $ele1];
    # puts $ele0
    # return $ele0
    }




    set i 0
    while {![eof $fread]} {
    set line [gets $fread]
    set data [split $line "\n"]
    foreach line $data {
    set line$i $line ; # [split $line ]
    incr i

    }

    }


    set e0 [element $line0]
    set e1 [element $line1]
    set e2 [element $line2]
    set e3 [element $line3]
    set e4 [element $line4]
    set e5 [element $line5]
    set e6 [element $line6]




    puts $fwrite $e0
    puts $fwrite $e1
    puts $fwrite $e2
    puts $fwrite $e3
    puts $fwrite $e4
    puts $fwrite $e5
    puts $fwrite $e6

    ReplyDelete
  9. #akshat
    set fp [open input.txt r]
    set fs [open report.txt w]
    while {[gets $fp line]>=0} {

    puts $fs "[lindex $line 0]\t[lindex $line 2]\t[lindex $line 3]\t[lindex $line 1]\t"
    }
    close $fp
    close $fs

    ReplyDelete