In tcl how can I group signals and put them in a particular category I have a few signals
ahb_hdata
apb_pdata_out
axi_reps_n
apb_req_n
ahb_reset_n
apb_pwrite
ahb_haddr
axi_ready_in
axi_trans
I wanted to group the signals in this format
ahb: apb: axi:
ahb_hdata apb_pdata_out axi_trans
ahb_reset_n apb_pwrite axi_reps_n
ahb_haddr apb_req_n axi_ready_in
So far I was able to search whether substring is present or not, But here I have many substrings that are to be searched and had to group them, I didn't how to group them and put them under a particular category
set string "ahb_hdata, apb_pdata_out, axi_reps_n, apb_req_n, ahb_reset_n, apb_pwrite,
ahb_haddr, axi_ready_in, axi_trans "
set substring "ahb"
if {[string first $substring $string] != -1} {
puts "$substring"
}
CodePudding user response:
Grouping everything is easy with dict
s - use the prefix as key and each value is a list of names. It's formatting the output into columns that's a little tricky, but since lindex
with an index out of range just returns an empty string, still fairly clean - calculate the maximum length of the lists of common prefixes, and iterate up to that, extracting the Nth value of each list in turn:
#!/usr/bin/env tclsh
proc group_signals {signals} {
set sigs [dict create]
set lengths [dict create]
foreach sig $signals {
if {[regexp {^[^_] } $sig prefix]} {
dict lappend sigs $prefix $sig
dict incr lengths $prefix
}
}
# Header
puts "[join [dict keys $sigs] ":\t"]:"
set maxlen [::tcl::mathfunc::max {*}[dict values $lengths]]
for {set n 0} {$n < $maxlen} {incr n} {
set line {}
dict for {_ list} $sigs {
lappend line [lindex $list $n]
}
puts [join $line \t]
}
}
group_signals {
ahb_hdata
apb_pdata_out
axi_reps_n
apb_req_n
ahb_reset_n
apb_pwrite
ahb_haddr
axi_ready_in
axi_trans
}