1 /* ###--------------------------------------------------------------------### */
2 /* # # */
3 /* # file : vel_util.c # */
4 /* | date : Jun 03 1997 | */
5 /* | version : 1.00 | */
6 /* | author : Denis Hommais | */
7 /* | | */
8 /* # contents : netlist vectorization function using VEL structures # */
9 /* # # */
10 /* ###--------------------------------------------------------------------### */
11 /*
12 * $Log: vel_util.c,v $
13 * Revision 1.1 2002/04/26 09:51:07 ludo
14 * Initial revision
15 *
16 * Revision 1.1.1.1 2002/04/11 08:54:53 fred
17 * Importing mbkvhdlg into the new Alliance CVS tree
18 *
19 * Revision 1.1.1.1 2002/02/28 12:58:31 disydent
20 * Creation of Disydent CVS Tree
21 *
22 * Revision 1.1.1.1 2001/11/19 16:55:27 pwet
23 * Changing the CVS tree structure of disydent
24 *
25 * Revision 1.1.1.1 2001/07/24 13:31:42 pwet
26 * cvs tree of part of disydent
27 *
28 * Revision 1.1.1.1 2001/07/19 14:32:20 pwet
29 * New cvs tree
30 *
31 * Revision 1.7 1998/11/12 10:39:26 pwet
32 * add makevelofig function.
33 *
34 * Revision 1.6 1998/01/06 09:54:27 denis
35 * change strdup to namealloc in mkvhdlname
36 *
37 * Revision 1.5 1997/12/17 16:41:33 denys
38 * add chkdir to check signal direction
39 *
40 * Revision 1.4 1997/12/10 10:43:05 denys
41 * Don't know
42 *
43 * Revision 1.3 1997/09/24 09:46:48 denys
44 * rename the signal when it is connected to a external connector by the
45 * name of the connector
46 *
47 * Revision 1.2 1997/09/23 09:42:46 denys
48 * add reverse in mkvector
49 * change mkvector to verify the vector
50 *
51 */
52 #ident "$Id: vel_util.c,v 1.1 2002/04/26 09:51:07 ludo Exp $"
53
54 #include <stdio.h>
55 #include <unistd.h>
56 #include <string.h>
57 #include <ctype.h>
58 #include <mut.h>
59 #include <mlo.h>
60 #include <mlu.h>
61 #include "vel_velo.h"
62
63 static int signalindex;
64 static char buf[BUFSIZ];
65
66 static void printvelosig(velosig *, char *);
67
68 /* ###--------------------------------------------------------------------### */
69 /* # Function buildvelo # */
70 /* ###--------------------------------------------------------------------### */
buildvelo(lofig_list * f)71 void buildvelo(lofig_list *f)
72 {
73 losig_list *s;
74 locon_list *c;
75 loins_list *i;
76 velosig *vs;
77 velocon *vc;
78 ptype_list *ps, *pc;
79 char *name;
80 long idx;
81
82 ps=f->USER=addptype(f->USER, VEL_SIG, NULL);
83 for (s=f->LOSIG;s;s=s->NEXT) {
84 name=getsigname(s);
85 idx=vectorindex(name);
86 vs=addvelosig(ps, vectorradical(name), idx, idx, s->TYPE);
87 vs->NAMECHAIN=s->NAMECHAIN;
88 s->USER=addptype(s->USER, VEL_SIG, vs);
89 }
90 pc=f->USER=addptype(f->USER, VEL_CON, NULL);
91 for (c=f->LOCON;c;c=c->NEXT) {
92 idx=vectorindex(c->NAME);
93 vc=addvelocon(pc, vectorradical(c->NAME), idx, idx);
94 vc->ROOT=addchain(NULL,c);
95 ps=getptype(c->SIG->USER, VEL_SIG);
96 vc->VSIG=addchain(NULL, ps->DATA);
97 }
98 for (i=f->LOINS;i;i=i->NEXT) {
99 pc=i->USER=addptype(i->USER, VEL_CON, NULL);
100 for (c=i->LOCON;c;c=c->NEXT) {
101 idx=vectorindex(c->NAME);
102 vc=addvelocon(pc, vectorradical(c->NAME), idx, idx);
103 vc->ROOT=addchain(NULL,c);
104 ps=getptype(c->SIG->USER, VEL_SIG);
105 vc->VSIG=addchain(NULL, ps->DATA);
106 }
107 }
108 }
109
110 /* ###--------------------------------------------------------------------### */
111 /* # Function vectorize_velosig # */
112 /* ###--------------------------------------------------------------------### */
113
vectorize_velosig(lofig_list * f)114 void vectorize_velosig(lofig_list *f)
115 {
116 ptype_list *p;
117 velosig *vs, *vv=NULL;
118 chain_list *ch;
119
120 p=getptype(f->USER, VEL_SIG);
121 for (vs=(velosig *)p->DATA;vs;vs=vs->NEXT) {
122 if (vv && vv->NAME==vs->NAME && abs(vv->RIGHT-vs->RIGHT)==1 ) {
123 vv->RIGHT=vs->RIGHT;
124 vv->VSIG=addchain(vv->VSIG, vs);
125 vs->TYPE='V';
126 vs->SUPER=vv;
127 } else {
128 if (vv) {
129 if (vs->NAME==vv->NAME) {
130 /* the vector isn't a valid one */
131 for (ch=vv->VSIG; ch; ch=ch->NEXT)
132 ((velosig *)ch->DATA)->TYPE=vv->TYPE;
133 } else
134 vv->VSIG=(chain_list *)reverse(vv->VSIG);
135 vv=NULL;
136 }
137 if (vs->LEFT!=-1) {
138 vv=dupvelosig(p, vs);
139 vv->VSIG=addchain(vv->VSIG, vs);
140 vs->TYPE='V';
141 vs->SUPER=vv;
142 }
143 }
144 }
145 if (vv)
146 vv->VSIG=(chain_list *)reverse(vv->VSIG);
147 }
148
149 #if 0
150 void vectorize_velosig(lofig_list *f)
151 {
152 ptype_list *p;
153 velosig *vs, vv, *vm;
154 chain_list *ch;
155
156 vv.NAME=NULL;
157 p=getptype(f->USER, VEL_SIG);
158 for (vs=(velosig *)p->DATA;vs;vs=vs->NEXT) {
159 if (vv.NAME && vv.NAME==vs->NAME) {
160 vv.RIGHT=vs->RIGHT;
161 vv.VSIG=addchain(vv.VSIG, vs);
162 vs->TYPE='V';
163 } else {
164 if (vv.NAME) {
165 for(ch=getvelosigbyname(p,vv.NAME); ch; ch=ch->NEXT) {
166 vm=(velosig *)ch->DATA;
167 if ((vm->RIGHT==vv.RIGHT && vm->LEFT == vv.LEFT)
168 || (vm->RIGHT==vv.LEFT && vm->LEFT == vv.RIGHT)) {
169 vm->TYPE=vv.TYPE;
170 vm->VSIG=(chain_list *)reverse(vv.VSIG);
171 }
172 }
173 vv.VSIG=(chain_list *)reverse(vv.VSIG);
174 vv.NAME=NULL;
175 }
176 if (vs->LEFT!=-1) {
177 memcpy(&vv,vs,sizeof(velosig));
178 vv.VSIG=addchain(vv.VSIG, vs);
179 vs->TYPE='V';
180 }
181 }
182 }
183 if (vv.NAME)
184 vv.VSIG=(chain_list *)reverse(vv.VSIG);
185 }
186 #endif
187
188 /* ###--------------------------------------------------------------------### */
189 /* # Function mkvector # */
190 /* ###--------------------------------------------------------------------### */
191
mkvector(lofig_list * f,chain_list * c)192 static chain_list *mkvector(lofig_list *f, chain_list *c)
193 {
194 velosig *s, *e;
195 char *name=NULL;
196 long left=0, right=0;
197 chain_list *r=NULL, *sigchain=NULL;
198 ptype_list *ps;
199
200 ps=getptype(f->USER, VEL_SIG);
201 while(c) {
202 s=(velosig *)c->DATA;
203 if (name && s->NAME == name && abs(s->RIGHT-right)==1 ) {
204 right=s->RIGHT;
205 sigchain=addchain(sigchain, s);
206 } else {
207 if (name) {
208 if (!(e=getvelosig(ps, name, left, right))) {
209 e=addvelosig(ps, name, left, right, 'M');
210 e->VSIG=reverse(sigchain);
211 e->SUPER=getvelosigbyname(ps, name);
212 }
213 r=addchain(r,e);
214 }
215 name=s->NAME;
216 left=s->LEFT;
217 right=s->RIGHT;
218 sigchain=addchain(NULL, s);
219 }
220 c=c->NEXT;
221 }
222 if (!(e=getvelosig(ps, name, left, right))) {
223 e=addvelosig(ps, name, left, right, 'M');
224 e->VSIG=reverse(sigchain);
225 e->SUPER=getvelosigbyname(ps, name);
226 }
227 r=addchain(r,e);
228
229 return reverse(r);
230 }
231
232 /* ###--------------------------------------------------------------------### */
233 /* # Function vectorize_velocon # */
234 /* ###--------------------------------------------------------------------### */
235
vectorize_velocon(lofig_list * f,ptype_list * p)236 void vectorize_velocon(lofig_list *f, ptype_list *p)
237 {
238 velocon *vc, *vv=NULL;
239
240 for (vc=(velocon *)p->DATA;vc;) {
241 if (vv && vv->NAME==vc->NAME) {
242 vv->RIGHT=vc->RIGHT;
243 vc->VSIG->NEXT=vv->VSIG;
244 vv->VSIG=vc->VSIG;
245 delvelocon(p, vc);
246 vc=vv->NEXT;
247 } else {
248 if (vv) {
249 vv->VSIG=(chain_list *)reverse(vv->VSIG);
250 vv->VSIG=mkvector(f, vv->VSIG);
251 vv=NULL;
252 }
253 if (vc->LEFT!=-1) {
254 vv=vc;
255 }
256 vc=vc->NEXT;
257 }
258 }
259 if (vv) {
260 vv->VSIG=(chain_list *)reverse(vv->VSIG);
261 vv->VSIG=mkvector(f, vv->VSIG);
262 }
263 }
264
265 /* ###--------------------------------------------------------------------### */
266 /* # Function sigcmp # */
267 /* ###--------------------------------------------------------------------### */
268
sigcmp(ptype_list * p,chain_list * c)269 static velosig *sigcmp(ptype_list *p, chain_list *c)
270 {
271 velosig *s;
272 chain_list *ms, *mc;
273
274 for(s=(velosig *)p->DATA; s; s=s->NEXT)
275 if (s->TYPE=='S') {
276 for (ms=s->VSIG, mc=c; ms&&mc; ms=ms->NEXT, mc=mc->NEXT)
277 if (ms->DATA != mc->DATA) break;
278 if (!ms && !mc)
279 return s;
280 }
281 return NULL;
282 }
283
284 /* ###--------------------------------------------------------------------### */
285 /* # Function addsignals # */
286 /* ###--------------------------------------------------------------------### */
287
addsignals(lofig_list * f)288 void addsignals(lofig_list *f)
289 {
290 velocon *c;
291 velosig *s;
292 loins_list *i;
293 ptype_list *p, *ps;
294
295 ps=getptype(f->USER, VEL_SIG);
296 for (i=f->LOINS; i; i=i->NEXT) {
297 p=getptype(i->USER, VEL_CON);
298 for (c=(velocon *)p->DATA; c; c=c->NEXT) {
299 if (c->VSIG->NEXT!=NULL) {
300 if (!(s=sigcmp(ps,c->VSIG))) {
301 sprintf(buf, "velo%d%s", signalindex++, c->NAME);
302 s=addvelosig(ps, buf, c->LEFT, c->RIGHT, 'S');
303 s->VSIG=c->VSIG;
304 }
305 c->VSIG=addchain(NULL, s);
306 c->VSIG=reverse(c->VSIG);
307 }
308 }
309 }
310
311 }
312
313 /* ###--------------------------------------------------------------------### */
314 /* # Function getvelosigbyname # */
315 /* ###--------------------------------------------------------------------### */
316
getvelosigbyname(ptype_list * p,char * name)317 velosig *getvelosigbyname(ptype_list *p, char *name)
318 {
319 velosig *vs;
320
321 for (vs=(velosig *)p->DATA; vs; vs=vs->NEXT)
322 if (vs->NAME==name && (vs->TYPE=='E' || vs->TYPE=='I' || vs->TYPE=='R'))
323 return vs;
324
325 return NULL;
326 }
327
328 /* ###--------------------------------------------------------------------### */
329 /* # Function getdir # */
330 /* ###--------------------------------------------------------------------### */
331
getdir(velosig * vs)332 static int getdir (velosig *vs)
333 {
334 long dir;
335
336 dir=vs->RIGHT-vs->LEFT;
337
338 if (dir!=0) {
339 if (dir<0)
340 return -1;
341 else
342 return 1;
343 }
344
345 return 0;
346 }
347
348 /* ###--------------------------------------------------------------------### */
349 /* # Function chkdir # */
350 /* ###--------------------------------------------------------------------### */
351
chkdir(lofig_list * f)352 void chkdir (lofig_list *f)
353 {
354 loins_list *ins;
355 ptype_list *ps;
356 ptype_list *pc;
357 velocon *c;
358 chain_list *ch, **prevch;
359 velosig *vs, *vr;
360 int vsdir,vrdir,i;
361
362 ps=getptype(f->USER, VEL_SIG);
363 pc=getptype(f->USER, VEL_CON);
364 for (c=(velocon *)pc->DATA; c; c=c->NEXT) {
365 prevch=&(c->VSIG);
366 for (ch=c->VSIG; ch; prevch=&(ch->NEXT), ch=ch->NEXT) {
367 vs=(velosig *)ch->DATA;
368 if (vs->TYPE=='M' && (vr=getvelosigbyname(ps, vs->NAME))) {
369 vsdir=getdir(vs);
370 vrdir=getdir(vr);
371 if (vsdir && vrdir && vsdir!=vrdir)
372 if (vr->LEFT==vs->RIGHT && vs->LEFT==vr->RIGHT) {
373 vs->TYPE=vr->TYPE;
374 vr->TYPE='V';
375 }
376 }
377 }
378 }
379
380 for (ins=f->LOINS; ins; ins=ins->NEXT) {
381 pc=getptype(ins->USER, VEL_CON);
382 for (c=(velocon *)pc->DATA; c; c=c->NEXT) {
383 prevch=&(c->VSIG);
384 for (ch=c->VSIG; ch; prevch=&(ch->NEXT), ch=ch->NEXT) {
385 vs=(velosig *)ch->DATA;
386 if (vs->TYPE=='M' && (vr=getvelosigbyname(ps, vs->NAME))) {
387 vsdir=getdir(vs);
388 vrdir=getdir(vr);
389 if (vsdir && vrdir && vsdir!=vrdir)
390 if (vr->TYPE=='I') {
391 if (vr->LEFT==vs->RIGHT && vs->LEFT==vr->RIGHT) {
392 vs->TYPE='R';
393 vs->VSIG=vr->VSIG;
394 } else {
395 addvelosig(ps, vr->NAME, vr->RIGHT, vr->LEFT, 'R');
396 }
397 vr->TYPE='V';
398 } else {
399 printvelosig(vs,">>>");
400 ch->DATA=getvelosig(ps, vs->NAME, vs->RIGHT, vs->RIGHT);
401 printvelosig(ch->DATA,">>>");
402 if (vsdir<0)
403 for (i=vs->RIGHT+1; i<=vs->LEFT; i=i+1)
404 *prevch=addchain(*prevch,
405 getvelosig(ps, vs->NAME,i,i));
406 else
407 for (i=vs->RIGHT-1; i>=vs->LEFT; i=i-1)
408 *prevch=addchain(*prevch,
409 getvelosig(ps, vs->NAME,i,i));
410 }
411 else
412 if (vsdir && vr->TYPE=='I')
413 vr->TYPE='R';
414 }
415 }
416 }
417 }
418 }
419
420 /* ###--------------------------------------------------------------------### */
421 /* # Function addoffset # */
422 /* ###--------------------------------------------------------------------### */
423
addoffset(char * offset,char * add)424 static char *addoffset(char *offset, char *add)
425 {
426 char *no;
427
428 no=(char *)mbkalloc(sizeof(char)*(strlen(offset)+strlen(add)+1));
429 strcpy(no,offset);
430 strcat(no,add);
431
432 return no;
433 }
434
435 /* ###--------------------------------------------------------------------### */
436 /* # Function mkvhdlname # */
437 /* ###--------------------------------------------------------------------### */
438
mkvhdlname(char * name)439 char *mkvhdlname(char *name)
440 {
441 char *n, *c;
442 int f=0;
443
444
445 c=buf;
446 if (!isalpha(*name)) {
447 sprintf(c,"v_%u", signalindex++);
448 c+=2;
449 f=1;
450 }
451
452 for (n=name; *n; n++)
453 if (!isalnum(*n)) {
454 if (*n!='_') {
455 if (c!=buf && *(c-1)=='_')
456 *c++='x';
457 *c++='_';
458 f=1;
459 } else {
460 if (c!=buf && *(c-1)=='_') {
461 *c++='x';
462 f=1;
463 } else
464 *c++=*n;
465 }
466 } else
467 *c++=*n;
468
469
470 if (f) {
471 *c='\0';
472 char buf2[BUFSIZ];
473 while (namefind(buf)) {
474 sprintf(buf2, "d%s", buf);
475 strncpy(buf, buf2, BUFSIZ-1);
476 }
477 return namealloc(buf);
478 } else {
479 return name;
480 }
481 }
482
483 /* ###--------------------------------------------------------------------### */
484 /* # Function printvelosig # */
485 /* ###--------------------------------------------------------------------### */
486
printvelosig(velosig * s,char * offset)487 static void printvelosig(velosig *s, char *offset)
488 {
489 chain_list *c;
490 char *no;
491
492 printf("%s +-%s[%ld:%ld] %c\n", offset, s->NAME, s->LEFT, s->RIGHT, s->TYPE);
493 /* printf("%s +-%s[%8x][%ld:%ld] %c\n", offset, s->NAME, s, s->LEFT, s->RIGHT, s->TYPE);
494 */
495 no=addoffset(offset, " |");
496 for (c=s->VSIG;c;c=c->NEXT)
497 printvelosig(c->DATA, no);
498 mbkfree(no);
499 }
500
501 /* ###--------------------------------------------------------------------### */
502 /* # Function printvelosiglist # */
503 /* ###--------------------------------------------------------------------### */
504
printvelosiglist(velosig * s)505 void printvelosiglist(velosig *s)
506 {
507 while (s) {
508 printvelosig(s," ");
509 s=s->NEXT;
510 }
511 }
512
513 /* ###--------------------------------------------------------------------### */
514 /* # Function printvelocon # */
515 /* ###--------------------------------------------------------------------### */
516
printvelocon(velocon * s,char * offset)517 static void printvelocon(velocon *s, char *offset)
518 {
519 chain_list *c;
520 char *no;
521
522 printf("%s *-%s[%ld:%ld]\n", offset, s->NAME, s->LEFT, s->RIGHT);
523 /* printf("%s *-%s[%8x][%ld:%ld]\n", offset, s->NAME, s, s->LEFT, s->RIGHT);
524 */
525 no=addoffset(offset, " |");
526 for (c=s->VSIG;c;c=c->NEXT)
527 printvelosig(c->DATA, no);
528 mbkfree(no);
529 }
530
531 /* ###--------------------------------------------------------------------### */
532 /* # Function printveloconlist # */
533 /* ###--------------------------------------------------------------------### */
534
printveloconlist(velocon * c)535 void printveloconlist(velocon *c)
536 {
537 while (c) {
538 printvelocon(c," ");
539 c=c->NEXT;
540 }
541 }
542