天天看点

Using Tcl With Synopsys(一)一.开始使用TCL二. TCL基础

Using Tcl With Synopsys

  • 一.开始使用TCL
    • 1. 帮助命令: help
    • 2. 帮助命令: man
    • 3. 命令状态
    • 4.输出数据: echo和puts
    • 5.命令解析(parsing)
      • 5.1替换(substitution) [],$,/
      • 5.2 引用(substitution) ",{}
      • 5.3 特殊字符总结
  • 二. TCL基础
    • 1. 变量(Variables)
    • 2. 脚本Scripts
      • 1. 注释
      • 2. 加载脚本
      • 3. 重定向脚本输出
      • 4. 简单脚本实例
    • 3. 数据类型(Data Types)
      • 1.字符串(Strings)
      • 2.列表(Lists)
      • 3. 数组(Arrays)
    • 4. 表达式(Expressions)
    • 5. 控制流(Control Flow)
      • 1.if
      • 2.while
      • 3.for
      • 4. foreach
      • 5.break and continue
      • 6. switch
    • 6. 基本文件操作(Basic File Commands)
      • 1. file命令
      • 2. glob命令
      • 3. open,close,flush

Using Tcl With Synopsys 学习记录

一.开始使用TCL

1. 帮助命令: help

EX:

dc_shell-t> help Procedures
           

2. 帮助命令: man

Ex:

dc_shell-t> man query_objects
           

3. 命令状态

命令状态为命令的返回值

Ex:

dc_shell-t> set total_cells 0
0
dc_shell-t> incr total_cells
1
           

4.输出数据: echo和puts

  1. echo

    Syntax: echo ?-n? ?argument?

    -n 输出不换行

    Using Tcl With Synopsys(一)一.开始使用TCL二. TCL基础
  2. puts

    Syntax: puts ?-nonewline? ?file_id? ?arg?

    -nonewline	不换行
    -file_id 	文件
    -arg 	输出数据
               

5.命令解析(parsing)

5.1替换(substitution) [],$,/

  1. 命令替换: []
    dc_shell-t> set a [expr 24*2]
               
  2. 变量替换: $.
    psyn_shell-t> set a 24.
    24
    psyn_shell-t> set b [expr $a * 2]
    48
               
  3. \ 替换

    命令行延续.

    psyn_shell-t> echo "This is line 1.\nThis is line 2."
    This is line 1.
    This is line 2.
               

5.2 引用(substitution) ",{}

  1. {}:强引用。

    对各种特殊字符都不会做特殊处理,跟普通字符一样处理。

    dc_shell-t> set a 5; set b 10
    10
    dc_shell-t> echo {[expr $b - $a]} evaluates to [expr $b - $a]
    [expr $b - $a] evaluates to 5
               
  2. “”: 弱引用。

    各种分隔符不作处理,但是对于换行符(\n)及$和[ ]两种置换符会照常处理。

    dc_shell-t> set A 10; set B 4
    4
    dc_shell-t> echo "A is $A; B is $B.\nNet is [expr $A - $B]."
    A is 10; B is 4.
    Net is 6.
               

5.3 特殊字符总结

  1. \ 打印特殊字符
    psyn_shell-t> set gp 1000; set ex 750
    750
    psyn_shell-t> echo "Net is: \$"[expr $gp - $ex]
    Net is: $250
               
Using Tcl With Synopsys(一)一.开始使用TCL二. TCL基础

二. TCL基础

1. 变量(Variables)

TCL支持两种类型变量:simple和array。

  1. 用法
    • 注意:TCL中所有的变量都是字符串。
      dc_shell> set buf_name lsi_10k/B1I
      lsi_10k/B1I
                 
    • append 追加字符串
      psyn_shell-t> set c1 U1; set c2 U2
      U2
      psyn_shell-t> append c1 " " $c2
      U1 U2
                 
    • incr 累加
      psyn_shell-t> set b 10
      10
      psyn_shell-t> incr b
      11 
      payn_shell-t> incr b -6
      5
                 
    • unset 删除变量
      psyn_shell-t> set b 10
      10
      psyn_shell-t> unset b
                 
    • info exists b :查看变量是否存在
      psyn_shell-t> info exists b
      0
                 
    • info vars b :查看变量匹配

      psyn_shell-t> info vars total_c*

  2. 数字变量精确度

    精确度取决于分配数字的位数

    fpc_shell> set a 10; set b 4.0; set c 4
    4		
    fpc_shell> expr $a/$b
    2.5
    fpc_shell> expr $a/$c
    2
               
  3. 预申明变量

    TCL中预申明变量比较少,一个例子是 env, 它是一个数组,包含了Synopsys命令运行的UNIX环境变量。

    • 可以通过array命令的names选项来查看列表
      dc_shell-t> array names env
                 
    • 通过$array()访问
      dc_shell-t> echo $env(HOME)
                 
    • getev查看环境变量
      dc_shell-t> getenv HOME
                 

2. 脚本Scripts

1. 注释

在注释行使用\后,下一行依会被视作注释。

# set CLK_NAME Sysclk; set CLK_PERIOD 10; \
	set INPUT_DELAY 2
           

2. 加载脚本

```
dc_shell-t> source mysession.tcl
 ```
           

3. 重定向脚本输出

利用-echo -verbose选项。
利用>。
```
dc_shell-t> source -echo -verbose myrun.tcl
dc_shell-t> source -echo -verbose myrun.tcl > myrun.out
```
           

4. 简单脚本实例

```javascript
set DESIGN_NAME top
set SUB_MODULE [list sub1.v sub2.v sub3.v]
set CLK_NAME Sysclk
set CLK_PERIOD 10
set INPUT_DELAY 2
set OUTPUT_DELAY 3
read_verilog [list $SUB_MODULE $DESIGN_NAME.v]
current_design $DESIGN_NAME
link
create_clock -p $CLK_PERIOD -n $CLK_NAME [get_ports $CLK_NAME]
set_input_delay $INPUT_DELAY -clock $CLK_NAME [list [all_inputs]]
set_output_delay $OUTPUT_DELAY -clock $CLK_NAME [list [all_outputs]]
compile
```
           

3. 数据类型(Data Types)

1.字符串(Strings)

  1. Syntax : string option ?arg …?
    • 比较compare
      string compare ?-nocase? ?-length len? str1 str2
                 
      根据字典顺序来比较字符串。使用-nocase来完成大小写无关的比较。使用- length来限制比较头len个字符。如果字符串相同则返回0,如果str1的顺序比str2靠前就返回-1,对于其它情况返回1。
    • 转换为大写

      string toupper string

      Using Tcl With Synopsys(一)一.开始使用TCL二. TCL基础
      细节见Tcl中的字符串处理

2.列表(Lists)

  1. 创建列表
    set D_pins "I1/FF3/D I1/FF4/D I1/FF5/D" 
    set D_pins {I1/FF3/D I1/FF4/D I1/FF5/D}
    set D_pins [list I1/FF3/D I1/FF4/D I1/FF5/D]
    set compound_list [list {x y} {1 2.5 3.75 4} {red green blue}]
               
  2. 访问数组

    lindex $list_name 0… 实现下标访问

    psyn_shell-t> echo [lindex $D_pins 0]
    I1/FF3/D
    psyn_shell-t> echo [lindex $compound_list 1]
    1 2.5 3.75 4
               
    注意:{}提供替换,所以必须用list关键字去创建有内嵌命令的列表,如:
    dc_shell> set a 5
     5 
    dc_shell> set b {c d $a [list $a Z]}
     c d $a [list $a z]}
    dc_shell> set b [list c d $a [list $a Z]]
     c d 5 {5 z}
    dc_shell> set b "c d $a [list $a Z]"
     c d 5 5 z
               
Using Tcl With Synopsys(一)一.开始使用TCL二. TCL基础

详细参考TCL列表相关命令

3. 数组(Arrays)

  1. 建立数组
    fpc_shell> set vio_rpt_ext(ir_drop) .volt
    .volt
    fpc_shell> set vio_rpt_ext(curr_dens) .em
    .em
    fpc_shell> set vio_rpt_ext(curr) .current
    .current
               
    感觉数组更像perl的哈希:
    Using Tcl With Synopsys(一)一.开始使用TCL二. TCL基础
  2. 访问数组
    fpc_shell> echo $vio_rpt_ext(curr)
               
  3. 数组属性
    dc_shell> array size vio_rpt_ext
    3
    dc_shell> array names vio_rpt_ext
    curr curr_dens ir_drop
               

4. 表达式(Expressions)

使用expr计算表达式

psyn_shell-t> set p 5
5
psyn_shell-t> set a [expr (12*$p)]
60
           
Using Tcl With Synopsys(一)一.开始使用TCL二. TCL基础
Using Tcl With Synopsys(一)一.开始使用TCL二. TCL基础

5. 控制流(Control Flow)

基本控制流命令:if,while, for, foreach, break, continue, switch

1.if

注意,if用{ },而不是(),与C++不同。

```
if {$x == 0} {  
echo "Equal"
} elseif {$x > 0} {
echo "Greater"
} else {
echo "Less"
}
```
           

2.while

```
set p 0
while {$p <= 10} {
echo "$p squared is: [expr $p * $p]"
incr p
```
           

3.for

Syntax:

for {initial_expr} {termination_expr} {reinit_expr}{

script

}

for {set p 0} {$p <= 10} {incr p} {
echo echo "$p squared is: [expr $p * $p]"
}
           

4. foreach

Syntax:

foreach var $somelist{

script

}

dc_shell-t> set mylist {I1/FF3/D I1/FF4/D I1/FF5/D}
I1/FF3/D I1/FF4/D I1/FF5/D
dc_shell-t> foreach i $mylist {echo $i}
I1/FF3/D
I1/FF4/D
I1/FF5/D
           

5.break and continue

break 退出当前循环

foreach f [which {VDD.ave GND.tech p4mvn2mb.idm}]
{
echo -n "File $f is "
if { [file isdirectory $f] == 0 } {
echo "NOT a directory"
} else {
echo "a directory"
break
}
}
           

continue 直接开始下一次循环

set p 0
while {$p <= 10} {
if {$p % 2} {
incr p
continue
}
echo "$p squared is: [expr $p * $p]"; incr p  ## 同一行的两个语句才加;
}
           

6. switch

统计不同文件名文件的数量

# Tabulates files by their type
set fnames [glob *.em *.volt *.current]
set curr_ct 0; set em_ct 0; set volt_ct 0
foreach f $fnames {
set f_ext [file extension $f]
switch $f_ext {
.current {incr curr_ct}
.em {incr em_ct}
.volt {incr volt_ct}
}
}
echo "There are $curr_ct current files."
echo "There are $em_ct current density files."
echo "There are $volt_ct IR drop files."
           

6. 基本文件操作(Basic File Commands)

1. file命令

命令 功能
file dirname fname 返回文件相对路径
file exists fname 文件存在返回1
file extension fname 返回文件扩展名
file isdirectory fname 如果是目录返回1
file isfile fname 如果是文件返回1
file readable fname 可读返回1
file rootname fname 返回文件名部分
file size fname 返回文件大小Bytes
file tail fname 返回文件列表的最后一个
file writable fname 可写返回1

2. glob命令

产生匹配到一个或者多个的文件列表

set flist [glob *.em *.volt] # match .en or .volt
           

3. open,close,flush

  1. open fname ?access_mode?

    访问模式 | 功能

    -------- | -----

    r | 只读,文件必须存在

    r+ | 读写,文件必须存在

    w | 只写,若文件存在,截断,不存在,新建

    w+ | 读写打开,若文件存在,截断,不存在,新建

    a | 只写打开,追加新数据,文件必须存在

    a+ | 读写打开,追加新数据,如果文件不存在,新建

  2. open and close
    set fid [open VDD.em w+]
    close $fid
               
  3. 文件写出时,当在缓冲队列时,可能不会立即在文件中显示,用以下命令刷新
    flush $fid
               

4.gets and puts

gets 读取文件中单行,读取完毕,返回-1,并给var赋一个空字符串

gets $fid var
           

puts ?fid? var

写出到文件或者直接输出

例如:

# Write out a line of text, then read it back and print it
set fname "mytext.txt"
# Open file, then write to it
set fid [open $fname w+]
puts $fid "This is my line of text."
close $fid
#
# Open file, then read from it
set fid [open $fname r]
set data_in [gets $fid]
close $fid
#
# Print out data read
echo $data_in
           

继续阅读