#bd.tcl-file
proc init { cellpath otherInfo } {
  set cell [get_bd_cells $cellpath]
  set paramList7 " C_S_AXI_AWUSER_WIDTH C_S_AXI_ARUSER_WIDTH "
  set paramList { C_S_AXI_ID_WIDTH }
  bd::mark_propagate_only $cell {C_BASEADDR C_HIGHADDR }
  #bd::mark_propagate_only $cell {C_BASEADDR C_HIGHADDR }
  bd::mark_propagate_only $cell $paramList
  bd::mark_propagate_overrideable $cell $paramList7
  set paramList1 { C_AXIS_TDATA_WIDTH C_AXIS_TID_WIDTH C_AXIS_TUSER_WIDTH  C_AXIS_TDEST_WIDTH }
  #bd::mark_propagate_only $cell $paramList1
  bd::mark_propagate_overrideable $cell $paramList1
  set paramList2 { C_AXIS2_HAS_TSTRB C_AXIS2_HAS_TKEEP }
  bd::mark_propagate_only $cell $paramList2
  set paramList3 { C_AXIS1_HAS_TSTRB C_AXIS1_HAS_TKEEP }
  bd::mark_propagate_overrideable $cell $paramList3

  set atg_slave_only  [get_property CONFIG.C_ATG_SLAVE_ONLY  $cell]
  if {[get_property CONFIG.C_ATG_MODE   $cell] == "AXI4-Stream"} {set atg_streaming 1} else {set atg_streaming 0}  
  if {[get_property CONFIG.C_ATG_MODE   $cell] == "AXI4-Lite"} {set atg_lite 1} else {set atg_lite 0}
  if {$atg_slave_only == "false" && $atg_streaming == 0 && $atg_lite == 0} {
    set_property CONFIG.HAS_BURST 0 [get_bd_intf_pins $cellpath/M_AXI]
    set_property CONFIG.HAS_BURST.VALUE_SRC CONSTANT [get_bd_intf_pins $cellpath/M_AXI]
  } 

}

#To update system paramters after the complete core configuration.
proc post_config_ip {cellpath otherInfo } {
 set cell_handle [get_bd_cells $cellpath]
 if { [get_property CONFIG.C_ATG_MODE  $cell_handle] == "AXI4" } { 
    #AXI_MASTER interface updates
    set maxi_intf_handle [get_bd_intf_pins $cellpath/M_AXI]
    #id width
    set m_axi_id_width [get_property CONFIG.C_M_AXI_THREAD_ID_WIDTH $cell_handle]
    set_property CONFIG.ID_WIDTH $m_axi_id_width $maxi_intf_handle
    #data width
    set m_axi_data_width [get_property CONFIG.C_M_AXI_DATA_WIDTH $cell_handle]
    set_property CONFIG.DATA_WIDTH $m_axi_data_width $maxi_intf_handle
    #awuser width
    set m_axi_awuser_width [get_property CONFIG.C_M_AXI_AWUSER_WIDTH $cell_handle]
    set_property CONFIG.AWUSER_WIDTH $m_axi_awuser_width $maxi_intf_handle
    #aruser width
    set m_axi_aruser_width [get_property CONFIG.C_M_AXI_ARUSER_WIDTH $cell_handle]
    set_property CONFIG.ARUSER_WIDTH $m_axi_aruser_width $maxi_intf_handle
 }
 if { [get_property CONFIG.C_ATG_MODE  $cell_handle] == "AXI4-Stream" && [get_property CONFIG.C_AXIS_MODE $cell_handle] == "Slave Loop back" } { 
  set cell [get_bd_cells $cellpath]
  set paramList2 { C_AXIS_TDATA_WIDTH C_AXIS_TID_WIDTH C_AXIS_TUSER_WIDTH  C_AXIS_TDEST_WIDTH }
  bd::mark_propagate_overrideable $cell $paramList2
 } else {
  set cell [get_bd_cells $cellpath]
  set paramList3 { C_AXIS_TDATA_WIDTH C_AXIS_TID_WIDTH C_AXIS_TUSER_WIDTH  C_AXIS_TDEST_WIDTH }
  bd::mark_user $cell $paramList3
 }
 if { [get_property CONFIG.C_ATG_MODE  $cell_handle] == "AXI4-Stream" && [get_property CONFIG.C_AXIS_MODE $cell_handle] == "Master Only" } {
  set cell [get_bd_cells $cellpath]
  set paramList4 { C_AXIS1_HAS_TKEEP C_AXIS1_HAS_TSTRB }
  bd::mark_user $cell $paramList4
 } else {
  set cell [get_bd_cells $cellpath]
  set paramList5 { C_AXIS1_HAS_TKEEP C_AXIS1_HAS_TSTRB }
  bd::mark_propagate_overrideable $cell $paramList5
 }
}
proc propagate {cellpath otherInfo } {
  #if { [get_property CONFIG.C_ATG_MODE  $cell_handle] == "AXI4-Stream" && [get_property CONFIG.C_AXIS_MODE $cell_handle] == "Slave Loop back" } { 
  # set cell [get_bd_cells $cellpath]
  # set paramList { C_AXIS_TID_WIDTH C_AXIS_TUSER_WIDTH  C_AXIS_TDEST_WIDTH }
  # bd::mark_propagate_only $cell $paramList
  #} else {
  # set cell [get_bd_cells $cellpath]
  # set paramList { C_AXIS_TID_WIDTH C_AXIS_TUSER_WIDTH  C_AXIS_TDEST_WIDTH }
  # bd::mark_user $cell $paramList
  #}
  #update ATG_CLK_FREQ_MHZ parameter.
  set cell_handle [get_bd_cells $cellpath]
  set clk_in_hz [get_property CONFIG.FREQ_HZ [get_bd_pins $cell_handle/s_axi_aclk]]
  set clk_in_mhz [expr {$clk_in_hz / 1000000.0}]
  set_property CONFIG.ATG_CLK_FREQ_MHZ [format "%3.3f" $clk_in_mhz] $cell_handle

 if { [get_property CONFIG.C_ATG_MODE  $cell_handle] == "AXI4-Stream" && [get_property CONFIG.C_AXIS_MODE $cell_handle] == "Master Loop back" } {
    set axis_intf_handle [get_bd_intf_pins $cellpath/S_AXIS_MASTER]
    set has_tkeep [get_property CONFIG.HAS_TKEEP $axis_intf_handle]
    set has_tstrb [get_property CONFIG.HAS_TSTRB $axis_intf_handle]
    set_property CONFIG.C_AXIS1_HAS_TSTRB $has_tstrb $cell_handle 
    set_property CONFIG.C_AXIS1_HAS_TKEEP $has_tkeep $cell_handle
 } 
 if { [get_property CONFIG.C_ATG_MODE  $cell_handle] == "AXI4-Stream" && [get_property CONFIG.C_AXIS_MODE $cell_handle] == "Slave Loop back" } { 
    set axis_intf_handle [get_bd_intf_pins $cellpath/S_AXIS_SLAVE]

    set axis_tid_width [get_property CONFIG.TID_WIDTH $axis_intf_handle]
    set_property CONFIG.C_AXIS_TID_WIDTH $axis_tid_width $cell_handle

    set axis_tuser_width [get_property CONFIG.TUSER_WIDTH $axis_intf_handle]
    set_property CONFIG.C_AXIS_TUSER_WIDTH $axis_tuser_width $cell_handle

    set axis_tdest_width [get_property CONFIG.TDEST_WIDTH $axis_intf_handle]
    set_property CONFIG.C_AXIS_TDEST_WIDTH $axis_tdest_width $cell_handle

    set axis_tdata_bytes [get_property CONFIG.TDATA_NUM_BYTES $axis_intf_handle]
    set axis_tdata_width [expr int($axis_tdata_bytes * 8)]
    set_property CONFIG.C_AXIS_TDATA_WIDTH $axis_tdata_width $cell_handle

    set has_tkeep [get_property CONFIG.HAS_TKEEP $axis_intf_handle]
    set has_tstrb [get_property CONFIG.HAS_TSTRB $axis_intf_handle]
    set_property CONFIG.C_AXIS2_HAS_TSTRB $has_tstrb $cell_handle 
    set_property CONFIG.C_AXIS2_HAS_TKEEP $has_tkeep $cell_handle
 } 
 if { [get_property CONFIG.C_ATG_MODE  $cell_handle] != "AXI4-Lite" && [get_property CONFIG.ATG_OPTIONS $cell_handle] == "Custom" } { 

    #S_AXI interface updates
    set saxi_intf_handle [get_bd_intf_pins $cellpath/S_AXI]
    #id width
    set s_axi_id_width [get_property CONFIG.ID_WIDTH $saxi_intf_handle]
    set_property CONFIG.C_S_AXI_ID_WIDTH $s_axi_id_width $cell_handle
    #data width
    set s_axi_data_width [get_property CONFIG.DATA_WIDTH $saxi_intf_handle]
    set_property CONFIG.C_S_AXI_DATA_WIDTH $s_axi_data_width $cell_handle
    #awuser width
    set s_axi_awuser_width [get_property CONFIG.AWUSER_WIDTH $saxi_intf_handle]
    set_property CONFIG.C_S_AXI_AWUSER_WIDTH $s_axi_awuser_width $cell_handle
    #aruser width
    set s_axi_aruser_width [get_property CONFIG.ARUSER_WIDTH $saxi_intf_handle]
    set_property CONFIG.C_S_AXI_ARUSER_WIDTH $s_axi_aruser_width $cell_handle
 }

}
proc post_propagate {cellpath otherinfo} {
  #perform check only for cases where S_AXI interface is available
  #ie. C_ATG_MODE != AXI4-Lite and ATG_OPTIONS == Custom
  set cell_handle [get_bd_cells $cellpath]
  if { [get_property CONFIG.C_ATG_MODE  $cell_handle] != "AXI4-Lite" && [get_property CONFIG.ATG_OPTIONS $cell_handle] == "Custom" } { 
    set ip [get_bd_cells $cellpath]
    set_base_high $cellpath
    set intfpin $cellpath/[format S_AXI 0]
    set sinf  [get_bd_addr_segs -addressable -of_objects [get_bd_intf_pins $intfpin]] 
    set addr_seg [get_bd_addr_seg -of_object $sinf]
    set def "0x00010000"
    foreach rnge [get_property range $addr_seg] {
    if { $rnge < $def} {
       bd::send_msg -of $cellpath -type ERROR -msg_id 1 -text " 
       #####################################################
       Minimum required address range is 64K for S_AXI interface.
       ##################################################### "
    }
   }
  }
}
#To update core paramters after the complete propagation.
proc set_base_high {cellpath} {

   #Get the IP
   set ip [get_bd_cells $cellpath]

#until the ipi code stops performing the setting of the
#base and high variables internally in c++, set this
#environment variable to turn it off before setting them from
#tcl
   set  ::env(IPI_SET_BASE_HIGH) FALSE


#place the default values you would like the 
#the unmapped segments to have into a dictionary.
#Note the key is by the name of the segment,
#not the full name and not the name of the
# base/high parameter

set dDefValues [dict create \
Reg0 [list 0x00000000 0x0000FFFF]]


   #Get the Slave bif. Use the commented version if your  slave bif uses
   #a non standard naming convention or there is more than one slave bif
   #with slave segments. 
   #set vSlvBifs [get_bd_intf_pins -of_object $ip -quiet -filter {Mode == "Slave"}]
   set vSlvBifs [get_bd_intf_pins "$ip/S_AXI"]
   set nSlvBifs [llength $vSlvBifs]

   #Get the slave segments related to the bifs
   set vSlvSegs [get_bd_addr_segs -of_objects $vSlvBifs]
   set nSlvSegs [llength $vSlvSegs]

   foreach slvSeg $vSlvSegs {
        #Get the name of the slave seg.
        set sSlvSeg [get_property name $slvSeg]

#Get the default values for this segment from the dictionary.
        #The convention of setting the base/high to 0xFFFFFFFFF and 
        #0x00000000 is an old EDK convention included here for safety.
        #If all the default values are specified correctly in the dictionary 
        #you can replace the tenary version below with just getting the 
        #values from the dictionary.

        #$set vDefValues [dict get $dDefValues $sSlvSeg]
        set vDefValues [expr [dict exists $dDefValues $sSlvSeg] ? {[dict get $dDefValues $sSlvSeg]} : {[list 0xFFFFFFF 0x00000000]}]

        #Use the ::bd:;addr tcl function to transfer the values from the mapped 
        #segment to the slave segment's base/high parameters
       ::bd::addr::cfg_base_high_of_slv $ip $slvSeg [lindex $vDefValues 0] [lindex $vDefValues 1] 
   }
}
