# Generate FPGA Array Data
## run_tool -name {GENERATEPROGRAMMINGDATA} 
run_tool_wrapper "run_tool -name {GENERATEPROGRAMMINGDATA} 
"
                  
# Configure and generate Design Initialization Data and Memories
# The following can be configured:
#   - Design initialization source - sNVM/uPROM/SPI-Flash
#   - sNVM user clients
#   - uPROM user clients
#   - Fabric RAM initialization content
#   - SPI-Flash user clients
# Examples TBD
#
# Example for configuring user snvm clients 
# Note that if using relative path, the path to the mem file specified in the SNVM.cfg file is relative to the libero *.prjx file. So please adjust the path according to the project location and where the SNVM.cfg and mem files are located. 
#configure_snvm -cfg_file {../snvm/SNVM.cfg} 
configure_ram -cfg_file {./src/cfg/RAM.cfg}

#use a dummy spi flash cfg to enable auto update alone for all the spi images
configure_spiflash -cfg_file {./src/cfg/spiflash_autoupdateon.cfg}

configure_design_initialization_data -second_stage_start_address {0x00000000} \
-third_stage_uprom_start_address {0x00000000} \
-third_stage_snvm_start_address {0x00000000} \
-third_stage_spi_start_address {0x00000400} \
-third_stage_spi_type {SPIFLASH_NO_BINDING_PLAINTEXT}\
 -third_stage_spi_clock_divider {6} \
 -init_timeout 128\
 -auto_calib_timeout {3000}\
 -broadcast_RAMs {1}\
 -standalone_initialization {0} 

generate_design_initialization_data

#1
 configure_tool \
         -name {CONFIGURE_PROG_OPTIONS} \
         -params {design_version:0} \


## run_tool -name {GENERATEPROGRAMMINGFILE} 
#run_tool -name {GENERATEPROGRAMMINGFILE}   
# Configure and generate programming file data
# Examples for configuring the programming files TBD
## run_tool -name GENERATEPROGRAMMINGFILE



configure_tool \
         -name {GENERATEPROGRAMMINGFILE} \
         -params {program_fabric:true} \
         -params {program_security:false} \
         -params {program_snvm:true} \
         -params {sanitize_snvm:false}
run_tool_wrapper "run_tool -name {GENERATEPROGRAMMINGFILE} 
"

puts "Programmingfile generated successfully\n"

# Export Golden SPI Image
export_bitstream_file \
         -file_name {golden_image_v0} \
         -export_dir ${PrjLocation}/designer/top/export \
         -format {SPI} \
         -for_ihp 0 \
         -limit_SVF_file_size 0 \
         -limit_SVF_file_by_max_filesize_or_vectors {} \
         -svf_max_filesize {} \
         -svf_max_vectors {} \
         -master_file 0 \
         -master_file_components {} \
         -encrypted_uek1_file 0 \
         -encrypted_uek1_file_components {} \
         -encrypted_uek2_file 0 \
         -encrypted_uek2_file_components {} \
         -trusted_facility_file 1 \
         -trusted_facility_file_components {FABRIC SNVM} \
         -zeroization_likenew_action 0 \
         -zeroization_unrecoverable_action 0 \
         -master_backlevel_bypass 0 \
         -uek1_backlevel_bypass 0 \
         -uek2_backlevel_bypass 0 \
         -master_include_plaintext_passkey 0 \
         -uek1_include_plaintext_passkey 0 \
         -uek2_include_plaintext_passkey 0 \
         -sanitize_snvm 0 

puts "Exported GOLDEN SPI IMAGE successfully\n"


#2
configure_tool \
         -name {CONFIGURE_PROG_OPTIONS} \
         -params {design_version:2} \
         -params {silicon_signature:23456789} 

configure_tool \
         -name {GENERATEPROGRAMMINGFILE} \
         -params {program_fabric:true} \
         -params {program_security:false} \
         -params {program_snvm:true} \
         -params {sanitize_snvm:false}
run_tool_wrapper "run_tool -name {GENERATEPROGRAMMINGFILE} 
"


puts "Programmingfile generated successfully\n"

#Export Auto update SPI Image
export_bitstream_file \
         -file_name {update_image_v2} \
         -export_dir ${PrjLocation}/designer/top/export \
         -format {SPI} \
         -for_ihp 0 \
         -limit_SVF_file_size 0 \
         -limit_SVF_file_by_max_filesize_or_vectors {} \
         -svf_max_filesize {} \
         -svf_max_vectors {} \
         -master_file 0 \
         -master_file_components {} \
         -encrypted_uek1_file 0 \
         -encrypted_uek1_file_components {} \
         -encrypted_uek2_file 0 \
         -encrypted_uek2_file_components {} \
         -trusted_facility_file 1 \
         -trusted_facility_file_components {FABRIC SNVM} \
         -zeroization_likenew_action 0 \
         -zeroization_unrecoverable_action 0 \
         -master_backlevel_bypass 0 \
         -uek1_backlevel_bypass 0 \
         -uek2_backlevel_bypass 0 \
         -master_include_plaintext_passkey 0 \
         -uek1_include_plaintext_passkey 0 \
         -uek2_include_plaintext_passkey 0 \
         -sanitize_snvm 0 	
puts "Exported AUTOUPDATE SPI IMAGE successfully\n"


#3
configure_tool \
         -name {CONFIGURE_PROG_OPTIONS} \
         -params {design_version:5} \
         -params {silicon_signature:56789ABC} 

configure_tool \
         -name {GENERATEPROGRAMMINGFILE} \
         -params {program_fabric:true} \
         -params {program_security:false} \
         -params {program_snvm:true} \
         -params {sanitize_snvm:false}
run_tool_wrapper "run_tool -name {GENERATEPROGRAMMINGFILE} 
"

puts "Programmingfile generated successfully\n"

#Export IAP SPI Image
export_bitstream_file \
         -file_name {iap_image_v5} \
         -export_dir ${PrjLocation}/designer/top/export \
         -format {SPI} \
         -for_ihp 0 \
         -limit_SVF_file_size 0 \
         -limit_SVF_file_by_max_filesize_or_vectors {} \
         -svf_max_filesize {} \
         -svf_max_vectors {} \
         -master_file 0 \
         -master_file_components {} \
         -encrypted_uek1_file 0 \
         -encrypted_uek1_file_components {} \
         -encrypted_uek2_file 0 \
         -encrypted_uek2_file_components {} \
         -trusted_facility_file 1 \
         -trusted_facility_file_components {FABRIC SNVM} \
         -zeroization_likenew_action 0 \
         -zeroization_unrecoverable_action 0 \
         -master_backlevel_bypass 0 \
         -uek1_backlevel_bypass 0 \
         -uek2_backlevel_bypass 0 \
         -master_include_plaintext_passkey 0 \
         -uek1_include_plaintext_passkey 0 \
         -uek2_include_plaintext_passkey 0 \
         -sanitize_snvm 0 
puts "Exported IAP IMAGE successfully\n"


#Main spi flash cfg
configure_spiflash -cfg_file {./src/cfg/spiflash_main.cfg}

generate_design_initialization_data

configure_tool \
         -name {CONFIGURE_PROG_OPTIONS} \
         -params {design_version:1} \
         -params {silicon_signature:12345678} 
    
# Configure and generate programming file data
# Examples for configuring the programming files TBD

	 
configure_tool \
         -name {GENERATEPROGRAMMINGFILE} \
         -params {program_fabric:true} \
         -params {program_security:false} \
         -params {program_snvm:true} \
         -params {sanitize_snvm:false} 
run_tool_wrapper "run_tool -name GENERATEPROGRAMMINGFILE
"

puts "Programmingfile generated successfully\n"
        
export_bitstream_file \
         -file_name {top} \
         -export_dir ${PrjLocation}/designer/top/export \
         -format {STP DAT PPD} \
         -for_ihp 0 \
         -limit_SVF_file_size 0 \
         -limit_SVF_file_by_max_filesize_or_vectors {} \
         -svf_max_filesize {} \
         -svf_max_vectors {} \
         -master_file 0 \
         -master_file_components {} \
         -encrypted_uek1_file 0 \
         -encrypted_uek1_file_components {} \
         -encrypted_uek2_file 0 \
         -encrypted_uek2_file_components {} \
         -trusted_facility_file 1 \
         -trusted_facility_file_components {FABRIC SNVM} \
         -zeroization_likenew_action 0 \
         -zeroization_unrecoverable_action 0 \
         -master_backlevel_bypass 0 \
         -uek1_backlevel_bypass 0 \
         -uek2_backlevel_bypass 0 \
         -master_include_plaintext_passkey 0 \
         -uek1_include_plaintext_passkey 0 \
         -uek2_include_plaintext_passkey 0 \
         -sanitize_snvm 0 
          
puts "Exported bit stream successfully\n"

# Export Programming Job
# Programming job files can be imported in FlasPro Express standalone for programming the device
export_prog_job \
    -job_file_name {top} \
    -export_dir ${PrjLocation}/designer/top/export \
    -bitstream_file_type {TRUSTED_FACILITY} \
    -bitstream_file_components {FABRIC SNVM} \
    -program_design 1 \
    -program_spi_flash 1 \
	-zeroization_likenew_action 0 \
    -zeroization_unrecoverable_action 0 \
    -include_plaintext_passkey 0 \
	-design_bitstream_format {PPD} \
    -prog_optional_procedures {} \
    -skip_recommended_procedures {} \
    -sanitize_snvm 0 
	
puts "Exported job file successfully\n"

## run_tool -name {GENERATE_SPI_FLASH_IMAGE} 
run_tool_wrapper "run_tool -name {GENERATE_SPI_FLASH_IMAGE} 
"

puts "SPI Flash image generated successfully\n"

export_spiflash_image -file_name {top} -export_dir ${PrjLocation}/designer/top/export

puts "Exported SPI Flash image successfully\n"

puts "Full design flow passed execution\n"	
