VIS

src/truesim/truesimCmd.c

Go to the documentation of this file.
00001 
00042 #include "truesimInt.h"
00043 
00044 /*---------------------------------------------------------------------------*/
00045 /* Constant declarations                                                     */
00046 /*---------------------------------------------------------------------------*/
00047 
00048 #define DEFAULT_NUM_PATTERNS 1000
00049 /*---------------------------------------------------------------------------*/
00050 /* Type declarations                                                         */
00051 /*---------------------------------------------------------------------------*/
00052 
00053 
00054 /*---------------------------------------------------------------------------*/
00055 /* Structure declarations                                                    */
00056 /*---------------------------------------------------------------------------*/
00057 
00058 
00059 /*---------------------------------------------------------------------------*/
00060 /* Variable declarations                                                     */
00061 /*---------------------------------------------------------------------------*/
00062 
00063 static jmp_buf timeOutEnv;
00064 
00065 /* Global variable used within truesim package */
00066 int createdPart;
00067 int truesimRptHeader;
00068 int truesimVerbose;
00069 
00072 /*---------------------------------------------------------------------------*/
00073 /* Static function prototypes                                                */
00074 /*---------------------------------------------------------------------------*/
00075 
00076 static int CommandTruesim(Hrc_Manager_t **hmgr, int argc, char **argv);
00077 static void TimeOutHandle(void);
00078 static int TestIsNetworkMultipleValued(Ntk_Network_t *network);
00079 
00083 /*---------------------------------------------------------------------------*/
00084 /* Definition of exported functions                                          */
00085 /*---------------------------------------------------------------------------*/
00086 
00096 void
00097 Truesim_Init(void)
00098 {
00099   Cmd_CommandAdd("truesim", CommandTruesim, 0);
00100 }
00101 
00111 void
00112 Truesim_End(void)
00113 {
00114 
00115 } /* End of Truesim_End */
00116 
00117 
00118 /*---------------------------------------------------------------------------*/
00119 /* Definition of internal functions                                          */
00120 /*---------------------------------------------------------------------------*/
00121 
00122 
00123 /*---------------------------------------------------------------------------*/
00124 /* Definition of static functions                                            */
00125 /*---------------------------------------------------------------------------*/
00126 
00231 static int
00232 CommandTruesim(
00233   Hrc_Manager_t **hmgr,
00234   int argc,
00235   char **argv)
00236 {
00237   Ntk_Network_t *network = NIL(Ntk_Network_t);
00238   graph_t *partition;
00239   boolean trueDelay;
00240   int genVectors,N,status;
00241   int c; /* For scanning command line arguments */
00242   long timeOutPeriod,initialTime,finalTime;
00243   char *probFile,*simFile,*probGenFile,*dumpFile;
00244   char *delayFile;
00245   FILE *fp = NIL(FILE);;
00246   
00247   /* These are the default values. */
00248   createdPart = 0; /* =1 when partiton is created by the command */
00249   trueDelay = FALSE; /* Use zero-delay simulation by default */
00250   timeOutPeriod   = 0; /* Unlimited time */ 
00251   genVectors = -1; /* Do not generate bit-vectors for simulation */
00252   N = -1; /* Number of patterns to simulate */
00253   probFile = NIL(char); /* File with probability values for PI */
00254   simFile = NIL(char); /* Simulation bit-vector file */
00255   probGenFile = NIL(char); /* File with prob. values for PI; used during
00256                             * bit-vector generation */
00257   dumpFile = NIL(char); /* File to dump generated bit-vectors */
00258   delayFile = NIL(char); /* File with delay vaules for nodes of the
00259                           * circuit */
00260   truesimVerbose = 0; /* Verbosity level */
00261   truesimRptHeader = -1; /* Print PI/PO names while printing simulation
00262                       * results */
00263 
00264   if (bdd_get_package_name() != CUDD) {
00265     (void) fprintf(vis_stderr,
00266                    "** truesim error: The truesim package can be used only with CUDD package\n");
00267     (void) fprintf(vis_stderr,
00268                    "** truesim error: Please link with CUDD package\n");
00269     return 0;
00270   }
00271         
00272   util_getopt_reset();
00273 
00274   while((c = util_getopt(argc, argv, "D:df:g:hn:p:r:T:t:v:")) != EOF) {
00275     switch(c) {
00276     case 'D':
00277       if (!util_optarg) {
00278         (void) fprintf(vis_stderr,
00279                        "** truesim error: File to output pattern vectors not specified.\n");
00280         goto usage;
00281       }
00282       dumpFile = util_strsav(util_optarg);
00283       break;
00284     case 'd':
00285       trueDelay = TRUE;
00286       break;
00287     case 'f': 
00288       if (!util_optarg) {
00289         (void) fprintf(vis_stderr,
00290                        "** truesim error: Simulation file not specified.\n");
00291         goto usage;
00292       } else {
00293         simFile = util_strsav(util_optarg);
00294         if ((fp = Cmd_FileOpen(simFile,"r",NIL(char *),1)) == NIL(FILE)) {
00295           (void) fprintf(vis_stderr,
00296                          "** truesim error: Could not open %s for reading.\n",
00297                          simFile);
00298           goto endgame;
00299         } else {
00300           fclose(fp);
00301         }
00302         /* Stop generation of vectors, in case -p is also used with -f */
00303         N = -1;
00304         genVectors = 0;
00305         if (probFile) {
00306           FREE(probFile);
00307           probFile = NIL(char);
00308         }
00309       }
00310       break;
00311     case 'g': 
00312       if (!util_optarg) {
00313         (void) fprintf(vis_stderr,
00314                        "** truesim error: File to output input probs. not specified.\n");
00315         goto usage;
00316       }
00317       probGenFile = util_strsav(util_optarg);
00318       break;
00319     case 'h':
00320       goto usage;
00321     case 'n':
00322       if (genVectors != 0) {
00323         N = atoi(util_optarg);
00324         genVectors = 1;
00325       } else {
00326         (void) fprintf(vis_stderr,
00327                        "** truesim warning: Simulation file already specified.\n");
00328         (void) fprintf(vis_stderr,
00329                        "** truesim warning: Ignoring -n option.\n");
00330       }
00331       break;
00332     case 'p': 
00333       if (genVectors != 0) {
00334         if (!util_optarg) {
00335           (void) fprintf(vis_stderr,
00336                          "** truesim error: Probability file not specified.\n");
00337           goto usage;
00338         } else {
00339           probFile = util_strsav(util_optarg);
00340           if ((fp = Cmd_FileOpen(probFile,"r",NIL(char *),1)) == NIL(FILE)) {
00341             (void) fprintf(vis_stderr,
00342                            "** truesim error: Could not open %s for reading.\n",
00343                            probFile);
00344             goto endgame;
00345           } else {
00346             fclose(fp);
00347           }
00348         }
00349         genVectors = 1;
00350       } else {
00351         (void) fprintf(vis_stderr,
00352                        "** truesim warning: Simulation file already specified.\n");
00353         (void) fprintf(vis_stderr,
00354                        "** truesim warning: Ignoring -p option.\n");
00355       }
00356       break;
00357     case 'r':
00358       truesimRptHeader = atoi(util_optarg);
00359       break;
00360     case 'T': 
00361       if (!util_optarg) {
00362         (void) fprintf(vis_stderr,
00363                        "** truesim error: Delay file not specified.\n");
00364         goto usage;
00365       } else {
00366         delayFile = util_strsav(util_optarg);
00367         if ((fp = Cmd_FileOpen(delayFile,"r",NIL(char *),1)) == NIL(FILE)) {
00368           (void) fprintf(vis_stderr,
00369                          "** truesim error: Could not open %s for reading.\n",
00370                          delayFile);
00371           goto endgame;
00372         } else {
00373           fclose(fp);
00374         }
00375       }
00376       break;
00377     case 't':
00378       timeOutPeriod = atoi(util_optarg);
00379       break;
00380     case 'v':
00381       truesimVerbose = atoi(util_optarg);
00382       break;
00383     default:
00384       goto usage;
00385     }
00386   }
00387 
00388   if(Hrc_ManagerReadCurrentNode(*hmgr) == NIL(Hrc_Node_t)) {
00389     (void) fprintf(vis_stderr,"** truesim error: The hierarchy manager is empty.");
00390     (void) fprintf(vis_stderr," Read in design.\n");
00391     goto endgame;
00392   }
00393 
00394   network = (Ntk_Network_t *) 
00395     Hrc_NodeReadApplInfo(Hrc_ManagerReadCurrentNode(*hmgr), 
00396                          NTK_HRC_NODE_APPL_KEY);
00397 
00398   if(network == NIL(Ntk_Network_t)) {
00399     (void) fprintf(vis_stderr,"** truesim error: There is no network. ");
00400     (void) fprintf(vis_stderr,"Use flatten_hierarchy.\n");
00401     goto endgame;
00402   }
00403 
00404   /* Check if the current network has signals with multiple values. */
00405   if (TestIsNetworkMultipleValued(network)) {
00406     (void) fprintf(vis_stderr,"** truesim error: Circuit has multiple valued variables.\n");
00407     (void) fprintf(vis_stderr,"** truesim error: This algorithm applies to boolean signals only.\n");
00408     goto endgame;
00409   }
00410 
00411   if(Ntk_NetworkReadNumPrimaryInputs(network) !=
00412      Ntk_NetworkReadNumInputs(network)) {
00413     (void) fprintf(vis_stderr,"** truesim error: This algorithm applies only for circuits descrbied in BLIF format.\n");
00414     goto endgame;
00415   }
00416 
00417   /* Generate a file with random values for primary inputs and return. No
00418      simulation is performed. */
00419   if (probGenFile) {
00420     Truesim_GeneratePrimaryInputProbs(network,probGenFile);
00421     FREE(probGenFile);
00422     if (probFile)
00423       FREE(probFile);
00424     if (simFile)
00425       FREE(simFile);
00426     if (dumpFile)
00427       FREE(dumpFile);
00428     if (delayFile)
00429       FREE(delayFile);
00430     return 0;
00431   }
00432 
00433   /* Make sure that we either generate random vectors or we are
00434      supplied with simulation vectors */
00435   if (genVectors == -1) {
00436     (void) fprintf(vis_stderr,
00437                    "** truesim error: Neither simulation vector file nor ");
00438     (void) fprintf(vis_stderr,
00439                    "PI probabilities provided.\n");
00440     (void) fprintf(vis_stderr,
00441                    "** truesim error: No simulation will be performed.\n");
00442     goto endgame;
00443   }
00444 
00445   if (dumpFile && !genVectors) {
00446     if (!simFile) {
00447       (void) fprintf(vis_stderr,
00448                      "** truesim error: PI probability file not specified.\n");
00449       (void) fprintf(vis_stderr, 
00450                      "** truesim error: Use -p option.\n");
00451       (void) fprintf(vis_stderr,
00452                      "** truesim error: No simulation will be performed.\n");
00453       goto endgame;
00454     } else {
00455       (void) fprintf(vis_stderr,
00456                      "** truesim warning: Simulation file %s specified.\n",
00457                      simFile);
00458       (void) fprintf(vis_stderr,
00459                      "** truesim warning: Ignoring dump file %s\n",dumpFile);
00460     }
00461   }
00462   
00463   /* Make sure N is specified or a default value is used */
00464   if (N == -1 && genVectors == 1) {
00465     (void) fprintf(vis_stderr,
00466                    "** truesim warning: Number of patterns to be simulated is not specified.\n");
00467     (void) fprintf(vis_stderr,
00468                    "Assuming N = 1000 patterns to be simulated.\n");
00469     N = DEFAULT_NUM_PATTERNS;
00470   }
00471 
00472   /* Access a 'total' partition */
00473   partition = (graph_t *) Ntk_NetworkReadApplInfo(network, 
00474                                                   PART_NETWORK_APPL_KEY);
00475   createdPart = 0; /* Using partition of the network. */
00476   if (partition == NIL(graph_t) || 
00477       (Part_PartitionReadMethod(partition) != Part_Total_c)) {
00478     partition = Part_NetworkCreatePartition(network, 
00479                                             NIL(Hrc_Node_t),
00480                                             "dummy", (lsList) 0, 
00481                                             (lsList) 0, NIL(mdd_t),
00482                                             Part_Total_c,
00483                                             (lsList) 0, 
00484                                             FALSE, FALSE, TRUE);
00485     if (partition == NIL(graph_t)) {
00486       (void) fprintf(vis_stderr,"** truesim error: Could not create a partition.\n");
00487       goto endgame;
00488     }
00489     Ntk_NetworkAddApplInfo(network,PART_NETWORK_APPL_KEY,
00490                            (Ntk_ApplInfoFreeFn)Part_PartitionFreeCallback,
00491                            (void *)partition);
00492     createdPart = 1; /* Using new partition */
00493   }
00494 
00495   /* Start the timer.*/
00496   if (timeOutPeriod > 0){
00497     (void) signal(SIGALRM, (void(*)(int))TimeOutHandle);
00498     (void) alarm(timeOutPeriod);
00499     if (setjmp(timeOutEnv) > 0) {
00500       (void) fprintf(vis_stderr, "** truesim warning: Timeout occurred after ");
00501       (void) fprintf(vis_stderr, "%ld seconds.\n", timeOutPeriod);
00502       alarm(0);
00503       goto endgame;
00504     }
00505   }
00506 
00507   initialTime = util_cpu_time();
00508   status = TruesimSimulateNetwork(network,simFile,probFile,delayFile,
00509                                   dumpFile,trueDelay,genVectors,N);
00510   finalTime = util_cpu_time();
00511   if(status) {
00512     (void) fprintf(vis_stdout, "%-20s%10ld\n", "** truesim info: analysis time =",
00513                    (finalTime-initialTime)/1000);
00514   }
00515   else {
00516     (void) fprintf(vis_stdout, "** truesim error: Simulation was not successful.\n");
00517   }
00518 
00519   alarm(0);
00520   /* Clean up */
00521   if (probGenFile)
00522     FREE(probGenFile);
00523   if (probFile)
00524     FREE(probFile);
00525   if (simFile)
00526     FREE(simFile);
00527   if (dumpFile)
00528     FREE(dumpFile);
00529   if (delayFile)
00530     FREE(delayFile);
00531   return 0;  /* normal exit */
00532 
00533 endgame:
00534   /* Clean up */
00535   if (probGenFile)
00536     FREE(probGenFile);
00537   if (probFile)
00538     FREE(probFile);
00539   if (simFile)
00540     FREE(simFile);
00541   if (dumpFile)
00542     FREE(dumpFile);
00543   if (delayFile)
00544     FREE(delayFile);
00545   
00546   if (createdPart)
00547     Ntk_NetworkFreeApplInfo(network,PART_NETWORK_APPL_KEY);
00548   
00549   return 1; /* Error exit */
00550 
00551 usage:
00552 
00553   (void) fprintf(vis_stderr, "\nusage: Also see 'help truesim' for more details.\n\n");
00554   (void) fprintf(vis_stderr, "    -D <filename>\tFile to dump randomly generated pattern vectors.\n");
00555   (void) fprintf(vis_stderr, "            \t\tPrimary input probability file SHOULD be specified with \n");
00556   (void) fprintf(vis_stderr, "            \t\t-p option.\n\n");
00557   (void) fprintf(vis_stderr, "    -d      \t\tPerform true delay simulation.\n\n");
00558   (void) fprintf(vis_stderr, "    -f <filename>\tFile containing simulation vectors.\n\n");
00559   (void) fprintf(vis_stderr, "    -g <filename>\tFile to output random probabilities for PI \n");
00560   (void) fprintf(vis_stderr, "            \t\tUse this option ONLY to produce a vector of \n");
00561   (void) fprintf(vis_stderr, "            \t\tprobability values for PI. No simulation is performed. \n\n");
00562   (void) fprintf(vis_stderr, "    -h      \t\tCommand usage.\n\n");
00563   (void) fprintf(vis_stderr, "    -n <N>  \t\tNumber of patterns to simulate. Default is 1000.\n");
00564   (void) fprintf(vis_stderr, "            \t\tShould ONLY be used with -p option.\n\n");
00565   (void) fprintf(vis_stderr, "    -p <filename>\tFile containing PI probabilities.\n");
00566   (void) fprintf(vis_stderr, "            \t\tSee help for file format.\n\n");
00567   (void) fprintf(vis_stderr, "    -r <N>  \t\tPrint the node name header ever N iterations.\n");
00568   (void) fprintf(vis_stderr, "            \t\tThis is effective only when verbosity flag is on.\n\n");
00569   (void) fprintf(vis_stderr, "    -T <filename>\tFile containing node delay and load values.\n");
00570   (void) fprintf(vis_stderr, "            \t\tSee help for file format.\n\n");
00571   (void) fprintf(vis_stderr, "    -t <time> \t\tTime in seconds allowed to finish the simulation.\n\n");
00572   (void) fprintf(vis_stderr, "    -v <N>  \t\tVerbosity level.\n");
00573 
00574   return 1;    /* error exit */
00575 }
00576 
00586 static void
00587 TimeOutHandle(void)
00588 {
00589   longjmp(timeOutEnv, 1);
00590 }
00591 
00592 
00605 static int
00606 TestIsNetworkMultipleValued(Ntk_Network_t *network)
00607 {
00608   Ntk_Node_t *node;
00609   lsGen gen;
00610   Var_Variable_t *var;
00611   int numValues;
00612 
00613   Ntk_NetworkForEachNode(network,gen,node) {
00614     var = Ntk_NodeReadVariable(node);
00615     numValues = Var_VariableReadNumValues(var);
00616     if (numValues > 2)
00617       return 1;
00618   }
00619   return 0;
00620 }