Search code examples
tclmodelsim

Determine if design element exists in library with script


I would like to determine whether or not a design element exists (has been compiled) in a given library in ModelSim (I'm using 10.3c PE) using Tcl, but I can't seem to find an appropriate function. Something like this theoretical code:

if {[design_object exists $lib.$entity]} {
  ...

While not ideal, I can check for certain custom libraries with:

if {[file exists $lib_path]} {
  ...

This uses a filesystem access, of course, and while ideally I would have liked to check for a logical name, this workaround is good enough for my limited purposes for now.

Unfortunately, there does not seem to be an exact equivalent for design entities, as ModelSim doesn't create individual files for compiled entities. I've considered parsing the library's _info file for the entity name, but that could be a relatively long operation. Is there a built-in way of doing this? Do ModelSim's Tcl extensions even provide access to logical names (outside of a simulation context)?


Solution

  • It looks like the vdir command is what you need to inspect the contents of a Modelsim library programmatically. It returns a multi-line string where each line has an object type followed by the name of the object. Entities can be extracted with the following:

    proc get_vdir_entities {lib_name} {
      set contents [split [vdir -lib $lib_name] "\n"]
      set rval {}
      foreach c $contents {
        if [regexp "^ENTITY" $c] {
          lappend rval [lindex $c 1]
        }
      }
      return $rval
    }
    
    set entities [get_vdir_entities "work"]
    

    Earlier solution

    Looking at the _info file shows that all compiled entities are recorded as strings with an "E" prefixed to their name. A quick test in a shell produced a comprehensive list for me:

    strings _info | sed -n -e "/^E/ p"
    

    It looks like there is a consistent prefix before these strings of "62 20 31 0a" hex and they are terminated with 0a hex. You can do the extraction in pure Tcl with the following:

    proc get_modelsim_entities {info_file} {
      set fh [open $info_file r]
      fconfigure $fh -translation binary
      set fields [split [read $fh] "\n"]
      close $fh
    
      set rval {}
      foreach f $fields {
        if [regexp -nocase "^E\[a-z0-9_\]+$" $f] {
          lappend rval [string range $f 1 end]
        }
      }
    
      return $rval
    }
    
    
    set entities [get_modelsim_entities "path/to/your/_info"]