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

Subversion Repositories sc2v

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

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jcastillo
/* -----------------------------------------------------------------------------
2
 *
3
 *  SystemC to Verilog Translator v0.3
4
 *  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
void
189
ShowSignalsList (SignalNode * list)
190
{
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
        if ((strcmp(name,bll->namebind) ==0))
230
        {
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
                          }
357
                   }
358
             }
359
                 printf ("%c", auxchar);
360
          }
361
      else
362
      {
363
        break;
364
          }
365
    }
366
 
367
    fclose (archivo);
368
    }
369
  );
370
 
371
}
372
 
373
InstanceNode *
374
InsertInstance (InstanceNode * list, char *nameinstance, char *namemodulo)
375
{
376
  InstanceNode *il;
377
  il = (InstanceNode *) malloc (sizeof (InstanceNode));
378
  strcpy (il->nameinstance, nameinstance);
379
  strcpy (il->namemodulo, namemodulo);
380
  il->bindslist = NULL;
381
  SGLIB_LIST_ADD (InstanceNode, list, il, next);
382
  return (list);
383
}
384
 
385
BindNode *
386
InsertBind (BindNode * list, char *nameport, char *namebind)
387
{
388
  BindNode *bl;
389
  bl = (BindNode *) malloc (sizeof (BindNode));
390
  strcpy (bl->nameport, nameport);
391
  strcpy (bl->namebind, namebind);
392
  SGLIB_LIST_ADD (BindNode, list, bl, next);
393
  return (list);
394
 
395
}
396
 
397
 
398
void
399
ShowInstancedModules (InstanceNode * list)
400
{
401
  InstanceNode *ill;
402
  SGLIB_LIST_MAP_ON_ELEMENTS (InstanceNode, list, ill, next,
403
  {
404
        printf ("%s %s (", ill->namemodulo,ill->nameinstance);
405
        BindNode * bll;
406
        SGLIB_LIST_MAP_ON_ELEMENTS (BindNode,ill->bindslist, bll,next,
407
        {
408
          printf (".%s(%s)",bll->nameport,bll->namebind);
409
          if (bll->next == NULL)
410
            printf (");\n");
411
          else
412
            printf (", ");}
413
    );}
414
  );
415
}
416
 
417
 
418
EnumeratesNode *InsertEnumerates (EnumeratesNode * list, char *name)
419
{
420
 
421
  EnumeratesNode *el;
422
  el = (EnumeratesNode *) malloc (sizeof (EnumeratesNode));
423
  strcpy (el->name, name);
424
  SGLIB_LIST_ADD (EnumeratesNode, list, el, next);
425
  return (list);
426
}
427
 
428
 
429
int
430
ShowEnumeratesList (EnumeratesNode *list)
431
{
432
 
433
  EnumeratesNode *ell;
434
  int i = 0;
435
 
436
  SGLIB_LIST_REVERSE(EnumeratesNode, list, next);
437
 
438
  printf ("parameter  %s = 0", list->name);
439
 
440
  if(list->next!=NULL)
441
    list=list->next;
442
 
443
  if(list->next!=NULL){
444
        printf(",\n");
445
        i=1;
446
    SGLIB_LIST_MAP_ON_ELEMENTS (EnumeratesNode,list, ell,next,
447
    {
448
      if(ell->next==NULL)
449
          {
450
                printf("           %s = %d;\n\n",ell->name,i);
451
                return(i);
452
          }
453
          else
454
          {
455
            printf("           %s = %d,\n",ell->name,i);
456
          }
457
          i++;
458
        }
459
        );
460
  }else{
461
    printf(";\n\n");
462
        return(i);
463
  }
464
}
465
 
466
EnumListNode *
467
InsertEnumList (EnumListNode * list, EnumeratesNode * enumlist, char *name,int istype)
468
{
469
  EnumListNode *el;
470
  el = (EnumListNode *) malloc (sizeof (EnumListNode));
471
  strcpy (el->name, name);
472
  el->istype=istype;
473
  el->list=enumlist;
474
  SGLIB_LIST_ADD (EnumListNode, list, el, next);
475
  return (list);
476
}
477
 
478
void
479
ShowEnumListList (EnumListNode * list)
480
{
481
 
482
  int items;
483
  EnumListNode *ell;
484
  double bits, bits_round;
485
  int bits_i;
486
 
487
  SGLIB_LIST_MAP_ON_ELEMENTS(EnumListNode,list, ell,next,
488
  {
489
 
490
          items = ShowEnumeratesList (ell->list);
491
 
492
          //Calculate the number of bits needed to represent the enumerate
493
          bits = log ((double) (items + 1)) / log (2.0);
494
          bits_i = bits;
495
          bits_round = bits_i;
496
          if (bits_round != bits)
497
            bits_i++;
498
          if (bits_i == 0)
499
            bits_i = 1;
500
 
501
          if (!(ell->istype))
502
            {
503
        if ((bits_i - 1) != 0)
504
                  printf ("reg [%d:0] %s;\n\n", bits_i - 1, ell->name);
505
        else
506
                  printf ("reg %s;\n\n", ell->name);
507
            }
508
 
509
  }
510
  );
511
}
512
 
513
 
514
int
515
findEnumList (EnumListNode * list, char *name)
516
{
517
 
518
  int i = 0;
519
  EnumListNode *ell;
520
 
521
  SGLIB_LIST_MAP_ON_ELEMENTS (EnumListNode,list, ell,next,
522
  {
523
          //printf("%s %s %d %d\n", aux->name,name,aux->istype,i);
524
          if ((strcmp (ell->name, name) == 0) && ((ell->istype) == 1))
525
          {
526
         return i;
527
      }
528
 
529
          i++;
530
   }
531
   );
532
   return -1;
533
}
534
 
535
 
536
int
537
findEnumerateLength (EnumListNode * list, int offset)
538
{
539
 
540
  int i,j = 0;
541
  double bits, bits_round;
542
  int bits_i;
543
 
544
  EnumListNode *ell;
545
  EnumeratesNode *enumll;
546
 
547
  j=0;
548
  i=0;
549
 
550
  SGLIB_LIST_MAP_ON_ELEMENTS (EnumListNode,list, ell,next,
551
  {
552
    i++;
553
    if(i==offset+1){
554
      SGLIB_LIST_MAP_ON_ELEMENTS (EnumeratesNode,ell->list, enumll,next,
555
      {
556
            j++;
557
          });
558
        }
559
  });
560
 
561
   //Calculate the number of bits needed to represent the enumerate
562
   bits = log ((double) (j)) / log (2.0);
563
   bits_i = bits;
564
   bits_round = bits_i;
565
 
566
   if (bits_round != bits)
567
    bits_i++;
568
   if (bits_i == 0)
569
        bits_i = 1;
570
 
571
   return bits_i;
572
 
573
 }

powered by: WebSVN 2.1.0

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