OpenCores
URL https://opencores.org/ocsvn/sc2v/sc2v/trunk

Subversion Repositories sc2v

[/] [sc2v/] [trunk/] [src/] [sc2v_step2.c] - Blame information for rev 39

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jcastillo
/* -----------------------------------------------------------------------------
2
 *
3 16 jcastillo
 *  SystemC to Verilog Translator v0.4
4 31 jcastillo
 *  Provided by Universidad Rey Juan Carlos
5 14 jcastillo
 *
6
 * -----------------------------------------------------------------------------
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU Library General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
 */
21
 
22
#include <stdlib.h>
23
#include <stdio.h>
24
#include <math.h>
25
 
26
#include "sc2v_step2.h"
27
 
28
void
29
ShowDefines (char *filedefines)
30
{
31
 
32
  int readok;
33
 
34
  char *auxchar;
35
  FILE *file;
36
 
37
  file = fopen (filedefines, (char *) "r");
38
 
39
  while (1){
40
    readok = fread ((void *) &auxchar, sizeof (char), 1, file);
41
    if (readok){
42
          printf ("%c", auxchar);
43
        }else{
44
          break;
45
        }
46
  }
47
}
48
 
49
WriteNode *
50
InsertWrite (WriteNode * list, char *name)
51
{
52
  WriteNode *wl;
53
 
54
  wl = (WriteNode *) malloc (sizeof (WriteNode));
55
  strcpy (wl->name, name);
56
  SGLIB_LIST_ADD (WriteNode, list, wl, next);
57
  return (list);
58
}
59
 
60
int
61
IsWrite (WriteNode * list, char *name)
62
{
63
  WriteNode *wll;
64
  SGLIB_LIST_MAP_ON_ELEMENTS (WriteNode, list, wll, next,
65
  {
66
        if ((strcmp (name, (char *) wll->name) == 0)) return (1);
67
    }
68
  );
69
  return (0);
70
}
71
 
72
void
73
ShowWritesList (WriteNode * list)
74
{
75
  WriteNode *wll;
76
  SGLIB_LIST_MAP_ON_ELEMENTS (WriteNode, list, wll, next,{
77
        printf ("%s\n", wll->name);
78
    }
79
  );
80
  return;
81
}
82
 
83
 
84
 
85
WriteNode *
86
ReadWritesFile (WriteNode *list,char *name)
87
{
88
 
89
  char *leido;
90
  int ret;
91
  FILE *file_writes;
92
  file_writes = fopen (name, (char *) "r");
93
 
94
  leido = (char *) malloc (256 * sizeof (char));
95
 
96
  while (1){
97
      ret = fscanf (file_writes, "%s", leido);
98
      if (ret == EOF)
99
        break;
100
      list = InsertWrite (list, leido);
101
  }
102
  return(list);
103
}
104
 
105
PortNode *
106 35 jcastillo
InsertPort (PortNode * list, char *name, char *tipo, int size, int pflag)
107 14 jcastillo
{
108
  PortNode *pl;
109
  pl = (PortNode *) malloc (sizeof (PortNode));
110
  strcpy (pl->name, name);
111
  strcpy (pl->tipo, tipo);
112
  pl->size = size;
113 35 jcastillo
  pl->pflag = pflag;
114 14 jcastillo
  SGLIB_LIST_ADD (PortNode, list, pl, next);
115
  return (list);
116
}
117
 
118
void
119
ShowPortList (PortNode * list)
120
{
121
 
122
  PortNode *pll;
123 29 jcastillo
 
124 14 jcastillo
  SGLIB_LIST_MAP_ON_ELEMENTS (PortNode, list, pll, next,
125
  {
126
        printf ("%s ", pll->tipo);
127 35 jcastillo
        if (pll->pflag == 1) printf("signed ");
128 14 jcastillo
        if (pll->size != 0 && pll->size != 1)
129
        {
130
          printf ("[%d:0] ", (-1 + pll->size));}
131 35 jcastillo
        printf ("%s;\n", pll->name);
132 14 jcastillo
        }
133
  );
134
  return;
135
}
136
 
137
void
138 22 jcastillo
RegOutputs (PortNode * list, InstanceNode *instances)
139 14 jcastillo
{
140
 
141
  PortNode *pll;
142
  SGLIB_LIST_MAP_ON_ELEMENTS (PortNode, list, pll, next,
143
  {
144
        if (strcmp (pll->tipo, "output") == 0)
145
        {
146 22 jcastillo
         if(!IsWire(pll->name,instances)){
147 35 jcastillo
           if (pll->pflag == 1) printf("reg signed "); else printf ("reg ");
148 14 jcastillo
          if (pll->size != 0 && pll->size != 1)
149
          {
150
            printf ("[%d:0] ", (-1 + pll->size));}
151
            printf ("%s;\n", pll->name);}
152
          }
153 22 jcastillo
         }
154 14 jcastillo
  );
155
  return;
156
}
157
 
158
void
159 29 jcastillo
EnumeratePorts (PortNode *list)
160 14 jcastillo
{
161
  PortNode *pll;
162 29 jcastillo
 
163 14 jcastillo
  SGLIB_LIST_MAP_ON_ELEMENTS (PortNode, list, pll, next,
164
  {
165
        if (pll->next == NULL)
166
        {
167
          printf ("%s", pll->name); break;
168
        }
169
        else
170
        {
171
          printf ("%s,", pll->name);}
172
        }
173
  );
174
  return;
175
}
176
 
177
SignalNode *
178 35 jcastillo
InsertSignal (SignalNode * list, char *name, int size, int arraysize,int sflag)
179 14 jcastillo
{
180
  SignalNode *sl;
181
 
182
  sl = (SignalNode *) malloc (sizeof (SignalNode));
183
  strcpy (sl->name, name);
184
  sl->size = size;
185 20 jcastillo
  sl->arraysize=arraysize;
186 35 jcastillo
  sl->sflag=sflag;
187 14 jcastillo
  SGLIB_LIST_ADD (SignalNode, list, sl, next);
188
  return (list);
189
 
190
}
191
 
192
 
193 15 jcastillo
void
194
ShowSignalsList (SignalNode * list, WriteNode * writeslist)
195 14 jcastillo
{
196
  SignalNode *sll;
197
  SGLIB_LIST_MAP_ON_ELEMENTS (SignalNode, list, sll, next,
198
  {
199
        if (IsWrite (writeslist, sll->name))
200
        {
201 35 jcastillo
          if (sll->sflag==1) printf("reg signed "); else printf ("reg ");
202 14 jcastillo
          if (sll->size != 0 && sll->size != 1)
203
          {
204
            printf ("[%d:0] ", (-1 + sll->size));
205
          }
206 20 jcastillo
          printf ("%s", sll->name);
207 14 jcastillo
        }
208
        else
209
        {
210 35 jcastillo
          if (sll->sflag==1) printf("wire signed "); else printf ("wire ");
211 14 jcastillo
          if (sll->size != 0 && sll->size != 1)
212
          {
213
                printf ("[%d:0] ", (-1 + sll->size));
214
          }
215 20 jcastillo
          printf ("%s", sll->name);
216 14 jcastillo
        }
217 20 jcastillo
        if(sll->arraysize !=0)
218 24 jcastillo
          printf("[%d:0]", (-1 + sll->arraysize));
219 20 jcastillo
        printf(";\n");
220 14 jcastillo
  }
221
  );
222
  return;
223
}
224
 
225
 
226
/* Decides if a signal is a wire or a reg*/
227
int
228
IsWire (char *name, InstanceNode * list)
229
{
230
 
231
  InstanceNode *ill;
232
  SGLIB_LIST_MAP_ON_ELEMENTS (InstanceNode, list, ill, next,
233
  {
234
    BindNode * bll;
235
        SGLIB_LIST_MAP_ON_ELEMENTS (BindNode,ill->bindslist, bll, next,
236
        {
237 19 jcastillo
        if ((strcmp(name,bll->namebind)==0))
238 14 jcastillo
        {
239
         return 1;
240
        }
241
        }
242
        );}
243
  );
244
  return 0;
245
}
246
 
247
 
248
 
249
SensibilityNode *
250
InsertSensibility (SensibilityNode * list, char *name, char *tipo)
251
{
252
  SensibilityNode *sl;
253
  sl = (SensibilityNode *) malloc (sizeof (SensibilityNode));
254
  strcpy (sl->name, name);
255
  strcpy (sl->tipo, tipo);
256
  SGLIB_LIST_ADD (SensibilityNode, list, sl, next);
257
  return (list);
258
}
259
 
260
void
261
ShowSensibilityList (SensibilityNode * list)
262
{
263
  SensibilityNode *sll;
264
  SGLIB_LIST_MAP_ON_ELEMENTS (SensibilityNode, list, sll, next,
265
  {
266
        if (!strcmp (sll->tipo, "posedge")|| !strcmp (sll->tipo,"negedge"))
267
          printf (" %s",sll->tipo);
268
        if (sll->next == NULL)
269
        {
270
          printf (" %s", sll->name); break;
271
        }
272
        else
273
        {
274
          printf (" %s or", sll->name);}
275
    }
276
  );
277
  return;
278
}
279
 
280
 
281
ProcessNode *
282
InsertProcess (ProcessNode * list, char *name,
283
               SensibilityNode * SensibilityList, char *tipo)
284
{
285
  ProcessNode *pl;
286
  pl = (ProcessNode *) malloc (sizeof (ProcessNode));
287
  strcpy (pl->name, name);
288
  strcpy (pl->tipo, tipo);
289
  pl->list = SensibilityList;
290
  SGLIB_LIST_ADD (ProcessNode, list, pl, next);
291
  return (list);
292
}
293
 
294
 
295
void
296
ShowProcessList (ProcessNode * list)
297
{
298
  ProcessNode *pll;
299
  SGLIB_LIST_MAP_ON_ELEMENTS (ProcessNode, list, pll, next,
300
  {
301
        printf ("%s: always @(", pll->name);
302
        ShowSensibilityList (pll->list); printf (")\n");
303
  }
304
  );
305
  return;
306
}
307
 
308
 
309
void
310
ShowProcessCode (ProcessNode * list)
311
{
312
  FILE *archivo;
313
  int readok;
314
  char lookahead;
315
  char *filename;
316
  char auxchar;
317
  char begin[10];
318
 
319
  ProcessNode *pll;
320
  SGLIB_LIST_MAP_ON_ELEMENTS (ProcessNode, list, pll, next,
321
  {
322
    fprintf(stderr,"Writing process code => %s\n",pll->name);
323
        printf ("//%s:\n", pll->name);
324
        filename =(char *) malloc (256 * sizeof (char));
325
        strcpy (filename, pll->name);
326
        strcat (filename, (char *) "_regs.sc2v");
327
        archivo = fopen (filename, (char *) "r");
328
        while (1)
329
        {
330
          readok =fread ((void *) &auxchar, sizeof (char), 1,archivo);
331
          if (readok) printf ("%c", auxchar);
332
          else
333
            break;
334
        }
335
    fclose (archivo);
336
    printf ("always @(");
337
 
338
    ShowSensibilityList (pll->list);
339
    printf (" )\n");
340
    printf ("   begin\n");
341
    strcpy (filename, pll->name);
342
    strcat (filename, (char *) ".sc2v");
343
    archivo = fopen (filename, (char *) "r");
344
 
345
        /*Read the initial begin of the file */
346
    fscanf (archivo, "%s", begin);
347
    readok =fread ((void *) &auxchar, sizeof (char), 1,archivo);
348
 
349
        /*Trim the beggining of the file */
350
        while (auxchar == '\n' || auxchar == ' ' || auxchar == '\t')
351
            readok =fread ((void *) &auxchar, sizeof (char), 1,archivo); printf ("\n   %c", auxchar);
352
 
353
        while (1){
354
          readok = fread ((void *) &auxchar, sizeof (char), 1,archivo);
355
          if (readok){
356
             if (strcmp (pll->tipo, "comb") == 0 && auxchar == '<')
357
                 {
358
                   readok = fread ((void *) &lookahead, sizeof (char), 1, archivo);
359
                   if (readok){
360
                          if (lookahead == '='){
361
                              auxchar = lookahead;
362
                          }else{
363
                              printf ("%c", auxchar);
364 19 jcastillo
                              auxchar = lookahead;
365 14 jcastillo
                          }
366
                   }
367
             }
368
                 printf ("%c", auxchar);
369
          }
370
      else
371
      {
372
        break;
373
          }
374
    }
375
 
376
    fclose (archivo);
377
    }
378
  );
379
 
380
}
381
 
382
InstanceNode *
383
InsertInstance (InstanceNode * list, char *nameinstance, char *namemodulo)
384
{
385
  InstanceNode *il;
386
  il = (InstanceNode *) malloc (sizeof (InstanceNode));
387
  strcpy (il->nameinstance, nameinstance);
388
  strcpy (il->namemodulo, namemodulo);
389
  il->bindslist = NULL;
390
  SGLIB_LIST_ADD (InstanceNode, list, il, next);
391
  return (list);
392
}
393
 
394
BindNode *
395
InsertBind (BindNode * list, char *nameport, char *namebind)
396
{
397
  BindNode *bl;
398
  bl = (BindNode *) malloc (sizeof (BindNode));
399
  strcpy (bl->nameport, nameport);
400
  strcpy (bl->namebind, namebind);
401
  SGLIB_LIST_ADD (BindNode, list, bl, next);
402
  return (list);
403
 
404
}
405
 
406
 
407
void
408
ShowInstancedModules (InstanceNode * list)
409
{
410
  InstanceNode *ill;
411
  SGLIB_LIST_MAP_ON_ELEMENTS (InstanceNode, list, ill, next,
412
  {
413
        printf ("%s %s (", ill->namemodulo,ill->nameinstance);
414
        BindNode * bll;
415
        SGLIB_LIST_MAP_ON_ELEMENTS (BindNode,ill->bindslist, bll,next,
416
        {
417
          printf (".%s(%s)",bll->nameport,bll->namebind);
418
          if (bll->next == NULL)
419
            printf (");\n");
420
          else
421
            printf (", ");}
422
    );}
423
  );
424
}
425
 
426
 
427
EnumeratesNode *InsertEnumerates (EnumeratesNode * list, char *name)
428
{
429
 
430
  EnumeratesNode *el;
431
  el = (EnumeratesNode *) malloc (sizeof (EnumeratesNode));
432
  strcpy (el->name, name);
433
  SGLIB_LIST_ADD (EnumeratesNode, list, el, next);
434
  return (list);
435
}
436
 
437
 
438
int
439
ShowEnumeratesList (EnumeratesNode *list)
440
{
441
 
442
  EnumeratesNode *ell;
443
  int i = 0;
444
 
445
  printf ("parameter  %s = 0", list->name);
446 22 jcastillo
 
447
  if(list->next!=NULL){
448 14 jcastillo
    list=list->next;
449 22 jcastillo
    printf(",\n");
450
    i=1;
451 14 jcastillo
    SGLIB_LIST_MAP_ON_ELEMENTS (EnumeratesNode,list, ell,next,
452
    {
453 19 jcastillo
      if(ell->next==NULL)
454
          {
455
                printf("           %s = %d;\n\n",ell->name,i);
456
                return(i);
457
          }
458
          else
459
          {
460
            printf("           %s = %d,\n",ell->name,i);
461
          }
462
          i++;
463 14 jcastillo
        }
464
        );
465
  }else{
466 19 jcastillo
    printf(";\n\n");
467 14 jcastillo
        return(i);
468
  }
469
}
470
 
471
EnumListNode *
472
InsertEnumList (EnumListNode * list, EnumeratesNode * enumlist, char *name,int istype)
473
{
474
  EnumListNode *el;
475
  el = (EnumListNode *) malloc (sizeof (EnumListNode));
476
  strcpy (el->name, name);
477
  el->istype=istype;
478
  el->list=enumlist;
479
  SGLIB_LIST_ADD (EnumListNode, list, el, next);
480
  return (list);
481
}
482
 
483
void
484
ShowEnumListList (EnumListNode * list)
485
{
486
 
487
  int items;
488
  EnumListNode *ell;
489
  double bits, bits_round;
490
  int bits_i;
491
 
492
  SGLIB_LIST_MAP_ON_ELEMENTS(EnumListNode,list, ell,next,
493
  {
494
 
495
          items = ShowEnumeratesList (ell->list);
496
 
497
          //Calculate the number of bits needed to represent the enumerate
498
          bits = log ((double) (items + 1)) / log (2.0);
499
          bits_i = bits;
500
          bits_round = bits_i;
501
          if (bits_round != bits)
502
            bits_i++;
503
          if (bits_i == 0)
504
            bits_i = 1;
505
 
506
          if (!(ell->istype))
507
            {
508 22 jcastillo
            if ((bits_i - 1) != 0)
509 14 jcastillo
                  printf ("reg [%d:0] %s;\n\n", bits_i - 1, ell->name);
510 22 jcastillo
            else
511 14 jcastillo
                  printf ("reg %s;\n\n", ell->name);
512
            }
513
 
514
  }
515
  );
516
}
517
 
518
 
519
int
520
findEnumList (EnumListNode * list, char *name)
521
{
522
 
523
  int i = 0;
524
  EnumListNode *ell;
525
 
526
  SGLIB_LIST_MAP_ON_ELEMENTS (EnumListNode,list, ell,next,
527
  {
528
          //printf("%s %s %d %d\n", aux->name,name,aux->istype,i);
529
          if ((strcmp (ell->name, name) == 0) && ((ell->istype) == 1))
530
          {
531
         return i;
532
      }
533
 
534
          i++;
535
   }
536
   );
537
   return -1;
538
}
539
 
540
 
541
int
542
findEnumerateLength (EnumListNode * list, int offset)
543
{
544
 
545
  int i,j = 0;
546
  double bits, bits_round;
547
  int bits_i;
548
 
549
  EnumListNode *ell;
550
  EnumeratesNode *enumll;
551
 
552
  j=0;
553
  i=0;
554
 
555
  SGLIB_LIST_MAP_ON_ELEMENTS (EnumListNode,list, ell,next,
556
  {
557
    i++;
558
    if(i==offset+1){
559
      SGLIB_LIST_MAP_ON_ELEMENTS (EnumeratesNode,ell->list, enumll,next,
560
      {
561
            j++;
562
          });
563
        }
564
  });
565
 
566
   //Calculate the number of bits needed to represent the enumerate
567
   bits = log ((double) (j)) / log (2.0);
568
   bits_i = bits;
569
   bits_round = bits_i;
570
 
571
   if (bits_round != bits)
572
    bits_i++;
573
   if (bits_i == 0)
574
        bits_i = 1;
575
 
576
   return bits_i;
577
 
578
 }
579 16 jcastillo
 
580
 /* Functions for functions inputs list*/
581 35 jcastillo
FunctionInputNode *InsertFunctionInput (FunctionInputNode * list, char *name, int lenght, int flag){
582 16 jcastillo
  FunctionInputNode *fl;
583
  fl = (FunctionInputNode *) malloc (sizeof (FunctionInputNode));
584
  strcpy (fl->name, name);
585
  fl->lenght=lenght;
586 35 jcastillo
  fl->sgnflag=flag;
587 16 jcastillo
  SGLIB_LIST_ADD (FunctionInputNode, list, fl, next);
588
  return (list);
589
}
590
 
591
void ShowFunctionInputs (FunctionInputNode * list){
592
 
593
  FunctionInputNode *fll;
594 29 jcastillo
 
595
  SGLIB_LIST_REVERSE(FunctionInputNode,list, next);
596
 
597 16 jcastillo
  SGLIB_LIST_MAP_ON_ELEMENTS (FunctionInputNode,list, fll,next,
598
  {
599 35 jcastillo
    if(fll->sgnflag==0)
600
    {
601 29 jcastillo
    if(fll->lenght!=1)
602 16 jcastillo
     printf("input [%d:0] %s;\n",(fll->lenght)-1,fll->name);
603
    else
604 29 jcastillo
     printf("input %s;\n",fll->name);
605 35 jcastillo
     } else {
606
     if(fll->lenght!=1)
607
     printf("input signed [%d:0] %s;\n",(fll->lenght)-1,fll->name);
608
    else
609
     printf("input signed %s;\n",fll->name);
610
     }
611 16 jcastillo
  });
612
}
613
 
614
/* Functions for functions list*/
615 35 jcastillo
FunctionNode *InsertFunction (FunctionNode *list, char *name,FunctionInputNode *InputsList,int outputlenght,int flag){
616 16 jcastillo
  FunctionNode *fl;
617
  fl = (FunctionNode *) malloc (sizeof (FunctionNode));
618
  strcpy (fl->name, name);
619
  fl->outputlenght=outputlenght;
620
  fl->list = InputsList;
621 35 jcastillo
  fl->sgnflag=flag;
622 16 jcastillo
  SGLIB_LIST_ADD (FunctionNode, list, fl, next);
623
  return (list);
624
}
625
 
626
void ShowFunctionCode (FunctionNode *list){
627
 
628
  FILE *archivo;
629
  int readok;
630
  char *filename;
631
  char auxchar;
632
  char begin[10];
633
 
634
  FunctionNode *fll;
635
 
636
  SGLIB_LIST_MAP_ON_ELEMENTS (FunctionNode,list, fll,next,
637
  {
638 35 jcastillo
   if(fll->sgnflag==0)
639
   {
640 16 jcastillo
        if(fll->outputlenght!=1)
641
     printf("function [%d:0] %s;\n\n",(fll->outputlenght)-1,fll->name);
642
    else
643
         printf("function %s;\n\n",fll->name);
644 35 jcastillo
        } else {
645
   if(fll->outputlenght!=1)
646
     printf("function signed [%d:0] %s;\n\n",(fll->outputlenght)-1,fll->name);
647
    else
648
         printf("function signed %s;\n\n",fll->name);
649
   }
650
 
651 16 jcastillo
        ShowFunctionInputs(fll->list);
652
 
653
        //Show Registers
654
        filename =(char *) malloc (256 * sizeof (char));
655
        strcpy (filename, fll->name);
656
        strcat (filename, (char *) "_regs.sc2v");
657
        archivo = fopen (filename, (char *) "r");
658
        if(archivo==NULL){
659
                fprintf(stderr,"Error opening file %s\n",filename);
660
                exit(1);
661
        }
662
        printf("\n");
663
        while (1)
664
        {
665
          readok =fread ((void *) &auxchar, sizeof (char), 1,archivo);
666
          if (readok) printf ("%c", auxchar);
667
          else{
668
                break;
669
          }
670
        }
671
    fclose (archivo);
672
 
673
        printf ("\n   begin\n");
674
    strcpy (filename, fll->name);
675
    strcat (filename, (char *) ".sc2v");
676
    archivo = fopen (filename, (char *) "r");
677
 
678
        /*Read the initial begin of the file */
679
    fscanf (archivo, "%s", begin);
680
    readok =fread ((void *) &auxchar, sizeof (char), 1,archivo);
681
 
682
        /*Trim the beggining of the file */
683
        while (auxchar == '\n' || auxchar == ' ' || auxchar == '\t')
684
            readok =fread ((void *) &auxchar, sizeof (char), 1,archivo); printf ("\n   %c", auxchar);
685
 
686
        while (1){
687
          readok = fread ((void *) &auxchar, sizeof (char), 1,archivo);
688
          if (readok)
689
             printf ("%c", auxchar);
690
          else
691
         break;
692
        }
693
    printf("endfunction\n\n");
694
 
695
    fclose (archivo);
696
 
697
  });
698
 
699
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.