You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TCL脚本修改求助:将temp.txt总线定义插入.lib指定pin位置

修正TCL脚本实现正确插入总线定义

问题根源

你的脚本没有正确定位到[0]结尾的pin定义位置,而是直接将总线定义写入文件开头,同时缺少同名pin组的匹配逻辑。

核心修改方向

  • 缓存总线定义:从temp.txt中按pin前缀(如ADDR对应ADDR[0])分组存储type和bus块,用字典关联pin名与对应定义。
  • 逐行定位插入:遍历XXXX.lib时,匹配到[0]结尾的pin起始行后,立即插入对应总线定义,而非全局写入文件开头。
  • 标记pin组范围:跟踪是否处于目标pin组内,避免误插入到其他位置。

修正后的代码示例

# 从temp.txt提取总线定义,按pin名存入字典
set bus_dict [dict create]
set curr_pin ""
set curr_def ""

set temp_f [open "temp.txt" r]
while {[gets $temp_f line] != -1} {
    # 收集type和bus块内容
    if {[string match "*type*" $line] || [string match "*bus*" $line]} {
        append curr_def "$line\n"
    }
    # 匹配带[0]的pin起始行,完成上一个pin的定义存储
    elseif {[regexp {^pin\s+(\w+)\[0\]} $line -> pin_base]} {
        if {$curr_pin ne "" && $curr_def ne ""} {
            dict set bus_dict $curr_pin $curr_def
        }
        set curr_pin $pin_base
        set curr_def ""
    }
}
# 处理最后一个pin的定义
if {$curr_pin ne "" && $curr_def ne ""} {
    dict set bus_dict $curr_pin $curr_def
}
close $temp_f

# 处理XXXX.lib,插入对应总线定义
set lib_in [open "XXXX.lib" r]
set lib_out [open "XXXX.lib.tmp" w]
set in_target_group 0

while {[gets $lib_in line] != -1} {
    # 匹配目标pin起始行,插入对应总线定义
    if {[regexp {^pin\s+(\w+)\[0\]} $line -> pin_base]} {
        if {[dict exists $bus_dict $pin_base]} {
            puts $lib_out [dict get $bus_dict $pin_base]
        }
        set in_target_group 1
    }
    # 检测pin组结束,重置状态
    if {$in_target_group && [string match "pin*" $line] && ![string match "*\[0\]" $line]} {
        set in_target_group 0
    }
    puts $lib_out $line
}

close $lib_in
close $lib_out

# 替换原文件(按需启用)
# file rename -force "XXXX.lib.tmp" "XXXX.lib"

关键说明

  • 字典bus_dict实现pin名与总线定义的精准映射,确保只插入同名pin组的定义。
  • 逐行处理lib文件时,找到目标pin行后立即插入定义,避免全局写入错误。
  • 状态变量in_target_group防止误插入到其他pin组或块中。

内容的提问来源于stack exchange,提问作者san269605

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 13:55:36