proc init { cell_name undefined_params } {
  bd::mark_propagate_only [get_bd_cells $cell_name] {S_AXI_ID_WIDTH}
}

proc propagate { cell_name  undefined_params } { 

  set ip [get_bd_cells $cell_name]
  set si [get_bd_intf_pins $cell_name/S_AXI]

    set p {ID_WIDTH} 
    set si_v [get_property CONFIG.ID_WIDTH $si]
    set ip_v [get_property CONFIG.S_AXI_ID_WIDTH $ip]

    if {$si_v > 0} {
      set_property CONFIG.S_AXI_ID_WIDTH $si_v $ip
    } else {
      bd::send_msg -of $ip -type INFO -msg_id 3 -text " Preserving $p <$ip_v> "
    }

  set value [get_property CONFIG.SUPPORTS_NARROW_BURST $si]
  if { $value == 0 } {
    set_property CONFIG.S_AXI_SUPPORTS_NARROW_BURST false $ip
  } else {
    set_property CONFIG.S_AXI_SUPPORTS_NARROW_BURST true $ip
  }
}
proc set_base_high {ip} {
  set ::env(IPI_SET_BASE_HIGH) FALSE
  set cfg_type [get_property CONFIG.INCLUDE_RC $ip]
  # base/high parameter
  set dDefValues [dict create \
  BAR0 [list 0x20000000 0x2FFFFFFF] \
  BAR1 [list 0x30000000 0x3FFFFFFF] \
  BAR2 [list 0x40000000 0x4FFFFFFF] \
  BAR3 [list 0x50000000 0x5FFFFFFF] \
  BAR4 [list 0x60000000 0x6FFFFFFF] \
  BAR5 [list 0x70000000 0x7FFFFFFF]]
  if {$cfg_type == "PCI_Express_Endpoint_device"} {
    dict append $dDefValues CTL0 [list 0x00001000 0x00001FFF]
  } else {
    dict append $dDefValues CTL0 [list 0x00000000 0x001FFFFF]
  }
  # Get slave interfaces and slave segments
 
  set vSB [get_bd_intf_pins -of_object $ip -quiet -filter {Mode == "Slave" && Name =~ "S_AXI*"}]
  set nSB [llength $vSB]
  #Get the slave segments related to the bifs
  set vSS [get_bd_addr_segs -of_objects $vSB]
  set vMS [get_bd_addr_segs -of_objects $vSS]
  set nSS [llength $vSS]
  set dSS_vMS [dict create]

  #puts "!!! vSS $vSS"

  foreach SS $vSS {
     set vMS [get_bd_addr_segs -of_objects $SS]
     #puts "!!! $SS : $vMS"
     dict set dSS_vMS $SS $vMS  
  }


  foreach SS $vSS {
        #Get the name of the slave seg.
        set sSS    [get_property name $SS]
        set vDefValues [expr [dict exists $dDefValues $sSS] ?   \
		             {[dict get $dDefValues   $sSS]}  : \
			     {[list 0xFFFFFFF 0x00000000]}]
	#puts "!!! vDefValues $vDefValues"
        cfx_base_high_of_slv $ip $SS $dSS_vMS [lindex $vDefValues 0] [lindex $vDefValues 1] 
   }
}

proc post_propagate { cell_name undefined_params } {
  set ip [get_bd_cells $cell_name]
  set clks [get_property CONFIG.axi_aclk_loopback $ip]	  
  set F true	  
  if {$clks == $F} {
  set aclk_out [get_bd_pins $cell_name/axi_aclk_out]
  set aclk     [get_bd_pins $cell_name/axi_aclk]
  set aclk_ctl_out [get_bd_pins $cell_name/axi_ctl_aclk_out]
  set aclk_ctl     [get_bd_pins $cell_name/axi_ctl_aclk]
  set aclk_out_domain [get_property CONFIG.CLK_DOMAIN $aclk_out]
  set aclk_domain     [get_property CONFIG.CLK_DOMAIN $aclk]
  set aclk_out_freq [get_property CONFIG.FREQ_HZ $aclk_out]
  set aclk_freq     [get_property CONFIG.FREQ_HZ $aclk]
  set aclk_out_phase [get_property CONFIG.PHASE $aclk_out]
  set aclk_phase     [get_property CONFIG.PHASE $aclk]
  if { $aclk_out_domain == $aclk_domain && $aclk_out_freq == $aclk_freq && $aclk_out_phase == $aclk_phase} {
    bd::send_msg -of $ip -type INFO -msg_id 1 -text " axi_aclk is connected to axi_aclk_out"
  } else {
    bd::send_msg -of $ip -type ERROR  -msg_id 1 -text " axi_aclk must be connected to axi_aclk_out"
  }
  set aclk_out_domain [get_property CONFIG.CLK_DOMAIN $aclk_ctl_out]
  set aclk_domain     [get_property CONFIG.CLK_DOMAIN $aclk_ctl]
  set aclk_out_freq [get_property CONFIG.FREQ_HZ $aclk_ctl_out]
  set aclk_freq     [get_property CONFIG.FREQ_HZ $aclk_ctl]
  set aclk_out_phase [get_property CONFIG.PHASE $aclk_ctl_out]
  set aclk_phase     [get_property CONFIG.PHASE $aclk_ctl]
  if { $aclk_out_domain == $aclk_domain && $aclk_out_freq == $aclk_freq && $aclk_out_phase == $aclk_phase} {
    bd::send_msg -of $ip -type INFO  -msg_id 2 -text " axi_ctl_aclk is connected to axi_ctl_aclk_out"
  } else {
    bd::send_msg -of $ip -type ERROR -msg_id 2 -text " axi_ctl_aclk must be connected to axi_ctl_aclk_out"
  }
  } else {
    bd::send_msg -of $ip -type INFO  -msg_id 1 -text " axi_aclk, axi_ctl_aclk are connected internally"
  }	  
  set_base_high $ip
}

proc post_config_ip {cell_name otherInfo} {
  set ip [get_bd_cells $cell_name]
  set clks [get_property CONFIG.axi_aclk_loopback $ip]	  
  set F true	  
  set clk_domain [get_property CONFIG.CLK_DOMAIN [get_bd_pins $cell_name/axi_aclk_out]]
  set_property CONFIG.CLK_DOMAIN $clk_domain [get_bd_pins $cell_name/axi_ctl_aclk_out]
  set lane [get_property CONFIG.NO_OF_LANES $ip]
  set speed [get_property CONFIG.MAX_LINK_SPEED $ip]
  set width [get_property CONFIG.S_AXI_DATA_WIDTH $ip]
  if {$lane == "X1" && $speed == "2.5_GT/s" && $width == "64"} {
   if {$clks == $F} {
   set_property CONFIG.FREQ_HZ 62500000 [get_bd_pins $cell_name/axi_aclk_out]
   set_property CONFIG.FREQ_HZ 62500000 [get_bd_pins $cell_name/axi_ctl_aclk_out]
   set_property CONFIG.FREQ_HZ 62500000 [get_bd_pins $cell_name/axi_aclk]
   set_property CONFIG.FREQ_HZ 62500000 [get_bd_pins $cell_name/axi_ctl_aclk]
   } else {
   set_property CONFIG.FREQ_HZ 62500000 [get_bd_pins $cell_name/axi_aclk_out]
   set_property CONFIG.FREQ_HZ 62500000 [get_bd_pins $cell_name/axi_ctl_aclk_out]
  } 
 } else {
   set_property CONFIG.FREQ_HZ 125000000 [get_bd_pins $cell_name/axi_aclk_out]
   set_property CONFIG.FREQ_HZ 125000000 [get_bd_pins $cell_name/axi_ctl_aclk_out]
  }

}


########################################################################
######### procedure to set the base/high parameters      ###############
######### related to a particular slave segment on an ip ###############
########################################################################
proc cfx_base_high_of_slv {ip slvSeg dSS_vMS nDefBase nDefHigh {bNormalizeGT32 True}} {

    #puts "!!!"
    #puts "!!!---------------------------------------------------" 
    #puts "!!!       cfg_base_high_of_slv $slvSeg"
    #puts "!!!---------------------------------------------------" 
	
    #::bd::util_cmd print_address_relationships  

    set vBaseHighNms [::bd::addr::get_base_high_names_of $slvSeg]

    if {2 != [llength $vBaseHighNms]} {
    	#puts "!!! $slvSeg could not find BaseHigh Names"
	::bd::send_msg -of $ip \
	    -type error \
	    -msg_id 1 \
	    -text "Could not find base high parameter names for $ip.$slvSeg"      
    	return
    }	
	
    #------------------------------------
    set CR877788 True
    #------------------------------------
    #With these old style  ip, the decoding is being done upstream at the interconnect 
    #or the smartconnect. There is no need for the ip to be decoding the full address. 
    #just needs to decode the mapped range. So set base to 0 and high to the range
    #------------------------------------
    
    set vMstSegs {}
    if {[dict exists $dSS_vMS $slvSeg]} {
      set vMstSegs [dict get $dSS_vMS $slvSeg]
    }

    #set vMstSegs [get_bd_addr_segs -of_objects $slvSeg]
    set nMstSegs [llength $vMstSegs]
    #puts "!!! vMstSegs $vMstSegs"
	    
    set vOfsRngs [::bd::addr::consolidate_mapped_segs $vMstSegs] 
    set nOfsRngs [llength $vOfsRngs]
    #puts "!!! vOfsRngs $vOfsRngs"
	
    set sBaseName [lindex $vBaseHighNms 0] 	
    set sHighName [lindex $vBaseHighNms 1] 	
	
    set vParams {}	
	
    if {0 == $nOfsRngs} {
	#Unmapped. Reset to default values
	#puts "!!! $slvSeg is unmapped"
        #puts "!!! default $sBaseName == $nDefBase"
        #puts "!!! default $sHighName == $nDefHigh"
	    
        lappend vParams CONFIG.$sBaseName $nDefBase
      	lappend vParams CONFIG.$sHighName $nDefHigh
        #puts "!!! 1 SETTING $vParams"
        set_property -quiet -dict $vParams $ip
	    
    } elseif {2 < $nOfsRngs} {
	#slave mapped to more than one address that cannot be combined
	#use index 0 and throw critical warning	
	    
	set nOfs [lindex $vOfsRngs 0]
	set nRng [lindex $vOfsRngs 1]
		
	::bd::send_msg -of $ip \
		  -type critical_warning \
	          -msg_id 1 \
		  -text "$slvSeg is mapped to multiple disjoint addresses $vOfsRngs. Using $nOfs $nRng"

        set dec_valu [::tcl::mathfunc::double $nOfs]
        set num_bits  [expr {$dec_valu ? [::tcl::mathfunc::int [::tcl::mathfunc::ceil [expr [::tcl::mathfunc::log $dec_valu] / [::tcl::mathfunc::log 2]]]] : 32}]
       set sFormat [expr {($num_bits > 32) ? {0x%016llX} : {0x%08X}}]

        #puts "!!! dec_valu $dec_valu"
        #puts "!!! num_bits $num_bits"
        #puts "!!! sFormat  $sFormat"

        if {$CR877788 &&  ($num_bits > 32)} {
	   if {$bNormalizeGT32} {
   	      set nBase [format {0x%08X} 0]
	      set nHigh [format {0x%08X} [expr {$nRng - 1}]]
   	   } else {
   	      set nBase [format $sFormat $nOfs]
	      set nHigh [format $sFormat [expr {$nOfs + $nRng - 1}]]
           }
        } else {
   	   set nBase [format {0x%08X} $nOfs]
	   set nHigh [format {0x%08X} [expr {$nOfs + $nRng - 1}]]
	}

	#puts "!!! $sBaseName == $nBase"
	#puts "!!! $sHighName == $nHigh"
		
	lappend vParams CONFIG.$sBaseName $nBase
	lappend vParams CONFIG.$sHighName $nHigh
        #puts "!!! 2 SETTING $vParams"
	set_property -quiet -dict $vParams $ip
    } else {
	#Normal slave mapped to a single address or all addresses are
	#subsets of each other		
	#
	set nOfs [lindex $vOfsRngs 0]
	set nRng [lindex $vOfsRngs 1]

        #puts "!!! OfsRng [format {0x%016llX} $nOfs] [format {0x%016llX} $nRng]"

        set  dec_valu [::tcl::mathfunc::double $nOfs]
        #puts "!!! dec_valu $dec_valu"
        set num_bits  [expr {$dec_valu ? [::tcl::mathfunc::int [::tcl::mathfunc::ceil [expr [::tcl::mathfunc::log $dec_valu] / [::tcl::mathfunc::log 2]]]] : 32}]
        #puts "!!! num_bits $num_bits"
        #
       set sFormat [expr {($num_bits > 32) ? {0x%016llX} : {0x%08X}}]

        if {$CR877788 && ($num_bits > 32)} {
	   if {$bNormalizeGT32} {
   	      set nBase [format {0x%08X} 0]
	      set nHigh [format {0x%08X} [expr {$nRng - 1}]]
   	   } else {
   	      set nBase [format $sFormat $nOfs]
	      set nHigh [format $sFormat [expr {$nOfs + $nRng - 1}]]
           }

        } else {
   	   set nBase [format {0x%08X} $nOfs]
	   set nHigh [format {0x%08X} [expr {$nOfs + $nRng - 1}]]
	}

	#puts "!!! CONFIG.$sBaseName == $nBase"
	#puts "!!! CONFIG.$sHighName == $nHigh"
		
	lappend vParams CONFIG.$sBaseName $nBase
	lappend vParams CONFIG.$sHighName $nHigh
        #puts "!!! 3 SETTING $vParams"
	set_property -quiet -dict $vParams $ip
     }
     #puts "!!!"
}



