x393_lma.py 87.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
from __future__ import print_function
'''
# Copyright (C) 2015, Elphel.inc.
# Fit DQ/DQS timing parameters using Levenberg-Marquardt algorithm 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http:#www.gnu.org/licenses/>.

@author:     Andrey Filippov
@copyright:  2015 Elphel, Inc.
@license:    GPLv3.0+
@contact:    andrey@elphel.coml
@deffield    updated: Updated
'''
__author__ = "Andrey Filippov"
__copyright__ = "Copyright 2015, Elphel, Inc."
__license__ = "GPL"
__version__ = "3.0+"
__maintainer__ = "Andrey Filippov"
__email__ = "andrey@elphel.com"
__status__ = "Development"
31 32
import math
import numpy as np
33 34 35 36 37 38 39 40 41 42 43 44 45 46
"""
For each byte lane:
tSDQS delay ps/step (~1/5 of datasheet value) - 1
tSDQi  delay ps/step (~1/5 of datasheet value) - 8
tDQSHL=tDQSH-tDQSL (ps) - 1
tDQiHL=tDQiH-tDQiL (ps) - 8
tDQS=0 (not adjusted here) - 0
tDQi - DQi routing delay with respect to DQS - 8
tFDQS - array of 5 fine delay steps (here in ps) - 5
tFDQi - array of 5 fine delay steps (here in ps)  for each bit - 5*8=40

error**2 here  (y-tFDQi-fXi)**2
"""

47 48 49 50 51 52 53 54 55 56
            
PARAMETER_TYPES=(
                     {"name":"tSDQS",   "size":1,            "units":"ps","description":"DQS input delay per step (1/5 of the datasheet value)","en":1},
                     {"name":"tSDQ",    "size":8,            "units":"ps","description":"DQ input delay per step (1/5 of the datasheet value)","en":1},
                     {"name":"tDQSHL",  "size":1,            "units":"ps","description":"DQS HIGH minus LOW difference","en":1},
                     {"name":"tDQHL",   "size":8,            "units":"ps","description":"DQi HIGH minus LOW difference","en":1},
                     {"name":"tDQS",    "size":1,            "units":"ps","description":"DQS delay (not adjusted)","en":0},
                     {"name":"tDQ",     "size":8,            "units":"ps","description":"DQi delay","en":1},
                     {"name":"tFDQS",   "size":4,            "units":"ps","description":"DQS fine delays (mod 5)","en":1}, #only 4 are independent, 5-th is -sum of 4 
                     {"name":"tFDQ",    "size":32,           "units":"ps","description":"DQ  fine delays (mod 5)","en":1},
57
                     {"name":"anaScale","size":1, "dflt":20, "units":"ps","description":"Scale for non-binary measured results","en":1}, #should not be 0 - singular matrix
58
                     {"name":"tCDQS",   "size":30,           "units":"ps","description":"DQS primary delays (all but 8 and 24","en":1}, #only 4 are independent, 5-th is -sum of 4 
59 60 61
                     )
FINE_STEPS=5
DLY_STEPS =FINE_STEPS * 32 # =160 
62
def test_data(meas_delays,
63
              compare_prim_steps,
64
              quiet=1):
65 66 67 68
    halfStep=0.5
    if compare_prim_steps:
        halfStep*=FINE_STEPS
        
69 70 71 72 73
    if quiet < 2:
        print ("DQS",end=" ")
        for f in ('ir','if','or','of'):
            for b in range (16):
                print ("%s_%d"%(f,b),end=" ")
74 75
        print() 
               
76 77 78 79 80 81 82 83 84 85 86 87 88 89
        for ldly, data in enumerate(meas_delays):
            print("%d"%ldly,end=" ")
            if data:
                """
                for typ in ((0,0),(0,1),(1,0),(1,1)):
                    for pData in data: # 16 DQs, each None nor a pair of lists for inPhase in (0,1), each a pair of edges, each a pair of (dly,diff)
                        if pData and (not pData[typ[0]][typ[1]] is None):
                            print ("%d"%pData[typ[0]][typ[1]],end=" ")
                        else:
                            print ("x",end=" ")
                """            
                for typ in range(4):
                    for pData in data: # 16 DQs, each None nor a pair of lists for inPhase in (0,1), each a pair of edges, each a pair of (dly,diff)
                        if pData and (not pData[typ] is None):
90
                            if pData[typ][1] is None:
91
                                print ("%d"%(pData[typ][0]+halfStep),end=" ")
92
                            else:
93
                                print ("%d"%(pData[typ][0]),end=" ")
94 95 96
                        else:
                            print ("x",end=" ")
            print()
97

98 99 100 101 102 103 104
def make_repeat(value,nRep):
    if isinstance(value,(list,tuple)):
        return value
    else:
        return (value,)*nRep
           
class X393LMA(object):
105 106
    lambdas={"initial":0.1,"current":0.1,"max":100.0}
    maxNumSteps=25
107
    finalDiffRMS=0.001
108
    parameters=None
109 110 111 112
#    parameterMask={}
    parameterMask={'tSDQS':    True,
                   'tSDQ':     [True, True, True, True, True, True, True, True],
                   'tDQSHL':   True,
113
                   'tDQHL':    [True, True, True, True, True, True, True, True],# 23.523465ps -> 23.315524 - too little difference?
114 115 116 117
                   'tDQS':     False,
                   'tDQ':      [True, True, True, True, True, True, True, True],
                   'tFDQS':    [True, True, True, True],
                   'tFDQ':     [True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True],
118
                   'anaScale': True, # False,# True, # False # Broke?
119 120 121 122 123 124
#                   'tCDQS':    False # True #False #True # list of 30
                   'tCDQS':    [True,  True,  True,  True,  True,  True,  True,  True, # 8
                               True,  True,  True,  True,  True,  True,  True,  True,  True,  True,  True,  True,  True,  True,  True,
#                                False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, #15
                                True,  True,  True,  True,  True,  True,  True]
#                                False, False, False, False, False, False, False] #7
125 126 127 128 129 130 131 132 133 134 135 136 137
                   }
    """
    parameterMask={'tSDQS':    True,
                   'tSDQ':     [True, True, True, True, True, True, True, True],
                   'tDQSHL':   True, # False, # True,
                   'tDQHL':    [True, True, True, True, True, True, True, True], # False, # [True, True, True, True, True, True, True, True], #OK
                   'tDQS':     False,
                   'tDQ':      [True, True, True, True, True, True, True, True], #BAD - without it 0 in JTbyJ for tFDQ
                   'tFDQS':    [True, True, True, True], # False, # [True, True, True, True], # OK
                   'tFDQ':     True, # False, # [True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True],
                   'anaScale': False
                   }
    """
138
    parameterVector=None
139 140
    clk_period=None
    analog_scale=20 # ps when there is analog result -0.5...+0.5, multiply it by analog_scale and add to result
141 142 143 144 145 146 147 148 149
#    hist_estimated=None # DQ/DQS delay period,
#                        # DQ-DQS shift (and number of periods later) for averaged and individual bits,
#                        # for each of 4 edge types
    def __init__(self):
        pass
    
    def createYandWvectors(self,
                           lane,
                           data_set,
150 151
                           compare_prim_steps,
                           scale_w=0.2, # multiply weight by this if fractions are undefined
152 153 154 155 156
                           periods=None,
                           quiet=1):
        
        if quiet < 3:
            print ("createYandWvectors(): scale_w=%f"%(scale_w))
157 158 159 160 161 162
        def pythIsNone(obj):
            return obj is None
        isNone=pythIsNone
        if isinstance(data_set,np.ndarray):
            isNone=np.isnan
        
163
        n=len(data_set)*32
164 165 166 167 168
#        fx=np.zeros((DLY_STEPS*32,))
        """
        use np.nan instead of the None data
        np.isnan() test
        , dtype=np.float
169
        @param compare_prim_steps while scanning, compare this delay with 1 less by primary(not fine) step,
170 171 172 173 174 175 176
                            save None for fraction in unknown (previous -0.5, next +0.5)
        """
        halfStep=0.5
        if compare_prim_steps:
            halfStep*=FINE_STEPS
#        extra_Y=(0.0,halfStep)    

177
        y=np.zeros((n,), dtype=np.int) #[0]*n
178
        
179
        w=np.zeros((n,)) #[0]*n
180 181 182
#        f=np.full((n,),np.nan) # fractions
        f=np.empty((n,)) # fractions
        f[:]=np.nan
183
        yf=np.zeros((n,)) # y with added fractions
184
        if not periods is None:
185
            p=np.zeros((n), dtype=np.int)#[0]*n 
186 187 188 189 190 191 192 193 194 195
        for dly,data in enumerate(data_set):
            if data:
                data_lane=data[lane*8:(lane+1)*8]
                pm=[None]*8
                for b,bData in enumerate(data_lane): # bdata for each bit is either None or has 4 (maybe None) DQ integer delay values
                    if bData:
                        pm[b]=[None]*4
                        for t,tData in enumerate(bData):
                            if not tData is None: #[dly],[b],[t] tData - int value
                                i=32*dly+8*t+b
196 197 198 199 200 201 202 203
                                y[i]=tData[0]
                                if not isNone(tData[1]):
                                    f[i] = tData[1]
                                    yf[i]=tData[0]
                                    w[i]=1
                                else:
                                    w[i]=scale_w
                                    yf[i]=tData[0]+halfStep
204
                                if not periods is None:
205 206 207 208
                                    p[i]=periods[dly][b][t]
        #Normalize weights
        S0=np.sum(w)
        w*=1.0/S0                            
209
        vectors={'y':y,'yf':yf,'w':w,'f':f} # yf - use for actual float value, y - integer
210 211 212 213 214 215
        if not periods is None:
            vectors['p']=p                        
        return vectors 

    def showYOrVector(self,
                      ywp,
216 217 218 219 220 221
                      filtered=False,
                      vector=None,
                      showMode="IA"):
        def pythIsNone(obj):
            return obj is None
        isNone=pythIsNone
222 223 224 225
        # If vector is None - print y vector (skipping zero mask),
        # otherwise print vector (should be the same length, using the same 'w' weight mask
        v=vector
        if v is None:
226
            v= ywp['yf']
227
        w=ywp['w']
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
        try:
            f=ywp['f']
            noF=False
        except:
            f=None
            noF=True
        if not noF:
            if isinstance(f,np.ndarray):
                isNone=np.isnan
#                print ("using np.isnan")
#        print("filtered=",filtered)
        n=len(v)/32
        if 'A' in showMode.upper():
            av=[]
            for dly in range(n):
                avd=[]
                SAX=0.0
                SA0=0.0
                for t in range(4):
                    SX=0.0
                    S0=0.0
                    for b in range(8):
                        i=32*dly+8*t+b
                        if w[i] and ((not filtered) or noF or (not isNone(f[i]))):
                            SX+=w[i]*v[i]
                            S0+=w[i]
                    SAX+=SX
                    SA0+=S0        
                    if S0>0:
                        SX/=S0
                    else:
                        SX=None
                    avd.append(SX)
                if SA0>0:
                    SAX/=SA0
                else:
                    SAX=None
                avd.append(SAX)
                av.append(avd)
              
268
        print("DQS_dly", end= " ")
269 270 271 272 273 274 275
        if "I" in  showMode.upper():
            for ft in ('ir','if','or','of'):
                for b in range (8):
                    print ("%s_%d"%(ft,b),end=" ")
        if "A" in  showMode.upper():
            for ft in ('ir','if','or','of','all'):
                    print ("%s"%(ft),end=" ")
276
        print()
277
        
278 279
        for dly in range(n):
            print("%d"%dly,end=" ")
280 281 282 283 284 285 286 287 288 289 290 291
            if "I" in  showMode.upper():
                for t in range(4):
                    for b in range(8):
                        i=32*dly+8*t+b
                        if w[i] and ((not filtered) or noF or (not isNone(f[i]))):
                            print("%s"%(str(v[i])),end=" ")
                        else:
                            print("?",end=" ")
            if "A" in  showMode.upper():
                for a in av[dly]:
                    if not a is None:
                        print("%f"%(a),end=" ")
292
                    else:
293 294
                        print("?",end=" ")
                                  
295
            print()
296 297 298
    def showENLresults(self,
                       DQvDQS):
        rslt_names=("early","nominal","late")
299
        err_name='maxErrDqs'
300 301 302 303 304 305 306 307 308 309 310 311 312 313
        numBits=0
        for n in DQvDQS:
            try:
                for d in DQvDQS[n]:
                    try:
                        numBits=len(d)
#                        print ("d=",d)
                        break
                    except:
                        pass
                break        
            except:
                pass
        if not numBits:
314 315 316
#            print("showENLresults(): No no-None data provided")
#            print("DQvDQS=",DQvDQS)
#            return
317
            raise Exception("showENLresults(): No no-None data provided")
318
        numLanes=numBits//8
319
#        print ("****numBits=%d"%(numBits))            
320 321 322 323
        enl_list=[]
        for k in rslt_names:
            if DQvDQS[k]:
                enl_list.append(k)
324
                
325 326 327 328
        print("DQS", end=" ")
        for enl in enl_list:
            for b in range(numBits):
                print("%s%d"%(enl[0].upper(),b),end=" ")
329 330 331 332 333 334
        for enl in enl_list:
            for lane in range(numLanes):
                    if numLanes > 1:
                        print("%s%d_err"%(enl[0].upper(),lane),end=" ")
                    else:    
                        print("%s_err"%(enl[0].upper()),end=" ")
335 336 337 338 339 340 341 342 343 344 345 346
        print()
        for dly in range(len(DQvDQS[enl_list[0]])):
            print ("%d"%(dly),end=" ")
            for enl in enl_list:
                if DQvDQS[enl][dly] is None:
                    print ("? "*numBits,end="")
                else:
                    for b in range(numBits):
                        if DQvDQS[enl][dly][b] is None:
                            print("?",end=" ")
                        else:
                            print("%d"%(DQvDQS[enl][dly][b]),end=" ")
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
            for enl in enl_list:
#                if numLanes>1:
#                    print ("DQvDQS[err_name]=",DQvDQS[err_name])
#                    print ("DQvDQS[err_name][enl]=",DQvDQS[err_name][enl])
#                    print ("DQvDQS[err_name][enl][dly]=",DQvDQS[err_name][enl][dly])
                if DQvDQS[err_name][enl][dly] is None:
                    print ("? "*numLanes,end="")
                else:
                    for lane in range(numLanes):
                        if DQvDQS[err_name][enl][dly] is None:
                            print("?",end=" ")
                        else:
                            if numLanes > 1:
                                if DQvDQS[err_name][enl][dly][lane] is None:
                                    print("?",end=" ")
                                else:
                                    print("%.1f"%(DQvDQS[err_name][enl][dly][lane]),end=" ")
                            else:
                                print("%.1f"%(DQvDQS[err_name][enl][dly]),end=" ")
366
            print()
367
        
368
    
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    def normalizeParameters(self,
                            parameters,
                            isMask=False):
        """
        Convert single/lists as needed
        """
        if parameters is None:
            parameters = self.parameters
        for par in PARAMETER_TYPES:
            name=par['name']
            size=par["size"]
            try:
                v=parameters[name]
            except:
                if isMask:
                    v=par['en']
                else:
                    try:
                        v=par['dflt']
                    except:
                        raise Exception("parameter['%s'] is not defined and PARAMETER_TYPES['%s'] does not provide default value"%(name,name))
            if size == 1:
                if isinstance(v,(list,tuple)):
                    v=v[0]
                if isMask:
                    if v:
                        v=True
                    else:
                        v=False
            else:
                if isinstance(v,tuple):
                    v=list(v)
                elif not isinstance(v,list):
                    v=[v]*size
                if isMask:
                    for i in range(size):
                        if v[i]:
                            v[i]=True
                        else:
                            v[i]=False
                
            parameters[name]=v 
        return parameters        
    def copyParameters(self,
                       parameters):
        newPars={}
        for k,v in parameters.items():
            if isinstance(v,(list,tuple)):
                newPars[k]=list(v)
            else:
                newPars[k]=v
        return newPars
            
422 423 424 425

    def createParameterVector(self,
                              parameters=None,
                              parameterMask=None):
426
#        global PARAMETER_TYPES
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
        if parameters is None:
            parameters = self.parameters
        if parameterMask is None:
            parameterMask = self.parameterMask
        vector=[]
        for par in PARAMETER_TYPES:
            name=par['name']
            size=par["size"]
            if par['en']:
                try:
                    mask=parameterMask[name]
                except:
                    mask=True
                try:
                    parVal=parameters[name]
                except:
                    parVal=None
                    # mask=False
                if mask:
                    mask=  make_repeat(mask,size)
                    parVal=make_repeat(parVal,size)
                    for m,p in zip(mask, parVal):
                        if m:
                            vector.append(p)
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
        return np.array(vector)

    def createParameterIndex(self,
                             parameters=None,
                             parameterMask=None):
        """
        create dict as parameters, but instead of values - index in the parameter vector, or -1
        """
        if parameters is None:
            parameters = self.parameters
        if parameterMask is None:
            parameterMask = self.parameterMask
        indices={}
        parIndex=0
        for par in PARAMETER_TYPES:
            name=par['name']
            size=par["size"]
            if par['en']:
                try:
                    mask=parameterMask[name]
                except:
                    mask=True
                if mask:
                    if size==1:
                        indices[name]=parIndex
                        parIndex += 1
                    else:
                        if not isinstance(mask,(list,tuple)):
                            mask=[mask]*size
                        indices[name]=[]
                        for m in mask:
                            if m:
                                indices[name].append(parIndex)
                                parIndex += 1
                            else:
                                indices[name].append(-1)
            if not name in indices:    
                if size==1:
                    indices[name]=-1
                else:
                    indices[name]=[-1]*size
        indices['numPars']=parIndex # extra key with total number of parameters            
        return indices
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
        
    def getParametersFromVector(self,
                                vector=None,
                                parameterMask=None,
                                parameters=None):# if not None, will be updated 
#        global PARAMETER_TYPES
        if vector is None:
            vector=self.parameterVector
        if parameterMask is None:
            parameterMask=self.parameterMask
        if parameters is None:
            parameters={}
        index=0
        for par in PARAMETER_TYPES:
            name=par['name']
            size=par["size"]
            if par['en']:
                try:
                    mask=parameterMask[name]
                except:
                    mask=True
                if mask:
                    mask=  make_repeat(mask,size)
                    if size==1:
                        if mask[0]:
                            parameters[name]=vector[index]
                            index += 1
                    else:
                        if not name in parameters:
                            parameters[name]=[None]*size
524 525 526 527
                        for i,m in enumerate(mask):
                            if m:
                                parameters[name][i]=vector[index]
                                index += 1
528 529
        return parameters
     
530
    def dqi_dqsi_estimate_from_histograms(self,
531 532 533 534 535 536
                                 lane, # byte lane
                                 bin_size,
                                 clk_period,
                                 dly_step_ds,
                                 primary_set,
                                 data_set,
537
                                 compare_prim_steps, 
538 539 540 541 542 543 544 545 546 547 548 549 550 551
                                 quiet=1):        
        """
        Prepare data by building and processing histograms to find
        DQ/DQS period (in fine delay steps),
        and for each data bit (and averaged) DQ-DQS shift (in fine steps) and
        full periods - for primary set (and same phase) - starting from
        closest to 0 (both signs) at DQS==0, for non-primary -\
        from DQ 180 degrees later tha primary
        
        using datasheet delay/step (without fine step delay)
        and the data set (for each DQS delay - list of 16 bits,
        each of 2x2 elements (DQ delay values) or null
        Create data set template - for each DQS delay and inPhase
         - branch - number of full periods to add
552 553 554 555 556 557 558
        @param lane          byte lane to process 
        @param bin_size     bin size for the histograms (should be 5/10/20/40)
        @param clk_period   SDCLK period in ps
        @param dly_step_ds  IDELAY step (from the datasheet)
        @param primary_set  which of the data edge series to use as leading (other will be trailing by 180) 
        @param data_set     measured data set
        @param compare_prim_steps while scanning, compare this delay with 1 less by primary(not fine) step,
559
                            save None for fraction in unknown (previous -0.5, next +0.5)
560
        @param quiet        reduce output   
561 562 563 564 565
        """
        num_hist_steps=2*((DLY_STEPS+bin_size-1)//bin_size)
        
        est_step_period=(clk_period/dly_step_ds)*FINE_STEPS
        est_bin_period=est_step_period/bin_size
566 567 568 569
        halfStep=0.5
        if compare_prim_steps:
            halfStep*=FINE_STEPS
        extra_Y=(0.0,halfStep)    
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
        hist=[0.0]* num_hist_steps
        hist4=[]
        for _ in range(4):
            hist4.append(list(hist))
        hist8x4=[] # [8][4][num_hist_steps]
        for _i in range(8):
            l=[]
            for _j in range(4):
                l.append(list(hist))
            hist8x4.append(l)
        for dly,data in enumerate(data_set):
            if data:
                data_lane=data[lane*8:(lane+1)*8]
                for b,bData in enumerate(data_lane):
                    if bData:
                        for t,tData in enumerate(bData):
                            if not tData is None:
587
                                binNum=int((tData[0]+extra_Y[tData[1] is None]-dly+DLY_STEPS+1) / bin_size)
588 589 590
                                hist8x4[b][t][binNum] += 1 # lowest bin will be 1 count shy
                                if binNum == 0:
                                    hist8x4[b][t][binNum] += 1.0/(bin_size-1.0)
591
                                    
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
        for t in range(4):
            for i in range(num_hist_steps):
                for b in range(8):
                    hist4[t][i]+=hist8x4[b][t][i]
                hist4[t][i] /= 8.0
        if quiet <1:
            for i in range(num_hist_steps):
                print ("%d"%i, end=" ")
                for t in range(4):
                    for b in range(8):
                        print ("%f"%(hist8x4[b][t][i]), end=" ")
                    print ("%f"%(hist4[t][i]), end=" ")
                print()    
        #Correlate
        corr=[0.0]* num_hist_steps
        for shft in range(num_hist_steps):
            for x in range(0,num_hist_steps-shft):
                for t in range(4):
                    corr[shft]+=hist4[t][x]*hist4[t][x+shft]
        if quiet <1:
            for i, c in enumerate(corr):
                print("%d %f"%(i,c))            
        if quiet <2:
            print ("est_step_period=%f\nest_bin_period=%f"%(est_step_period,est_bin_period))
        # find actual period  using correlation
        if est_bin_period > (0.8*len(corr)):
            raise Exception("Estimated DQS period %f is too high to measure with this data set correlation (%d)"%
                            (est_bin_period, len(corr)))
        corr_low=int(0.5*est_bin_period)
        corr_high=min(int(1.5*est_bin_period),len(corr))
        if quiet <1:
            print ("corr_low=%d, corr_high=%d"%(corr_low, corr_high))
        xmx=corr_low
        for x in range (corr_low,corr_high+1):
            if corr[x] > corr[xmx]:
                xmx=x
        span=max(int(round(est_bin_period/8)),4)
629 630
        corr_low=max(corr_low,xmx-span,-num_hist_steps//2)
        corr_high=min(corr_high,xmx+span,num_hist_steps//2-1)
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
        if quiet <1:
            print ("corrected corr_low=%d, corr_high=%d, xmx=%d"%(corr_low, corr_high, xmx))
        S0=0
        SX=0
        for x in range (corr_low,corr_high+1):
            S0+=corr[x]
            SX+=corr[x]*x
        corr_bin_period=SX/S0
        corr_period= corr_bin_period*bin_size # in finedelay steps
        if quiet <2:
            print ("Period by correlation=%f, (in bin steps: %f)"%(corr_period,corr_bin_period))
        xSpan=min(int(corr_bin_period/2)+1,num_hist_steps//2)
        corr_low= -xSpan
        corr_high= xSpan
        xmx=None
        mx=0
        if quiet <1:
            print ("corr_low=%d, corr_high=%d"%(corr_low, corr_high))
        for x in range(corr_low,corr_high+1):
            y=hist4[primary_set][num_hist_steps//2 + x]
            if y > mx:
                mx=y
                xmx=x
        span=max(int(round(corr_bin_period/8)),4)
655 656
        corr_low=max(corr_low,xmx-span,-num_hist_steps//2)
        corr_high=min(corr_high,xmx+span,num_hist_steps//2-1)
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
        if quiet < 1:
            print ("corrected corr_low=%d, corr_high=%d, xmx=%d"%(corr_low, corr_high, xmx))
        S0=0
        SX=0
        for x in range (corr_low,corr_high+1):
            y=hist4[primary_set][num_hist_steps//2 + x]
            S0+=y
            SX+=y*x
        primary_dly_shift= (SX/S0)* bin_size 
        if quiet < 1:
            print ("tDQ-tDQS difference for primary set =%f (dly fine steps) (in bin steps %f)"%(primary_dly_shift,primary_dly_shift/bin_size))
                        
        # now for each of the other series find maximum closest to either primary or primary +-180 (sign here - opposite to the primary sign)
        # do not forget to apply that sign later, so primary is always leading
        b_series=[None]*4
        for t in range(4):
            if t==primary_set:
                b_series[t]=(primary_dly_shift,0)
            else:
                b_start=primary_dly_shift # will search around b_start
                periods=0
                if ((t ^ primary_set) & 2):
                    if primary_dly_shift > 0:
                        b_start -= corr_period/2
                        periods=-1
                    else:
                        b_start += corr_period/2
                        periods=0 # as expected - primary is supposed to have lower DQ delay, than secondary
                xSpan= corr_bin_period/2
                #scanning in bin, not dly steps
                corr_low= max(int(b_start/bin_size-xSpan),-(num_hist_steps//2))
688
                corr_high=min(int(b_start/bin_size+xSpan), (num_hist_steps//2)-1)
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
                xmx=None
                mx=0
                if quiet < 1:
                    print ("series=%d, b_start=%f, corr_low=%d, corr_high=%d, xSpan=%f"%(t, b_start, corr_low, corr_high,xSpan))
                for x in range(corr_low,corr_high+1):
                    y=hist4[t][num_hist_steps//2 + x]
                    if y > mx:
                        mx=y
                        xmx=x
                span=max(int(round(corr_bin_period/8)),4)
                corr_low=max(corr_low,xmx-span)
                corr_high=min(corr_high,xmx+span)
                if quiet < 1:
                    print ("series=%d corrected corr_low=%d, corr_high=%d, xmx=%d"%(t, corr_low, corr_high, xmx))
                S0=0
                SX=0
                for x in range (corr_low,corr_high+1):
                    y=hist4[t][num_hist_steps//2 + x]
                    S0+=y
                    SX+=y*x
                b_series[t]= ((SX/S0)* bin_size,periods) 
                if quiet < 1:
                    print ("tDQ-tDQS difference for set%d =%s (dly fine steps) (in bin steps %d)"%(t,  str(b_series[t]), b_series[t][0]))
        if quiet < 2:
            print ("b_series=%s"%(str(b_series)))                
        #Now find per-bit maximums closest to the average ones                            
        b_indiv=[]
        for b, hst in enumerate(hist8x4):
            b_iseries=[None]*4
            for t in range(4):
                periods=b_series[t][1] # period shift of the averaged series
                b_start=b_series[t][0] # will search around b_start
                xSpan= corr_bin_period/2
                #scanning in bin, not dly steps
                corr_low= max(int(b_start/bin_size-xSpan),-(num_hist_steps//2))
724
                corr_high=min(int(b_start/bin_size+xSpan), (num_hist_steps//2)-1)
725 726 727 728 729 730 731 732 733 734
                xmx=None
                mx=0
                if quiet < 1:
                    print ("DQ[%d], series=%d, b_start=%f, corr_low=%d, corr_high=%d, xSpan=%f"%(b, t, b_start, corr_low, corr_high,xSpan))
                for x in range(corr_low,corr_high+1):
                    y=hst[t][num_hist_steps//2 + x]
                    if y > mx:
                        mx=y
                        xmx=x
                span=max(int(round(corr_bin_period/8)),4)
735 736
                corr_low=max(corr_low,xmx-span,-num_hist_steps//2)
                corr_high=min(corr_high,xmx+span,num_hist_steps//2-1)
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
                if quiet < 1:
                    print ("DQ[%d], series=%d corrected corr_low=%d, corr_high=%d, xmx=%d"%(b, t, corr_low, corr_high, xmx))
                S0=0
                SX=0
                for x in range (corr_low,corr_high+1):
                    y=hst[t][num_hist_steps//2 + x]
                    S0+=y
                    SX+=y*x
                b_iseries[t]= ((SX/S0)* bin_size,periods) 
                if quiet < 1:
                    print ("DQ[%d], tDQ-tDQS difference for set%d =%s (dly fine steps) (in bin steps %d)"%(b, t,  str(b_iseries[t]), b_iseries[t][0]))
            b_indiv.append(b_iseries)
        if quiet < 2:
            print ("b_indiv=%s"%(str(b_indiv)))
        return {'period':corr_period,
                'b_series':b_series,
                'b_indiv':b_indiv}
    
    def  get_periods_map(self,
                         lane,
                         data_set,
758
                         compare_prim_steps,
759
                         hist_estimated,
760 761
                         quiet=1): 
        """
762
        @param compare_prim_steps while scanning, compare this delay with 1 less by primary(not fine) step,
763 764 765
                            save None for fraction in unknown (previous -0.5, next +0.5)
        
        """                      
766
        #assign most likely period shift for each data sample
767 768 769 770
        halfStep=0.5
        if compare_prim_steps:
            halfStep*=FINE_STEPS
        extra_Y=(0.0,halfStep)    
771 772 773 774 775 776 777 778 779 780 781 782 783
        period=hist_estimated['period']
        data_periods_map=[]
        for dly,data in enumerate(data_set):
            if data:
                data_lane=data[lane*8:(lane+1)*8]
                pm=[None]*8
                for b,bData in enumerate(data_lane): # bdata for each bit is either None or has 4 (maybe None) DQ integer delay values
                    if bData:
                        pm[b]=[None]*4
                        for t,tData in enumerate(bData):
                            if not tData is None: #[dly],[b],[t] tData - int value
                                he=hist_estimated['b_indiv'][b][t] # tuple (b, periods)
                                #find most likely period shift
784
                                pm[b][t]=int(round((tData[0]+extra_Y[tData[1] is None]-dly-he[0])/period))+he[1]
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
                data_periods_map.append(pm)
            else:
                data_periods_map.append(None)
        if quiet < 1:       
            print ("\nDQS%d measured data"%lane)
            print ("DQS%d"%lane,end=" ")
            for f in ('ir','if','or','of'):
                for b in range (8):
                    print ("%s_%d"%(f,b),end=" ")
            print()        
                    
            for dly, data in enumerate(data_set):
                print("%d"%dly,end=" ")
                if data:
                    data_lane=data[lane*8:(lane+1)*8]
                    for typ in range(4):
                        for b, bData in enumerate(data_lane): # 8 DQs, each ... 
                            if bData and (not bData[typ] is None):
803 804
                                d=bData[typ][0]+extra_Y[bData[typ][1] is None]
                                print ("%d"%(d),end=" ")
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
                            else:
                                print ("x",end=" ")
                print()
    
        if quiet < 1:       
            print ("\nDQS%d periods data"%lane)
            print ("DQS%d"%lane,end=" ")
            for f in ('ir','if','or','of'):
                for b in range (8):
                    print ("%s_%d"%(f,b),end=" ")
            print()        
                    
            for dly, data in enumerate(data_set):
                print("%d"%dly,end=" ")
                if data:
                    data_lane=data[lane*8:(lane+1)*8]
                    for typ in range(4):
                        for b, bData in enumerate(data_lane): # 8 DQs, each ... 
                            if bData and (not bData[typ] is None):
                                print ("%d"%(data_periods_map[dly][b][typ]),end=" ")
                            else:
                                print ("x",end=" ")
                print()
        if quiet < 2:       
            print ("\nDQS%d combined data"%lane)
            print ("DQS%d"%lane,end=" ")
            for f in ('ir','if','or','of'):
                for b in range (8):
                    print ("%s_%d"%(f,b),end=" ")
            print()        
                    
            for dly, data in enumerate(data_set):
                print("%d"%dly,end=" ")
                if data:
                    data_lane=data[lane*8:(lane+1)*8]
                    for typ in range(4):
                        for b, bData in enumerate(data_lane): # 8 DQs, each ... 
                            if bData and (not bData[typ] is None):
843 844
                                d=bData[typ][0]+extra_Y[bData[typ][1] is None]
                                print ("%f"%(d-period*data_periods_map[dly][b][typ]),end=" ")
845 846 847 848 849
                            else:
                                print ("x",end=" ")
                print()
        
        return data_periods_map
850
    def lma_fit_dq_dqs(self,
851 852 853 854 855 856 857 858 859
                       lane, # byte lane
                       bin_size,
                       clk_period,
                       dly_step_ds,
                       primary_set,
                       data_set,
                       compare_prim_steps,
                       scale_w, 
                       quiet=1):        
860 861 862 863 864 865 866
        """
        Initialize parameters and y-vector
        using datasheet delay/step (without fine step delay)
        and the data set (for each DQS delay - list of 16 bits,
        each of 2x2 elements (DQ delay values) or null
        Create data set template - for each DQS delay and inPhase
         - branch - number of full periods to add
867 868 869
        After initial parametersn are created - run LMA to find optimal ones,
        then return up to 3 varints (early, nominal, late) providing the best
        DQ input delay for each DQS one
870 871 872 873 874 875 876
        @param lane         byte lane to process (or non-number - process all byte lanes of the device) 
        @param bin_size     bin size for the histograms (should be 5/10/20/40)
        @param clk_period   SDCLK period in ps
        @param dly_step_ds  IDELAY step (from the datasheet)
        @param primary_set  which of the data edge series to use as leading (other will be trailing by 180) 
        @param data_set     measured data set
        @param compare_prim_steps while scanning, compare this delay with 1 less by primary(not fine) step,
877
                            save None for fraction in unknown (previous -0.5, next +0.5)
878 879
        @param scale_w        weight for "uncertain" values (where samples chane from all 0 to all 1 in one step)
        @param quiet        reduce output
880 881
        @return 3-element dictionary of ('early','nominal','late'), each being None or a 160-element list,
                each element being either None, or a list of 3 best DQ delay values for the DQS delay (some mey be None too) 
882
        """
883 884 885 886
        if not isinstance(lane,(int, long)): # ignore content, process both lanes
            lane_rslt=[]
            numLanes=2
            parametersKey='parameters'
887
            errorKey='maxErrDqs'
888 889 890
            tDQKey='tDQ'
            earlyKey,nominalKey,lateKey=('early','nominal','late')
#            periods={earlyKey:-1,nominalKey:0,lateKey:1} #t0~= +1216 t1~=-1245
891
            for lane in range(numLanes):
892
                lane_rslt.append(self.lma_fit_dq_dqs(lane, # byte lane
893 894 895 896 897 898 899
                                        bin_size,
                                        clk_period,
                                        dly_step_ds,
                                        primary_set,
                                        data_set,
                                        compare_prim_steps,
                                        scale_w, 
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
                                        quiet))
            #fix parameters if they have average tDQ different by +/- period(s)
            tDQ_avg=[]
            for lane in range(numLanes):
                tDQ_avg.append(sum(lane_rslt[lane][parametersKey][tDQKey])/len(lane_rslt[lane][parametersKey][tDQKey]))
            per1_0=int(round((tDQ_avg[1]-tDQ_avg[0])/clk_period))
            if abs(tDQ_avg[1]-tDQ_avg[0]) > clk_period/2:
                if quiet <5:
                    print ("lma_fit_dq_dqs: Data lanes tDQ average differs by %.1fps (%d clocks), shifting to match"%(abs(tDQ_avg[1]-tDQ_avg[0]),per1_0))
                if abs(per1_0) > 1:
                    raise Exception ("BUG: lma_fit_dq_dqs: dta lanes differ by more than a period (per1_0=%d periods) - should not happen"%(per1_0))
                #see number of valid items in early,nominal,late branches of each lane
                numInVar=[{},{}]
                for lane in range(numLanes):    
                    for k in lane_rslt[lane].keys():
                        if (k != parametersKey) and (k != errorKey):
                            numValid=0
                            try:
                                for ph in lane_rslt[lane][k]:
                                    if not ph is None:
                                        numValid+=1
                            except:
                                pass
                            numInVar[lane][k]=numValid
                if quiet < 2:
                    print ("numInVar=",numInVar)
                late_lane=(0,1)[per1_0<0] # for late_lane E,N,L -> x, E, N, for not late_lane: E,N,L -> N, L, x
                move_late_lane= (0,1)[numInVar[late_lane][earlyKey] <= numInVar[1-late_lane][lateKey]] # currently both are 0 - nothing is lost
                tDQ_delta=clk_period*(-1,1)[move_late_lane]
                lane_to_change=(1,0)[late_lane ^ move_late_lane]
                if quiet < 2:
                    print ("late_lane=     ",late_lane)
                    print ("move_late_lane=",move_late_lane)
                    print ("lane_to_change=",lane_to_change)
                    print ("tDQ_delta=     ",tDQ_delta)
                # modify tDQ:
                for b in range(len(lane_rslt[lane_to_change][parametersKey][tDQKey])):
                    lane_rslt[lane_to_change][parametersKey][tDQKey][b]+=tDQ_delta
                if quiet < 2:
                    print ("lane_rslt[%d]['%s']['%s']=%s"%(lane_to_change,parametersKey,tDQKey, str(lane_rslt[lane_to_change][parametersKey][tDQKey])))
                # modify variants:
                if move_late_lane:
                    lane_rslt[lane_to_change][earlyKey],  lane_rslt[lane_to_change][nominalKey], lane_rslt[lane_to_change][lateKey]= (
                    lane_rslt[lane_to_change][nominalKey],lane_rslt[lane_to_change][lateKey],    None)
                else:     
                    lane_rslt[lane_to_change][lateKey],   lane_rslt[lane_to_change][nominalKey], lane_rslt[lane_to_change][earlyKey]= (
                    lane_rslt[lane_to_change][nominalKey],lane_rslt[lane_to_change][earlyKey],    None)
                
            # If there are only 2 ENL variants, make 'Nominal' - the largest
            
950 951
            rslt={}
            for k in lane_rslt[0].keys():
952 953
                if (k != parametersKey) and (k != errorKey):
                    for r in lane_rslt: # lane_rslt is list of two dictionaries
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
                        try:
                            l=len(r[k])
                            break
                        except:
                            pass
                    else:
                        rslt[k]=None
                        continue
                    rslt[k]=[]
                    for dly in range(l):
                        w=[]
                        for lane in range(numLanes):
                            if (lane_rslt[lane][k] is None) or (lane_rslt[lane][k][dly] is None):
                                w += [None]*8
                            else:
                                w+=lane_rslt[lane][k][dly]
                        for b in w:
                            if not b is None:
                                rslt[k].append(w)
                                break
                        else:
                            rslt[k].append(None)
            rslt[parametersKey] = []             
            for lane in range(numLanes):
978
                rslt[parametersKey].append(lane_rslt[lane][parametersKey])
979 980 981 982 983 984 985 986 987 988
#per1_0
#print parameters?
            if quiet <4:
                print ("'%s' = ["%(parametersKey))
                for lane_params in rslt[parametersKey]:
                    print("{")
                    for k,v in lane_params.items():
                        print('%s:%s'%(k,str(v)))  
                    print("},") 
                print ("]")    
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
            rslt[errorKey]={}
            for k in lane_rslt[0][errorKey].keys():
                for r in lane_rslt: # lane_rslt is list of two dictionaries
                    try:
                        l=len(r[errorKey][k])
                        break
                    except:
                        pass
                else:
                    rslt[errorKey][k]=None
                    continue
                rslt[errorKey][k]=[]
                for dly in range(l):
                    w=[]
                    for lane in range(numLanes):
                        if (lane_rslt[lane][errorKey][k] is None) or (lane_rslt[lane][errorKey][k][dly] is None):
                            w.append(None)
                        else:
                            w.append(lane_rslt[lane][errorKey][k][dly])
                    for lane in w:
                        if not lane is None:
                            rslt[errorKey][k].append(w)
                            break
                    else:
                        rslt[errorKey][k].append(None)
                        
1015 1016 1017 1018
            return rslt # byte lanes combined        
                
                
                
1019 1020
        if quiet < 3:
            print ("init_parameters(): scale_w=%f"%(scale_w))
1021 1022
        self.clk_period=clk_period
        
1023
        hist_estimated=self.dqi_dqsi_estimate_from_histograms(lane, # byte lane
1024 1025 1026 1027 1028
                                                     bin_size,
                                                     clk_period,
                                                     dly_step_ds,
                                                     primary_set,
                                                     data_set,
1029
                                                     compare_prim_steps, 
1030
                                                     quiet)
1031 1032
        if quiet < 3:
            print ("hist_estimated=%s"%(str(hist_estimated)))
1033 1034
        data_periods_map=self.get_periods_map(lane,
                                              data_set,
1035
                                              compare_prim_steps, 
1036 1037 1038 1039 1040
                                              hist_estimated,
                                              quiet)  #+1)

        ywp=    self.createYandWvectors(lane,
                                       data_set,
1041 1042
                                       compare_prim_steps,
                                       scale_w, 
1043 1044
                                       data_periods_map,
                                       quiet)
1045
#        print("ywp=%s"%(str(ywp)))
1046 1047
        if quiet < 2:
            print("\nY-vector:")
1048 1049 1050
            self.showYOrVector(ywp,False,None)
            print("\nY-vector(filtered):")
            self.showYOrVector(ywp,True,None)
1051
        if quiet < 2:
1052 1053 1054 1055 1056
            print("\nperiods_map:")
            self.showYOrVector(ywp,False,ywp['p'])
        if quiet < 2:
            print("\nweights_map:")
            self.showYOrVector(ywp,False,ywp['w'])
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
        
        
        

        step_ps=clk_period/hist_estimated['period'] #~15.6
        tDQSHL=0;
        tDQHL=[None]*8
        for b, d in enumerate(hist_estimated['b_indiv']):
            tDQSHL  += (d[1][0]-d[0][0] +d[3][0]-d[2][0])*step_ps
            tDQHL[b] = (d[0][0]-d[1][0] +d[3][0]-d[2][0])*step_ps
1067 1068
            if quiet < 3:
                print ("%d: S=%f, D=%f"%(b, d[1][0]-d[0][0] +d[3][0]-d[2][0], d[0][0]-d[1][0] +d[3][0]-d[2][0])) 
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
        tDQSHL  /= 8.0
        # calculate primary tDQ delays (primary - for the edges selected by 'primary_set'
        tDQ=[0.0]*8
        for b, d in enumerate(hist_estimated['b_indiv']):
            for dp in d:
                tDQ[b] += dp[0]-dp[1] * hist_estimated['period']
            tDQ[b] = step_ps*0.25*(tDQ[b] - hist_estimated['period'])    
         
        parameters={
                    "tSDQS":  step_ps,
                    "tSDQ":   (step_ps,)*8,
1080
                    "tDQSHL": tDQSHL, # 0.0,    # improve Seems that initial value does not match final by sign!
1081 1082 1083 1084
                    "tDQHL":  tDQHL, # (0.0)*8, # improve
                    "tDQS":   0.0,
                    "tDQ":    tDQ,
                    "tFDQS":  (0.0,)*4, 
1085 1086
                    "tFDQ":   (0.0,)*32,
                    "tCDQS":  (0.0,)*30
1087
#                    "anaScale":self.analog_scale
1088
                    }
1089 1090 1091 1092 1093 1094
        """
        Returns # best (early,nominal,late) for each bit for each delay ([3][160][8])
        Outer list each has 160-element list, some of which are None, others hove 8 elements (including None ones)
        """
        if quiet < 2:
            print ("parameters=%s"%(str(parameters)))
1095
        self.normalizeParameters(parameters) #isMask=False)
1096
        if quiet < 3:
1097
            print ("normalized parameters=%s"%(str(parameters)))
1098 1099 1100 1101 1102 1103 1104 1105 1106
        """
            both ways work:
        self.parameterMask={}
        self.normalizeParameters(self.parameterMask,isMask=True)
            and
        """
#        self.parameterMask=self.normalizeParameters({},isMask=True)
        self.parameterMask=self.normalizeParameters(self.parameterMask,isMask=True)
        
1107 1108
        if quiet < 4:
            print ("parameters mask=%s"%(str(self.parameterMask)))
1109 1110 1111 1112 1113
        create_jacobian=True

        fxj= self.createFxAndJacobian(parameters,
                                     ywp,   # keep in self.variable?
                                     primary_set,
1114 1115 1116
                                     create_jacobian,
                                     None, #parMask
                                     quiet)
1117 1118 1119 1120 1121 1122 1123 1124
        
        if create_jacobian:
            fx=fxj['fx']
        else:
            fx=fxj
            
        if quiet < 2:
            print("\nfx:")
1125 1126 1127 1128
            self.showYOrVector(ywp,False,fx)
            print("\nfx (filtered):")
            self.showYOrVector(ywp,True,fx)
            
1129
        if quiet < 5:
1130 1131 1132 1133
            arms = self.getParAvgRMS(parameters,
                                     ywp,
                                     primary_set, # prima
                                     quiet+1)
1134 1135
            print ("Before LMA (DQ lane %d): average(fx)= %fps, rms(fx)=%fps"%(lane,arms['avg'],arms['rms']))
            
1136
        if quiet < 3:
1137
            jByJT=np.dot(fxj['jacob'],np.transpose(fxj['jacob']))
1138 1139 1140 1141 1142 1143 1144
            print("\njByJT:")
            for i,l in enumerate(jByJT):
                print ("%d"%(i),end=" ")
                for d in l: 
                    print ("%f"%(d),end=" ")
                print()
        self.lambdas ['current']=self.lambdas ['initial']       
1145
        for n_iter in range(self.maxNumSteps):
1146 1147 1148 1149 1150 1151 1152
            OK,finished=self.LMA_step(parameters,
                            ywp, # keep in self.variable?
                            primary_set, # prima
                            None, # parMask=    None,
                            self.lambdas,
                            self.finalDiffRMS,
                            quiet)
1153
            if (quiet < 5) or ((quiet < 6) and finished):
1154 1155 1156 1157 1158 1159 1160 1161
                arms = self.getParAvgRMS(parameters,
                                         ywp,
                                         primary_set, # prima
                                         quiet+1)

                print ("%d: LMA_step %s average(fx)= %fps, rms(fx)=%fps"%(n_iter,("FAILURE","SUCCESS")[OK],arms['avg'],arms['rms']))
            if OK and quiet < 2:
                print ("updated parameters=%s"%(str(parameters)))
1162
            if finished:
1163 1164
                if quiet < 4:
                    print ("final parameters=%s"%(str(parameters)))
1165 1166 1167 1168 1169 1170
                break    
                
        fx= self.createFxAndJacobian(parameters,
                                     ywp,   # keep in self.variable?
                                     primary_set,
                                     False,
1171 1172
                                     None,
                                     quiet)
1173 1174
        
        if quiet < 3:
1175 1176 1177 1178 1179 1180
            print("\nfx-postLMA:")
            self.showYOrVector(ywp,False,fx)
            print("\nfx-postLMA (filtered):")
            self.showYOrVector(ywp,True,fx)
            
        # calculate DQ[i] vs. DQS for -1, 0 and +1 period
1181 1182 1183 1184 1185 1186
        DQvDQS_withErr=self.getBestDQforDQS(parameters,
                                            primary_set,
                                            quiet)

        DQvDQS=    DQvDQS_withErr['dqForDqs']
        DQvDQS_ERR=DQvDQS_withErr['maxErrDqs']
1187 1188 1189 1190
        rslt={}
        rslt_names=("early","nominal","late")
        for i, d in enumerate(DQvDQS):
            rslt[rslt_names[i]] = d
1191
        rslt['parameters']=parameters
1192 1193 1194 1195
        
        rslt['maxErrDqs']={} # {enl}[dly]
        for i, d in enumerate(DQvDQS_ERR):
            rslt['maxErrDqs'][rslt_names[i]] = d
1196
        if quiet < 3:
1197
            self.showDQDQSValues(parameters)
1198 1199 1200
        if quiet < 3:
            print ("*** quiet=",quiet)
            self.showENLresults(rslt) # here DQvDQS already contain the needed data 
1201
        
1202
        return rslt
1203 1204


1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
    def getBestDQforDQS(self,
                        parameters,
                        primary_set, # prima
                        quiet=1):
        period=self.clk_period
        tFDQS5=list(parameters['tFDQS'])
        tFDQS5.append(-tFDQS5[0]-tFDQS5[1]-tFDQS5[2]-tFDQS5[3])
        tSDQS=parameters['tSDQS']
        tSDQ= parameters['tSDQ'] # list
        
        tDQS =parameters['tDQS']#single value
        tDQ=  parameters['tDQ'] # list
        
        tCDQS32=list(parameters['tCDQS'][0:8])+[0]+list(parameters['tCDQS'][8:23])+[0]+list(parameters['tCDQS'][23:30])
1219 1220 1221
        
        tDQSHL =parameters['tDQSHL']#single value
        tDQHL=  parameters['tDQHL'] # list
1222

1223
        tFDQs=[] #corrections in steps?
1224 1225 1226 1227
        for b in range(8):
            tFDQi=list(parameters['tFDQ'][4*b:4*(b+1)])
            tFDQi.append(-tFDQi[0]-tFDQi[1]-tFDQi[2]-tFDQi[3])
            tFDQs.append(tFDQi)
1228 1229 1230 1231 1232
        #calculate worst bit error caused by duty cycles (asymmetry) of DQS and DQ lines
        #bit delay error just adds to the  0.25*(abs(tDQSHL)+abs(tDQHL))
        asym_err=[]
        for i in range(8):
            asym_err.append(0.25*(abs(tDQSHL)+abs(tDQHL[i])))
1233 1234
        if quiet < 3:
            print("asym_err=",asym_err) 
1235
        dqForDqs=[]
1236
        maxErrDqs=[]
1237 1238
        for enl in (0,1,2):
            vDQ=[]
1239
            vErr=[]
1240 1241 1242 1243
            someData=False
            for dly in range(DLY_STEPS):
                tdqs=dly * tSDQS - tDQS - tFDQS5[dly % FINE_STEPS] # t - time from DQS pad to internal DQS clock with zero setup/hold times to DQ FFs
                tdqs-=tCDQS32[dly // FINE_STEPS]
1244
                tdqs3=tdqs +(-0.75+enl)*period # (early, nominal, late) in ps, centered in the middle
1245
                bDQ=[]
1246 1247 1248
                errDQ=None
#                dbg_worstBit=None
#                dbg_errs=[None]*8
1249
                for b in range(8): # use all 4 variants
1250 1251
                    tdq=(tdqs3+tDQ[b])/tSDQ[b]
                    itdq=int((tdqs3+tDQ[b])/tSDQ[b]) # in delay steps, approximate (not including tFDQ
1252 1253 1254 1255
                    bestDQ=None
                    if (itdq >= 0) and (itdq < DLY_STEPS):
                        bestDiff=None
                        for idq in range (max(itdq-FINE_STEPS,0),min(itdq+FINE_STEPS,DLY_STEPS-1)+1):
1256 1257 1258
                            tdq=idq * tSDQ[b] - tDQ[b] - tFDQs[b][idq % FINE_STEPS]
                            diff=tdq - tdqs3 # idq-tFDQs[b][idq % FINE_STEPS]
                            if (bestDQ is None) or (abs(diff) < abs(bestDiff)):
1259
                                bestDQ=idq
1260
                                bestDiff=diff
1261 1262 1263
                    if bestDQ is None:            
                        bDQ=None
                        break
1264 1265 1266 1267 1268
                    bDQ.append(bestDQ) #tuple(delay,signed error in ps)
                    fullBitErr=abs(bestDiff) +asym_err[b] #TODO: Restore the full error!
                    if (errDQ is None) or (fullBitErr > errDQ):
                        errDQ= fullBitErr
                    
1269 1270
                    someData=True
                vDQ.append(bDQ)
1271
                vErr.append(errDQ)
1272 1273
            if someData:    
                dqForDqs.append(vDQ)
1274
                maxErrDqs.append(vErr)
1275
            else:
1276
                dqForDqs.append(None)
1277 1278
                maxErrDqs.append(None)
        return {'dqForDqs':dqForDqs,'maxErrDqs':maxErrDqs}
1279
        
1280 1281 1282 1283 1284 1285 1286 1287 1288
    """
    ir = ir0 - s/4 + d/4 # ir - convert to ps from steps
    if = if0 + s/4 - d/4
    or = or0 - s/4 - d/4 # ir - convert to ps from steps
    of = of0 + s/4 + d/4
    (s-d)/2=if-ir
    (s+d)/2=of-or
    s=if-ir+of-or
    d=ir-if+of-or
1289
    """
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
    def showDQDQSValues(self,
                        parameters):
        tFDQS5=list(parameters['tFDQS'])
        tFDQS5.append(-tFDQS5[0]-tFDQS5[1]-tFDQS5[2]-tFDQS5[3])
        tSDQS=parameters['tSDQS']
        tSDQ= parameters['tSDQ'] # list
        tCDQS32=list(parameters['tCDQS'][0:8])+[0]+list(parameters['tCDQS'][8:23])+[0]+list(parameters['tCDQS'][23:30])
        tFDQs=[] #corrections in steps?
        for b in range(8):
            tFDQi=list(parameters['tFDQ'][4*b:4*(b+1)])
            tFDQi.append(-tFDQi[0]-tFDQi[1]-tFDQi[2]-tFDQi[3])
            tFDQs.append(tFDQi)
        print("\nRelative delay vs delay value")
        print ("dly DSQS", end=" ")
        for b in range(8):
            print ("DQ%d"%(b),end=" ")
        print ()
        for dly in range(DLY_STEPS): # no constant delay - just scale and corrections
            print ("%d"%(dly),end=" ")
            tdqs=dly * tSDQS  - tFDQS5[dly % FINE_STEPS] # t - time from DQS pad to internal DQS clock with zero setup/hold times to DQ FFs
            tdqs-=tCDQS32[dly // FINE_STEPS]
            print("%.3f"%(tdqs),end=" ")
            for b in range(8):
                tdq=dly * tSDQ[b] - tFDQs[b][dly % FINE_STEPS]
                print("%.3f"%(tdq),end=" ")
            print()    

    
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
    def createFxAndJacobian(self,
                            parameters,
                            y_data, # keep in self.variable?
                            primary_set, # prima
                            jacobian=False, # create jacobian, False - only fx
                            parMask=None,
                            quiet=1):
        def pythIsNone(obj):
            return obj is None
        isNone=pythIsNone # swithch to np.isnan
        y_vector = y_data['y']
1329
        yf_vector = y_data['yf'] # when no fractions available - half interval (0.5 or 2.5) is added, if available - nothing is added
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
        periods_vector=y_data['p']
        period=self.clk_period
        try:
            y_fractions = y_data['f']
        except:
            y_fractions = None
        try:
            w_vector = y_data['w']
        except:
            w_vector = None
        
        anaScale = parameters['anaScale']
        if y_fractions is None:
            anaScale = 0
        elif isinstance(y_fractions,np.ndarray):
            isNone=np.isnan
#        fx=[0.0]*DLY_STEPS*32
        fx=np.zeros((DLY_STEPS*32,))
        #self.clk_period
        tFDQS5=list(parameters['tFDQS'])
        tFDQS5.append(-tFDQS5[0]-tFDQS5[1]-tFDQS5[2]-tFDQS5[3])
1351
        tCDQS32=list(parameters['tCDQS'][0:8])+[0]+list(parameters['tCDQS'][8:23])+[0]+list(parameters['tCDQS'][23:30])
1352
#        print("*****tCDQS32=",tCDQS32)            
1353
        
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
        tFDQ=[]
        for b in range(8):
            tFDQi=list(parameters['tFDQ'][4*b:4*(b+1)])
            tFDQi.append(-tFDQi[0]-tFDQi[1]-tFDQi[2]-tFDQi[3])
            tFDQ.append(tFDQi)
        tSDQS=parameters['tSDQS']
        tSDQ= parameters['tSDQ'] # list
        
        tDQS =parameters['tDQS']#single value
        tDQ=  parameters['tDQ'] # list
        
        tDQSHL =parameters['tDQSHL']#single value
        tDQHL=  parameters['tDQHL'] # list
        for dly in range(DLY_STEPS):
            tdqs=dly * tSDQS - tDQS - tFDQS5[dly % FINE_STEPS] # t - time from DQS pad to internal DQS clock with zero setup/hold times to DQ FFs
1369 1370 1371
            tdqs-=tCDQS32[dly // FINE_STEPS]
            tdqs_r = tdqs - 0.25 * tDQSHL # sign opposite from: ir = ir0 - s/4 + d/4; or = or0 - s/4 - d/4 - NOT, but maybe other is wrong
            tdqs_f = tdqs + 0.25 * tDQSHL # sign opposite from: if = if0 + s/4 - d/4; of = of0 + s/4 + d/4
1372 1373 1374 1375 1376 1377
            tdqs_rf=(tdqs_r, tdqs_f)
            #correct for DQS edge type
            for b in range(8): # use all 4 variants
                for t in range(4):
                    indx=32*dly+t*8+b
                    if (w_vector is None) or (w_vector[indx] > 0):
1378
                        tdq=yf_vector[indx] * tSDQ[b] - tDQ[b] - tFDQ[b][y_vector[indx] % FINE_STEPS]
1379 1380 1381 1382 1383 1384 1385 1386
                        # correct for periods
                        tdq -= period*periods_vector[indx] # or should it be minus here?
                        # correct for edge types
                        if (t == 0) or (t == 3):
                            tdq -= 0.25*tDQHL[b]
                        else: 
                            tdq += 0.25*tDQHL[b]
                        if anaScale:
1387 1388
                            if not isNone(y_fractions[indx]):
                                tdq-=anaScale*y_fractions[indx] # negative values mean that actual zero-point is not yet reached
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
                        if (t ^ primary_set) & 2:
                            tdq -= 0.5*period
                        fx[indx] = tdq - tdqs_rf[t & 1] # odd are falling DQS, even are rising DQS
        if not jacobian:
            return fx
        if parMask is None:
            parMask=self.normalizeParameters(self.parameterMask,isMask=True)
#        pv= self.createParameterVector(parameters,parMask)
#        numPars=len(pv)
#        print("pv=%s"%(str(pv)))
        parInd=self.createParameterIndex(parameters,parMask)
1400 1401
        if quiet <2:
            print("parInd=%s"%(str(parInd)))
1402 1403
        numPars=parInd['numPars']    
        jacob=np.zeros((numPars,DLY_STEPS*32))
1404
        """
1405 1406 1407 1408
        fineM5=((1.0, 0.0, 0.0, 0.0, -0.25),
                (0.0, 1.0, 0.0, 0.0, -0.25),
                (0.0, 0.0, 1.0, 0.0, -0.25),
                (0.0, 0.0, 0.0, 1.0, -0.25))
1409 1410 1411 1412 1413 1414
        """
        fineM5=((1.0, 0.0, 0.0, 0.0, -1.0),
                (0.0, 1.0, 0.0, 0.0, -1.0),
                (0.0, 0.0, 1.0, 0.0, -1.0),
                (0.0, 0.0, 0.0, 1.0, -1.0))
        
1415 1416 1417 1418 1419 1420
        dqs_finedelay_en=parInd['tFDQS']
        for e in dqs_finedelay_en:
            if e>=0:
                break
        else:
            dqs_finedelay_en=None
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
            
        dqs_delay32_en=parInd['tCDQS']
        for e in dqs_delay32_en:
            if e>=0:
                break
        else:
            dqs_delay32_en=None
#        tCDQS32=list(parameters['tCDQS'][0:8])+[0]+list(parameters['tCDQS'][8:23])+[0]+list(parameters['tCDQS'][23:30])
        if not dqs_delay32_en is None:
            dqs_delay32_index=range(0,8)+[-1]+range(8,23)+[-1]+range(23,30)
            for i,d in enumerate(dqs_delay32_index):
                if d >= 0:
                    dqs_delay32_index[i] = dqs_delay32_en[d]
                    
1435
#            print("*****dqs_delay32_index=",dqs_delay32_index)            
1436
            
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
        dq_finedelay_en=[None]*8
        for b in range(8):
            dq_finedelay_en[b]=parInd['tFDQ'][4*b:4*(b+1)]
            for e in dq_finedelay_en[b]:
                if e>=0:
                    break
            else:
                dq_finedelay_en[b]=None
            
        for dly in range(DLY_STEPS):
            dlyMod5=dly % FINE_STEPS
1448
            dlyDiv5=dly // FINE_STEPS
1449 1450 1451
            dtdqs_dtSDQS = dly
            dtdqs_dtDQS = -1.0
            dtdqs_dtFDQS = (-fineM5[0][dlyMod5],-fineM5[1][dlyMod5],-fineM5[2][dlyMod5],-fineM5[3][dlyMod5])
1452
            dtdqs_dtDQSHL_rf=(-0.25,+0.25) #  ign opposite from: ir = ir0 - s/4 + d/4; or = or0 - s/4 - d/4, ... - NOT, but maybe other is wrong
1453
            #correct for DQS edge type
1454
#            dbg=[0.0]*32
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
            for b in range(8): # use all 4 variants
                for t in range(4):
                    indx=32*dly+t*8+b
                    if (w_vector is None) or (w_vector[indx] > 0):
                        #dependencies of DQS delays
                        if parInd['tSDQS'] >= 0:
                            jacob[parInd['tSDQS'],indx]=-dtdqs_dtSDQS
                        if parInd['tDQS'] >= 0:
                            jacob[parInd['tDQS'],indx]=-dtdqs_dtDQS
                        if dqs_finedelay_en:
                            for i,pIndx in enumerate (dqs_finedelay_en):
                                if pIndx >= 0:
                                    jacob[pIndx,indx]=-dtdqs_dtFDQS[i]
1468 1469
                                    
                        if dqs_delay32_en:
1470
                            for i,pIndx in enumerate (dqs_delay32_index):
1471 1472
                                if pIndx >= 0:
                                    jacob[pIndx,indx]=(0,1.0)[i==dlyDiv5]
1473
#                                    dbg[i]+=jacob[pIndx,indx]
1474
                                    
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
                        if parInd['tDQSHL'] >= 0:
                            jacob[parInd['tDQSHL'],indx]=-dtdqs_dtDQSHL_rf[t & 1]
                        #dependencies of DQ delays
                        # tdq=y_vector[indx] * tSDQ[b] - tDQ[b] - tFDQ[b][y_vector[indx] % FINE_STEPS]
                        if parInd['tSDQ'][b] >= 0:
                            jacob[parInd['tSDQ'][b],indx]=y_vector[indx]
                        if parInd['tDQ'][b] >= 0:
                            jacob[parInd['tDQ'][b],indx] = -1
                        if dq_finedelay_en[b]:
                            yMod5=y_vector[indx] % FINE_STEPS
                            dtdq_dtFDQ = (-fineM5[0][yMod5],-fineM5[1][yMod5],-fineM5[2][yMod5],-fineM5[3][yMod5])
                            for i,pIndx in enumerate (dq_finedelay_en[b]):
                                if pIndx >= 0:
                                    jacob[pIndx,indx]=dtdq_dtFDQ[i]
                        if parInd['tDQHL'][b] >= 0:
                            if (t == 0) or (t == 3):
                                jacob[parInd['tDQHL'][b],indx]=-0.25
                            else:
                                jacob[parInd['tDQHL'][b],indx]=+0.25
                        if parInd['anaScale'] >= 0:
                            if anaScale and not isNone(y_fractions[indx]):
1496
                                jacob[parInd['anaScale'],indx]=-y_fractions[indx]
1497
#            print("dbg: %d: "%(dly),dbg)                            
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
        return {'fx':fx,'jacob':jacob}
    def getParAvgRMS(self,
                  parameters,
                  ywp,
                  primary_set, # prima
                  quiet=1):
        fx= self.createFxAndJacobian(parameters,
                                     ywp,   # keep in self.variable?
                                     primary_set,
                                     False, # jacobian
                                     None,
                                     quiet)
1510
        """
1511 1512 1513 1514 1515 1516 1517 1518
        SX=0.0
        SX2=0.0
        S0=0.0
        for d,w in zip(fx,ywp['w']):
            if w>0:
                S0+=w
                SX+=w*d
                SX2+=w*d*d
1519 1520 1521 1522
        """
        S0=np.sum(ywp['w'])
        SX=np.sum(fx*ywp['w'])
        SX2=np.sum(fx*fx*ywp['w'])
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
        avg= SX/S0
        rms= math.sqrt(SX2/S0)
        return {"avg":avg,"rms":rms}
        


    def LMA_step(self,
                parameters,
                ywp, # keep in self.variable?
                primary_set, # prima
                parMask,
                lambdas, #single-element list to update value
                finalDiffRMS,
                quiet=      1):
        parVector0=self.createParameterVector(parameters, parMask) # initial parameter vector
        arms0 = self.getParAvgRMS(parameters,
                                  ywp,
                                  primary_set, # prima
                                  quiet+1)
1542
        if quiet < 2:
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
                print ("LMA_step <start>: average(fx)= %fps, rms(fx)=%fps"%(arms0['avg'],arms0['rms']))

        delta=self.LMA_solve(parameters,
                             ywp, # keep in self.variable?
                             primary_set, # prima
                             parMask,
                             lambdas["current"],
                             quiet)
        parVector= parVector0+delta
#        print ("\nparVector0=%s"%(str(parVector0)))
#        print ("\ndelta=%s"%(str(delta)))
#        print ("\nparVector=%s"%(str(parVector)))
#        newPars = {}.update(parameters) # so fixed parameters will appear in the newPars
        newPars = self.copyParameters(parameters) # so fixed parameters will appear in the newPars
#        newPars = self.getParametersFromVector(parVector,
        if quiet < 2:
            print ("\nparameters=%s"%(str(parameters)))
#        print ("\n1: newPars=%s"%(str(newPars)))
        self.getParametersFromVector(parVector,
                                               parMask,
                                               newPars) # parameters=None):# if not None, will be updated 
        if quiet < 2:
            print ("\n2: newPars=%s"%(str(newPars)))
1566
#            print ("\nparameters=%s"%(str(parameters)))
1567 1568 1569 1570 1571
        arms1 = self.getParAvgRMS(newPars,
                                  ywp,
                                  primary_set, # prima
                                  quiet+1)
        finished=False
1572
        if arms1['rms'] <= arms0['rms']:
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
            parameters.update(newPars) 
            lambdas["current"]*=.5
            success=True
            if (arms0['rms'] - arms1['rms']) < finalDiffRMS:
                finished=True
        else:
            lambdas["current"]*=8.0
            success=False
            if lambdas["current"] > lambdas["max"]:
                finished=True
1583
        if (quiet < 2) or ((quiet < 4) and (not success)):
1584 1585 1586 1587 1588
                print ("LMA_step %s: average(fx)= %fps, rms(fx)=%fps, lambda=%f"%(('FAILURE','SUCCESS')[success],arms1['avg'],arms1['rms'],lambdas["current"]))
        return (success,finished)    
            
            

1589
    
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
    def LMA_solve(self,
                parameters,
                ywp, # keep in self.variable?
                primary_set, # prima
                parMask=    None,
                lmbda=      0.001,
                quiet=      1):
        fxj= self.createFxAndJacobian(parameters,
                                      ywp,   # keep in self.variable?
                                      primary_set,
                                      True, # jacobian
                                      parMask,
                                      quiet)
1603 1604 1605 1606 1607
        try:
            w_vector = ywp['w']
        except:
            w_vector = np.full((len(fxj['fx']),),1.0)
        wJ=fxj['jacob'] *w_vector
1608
        JT=np.transpose(fxj['jacob'])
1609
        #np.fill_diagonal(z3,np.diag(z3)*0.1)
1610
        jByJT=np.dot(wJ,JT)
1611 1612
        for i,_ in enumerate(jByJT):
            jByJT[i,i] += lmbda*jByJT[i,i]
1613
        jByDiff= -np.dot(wJ,fxj['fx'])
1614 1615 1616 1617
        delta=np.linalg.solve(jByJT,jByDiff)
        return delta
                
    """
1618
    
1619 1620 1621 1622 1623 1624 1625 1626 1627
    ir = ir0 - s/4 + d/4 # ir - convert to ps from steps
    if = if0 + s/4 - d/4
    or = or0 - s/4 - d/4 # ir - convert to ps from steps
    of = of0 + s/4 + d/4
    (s-d)/2=if-ir
    (s+d)/2=of-or
    s=if-ir+of-or
    d=ir-if+of-or
    """
1628
    def lma_fit_dqs_phase(self,
1629 1630 1631
                           lane, # byte lane
                           bin_size_ps,
                           clk_period,
1632 1633
                           dqs_dq_parameters,
                           tSDQS, # use if dqs_dq_parameters are not available
1634 1635 1636
                           data_set,
                           compare_prim_steps,
                           scale_w,
1637 1638 1639 1640
                           numPhaseSteps,
                           maxDlyErr=200.0, # ps - trying multiple overlapping branches
                           fallingPhase=False, # output mode - delays decrease when phase increases
                           shiftFracPeriod=0.5, # measured data is marginal, shift optimal by half period 
1641
                           quiet=1):
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
        """
        Calculate linear approximation for DQS-in or DQS-out vs. phase, crossing periods
        @param lane byte          lane to use (0,1 or 'all')
        @param bin_size_ps        histogram bin size in ps
        @param clk_period         clock period, in ps
        @param dqs_dq_parameters  dq{i,0} vs dqs[i,o} parameters or Null if not yet available (after write levelling)
                                  used to get initial delay scale and fine delay correction
        @param tSDQS              delay in ps for one finedelay step - used only if dqs_dq_parameters are not available 
        @param data_set           data set number for hard-coded debug data or -1 to use actual just measured results
        @param compare_prim_steps if True input data was calibrated with primary delay steps, False - if with fine delay
                                  That means that delay data is on average is either 2.5 or 0.5 lower than unbiased average  
        @param scale_w            weight for samples that have "binary" data with full uncertainty of +/-2.5 or +/-0.5 steps
        @param numPhaseSteps      Total number of delay steps (currently 5*32 = 160)
        @param maxDlyErr          Made for testing multiple overlapping branches, maximal error in ps to keep the result branch
        @param fallingPhase       input data is decreasing with phase increasing (command/addresses, data output), False - increasing
                                  as for DQS in /DQ in
        @param shiftFracPeriod    When measured data is marginal, not optimal, result needs to be shifted by this fraction of the period
                                  Currently it should be 0.5 for input, 0.0 - for output
        @param quiet=1):
        """
        
#       print("++++++lma_fit_dqs_phase(), quiet=",quiet)
        phase_sign=(1,-1)[fallingPhase]
        phase_add=(0,numPhaseSteps)[fallingPhase]
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
        def show_input_data(filtered):
            print(('unfiltered','filtered')[filtered])
            for phase,d in enumerate(data_set):
                print ("%d"%(phase),end=" ")
                if not d is None:
                    for lane,dl in enumerate(d):
                        if (not dl is None) and (not dl[0] is None) and ((not filtered) or (not dl[1] is None)):
                            dly = dl[0]
                            if dl[1] is None:
                                dly+=halfStep
#                            print ("%f %f"%(dly, dly-phase*phase_step/dbg_tSDQS[lane]), end=" ")
#                            print ("%f %f"%(dly*dbg_tSDQS[lane]/phase_step, dly*dbg_tSDQS[lane]/phase_step-phase), end=" ")
1678
                            print ("%f %f"%(dly*dbg_tSDQS[lane], dly*dbg_tSDQS[lane]+(phase_add-phase)*phase_step), end=" ")
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
                        else:
                            print ("? ?", end=" ")
                print()
        def get_shift_by_hist(span=5):
            for phase,d in enumerate(data_set):
                if not d is None:
                    dl=d[lane]
                    if (not dl is None) and (not dl[0] is None):
                        dly = dl[0]
                        if dl[1] is None:
                            dly+=halfStep
1690
                        diff_ps=dly*tSDQS+(phase_add-phase)*phase_step
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
                        binArr[int((diff_ps-minVal)/bin_size_ps)]+=1
            if quiet < 3:
                for i,h in enumerate(binArr):
                    print ("%d %d"%(i,h))
            indx=0
            for i,h in enumerate(binArr):
                if h > binArr[indx]:
                    indx=i
            low = max(0,indx-span)
            high = min(len(binArr)-1,indx+span)
            S0=0
            SX=0.0
            for i in range (low, high+1):
                S0+=binArr[i]
                SX+=binArr[i]*i
            if S0>0:
                SX/=S0
1708 1709
            if quiet < 3:
                print ("SX=",SX)
1710 1711 1712
            return minVal+bin_size_ps*(SX+0.5) # ps
        
        if not isinstance(lane,(int, long)): # ignore content, process both lanes
1713
            rslt_names=("dqs_optimal_ps","dqs_phase","dqs_phase_multi","dqs_phase_err","dqs_min_max_periods")
1714 1715 1716 1717 1718