## File: bd/bd.tcl
proc init { cellpath otherInfo } {
set ip [get_bd_cells $cellpath]

set cell_handle [get_bd_cells $cellpath]
#mark some parameters as propagate only
set paramList "C_S_AXI_CLK_PERIOD_PS C_PRH0_BASEADDR C_PRH0_HIGHADDR C_PRH1_BASEADDR C_PRH1_HIGHADDR C_PRH2_BASEADDR C_PRH2_HIGHADDR C_PRH3_BASEADDR C_PRH3_HIGHADDR" 
bd::mark_propagate_only $cell_handle $paramList
}
# This is needed only for slaves where the internal size of the slave
# can change based on BASE/HIGH addr parameters (e.g. bram controllers)
proc configure_ip { cellpath otherInfo } {
     
#  set clk_pin_handle_prh [get_bd_pins $cellpath/prh_aclk]
#    set freq_Hz [get_property CONFIG.FREQ_HZ [ get_bd_ports $clk_pin_handle]]
#    set time_period_ps [expr {1000000000/$freq_Hz}];
#    set_property CONFIG.C_S_AXI_CLK_PERIOD_PS $time_period_ps $cell 
   




# disable Rng1 based on user parameter
#set_property numRanges [get_property $cell CONFIG.C_NUM_PERIPHERALS]
#if {[expr $numRanges] < 2} {
#    # disable addrSeg1 settings its range to 0
#    set addrSeg1 [get_bd_addr_seg ${cellpath}/S_AXI_MEM/PRH1]
#    #set_property range {0} $addrSeg1
#    set_property enablement false $addrSeg1
#    }
#if {[expr $numRanges] < 3} {
#    # disable addrSeg2 settings its range to 0
#    set addrSeg2 [get_bd_addr_seg ${cellpath}/S_AXI_MEM/PRH2]
#    #set_property range {0} $addrSeg2
#    set_property enablement false $addrSeg2
#    }
#if {[expr $numRanges] < 4} {
#    # disable addrSeg3 settings its range to 0
#    set addrSeg3 [get_bd_addr_seg ${cellpath}/S_AXI_MEM/PRH3]
#    #set_property range {0} $addrSeg3
#    set_property enablement false $addrSeg3
#    }
}

proc propagate {cellpath otherInfo } {
    set ip [get_bd_cells $cellpath]
        ## Assign AXI clock frequency to parameter 
    set clk_pin_handle [get_bd_pins $cellpath/s_axi_aclk]

    set freq_Hz [get_property CONFIG.FREQ_HZ $clk_pin_handle]
    set time_period_ps [expr {1000000000000/$freq_Hz}]
    set_property CONFIG.C_S_AXI_CLK_PERIOD_PS $time_period_ps $ip 

#    set prh_clk_pin_handle [get_bd_pins $cellpath/prh_clk]

#    set prh_freq_Hz [get_property CONFIG.FREQ_HZ $prh_clk_pin_handle]
#    set prh_clk_time_period_ps [expr {1000000000000/$prh_freq_Hz}]
#    set_property CONFIG.C_PRH_CLK_PERIOD_PS $prh_clk_time_period_ps $ip 

}

proc post_propagate {cellpath otherInfo} {
   set_base_high $cellpath
    #    set ip [get_bd_cells $cellpath]
#    set prh_clock_used [get_property CONFIG.C_PRH_CLK_SUPPORT $ip]
#
#    set axi_aclk [get_property CONFIG.FREQ_HZ [get_bd_pins $ip/s_axi_aclk]] 
#    set axi_aclk_domain [get_property CONFIG.CLK_DOMAIN [get_bd_pins $ip/s_axi_aclk]]
#    set axi_aclk_phase [get_property CONFIG.PHASE [get_bd_pins $ip/s_axi_aclk]] 
#
#    set prh_clk [get_property CONFIG.FREQ_HZ [get_bd_pins $ip/prh_clk]] 
#    set prh_clk_domain [get_property CONFIG.CLK_DOMAIN [get_bd_pins $ip/prh_clk]]
#    set prh_clk_phase [get_property CONFIG.PHASE [get_bd_pins $ip/prh_clk]] 
#
#    if {($axi_aclk != 0 ) && ($prh_clk != 0) && ($axi_aclk == $prh_clk) &&
#         ($axi_aclk_phase == $prh_clk_phase) && ($axi_aclk_domain == $prh_clk_domain) } {
#            set paramList2 "C_PRH0_SYNC C_PRH1_SYNC C_PRH2_SYNC C_PRH3_SYNC"
#            bd::mark_propagate_overrideable $ip $paramList2
#            set_property CONFIG.C_PRH_CLK_SUPPORT 0 $ip
#            bd::send_msg -of $cellpath -type INFO -msg_id 11 -text " 
#             ##########################################################################################
#             AXI EPC is using s_axi_aclk for peripherals....
#             ##########################################################################################"
#        set clk_control_value 1
#        set_property CONFIG.C_PRH0_SYNC $clk_control_value $ip
#        set_property CONFIG.C_PRH1_SYNC $clk_control_value $ip
#        set_property CONFIG.C_PRH2_SYNC $clk_control_value $ip
#        set_property CONFIG.C_PRH3_SYNC $clk_control_value $ip
#        } else { 
#          if { ($axi_aclk >= $prh_clk) } {
#           set_property CONFIG.C_PRH_CLK_SUPPORT 1 $ip
#           set paramList2 "C_PRH0_SYNC C_PRH1_SYNC C_PRH2_SYNC C_PRH3_SYNC"
#           bd::mark_propagate_only $ip $paramList2
#           set clk_control_value 0
#           set_property CONFIG.C_PRH0_SYNC $clk_control_value $ip
#           set_property CONFIG.C_PRH1_SYNC $clk_control_value $ip
#           set_property CONFIG.C_PRH2_SYNC $clk_control_value $ip
#           set_property CONFIG.C_PRH3_SYNC $clk_control_value $ip
#            bd::send_msg -of $cellpath -type INFO -msg_id 11 -text " 
#             ##########################################################################################
#             Clocks connected to AXI EPC are not same...  prh_clk support is enabled
#             ##########################################################################################"
#          } else {
#              bd::send_msg -of $cellpath -type ERROR -msg_id 1 -text " 
#               ##########################################################################################
#               Clocks have different frequencies. Ensure 's_axi_aclk' is more than 'prh_clk' 
#               ##########################################################################################"
#
#             }
#
#        }

}

proc set_base_high {cellpath} {

set ip [get_bd_cells $cellpath]

#Environment variable to turn off setting of base and high variables internally in c++ before setting them from tcl
set  ::env(IPI_SET_BASE_HIGH) FALSE
#puts "Making the IPI_SET_BASE_HIGH env variable as FALSE"

#Elacing the default values for 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 \
PRH0 [list 0xA0000000 0xAFFFFFFF] \
PRH1 [list 0xB0000000 0xBFFFFFFF] \
PRH2 [list 0xC0000000 0xCFFFFFFF] \
PRH3 [list 0xD0000000 0xDFFFFFFF]]

#puts "Setting the default values "


#Getting the Slave bus interface. 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_MEM"]
   set slave_bif [get_bd_intf_pins "$ip/S_AXI"]
   set sl_bif_len [llength $slave_bif]
#puts "Slave BUSIF name is $slave_bif and Length $sl_bif_len "

   #Get the slave segments related to the bifs
   set slave_SEGs [get_bd_addr_segs -of_objects $slave_bif]
   set num_slv_SEGs [llength $slave_SEGs]
#puts "Slave Segment names are $slave_SEGs and Length $num_slv_SEGs "

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

        #Getting 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.

        #$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] 
   }
}
