# start by reading in all the IP blocks generated by vivado

set partNumber $::env(XILINX_PART)
set boardName $::env(XILINX_BOARD)
set boardSubName [lindex [split ${boardName} :] 1]
set board $::env(board)

#set partNumber xc7a100tcsg324-1
#set boardName digilentinc.com:arty-a7-100:part0:1.1
#set boardSubName arty-a7-100
#set board ArtyA7

#set partNumber xcvu095-ffva2104-2-e
#set boardName xilinx.com:vcu108:part0:1.7
#set boardSubName vcu108
#set board FPU_VCU

set ipName WallyFPGA

create_project $ipName . -force -part $partNumber
if {$boardName!="ArtyA7"} {
    set_property board_part $boardName [current_project]
}

# read package first
add_files  ../src/CopiedFiles_do_not_add_to_repo/cvw.sv
#read_verilog -sv  ../src/wallypipelinedsocwrapper.sv
# then read top level
if {$board=="ArtyA7"} {
    add_files  {../src/fpgaTopArtyA7.sv}
} else {
    add_files  {../src/fpgaTop.sv}
}

# read in ip
import_ip IP/sysrst.srcs/sources_1/ip/sysrst/sysrst.xci
import_ip IP/ahbaxibridge.srcs/sources_1/ip/ahbaxibridge/ahbaxibridge.xci
import_ip IP/clkconverter.srcs/sources_1/ip/clkconverter/clkconverter.xci

if {$board=="ArtyA7"} {
    import_ip IP/ddr3.srcs/sources_1/ip/ddr3/ddr3.xci
    import_ip IP/mmcm.srcs/sources_1/ip/mmcm/mmcm.xci
} else {
    import_ip IP/ddr4.srcs/sources_1/ip/ddr4/ddr4.xci
}

# read in all other rtl
add_files [glob -type f  ../src/CopiedFiles_do_not_add_to_repo/*/*.sv ../src/CopiedFiles_do_not_add_to_repo/*/*/*.sv]

set_property include_dirs {../src/CopiedFiles_do_not_add_to_repo/config ../../config/shared} [current_fileset]


# define top level
set_property top fpgaTop [current_fileset]

update_compile_order -fileset sources_1
# This is important as the ddr3/4 IP contains the generate clock constraint which the user constraints depend on.
exec mkdir -p reports/
exec rm -rf reports/*


report_compile_order -constraints > reports/compile_order.rpt

# this is elaboration not synthesis.
#synth_design -rtl -name rtl_1  -flatten_hierarchy none

# apply timing constraint after elaboration
if {$board=="ArtyA7"} {
    add_files -fileset constrs_1 -norecurse ../constraints/constraints-$board.xdc
    set_property PROCESSING_ORDER NORMAL [get_files  ../constraints/constraints-$board.xdc]
} else {
    add_files -fileset constrs_1 -norecurse ../constraints/constraints-$boardSubName.xdc
    set_property PROCESSING_ORDER NORMAL [get_files  ../constraints/constraints-$boardSubName.xdc]
}


# Temp
set_param messaging.defaultLimit 100000

# this does synthesis?

launch_runs synth_1 -jobs 16

wait_on_run synth_1
open_run synth_1

report_clocks -file reports/clocks.rpt

check_timing -verbose                                                   -file reports/check_timing.rpt
report_timing -max_paths 10 -nworst 10 -delay_type max -sort_by slack   -file reports/timing_WORST_10.rpt
report_timing -nworst 1 -delay_type max -sort_by group                  -file reports/timing.rpt
report_utilization -hierarchical                                        -file reports/utilization.rpt
report_cdc                                                              -file reports/cdc.rpt
report_clock_interaction                                                -file reports/clock_interaction.rpt

write_verilog -force -mode funcsim sim/syn-funcsim.v

if {$board=="ArtyA7"} {
    #source ../constraints/small-debug.xdc
    #source ../constraints/small-debug-rvvi.xdc
    source ../constraints/small-debug-wfi.xdc
} else {
    #source ../constraints/vcu-small-debug.xdc
    #source ../constraints/small-debug.xdc
    #source ../constraints/small-debug.xdc
    source ../constraints/big-debug-spi.xdc
}


# set for RuntimeOptimized implementation
#set_property "steps.place_design.args.directive" "RuntimeOptimized" [get_runs impl_1]
#set_property "steps.route_design.args.directive" "RuntimeOptimized" [get_runs impl_1]

launch_runs impl_1 -jobs 16
wait_on_run impl_1
launch_runs impl_1 -to_step write_bitstream
wait_on_run impl_1
open_run impl_1

# output Verilog netlist + SDC for timing simulation
exec mkdir -p sim/
exec rm -rf sim/*

write_verilog -force -mode funcsim sim/imp-funcsim.v
write_verilog -force -mode timesim sim/imp-timesim.v
write_sdf     -force sim/imp-timesim.sdf

# reports
check_timing                                                              -file reports/imp_check_timing.rpt
report_timing -max_paths 10 -nworst 10 -delay_type max -sort_by slack     -file reports/imp_timing_WORST_10.rpt
report_timing -nworst 1 -delay_type max -sort_by group                    -file reports/imp_timing.rpt
report_utilization -hierarchical                                          -file reports/imp_utilization.rpt
