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

Subversion Repositories sc2v

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

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
InsertPort (PortNode * list, char *name, char *tipo, int size)
107
{
108
  PortNode *pl;
109
  pl = (PortNode *) malloc (sizeof (PortNode));
110
  strcpy (pl->name, name);
111
  strcpy (pl->tipo, tipo);
112
  pl->size = size;
113
  SGLIB_LIST_ADD (PortNode, list, pl, next);
114
  return (list);
115
}
116
 
117
void
118
ShowPortList (PortNode * list)
119
{
120
 
121
  PortNode *pll;
122 29 jcastillo
 
123 14 jcastillo
  SGLIB_LIST_MAP_ON_ELEMENTS (PortNode, list, pll, next,
124
  {
125
        printf ("%s ", pll->tipo);
126
        if (pll->size != 0 && pll->size != 1)
127
        {
128
          printf ("[%d:0] ", (-1 + pll->size));}
129
          printf ("%s;\n", pll->name);
130
        }
131
  );
132
  return;
133
}
134
 
135
void
136 22 jcastillo
RegOutputs (PortNode * list, InstanceNode *instances)
137 14 jcastillo
{
138
 
139
  PortNode *pll;
140
  SGLIB_LIST_MAP_ON_ELEMENTS (PortNode, list, pll, next,
141
  {
142
        if (strcmp (pll->tipo, "output") == 0)
143
        {
144 22 jcastillo
         if(!IsWire(pll->name,instances)){
145 14 jcastillo
          printf ("reg ");
146
          if (pll->size != 0 && pll->size != 1)
147
          {
148
            printf ("[%d:0] ", (-1 + pll->size));}
149
            printf ("%s;\n", pll->name);}
150
          }
151 22 jcastillo
         }
152 14 jcastillo
  );
153
  return;
154
}
155
 
156
void
157 29 jcastillo
EnumeratePorts (PortNode *list)
158 14 jcastillo
{
159
  PortNode *pll;
160 29 jcastillo
 
161 14 jcastillo
  SGLIB_LIST_MAP_ON_ELEMENTS (PortNode, list, pll, next,
162
  {
163
        if (pll->next == NULL)
164
        {
165
          printf ("%s", pll->name); break;
166
        }
167
        else
168
        {
169
          printf ("%s,", pll->name);}
170
        }
171
  );
172
  return;
173
}
174
 
175
SignalNode *
176 20 jcastillo
InsertSignal (SignalNode * list, char *name, int size, int arraysize)
177 14 jcastillo
{
178
  SignalNode *sl;
179
 
180
  sl = (SignalNode *) malloc (sizeof (SignalNode));
181
  strcpy (sl->name, name);
182
  sl->size = size;
183 20 jcastillo
  sl->arraysize=arraysize;
184 14 jcastillo
  SGLIB_LIST_ADD (SignalNode, list, sl, next);
185
  return (list);
186
 
187
}
188
 
189
 
190 15 jcastillo
void
191
ShowSignalsList (SignalNode * list, WriteNode * writeslist)
192 14 jcastillo
{
193
  SignalNode *sll;
194
  SGLIB_LIST_MAP_ON_ELEMENTS (SignalNode, list, sll, next,
195
  {
196
        if (IsWrite (writeslist, sll->name))
197
        {
198
          printf ("reg ");
199
          if (sll->size != 0 && sll->size != 1)
200
          {
201
            printf ("[%d:0] ", (-1 + sll->size));
202
          }
203 20 jcastillo
          printf ("%s", sll->name);
204 14 jcastillo
        }
205
        else
206
        {
207
          printf ("wire ");
208
          if (sll->size != 0 && sll->size != 1)
209
          {
210
                printf ("[%d:0] ", (-1 + sll->size));
211
          }
212 20 jcastillo
          printf ("%s", sll->name);
213 14 jcastillo
        }
214 20 jcastillo
        if(sll->arraysize !=0)
215 24 jcastillo
          printf("[%d:0]", (-1 + sll->arraysize));
216 20 jcastillo
        printf(";\n");
217 14 jcastillo
  }
218
  );
219
  return;
220
}
221
 
222
 
223
/* Decides if a signal is a wire or a reg*/
224
int
225
IsWire (char *name, InstanceNode * list)
226
{
227
 
228
  InstanceNode *ill;
229
  SGLIB_LIST_MAP_ON_ELEMENTS (InstanceNode, list, ill, next,
230
  {
231
    BindNode * bll;
232
        SGLIB_LIST_MAP_ON_ELEMENTS (BindNode,ill->bindslist, bll, next,
233
        {
234 19 jcastillo
        if ((strcmp(name,bll->namebind)==0))
235 14 jcastillo
        {
236
         return 1;
237
        }
238
        }
239
        );}
240
  );
241
  return 0;
242
}
243
 
244
 
245
 
246
SensibilityNode *
247
InsertSensibility (SensibilityNode * list, char *name, char *tipo)
248
{
249
  SensibilityNode *sl;
250
  sl = (SensibilityNode *) malloc (sizeof (SensibilityNode));
251
  strcpy (sl->name, name);
252
  strcpy (sl->tipo, tipo);
253
  SGLIB_LIST_ADD (SensibilityNode, list, sl, next);
254
  return (list);
255
}
256
 
257
void
258
ShowSensibilityList (SensibilityNode * list)
259
{
260
  SensibilityNode *sll;
261
  SGLIB_LIST_MAP_ON_ELEMENTS (SensibilityNode, list, sll, next,
262
  {
263
        if (!strcmp (sll->tipo, "posedge")|| !strcmp (sll->tipo,"negedge"))
264
          printf (" %s",sll->tipo);
265
        if (sll->next == NULL)
266
        {
267
          printf (" %s", sll->name); break;
268
        }
269
        else
270
        {
271
          printf (" %s or", sll->name);}
272
    }
273
  );
274
  return;
275
}
276
 
277
 
278
ProcessNode *
279
InsertProcess (ProcessNode * list, char *name,
280
               SensibilityNode * SensibilityList, char *tipo)
281
{
282
  ProcessNode *pl;
283
  pl = (ProcessNode *) malloc (sizeof (ProcessNode));
284
  strcpy (pl->name, name);
285
  strcpy (pl->tipo, tipo);
286
  pl->list = SensibilityList;
287
  SGLIB_LIST_ADD (ProcessNode, list, pl, next);
288
  return (list);
289
}
290
 
291
 
292
void
293
ShowProcessList (ProcessNode * list)
294
{
295
  ProcessNode *pll;
296
  SGLIB_LIST_MAP_ON_ELEMENTS (ProcessNode, list, pll, next,
297
  {
298
        printf ("%s: always @(", pll->name);
299
        ShowSensibilityList (pll->list); printf (")\n");
300
  }
301
  );
302
  return;
303
}
304
 
305
 
306
void
307
ShowProcessCode (ProcessNode * list)
308
{
309
  FILE *archivo;
310
  int readok;
311
  char lookahead;
312
  char *filename;
313
  char auxchar;
314
  char begin[10];
315
 
316
  ProcessNode *pll;
317
  SGLIB_LIST_MAP_ON_ELEMENTS (ProcessNode, list, pll, next,
318
  {
319
    fprintf(stderr,"Writing process code => %s\n",pll->name);
320
        printf ("//%s:\n", pll->name);
321
        filename =(char *) malloc (256 * sizeof (char));
322
        strcpy (filename, pll->name);
323
        strcat (filename, (char *) "_regs.sc2v");
324
        archivo = fopen (filename, (char *) "r");
325
        while (1)
326
        {
327
          readok =fread ((void *) &auxchar, sizeof (char), 1,archivo);
328
          if (readok) printf ("%c", auxchar);
329
          else
330
            break;
331
        }
332
    fclose (archivo);
333
    printf ("always @(");
334
 
335
    ShowSensibilityList (pll->list);
336
    printf (" )\n");
337
    printf ("   begin\n");
338
    strcpy (filename, pll->name);
339
    strcat (filename, (char *) ".sc2v");
340
    archivo = fopen (filename, (char *) "r");
341
 
342
        /*Read the initial begin of the file */
343
    fscanf (archivo, "%s", begin);
344
    readok =fread ((void *) &auxchar, sizeof (char), 1,archivo);
345
 
346
        /*Trim the beggining of the file */
347
        while (auxchar == '\n' || auxchar == ' ' || auxchar == '\t')
348
            readok =fread ((void *) &auxchar, sizeof (char), 1,archivo); printf ("\n   %c", auxchar);
349
 
350
        while (1){
351
          readok = fread ((void *) &auxchar, sizeof (char), 1,archivo);
352
          if (readok){
353
             if (strcmp (pll->tipo, "comb") == 0 && auxchar == '<')
354
                 {
355
                   readok = fread ((void *) &lookahead, sizeof (char), 1, archivo);
356
                   if (readok){
357
                          if (lookahead == '='){
358
                              auxchar = lookahead;
359
                          }else{
360
                              printf ("%c", auxchar);
361 19 jcastillo
                              auxchar = lookahead;
362 14 jcastillo
                          }
363
                   }
364
             }
365
                 printf ("%c", auxchar);
366
          }
367
      else
368
      {
369
        break;
370
          }
371
    }
372
 
373
    fclose (archivo);
374
    }
375
  );
376
 
377
}
378
 
379
InstanceNode *
380
InsertInstance (InstanceNode * list, char *nameinstance, char *namemodulo)
381
{
382
  InstanceNode *il;
383
  il = (InstanceNode *) malloc (sizeof (InstanceNode));
384
  strcpy (il->nameinstance, nameinstance);
385
  strcpy (il->namemodulo, namemodulo);
386
  il->bindslist = NULL;
387
  SGLIB_LIST_ADD (InstanceNode, list, il, next);
388
  return (list);
389
}
390
 
391
BindNode *
392
InsertBind (BindNode * list, char *nameport, char *namebind)
393
{
394
  BindNode *bl;
395
  bl = (BindNode *) malloc (sizeof (BindNode));
396
  strcpy (bl->nameport, nameport);
397
  strcpy (bl->namebind, namebind);
398
  SGLIB_LIST_ADD (BindNode, list, bl, next);
399
  return (list);
400
 
401
}
402
 
403
 
404
void
405
ShowInstancedModules (InstanceNode * list)
406
{
407
  InstanceNode *ill;
408
  SGLIB_LIST_MAP_ON_ELEMENTS (InstanceNode, list, ill, next,
409
  {
410
        printf ("%s %s (", ill->namemodulo,ill->nameinstance);
411
        BindNode * bll;
412
        SGLIB_LIST_MAP_ON_ELEMENTS (BindNode,ill->bindslist, bll,next,
413
        {
414
          printf (".%s(%s)",bll->nameport,bll->namebind);
415
          if (bll->next == NULL)
416
            printf (");\n");
417
          else
418
            printf (", ");}
419
    );}
420
  );
421
}
422
 
423
 
424
EnumeratesNode *InsertEnumerates (EnumeratesNode * list, char *name)
425
{
426
 
427
  EnumeratesNode *el;
428
  el = (EnumeratesNode *) malloc (sizeof (EnumeratesNode));
429
  strcpy (el->name, name);
430
  SGLIB_LIST_ADD (EnumeratesNode, list, el, next);
431
  return (list);
432
}
433
 
434
 
435
int
436
ShowEnumeratesList (EnumeratesNode *list)
437
{
438
 
439
  EnumeratesNode *ell;
440
  int i = 0;
441
 
442
  printf ("parameter  %s = 0", list->name);
443 22 jcastillo
 
444
  if(list->next!=NULL){
445 14 jcastillo
    list=list->next;
446 22 jcastillo
    printf(",\n");
447
    i=1;
448 14 jcastillo
    SGLIB_LIST_MAP_ON_ELEMENTS (EnumeratesNode,list, ell,next,
449
    {
450 19 jcastillo
      if(ell->next==NULL)
451
          {
452
                printf("           %s = %d;\n\n",ell->name,i);
453
                return(i);
454
          }
455
          else
456
          {
457
            printf("           %s = %d,\n",ell->name,i);
458
          }
459
          i++;
460 14 jcastillo
        }
461
        );
462
  }else{
463 19 jcastillo
    printf(";\n\n");
464 14 jcastillo
        return(i);
465
  }
466
}
467
 
468
EnumListNode *
469
InsertEnumList (EnumListNode * list, EnumeratesNode * enumlist, char *name,int istype)
470
{
471
  EnumListNode *el;
472
  el = (EnumListNode *) malloc (sizeof (EnumListNode));
473
  strcpy (el->name, name);
474
  el->istype=istype;
475
  el->list=enumlist;
476
  SGLIB_LIST_ADD (EnumListNode, list, el, next);
477
  return (list);
478
}
479
 
480
void
481
ShowEnumListList (EnumListNode * list)
482
{
483
 
484
  int items;
485
  EnumListNode *ell;
486
  double bits, bits_round;
487
  int bits_i;
488
 
489
  SGLIB_LIST_MAP_ON_ELEMENTS(EnumListNode,list, ell,next,
490
  {
491
 
492
          items = ShowEnumeratesList (ell->list);
493
 
494
          //Calculate the number of bits needed to represent the enumerate
495
          bits = log ((double) (items + 1)) / log (2.0);
496
          bits_i = bits;
497
          bits_round = bits_i;
498
          if (bits_round != bits)
499
            bits_i++;
500
          if (bits_i == 0)
501
            bits_i = 1;
502
 
503
          if (!(ell->istype))
504
            {
505 22 jcastillo
            if ((bits_i - 1) != 0)
506 14 jcastillo
                  printf ("reg [%d:0] %s;\n\n", bits_i - 1, ell->name);
507 22 jcastillo
            else
508 14 jcastillo
                  printf ("reg %s;\n\n", ell->name);
509
            }
510
 
511
  }
512
  );
513
}
514
 
515
 
516
int
517
findEnumList (EnumListNode * list, char *name)
518
{
519
 
520
  int i = 0;
521
  EnumListNode *ell;
522
 
523
  SGLIB_LIST_MAP_ON_ELEMENTS (EnumListNode,list, ell,next,
524
  {
525
          //printf("%s %s %d %d\n", aux->name,name,aux->istype,i);
526
          if ((strcmp (ell->name, name) == 0) && ((ell->istype) == 1))
527
          {
528
         return i;
529
      }
530
 
531
          i++;
532
   }
533
   );
534
   return -1;
535
}
536
 
537
 
538
int
539
findEnumerateLength (EnumListNode * list, int offset)
540
{
541
 
542
  int i,j = 0;
543
  double bits, bits_round;
544
  int bits_i;
545
 
546
  EnumListNode *ell;
547
  EnumeratesNode *enumll;
548
 
549
  j=0;
550
  i=0;
551
 
552
  SGLIB_LIST_MAP_ON_ELEMENTS (EnumListNode,list, ell,next,
553
  {
554
    i++;
555
    if(i==offset+1){
556
      SGLIB_LIST_MAP_ON_ELEMENTS (EnumeratesNode,ell->list, enumll,next,
557
      {
558
            j++;
559
          });
560
        }
561
  });
562
 
563
   //Calculate the number of bits needed to represent the enumerate
564
   bits = log ((double) (j)) / log (2.0);
565
   bits_i = bits;
566
   bits_round = bits_i;
567
 
568
   if (bits_round != bits)
569
    bits_i++;
570
   if (bits_i == 0)
571
        bits_i = 1;
572
 
573
   return bits_i;
574
 
575
 }
576 16 jcastillo
 
577
 /* Functions for functions inputs list*/
578
FunctionInputNode *InsertFunctionInput (FunctionInputNode * list, char *name, int lenght){
579
  FunctionInputNode *fl;
580
  fl = (FunctionInputNode *) malloc (sizeof (FunctionInputNode));
581
  strcpy (fl->name, name);
582
  fl->lenght=lenght;
583
  SGLIB_LIST_ADD (FunctionInputNode, list, fl, next);
584
  return (list);
585
}
586
 
587
void ShowFunctionInputs (FunctionInputNode * list){
588
 
589
  FunctionInputNode *fll;
590 29 jcastillo
 
591
  SGLIB_LIST_REVERSE(FunctionInputNode,list, next);
592
 
593 16 jcastillo
  SGLIB_LIST_MAP_ON_ELEMENTS (FunctionInputNode,list, fll,next,
594
  {
595 29 jcastillo
    if(fll->lenght!=1)
596 16 jcastillo
     printf("input [%d:0] %s;\n",(fll->lenght)-1,fll->name);
597
    else
598 29 jcastillo
     printf("input %s;\n",fll->name);
599 16 jcastillo
  });
600
}
601
 
602
/* Functions for functions list*/
603
FunctionNode *InsertFunction (FunctionNode *list, char *name,FunctionInputNode *InputsList,int outputlenght){
604
  FunctionNode *fl;
605
  fl = (FunctionNode *) malloc (sizeof (FunctionNode));
606
  strcpy (fl->name, name);
607
  fl->outputlenght=outputlenght;
608
  fl->list = InputsList;
609
  SGLIB_LIST_ADD (FunctionNode, list, fl, next);
610
  return (list);
611
}
612
 
613
void ShowFunctionCode (FunctionNode *list){
614
 
615
  FILE *archivo;
616
  int readok;
617
  char *filename;
618
  char auxchar;
619
  char begin[10];
620
 
621
  FunctionNode *fll;
622
 
623
  SGLIB_LIST_MAP_ON_ELEMENTS (FunctionNode,list, fll,next,
624
  {
625
        if(fll->outputlenght!=1)
626
     printf("function [%d:0] %s;\n\n",(fll->outputlenght)-1,fll->name);
627
    else
628
         printf("function %s;\n\n",fll->name);
629
 
630
        ShowFunctionInputs(fll->list);
631
 
632
        //Show Registers
633
        filename =(char *) malloc (256 * sizeof (char));
634
        strcpy (filename, fll->name);
635
        strcat (filename, (char *) "_regs.sc2v");
636
        archivo = fopen (filename, (char *) "r");
637
        if(archivo==NULL){
638
                fprintf(stderr,"Error opening file %s\n",filename);
639
                exit(1);
640
        }
641
        printf("\n");
642
        while (1)
643
        {
644
          readok =fread ((void *) &auxchar, sizeof (char), 1,archivo);
645
          if (readok) printf ("%c", auxchar);
646
          else{
647
                break;
648
          }
649
        }
650
    fclose (archivo);
651
 
652
        printf ("\n   begin\n");
653
    strcpy (filename, fll->name);
654
    strcat (filename, (char *) ".sc2v");
655
    archivo = fopen (filename, (char *) "r");
656
 
657
        /*Read the initial begin of the file */
658
    fscanf (archivo, "%s", begin);
659
    readok =fread ((void *) &auxchar, sizeof (char), 1,archivo);
660
 
661
        /*Trim the beggining of the file */
662
        while (auxchar == '\n' || auxchar == ' ' || auxchar == '\t')
663
            readok =fread ((void *) &auxchar, sizeof (char), 1,archivo); printf ("\n   %c", auxchar);
664
 
665
        while (1){
666
          readok = fread ((void *) &auxchar, sizeof (char), 1,archivo);
667
          if (readok)
668
             printf ("%c", auxchar);
669
          else
670
         break;
671
        }
672
    printf("endfunction\n\n");
673
 
674
    fclose (archivo);
675
 
676
  });
677
 
678
}

powered by: WebSVN 2.1.0

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