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

Subversion Repositories sc2v

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

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

powered by: WebSVN 2.1.0

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