#!/usr/local/bin/perl -w
#====================================================================================
# Design unit  : SEUs simulation Tool (SST)
#
# File name    : SST_test_loop_ff.tcl
#
# Purpose      : This script is used to upset several FF in a serial manner (one per
#                run) in order to locate a damaged TMR in a particular netlist
#                (spwrlink_tmr).
#
# 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 consumed 0
set simulation_end 0

#----------------------------------------------------------------------------
# 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 all_instances_bak [file join $control_f_dir all_instances.bak]
set all_instances_tmp [file join $control_f_dir all_instances.tmp]

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

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

while {!$simulation_end} {
  
  # we need two temporary files, one holding only the instance selected in this
  # particular run (AI_TMP) and the one holding all the instances but the one
  # selected (AI_CNS)
        
  set AI [open $all_instances_dat r]
  set AI_TMP [open $all_instances_tmp w]
  set AI_BAK [open $all_instances_bak w]
   
  while { [gets $AI ai_line] != -1} {
    if { ([regexp -nocase {^ *Y} $ai_line]) && ($consumed == 0)} {
      set consumed 1
      puts $AI_TMP $ai_line
      regsub -nocase {Yes} $ai_line {No} ai_bak_line
      puts $AI_BAK $ai_bak_line
    } elseif { ([regexp -nocase {^ *y} $ai_line]) && ($consumed == 1) } {
      puts $AI_BAK $ai_line
      regsub -nocase {Yes} $ai_line {No} ai_tmp_line
      puts $AI_TMP $ai_tmp_line
    } else {
      puts $AI_TMP $ai_line
      puts $AI_BAK $ai_line
    }
  }  
  close $AI
  close $AI_TMP
  close $AI_BAK
  file rename -force $all_instances_tmp $all_instances_dat
  
    
  # If at least there is one instance left, execute the script and
  # run a simulation
  if {$consumed} {
    # 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 3 q -t 180us 30us
    
    # run the modelsim macro created by that script
    #vsim -restore test_loop.cp -do $sst_do
    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
  }
  
  file rename -force $all_instances_bak $all_instances_dat
  set consumed 0
}

destroy .signals
set after [clock seconds]
set total [expr $after - $before]
echo "Total Time " $total " Seconds" 