#!/usr/local/bin/perl -w
#====================================================================================
# Design unit  : SEUs simulation Tool (SST)
#
# File name    : SST_test_loop_time.tcl
#
# Purpose      : This script is used to upset one FF at different time windows.
#
# 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	15/11/04 --DGG
#====================================================================================
#########################################################################################
# Before running this script we need to:
# * Load the design and run SST_startup.tcl
# * Have an starting all_instances.dat file that we want to consume
#########################################################################################

#----------------------------------------------------------------------------
# My variables
#----------------------------------------------------------------------------  
set before [clock seconds]
set ok_to_upset 0
set simulation_end 0
set window_width_value 20
# Note that we use the same unit everywhere
set unit "us"
set first_time_value 160
set last_time_value 220

#----------------------------------------------------------------------------
# SST directory and file names
#----------------------------------------------------------------------------  

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

set all_instances_dat [file join $control_f_dir all_instances.dat] 
set sst_do [file join "../Spacewire/uod/spwb1-4/codec/src/syn/sim/" $control_f_dir sst.do]

#----------------------------------------------------------------------------
# Script main body
#---------------------------------------------------------------------------- 

set start_time_value $first_time_value

while {!$simulation_end} {
  
  set AI [open $all_instances_dat r]
  while { [gets $AI ai_line] != -1} {
    if { ([regexp -nocase {^ *Y} $ai_line]) } {
      set ok_to_upset 1
    }     
  }  
  close $AI
  
  # If at least there is one instance to be upset, execute the script and
  # run a simulation
  
  if {$ok_to_upset} {
    # Execute SST_upset_generator to prepare the environment to upset only
    # the FF we are interested in
    
    exec perl -S SST_upset_generator.pl -iread -nfilter 1 q -t $start_time_value$unit $window_width_value$unit
        
    # run the modelsim macro created by that script
    do $sst_do
    
    #Check status at the end of the simulation
    view signals
    env /tb_top/uod_ctrl_1
    set tb_ok_index [.signals.tree find tb_receive_success]
    set tb_ok [.signals.tree get2 $tb_ok_index]
    set uut_ok_index [.signals.tree find uut_receive_success]
    set uut_ok [.signals.tree get2 $uut_ok_index]
    set keep_running_tb [regexp {true} $tb_ok]
    set keep_running_uut [regexp {true} $uut_ok]
    
    if { ($keep_running_tb) && ($keep_running_uut) } {
      destroy .source
      restart -f
    } else {
      puts stdout "---Errors found!!\n\n"
      set simulation_end 1
    }  
  } else {
    set simulation_end 1
  }
  
  # Update time values
  
  set start_time_value [expr ($start_time_value + $window_width_value)]
  if {$start_time_value >= $last_time_value} {
    set simulation_end 1
  }  
  set ok_to_upset 0
}
destroy .signals
set after [clock seconds]
set total [expr $after - $before]
echo "Total Time " [clock format $total -format {%H h %M min %S sec} -gmt 1]