tim.c 28.7 KB
Newer Older
Alan Mishchenko committed
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
/**CFile****************************************************************

  FileName    [tim.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [A timing manager.]

  Synopsis    [Representation of timing information.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - April 28, 2007.]

  Revision    [$Id: tim.c,v 1.00 2007/04/28 00:00:00 alanmi Exp $]

***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

26 27
#include "misc/vec/vec.h"
#include "misc/mem/mem.h"
Alan Mishchenko committed
28 29
#include "tim.h"

30 31 32
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

typedef struct Tim_Box_t_           Tim_Box_t;
typedef struct Tim_Obj_t_           Tim_Obj_t;

// timing manager
struct Tim_Man_t_
{
    Vec_Ptr_t *      vBoxes;         // the timing boxes
    Vec_Ptr_t *      vDelayTables;   // pointers to the delay tables
    Mem_Flex_t *     pMemObj;        // memory manager for boxes
    int              nTravIds;       // traversal ID of the manager
Alan Mishchenko committed
47
    int              fUseTravId;     // enables the use of traversal ID
Alan Mishchenko committed
48 49 50 51
    int              nCis;           // the number of PIs
    int              nCos;           // the number of POs
    Tim_Obj_t *      pCis;           // timing info for the PIs
    Tim_Obj_t *      pCos;           // timing info for the POs
Alan Mishchenko committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
};

// timing box
struct Tim_Box_t_
{
    int              iBox;           // the unique ID of this box
    int              TravId;         // traversal ID of this box
    int              nInputs;        // the number of box inputs (POs)
    int              nOutputs;       // the number of box outputs (PIs)
    float *          pDelayTable;    // delay for each input->output path
    int              Inouts[0];      // the int numbers of PIs and POs
};

// timing object
struct Tim_Obj_t_
{
    int              Id;             // the ID of this object
    int              TravId;         // traversal ID of this object
    int              iObj2Box;       // mapping of the object into its box
    int              iObj2Num;       // mapping of the object into its number in the box
    float            timeArr;        // arrival time of the object
    float            timeReq;        // required time of the object
};

76 77 78
static inline Tim_Obj_t * Tim_ManCi( Tim_Man_t * p, int i )                           { assert( i < p->nCis ); return p->pCis + i;      }
static inline Tim_Obj_t * Tim_ManCo( Tim_Man_t * p, int i )                           { assert( i < p->nCos ); return p->pCos + i;      }
static inline Tim_Box_t * Tim_ManBox( Tim_Man_t * p, int i )                          { return (Tim_Box_t *)Vec_PtrEntry(p->vBoxes, i); }
Alan Mishchenko committed
79

80 81
static inline Tim_Box_t * Tim_ManCiBox( Tim_Man_t * p, int i )                        { return Tim_ManCi(p,i)->iObj2Box < 0 ? NULL : (Tim_Box_t *)Vec_PtrEntry( p->vBoxes, Tim_ManCi(p,i)->iObj2Box ); }
static inline Tim_Box_t * Tim_ManCoBox( Tim_Man_t * p, int i )                        { return Tim_ManCo(p,i)->iObj2Box < 0 ? NULL : (Tim_Box_t *)Vec_PtrEntry( p->vBoxes, Tim_ManCo(p,i)->iObj2Box ); }
Alan Mishchenko committed
82

Alan Mishchenko committed
83 84
static inline Tim_Obj_t * Tim_ManBoxInput( Tim_Man_t * p, Tim_Box_t * pBox, int i )   { assert( i < pBox->nInputs  ); return p->pCos + pBox->Inouts[i];               }
static inline Tim_Obj_t * Tim_ManBoxOutput( Tim_Man_t * p, Tim_Box_t * pBox, int i )  { assert( i < pBox->nOutputs ); return p->pCis + pBox->Inouts[pBox->nInputs+i]; }
Alan Mishchenko committed
85 86 87 88 89 90

#define Tim_ManBoxForEachInput( p, pBox, pObj, i )                               \
    for ( i = 0; (i < (pBox)->nInputs) && ((pObj) = Tim_ManBoxInput(p, pBox, i)); i++ )
#define Tim_ManBoxForEachOutput( p, pBox, pObj, i )                              \
    for ( i = 0; (i < (pBox)->nOutputs) && ((pObj) = Tim_ManBoxOutput(p, pBox, i)); i++ )

Alan Mishchenko committed
91 92
#define Tim_ManForEachCi( p, pObj, i )                                           \
    for ( i = 0; (i < (p)->nCis) && ((pObj) = (p)->pCis + i); i++ )              \
Alan Mishchenko committed
93
        if ( pObj->iObj2Box >= 0 ) {} else 
Alan Mishchenko committed
94 95
#define Tim_ManForEachCo( p, pObj, i )                                           \
    for ( i = 0; (i < (p)->nCos) && ((pObj) = (p)->pCos + i); i++ )              \
Alan Mishchenko committed
96 97
        if ( pObj->iObj2Box >= 0 ) {} else 
#define Tim_ManForEachBox( p, pBox, i )                                          \
98
    Vec_PtrForEachEntry( Tim_Box_t *, p->vBoxes, pBox, i )
Alan Mishchenko committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Starts the timing manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
115
Tim_Man_t * Tim_ManStart( int nCis, int nCos )
Alan Mishchenko committed
116 117 118
{
    Tim_Man_t * p;
    int i;
Alan Mishchenko committed
119
    p = ABC_ALLOC( Tim_Man_t, 1 );
Alan Mishchenko committed
120 121 122
    memset( p, 0, sizeof(Tim_Man_t) );
    p->pMemObj = Mem_FlexStart();
    p->vBoxes  = Vec_PtrAlloc( 100 );
Alan Mishchenko committed
123 124
    p->nCis = nCis;
    p->nCos = nCos;
Alan Mishchenko committed
125
    p->pCis = ABC_ALLOC( Tim_Obj_t, nCis );
Alan Mishchenko committed
126
    memset( p->pCis, 0, sizeof(Tim_Obj_t) * nCis );
Alan Mishchenko committed
127
    p->pCos = ABC_ALLOC( Tim_Obj_t, nCos );
Alan Mishchenko committed
128 129
    memset( p->pCos, 0, sizeof(Tim_Obj_t) * nCos );
    for ( i = 0; i < nCis; i++ )
Alan Mishchenko committed
130
    {
Alan Mishchenko committed
131 132 133 134 135
        p->pCis[i].Id = i;
        p->pCis[i].iObj2Box = p->pCis[i].iObj2Num = -1;
        p->pCis[i].timeReq = TIM_ETERNITY;
        p->pCis[i].timeArr = 0.0;
        p->pCis[i].TravId = 0;
Alan Mishchenko committed
136
    }
Alan Mishchenko committed
137
    for ( i = 0; i < nCos; i++ )
Alan Mishchenko committed
138
    {
Alan Mishchenko committed
139 140 141 142 143
        p->pCos[i].Id = i;
        p->pCos[i].iObj2Box = p->pCos[i].iObj2Num = -1;
        p->pCos[i].timeReq = TIM_ETERNITY;
        p->pCos[i].timeArr = 0.0;
        p->pCos[i].TravId = 0;
Alan Mishchenko committed
144
    }
Alan Mishchenko committed
145
    p->fUseTravId = 1;
Alan Mishchenko committed
146 147 148 149 150
    return p;
}

/**Function*************************************************************

Alan Mishchenko committed
151 152
  Synopsis    [Duplicates the timing manager.]

Alan Mishchenko committed
153 154
  Description [Derives discrete-delay-model timing manager. 
  Useful for AIG optimization with approximate timing information.]
Alan Mishchenko committed
155 156 157 158 159 160 161 162 163 164 165 166
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Tim_Man_t * Tim_ManDup( Tim_Man_t * p, int fDiscrete )
{
    Tim_Man_t * pNew;
    Tim_Box_t * pBox;
    float * pDelayTableNew;
    int i, k;
Alan Mishchenko committed
167 168 169 170 171 172 173
    pNew = Tim_ManStart( p->nCis, p->nCos );
    memcpy( pNew->pCis, p->pCis, sizeof(Tim_Obj_t) * p->nCis );
    memcpy( pNew->pCos, p->pCos, sizeof(Tim_Obj_t) * p->nCos );
    for ( k = 0; k < p->nCis; k++ )
        pNew->pCis[k].TravId = 0;
    for ( k = 0; k < p->nCos; k++ )
        pNew->pCos[k].TravId = 0;
Alan Mishchenko committed
174
    if ( fDiscrete )
Alan Mishchenko committed
175
    {
Alan Mishchenko committed
176 177
        for ( k = 0; k < p->nCis; k++ )
            pNew->pCis[k].timeArr = 0.0; // modify here
Alan Mishchenko committed
178 179
        // modify the required times
    }
Alan Mishchenko committed
180 181 182
    pNew->vDelayTables = Vec_PtrAlloc( 100 );
    Tim_ManForEachBox( p, pBox, i )
    {
183
//printf( "%d %d\n", pBox->nInputs, pBox->nOutputs );
Alan Mishchenko committed
184
        pDelayTableNew = ABC_ALLOC( float, pBox->nInputs * pBox->nOutputs );
Alan Mishchenko committed
185 186
        Vec_PtrPush( pNew->vDelayTables, pDelayTableNew );
        if ( fDiscrete )
Alan Mishchenko committed
187
        {
Alan Mishchenko committed
188 189
            for ( k = 0; k < pBox->nInputs * pBox->nOutputs; k++ )
                pDelayTableNew[k] = 1.0; // modify here
190 191 192 193 194 195 196 197 198 199 200 201

///// begin part of improved CIN/COUT propagation
            for ( k = 0; k < pBox->nInputs; k++ )  // fill in the first row
               pDelayTableNew[k] = 0.5;
            for ( k = 0; k < pBox->nOutputs; k++ ) // fill in the first column
               pDelayTableNew[k*pBox->nInputs] = 0.5;
            pDelayTableNew[0] = 0.0; // fill in the first entry 
///// end part of improved CIN/COUT propagation

            /// change
//            pDelayTableNew[0] = 0.0;
            /// change
Alan Mishchenko committed
202
        }
Alan Mishchenko committed
203 204 205 206 207 208 209 210 211 212
        else
            memcpy( pDelayTableNew, pBox->pDelayTable, sizeof(float) * pBox->nInputs * pBox->nOutputs );
        Tim_ManCreateBoxFirst( pNew, pBox->Inouts[0], pBox->nInputs, 
            pBox->Inouts[pBox->nInputs], pBox->nOutputs, pDelayTableNew );
    }
    return pNew;
}

/**Function*************************************************************

Alan Mishchenko committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  Synopsis    [Duplicates the timing manager.]

  Description [Derives unit-delay-model timing manager. 
  Useful for levelizing the network.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Tim_Man_t * Tim_ManDupUnit( Tim_Man_t * p )
{
    Tim_Man_t * pNew;
    Tim_Box_t * pBox;
    float * pDelayTableNew;
    int i, k;
Alan Mishchenko committed
229 230 231 232
    pNew = Tim_ManStart( p->nCis, p->nCos );
    memcpy( pNew->pCis, p->pCis, sizeof(Tim_Obj_t) * p->nCis );
    memcpy( pNew->pCos, p->pCos, sizeof(Tim_Obj_t) * p->nCos );
    for ( k = 0; k < p->nCis; k++ )
Alan Mishchenko committed
233
    {
Alan Mishchenko committed
234 235
        pNew->pCis[k].TravId = 0;
        pNew->pCis[k].timeArr = 0.0; 
Alan Mishchenko committed
236
    }
Alan Mishchenko committed
237 238
    for ( k = 0; k < p->nCos; k++ )
        pNew->pCos[k].TravId = 0;
Alan Mishchenko committed
239 240 241
    pNew->vDelayTables = Vec_PtrAlloc( 100 );
    Tim_ManForEachBox( p, pBox, i )
    {
Alan Mishchenko committed
242
        pDelayTableNew = ABC_ALLOC( float, pBox->nInputs * pBox->nOutputs );
Alan Mishchenko committed
243 244 245 246 247 248 249 250 251 252 253
        Vec_PtrPush( pNew->vDelayTables, pDelayTableNew );
        for ( k = 0; k < pBox->nInputs * pBox->nOutputs; k++ )
            pDelayTableNew[k] = 1.0;
        Tim_ManCreateBoxFirst( pNew, pBox->Inouts[0], pBox->nInputs, 
            pBox->Inouts[pBox->nInputs], pBox->nOutputs, pDelayTableNew );
    }
    return pNew;
}

/**Function*************************************************************

Alan Mishchenko committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267
  Synopsis    [Duplicates the timing manager.]

  Description [Derives the approximate timing manager with realistic delays
  but without white-boxes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Tim_Man_t * Tim_ManDupApprox( Tim_Man_t * p )
{
    Tim_Man_t * pNew;
    int k;
Alan Mishchenko committed
268 269 270 271
    pNew = Tim_ManStart( p->nCis, p->nCos );
    for ( k = 0; k < p->nCis; k++ )
        if ( p->pCis[k].iObj2Box == -1 )
            pNew->pCis[k].timeArr = p->pCis[k].timeArr;
Alan Mishchenko committed
272
        else
Alan Mishchenko committed
273 274 275
            pNew->pCis[k].timeArr = p->pCis[k].timeReq;
    for ( k = 0; k < p->nCos; k++ )
        pNew->pCos[k].timeReq = p->pCos[k].timeReq;
Alan Mishchenko committed
276 277 278 279 280
    return pNew;
}

/**Function*************************************************************

Alan Mishchenko committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  Synopsis    [Stops the timing manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManStop( Tim_Man_t * p )
{
    float * pTable;
    int i;
    if ( p->vDelayTables )
    {
296
        Vec_PtrForEachEntry( float *, p->vDelayTables, pTable, i )
Alan Mishchenko committed
297
            ABC_FREE( pTable );
Alan Mishchenko committed
298 299 300 301
        Vec_PtrFree( p->vDelayTables );
    }
    Vec_PtrFree( p->vBoxes );
    Mem_FlexStop( p->pMemObj, 0 );
Alan Mishchenko committed
302 303 304
    ABC_FREE( p->pCis );
    ABC_FREE( p->pCos );
    ABC_FREE( p );
Alan Mishchenko committed
305 306 307 308 309 310 311 312 313 314 315 316 317
}

/**Function*************************************************************

  Synopsis    [Stops the timing manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
void Tim_ManStopP( Tim_Man_t ** p )
{
    if ( *p == NULL )
        return;
    Tim_ManStop( *p );
    *p = NULL;
}

/**Function*************************************************************

  Synopsis    [Stops the timing manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
337 338 339 340 341 342
void Tim_ManPrint( Tim_Man_t * p )
{
    Tim_Box_t * pBox;
    Tim_Obj_t * pObj;
    int i;
    printf( "TIMING INFORMATION:\n" );
Alan Mishchenko committed
343
    Tim_ManForEachCi( p, pObj, i )
Alan Mishchenko committed
344
        printf( "pi%5d :  arr = %5.3f  req = %5.3f\n", i, pObj->timeArr, pObj->timeReq );
Alan Mishchenko committed
345
    Tim_ManForEachCo( p, pObj, i )
Alan Mishchenko committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        printf( "po%5d :  arr = %5.3f  req = %5.3f\n", i, pObj->timeArr, pObj->timeReq );
    Tim_ManForEachBox( p, pBox, i )
    {
        printf( "*** Box %3d :  Ins = %d. Outs = %d.\n", i, pBox->nInputs, pBox->nOutputs );
        printf( "Delay table:" );
        for ( i = 0; i < pBox->nInputs * pBox->nOutputs; i++ )
            printf( " %5.3f", pBox->pDelayTable[i] );
        printf( "\n" );
        Tim_ManBoxForEachInput( p, pBox, pObj, i )
            printf( "box-inp%3d :  arr = %5.3f  req = %5.3f\n", i, pObj->timeArr, pObj->timeReq );
        Tim_ManBoxForEachOutput( p, pBox, pObj, i )
            printf( "box-out%3d :  arr = %5.3f  req = %5.3f\n", i, pObj->timeArr, pObj->timeReq );
    }
    printf( "\n" );
}

/**Function*************************************************************

Alan Mishchenko committed
364 365 366 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
  Synopsis    [Disables the use of the traversal ID.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManTravIdDisable( Tim_Man_t * p )
{
    p->fUseTravId = 0;
}

/**Function*************************************************************

  Synopsis    [Enables the use of the traversal ID.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManTravIdEnable( Tim_Man_t * p )
{
    p->fUseTravId = 1;
}

/**Function*************************************************************

Alan Mishchenko committed
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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
  Synopsis    [Label box inputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManSetCurrentTravIdBoxInputs( Tim_Man_t * p, int iBox )
{
    Tim_Box_t * pBox;
    Tim_Obj_t * pObj;
    int i;
    pBox = Tim_ManBox( p, iBox );
    Tim_ManBoxForEachInput( p, pBox, pObj, i )
        pObj->TravId = p->nTravIds;
}

/**Function*************************************************************

  Synopsis    [Label box outputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManSetCurrentTravIdBoxOutputs( Tim_Man_t * p, int iBox )
{
    Tim_Box_t * pBox;
    Tim_Obj_t * pObj;
    int i;
    pBox = Tim_ManBox( p, iBox );
    Tim_ManBoxForEachOutput( p, pBox, pObj, i )
        pObj->TravId = p->nTravIds;
}

/**Function*************************************************************

Alan Mishchenko committed
438 439 440 441 442 443 444 445 446 447 448 449 450 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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
  Synopsis    [Label box inputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManSetPreviousTravIdBoxInputs( Tim_Man_t * p, int iBox )
{
    Tim_Box_t * pBox;
    Tim_Obj_t * pObj;
    int i;
    pBox = Tim_ManBox( p, iBox );
    Tim_ManBoxForEachInput( p, pBox, pObj, i )
        pObj->TravId = p->nTravIds - 1;
}

/**Function*************************************************************

  Synopsis    [Label box outputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManSetPreviousTravIdBoxOutputs( Tim_Man_t * p, int iBox )
{
    Tim_Box_t * pBox;
    Tim_Obj_t * pObj;
    int i;
    pBox = Tim_ManBox( p, iBox );
    Tim_ManBoxForEachOutput( p, pBox, pObj, i )
        pObj->TravId = p->nTravIds - 1;
}

/**Function*************************************************************

  Synopsis    [Updates required time of the PO.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Tim_ManIsCiTravIdCurrent( Tim_Man_t * p, int iCi )
{
    assert( iCi < p->nCis );
    assert( p->fUseTravId );
    return p->pCis[iCi].TravId == p->nTravIds;
}

/**Function*************************************************************

  Synopsis    [Updates required time of the PO.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Tim_ManIsCoTravIdCurrent( Tim_Man_t * p, int iCo )
{
    assert( iCo < p->nCos );
    assert( p->fUseTravId );
    return p->pCos[iCo].TravId == p->nTravIds;
}

/**Function*************************************************************

Alan Mishchenko committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
  Synopsis    [Sets the vector of timing tables associated with the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManSetDelayTables( Tim_Man_t * p, Vec_Ptr_t * vDelayTables )
{
    assert( p->vDelayTables == NULL );
    p->vDelayTables = vDelayTables;
}

/**Function*************************************************************

  Synopsis    [Creates the new timing box.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManCreateBox( Tim_Man_t * p, int * pIns, int nIns, int * pOuts, int nOuts, float * pDelayTable )
{
    Tim_Box_t * pBox;
    int i;
    pBox = (Tim_Box_t *)Mem_FlexEntryFetch( p->pMemObj, sizeof(Tim_Box_t) + sizeof(int) * (nIns+nOuts) );
    memset( pBox, 0, sizeof(Tim_Box_t) );
    pBox->iBox = Vec_PtrSize( p->vBoxes );
    Vec_PtrPush( p->vBoxes, pBox );
    pBox->pDelayTable = pDelayTable;
    pBox->nInputs  = nIns;
    pBox->nOutputs = nOuts;
    for ( i = 0; i < nIns; i++ )
    {
Alan Mishchenko committed
555
        assert( pIns[i] < p->nCos );
Alan Mishchenko committed
556
        pBox->Inouts[i] = pIns[i];
Alan Mishchenko committed
557 558
        p->pCos[pIns[i]].iObj2Box = pBox->iBox;
        p->pCos[pIns[i]].iObj2Num = i;
Alan Mishchenko committed
559 560 561
    }
    for ( i = 0; i < nOuts; i++ )
    {
Alan Mishchenko committed
562
        assert( pOuts[i] < p->nCis );
Alan Mishchenko committed
563
        pBox->Inouts[nIns+i] = pOuts[i];
Alan Mishchenko committed
564 565
        p->pCis[pOuts[i]].iObj2Box = pBox->iBox;
        p->pCis[pOuts[i]].iObj2Num = i;
Alan Mishchenko committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    }
}

/**Function*************************************************************

  Synopsis    [Creates the new timing box.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManCreateBoxFirst( Tim_Man_t * p, int firstIn, int nIns, int firstOut, int nOuts, float * pDelayTable )
{
    Tim_Box_t * pBox;
    int i;
Alan Mishchenko committed
584
//    printf( "Creating %d x %d box with first inputs (%d and %d).\n", nIns, nOuts, firstIn, firstOut );
585

Alan Mishchenko committed
586 587 588 589 590 591 592 593 594
    pBox = (Tim_Box_t *)Mem_FlexEntryFetch( p->pMemObj, sizeof(Tim_Box_t) + sizeof(int) * (nIns+nOuts) );
    memset( pBox, 0, sizeof(Tim_Box_t) );
    pBox->iBox = Vec_PtrSize( p->vBoxes );
    Vec_PtrPush( p->vBoxes, pBox );
    pBox->pDelayTable = pDelayTable;
    pBox->nInputs  = nIns;
    pBox->nOutputs = nOuts;
    for ( i = 0; i < nIns; i++ )
    {
Alan Mishchenko committed
595
        assert( firstIn+i < p->nCos );
Alan Mishchenko committed
596
        pBox->Inouts[i] = firstIn+i;
Alan Mishchenko committed
597 598
        p->pCos[firstIn+i].iObj2Box = pBox->iBox;
        p->pCos[firstIn+i].iObj2Num = i;
Alan Mishchenko committed
599 600 601
    }
    for ( i = 0; i < nOuts; i++ )
    {
Alan Mishchenko committed
602
        assert( firstOut+i < p->nCis );
Alan Mishchenko committed
603
        pBox->Inouts[nIns+i] = firstOut+i;
Alan Mishchenko committed
604 605
        p->pCis[firstOut+i].iObj2Box = pBox->iBox;
        p->pCis[firstOut+i].iObj2Num = i;
Alan Mishchenko committed
606
    }
607 608
//    if ( pBox->iBox < 50 )
//    printf( "%4d  %4d  %4d  %4d  \n", firstIn, nIns, firstOut, nOuts );
Alan Mishchenko committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
}



/**Function*************************************************************

  Synopsis    [Increments the trav ID of the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManIncrementTravId( Tim_Man_t * p )
{
Alan Mishchenko committed
626 627 628 629
    int i;
    if ( p->nTravIds >= (1<<30)-1 )
    {
        p->nTravIds = 0;
Alan Mishchenko committed
630 631 632 633
        for ( i = 0; i < p->nCis; i++ )
            p->pCis[i].TravId = 0;
        for ( i = 0; i < p->nCos; i++ )
            p->pCos[i].TravId = 0;
Alan Mishchenko committed
634 635
    }
    assert( p->nTravIds < (1<<30)-1 );
Alan Mishchenko committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649
    p->nTravIds++;
}

/**Function*************************************************************

  Synopsis    [Initializes arrival time of the PI.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
650
void Tim_ManInitCiArrival( Tim_Man_t * p, int iCi, float Delay )
Alan Mishchenko committed
651
{
Alan Mishchenko committed
652 653
    assert( iCi < p->nCis );
    p->pCis[iCi].timeArr = Delay;
Alan Mishchenko committed
654 655 656 657 658 659 660 661 662 663 664 665 666
}

/**Function*************************************************************

  Synopsis    [Initializes required time of the PO.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
667
void Tim_ManInitCoRequired( Tim_Man_t * p, int iCo, float Delay )
Alan Mishchenko committed
668
{
Alan Mishchenko committed
669 670
    assert( iCo < p->nCos );
    p->pCos[iCo].timeReq = Delay;
Alan Mishchenko committed
671 672 673 674 675 676 677 678 679 680 681 682 683
}

/**Function*************************************************************

  Synopsis    [Updates required time of the PO.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
684
void Tim_ManSetCoArrival( Tim_Man_t * p, int iCo, float Delay )
Alan Mishchenko committed
685
{
Alan Mishchenko committed
686 687 688 689
    assert( iCo < p->nCos );
    assert( !p->fUseTravId || p->pCos[iCo].TravId != p->nTravIds );
    p->pCos[iCo].timeArr = Delay;
    p->pCos[iCo].TravId = p->nTravIds;
Alan Mishchenko committed
690 691 692 693 694 695 696 697 698 699 700 701 702
}

/**Function*************************************************************

  Synopsis    [Updates arrival time of the PI.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
703
void Tim_ManSetCiRequired( Tim_Man_t * p, int iCi, float Delay )
Alan Mishchenko committed
704
{
Alan Mishchenko committed
705 706 707 708
    assert( iCi < p->nCis );
    assert( !p->fUseTravId || p->pCis[iCi].TravId != p->nTravIds );
    p->pCis[iCi].timeReq = Delay;
    p->pCis[iCi].TravId = p->nTravIds;
Alan Mishchenko committed
709 710 711 712 713 714 715 716 717 718 719 720 721
}

/**Function*************************************************************

  Synopsis    [Updates required time of the PO.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
722
void Tim_ManSetCoRequired( Tim_Man_t * p, int iCo, float Delay )
Alan Mishchenko committed
723
{
Alan Mishchenko committed
724 725 726 727
    assert( iCo < p->nCos );
    assert( !p->fUseTravId || p->pCos[iCo].TravId != p->nTravIds );
    p->pCos[iCo].timeReq = Delay;
    p->pCos[iCo].TravId = p->nTravIds;
Alan Mishchenko committed
728 729 730 731 732
}

/**Function*************************************************************

  Synopsis    [Sets the correct required times for all POs.]
Alan Mishchenko committed
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManSetCiArrivalAll( Tim_Man_t * p, float Delay )
{
    Tim_Obj_t * pObj;
    int i;
    Tim_ManForEachCi( p, pObj, i )
        Tim_ManInitCiArrival( p, i, Delay );
}

/**Function*************************************************************

  Synopsis    [Sets the correct required times for all POs.]
Alan Mishchenko committed
752 753 754 755 756 757 758 759

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
760
void Tim_ManSetCoRequiredAll( Tim_Man_t * p, float Delay )
Alan Mishchenko committed
761 762 763
{
    Tim_Obj_t * pObj;
    int i;
Alan Mishchenko committed
764
    Tim_ManForEachCo( p, pObj, i )
765
    {
Alan Mishchenko committed
766
        Tim_ManSetCoRequired( p, i, Delay );
767 768
//printf( "%d ", i );
    }
Alan Mishchenko committed
769 770 771 772 773 774 775 776 777 778 779 780 781 782
}


/**Function*************************************************************

  Synopsis    [Returns PI arrival time.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
783
float Tim_ManGetCiArrival( Tim_Man_t * p, int iCi )
Alan Mishchenko committed
784 785 786 787 788 789
{
    Tim_Box_t * pBox;
    Tim_Obj_t * pObjThis, * pObj, * pObjRes;
    float * pDelays, DelayBest;
    int i, k;
    // consider the already processed PI
Alan Mishchenko committed
790
    pObjThis = Tim_ManCi( p, iCi );
Alan Mishchenko committed
791
    if ( p->fUseTravId && pObjThis->TravId == p->nTravIds )
Alan Mishchenko committed
792 793 794
        return pObjThis->timeArr;
    pObjThis->TravId = p->nTravIds;
    // consider the main PI
Alan Mishchenko committed
795
    pBox = Tim_ManCiBox( p, iCi );
Alan Mishchenko committed
796 797 798 799 800
    if ( pBox == NULL )
        return pObjThis->timeArr;
    // update box timing
    pBox->TravId = p->nTravIds;
    // get the arrival times of the inputs of the box (POs)
Alan Mishchenko committed
801
    if ( p->fUseTravId )
Alan Mishchenko committed
802 803
    Tim_ManBoxForEachInput( p, pBox, pObj, i )
        if ( pObj->TravId != p->nTravIds )
Alan Mishchenko committed
804
            printf( "Tim_ManGetCiArrival(): Input arrival times of the box are not up to date!\n" );
Alan Mishchenko committed
805 806 807 808
    // compute the arrival times for each output of the box (PIs)
    Tim_ManBoxForEachOutput( p, pBox, pObjRes, i )
    {
        pDelays = pBox->pDelayTable + i * pBox->nInputs;
Alan Mishchenko committed
809
        DelayBest = -TIM_ETERNITY;
Alan Mishchenko committed
810
        Tim_ManBoxForEachInput( p, pBox, pObj, k )
811
            DelayBest = Abc_MaxInt( DelayBest, pObj->timeArr + pDelays[k] );
Alan Mishchenko committed
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
        pObjRes->timeArr = DelayBest;
        pObjRes->TravId = p->nTravIds;
    }
    return pObjThis->timeArr;
}

/**Function*************************************************************

  Synopsis    [Returns PO required time.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
829
float Tim_ManGetCoRequired( Tim_Man_t * p, int iCo )
Alan Mishchenko committed
830 831 832 833 834 835
{
    Tim_Box_t * pBox;
    Tim_Obj_t * pObjThis, * pObj, * pObjRes;
    float * pDelays, DelayBest;
    int i, k;
    // consider the already processed PO
Alan Mishchenko committed
836
    pObjThis = Tim_ManCo( p, iCo );
Alan Mishchenko committed
837
    if ( p->fUseTravId && pObjThis->TravId == p->nTravIds )
Alan Mishchenko committed
838 839 840
        return pObjThis->timeReq;
    pObjThis->TravId = p->nTravIds;
    // consider the main PO
Alan Mishchenko committed
841
    pBox = Tim_ManCoBox( p, iCo );
Alan Mishchenko committed
842 843 844 845
    if ( pBox == NULL )
        return pObjThis->timeReq;
    // update box timing
    pBox->TravId = p->nTravIds;
Alan Mishchenko committed
846 847
    // get the required times of the outputs of the box (PIs)
    if ( p->fUseTravId )
Alan Mishchenko committed
848 849
    Tim_ManBoxForEachOutput( p, pBox, pObj, i )
        if ( pObj->TravId != p->nTravIds )
Alan Mishchenko committed
850
            printf( "Tim_ManGetCoRequired(): Output required times of output %d the box %d are not up to date!\n", i, pBox->iBox );
Alan Mishchenko committed
851
    // compute the required times for each input of the box (POs)
Alan Mishchenko committed
852 853
    Tim_ManBoxForEachInput( p, pBox, pObjRes, i )
    {
Alan Mishchenko committed
854
        DelayBest = TIM_ETERNITY;
Alan Mishchenko committed
855 856 857
        Tim_ManBoxForEachOutput( p, pBox, pObj, k )
        {
            pDelays = pBox->pDelayTable + k * pBox->nInputs;
Alan Mishchenko committed
858
            DelayBest = Abc_MinFloat( DelayBest, pObj->timeReq - pDelays[i] );
Alan Mishchenko committed
859 860 861 862 863 864 865
        }
        pObjRes->timeReq = DelayBest;
        pObjRes->TravId = p->nTravIds;
    }
    return pObjThis->timeReq;
}

Alan Mishchenko committed
866 867 868 869 870 871 872 873 874 875 876 877 878
/**Function*************************************************************

  Synopsis    [Returns the box number for the given input.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Tim_ManBoxForCi( Tim_Man_t * p, int iCi )
{
Alan Mishchenko committed
879
    if ( iCi >= p->nCis )
Alan Mishchenko committed
880
        return -1;
Alan Mishchenko committed
881
    return p->pCis[iCi].iObj2Box;
Alan Mishchenko committed
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
}

/**Function*************************************************************

  Synopsis    [Returns the box number for the given output.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Tim_ManBoxForCo( Tim_Man_t * p, int iCo )
{
Alan Mishchenko committed
897
    if ( iCo >= p->nCos )
Alan Mishchenko committed
898
        return -1;
Alan Mishchenko committed
899
    return p->pCos[iCo].iObj2Box;
Alan Mishchenko committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
}

/**Function*************************************************************

  Synopsis    [Returns the first input of the box.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Tim_ManBoxInputFirst( Tim_Man_t * p, int iBox )
{
915
    Tim_Box_t * pBox = (Tim_Box_t *)Vec_PtrEntry( p->vBoxes, iBox );
Alan Mishchenko committed
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
    return pBox->Inouts[0];
}

/**Function*************************************************************

  Synopsis    [Returns the first input of the box.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Tim_ManBoxOutputFirst( Tim_Man_t * p, int iBox )
{
932
    Tim_Box_t * pBox = (Tim_Box_t *)Vec_PtrEntry( p->vBoxes, iBox );
Alan Mishchenko committed
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
    return pBox->Inouts[pBox->nInputs];
}

/**Function*************************************************************

  Synopsis    [Returns the first input of the box.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Tim_ManBoxInputNum( Tim_Man_t * p, int iBox )
{
949
    Tim_Box_t * pBox = (Tim_Box_t *)Vec_PtrEntry( p->vBoxes, iBox );
Alan Mishchenko committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
    return pBox->nInputs;
}

/**Function*************************************************************

  Synopsis    [Returns the first input of the box.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Tim_ManBoxOutputNum( Tim_Man_t * p, int iBox )
{
966
    Tim_Box_t * pBox = (Tim_Box_t *)Vec_PtrEntry( p->vBoxes, iBox );
Alan Mishchenko committed
967 968 969
    return pBox->nOutputs;
}

970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Tim_ManChangeForAdders( Tim_Man_t * p )
{
    Tim_Box_t * pBox;
    int i;
    Tim_ManForEachBox( p, pBox, i )
        pBox->pDelayTable[0] = 0.0;
}

Alan Mishchenko committed
989 990 991 992 993 994

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


995 996
ABC_NAMESPACE_IMPL_END