###############################################################################
##
## Copyright (c) 2003 Xilinx, Inc. All Rights Reserved.
## DO NOT COPY OR MODIFY THIS FILE. 
## THE CONTENTS AND METHODOLOGY MAY CHANGE IN FUTURE RELEASES. 
##
## emc_common_v2_1_0.tcl
##
###############################################################################


#
# check address range
# if C_DEV_MIR_ENABLE = 1 then C_HIGHADDR - C_BASEADDR >= 0x144
#
# @param   mhsinst    the mhs instance handle
#
proc check_addr_range {mhsinst} {

    set base_param "C_BASEADDR"
    set high_param "C_HIGHADDR"

    set base_addr [xget_value $mhsinst "parameter" $base_param]
    set high_addr [xget_value $mhsinst "parameter" $high_param]

    # convert to hexadecimal format
    set base_addr [xformat_addr_string $base_addr $base_param]
    set high_addr [xformat_addr_string $high_addr $high_param]

    if {[compare_unsigned_addr_strings $base_addr $base_param $high_addr $high_param] == 1} {

        return

    }

    set range [expr $high_addr - $base_addr]

    if {[xget_value $mhsinst "parameter" "C_DEV_MIR_ENABLE"]} {

        # addr_range >= 0x144
        if {$range < [format %d "0x144"]} {

            set ipname [xget_value $mhsinst "option" "ipname"]
            error "Invalid address range for $ipname:\naddress range specified by $base_param and $high_param must be larger than or equal to 0x144" "" "libgen_error"
        }
    }          
}


#
# check C_MAX_MEM_WIDTH
# C_MAX_MEM_WIDTH = max(C_MEMx_WIDTH)
#
# @param   mhsinst    the mhs instance handle
#
proc check_max_mem_width { mhsinst  } {

    set max_mem_width [xget_value $mhsinst "parameter" "C_MAX_MEM_WIDTH"]
    set num_banks     [xget_value $mhsinst "parameter" "C_NUM_BANKS_MEM"]
    set max "0"

    for {set i 0} {$i < $num_banks} {incr i} {

        set bank_param [concat C_MEM${i}_WIDTH] 
        set bank_value [xget_value $mhsinst "parameter" $bank_param ]

        if {$bank_value > $max} {

            set max $bank_value
        }
    }

    if {$max_mem_width != $max} {

        set ipname [xget_value $mhsinst "option" "ipname"]
        error "Invalid $ipname parameter:\nC_MAX_MEM_WIDTH must be the maximum value among width of memory bank data bus" "" "libgen_error"
 
    }

}

