1 /****************************************************************************/
3 /* file : bsc_drive.c */
4 /* date : March 2003 */
6 /* author(s) : PINTO A. */
8 /* description : Utilities for SystemC HDL driver */
10 /****************************************************************************/
20 #include "bsc_utype.h"
22 #include "bsc_utdef.h"
25 char *bsc_getBitStr(char *str
);
26 static int bsc_2puiss(int exp
);
27 char *bsc_bitToHex(char *str
);
29 /****************************************************************************/
30 /*{{{ bgl_mystrcat() */
33 /****************************************************************************/
34 static char *bsc_mystrcat(char *str1
, char *str2
, ... )
40 vsprintf(buf
,str2
,arg
);
42 return strcat(str1
,buf
);
45 /*}}}************************************************************************/
49 /****************************************************************************/
50 static struct beden
**namtab
=NULL
;
52 void bsc_freenames(void)
58 char *bsc_name (char *name
)
65 static char *keywrd
[] =
67 "abs" , "access" , "after" , "alias" ,
68 "all" , "and" , "architecture", "array" ,
69 "assert" , "attribute" , "begin" , "bit" ,
70 "bit_vector" , "block" , "body" , "buffer" ,
71 "bus" , "case" , "component" , "configuration",
72 "constant" , "disconnect" , "downto" , "else" ,
73 "elsif" , "end" , "entity" , "error" ,
74 "exit" , "file" , "for" , "function" ,
75 "generate" , "generic" , "guarded" , "if" ,
76 "in" , "inout" , "is" , "label" ,
77 "library" , "linkage" , "loop" , "map" ,
78 "mod" , "mux_bit" , "mux_vector" , "nand" ,
79 "natural" , "new" , "next" , "nor" ,
80 "not" , "null" , "of" , "on" ,
81 "open" , "or" , "others" , "out" ,
82 "package" , "port" , "procedure" , "process" ,
83 "range" , "record" , "reg_bit" , "reg_vector" ,
84 "register" , "rem" , "report" , "return" ,
85 "select" , "severity" , "signal" , "stable" ,
86 "subtype" , "then" , "to" , "transport" ,
87 "type" , "units" , "until" , "use" ,
88 "variable" , "wait" , "warning" , "when" ,
89 "while" , "with" , "wor_bit" , "wor_vector" ,
95 namtab
= beh_initab ();
96 for (i
= 0; i
< 93; i
++)
97 beh_addtab (namtab
, namealloc (keywrd
[i
]), NULL
, BSC_NEWDFN
, 1);
100 tmp_name
= namealloc (name
);
101 new_name
= (char *) beh_chktab (namtab
,tmp_name
,NULL
,BSC_PNTDFN
);
103 if (beh_chktab (namtab
,tmp_name
,NULL
,BSC_NAMDFN
) == 0)
109 while (tmp_name
[i
] != '\0')
111 buffer
[j
] = tmp_name
[i
];
112 if ( ((tmp_name
[i
] >= 'a') && (tmp_name
[i
] <= 'z')) ||
113 ((tmp_name
[i
] >= 'A') && (tmp_name
[i
] <= 'Z')) ||
114 ((tmp_name
[i
] >= '0') && (tmp_name
[i
] <= '9') && (i
!= 0)))
126 if (buffer
[j
-1] == '_')
129 new_name
= namealloc (buffer
);
132 while (beh_chktab (namtab
,new_name
,NULL
,BSC_NEWDFN
) != 0)
135 sprintf (buffer
,"%s_%d",new_name
,number
++);
137 new_name
= namealloc (buffer
);
139 beh_addtab (namtab
,new_name
,NULL
,BSC_NEWDFN
,1);
140 beh_addtab (namtab
,tmp_name
,NULL
,BSC_PNTDFN
,(long)new_name
);
141 beh_addtab (namtab
,tmp_name
,NULL
,BSC_NAMDFN
,1);
147 /*}}}************************************************************************/
148 /*{{{ bsc_vectorize() */
150 /* description : put parenthesis on element of bussed signals */
152 /****************************************************************************/
153 char *bsc_vectorize(char *name
)
161 static struct beden
**tab
= NULL
;
166 new_name
= (char *) beh_chktab (tab
, name
, NULL
, BSC_PNTDFN
);
167 if (new_name
== NULL
)
171 while ((name
[i
] != '\0') && (name
[i
] != ' ' ) && (name
[i
] != '\''))
175 new_name
= bsc_name (tmp1
);
177 if (name
[i
] != '\0')
183 while ((name
[j
]!='\0')&&(name
[j
]!=':'))
187 left
= atoi(&name
[i
+1]);
188 right
= atoi(&name
[j
+1]);
189 sprintf (tmp1
,"%s(%d %s %d)",new_name
,
190 left
,(left
<right
)?"TO":"DOWNTO",right
);
194 sprintf (tmp1
, "%s(%s)", new_name
, &name
[i
+1]);
197 sprintf (tmp1
, "%s'%s" , new_name
, &name
[i
+1]);
198 new_name
= namealloc (tmp1
);
200 beh_addtab (tab
, name
, NULL
, BSC_PNTDFN
, (long)new_name
);
206 /*}}}************************************************************************/
207 /*{{{ bsc_vectnam() */
209 /* analyze a list of signal and return the bounds of the vectorised */
210 /* signals, if they occure. */
211 /* attention !!! cette fonction part du principe que ->NAME est toujours */
212 /* le 2 eme champs de chaque structure beXXX */
214 /****************************************************************************/
215 void *bsc_vectnam(void *pt_list
, int *left
, int *right
, char **name
, char type
)
216 /* If type = 0 bepor_list, type = 1 bebux_list*/
217 /* If type = 2 bereg_list, type = 3 beaux_list*/
229 /* Case bepor_list */
232 ptpor
= (bepor_list
*)pt_list
;
234 sig_name
= ptpor
->NAME
;
235 *name
= (char*)mbkalloc(strlen(sig_name
) + 1);
236 strcpy(*name
,sig_name
);
237 blank_space
= strchr(*name
,' ');
238 if (blank_space
!= NULL
)
240 strcpy(number
,blank_space
);
241 *left
= atoi(number
);
248 if(ptpor
->NEXT
!= NULL
)
250 strcpy(name_tmp
,(ptpor
->NEXT
)->NAME
);
251 blank_space
= strchr(name_tmp
,' ');
252 if(blank_space
!=NULL
)
254 strcpy(number
,blank_space
);
256 if(!strcmp(*name
,name_tmp
))
258 *right
= atoi(number
);
276 ptbux
= (bebux_list
*)pt_list
;
277 /* Extract the name and number of an element */
279 sig_name
= ptbux
->NAME
;
280 *name
= (char *)mbkalloc(strlen(sig_name
) + 1);
281 strcpy(*name
,sig_name
);
282 blank_space
= strchr(*name
,' ');
283 if (blank_space
!= NULL
)
285 strcpy(number
,blank_space
);
286 *right
= atoi(number
);
293 if(ptbux
->NEXT
!= NULL
)
295 strcpy(name_tmp
,ptbux
->NEXT
->NAME
);
296 blank_space
= strchr(name_tmp
,' ');
297 if(blank_space
!=NULL
)
299 strcpy(number
,blank_space
);
301 if(!strcmp(*name
,name_tmp
))
303 *right
= atoi(number
);
321 ptreg
= (bereg_list
*)pt_list
;
322 /* Extract the name and number of an element */
324 sig_name
= ptreg
->NAME
;
325 *name
= (char *)mbkalloc(strlen(sig_name
) + 1);
326 strcpy(*name
,sig_name
);
327 blank_space
= strchr(*name
,' ');
328 if (blank_space
!= NULL
)
330 strcpy(number
,blank_space
);
331 *right
= atoi(number
);
338 if(ptreg
->NEXT
!= NULL
)
340 strcpy(name_tmp
,ptreg
->NEXT
->NAME
);
341 blank_space
= strchr(name_tmp
,' ');
342 if(blank_space
!=NULL
)
344 strcpy(number
,blank_space
);
346 if(!strcmp(*name
,name_tmp
))
348 *right
= atoi(number
);
366 ptaux
= (beaux_list
*)pt_list
;
367 /* Extract the name and number of an element */
369 sig_name
= ptaux
->NAME
;
370 *name
= (char *)mbkalloc(strlen(sig_name
) + 1);
371 strcpy(*name
,sig_name
);
372 blank_space
= strchr(*name
,' ');
373 if (blank_space
!= NULL
)
375 strcpy(number
,blank_space
);
376 *right
= atoi(number
);
383 if(ptaux
->NEXT
!= NULL
)
385 strcpy(name_tmp
,ptaux
->NEXT
->NAME
);
386 blank_space
= strchr(name_tmp
,' ');
387 if(blank_space
!=NULL
)
389 strcpy(number
,blank_space
);
391 if(!strcmp(*name
,name_tmp
))
393 *right
= atoi(number
);
411 /*}}}************************************************************************/
412 /*{{{ bsc_printabl() */
414 /* put a \n for a better presentation of an abl */
416 /****************************************************************************/
417 char *bsc_printabl(char *chaine
)
419 char *chaine_tmp
= NULL
;
423 while (strlen(chaine_tmp
)>60)
425 chaine_tmp
= &chaine_tmp
[60];
428 blanc
= strchr(chaine_tmp
,' ');
429 if (blanc
!= NULL
) chaine_tmp
= blanc
+1;
432 ( *(blanc
-1) == '=' || *(blanc
+1) == '=' || *(blanc
+1) == '('));
440 /*}}}************************************************************************/
441 /*{{{ bsc_abl2str() */
443 /* return a string corresponding to an expression */
445 /****************************************************************************/
446 char *bsc_abl2str (chain_list
*expr
, char *chaine
, int *size_pnt
)
449 struct chain
*operand
;
454 /* ###--------------------------------------------------------### */
455 /* if there is not enough space left allocate a bigger block*/
456 /* ###--------------------------------------------------------### */
458 if ((size_t)*size_pnt
< (strlen (chaine
) + 256))
460 *size_pnt
= *size_pnt
+ 512;
461 chaine
= (char *) mbkrealloc (chaine
, *size_pnt
);
464 /* ###------------------------------------------------------### */
465 /* if the expression is a terminal (atom) add its name to */
466 /* the previous string */
467 /* ###------------------------------------------------------### */
469 if (expr
->NEXT
== NULL
)
471 if (isBitStr(expr
->DATA
))
473 tmp
= bsc_getBitStr(expr
->DATA
);
474 strcat (chaine
, tmp
);
478 strcat (chaine
, bsc_vectorize (expr
->DATA
));
483 /* ###------------------------------------------------------### */
484 /* if the expression is not a terminal : */
485 /* - for unary operators (not, stable) add */
486 /* "operator ( operand )" to the previous string */
488 /* - for binary operators (and, or, nor, xor, ...) add */
489 /* "(operand operator operand operator operand ...)" */
490 /* ###------------------------------------------------------### */
492 operand
= (struct chain
*)expr
->NEXT
->DATA
;
497 bsc_mystrcat(chaine
,"%s'STABLE",bsc_vectorize (operand
->DATA
));
499 // added by anto to compute vectabl
502 for (i
= 0; i
< REP_NB(expr
); i
++)
504 strcat(chaine
,bsc_vectorize (operand
->DATA
));
505 if (i
!= REP_NB(expr
)-1)
506 strcat(chaine
," & ");
508 strcat (chaine
, ")");
511 strcat (chaine
, "not (");
512 chaine
= bsc_abl2str (operand
, chaine
, size_pnt
);
513 strcat (chaine
, ")");
516 oper
= operToChar (OPER(expr
));
518 while ( (expr
= expr
->NEXT
) )
520 chaine
= bsc_abl2str (expr
->DATA
, chaine
, size_pnt
);
522 bsc_mystrcat(chaine
, " %s ",oper
);
524 strcat (chaine
, ")");
531 /*}}}************************************************************************/
532 /*{{{ bsc_getBitStr() */
534 /* create an easy to drive copy of the bitstring */
536 /****************************************************************************/
537 char *bsc_getBitStr(char *str
)
543 for (i
= j
= n
; i
>= 0; i
--)
547 case 'u' : buf
[j
--] = 'U'; break;
548 case 'z' : buf
[j
--] = 'Z'; break;
549 case 'd' : buf
[j
--] = 'D'; break;
550 default : buf
[j
--] = str
[i
]; break;
554 return bsc_bitToHex(buf
);
556 return mbkstrdup(buf
);
559 /*}}}************************************************************************/
560 /*{{{ bsc_2puiss() */
563 /****************************************************************************/
564 static int bsc_2puiss(int exp
)
567 return 2 * bsc_2puiss(exp
- 1);
572 /*}}}************************************************************************/
573 /*{{{ bsc_bitToHex() */
576 /****************************************************************************/
577 char *bsc_bitToHex(char *str
)
584 j
= (n
- 2) / 4 + 3 + (((n
- 2) % 4) ? 1 : 0);
588 for (i
= n
; i
>= 0; i
--)
592 case 'u' : case 'z' : case 'd' : case '\'' :
596 buf
[j
--] = (tmp
< 10) ? '0' + tmp
: 'A' + tmp
% 10 ;
605 if (!(pos
= (n
- (i
+2))% 4))
609 buf
[j
--] = (tmp
< 10) ? '0' + tmp
: 'A' + tmp
% 10 ;
615 tmp
+= (str
[i
] - '0') * bsc_2puiss(pos
);
618 return mbkstrdup(buf
);
621 /*}}}************************************************************************/
622 /*{{{ bsc_abl2strbool() */
624 /* This is a modified version of the previous function that is meant */
625 /* to be synopsys compliant regarding the boolean nature of a number */
626 /* of variables and signals. */
628 /****************************************************************************/
629 char *bsc_abl2strbool (chain_list
*expr
, char *chaine
, int *size_pnt
)
632 struct chain
*operand
;
635 /* ###------------------------------------------------------### */
636 /* if there is not enough space left allocate a bigger block */
637 /* ###------------------------------------------------------### */
639 if ((size_t)*size_pnt
< (strlen (chaine
) + 256))
641 *size_pnt
= *size_pnt
+ 512;
642 chaine
= (char *) mbkrealloc (chaine
, *size_pnt
);
645 /* ###------------------------------------------------------### */
646 /* if the expression is a terminal (atom) add its name to */
647 /* the previous string */
648 /* ###------------------------------------------------------### */
652 // added by anto to compute vectabl
653 if (isBitStr(expr
->DATA
))
654 strcat (chaine
, expr
->DATA
);
657 size
= getAtomSize(expr
);
658 bsc_mystrcat( chaine
,"%s = %s", bsc_vectorize (expr
->DATA
),
664 /* ###------------------------------------------------------### */
665 /* if the expression is not a terminal : */
666 /* - for unary operators (not, stable) add */
667 /* "operator ( operand )" to the previous string */
669 /* - for binary operators (and, or, nor, xor, ...) add */
670 /* "(operand operator operand operator operand ...)" */
671 /* ###------------------------------------------------------### */
673 operand
= (struct chain
*)expr
->NEXT
->DATA
;
678 bsc_mystrcat(chaine
, "not (%s'EVENT)",
679 bsc_vectorize (operand
->DATA
));
682 if (operand
->NEXT
!= NULL
683 && (long)((chain_list
* )operand
->DATA
)->DATA
== STABLE
)
686 bsc_vectorize (((chain_list
* )operand
->NEXT
->DATA
)->DATA
));
687 strcat (chaine
, "'EVENT");
689 // added by anto to compute vectabl
690 else if (isBitStr(operand
->DATA
))
691 strcat (chaine
, operand
->DATA
);
698 size
= getAtomSize(operand
);
700 buf
= mbkalloc(bufsize
);
702 bsc_abl2str(operand
,buf
,&bufsize
);
703 sprintf(buffer
, "%s = %s", buf
, genZeroBitStr(size
));
705 strcat (chaine
, buffer
);
708 // added by anto to compute vectabl
716 buf
= mbkalloc(bufsize
);
717 bitstr
= beg_boolDelNot(expr
,&tmpExpr
);
720 bsc_abl2str(tmpExpr
,buf
,&bufsize
);
721 bsc_mystrcat(chaine
, "STD_LOGIC_VECTOR'%s = %s",buf
,bitstr
);
727 oper
= operToChar (OPER(expr
));
728 strcat (chaine
, "(");
729 while ((expr
= expr
->NEXT
))
731 chaine
= bsc_abl2strbool (expr
->DATA
, chaine
, size_pnt
);
733 bsc_mystrcat(chaine
, " %s ", oper
);
735 strcat (chaine
, ")");
741 /*}}}************************************************************************/