# 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 %}


# Import HDL sources and constraints
import_files \
        {% 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] %}
import_files \
        -library {{op}}{{lib}}{{cl}} \
        {% for f in files %}
        -hdl_source {{op}}{{f}}{{cl}} \
        {% endfor %}

{% 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}}

# 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 %}

# Configure Synthesize tool to use the generated Synplify TCL script
configure_tool -name {SYNTHESIZE} \
        -params {SYNPLIFY_TCL_FILE:../../{{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 %}

{% set ns = namespace(SDC=false) %}{# If SDC files are present #}
{% for src_file in src_files if src_file|constraint_file_filter("SDC") %}
{% set ns.SDC=true %}
puts "----------------------- Synthesize Constraints ---------------------------"
puts "File: {{prj_root}}/{{src_file|constraint_file_filter}}"
{% endfor %}

{%- if ns.SDC %}
# Configure Synthesize tool to use the project constraints
organize_tool_files -tool {SYNTHESIZE} \
        {% for src_file in src_files if src_file|constraint_file_filter("SDC") %}
        -file {{op}}{{prj_root}}/{{src_file|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 ----------------------"
{% for src_file in src_files if src_file|constraint_file_filter %}
puts "File: {{prj_root}}/{{src_file|constraint_file_filter}}"
{% endfor %}

organize_tool_files -tool {PLACEROUTE} \
        {% for src_file in src_files if src_file|constraint_file_filter %}
        -file {{op}}{{prj_root}}/{{src_file|constraint_file_filter}}{{cl}} \
        {% endfor %}
        -module {{op}}{{toplevel}}::work{{cl}} -input_type {constraint}

{% if ns.SDC %}
# Configure Verify Timing tool to use the project constraints
{% endif %}
{%- for src_file in src_files if src_file|constraint_file_filter("SDC") %}
puts "----------------------- Verify Timings Constraints -----------------------"
puts "File: {{prj_root}}/{{src_file|constraint_file_filter}}"
{% endfor %}

{%- if ns.SDC %}
organize_tool_files -tool {VERIFYTIMING} \
        {% for src_file in src_files if src_file|constraint_file_filter("SDC") %}
        -file {{op}}{{prj_root}}/{{src_file|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 -----------------------------"
