VIS

src/sim/simUtil.c

Go to the documentation of this file.
00001 
00032 #include "simInt.h"
00033 #include <sys/time.h>
00034 
00035 static char rcsid[] UNUSED = "$Id: simUtil.c,v 1.8 2005/04/26 19:10:09 jinh Exp $";
00036 
00039 /*---------------------------------------------------------------------------*/
00040 /* Static function prototypes                                                */
00041 /*---------------------------------------------------------------------------*/
00042 
00043 
00047 /*---------------------------------------------------------------------------*/
00048 /* Definition of exported functions                                          */
00049 /*---------------------------------------------------------------------------*/
00050 
00062 st_table *
00063 Sim_NetworkBuildNodeToMvfTable(
00064   Ntk_Network_t *network,
00065   array_t       *nodesArray,
00066   int            internalPartitionHead,
00067   int            nextStateHead)
00068 {
00069   array_t   *rootsName;
00070   array_t   *leavesMddId;
00071   array_t   *mvfArray;
00072   st_table  *nodeToMvfTable;
00073 
00074   SimNodesArrayBuildRootsAndLeaves(network, nodesArray, internalPartitionHead, nextStateHead, &rootsName, &leavesMddId);
00075   mvfArray = Part_PartitionBuildFunctions(Part_NetworkReadPartition(network),
00076                                           rootsName, leavesMddId, NIL(mdd_t));
00077 
00078   array_free(leavesMddId);
00079   array_free(rootsName);
00080   nodeToMvfTable = SimNodesArrayBuildNodeToMvfTable(nodesArray, internalPartitionHead, mvfArray);
00081   array_free(mvfArray);
00082   return(nodeToMvfTable);
00083 }
00084 
00085 /*---------------------------------------------------------------------------*/
00086 /* Definition of internal functions                                          */
00087 /*---------------------------------------------------------------------------*/
00088   
00101 int
00102 SimNodeReadValueCode(
00103   Ntk_Node_t * node,
00104   char       * value)
00105 {
00106   int             index;
00107   Var_Variable_t *var = Ntk_NodeReadVariable(node);
00108 
00109   if (Var_VariableTestIsSymbolic(var)) {
00110     return(Var_VariableReadIndexFromSymbolicValue(var, value));
00111   }
00112 
00113   /* Enumerative or binary variable */
00114   else {
00115     index = atoi(value);
00116     if (index >= Var_VariableReadNumValues(var) || index < 0) {
00117       return(-1);
00118     }
00119     else {
00120       return(index);
00121     }
00122   }
00123 }
00124 
00136 boolean
00137 SimTestPartInTermsOfCI(
00138   Sim_Sim_t *sim
00139   )
00140 {
00141   int j, k, value, mddId;
00142   Ntk_Node_t *node, *supportNode;
00143   Mvf_Function_t *mvFunction;
00144   array_t *mddIdArray;
00145   
00146   /* if there are no internal nodes, return FALSE. This allows
00147      simulation to proceed when partition method inout is used.
00148      */
00149   if(sim->internalPartitionHead == sim->nextStateHead){
00150     return FALSE;
00151   }
00152   
00153   for (j = sim->nextStateHead; j < array_n(sim->nodesArray); j++) {
00154     node =  array_fetch(Ntk_Node_t *, sim->nodesArray, j);
00155     (void) st_lookup(sim->nodeToMvfTable, (char *) node, &mvFunction); 
00156     mddIdArray = Mvf_FunctionComputeSupport(mvFunction,
00157                             Ntk_NetworkReadMddManager(sim->network), &value);
00158     if(mddIdArray != NIL(array_t)){
00159       for(k = 0; k < array_n(mddIdArray); k++){
00160         mddId = array_fetch(int, mddIdArray, k);
00161         supportNode = Ntk_NetworkFindNodeByMddId(sim->network, mddId);
00162         if(!Ntk_NodeTestIsCombInput(supportNode)){
00163           array_free(mddIdArray);
00164           return FALSE;
00165         }
00166       }
00167       array_free(mddIdArray);
00168     }/* if array not nil */
00169   }/* for all nodes */
00170   
00171   return TRUE;
00172 }
00173 
00184 char *
00185 SimInteger2ASCII(
00186   int  number)
00187 {
00188   char * str = ALLOC(char, 21);
00189 
00190   (void) sprintf(str, "%d", number);
00191   return(str);
00192 }
00193   
00194 
00206 void
00207 SimStringPrint(
00208   FILE * fp,
00209   char * string,
00210   int  len)
00211 {
00212   int i;
00213   
00214   fprintf(fp, "%s", string);
00215   for(i = strlen(string); i < len; i++) {
00216     fprintf(fp, " ");
00217   }
00218   fprintf(fp, " ");
00219 }
00220   
00233 array_t *
00234 SimSimInitDataFormat(
00235   Sim_Sim_t * sim)
00236 {
00237   int           i, j;
00238   int           size;
00239   int           maxSize;
00240   int           numValue;
00241   boolean       symbolic;
00242   Ntk_Node_t   *node;
00243   Var_Variable_t *var;
00244   array_t      *formatArray = array_alloc(int, 0);
00245   
00246   for (j = 0; j < array_n(sim->nodesArray); j++) {
00247     node     = array_fetch(Ntk_Node_t *, sim->nodesArray, j);
00248     var      = Ntk_NodeReadVariable(node);
00249     symbolic = Var_VariableTestIsSymbolic(var);
00250     numValue = Var_VariableReadNumValues(var);
00251     maxSize = 0;
00252     if (symbolic) {
00253       for (i = 0; i < numValue; i++) {
00254         size = strlen(Var_VariableReadSymbolicValueFromIndex(var, i));
00255         if (size > maxSize) {
00256           maxSize = size;
00257         }
00258       }
00259     }
00260     else { /* Binary or enumertive type */
00261       while(numValue != 0) {
00262         maxSize++;
00263         numValue = numValue/10;
00264       }
00265     }
00266     array_insert_last(int, formatArray, maxSize);
00267   }
00268   return(formatArray);
00269 }
00270 
00283 void
00284 SimSimVectorFillCurrentState(
00285   Sim_Sim_t * sim /* sim data-structure */,
00286   int  n /* index of nextState in the vectorArray */)
00287 {
00288   int          i;
00289   array_t     *currentVector  = array_fetch(array_t *, sim->vectorArray, n-1);
00290   array_t     *nextVector     = array_fetch(array_t *, sim->vectorArray, n);
00291   
00292   for (i = sim->nextStateHead; i < sim->outputHead; i++) {
00293     /* We assume nextVector contains only the inputs : insert_last is convenient */
00294     int value = array_fetch(int, currentVector, i);
00295     array_insert_last(int, nextVector, value);
00296   }
00297 }
00298  
00314 int
00315 SimNodeComputeRandomValue(
00316   Ntk_Node_t * node,
00317   Sim_PseudoSrc  pseudoInputSource)
00318 {
00319   int value = 0; /* initialize so that lint doesn't complain */ 
00320 
00321   if (Ntk_NodeTestIsPseudoInput(node)) {
00322     Tbl_Entry_t *entry;
00323     int          rowNum;
00324     int          colNum;
00325     int          rowChoice;
00326     Tbl_Table_t *table     = Ntk_NodeReadTable(node);
00327     int          outIndex  = Ntk_NodeReadOutputIndex(node);
00328 
00329     /* Decide which row to take value from. */
00330     if (pseudoInputSource == Sim_First_c) {
00331       rowChoice = 0;
00332     }
00333     else {
00334       int numRows = Tbl_TableReadNumRows(table);
00335       rowChoice = ((int) util_random()) % numRows;
00336     }
00337     
00338     Tbl_TableForEachOutputEntry(table, rowNum, colNum, entry) {
00339       if ((colNum == outIndex) && (rowNum == rowChoice)) {
00340         Tbl_Range_t *range;
00341         lsGen        lsGen;
00342         int          tempValue;
00343         int          entryChoice;
00344         int          i = 0;
00345 
00346         /* We are now in rowChoice. Decide which value of entry to take. */
00347         if (pseudoInputSource == Sim_First_c) {
00348           entryChoice = 0;
00349         }
00350         else {
00351           int numValues = Tbl_EntryReadNumValues(entry);
00352           entryChoice = ((int) util_random()) % numValues;
00353         }
00354     
00355         Tbl_EntryForEachValue(entry, tempValue, lsGen, range) {
00356           if(entryChoice == i) {
00357             value = tempValue;
00358             /*
00359              * We could break out of the two FOR loops to avoid unnecessary
00360              * work. However, the inner loop has an lsGen that must be freed
00361              * if the loop is broken early, and I couldn't get lsFinish to be
00362              * called correctly.  Also, the table shouldn't be big to worry
00363              * about it.
00364              */
00365           }
00366           i++;
00367         }
00368       }
00369     }
00370   }
00371   else {
00372     Var_Variable_t *var = Ntk_NodeReadVariable(node);
00373 
00374     value = ((int) util_random()) % Var_VariableReadNumValues(var);
00375   }
00376 
00377   return value;
00378 }
00379 
00380 
00393 mdd_t *
00394 SimSimVectorBuildMdd(
00395   Sim_Sim_t * sim,
00396   array_t * vector,
00397   array_t * partitionVector)
00398 {
00399   int            i;
00400   int            value;
00401   mdd_t         *mddMinterm;
00402   Ntk_Node_t    *node;
00403   mdd_t         *literalMdd;
00404   mdd_t         *tmpMdd;
00405   Ntk_Network_t *network = sim->network;
00406   mdd_manager   *mddManager = Ntk_NetworkReadMddManager(network);
00407 
00408   mddMinterm = mdd_one(mddManager);
00409   for(i = 0; i < sim->internalPartitionHead; i++) { /* For every combinational input */
00410 
00411     node     = array_fetch(Ntk_Node_t *, sim->nodesArray, i);
00412     value    = array_fetch(int, vector, i);
00413 
00414     /* LiteralMdd will represent the mdd of  "Node-variable = value" */
00415     literalMdd = mdd_eq_c(mddManager, Ntk_NodeReadMddId(node), value);
00416     tmpMdd = mdd_and(literalMdd, mddMinterm, 1, 1);
00417     mdd_free(mddMinterm);
00418     mdd_free(literalMdd);
00419     mddMinterm = tmpMdd;
00420   }
00421 
00422   for(i = 0; i < array_n(partitionVector); i++) { /* For every internal Partition node */
00423 
00424     node     = array_fetch(Ntk_Node_t *, sim->nodesArray, i + sim->internalPartitionHead);
00425     value    = array_fetch(int, partitionVector, i);
00426 
00427     /* LiteralMdd will represent the mdd of  "Node-variable = value" */
00428     literalMdd = mdd_eq_c(mddManager, Ntk_NodeReadMddId(node), value);
00429     tmpMdd = mdd_and(literalMdd, mddMinterm, 1, 1);
00430     mdd_free(mddMinterm);
00431     mdd_free(literalMdd);
00432     mddMinterm = tmpMdd;
00433   }
00434 
00435   return(mddMinterm);
00436 }
00437 
00448 void
00449 SimAppendErrorMsg(
00450   char * str1,
00451   char * str2,
00452   char * str3)
00453 {
00454   error_append(str1);
00455   error_append(str2);
00456   error_append(str3);
00457 }
00458 
00468 int
00469 SimComputeRandomInteger(void)
00470 {
00471   struct timeval tp;
00472   struct timezone tzp;
00473 
00474   if ((int) gettimeofday(&tp,&tzp) == -1) {
00475     fprintf (vis_stderr, "sim : Error while calling gettimeofday.\n");
00476     exit(-1);
00477   }
00478   return((int) (tp.tv_sec));
00479 }
00480 
00495 void
00496 SimNodesArrayBuildRootsAndLeaves(
00497   Ntk_Network_t *network,                                
00498   array_t *  nodesArray,
00499   int        internalPartitionHead,
00500   int        nextStateHead,
00501   array_t ** roots,
00502   array_t ** leaves)
00503 {
00504   int         i;
00505   Ntk_Node_t *node;
00506 
00507   *roots  = array_alloc(char *, 0);
00508   *leaves = array_alloc(int, 0);
00509 
00510   for(i = 0; i < nextStateHead; i++) {
00511     node = array_fetch(Ntk_Node_t *, nodesArray, i);
00512     array_insert_last(int, *leaves, Ntk_NodeReadMddId(node));
00513   }
00514 
00515   for(i = internalPartitionHead; i < array_n(nodesArray); i++) {
00516     node = array_fetch(Ntk_Node_t *, nodesArray, i);
00517     array_insert_last(char *, *roots, Ntk_NodeReadName(node));
00518   }
00519 
00520 }
00521 
00532 st_table *
00533 SimNodesArrayBuildNodeToMvfTable(
00534   array_t  * nodesArray,
00535   int        internalPartitionHead,
00536   array_t  * mvfArray)
00537 {
00538   int         i;
00539   Ntk_Node_t *node;
00540   array_t    *mvf;
00541   st_table   *nodeToMvfTable = st_init_table(st_ptrcmp, st_ptrhash);
00542   
00543   for(i = internalPartitionHead; i < array_n(nodesArray); i++) {
00544     node = array_fetch(Ntk_Node_t *, nodesArray, i);
00545     mvf  = array_fetch(Mvf_Function_t *, mvfArray, i - internalPartitionHead);
00546     st_insert(nodeToMvfTable, (char *) node, (char *) mvf);
00547   }
00548   return(nodeToMvfTable);
00549 }
00550 
00551 /*---------------------------------------------------------------------------*/
00552 /* Definition of static functions                                            */
00553 /*---------------------------------------------------------------------------*/
00554   
00555   
00556 
00557 
00558 
00559 
00560 
00561 
00562 
00563 
00564 
00565 
00566 
00567 
00568 
00569 
00570 
00571 
00572 
00573 
00574 
00575 
00576 
00577 
00578 
00579 
00580 
00581 
00582 
00583 
00584 
00585 
00586 
00587 
00588 
00589 
00590 
00591 
00592 
00593 
00594 
00595 
00596 
00597 
00598 
00599 
00600 
00601 
00602 
00603 
00604