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

Subversion Repositories sc2v

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

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 14 jcastillo
 *  Provided by OpenSoc Design
5
 *
6
 *  www.opensocdesign.com
7
 *
8
 * -----------------------------------------------------------------------------
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Library General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program; if not, write to the Free Software
21
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
 */
23
 
24
#include <stdlib.h>
25
#include <stdio.h>
26
#include <math.h>
27
 
28
#include "sc2v_step2.h"
29
 
30
void
31
ShowDefines (char *filedefines)
32
{
33
 
34
  int readok;
35
 
36
  char *auxchar;
37
  FILE *file;
38
 
39
  file = fopen (filedefines, (char *) "r");
40
 
41
  while (1){
42
    readok = fread ((void *) &auxchar, sizeof (char), 1, file);
43
    if (readok){
44
          printf ("%c", auxchar);
45
        }else{
46
          break;
47
        }
48
  }
49
}
50
 
51
WriteNode *
52
InsertWrite (WriteNode * list, char *name)
53
{
54
  WriteNode *wl;
55
 
56
  wl = (WriteNode *) malloc (sizeof (WriteNode));
57
  strcpy (wl->name, name);
58
  SGLIB_LIST_ADD (WriteNode, list, wl, next);
59
  return (list);
60
}
61
 
62
int
63
IsWrite (WriteNode * list, char *name)
64
{
65
  WriteNode *wll;
66
  SGLIB_LIST_MAP_ON_ELEMENTS (WriteNode, list, wll, next,
67
  {
68
        if ((strcmp (name, (char *) wll->name) == 0)) return (1);
69
    }
70
  );
71
  return (0);
72
}
73
 
74
void
75
ShowWritesList (WriteNode * list)
76
{
77
  WriteNode *wll;
78
  SGLIB_LIST_MAP_ON_ELEMENTS (WriteNode, list, wll, next,{
79
        printf ("%s\n", wll->name);
80
    }
81
  );
82
  return;
83
}
84
 
85
 
86
 
87
WriteNode *
88
ReadWritesFile (WriteNode *list,char *name)
89
{
90
 
91
  char *leido;
92
  int ret;
93
  FILE *file_writes;
94
  file_writes = fopen (name, (char *) "r");
95
 
96
  leido = (char *) malloc (256 * sizeof (char));
97
 
98
  while (1){
99
      ret = fscanf (file_writes, "%s", leido);
100
      if (ret == EOF)
101
        break;
102
      list = InsertWrite (list, leido);
103
  }
104
  return(list);
105
}
106
 
107
PortNode *
108
InsertPort (PortNode * list, char *name, char *tipo, int size)
109
{
110
  PortNode *pl;
111
  pl = (PortNode *) malloc (sizeof (PortNode));
112
  strcpy (pl->name, name);
113
  strcpy (pl->tipo, tipo);
114
  pl->size = size;
115
  SGLIB_LIST_ADD (PortNode, list, pl, next);
116
  return (list);
117
}
118
 
119
void
120
ShowPortList (PortNode * list)
121
{
122
 
123
  PortNode *pll;
124
  SGLIB_LIST_MAP_ON_ELEMENTS (PortNode, list, pll, next,
125
  {
126
        printf ("%s ", pll->tipo);
127
        if (pll->size != 0 && pll->size != 1)
128
        {
129
          printf ("[%d:0] ", (-1 + pll->size));}
130
          printf ("%s;\n", pll->name);
131
        }
132
  );
133
  return;
134
}
135
 
136
void
137
RegOutputs (PortNode * list)
138
{
139
 
140
  PortNode *pll;
141
  SGLIB_LIST_MAP_ON_ELEMENTS (PortNode, list, pll, next,
142
  {
143
        if (strcmp (pll->tipo, "output") == 0)
144
        {
145
          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
  );
152
  return;
153
}
154
 
155
void
156
EnumeratePorts (PortNode * list)
157
{
158
  PortNode *pll;
159
 
160
  SGLIB_LIST_MAP_ON_ELEMENTS (PortNode, list, pll, next,
161
  {
162
        if (pll->next == NULL)
163
        {
164
          printf ("%s", pll->name); break;
165
        }
166
        else
167
        {
168
          printf ("%s,", pll->name);}
169
        }
170
  );
171
  return;
172
}
173
 
174
SignalNode *
175 20 jcastillo
InsertSignal (SignalNode * list, char *name, int size, int arraysize)
176 14 jcastillo
{
177
  SignalNode *sl;
178
 
179
  sl = (SignalNode *) malloc (sizeof (SignalNode));
180
  strcpy (sl->name, name);
181
  sl->size = size;
182 20 jcastillo
  sl->arraysize=arraysize;
183 14 jcastillo
  SGLIB_LIST_ADD (SignalNode, list, sl, next);
184
  return (list);
185
 
186
}
187
 
188
 
189 15 jcastillo
void
190
ShowSignalsList (SignalNode * list, WriteNode * writeslist)
191 14 jcastillo
{
192
  SignalNode *sll;
193
  SGLIB_LIST_MAP_ON_ELEMENTS (SignalNode, list, sll, next,
194
  {
195
        if (IsWrite (writeslist, sll->name))
196
        {
197
          printf ("reg ");
198
          if (sll->size != 0 && sll->size != 1)
199
          {
200
            printf ("[%d:0] ", (-1 + sll->size));
201
          }
202 20 jcastillo
          printf ("%s", sll->name);
203 14 jcastillo
        }
204
        else
205
        {
206
          printf ("wire ");
207
          if (sll->size != 0 && sll->size != 1)
208
          {
209
                printf ("[%d:0] ", (-1 + sll->size));
210
          }
211 20 jcastillo
          printf ("%s", sll->name);
212 14 jcastillo
        }
213 20 jcastillo
        if(sll->arraysize !=0)
214
          printf("[%d:0]", (-1 + sll->size));
215
        printf(";\n");
216 14 jcastillo
  }
217
  );
218
  return;
219
}
220
 
221
 
222
/* Decides if a signal is a wire or a reg*/
223
int
224
IsWire (char *name, InstanceNode * list)
225
{
226
 
227
  InstanceNode *ill;
228
  SGLIB_LIST_MAP_ON_ELEMENTS (InstanceNode, list, ill, next,
229
  {
230
    BindNode * bll;
231
        SGLIB_LIST_MAP_ON_ELEMENTS (BindNode,ill->bindslist, bll, next,
232
        {
233 19 jcastillo
        if ((strcmp(name,bll->namebind)==0))
234 14 jcastillo
        {
235
         return 1;
236
        }
237
        }
238
        );}
239
  );
240
  return 0;
241
}
242
 
243
 
244
 
245
SensibilityNode *
246
InsertSensibility (SensibilityNode * list, char *name, char *tipo)
247
{
248
  SensibilityNode *sl;
249
  sl = (SensibilityNode *) malloc (sizeof (SensibilityNode));
250
  strcpy (sl->name, name);
251
  strcpy (sl->tipo, tipo);
252
  SGLIB_LIST_ADD (SensibilityNode, list, sl, next);
253
  return (list);
254
}
255
 
256
void
257
ShowSensibilityList (SensibilityNode * list)
258
{
259
  SensibilityNode *sll;
260
  SGLIB_LIST_MAP_ON_ELEMENTS (SensibilityNode, list, sll, next,
261
  {
262
        if (!strcmp (sll->tipo, "posedge")|| !strcmp (sll->tipo,"negedge"))
263
          printf (" %s",sll->tipo);
264
        if (sll->next == NULL)
265
        {
266
          printf (" %s", sll->name); break;
267
        }
268
        else
269
        {
270
          printf (" %s or", sll->name);}
271
    }
272
  );
273
  return;
274
}
275
 
276
 
277
ProcessNode *
278
InsertProcess (ProcessNode * list, char *name,
279
               SensibilityNode * SensibilityList, char *tipo)
280
{
281
  ProcessNode *pl;
282
  pl = (ProcessNode *) malloc (sizeof (ProcessNode));
283
  strcpy (pl->name, name);
284
  strcpy (pl->tipo, tipo);
285
  pl->list = SensibilityList;
286
  SGLIB_LIST_ADD (ProcessNode, list, pl, next);
287
  return (list);
288
}
289
 
290
 
291
void
292
ShowProcessList (ProcessNode * list)
293
{
294
  ProcessNode *pll;
295
  SGLIB_LIST_MAP_ON_ELEMENTS (ProcessNode, list, pll, next,
296
  {
297
        printf ("%s: always @(", pll->name);
298
        ShowSensibilityList (pll->list); printf (")\n");
299
  }
300
  );
301
  return;
302
}
303
 
304
 
305
void
306
ShowProcessCode (ProcessNode * list)
307
{
308
  FILE *archivo;
309
  int readok;
310
  char lookahead;
311
  char *filename;
312
  char auxchar;
313
  char begin[10];
314
 
315
  ProcessNode *pll;
316
  SGLIB_LIST_MAP_ON_ELEMENTS (ProcessNode, list, pll, next,
317
  {
318
    fprintf(stderr,"Writing process code => %s\n",pll->name);
319
        printf ("//%s:\n", pll->name);
320
        filename =(char *) malloc (256 * sizeof (char));
321
        strcpy (filename, pll->name);
322
        strcat (filename, (char *) "_regs.sc2v");
323
        archivo = fopen (filename, (char *) "r");
324
        while (1)
325
        {
326
          readok =fread ((void *) &auxchar, sizeof (char), 1,archivo);
327
          if (readok) printf ("%c", auxchar);
328
          else
329
            break;
330
        }
331
    fclose (archivo);
332
    printf ("always @(");
333
 
334
    ShowSensibilityList (pll->list);
335
    printf (" )\n");
336
    printf ("   begin\n");
337
    strcpy (filename, pll->name);
338
    strcat (filename, (char *) ".sc2v");
339
    archivo = fopen (filename, (char *) "r");
340
 
341
        /*Read the initial begin of the file */
342
    fscanf (archivo, "%s", begin);
343
    readok =fread ((void *) &auxchar, sizeof (char), 1,archivo);
344
 
345
        /*Trim the beggining of the file */
346
        while (auxchar == '\n' || auxchar == ' ' || auxchar == '\t')
347
            readok =fread ((void *) &auxchar, sizeof (char), 1,archivo); printf ("\n   %c", auxchar);
348
 
349
        while (1){
350
          readok = fread ((void *) &auxchar, sizeof (char), 1,archivo);
351
          if (readok){
352
             if (strcmp (pll->tipo, "comb") == 0 && auxchar == '<')
353
                 {
354
                   readok = fread ((void *) &lookahead, sizeof (char), 1, archivo);
355
                   if (readok){
356
                          if (lookahead == '='){
357
                              auxchar = lookahead;
358
                          }else{
359
                              printf ("%c", auxchar);
360 19 jcastillo
                              auxchar = lookahead;
361 14 jcastillo
                          }
362
                   }
363
             }
364
                 printf ("%c", auxchar);
365
          }
366
      else
367
      {
368
        break;
369
          }
370
    }
371
 
372
    fclose (archivo);
373
    }
374
  );
375
 
376
}
377
 
378
InstanceNode *
379
InsertInstance (InstanceNode * list, char *nameinstance, char *namemodulo)
380
{
381
  InstanceNode *il;
382
  il = (InstanceNode *) malloc (sizeof (InstanceNode));
383
  strcpy (il->nameinstance, nameinstance);
384
  strcpy (il->namemodulo, namemodulo);
385
  il->bindslist = NULL;
386
  SGLIB_LIST_ADD (InstanceNode, list, il, next);
387
  return (list);
388
}
389
 
390
BindNode *
391
InsertBind (BindNode * list, char *nameport, char *namebind)
392
{
393
  BindNode *bl;
394
  bl = (BindNode *) malloc (sizeof (BindNode));
395
  strcpy (bl->nameport, nameport);
396
  strcpy (bl->namebind, namebind);
397
  SGLIB_LIST_ADD (BindNode, list, bl, next);
398
  return (list);
399
 
400
}
401
 
402
 
403
void
404
ShowInstancedModules (InstanceNode * list)
405
{
406
  InstanceNode *ill;
407
  SGLIB_LIST_MAP_ON_ELEMENTS (InstanceNode, list, ill, next,
408
  {
409
        printf ("%s %s (", ill->namemodulo,ill->nameinstance);
410
        BindNode * bll;
411
        SGLIB_LIST_MAP_ON_ELEMENTS (BindNode,ill->bindslist, bll,next,
412
        {
413
          printf (".%s(%s)",bll->nameport,bll->namebind);
414
          if (bll->next == NULL)
415
            printf (");\n");
416
          else
417
            printf (", ");}
418
    );}
419
  );
420
}
421
 
422
 
423
EnumeratesNode *InsertEnumerates (EnumeratesNode * list, char *name)
424
{
425
 
426
  EnumeratesNode *el;
427
  el = (EnumeratesNode *) malloc (sizeof (EnumeratesNode));
428
  strcpy (el->name, name);
429
  SGLIB_LIST_ADD (EnumeratesNode, list, el, next);
430
  return (list);
431
}
432
 
433
 
434
int
435
ShowEnumeratesList (EnumeratesNode *list)
436
{
437
 
438
  EnumeratesNode *ell;
439
  int i = 0;
440
 
441
  SGLIB_LIST_REVERSE(EnumeratesNode, list, next);
442
 
443
  printf ("parameter  %s = 0", list->name);
444
 
445
  if(list->next!=NULL)
446
    list=list->next;
447
 
448
  if(list->next!=NULL){
449
        printf(",\n");
450
        i=1;
451
    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
        if ((bits_i - 1) != 0)
509
                  printf ("reg [%d:0] %s;\n\n", bits_i - 1, ell->name);
510
        else
511
                  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
FunctionInputNode *InsertFunctionInput (FunctionInputNode * list, char *name, int lenght){
582
  FunctionInputNode *fl;
583
  fl = (FunctionInputNode *) malloc (sizeof (FunctionInputNode));
584
  strcpy (fl->name, name);
585
  fl->lenght=lenght;
586
  SGLIB_LIST_ADD (FunctionInputNode, list, fl, next);
587
  return (list);
588
}
589
 
590
void ShowFunctionInputs (FunctionInputNode * list){
591
 
592
  FunctionInputNode *fll;
593
 
594
  SGLIB_LIST_MAP_ON_ELEMENTS (FunctionInputNode,list, fll,next,
595
  {
596
        if(fll->lenght!=1)
597
     printf("input [%d:0] %s;\n",(fll->lenght)-1,fll->name);
598
    else
599
         printf("input %s;\n",fll->name);
600
  });
601
}
602
 
603
/* Functions for functions list*/
604
FunctionNode *InsertFunction (FunctionNode *list, char *name,FunctionInputNode *InputsList,int outputlenght){
605
  FunctionNode *fl;
606
  fl = (FunctionNode *) malloc (sizeof (FunctionNode));
607
  strcpy (fl->name, name);
608
  fl->outputlenght=outputlenght;
609
  fl->list = InputsList;
610
  SGLIB_LIST_ADD (FunctionNode, list, fl, next);
611
  return (list);
612
}
613
 
614
void ShowFunctionCode (FunctionNode *list){
615
 
616
  FILE *archivo;
617
  int readok;
618
  char *filename;
619
  char auxchar;
620
  char begin[10];
621
 
622
  FunctionNode *fll;
623
 
624
  SGLIB_LIST_MAP_ON_ELEMENTS (FunctionNode,list, fll,next,
625
  {
626
        if(fll->outputlenght!=1)
627
     printf("function [%d:0] %s;\n\n",(fll->outputlenght)-1,fll->name);
628
    else
629
         printf("function %s;\n\n",fll->name);
630
 
631
        ShowFunctionInputs(fll->list);
632
 
633
        //Show Registers
634
        filename =(char *) malloc (256 * sizeof (char));
635
        strcpy (filename, fll->name);
636
        strcat (filename, (char *) "_regs.sc2v");
637
        archivo = fopen (filename, (char *) "r");
638
        if(archivo==NULL){
639
                fprintf(stderr,"Error opening file %s\n",filename);
640
                exit(1);
641
        }
642
        printf("\n");
643
        while (1)
644
        {
645
          readok =fread ((void *) &auxchar, sizeof (char), 1,archivo);
646
          if (readok) printf ("%c", auxchar);
647
          else{
648
                break;
649
          }
650
        }
651
    fclose (archivo);
652
 
653
        printf ("\n   begin\n");
654
    strcpy (filename, fll->name);
655
    strcat (filename, (char *) ".sc2v");
656
    archivo = fopen (filename, (char *) "r");
657
 
658
        /*Read the initial begin of the file */
659
    fscanf (archivo, "%s", begin);
660
    readok =fread ((void *) &auxchar, sizeof (char), 1,archivo);
661
 
662
        /*Trim the beggining of the file */
663
        while (auxchar == '\n' || auxchar == ' ' || auxchar == '\t')
664
            readok =fread ((void *) &auxchar, sizeof (char), 1,archivo); printf ("\n   %c", auxchar);
665
 
666
        while (1){
667
          readok = fread ((void *) &auxchar, sizeof (char), 1,archivo);
668
          if (readok)
669
             printf ("%c", auxchar);
670
          else
671
         break;
672
        }
673
    printf("endfunction\n\n");
674
 
675
    fclose (archivo);
676
 
677
  });
678
 
679
}

powered by: WebSVN 2.1.0

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