#====================================================================================
# Design unit  : SEUs simulation tool (SST)
#
# File name    : SST_startup.tcl
#
# Purpose      : This is a modelsim startup macro for the SST. It is used to 
#                set up the environment to be able to run the SST_upset_generator.pl 
#                script.
#
# Authors      : Daniel González Gutiérrez TEC-EDM
#
# Contact      : mailto:microelectronics@estec.esa.int
#                http://www.estec.esa.int/microelectronics
#
# Copyright (C): European Space Agency (ESA) 2004. No part may be reproduced
#                in any form without the prior written permission of ESA.
#
# Disclaimer   : All information is provided "as is", there is no warranty that
#                the information is correct or suitable for any purpose,
#                neither implicit nor explicit. This information does not
#                necessarily reflect the policy of the European Space Agency.
#====================================================================================

#====================================================================================
# Revision control
# 
###Version 1.0	31/10/03 --DGG
#
###Revision 1.1	22/03/04 --DGG
#
###Revision 1.2	27/07/04 --DGG
#  - struct.dat renamed to hierarchy.dat  
#  - Minor changes to make it compliant with revision 1.2 of SST_list_instances.pl
#
###Revision 1.3	25/11/04 --DGG
#====================================================================================

#----------------------------------------------------------------------------
# SST directories creation
#----------------------------------------------------------------------------  

file mkdir SST SST/wire_files SST/control_files
set wire_f_dir {SST/wire_files}
set control_f_dir {SST/control_files}

#----------------------------------------------------------------------------
# Load configuration file
#----------------------------------------------------------------------------  

do SST_config.tcl

#----------------------------------------------------------------------------
# Create perl package
#---------------------------------------------------------------------------- 

set SST_perl_package [open [file join "$control_f_dir" SST_perl_package.pm] w+]
puts $SST_perl_package "#==================================================="
puts $SST_perl_package "# Perl package generated by SST_startup.tcl"
puts $SST_perl_package "#===================================================\n"
puts $SST_perl_package "package SST_perl_package;"
puts $SST_perl_package "require Exporter;\n"
puts $SST_perl_package "\@ISA    = qw(Exporter);"
puts $SST_perl_package "@EXPORT = qw(\$wire_files_ext \$reference_unit \$reference_step \$td_hierarchy_on \$weight_in \$seu_prob);\n"

puts $SST_perl_package "\$wire_files_ext = \'$wire_files_ext\';"
puts $SST_perl_package "\$reference_unit = \'$reference_unit\';"
puts $SST_perl_package "\$reference_step = $reference_step;"
puts $SST_perl_package "\$td_hierarchy_on = $td_hierarchy_on;"
puts $SST_perl_package "\$weight_in = $weight_in;"
puts $SST_perl_package "\$seu_prob = $seu_prob;"
close $SST_perl_package

#----------------------------------------------------------------------------
# Structure window processing
#----------------------------------------------------------------------------  

view structure
set design [.structure.tree expandall -1]
if {!$design} {
  destroy .structure
  error {No design loaded}
}  
.structure.tree write $control_f_dir.\\hierarchy.dat

# create the file all_instances.dat from hierarchy.dat
exec perl -S SST_list_instances.pl $control_f_dir.\\hierarchy.dat

#----------------------------------------------------------------------------
# Signals windows processing
#----------------------------------------------------------------------------  

# read all_instances.dat line by line to open each instance and
# lists its signals. 

set all_instances [open [file join "$control_f_dir" all_instances.dat]]
# These two gets are used to remove the header. 
gets $all_instances line 
gets $all_instances line 
view signals
while { [gets $all_instances raw_line] != -1} {
  
  # we need a couple of intermediate variables to get the full path of 
  # the new instance, otherwise we loose all the backslashes present in it
  regsub -all {\s+} $raw_line { } line
  set list [split $line { }]
  set sim_path [lindex $list 3]
  env $sim_path
  
  # We want the signals window to display only signals 
  # Set acc to AllSignals 
  .signals.tree config -acc AllSignals
  
  # Check modelsim version & set wire mask
  set vernum [regexp -inline {\d.\d} [vsimId]]
  if {$vernum < 5.8} {  
    .signals.tree.tree1 config -viewmask $wire_mask 
  } else {
    .signals.tree config -viewmask $wire_mask   
  }
  
  if {[.signals.tree size] != 0} {
    .signals.tree expandall -1
    # delete 'sim' from $sim_path
    set filename [lindex $list 1]
    .signals.tree write $wire_f_dir.\\$filename.sig
  } 
}
destroy .signals
destroy .structure
close $all_instances

# create .in files from .sig files
exec perl -S SST_wires_parser.pl
