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

Subversion Repositories plasma

[/] [plasma/] [trunk/] [tools/] [ram_image.c] - Blame information for rev 401

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

Line No. Rev Author Line
1 387 rhoads
/* ram_image.c by Steve Rhoads 11/7/05
2
 * This program take the ram_xilinx.vhd file as input
3
 * and the code.txt file as input.
4
 * It then creates ram_image.vhd as output with the
5
 * initialization vectors set to the contents of code.txt.
6
 
7
 UPDATED: 09/07/10 Olivier Rinaudo (orinaudo@gmail.com)
8
 new behaviour: 8KB expandable to 64KB of internal RAM
9
 to be used with new ram_image.vhd enabling expandable
10
 internal ram.
11
 */
12
#include <stdio.h>
13
#include <stdlib.h>
14
#include <string.h>
15
 
16
//1MB buffer
17
#define BUF_SIZE (1024*1024)
18
//we have (up to) 8 blocks composed by 4 RAMB16_S9 instance (one per byte)
19
//each RAMB have 64 hex rows -> max=8*4*64
20
#define RAM_BLOCKS (8)
21
#define RAM_SPLIT (4)
22
#define RAM_ROWS (64)
23
#define RAM_ROWS_TOTAL (RAM_BLOCKS*RAM_SPLIT*RAM_ROWS)
24
#define RAM_DWORDPERROW (8)
25
 
26
 
27
int main(int argc, char *argv[])
28
{
29
   FILE *file;
30
   int i, iposinrow, iblock, irowinsplit, index, size, count;
31
   char *buf, *ptr, *ptr_list[RAM_ROWS_TOTAL], text[80];
32
   unsigned int *code;
33
 
34
   if(argc < 4)
35
   {
36
      printf("Usage: ram_image <in.vhd> <in_code.txt> <out.vhd>\n");
37
      printf("Usage: ram_image ram_xilinx.vhd code.txt ram_image.vhd\n");
38
      return 0;
39
   }
40
 
41
   buf = (char*)malloc(BUF_SIZE);
42
   code = (unsigned int*)malloc(BUF_SIZE);
43
 
44
   //Read ram_xilinx.vhd
45
   file = fopen(argv[1], "rb");
46
   if(file == NULL)
47
   {
48
      printf("Can't open '%s'!\n", argv[1]);
49
      return -1;
50
   }
51
   size = fread(buf, 1, BUF_SIZE, file);
52
   fclose(file);
53
 
54
   //Read code.txt
55
   file = fopen(argv[2], "r");
56
   if(file == NULL)
57
   {
58
      printf("Can't open '%s'!\n", argv[2]);
59
      return -1;
60
   }
61
   //store DWORDs in code buffer
62
   for(count = 0; count < RAM_ROWS_TOTAL*RAM_DWORDPERROW; ++count)
63
   {
64
      if(feof(file))
65
      {
66
         count--;
67
         break;
68
      }
69
      fscanf(file, "%x", &code[count]);
70
   }
71
   fclose(file);
72
 
73
   //Find 'INIT_00 => X"'
74
 
75
   //start at buf, then seek next occurence
76
   ptr = buf;
77
   for(i = 0; i < RAM_ROWS_TOTAL; ++i)
78
   {
79
      sprintf(text, "INIT_%2.2X => X\"", i % RAM_ROWS);
80
      ptr = strstr(ptr, text);
81
      if(ptr == NULL)
82
      {
83
         printf("ERROR: Can't find '%s', block %d, instance %d in '%s'!\n",
84
            text, (i/(RAM_SPLIT*RAM_ROWS)),
85
            (i%(RAM_SPLIT*RAM_ROWS))/RAM_ROWS, argv[1]);
86
         return -1;
87
      }
88
      ptr_list[i] = ptr + strlen(text);
89
   }
90
 
91
   //Modify vhdl source code
92
   iposinrow = RAM_DWORDPERROW*8-2; //start filling from end of line
93
   iblock = 0;
94
   irowinsplit = 0;
95
   for(i = 0; i < count; ++i)
96
   {
97
      sprintf(text, "%8.8x", code[i]);
98
      index = iblock*RAM_ROWS*RAM_SPLIT+irowinsplit;
99
 
100
      ptr_list[index][iposinrow]              = text[0];
101
      ptr_list[index][iposinrow+1]            = text[1];
102
      ptr_list[index+RAM_ROWS][iposinrow]     = text[2];
103
      ptr_list[index+RAM_ROWS][iposinrow+1]   = text[3];
104
      ptr_list[index+RAM_ROWS*2][iposinrow]   = text[4];
105
      ptr_list[index+RAM_ROWS*2][iposinrow+1] = text[5];
106
      ptr_list[index+RAM_ROWS*3][iposinrow]   = text[6];
107
      ptr_list[index+RAM_ROWS*3][iposinrow+1] = text[7];
108
      iposinrow -= 2;
109
      if(iposinrow < 0)
110
      {
111
        iposinrow = RAM_DWORDPERROW*8-2; //reset row
112
        irowinsplit++;
113
        if (irowinsplit>RAM_ROWS-1)
114
        {
115
          irowinsplit = 0;
116
          iblock++;
117
        }
118
      }
119
   }
120
 
121
   //Write ram_image.vhd
122
   file = fopen(argv[3], "wb");
123
   if(file == NULL)
124
   {
125
      printf("Can't write '%s'!\n", argv[3]);
126
      return -1;
127
   }
128
   fwrite(buf, 1, size, file);
129
   fclose(file);
130
   free(buf);
131
   free(code);
132
   return 0;
133
}

powered by: WebSVN 2.1.0

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