1 /****************************************************************************/
3 /* file : vhd_drive.c */
6 /* author(s): LESTER A.P.P */
8 /* description : Synopsys VHDL driver */
10 /****************************************************************************/
31 #include "bgl_drive.h"
33 /* Global variables */
35 static char *TIME_UNIT
;
37 int BGL_DRIVE_PRIMITIVES
= FALSE
;
38 int BGL_ONLY_PRIMITIVES
= FALSE
;
41 static int BGL_VECT_ASS
= 0;
42 static int BGL_CPT_IND
= 0;
44 static char *CLOCKSTR
;
46 /****************************************************************************/
47 /*{{{ Static function prototypes */
48 /****************************************************************************/
49 static void writelist(FILE *fp
, chain_list
*ptchain
, int indent
, int posEdge
, int negEdge
);
50 static void print_value(FILE *fp
,chain_list
*abl
, char type
, int time
, int,int,char **buffer
, int *bufsize
);
51 //static void begen_treatment (FILE *fp, befig_list *figure);
53 static void print_withselect(FILE *fp
, biabl_list
*biabl
, char *name
, int vectsize
, char **buffer
, int *bufsize
);
54 static void print_affected(FILE *fp
, char indent
, char type
, int time
, int,int,char *name
, char *vname
, loop_list
*loop
,long);
55 static void print_loop (FILE *fp
, char indent
,char *head
,int np
,int *cpt
,loop_list
*loop
);
56 static void print_endLoop (FILE *fp
,char indent
,char *head
,int np
,int cpt
, int nbsame
,loop_list
*loop
);
57 static void print_loopList (FILE *fp
,char indent
,char *head
,int np
,int *cpt
, int nbsame
,loop_list
*loop
);
58 static void print_signal(FILE *fp
, char *name
, char type
, int left
, int right
);
59 static chain_list
* print_process_declaration(FILE *fp
, biabl_list
*biabl
, char *name
, int np
, char *head
, char **buffer
,ptype_list
*clock
);
60 static char *bgl_bebusname(char *name
);
61 static char *bgl_beregname(char *name
);
62 static void print_interassign(FILE *fp
, char *dest
, char *src
, int mode
);
65 static void print_special(long flags
, char *buf
)
68 if (flags
& (BEH_CND_WEAK
|BEH_CND_STRONG
|BEH_CND_PULL
))
71 if (flags
& BEH_CND_STRONG
) str
="strong";
72 else if (flags
& BEH_CND_PULL
) str
="pull";
73 sprintf(buf
,"(%s1, %s0) ", str
, str
);
76 /*}}}************************************************************************/
78 /****************************************************************************/
81 /* ecrit dans un fichier une chaine list */
82 /****************************************************************************/
84 writelist(FILE *fp
, chain_list
*ptchain
, int indent
, int posEdge
, int negEdge
)
92 chain_list
*namelist
= NULL
;
96 for (i
= 0; i
< indent
; i
++)
102 name
= bgl_getRadical(CLOCKSTR
,NULL
);
103 hpos
+= strlen(name
);
106 fprintf(fp
, "posedge ");
109 fprintf(fp
, "%s", name
);
114 name
= bgl_getRadical(CLOCKSTR
,NULL
);
115 hpos
+= strlen(name
) + 8;
116 fprintf(fp
, "negedge %s", name
);
121 // pre-treatment to delete twice word
122 // and clock in case of posedge or negedge
123 for (; ptchain
; ptchain
= delchain(ptchain
,ptchain
))
127 if ( isBitStr(ptchain
->DATA
)
128 /*|| ( (posEdge || negEdge) && ptchain->DATA == CLOCKSTR)*/)
130 beg_get_vectname(ptchain
->DATA
,&name
,NULL
,NULL
,BEG_SEARCH_ABL
);
132 for (chainx
= namelist
; chainx
; chainx
= chainx
->NEXT
)
135 if ( name
== chainx
->DATA
)
140 tail
->NEXT
= addchain(NULL
,name
);
142 namelist
= addchain(NULL
,name
);
147 for (chainx
= namelist
; chainx
; chainx
= delchain(chainx
,chainx
))
153 fprintf(fp
, "\n%s", indentstr
);
157 hpos
+= strlen(name
) + 4 ;
158 fprintf(fp
,"%s",bgl_vectorize_sub(name
, buf
));
164 /*}}}************************************************************************/
165 /*{{{ print_time() */
168 /****************************************************************************/
169 static void print_time_int(FILE *fp
,int time
,int timer
, int timef
, int timehz
)
171 if (timef
<=0) timef
=timer
;
172 if (timer
<=0) timer
=timef
;
173 if (timer
==0 && timef
==0) timef
=timer
=time
;
180 fprintf(fp
,"#%u ",timer
);
182 fprintf(fp
,"#(%u, %u) ",timer
, timef
);
186 fprintf(fp
,"#(%u, %u, %u) ",timer
, timef
, timehz
);
191 static void print_time(FILE *fp
,int time
,int timer
, int timef
)
193 print_time_int(fp
,time
,timer
,timef
,-1);
196 /*}}}************************************************************************/
200 /****************************************************************************/
201 static int isone(char *txt
)
203 return !strcmp(txt
,"1'b1");
207 /*}}}************************************************************************/
208 /*{{{ bebus_suffix() */
211 /****************************************************************************/
212 static char *bgl_bebusname(char *name
)
216 if ((c
=strchr(name
, ' '))!=NULL
&& *(c
+1)!='\0')
219 sprintf(buf
, "%s_bebus %s",name
,c
+1);
223 sprintf(buf
, "%s_bebus", name
);
224 return namealloc(buf
);
227 /*}}}************************************************************************/
228 /*{{{ bereg_suffix() */
231 /****************************************************************************/
232 static char *bgl_beregname(char *name
)
236 if ((c
=strchr(name
, ' '))!=NULL
&& *(c
+1)!='\0')
239 sprintf(buf
, "%s_bereg %s",name
,c
+1);
243 sprintf(buf
, "%s_bereg", name
);
244 return namealloc(buf
);
247 /*}}}************************************************************************/
248 /*{{{ Module declaration */
249 /****************************************************************************/
250 /*{{{ module_declaration_vect() */
253 /****************************************************************************/
254 static void module_declaration_vect(FILE *fp
,bevectpor_list
*vpor
,int cpt
)
258 module_declaration_vect(fp
,vpor
->NEXT
,cpt
+1);
259 fprintf(fp
,"%s",bgl_vlogname(vpor
->NAME
));
265 /*}}}************************************************************************/
266 /*{{{ module_declaration_sing() */
269 /****************************************************************************/
270 static void module_declaration_sing(FILE *fp
,bepor_list
*por
,int vpor
)
280 por
= (bepor_list
*)reverse((chain_list
*)por
);
281 for (porx
= por
; porx
; porx
= porx
->NEXT
)
283 porx
= bgl_vectnam(porx
, &left
, &right
, &name
, 0);
284 fprintf(fp
, "%s", bgl_vlogname(name
));
289 por
= (bepor_list
*)reverse((chain_list
*)por
);
293 /*}}}************************************************************************/
294 /*{{{ module_declaration() */
297 /****************************************************************************/
299 module_declaration(FILE *fp
, befig_list
*figure
)
301 iprint(fp
,'i',"module %s", bgl_vlogname(figure
->NAME
));
303 if (figure
->BEPOR
|| figure
->BEVECTPOR
)
306 module_declaration_vect(fp
,figure
->BEVECTPOR
,0);
307 module_declaration_sing(fp
,figure
->BEPOR
,(int)(long)figure
->BEVECTPOR
);
311 fprintf(fp
, ";\n\n");
314 /*}}}************************************************************************/
315 /*}}}************************************************************************/
316 /*{{{ print_process_declaration() */
318 /* Create the sensitivity list */
319 /****************************************************************************/
321 print_process_declaration(FILE *fp
, biabl_list
*biabl
, char *name
,
322 int np
, char *head
, char **buffer
, ptype_list
*clock
)
324 chain_list
*loopchain
= NULL
;
328 chain_list
*temp_expr
;
329 int isFlipFlop
= FALSE
;
330 int isEdgeCondition
= FALSE
;
335 iprint(fp
,'i',"always @ (");
336 for (biablx
= biabl
; biablx
; biablx
= biablx
->NEXT
)
338 if (beh_isloop(biablx
))
340 for (loop
= beh_getloop(biablx
); loop
; loop
= loop
->NEXT
)
342 for (chainx
= loopchain
; chainx
; chainx
= chainx
->NEXT
)
343 if (chainx
->DATA
== loop
->VARNAME
)
346 loopchain
= addchain(loopchain
,loop
->VARNAME
);
349 if (biablx
->VALABL
== NULL
)// || biablx->CNDABL == NULL)
350 beh_toolbug(20, "print_process_declaration", name
, 0);
351 /* if ( biablx->CNDABL &&
352 (isEdgeCondition = bef_search_stable(biablx->CNDABL,&CLOCKSTR)) > 0)
354 if (bef_search_notck(biablx->CNDABL,CLOCKSTR) > 0)
361 if (clock
==NULL
|| clock
->DATA
==NULL
)
364 temp_expr
= createExpr(OR
);
366 for (biablx
= biabl
; biablx
; biablx
= biablx
->NEXT
)
368 if (clock
==NULL
|| (clock
->DATA
==NULL
&& clock
->TYPE
==1 && bef_search_stable(biablx
->CNDABL
,&clk
))<=0)
371 addQExpr(temp_expr
, copyExpr(biablx
->CNDABL
));
372 addQExpr(temp_expr
, copyExpr(biablx
->VALABL
));
376 for (biablx
= biabl
; biablx
; biablx
= biablx
->NEXT
)
379 if (clock
==NULL
|| (clock
->DATA
==NULL
&& clock
->TYPE
==1 && bef_search_stable(biablx
->CNDABL
,&clk
))<=0)
380 addQExpr(temp_expr
, copyExpr(biablx
->CNDABL
));
383 list
=supportChain_listExpr(temp_expr
);
384 /*if (clock!=NULL && clock->DATA==NULL)
386 chain_list *temp=NULL;
389 if (bgl_getRadical((char *)list->DATA,NULL)==list->DATA)
390 temp=addchain(temp, list->DATA);
391 list=delchain(list, list);
395 writelist(fp
, list
, 10, posEdge
, negEdge
);
400 CLOCKSTR
=clock
->DATA
;
401 writelist(fp
, addchain(NULL
, clock
->DATA
), 10, clock
->TYPE
==1?1:0, clock
->TYPE
==0?1:0);
413 /*}}}************************************************************************/
414 /*{{{ Process driving */
415 /****************************************************************************/
416 static void print_procBegin(FILE *fp
, chain_list
*loopchain
)
422 iprint(fp
,'i',"begin : LOOP_NB_%d\n",BGL_CPT_IND
++);
423 iprint(fp
,'0',"integer ");
424 for (chainx
= loopchain
; chainx
; chainx
= delchain(chainx
,chainx
))
425 if (chainx
!= loopchain
)
426 fprintf(fp
,", %s",(char*)chainx
->DATA
);
428 fprintf(fp
,"%s",(char*)chainx
->DATA
);
432 iprint(fp
,'i',"begin\n");
435 static void print_procEnd(FILE *fp
)
437 iprint(fp
,'-',"end\n");
440 static void print_procIf(FILE *fp
, char *buffer
)
444 // iprint(fp,'+',"%s\n",buffer);
448 iprint(fp
,'+',"if (%s)\n",bgl_printabl(buffer
));
451 static void print_procElsif(FILE *fp
, char *buffer
)
453 iprint(fp
,'+',"else if (%s)\n",bgl_printabl(buffer
));
456 static int print_procElse(FILE *fp
)
458 iprint(fp
,'+',"else\n");
463 static int print_procEndif(FILE *fp
, char ind
)
473 static int print_edge(char *buffer
, char *ckname
, char ckstate
,
476 sprintf(buffer
,"@ (%sedge %s)",(ckstate
=='1')?"pos":"neg",ckname
);
483 /*}}}************************************************************************/
484 /*{{{ Ports driving */
487 /****************************************************************************/
489 static void port_begin(FILE *fp
)
494 static void port_end(FILE *fp
)
499 /*}}}************************************************************************/
500 /*{{{ print_endLoop() */
503 /****************************************************************************/
504 static void print_endLoop(FILE *fp
,char indent
,char *head
,int np
,int cpt
,
505 int nbsame
,loop_list
*loop
)
510 iprint(fp
,indent
,"end\n");
511 print_endLoop(fp
,indent
,head
,np
,0,nbsame
-1,loop
->NEXT
);
517 /*}}}************************************************************************/
518 /*{{{ print_loopList() */
521 /****************************************************************************/
522 static void print_loopList(FILE *fp
,char indent
,char *head
,int np
,int *cpt
,
523 int nbsame
,loop_list
*loop
)
528 print_loop(fp
,indent
,head
,np
,cpt
,loop
);
529 print_loopList(fp
,indent
,head
,np
,cpt
,nbsame
- 1,loop
->NEXT
);
533 /*}}}************************************************************************/
534 /*{{{ print_loop() */
537 /****************************************************************************/
538 static void print_loop(FILE *fp
,char indent
,char *head
,int np
,int *cpt
,
544 name
= loop
->VARNAME
;
547 up
= (left
< right
) ? 1 : 0;
549 iprint(fp
,'0',"for (%s = %d; %s %c= %d; %s = %s %c 1)\n",
550 name
,left
,name
,(up
)?'<':'>',right
,name
,name
,(up
)?'+':'-');
551 iprint(fp
,indent
,"begin\n");
558 /*}}}************************************************************************/
559 /*{{{ print_withselect() */
562 /****************************************************************************/
564 print_withselect(FILE *fp
, biabl_list
*biabl
, char *name
,
565 int vectsize
, char **buffer
, int *bufsize
)
571 print_process_declaration(fp
,biabl
,name
,cpt
,"BUS",buffer
,NULL
);
574 iprint(fp
,'i',"begin\n");
577 *buffer
= bgl_abl2str(biabl
->CNDABL
,*buffer
,bufsize
);
579 iprint(fp
,'+',"case (%s)\n",bgl_printabl(*buffer
));
581 for (; biabl
; biabl
= biabl
->NEXT
, cpt
++)
583 if (biabl
->VALABL
== NULL
)
584 beh_toolbug(20,"print_withselect",name
,0);
586 if (biabl
->FLAG
&BEH_CND_SEL_OTHERS
)
587 iprint (fp
,'0',"default : ");
588 else if (biabl
->FLAG
&BEH_CND_SELECT
)
592 bitcnd
= bgl_getBitStr(beh_getcndvectfbiabl(biabl
),buf
);
593 iprint(fp
,'0',"%s : ",bitcnd
);
597 beh_toolbug(19, "print_withselect", name
, 0);
599 fprintf(fp
,"%s = ",name
);
600 print_time(fp
,biabl
->TIME
, 0, 0);
602 *buffer
= bgl_abl2str(biabl
->VALABL
, *buffer
, bufsize
);
603 fprintf(fp
,"%s;\n",bgl_printabl(*buffer
));
606 if ( !(biabl
->FLAG
&BEH_CND_SEL_OTHERS
) )
610 iprint (fp
,'0',"default : ");
611 fprintf(fp
,"%s = ",name
);
612 tmp
= bgl_getBitStr(genHZBitStr(vectsize
),buf
);
613 fprintf(fp
,"%s;\n",tmp
);
617 iprint(fp
,'<',"endcase\n");
618 iprint(fp
,'<',"end\n");
621 /*}}}************************************************************************/
622 /*{{{ print_value() */
625 /****************************************************************************/
627 print_value(FILE *fp
,chain_list
*abl
, char type
, int time
, int timer
, int timef
,
628 char **buffer
, int *bufsize
)
635 print_time(fp
,time
,timer
,timef
);
643 abl
= beg_compactAbl(copyExpr(abl
));
644 *buffer
= bgl_abl2str(abl
, *buffer
, bufsize
);
648 *buffer
= bgl_abl2str(abl
, *buffer
, bufsize
);
649 fprintf(fp
,"%s",bgl_printabl(*buffer
));
656 /*}}}************************************************************************/
657 /*{{{ print_affected() */
660 /****************************************************************************/
662 print_affected(FILE *fp
, char indent
, char type
, int time
, int timer
, int timef
,
663 char *name
, char *vname
, loop_list
*loop
, long specialflags
)
668 iprint(fp
,indent
,"");
673 fprintf(fp
,"assign ");
674 print_special(specialflags
, buf
);
677 print_time_int(fp
,time
,timer
,timef
,0);
679 print_time(fp
,time
,timer
,timef
);
682 if (V_BOOL_TAB
[__MGL_NONBLOKING_ASSIGN
].VALUE
)
689 fprintf(fp
,"%s %s ",bgl_vectorize(name
), sign
);
690 else if (loop
&& loop
->INDEX
)
691 fprintf(fp
,"%s[%s] %s ",name
,loop
->INDEX
, sign
);
693 fprintf(fp
,"%s %s ",vname
, sign
);
696 /*}}}************************************************************************/
697 /*{{{ print_signal() */
700 /****************************************************************************/
702 print_signal(FILE *fp
, char *name
, char type
, int left
, int right
)
709 iprint(fp
,'0',"reg ");
715 iprint(fp
,'0',"wire ");
718 iprint(fp
,'0',"linkage ");
721 name
= bgl_bebusname(name
);
722 else if (type
== 'b')
723 name
= bgl_bebusname(name
);
724 else if (type
== 'P')
725 name
= bgl_beregname(name
);
728 fprintf(fp
,"[%d:%d] %s;\n",left
,right
,bgl_vlogname(name
));
730 fprintf(fp
,"%s;\n",bgl_vlogname(name
));
733 /*}}}************************************************************************/
737 /****************************************************************************/
738 static void print_por(FILE *fp
, char *name
, char dir
, int left
, int right
)
747 mode
= "input"; break;
749 mode
= "output"; break;
751 mode
= "inout"; break;
753 mode
= "input"; break;
757 iprint(fp
,'0',"%-6s [%d:%d] %s", mode
, left
, right
, bgl_vlogname(name
));
759 iprint(fp
,'0',"%-6s %s", mode
, bgl_vlogname(name
));
762 /*}}}************************************************************************/
763 /*{{{ print_interassign() */
766 /****************************************************************************/
767 static void print_interassign(FILE *fp
, char *dest
, char *src
, int mode
)
772 iprint(fp
,'0',"assign %s = %s;\n",bgl_vectorize_sub(dest
,buf0
),bgl_vectorize_sub(src
,buf1
));
774 iprint(fp
,'0',"assign %s = %s;\n", dest
, src
);
777 /*}}}************************************************************************/
778 /*}}}************************************************************************/
779 ///*{{{ Traitement de la begen */
780 ///****************************************************************************/
781 ///*{{{ begen_treatment() */
784 ///****************************************************************************/
786 //begen_treatment(FILE *fp, befig_list *figure)
790 // gen = figure->BEGEN;
792 // iprint(fp,'i',NULL);
795 // iprint(fp,'+',"GENERIC (\n");
797 // for (gen = figure->BEGEN; gen->NEXT; gen = gen->NEXT)
798 // iprint(fp,'0',"CONSTANT %s : %s := %ld ;\t-- %s\n",
799 // gen->NAME,gen->TYPE,*((long *)gen->VALUE),gen->NAME);
801 // iprint(fp,'-',"CONSTANT %s : %s := %ld \t-- %s\n",
802 // gen->NAME,gen->TYPE,*((long *)gen->VALUE),gen->NAME);
803 // iprint(fp,'-',"); \n");
807 ///*}}}************************************************************************/
808 ///*}}}************************************************************************/
809 /*{{{ Traitement des messages */
810 /****************************************************************************/
811 /*{{{ bgl_initDriver() */
814 /****************************************************************************/
815 void bgl_initDriver(bef_driver
*driver
, FILE *fp
, befig_list
*fig
, ht
*porht
,
816 char **buffer
, int *bufsize
)
818 bef_driverSet(driver
,fp
,fig
,buffer
,bufsize
,
828 bef_driverSetAbl(driver
,
830 bef_driverSetProcess(driver
,
832 print_process_declaration
,
840 bef_driverSetStr(driver
,
842 beh_buildvlgvectname
,
847 bef_driverSetPor(driver
,
855 /*}}}************************************************************************/
856 /*}}}************************************************************************/
857 /*{{{ Function de drive */
858 /****************************************************************************/
859 /*{{{ vlogsavebefig() */
861 /* description : print out a text file containing a data-flow VHDL */
863 /****************************************************************************/
865 vlogsavebefig(befig_list
*pthedbefig
, unsigned int trace_mode
)
874 BGL_VECT_ASS
= (int)(long)getenv("DRI_VECT_ASS");
877 beh_toolbug(10, "vlogsavebefig", NULL
, 0);
880 buffer
= mbkalloc(bufsize
);
883 suffix
= V_STR_TAB
[__BGL_FILE_SUFFIX
].VALUE
;
885 suffix
= namealloc("v");
886 fp
= mbkfopen(pthedbefig
->NAME
,suffix
,WRITE_TEXT
);
889 beh_error(107, NULL
);
893 vlgportht
= addht(40);
895 bgl_initDriver(&driver
,fp
,pthedbefig
,vlgportht
,&buffer
,&bufsize
);
896 str
= getenv("BGL_DRIVE_PRIMITIVES");
898 if ((str
[0]=='y') || (str
[0]=='Y'))
899 BGL_DRIVE_PRIMITIVES
= 1;
901 BGL_DRIVE_PRIMITIVES
= 0;
903 BGL_DRIVE_PRIMITIVES
= 0;
907 if ((trace_mode
& BGL_TRACE
) != 0)
908 (void) fprintf(stdout
, "Saving '%s' in a verilog file (%s)\n",
909 pthedbefig
->NAME
, suffix
);
911 sprintf(buffer
,"Verilog data flow description generated from `%s`\n",
913 (void) avt_printExecInfo(fp
, "/*", buffer
, "*/");
915 TIME_UNIT
= bef_gettimeunit(pthedbefig
->TIME_UNIT
);
917 fprintf(fp
, "`timescale 1 %s/1 %s\n\n", TIME_UNIT
, TIME_UNIT
);
920 module_declaration(fp
, pthedbefig
);
922 bef_drive_porDeclaration(&driver
);
923 bef_drive_sigDeclaration(&driver
);
928 bef_drive_aux(&driver
);
929 bef_drive_reg(&driver
);
930 bef_drive_bux(&driver
);
931 bef_drive_bus(&driver
);
932 bef_drive_out(&driver
);
937 (void) fprintf(fp
, "\nendmodule\n");
940 if (V_BOOL_TAB
[__API_DRIVE_CORRESP
].VALUE
)
941 bvl_drivecorresp(pthedbefig
->NAME
, pthedbefig
, 1);
947 /*}}}************************************************************************/
948 /*}}}************************************************************************/