# viv_do_all.tcl  PeterJ,    24-Jan-2018.
#
# This script does:
#      set project name
#      set part name
#      creates a new project
#      imports the source files
#      launches synthesis
#      launches implementation
#      generates a bitfile
# ------------------------------------

# set proj_name and proj_dir:
source proj_properties.tcl

# Format the raw time [clock seconds] to a date string
puts "[set date_string [clock format [clock seconds] -format "date: %y-%m-%d, time: %H:%M:%S"]] Implementation Started of $proj_name"

#Close currently open project and create a new one. (OVERWRITES PROJECT!!)
close_project -quiet

# Remove old and Create the new build directory
file delete -force $proj_dir
#file mkdir $proj_dir

create_project -force -part $device $proj_name ./$proj_dir
# work_directory is *full* path project directory.
# Vivado doesn't seem to accept relative paths!?
set work_directory [get_property DIRECTORY [current_project]]

# Create a hdl_version.xdc file to set the bitfile USERID to the revision date/version
set revision_log_file "revisiondate_log.txt"
if [file exists $revision_log_file] { 
    set revision_log_fileptr [open $revision_log_file]
    gets $revision_log_fileptr revision_log_date
    gets $revision_log_fileptr revision_log_revnumber
    close $revision_log_fileptr
    set userid [format "0x%6d%02d" $revision_log_date $revision_log_revnumber]
    puts "Bitfile USERID set to $userid"
} else {
    set userid 0xFFFFFFFF
    puts "WARNING Bitfile USERID set to 0xFFFFFFFF"
}
set hdl_version_file [open hdl_version.xdc w]
puts $hdl_version_file "set_property BITSTREAM.CONFIG.USERID $userid \[current_design\]"
close $hdl_version_file

set_property target_language VHDL [current_project]

# ------------------------------------------------------------------------------
# Read source files
# ------------------------------------------------------------------------------

set fp [open "proj_file_list.txt" r]
set file_data [read $fp]
close $fp
set content [split $file_data "\n"]

set use_bmm false

foreach line $content {
  puts $line
  set line_length [ string length $line ]
  if {[string range $line 0 0] == "#"} {
    # puts "comment"
  } elseif {[string range $line $line_length-4 $line_length] == ".vhd"} {
    # puts "vhdl"
    read_vhdl -library work $line
  } elseif {[string range $line $line_length-2 $line_length] == ".v"} {
    # puts "verilog"
    read_verilog -library work $line
  } elseif {[string range $line $line_length-3 $line_length] == ".vh"} {
    # puts "verilog"
    read_verilog -library work $line
  } elseif {[string range $line $line_length-3 $line_length] == ".sv"} {
    # puts "verilog"
    read_verilog -sv  -library work $line
  } elseif {[string range $line $line_length-4 $line_length] == ".svh"} {
    # puts "verilog"
    read_verilog -sv -library work $line
  } elseif {[string range $line $line_length-4 $line_length] == ".xci"} {
    # puts "system verilog"
    read_ip $line
  } elseif {[string range $line $line_length-4 $line_length] == ".xdc"} {
    # Read constraints file if any
    # puts "xdc"
    read_xdc -verbose $line
  } elseif {[string range $line $line_length-4 $line_length] == ".bmm"} {
    # Set pointer to bmm file if any
    # puts "bmm"
    set use_bmm true
    set bmm_file [pwd]/$line
    set bmm_bd ${proj_name}_bd.bmm
    add_files -norecurse $bmm_file
  }
}

# Set Top level
set_property top $proj_name [current_fileset]
set_property source_mgmt_mode All [current_project]
update_compile_order -fileset sources_1

# Pass generics
set_property generic $generics [current_fileset]

puts "INFO: Reading Source Files Done!"

source ../top/ip/processing_system_bd.tcl

# ------------------------------------------------------------------------------

source $script_dir/viv_do_synt.tcl 
source $script_dir/viv_do_impl.tcl

if {$use_bmm} {
  source $script_dir/viv_generate_bd_bmm.tcl
}

file copy -force $proj_dir/$proj_name.runs/impl_1/$proj_name.bit .

write_hw_platform -fixed -force -include_bit -file $proj_name.xsa
