
#########################################################
#                 2_synthesize_optimize.tcl
#
# Description:  Synthesize and optimize the 
#               DEMO CHIP and generate
#               the Oasys-RTL databases. 
#
# Usage:        source in Oasys-RTL Command prompt
#
# Dependencies: init_design.tcl
#               1_read_design.tcl
#               Launched from Oasys-RTL shell
#
#########################################################


#=======================================================#
#Check Status of Flow and load prior scripts if needed
#=======================================================#
set script_dir [file dirname [info script]]
set ekit_dir   [file dirname ${script_dir}]

#Check if dependent scripts have been loaded
if {![info exists top_module]} {
     source ${script_dir}/init_design.tcl
     source ${script_dir}/1_read_design.tcl
}
#=======================================================#
#Synthesize the DEMO CHIP RTL core
#=======================================================#

if {![file exists ${ekit_dir}/riscv_rtl/hw/rtl/pc.sv]} {
  puts "ERROR: RTL source files from OpenCores.org were not found.\n"
  puts "You can read a design database for a previously optimized design"
  puts "by running the following command:\n"
  puts "read_db ${ekit_dir}/demo_chip_rtl/demo_chip.odb\n\n"
  return
}

#Perform Synthesis
synthesize -module ${top_module} -map_to_scan -gate_clock

set_route_layer_max_usage metal2 0.5
set_route_layer_max_usage metal3 0.8
set_route_layer_max_usage metal6 0.8

write_db  ${output_dir}/odb/riscv_chip.syn.odb

#=======================================================#
##Read constraints (logical and physical)
#=======================================================#
#read_sdc -verbose $riscv.sdc 
read_sdc -verbose  ${ekit_dir}/constraints/riscv.sdc 
report_design_metrics
check_timing

# Create User Path groups
group_path -name I2R -from [all_inputs]
group_path -name I2O -from [all_inputs] -to [all_outputs]
group_path -name R2O                    -to  [all_outputs]
report_path_groups

#=======================================================#
#Optimize for timing 
#=======================================================#
optimize -virtual
write_db  ${output_dir}/odb/riscv_chip.virtual_opt.odb
report_timing
report_path_groups


echo "\n-------------------------------------"
echo "\nSynthesis and optimization complete"
echo "\n-------------------------------------\n"

