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

Subversion Repositories sc2v

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

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

powered by: WebSVN 2.1.0

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