package require ::quartus::flow
#==============================================================================
# Project Settings
#==============================================================================
# Quartus Project Name
set projName top_quartus
# Quartus Project Directory
set projDir  ./results
# FPGA Top Level Entity
set topName  top

#==============================================================================
# Create Project
#==============================================================================
puts "------------------------"
puts "Creating Project        "
puts "------------------------"
# Delete project directory if exists
foreach dirname $projDir {
  if [catch {file delete -force -- $dirname} result] {
    puts "Error deleting $dirname \[$result\]"
  }
}
# Create project directory
file mkdir $projDir
cd   $projDir
# Create Project
project_new -revision $projName $projName

puts "------------------------"
puts "Including Source Files  "
puts "------------------------"
# --------------------------------------
# Include VHDL Sources and set Top Level
# --------------------------------------
source ../sources.tcl
set_global_assignment -name TOP_LEVEL_ENTITY $topName

puts "------------------------"
puts "Including Constrains    "
puts "------------------------"
# Load Physical Constrains
source ../physical.tcl
# Load Timing Constrains
set_global_assignment -name SDC_FILE ../timing.sdc

# ---------------------------------
# Simulation Net/List VHDL settings
# ---------------------------------
set_global_assignment -name EDA_SIMULATION_TOOL "ModelSim (VHDL)"
#set_global_assignment -name EDA_SIMULATION_TOOL "ModelSim-Altera (VHDL)"
set_global_assignment -name EDA_TIME_SCALE "1 ps" -section_id eda_simulation
set_global_assignment -name EDA_OUTPUT_DATA_FORMAT VHDL -section_id eda_simulation

# ---------------------------------
# Timing parameters
# ---------------------------------
set_global_assignment -name TIMEQUEST_DO_REPORT_TIMING ON


# ------------------------
# Commit and Close
# ------------------------
# Commit assignments
export_assignments

# ------------------------
# Run Implementation
# ------------------------
puts "------------------------"
puts "Running Mapper          "
puts "------------------------"
if {[catch {execute_module -tool map} result]} {
puts "\nResult: $result\n"
puts "ERROR: Analysis & Synthesis failed. See the report file.\n"
} else {
puts "\nINFO: Analysis & Synthesis was successful.\n"
}


puts "------------------------"
puts "Running Fitter          "
puts "------------------------"
if {[catch {execute_module -tool fit} result]} {
puts "\nResult: $result\n"
puts "ERROR: Fitter failed. See the report file.\n"
} else {
puts "\nINFO: Fitter was successful.\n"
}

puts "------------------------"
puts "Running Assembler       "
puts "------------------------"
if {[catch {execute_module -tool asm} result]} {
puts "\nResult: $result\n"
puts "ERROR: Assembler failed. See the report file.\n"
} else {
puts "\nINFO: Assembler was successful.\n"
}

puts "------------------------"
puts "Running STA             "
puts "------------------------"
if {[catch {execute_module -args --report_script=../../../scripts/_ext_ttsl/impl_scripts/quartus_gen_timing_reports.tcl -tool sta} result]} {
puts "\nResult: $result\n"
puts "ERROR: Static timing analysis failed. See the report file.\n"
} else {
puts "\nINFO: Static timing analysis was successful.\n"
}
puts "------------------------"
puts "Running EDA Gen         "
puts "------------------------"
if {[catch {execute_module -tool eda} result]} {
puts "\nResult: $result\n"
puts "ERROR: EDA Generator failed. See the report file.\n"
} else {
puts "\nINFO: EDA Generator was successful.\n"
}
puts "------------------------"
puts "Generete Reports        "
puts "------------------------"
exec python ../publish_worst_case_paths.py top_quartus

puts "------------------------"
puts "Closing Project         "
puts "------------------------"
# Close Project
project_close
