ifSeq.c 11.5 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
/**CFile****************************************************************

  FileName    [ifSeq.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [FPGA mapping based on priority cuts.]

  Synopsis    [Sequential mapping.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - November 21, 2006.]

  Revision    [$Id: ifSeq.c,v 1.00 2006/11/21 00:00:00 alanmi Exp $]

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

#include "if.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

30
extern abctime s_MappingTime;
Alan Mishchenko committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

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

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

  Synopsis    [Prepares for sequential mapping by linking the latches.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_ManPrepareMappingSeq( If_Man_t * p )
{
    If_Obj_t * pObjLi, * pObjLo;
    int i;

    // link the latch outputs (CIs) directly to the drivers of latch inputs (COs)
53
    for ( i = 0; i < p->pPars->nLatchesCi; i++ )
Alan Mishchenko committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    {
        pObjLi = If_ManLi( p, i );
        pObjLo = If_ManLo( p, i );
        pObjLo->pFanin0 = If_ObjFanin0( pObjLi );
        pObjLo->fCompl0 = If_ObjFaninC0( pObjLi );
    }
}

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

  Synopsis    [Collects latches in the topological order.]

  Description []
               
  SideEffects []

  SeeAlso     []
 
***********************************************************************/
void If_ManCollectLatches_rec( If_Obj_t * pObj, Vec_Ptr_t * vLatches )
{
    if ( !If_ObjIsLatch(pObj) )
        return;
    if ( pObj->fMark )
        return;
    pObj->fMark = 1;
    If_ManCollectLatches_rec( pObj->pFanin0, vLatches );
    Vec_PtrPush( vLatches, pObj );
}

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

  Synopsis    [Collects latches in the topological order.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * If_ManCollectLatches( If_Man_t * p )
{
    Vec_Ptr_t * vLatches;
    If_Obj_t * pObj;
    int i;
    // collect latches 
101
    vLatches = Vec_PtrAlloc( p->pPars->nLatchesCi );
Alan Mishchenko committed
102 103 104
    If_ManForEachLatchOutput( p, pObj, i )
        If_ManCollectLatches_rec( pObj, vLatches );
    // clean marks
105
    Vec_PtrForEachEntry( If_Obj_t *, vLatches, pObj, i )
Alan Mishchenko committed
106
        pObj->fMark = 0;
107
    assert( Vec_PtrSize(vLatches) == p->pPars->nLatchesCi );
Alan Mishchenko committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    return vLatches;
}

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

  Synopsis    [Performs one pass of l-value computation over all nodes.]

  Description [Experimentally it was found that checking POs changes
  is not enough to detect the convergence of l-values in the network.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int If_ManPerformMappingRoundSeq( If_Man_t * p, int nIter )
{
    If_Obj_t * pObj;
126
    int i;
127
    abctime clk = Abc_Clock();
Alan Mishchenko committed
128 129 130
    int fVeryVerbose = 0;
    int fChange = 0;

Alan Mishchenko committed
131 132 133
    if ( nIter == 1 )
    {
        // if some latches depend on PIs, update their values
134
        Vec_PtrForEachEntry( If_Obj_t *, p->vLatchOrder, pObj, i )
Alan Mishchenko committed
135 136 137 138 139 140
        {
            If_ObjSetLValue( pObj, If_ObjLValue(If_ObjFanin0(pObj)) - p->Period );
            If_ObjSetArrTime( pObj, If_ObjLValue(pObj) );
        }
    }

Alan Mishchenko committed
141 142 143 144 145 146 147 148 149 150
    // map the internal nodes
    p->nCutsMerged = 0;
    If_ManForEachNode( p, pObj, i )
    {
        If_ObjPerformMappingAnd( p, pObj, 0, 0 );
        if ( pObj->fRepr )
            If_ObjPerformMappingChoice( p, pObj, 0, 0 );
    }

    // postprocess the mapping
151
//Abc_Print( 1, "Itereation %d: \n", nIter );
Alan Mishchenko committed
152 153 154 155 156 157 158 159
    If_ManForEachNode( p, pObj, i )
    {
        // update the LValues stored separately
        if ( If_ObjLValue(pObj) < If_ObjCutBest(pObj)->Delay - p->fEpsilon )
        {
            If_ObjSetLValue( pObj, If_ObjCutBest(pObj)->Delay );
            fChange = 1;
        }
160
//Abc_Print( 1, "%d ", (int)If_ObjLValue(pObj) );
Alan Mishchenko committed
161 162 163 164
        // reset the visit counters
        assert( pObj->nVisits == 0 );
        pObj->nVisits = pObj->nVisitsCopy;
    }
165
//Abc_Print( 1, "\n" );
Alan Mishchenko committed
166 167

    // propagate LValues over the registers
168
    Vec_PtrForEachEntry( If_Obj_t *, p->vLatchOrder, pObj, i )
Alan Mishchenko committed
169 170 171 172 173 174
    {
        If_ObjSetLValue( pObj, If_ObjLValue(If_ObjFanin0(pObj)) - p->Period );
        If_ObjSetArrTime( pObj, If_ObjLValue(pObj) );
    }

    // compute area and delay
Alan Mishchenko committed
175
    If_ManMarkMapping( p );
Alan Mishchenko committed
176 177 178
    if ( fVeryVerbose )
    {
        p->RequiredGlo = If_ManDelayMax( p, 1 );
Alan Mishchenko committed
179
//        p->AreaGlo = If_ManScanMapping(p);
180
        Abc_Print( 1, "S%d:  Fi = %6.2f. Del = %6.2f. Area = %8.2f. Cuts = %8d. ", 
Alan Mishchenko committed
181
             nIter, (float)p->Period, p->RequiredGlo, p->AreaGlo, p->nCutsMerged );
182
        Abc_PrintTime( 1, "T", Abc_Clock() - clk );
Alan Mishchenko committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    }
    return fChange;
}

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

  Synopsis    [Returns 1 if retiming with this clock period is feasible.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int If_ManBinarySearchPeriod( If_Man_t * p )
{
    If_Obj_t * pObj;
    int i, c, fConverged;
202
//    int fResetRefs = 0;
Alan Mishchenko committed
203 204 205 206 207
    p->nAttempts++;

    // reset initial LValues (PIs to 0; others to -inf)
    If_ManForEachObj( p, pObj, i )
    {
Alan Mishchenko committed
208 209
        If_ObjSetLValue( pObj, (float)-IF_INFINITY );
        If_ObjSetArrTime( pObj, (float)-IF_INFINITY );
Alan Mishchenko committed
210 211 212 213
        // undo any previous mapping, except for CIs
        if ( If_ObjIsAnd(pObj) )
            If_ObjCutBest(pObj)->nLeaves = 0;
    }
Alan Mishchenko committed
214 215 216 217 218 219 220 221 222
    pObj = If_ManConst1( p );
    If_ObjSetLValue( pObj, (float)0.0 );
    If_ObjSetArrTime( pObj, (float)0.0 );
    If_ManForEachPi( p, pObj, i )
    {
        pObj = If_ManCi( p, i );
        If_ObjSetLValue( pObj, (float)0.0 );
        If_ObjSetArrTime( pObj, (float)0.0 );
    }
Alan Mishchenko committed
223 224 225 226 227 228 229 230 231 232 233 234

    // update all values iteratively
    fConverged = 0;
    for ( c = 1; c <= p->nMaxIters; c++ )
    {
        if ( !If_ManPerformMappingRoundSeq( p, c ) )
        {
            p->RequiredGlo = If_ManDelayMax( p, 1 );
            fConverged = 1;
            break;
        }
        p->RequiredGlo = If_ManDelayMax( p, 1 );
235
//Abc_Print( 1, "Global = %d \n", (int)p->RequiredGlo );
Alan Mishchenko committed
236 237 238 239 240
        if ( p->RequiredGlo > p->Period + p->fEpsilon )
            break; 
    }

    // report the results
Alan Mishchenko committed
241
    If_ManMarkMapping( p );
Alan Mishchenko committed
242 243
    if ( p->pPars->fVerbose )
    {
Alan Mishchenko committed
244
//        p->AreaGlo = If_ManScanMapping(p);
245
        Abc_Print( 1, "Attempt = %2d.  Iters = %3d.  Area = %10.2f.  Fi = %6.2f.  ", p->nAttempts, c, p->AreaGlo, (float)p->Period );
Alan Mishchenko committed
246
        if ( fConverged )
247
            Abc_Print( 1, "  Feasible" );
Alan Mishchenko committed
248
        else if ( c > p->nMaxIters )
249
            Abc_Print( 1, "Infeasible (timeout)" );
Alan Mishchenko committed
250
        else
251 252
            Abc_Print( 1, "Infeasible" );
        Abc_Print( 1, "\n" );
Alan Mishchenko committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    }
    return fConverged;
}


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

  Synopsis    [Performs binary search for the optimal clock period.]

  Description [Assumes that FiMin is infeasible while FiMax is feasible.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int If_ManBinarySearch_rec( If_Man_t * p, int FiMin, int FiMax )
{
    assert( FiMin < FiMax );
    if ( FiMin + 1 == FiMax )
        return FiMax;
    // compute the median
    p->Period = FiMin + (FiMax - FiMin)/2;
    if ( If_ManBinarySearchPeriod( p ) )
        return If_ManBinarySearch_rec( p, FiMin, p->Period ); // Median is feasible
    else 
        return If_ManBinarySearch_rec( p, p->Period, FiMax ); // Median is infeasible
}

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

  Synopsis    [Performs sequential mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_ManPerformMappingSeqPost( If_Man_t * p )
{
    If_Obj_t * pObjLi, * pObjLo, * pObj;
    int i;
297
    assert( 0 );
Alan Mishchenko committed
298 299 300 301 302 303 304 305

    // set arrival times
    assert( p->pPars->pTimesArr != NULL );
    If_ManForEachLatchOutput( p, pObjLo, i )
        p->pPars->pTimesArr[i] = If_ObjLValue(pObjLo);

    // set the required times
    assert( p->pPars->pTimesReq == NULL );
Alan Mishchenko committed
306
    p->pPars->pTimesReq = ABC_ALLOC( float, If_ManCoNum(p) );
Alan Mishchenko committed
307 308 309
    If_ManForEachPo( p, pObj, i )
    {
        p->pPars->pTimesReq[i] = p->RequiredGlo2;
310
//        Abc_Print( 1, "Out %3d : %2d   \n", i, (int)p->pPars->pTimesReq[i] );
Alan Mishchenko committed
311 312 313 314
    }
    If_ManForEachLatchInput( p, pObjLi, i )
    {
        p->pPars->pTimesReq[i] = If_ObjLValue(If_ObjFanin0(pObjLi));
315
//        Abc_Print( 1, "Out %3d : %2d   \n", i, (int)p->pPars->pTimesReq[i] );
Alan Mishchenko committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    }

    // undo previous mapping
    If_ManForEachObj( p, pObj, i )
        if ( If_ObjIsAnd(pObj) )
            If_ObjCutBest(pObj)->nLeaves = 0;

    // map again combinationally
    p->pPars->fSeqMap = 0;
    If_ManPerformMappingComb( p );
    p->pPars->fSeqMap = 1;
}

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

  Synopsis    [Performs sequential mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int If_ManPerformMappingSeq( If_Man_t * p )
{
342
    abctime clkTotal = Abc_Clock();
Alan Mishchenko committed
343 344 345 346 347 348
    int PeriodBest;

    p->SortMode = 0;

    // perform combinational mapping to get the upper bound on the clock period
    If_ManPerformMappingRound( p, 1, 0, 0, NULL );
Alan Mishchenko committed
349
    p->RequiredGlo  = If_ManDelayMax( p, 0 );
Alan Mishchenko committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    p->RequiredGlo2 = p->RequiredGlo;

    // set direct linking of latches with their inputs
    If_ManPrepareMappingSeq( p );

    // collect latches
    p->vLatchOrder = If_ManCollectLatches( p );

    // set parameters
    p->nCutsUsed = p->pPars->nCutsMax;
    p->nAttempts = 0;
    p->nMaxIters = 50;
    p->Period    = (int)p->RequiredGlo;

    // make sure the clock period works
    if ( !If_ManBinarySearchPeriod( p ) )
    {
367
        Abc_Print( 1, "If_ManPerformMappingSeq(): The upper bound on the clock period cannot be computed.\n" );
Alan Mishchenko committed
368 369 370 371 372 373 374 375 376 377 378 379
        return 0;
    }

    // perform binary search
    PeriodBest = If_ManBinarySearch_rec( p, 0, p->Period );

    // recompute the best l-values
    if ( p->Period != PeriodBest )
    {
        p->Period = PeriodBest;
        if ( !If_ManBinarySearchPeriod( p ) )
        {
380
            Abc_Print( 1, "If_ManPerformMappingSeq(): The final clock period cannot be confirmed.\n" );
Alan Mishchenko committed
381 382 383
            return 0;
        }
    }
Alan Mishchenko committed
384
//    if ( p->pPars->fVerbose )
Alan Mishchenko committed
385
    {
386
        Abc_Print( 1, "The best clock period is %3d.  ", p->Period );
387
        Abc_PrintTime( 1, "Time", Abc_Clock() - clkTotal );
Alan Mishchenko committed
388
    }
Alan Mishchenko committed
389
    p->RequiredGlo = (float)(PeriodBest);
Alan Mishchenko committed
390 391 392

    // postprocess it using combinational mapping
    If_ManPerformMappingSeqPost( p );
393
    s_MappingTime = Abc_Clock() - clkTotal;
Alan Mishchenko committed
394 395 396 397 398 399 400 401
    return 1;
}

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


402 403
ABC_NAMESPACE_IMPL_END