s exceeds the address width ($aw) of its associated AXI master interface. Please re-assign the offset address of master address space segment $as to be within the valid address width limit ($aw) of master address space $mas."      
    } else {
      lappend new_as $as
    }
  }
  ifx_debug_proc_footer
  return $new_as
}

proc segments_to_segment_tuples { segments } {
  ifx_debug_proc_header
  set l {}
  foreach as $segments {
    set r [get_property RANGE $as]
    set ba [get_property OFFSET $as]
    ifx_puts "$as range: $r offset: $ba"
    set ha [expr $ba + ($r-1)]
    lappend l [list [expr $ba] [expr $ha] $as]
  }
  ifx_debug_proc_footer
  return $l
}

proc group_by_largest_subset_overlap { segment_tuples } {
  set sorted_segment_tuples [lsort -real -increasing -index 0 $segment_tuples]
  set prev_ha -1
  set prev_ba 0
  set prev_as ""
  set segment_groups {}
  foreach tuple $sorted_segment_tuples {
    lassign $tuple curr_ba curr_ha curr_as
    if {($curr_ba > $prev_ha)} {
      dict set segment_groups $curr_as ""
      set prev_ba $curr_ba
      set prev_ha $curr_ha
      set prev_as $curr_as
      set prev_segs ""
    } else {
      if {[dict exists $segment_groups $prev_as]} {
        set val  [dict get $segment_groups $prev_as]
        if {[llength $val] > 0} {
          set prev_segs $val
        }
      }
      if {$curr_ha <= $prev_ha} {
        lappend prev_segs $curr_as
        dict set segment_groups $prev_as $prev_segs
      } else {
        dict unset segment_groups $prev_as
        lappend prev_segs $prev_as
        dict set segment_groups   $curr_as $prev_segs
        set prev_as $curr_as
        set prev_ha $curr_ha
      }
    }
  }
  return $segment_groups
}

proc trim_si_acceptance_from_reachable_mi { cell } {
  ifx_debug_proc_header

  set ip [get_bd_cells $cell]
  set num_si [get_property CONFIG.NUM_SI $ip]
  set num_mi [get_property CONFIG.NUM_MI $ip]

  foreach m {READ WRITE} {
    for {set si 0} {$si < $num_si} {incr si} {
      ifx_puts "Optimizing SI$si.${m}_ACCEPTANCE"
      set si_avail_issuing 0

      set rwm [get_property CONFIG.READ_WRITE_MODE [get_bd_intf_pins $cell/S[format %.2d $si]_AXI]]
      if {![string match *$m* $rwm]} {
        ifx_puts "SI:$si is $rwm so no post-optimization required"
        continue
      }

      for {set mi 0} {$mi < $num_mi} {incr mi} {
        set rwm [get_property CONFIG.READ_WRITE_MODE [get_bd_intf_pins $cell/M[format %.2d $mi]_AXI]]
        if {![string match *$m* $rwm]} {
          ifx_puts "MI:$mi is $rwm so excluding its $m issuance level for SI trimming optimization"
          continue
        }

        if {[get_property CONFIG.M[format %.2d $mi]_S[format %.2d $si]_${m}_CONNECTIVITY $ip] == 1} {
          set mi_issuing [get_property CONFIG.M[format %.2d $mi]_${m}_ISSUING $ip]
          ifx_puts "found M$mi issuing $mi_issuing"
          incr si_avail_issuing $mi_issuing
        }
      }
      set si_accept [get_property CONFIG.S[format %.2d $si]_${m}_ACCEPTANCE $ip]
      if {$si_accept > $si_avail_issuing} {
        ifx_puts "SI Acceptance ($si_accept) exceeds the combined issuing capability of the connected MI...  Reducing to combined MI issuing ($si_avail_issuing)"
        set_property CONFIG.S[format %.2d $si]_${m}_ACCEPTANCE $si_avail_issuing $ip
        set_property CONFIG.NUM_${m}_OUTSTANDING $si_avail_issuing [get_bd_intf_pins $cell/S[format %.2d $si]_AXI]
      }
    }
  }
  ifx_debug_proc_footer

}

proc lock_rwm_by_connectivity { cell } {
  set ip [get_bd_cells $cell]

  
  # Inhibit inference on slave interface read/write mode to match endpoint on vectored ports
  for {set si 0} {$si < [get_property CONFIG.NUM_SI $ip]} {incr si} {
    set r 0
    set w 0

    for {set mi 0} {$mi < [get_property CONFIG.NUM_MI $ip]} {incr mi} {
      set r [expr $r | [get_property CONFIG.M[format %.2d $mi]_S[format %.2d $si]_READ_CONNECTIVITY $ip]]
      set w [expr $w | [get_property CONFIG.M[format %.2d $mi]_S[format %.2d $si]_WRITE_CONNECTIVITY $ip]]
    }
    if {($r == 0 || $w == 0) && ($r+$w > 0)} {
      if {$r==0} { set rwm WRITE_ONLY  }
      if {$w==0} { set rwm READ_ONLY }
      set intf [get_bd_intf_pins  $ip/S[format %.2d $si]_AXI]
      ifx_puts "SI: $si r: $r w: $w - forcing intf: $intf READ_WRITE_MODE to $rwm at CONSTANT"
      set_property CONFIG.READ_WRITE_MODE $rwm $intf
      set_property CONFIG.READ_WRITE_MODE.VALUE_SRC CONSTANT $intf
    } elseif {$r+$w == 2} {
      ifx_puts "SI: $si endpoint mode is READ_WRITE - no need to modify interface property"
    } 
  }

  # Inhibit inference on master interface read/write mode to match endpoint on vectored ports
  for {set mi 0} {$mi < [get_property CONFIG.NUM_MI $ip]} {incr mi} {
    set r 0
    set w 0

    for {set si 0} {$si < [get_property CONFIG.NUM_SI $ip]} {incr si} {
      set r [expr $r | [get_property CONFIG.M[format %.2d $mi]_S[format %.2d $si]_READ_CONNECTIVITY $ip]]
      set w [expr $w | [get_property CONFIG.M[format %.2d $mi]_S[format %.2d $si]_WRITE_CONNECTIVITY $ip]]
    }
    if {($r == 0 || $w == 0) && ($r+$w > 0)} {
      if {$r==0} { set rwm WRITE_ONLY  }
      if {$w==0} { set rwm READ_ONLY }
      set intf [get_bd_intf_pins  $ip/M[format %.2d $mi]_AXI]
      ifx_puts "MI: $mi r: $r w: $w - forcing intf: $intf READ_WRITE_MODE to $rwm at CONSTANT"
      set_property CONFIG.READ_WRITE_MODE $rwm $intf
      set_property CONFIG.READ_WRITE_MODE.VALUE_SRC CONSTANT $intf
    } elseif {$r+$w == 2} {
      ifx_puts "MI: $mi endpoint mode is READ_WRITE - no need to modify interface property"
    }
  }
}

proc print_map { s } {
  ifx_debug_proc_header
  foreach k [dict keys $s] {
    ifx_puts "$k"
    foreach l [dict get $s $k] {
      ifx_puts "...> <$l>"
    }
  }
  ifx_debug_proc_footer
}

proc post_propagate {cell_name args} {
  ifx_debug_proc_header
  set ip  [get_bd_cells $cell_name]

  set num_mi [get_property CONFIG.NUM_MI $ip]
  set num_as [get_property CONFIG.ADDR_RANGES $ip]

  if {[get_param bd.addr_relations_use_matrices] == 0} {
    ifx_puts "----------------------------------------------------------------------"
    ifx_puts "----------------------------------------------------------------------"
    ifx_puts "NEW ADDRESSING:"
    set assignments {}
    set max_as 0
    for {set i 0} {$i < $num_mi} {incr i} {
      set mi [get_bd_intf_pins $ip/M[format %.2d $i]_AXI]
      set addresses [::bd::addr::get_addresses_of $mi]
      set as 0
      if {[llength $addresses] > 0} {
        foreach tuple $addresses {
          lassign $tuple base range
          set aw  [expr round(log($range)/log(2))]
          ifx_puts "MI: $i AS: $as base: $base range: $range"
	  if {[get_property CONFIG.M[format %.2d $i]_A[format %.2d $as]_BASE_ADDR.VALUE_SRC $ip] != "USER"} {
            lappend assignments CONFIG.M[format %.2d $i]_A[format %.2d $as]_BASE_ADDR $base
	  } else {
	    ifx_puts "CONFIG.M[format %.2d $i]_A[format %.2d $as]_BASE_ADDR is USER and will be preserved"
	  }
	  if {[get_property CONFIG.M[format %.2d $i]_A[format %.2d $as]_ADDR_WIDTH.VALUE_SRC $ip] != "USER"} {
            lappend assignments CONFIG.M[format %.2d $i]_A[format %.2d $as]_ADDR_WIDTH $aw
	  } else {
	    ifx_puts "CONFIG.M[format %.2d $i]_A[format %.2d $as]_ADDR_WIDTH is USER and will be preserved"
	  }
          incr as
        }
      } else {
        ifx_puts "No address segments found for MI $i"
      }
      if {$as > $max_as} { set max_as $as }
    }

    set ar_s [get_property CONFIG.ADDR_RANGES.VALUE_SRC $ip]
    if {[get_property CONFIG.ADDR_RANGES.VALUE_SRC $ip] != "USER"} {
      ifx_puts "Max Addr Ranges: $max_as"
      if {$max_as > 0} {
        set_property CONFIG.ADDR_RANGES $max_as $ip
      } else {
        set_property CONFIG.ADDR_RANGES 1 $ip
      }
    } else {
      ifx_puts "ADDR Ranges is USER and will be preserved"
    }

    if {[llength $assignments] > 0} {
      set_property -dict $assignments $ip
    } else {
      ifx_puts "No address assignments detected"
    }
  } else {
    ifx_dump_addr_segs $cell_name

    ifx_puts "----------------------------------------------------------------------"
    set maxj 0
    set assignments {}
    for {set i 0} {$i < $num_mi} {incr i} {
      set as [get_bd_master_addr_segs $cell_name/[format M%.2d_AXI $i]]
      if {[llength $as] > 16} {
        ::bd::send_msg -of $cell_name -type error -msg_id 1 -text "IP Integrator address map contains too many address segments ([llength $as])."
        break
      }

      ifx_puts "before remove_offset_violations $as"
      set as [remove_offset_violations $as]

      ifx_puts "before segments_to_segment_tuples: $as"
      set segment_tuples [segments_to_segment_tuples $as]

      ifx_puts "segment tuples: $segment_tuples"
      set grouped_segments [group_by_largest_subset_overlap $segment_tuples]

      ifx_puts "grouped_segments:$grouped_segments"
      print_map $grouped_segments

      detect_overlapping_keyhole_segments $cell_name $grouped_segments    

      detect_slave_segment_remapping $cell_name $grouped_segments

      set as [::bd::addr::sort_by_seg_name [dict keys $grouped_segments]]

      set j 0
      foreach a $as {
        set op [format M%.2d_A%.2d_BASE_ADDR $i $j]
        set o  [format 0x%.16x [get_property offset [get_bd_addr_segs $a]]]
        set rp [format M%.2d_A%.2d_ADDR_WIDTH $i $j]
        set r  [get_property range [get_bd_addr_segs $a]]
        if {$r < 0} {
          ::bd::send_msg -of $cell_name -type critical_warning -msg_id 1 -text "Invalid range ($r) dectected in address segment $a.  Address segment will not be imported."
          continue
        }
        set r  [expr round(log($r)/log(2))]

        set ip_v [get_property CONFIG.$op $ip]
        set ip_s [get_property CONFIG.$op.VALUE_SRC $ip]
        if {$ip_s != "USER" && ![string equal $ip_v $o]} {
          lappend assignments CONFIG.$op $o 
        } else {
          if {[string equal $ip_v $o]} {
            ifx_puts "$ip.CONFIG.$op is already $o ($ip_v) - no need to update"
          } else {
            ifx_puts "$ip.CONFIG.$op is $ip_v $ip_s and will be preserved"          
          }
        }

        set ip_v [get_property CONFIG.$rp $ip]
        set ip_s [get_property CONFIG.$rp.VALUE_SRC $ip]
        if {$ip_s != "USER" && ![string equal $ip_v $r]} {        
          lappend assignments CONFIG.$rp $r 
        } else {
          if {[string equal $ip_v $r]} {
            ifx_puts "$ip.CONFIG.$op is already $r ($ip_v) - no need to update"
          } else {
            ifx_puts "$ip.CONFIG.$rp is $ip_v $ip_s and will be preserved"        
          }
        }
        incr j
      }
      if {$maxj < $j} {set maxj $j}
    }

    if {$maxj > 0} {
      lappend assignments CONFIG.ADDR_RANGES $maxj 
    } else {
      ifx_puts "WARNING: No address map detected"
    }
    if {[llength $assignments] > 0} {
      ifx_puts "Applying address assignments $assignments"
      if {[catch {set_property -dict $assignments $ip}]} {
        ::bd::send_msg -of $cell_name -type error -msg_id 2 -text "Incompatible IPI address assignments detected.  Please resolve incompatible assignments using the IPI address editor before continuing."
      }
    } else {
      ifx_puts "No address assignments required"
    }
  }

  lock_rwm_by_connectivity $ip
  trim_si_acceptance_from_reachable_mi $ip
  ifx_debug_proc_footer
}

ifx_debug_trace_setup
