# This file is generated by Edalize.
# Microsemi Tcl Script
# Libero

puts "----------------- Creating project {{name}} ------------------------------"
# Create a new project with device parameters
new_project \
    -location {{op}}{{prj_root}}{{cl}} \
    -name {{name}} \
    -project_description {} \
    -hdl {{op}}{{tool_options.hdl}}{{cl}} \
    -family {{op}}{{tool_options.family}}{{cl}} \
    -die {{op}}{{tool_options.die}}{{cl}} \
    -package {{op}}{{tool_options.package}}{{cl}} \
{% if tool_options.speed %}
    -speed {{op}}{{tool_options.speed}}{{cl}}{{sp}} \
{% endif %}
{% if tool_options.dievoltage %}
    -die_voltage {{op}}{{tool_options.dievoltage}}{{cl}}{{sp}} \
{% endif %}
{% if tool_options.range %}
    -part_range {{op}}{{tool_options.range}}{{cl}}{{sp}} \
{% endif %}
{% if tool_options.defiostd %}
    -adv_options {IO_DEFT_STD:{{tool_options.defiostd}}{{cl}} \
{% endif %}

{% if incdirs %}
# Set up the include directories
set_global_include_path_order -paths "{% for incdir in incdirs %} {{incdir}} {% endfor %}"
build_design_hierarchy
{% endif %}

# Link HDL sources and constraints
create_links \
{% for src_file in src_files if src_file|src_file_filter%}
    {{src_file|src_file_filter}}{{cl}} \
{% endfor %}

# Import HDL sources on libraries (logical_names)
{% for library in library_files.items() %}
{% set lib = library[0] %}
{% set files = library[1] %}
create_links \
        -library {{op}}{{lib}}{{cl}} \
        {% for f in files %}
        -hdl_source {{op}}{{f}}{{cl}} \
        {% endfor %}

{% endfor %}

# Source user defined TCL scripts
{% for src_file in src_files if src_file|tcl_file_filter%}
puts "---------- Executing User TCL script: {{src_file|tcl_file_filter|replace("source", "")|trim}} ----------"
{{src_file|tcl_file_filter}}
{% endfor %}

# Build design hierarchy and set the top module
build_design_hierarchy
puts "Setting top level module to: {{op}}{{toplevel}}::work{{cl}}"
set_root -module {{op}}{{toplevel}}::work{{cl}}



# Configure Synthesize tool to use the generated Synplify TCL script 
configure_tool -name {SYNTHESIZE} \
        -params [join "SYNPLIFY_TCL_FILE:" [file join [file dirname [info script]] /{{name}}-syn-user.tcl]]

puts "Configured Synthesize tool to use script: {{name}}-syn-user.tcl"
puts "Configured Synthesize tool to include dirs:"
{% if incdirs -%}{%- for dir in incdirs %}
puts "- ../../{{dir}}"
{% endfor -%}{% endif %}

puts "----------------------- Synthesize Constraints ---------------------------"
{% set ns = namespace(SYN=false) %}{# If synthesis constraints are present #}
{% for src_file in src_files if src_file|syn_constraint_file_filter %}
{% set ns.SYN=true %}
puts "File: {{src_file|syn_constraint_file_filter}}"
{% endfor %}

{%- if ns.SYN %}
# Configure Synthesize tool to use the project constraints
organize_tool_files -tool {SYNTHESIZE} \
        {% for src_file in src_files if src_file|syn_constraint_file_filter %}
        -file {{op}}{{src_file|syn_constraint_file_filter}}{{cl}} \
        {% endfor %}
        -module {{op}}{{toplevel}}::work{{cl}} -input_type {constraint}
{% endif %}

# Configure Place and Route tool to use the project constraints
puts "----------------------- Place and Route Constraints ----------------------"
{% set ns.PNR=false %}
{% for src_file in src_files if src_file|pnr_constraint_file_filter %}
{% set ns.PNR=true %}
puts "File: {{src_file|pnr_constraint_file_filter}}"
{% endfor %}

{% if ns.PNR %}
organize_tool_files -tool {PLACEROUTE} \
        {% for src_file in src_files if src_file|pnr_constraint_file_filter %}
        -file {{op}}{{src_file|pnr_constraint_file_filter}}{{cl}} \
        {% endfor %}
        -module {{op}}{{toplevel}}::work{{cl}} -input_type {constraint}
{% endif %}

# Configure Verify Timing tool to use the project constraints
puts "----------------------- Verify Timings Constraints -----------------------"
{% set ns.TIM=false %}
{%- for src_file in src_files if src_file|tim_constraint_file_filter %}
{% set ns.TIM=true %}
puts "File: {{src_file|tim_constraint_file_filter}}"
{% endfor %}

{%- if ns.TIM %}
organize_tool_files -tool {VERIFYTIMING} \
        {% for src_file in src_files if src_file|tim_constraint_file_filter %}
        -file {{op}}{{src_file|tim_constraint_file_filter}}{{cl}} \
        {% endfor %}
        -module {{op}}{{toplevel}}::work{{cl}} -input_type {constraint}
{% endif %}

save_project

puts "If desired, execute the {{name}}-run.tcl script to run the generation flow."
puts "----------------- Finished Importing project -----------------------------"
