load librdi_iptasks[info sharedlibextension]
source [::bd::get_vlnv_dir xilinx.com:ip:mem:1.3]/utility/bd_utility.tcl

proc init {cellpath otherInfo } {
  # cell initialization here
  set cell [get_bd_cells $cellpath]
  set paramList {C0.DDR3_AxiIDWidth C0.DDR3_AxiNarrowBurst}
  bd::mark_propagate_overrideable $cell $paramList
}


proc post_config_ip { cellpath otherInfo } {
   set cell [get_bd_cells $cellpath]
   set controllerType [ get_property CONFIG.C0.ControllerType $cell ]
   
   set ui_clk_handle [ get_bd_pins $cell/c0_ddr3_ui_clk ]
   if { $ui_clk_handle != "" } {

        set config_attr CONFIG.C0.DDR3_UI_CLOCK
        set current_val [ get_property CONFIG.FREQ_HZ $ui_clk_handle ]

        set phyVal [get_property CONFIG.C0.DDR3_PhyClockRatio $cell ]

        if { [ string compare $phyVal "4:1" ] == 0 } {
          set clkRatio [ expr 4 ]
        } elseif { [ string compare $phyVal "2:1" ] == 0 } {
          set clkRatio [ expr 2 ]
        }

        set timePeriod [ get_property CONFIG.C0.DDR3_TimePeriod $cell ]
#bd::send_msg -of $cellpath -type INFO -msg_id 1 -text "DDR3_SDRAM: Setting Values for TimePeriod: current value: $timePeriod "
        set hzCovert [ expr (1000000/$timePeriod)*1000000 ]

        set config_val [ expr $hzCovert/$clkRatio ]

#bd::send_msg -of $cellpath -type INFO -msg_id 1 -text "DDR3_SDRAM: UI CLock Setting Values for CONFIG.FREQ_HZ: current value: $current_val & new value: $config_val "
        set_property CONFIG.FREQ_HZ $config_val $ui_clk_handle
#bd::send_msg -of $cellpath -type INFO -msg_id 1 -text "DDR3_SDRAM: UI Clock Setting Values for CONFIG.FREQ_HZ: current value: $current_val & new value: $config_val "
   }

 
   set ddr3_handle [ get_bd_intf_pins $cell/C0_DDR3 ]
   if { $ddr3_handle != "" } {
       set timeperiod_val [ get_property CONFIG.C0.DDR3_TimePeriod $cell ]
       set_property CONFIG.TIMEPERIOD_PS $timeperiod_val $ddr3_handle

       set memory_type_val [ get_property CONFIG.C0.DDR3_MemoryType $cell ]
       set_property CONFIG.MEMORY_TYPE $memory_type_val $ddr3_handle

       set memory_part_val [ get_property CONFIG.C0.DDR3_MemoryPart $cell ]
       set_property CONFIG.MEMORY_PART $memory_part_val $ddr3_handle

       set datawidth_val [ get_property CONFIG.C0.DDR3_DataWidth $cell ]
       set_property CONFIG.DATA_WIDTH $datawidth_val $ddr3_handle

       set cs_enabled_val [ get_property CONFIG.C0.DDR3_ChipSelect $cell ]
       set_property CONFIG.CS_ENABLED $cs_enabled_val $ddr3_handle

       set dm_enabled_val [ get_property CONFIG.C0.DDR3_DataMask $cell ]
       set_property CONFIG.DATA_MASK_ENABLED $dm_enabled_val $ddr3_handle

       set mem_addrmap_val [ get_property CONFIG.C0.DDR3_Mem_Add_Map $cell ]
       set_property CONFIG.MEM_ADDR_MAP $mem_addrmap_val $ddr3_handle

       set burst_len_val [ get_property CONFIG.C0.DDR3_BurstLength $cell ]
       set_property CONFIG.BURST_LENGTH $burst_len_val $ddr3_handle

       set axi_arb_val [ get_property CONFIG.C0.DDR3_AxiArbitrationScheme $cell ]
       set_property CONFIG.AXI_ARBITRATION_SCHEME $axi_arb_val $ddr3_handle

       set cas_latency_val [ get_property CONFIG.C0.DDR3_CasLatency $cell ]
       set_property CONFIG.CAS_LATENCY $cas_latency_val $ddr3_handle

       set casw_latency_val [ get_property CONFIG.C0.DDR3_CasWriteLatency $cell ]
       set_property CONFIG.CAS_WRITE_LATENCY $casw_latency_val $ddr3_handle

       set custom_parts_val [ get_property CONFIG.C0.DDR3_CustomParts $cell ]
       set_property CONFIG.CUSTOM_PARTS $custom_parts_val $ddr3_handle

       set slot_val [ get_property CONFIG.C0.DDR3_Slot $cell ]
       set_property CONFIG.SLOT $slot_val $ddr3_handle
    }

   foreach addn_clk_index { 1 2 3 4 } {
     set addn_ui_clk_handle [ get_bd_pins $cell/addn_ui_clkout${addn_clk_index} ]
     if { $addn_ui_clk_handle != "" } {

       set config_attr CONFIG.ADDN_UI_CLKOUT${addn_clk_index}_FREQ_HZ
       set current_val [ get_property CONFIG.FREQ_HZ $addn_ui_clk_handle ]
       set clkout_mhz [ get_property $config_attr $cell ]
       set config_val [ expr ($clkout_mhz * 1000000) ]

#bd::send_msg -of $cellpath -type INFO -msg_id 1 -text "DDR3_SDRAM: Additional Clock Setting Values for CONFIG.FREQ_HZ: current value: $current_val & new value: $config_val "
       set_property CONFIG.FREQ_HZ $config_val $addn_ui_clk_handle
#bd::send_msg -of $cellpath -type INFO -msg_id 1 -text "DDR3_SDRAM: Additional Clock Setting Values for CONFIG.FREQ_HZ: current value: $current_val & new value: $config_val "
        set conttype "ddr3"
		set ui_clk_handle [ get_bd_pins $cell/c0_${conttype}_ui_clk ]
		set clock_domain [ get_property CONFIG.CLK_DOMAIN $ui_clk_handle ]
	   set_property CONFIG.CLK_DOMAIN $clock_domain $addn_ui_clk_handle
	 }
   }
}

proc post_propagate { cellpath otherInfo } {
    set cell [get_bd_cells $cellpath]

    set controllerType [ get_property CONFIG.C0.ControllerType $cell ]
    set cType "DDR3"
    set slave_axi_handle [ get_bd_intf_pins $cell/C0_DDR3_S_AXI ]
    if { $slave_axi_handle != "" } {

#set_property CONFIG.NUM_READ_OUTSTANDING 8 $slave_axi_handle
#set_property CONFIG.NUM_WRITE_OUTSTANDING 8 $slave_axi_handle

	set config_attr CONFIG.C0.DDR3_AxiIDWidth
	if {[get_property ${config_attr}.VALUE_SRC $cell] != "USER"} {
       set drive_intf [find_bd_objs -quiet -thru_hier -relation connected_to $slave_axi_handle]
       if { $drive_intf != "" } {
	       set conn_intf_val [ get_property CONFIG.ID_WIDTH $drive_intf ]
	       if { [ string compare $conn_intf_val "0" ] !=0 } {
		       set_property $config_attr $conn_intf_val $cell
	       } else {
		       set_property $config_attr 1 $cell
	       }
       }
	    #bd::send_msg -of $cellpath -type INFO -msg_id 1 -text "DDR4: ID_WIDTH Propagated value is $propagated_val"
	}
#bd::send_msg -of $cellpath -type INFO -msg_id 1 -text "DDR3_SDRAM: ID_WIDTH Propagated value is $propagated_val"

        set config_attr CONFIG.C0.DDR3_AxiNarrowBurst
		if {[get_property ${config_attr}.VALUE_SRC $cell] != "USER"} {
        set propagated_val [ get_property CONFIG.SUPPORTS_NARROW_BURST $slave_axi_handle ]
        set_property $config_attr $propagated_val $cell
		}
#bd::send_msg -of $cellpath -type INFO -msg_id 1 -text "DDR3_SDRAM: NARROW_BURST Propagated value is $propagated_val"
   }
   #DRC Input clock period (SYS_CLK or SYS_CLK_i) should be in the range of
   #+-5ps of the value of InputClockPeriod.

   set Auto_M_D [get_property CONFIG.C0.DDR3_Specify_MandD $cell]
   if {$Auto_M_D} {
      	set Mval [get_property CONFIG.C0.DDR3_CLKFBOUT_MULT $cell]
      	set Dval [get_property CONFIG.C0.DDR3_DIVCLK_DIVIDE $cell]
      	set D0val [get_property CONFIG.C0.DDR3_CLKOUT0_DIVIDE $cell]
      	set CLKOUT [get_property CONFIG.C0.DDR3_TimePeriod $cell]
	    set phy_clk_ratio 4
    
	    set fpgapart [get_property PART [current_project]]
	    set controllerType [ get_property CONFIG.C0.ControllerType $cell ]
	    #bd::send_msg -of $cellpath -type INFO -msg_id 17 -text "fpgapart $fpgapart" 
	    set CLKIN_ps [cal_clkin_ps $CLKOUT $phy_clk_ratio $Mval $Dval $D0val]
	    #bd::send_msg -of $cellpath -type INFO -msg_id 12 -text "CLKIN_ps = $CLKIN_ps" 
	    set ddr3_clk_period [cal_clkin_ps_rounded $cellpath $cell $controllerType $fpgapart $CLKIN_ps $Mval $Dval]		
        
	 }  else {
        set ddr3_clk_period [get_property CONFIG.C0.DDR3_InputClockPeriod $cell]
	 }

   set max_clk_period [ expr ($ddr3_clk_period + 5) ]
   set min_clk_period [ expr ($ddr3_clk_period - 5) ]
   set max_clk_freqMhz [ expr (1000000.0/$min_clk_period) ]
   set min_clk_freqMhz [ expr (1000000.0/$max_clk_period) ]
   set ddr3_clk_freqMhz [ expr (1000000.0/$ddr3_clk_period) ]

   set max_clk_freqMhz [format "%.3f" $max_clk_freqMhz]
   set min_clk_freqMhz [format "%.3f" $min_clk_freqMhz]
   set ddr3_clk_freqMhz [format "%.3f" $ddr3_clk_freqMhz]

   set min_clk_freqHz [ expr (1000000000000.0/$max_clk_period) ]
   set min_clk_freqHz [format "%.3f" $min_clk_freqHz]
	
	set max_clk_freqHz [ expr (1000000000000.0/$min_clk_period) ]
	set max_clk_freqHz [format "%.3f" $max_clk_freqHz]
	
   set slave_sys_clk_handle [ get_bd_intf_pins $cell/C0_SYS_CLK ]

   if { $slave_sys_clk_handle != "" } {
     set input_clk_fhz [ get_property CONFIG.FREQ_HZ $slave_sys_clk_handle ]
     set input_clk_fMhz [ expr ($input_clk_fhz/1000000.0) ]
     set input_clk_fMhz [format "%.3f" $input_clk_fMhz]
     if { $input_clk_fMhz < $min_clk_freqMhz || $input_clk_fMhz > $max_clk_freqMhz } {
        bd::send_msg -of $cellpath -type CRITICAL_WARNING -msg_id 1 -text " Clock frequency of the connected clock (${slave_sys_clk_handle}) is $input_clk_fMhz MHz while \"Reference Input Clock Speed\" is $ddr3_clk_freqMhz MHz. Please either reconfigure the parameter \"Reference Input Clock Speed (CONFIG.C0.DDR3_InputClockPeriod)\" of IP or change frequency of the connected clock (CONFIG.FREQ_HZ) within the range of $min_clk_freqHz to $max_clk_freqHz Hz."
     }
  }

  set slave_sys_clk_handle [ get_bd_pins $cell/c0_sys_clk_i ]
  if { $slave_sys_clk_handle != "" } {
     set input_clk_fhz [ get_property CONFIG.FREQ_HZ $slave_sys_clk_handle ]
     set input_clk_fMhz [ expr ($input_clk_fhz/1000000.0) ]
     set input_clk_fMhz [format "%.3f" $input_clk_fMhz]
     if { $input_clk_fMhz < $min_clk_freqMhz || $input_clk_fMhz > $max_clk_freqMhz } {
        bd::send_msg -of $cellpath -type CRITICAL_WARNING -msg_id 2 -text " Clock frequency of the connected clock (${slave_sys_clk_handle}) is $input_clk_fMhz MHz while \"Reference Input Clock Speed\" is $ddr3_clk_freqMhz MHz. Please either reconfigure the parameter \"Reference Input Clock Speed (CONFIG.C0.DDR3_InputClockPeriod)\" of IP or change frequency of the connected clock (CONFIG.FREQ_HZ) within the range of $min_clk_freqHz to $max_clk_freqHz Hz."
     }
 }
}


