# -----------------------------------------------------------------------------
#  The confidential and proprietary information contained in this file may
#  only be used by a person authorised under and to the extent permitted
#  by a subsisting licensing agreement from ARM limited.
#
#             (C) COPYRIGHT 2018 ARM limited.
#                 ALL RIGHTS RESERVED
#
#  This entire notice must be reproduced on all copies of this file
#  and copies of this file may only be made by a person if such person is
#  permitted to do so under the terms of a subsisting license agreement
#  from ARM limited.
#
#       SVN Information
#
#       Checked In          : $Date$
#
#       Revision            : $Revision$
#
#       Release Information : Cortex-M1 DesignStart-r0p1-00rel0
#                             
# -----------------------------------------------------------------------------
#  Project : Cortex-M1 Arty A7 Example design with V2C-DAPLink adaptor board
#
#  Purpose : Script to create bit and mcs files for Arty A7 board
#
#            Combines the original bit file, mmi file, and software elf to create
#            the full bitstream
#            Then converts full bitstream to mcs for download to the onboard flash
#
#            Can be run either in Vivado GUI TCL console, or else in batch mode
#            from command line
#            If run in Vivado TCL console, pwd needs to be set to root of project, 
#            in the same location as the bit file
# -----------------------------------------------------------------------------

# Input files
set mmi_file            "./m1.mmi"
set elf_file            "../bsp/bootloader.elf"
set source_bit_file     "./aum1.runs/impl_1/au_top.bit"
set reference_bit_file  "./aum1_reference.bit"

# Output files
set output_bit_file "aum1.bit"
set output_bin_file "aum1.bin"

# Enable to turn on debug
set updatemem_debug 0

# Assemble bit file that can be downloaded to device directly
# Combine the original bit file, mmi file, and software elf to create the full bitstream

# Delete target file
file delete -force $output_bit_file
file delete -force $output_bin_file

# Determine if the user has built the project and has the target source file
# If not, then use the reference bit file shipped with the project
if { ![file exists $source_bit_file] } {
    puts "\n********************************************"
    puts "INFO - File $source_bit_file doesn't exist as project has not been built"
    puts "       Using $reference_bit_file instead\n"
    puts "********************************************/n"
    set source_bit_file $reference_bit_file
}

# Banner message to console as there is no output for a few seconds
puts "  Running updatemem ..."

if { $updatemem_debug } {
    set error [catch {exec updatemem --debug --force --meminfo $mmi_file --data $elf_file --bit $source_bit_file --proc dummy --out $output_bit_file} result]
} else {
    set error [catch {exec updatemem --force --meminfo $mmi_file --data $elf_file --bit $source_bit_file --proc dummy --out $output_bit_file} result]
}

# Print the stdout from updatemem
puts $result

# Updatemem returns 0 even when there is an error, so cannot trap on error.  Having deleted output file to start, then
# detect if it now exists, else exit.
if { ![file exists $output_bit_file] } {
    puts "ERROR - $output_bit_file not made"
    return -1
} else {
    puts "\n********************************************"
    puts "  $output_bit_file correctly generated"
    puts "********************************************\n"
}

# Create BIN file for base board QSPI flash memory
write_cfgmem -force -format BIN -size 2 -interface SPIx1 -loadbit " up 0 $output_bit_file" $output_bin_file

# Check BIN was correctly made
if { ![file exists $output_bin_file] } {
    puts "ERROR - $output_bit_file not made"
    return -1
} else {
    puts "\n********************************************"
    puts "  $output_bin_file correctly generated"
    puts "********************************************\n"
}