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

Subversion Repositories sc2v

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

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

powered by: WebSVN 2.1.0

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