VIS

src/restr/restrDebug.c

Go to the documentation of this file.
00001 
00022 #include "restrInt.h"
00023 
00024 
00025 /*---------------------------------------------------------------------------*/
00026 /* Constant declarations                                                     */
00027 /*---------------------------------------------------------------------------*/
00028 
00029 
00030 /*---------------------------------------------------------------------------*/
00031 /* Type declarations                                                         */
00032 /*---------------------------------------------------------------------------*/
00033 
00034 
00035 /*---------------------------------------------------------------------------*/
00036 /* Structure declarations                                                    */
00037 /*---------------------------------------------------------------------------*/
00038 
00039 
00040 /*---------------------------------------------------------------------------*/
00041 /* Variable declarations                                                     */
00042 /*---------------------------------------------------------------------------*/
00043 
00044 
00045 /*---------------------------------------------------------------------------*/
00046 /* Macro declarations                                                        */
00047 /*---------------------------------------------------------------------------*/
00048 
00049 
00052 /*---------------------------------------------------------------------------*/
00053 /* Static function prototypes                                                */
00054 /*---------------------------------------------------------------------------*/
00055 
00056 
00060 /*---------------------------------------------------------------------------*/
00061 /* Definition of exported functions                                          */
00062 /*---------------------------------------------------------------------------*/
00063 
00064 
00065 /*---------------------------------------------------------------------------*/
00066 /* Definition of internal functions                                          */
00067 /*---------------------------------------------------------------------------*/
00068 
00078 void
00079 RestrNamePrintByMddId(
00080   Ntk_Network_t *network,
00081   array_t       *array)
00082 
00083 {
00084   char *name;
00085   int i,id;
00086 
00087   arrayForEachItem(int, array, i, id) {
00088     name = Ntk_NodeReadName(Ntk_NetworkFindNodeByMddId(network,id));
00089     fprintf(vis_stdout, "%s\n",name);
00090   }
00091 }
00092 
00102 void
00103 RestrPrintMvfArray(
00104   mdd_manager    *ddManager,
00105   Mvf_Function_t *mvfArray,
00106   array_t        *nodeArray,
00107   array_t        *idArray)
00108 {
00109   int i;
00110   mdd_t *tempMdd;
00111   Mvf_Function_t *mvf;
00112   Ntk_Node_t    *node;
00113 
00114   for(i=0;i<array_n(mvfArray);i++){
00115     mvf = array_fetch(Mvf_Function_t *, mvfArray, i);
00116     tempMdd = array_fetch(mdd_t *, mvf, 1);
00117     if(!(nodeArray == NIL(array_t))) {
00118       node = array_fetch(Ntk_Node_t *, nodeArray, i);
00119       fprintf(vis_stdout, "%s ", Ntk_NodeReadName(node));
00120     }
00121     if(!(idArray == NIL(array_t))) {
00122       fprintf(vis_stdout, " %d", array_fetch(int,idArray,i));
00123     }
00124     fprintf(vis_stdout, "\n");
00125     bdd_print_minterm(tempMdd);
00126   }
00127 }
00128 
00138 void
00139 RestrPrintAllVarNames(
00140   bdd_manager   *ddManager)
00141 {
00142   int size, i;
00143   mvar_type var;
00144   size = bdd_num_vars((bdd_manager *)ddManager);
00145   for(i=0;i<size;i++) {
00146     var = mdd_get_var_by_id(ddManager,i);
00147     fprintf(vis_stdout," %s",var.name);
00148   }
00149   fprintf(vis_stdout,"\n");
00150 }
00151 
00152 
00162 void
00163 RestrPrintBddNode(
00164   bdd_manager *manager,
00165   bdd_node *ddNode)
00166 {
00167     bdd_t *mdd;
00168     bdd_node *temp;
00169     bdd_ref(temp = ddNode);
00170     mdd = bdd_construct_bdd_t(manager,temp);
00171     bdd_print_minterm(mdd);
00172     bdd_free(mdd);
00173     return;
00174 }
00175 
00186 void
00187 RestrVerifyFsmEquivBySimulation(
00188   bdd_manager *ddManager,
00189   bdd_node *oldTr,
00190   bdd_node *newTr,
00191   array_t *outBdds1,
00192   array_t *outBdds2,
00193   bdd_node *initBdd1,
00194   bdd_node *initBdd2,
00195   bdd_node **xVars,
00196   bdd_node **yVars,
00197   bdd_node **piVars,
00198   int nVars,
00199   int nPi)
00200 {
00201   int **sim, N = 11;
00202   int i,j,numOut1,numOut2;
00203   bdd_node *lambda1,*lambda2;
00204   bdd_node **lVars;
00205   bdd_node *present1,*present2,*next1,*next2;
00206   bdd_node *out1,*out2;
00207   bdd_node *input,*inputPre1,*inputPre2;
00208 
00209   sim = ALLOC(int *,N);
00210   for (i = 0; i < N; i++) {
00211     sim[i] = ALLOC(int,nPi);
00212     for (j = 0; j < nPi; j++) {
00213       sim[i][j] = 0;
00214     }
00215   }
00216   
00217   numOut1 = array_n(outBdds1);
00218   numOut2 = array_n(outBdds2);
00219   assert(numOut1 == numOut2);
00220 
00221   /* New variables to compute output relation */
00222   lVars = ALLOC(bdd_node *,numOut1);
00223   for (i = 0; i < numOut1; i++) {
00224     bdd_ref(lVars[i] = bdd_bdd_new_var(ddManager));
00225   }
00226 
00227   bdd_ref(lambda1 = bdd_read_one(ddManager));
00228   bdd_ref(lambda2 = bdd_read_one(ddManager));
00229   for (i = 0; i < numOut1; i++) {
00230     bdd_node *temp,*outRel;
00231     bdd_ref(outRel = bdd_bdd_xnor(ddManager,lVars[i],
00232                                   array_fetch(bdd_node *,outBdds1,i)));
00233     bdd_ref(temp = bdd_bdd_and(ddManager,lambda1,outRel));
00234     bdd_recursive_deref(ddManager,outRel);
00235     bdd_recursive_deref(ddManager,lambda1);
00236     lambda1 = temp;
00237 
00238     bdd_ref(outRel = bdd_bdd_xnor(ddManager,lVars[i],
00239                                   array_fetch(bdd_node *,outBdds2,i)));
00240     bdd_ref(temp = bdd_bdd_and(ddManager,lambda2,outRel));
00241     bdd_recursive_deref(ddManager,outRel);
00242     bdd_recursive_deref(ddManager,lambda2);
00243     lambda2 = temp;
00244   }
00245 
00246   bdd_ref(present1 = initBdd1);
00247   bdd_ref(present2 = initBdd2);
00248   /* Simulate the patterns */
00249   for (i = 0; i < N; i++) {
00250     bdd_node *temp1;
00251 
00252     bdd_ref(input = bdd_bdd_compute_cube(ddManager,piVars,sim[i],nPi));
00253 
00254     /* Compute next state and outputs for machine 1 */
00255     bdd_ref(inputPre1 = bdd_bdd_and(ddManager,input,present1));
00256     bdd_ref(temp1 = bdd_bdd_cofactor(ddManager,oldTr,inputPre1));
00257     bdd_ref(next1 = bdd_bdd_swap_variables(ddManager,temp1,yVars,xVars,nVars));
00258     bdd_recursive_deref(ddManager,temp1);
00259     bdd_recursive_deref(ddManager,present1);
00260     present1 = next1;
00261     bdd_ref(out1 = bdd_bdd_cofactor(ddManager,lambda1,inputPre1));
00262     bdd_recursive_deref(ddManager,inputPre1);
00263 
00264     /* Compute next state and outputs for machine 2 */
00265     bdd_ref(inputPre2 = bdd_bdd_and(ddManager,input,present2));
00266     bdd_ref(temp1 = bdd_bdd_cofactor(ddManager,newTr,inputPre2));
00267     bdd_ref(next2 = bdd_bdd_swap_variables(ddManager,temp1,yVars,xVars,nVars));
00268     bdd_recursive_deref(ddManager,temp1);
00269     bdd_recursive_deref(ddManager,present2);
00270     present2 = next2;
00271     bdd_ref(out2 = bdd_bdd_cofactor(ddManager,lambda2,inputPre2));
00272     bdd_recursive_deref(ddManager,inputPre2);
00273     
00274     bdd_recursive_deref(ddManager,input);
00275 
00276     if (out1 != out2) {
00277       (void) fprintf(vis_stdout,"Unequal outputs at i = %d\n",i);
00278     }
00279     bdd_recursive_deref(ddManager,out1);
00280     bdd_recursive_deref(ddManager,out2);
00281   }
00282 
00283   bdd_recursive_deref(ddManager,present1);
00284   bdd_recursive_deref(ddManager,present2);
00285 }
00286 
00296 void
00297 RestrTestIsEquivRelation(
00298   bdd_manager *ddManager,
00299   bdd_node *rel,
00300   bdd_node **xVars,
00301   bdd_node **yVars,
00302   bdd_node **uVars,
00303   bdd_node **vVars,
00304   int nVars)
00305 {
00306   
00307   bdd_node *one,*reflex;
00308   bdd_node *Rxy,*Rvy,*Ruy,*Rux;
00309   bdd_node *left,*result,*uCube;
00310   bdd_node **compose;
00311   int numVars,i;
00312 
00313   numVars = bdd_num_vars(ddManager);
00314   one = bdd_read_one(ddManager);
00315   
00316   /* rel is interms of xVars and uVars */
00317   /* Test if rel is reflexive: R(x,x) == 1 */
00318   compose = ALLOC(bdd_node *,numVars);
00319   for (i = 0; i < numVars; i++) {
00320     compose[i] = bdd_bdd_ith_var(ddManager,i);
00321   }
00322   for (i = 0; i < nVars; i++) {
00323     int index;
00324     index = bdd_node_read_index(uVars[i]);
00325     compose[index] = xVars[i];
00326   }
00327   bdd_ref(reflex = bdd_bdd_vector_compose(ddManager,rel,compose));
00328   if (reflex != one) {
00329     (void) fprintf(vis_stdout,"Relation is not reflexive.\n");
00330   } else {
00331     (void) fprintf(vis_stdout,"Relation IS reflexive.\n");
00332   }
00333   bdd_recursive_deref(ddManager,reflex);
00334   FREE(compose);
00335 
00336   /* Test if rel is symmetric: R(x,u) == R(u,x) */
00337   bdd_ref(Rxy = bdd_bdd_swap_variables(ddManager,rel,uVars,yVars,nVars));
00338   bdd_ref(Rvy = bdd_bdd_swap_variables(ddManager,Rxy,xVars,vVars,nVars));
00339   bdd_ref(Ruy = bdd_bdd_swap_variables(ddManager,Rvy,vVars,uVars,nVars));
00340   bdd_ref(Rux = bdd_bdd_swap_variables(ddManager,Ruy,yVars,xVars,nVars));
00341   bdd_recursive_deref(ddManager,Rvy);
00342   if (Rux != rel) {
00343     (void) fprintf(vis_stdout,"Relation is not symmetric.\n");
00344   } else {
00345     (void) fprintf(vis_stdout,"Relation IS symmetric.\n");
00346   }
00347   bdd_recursive_deref(ddManager,Rux);
00348   
00349   /* Test if rel is transitive: (\exists_u R(x,u) * R(u,y)) ==> R(x,y) */
00350   bdd_ref(uCube = bdd_bdd_compute_cube(ddManager,uVars,NIL(int),nVars));
00351   bdd_ref(left = bdd_bdd_and_abstract(ddManager,rel,Ruy,uCube));
00352   bdd_recursive_deref(ddManager,Ruy);
00353   bdd_recursive_deref(ddManager,uCube);
00354   bdd_ref(result = bdd_bdd_ite(ddManager,left,Rxy,one));
00355   bdd_recursive_deref(ddManager,Rxy);
00356   bdd_recursive_deref(ddManager,left);
00357   if (result != one) {
00358     (void) fprintf(vis_stdout,"Relation is not transitive.\n");
00359   } else {
00360     (void) fprintf(vis_stdout,"Relation IS transitive.\n");
00361   }
00362   bdd_recursive_deref(ddManager,result);
00363 }
00364 
00374 void
00375 RestrPrintVarArrayNames(
00376   bdd_manager *ddManager,
00377   bdd_node **xVars,
00378   int nVars)
00379 {
00380   int index, i;
00381   mvar_type var;
00382 
00383   for(i=0;i<nVars;i++) {
00384     index = bdd_node_read_index(xVars[i]);
00385     var = mdd_get_var_by_id(ddManager,index);
00386     fprintf(vis_stdout," %s",var.name);
00387   }
00388   fprintf(vis_stdout,"\n");
00389 }
00390 
00400 void
00401 RestrPrintNameArray(array_t *nameArray)
00402 {
00403   int i;
00404   char *str;
00405   arrayForEachItem(char *,nameArray,i,str) {
00406     (void) fprintf(vis_stdout,"%s\n",str);
00407   }
00408 }
00409 
00419 int
00420 RestrTestIsDeterministic(
00421   bdd_manager *ddManager,
00422   bdd_node *tr,
00423   bdd_node *initBdd,
00424   bdd_node **xVars,
00425   bdd_node **yVars,
00426   int nVars)
00427 {
00428   bdd_node *cproj2,*ref;
00429   int flag;
00430 
00431   bdd_ref(ref = bdd_bdd_swap_variables(ddManager,initBdd,xVars,
00432                                        yVars,nVars));
00433   bdd_ref(cproj2 = bdd_bdd_cprojection(ddManager,tr,ref));
00434   if (cproj2 != tr) {
00435     bdd_node *newEdges;
00436     (void) fprintf(vis_stdout,"** restr diag: Relation is non-deterministic\n");
00437     /* Let's find out the newly added edges */
00438     bdd_ref(newEdges = bdd_bdd_and(ddManager,tr,
00439                                    bdd_not_bdd_node(cproj2)));
00440     (void) fprintf(vis_stdout,"** restr diag: New edges are:\n");
00441     RestrPrintBddNode(ddManager,newEdges);
00442     bdd_recursive_deref(ddManager,newEdges);
00443     flag = 0;
00444   } else {
00445     (void) fprintf(vis_stdout,"** restr diag: Relation IS deterministic\n");
00446     flag = 1;
00447   }
00448   bdd_recursive_deref(ddManager,cproj2);
00449   bdd_recursive_deref(ddManager,ref);
00450   
00451   return flag;
00452 }
00453 
00454 /*---------------------------------------------------------------------------*/
00455 /* Definition of static functions                                            */
00456 /*---------------------------------------------------------------------------*/