# SPDX-FileCopyrightText: 2022 CERN (home.cern)
#
# SPDX-License-Identifier: CERN-OHL-S-2.0+
#
# Author: Grzegorz Daniluk <greg.daniluk@cern.ch>

# Borrowed and adjusted from GSI bel-projects
proc qmegawiz {files} {
  set dir [file dirname [info script]]
  post_message "Testing for megawizard regeneration in $dir:$files"

  set device  [ get_global_assignment -name DEVICE ]
  set family  [ get_global_assignment -name FAMILY ]

  foreach i $files {
    if {![file exists "$dir/$i.qip"] || [file mtime "$dir/$i.txt"] > [file mtime "$dir/$i.qip"]} {
      post_message -type info "Regenerating $i using qmegawiz"
      file delete "$dir/$i.qip"
      file copy -force "$dir/$i.txt" "$dir/$i.vhd"

      set sf [open "| qmegawiz -silent \"-defaultfamily:$family\" \"-defaultdevice:$device\" OPTIONAL_FILES=SIM_NETLIST \"$dir/$i.vhd\" 2>@stderr" r]
      while {[gets $sf line] >= 0} { post_message -type info "$line" }
      if {[catch {close $sf} err]} {
	post_message -type error "Executing qmegawiz: $err"
	exit 1
      }
      if {![file exists "$dir/$i.qip"]} {
	post_message -type error "Executing qmegawiz: did not create $dir/$i.qip!"
	exit 1
      }

      file mtime "$dir/$i.qip" [file mtime "$dir/$i.vhd"]
    }
      set_global_assignment -name QIP_FILE "$dir/$i.qip"
  }
}

source ../16z091-01_src/Source/x4/x4.tcl
source ../16z091-01_src/Source/alt_reconf/gen_alt_reconf.tcl
source ../top/pll_pcie/gen_pll_pcie.tcl
source ../16z126-01_src/Source/z126_01_pasmi/gen_m25p32.tcl
source ../16z126-01_src/Source/z126_01_ru/gen_ru.tcl

# Needed for testbench
source ../16z091-01_src/Source/x1/x1.tcl
