Root/plasma/tools/ramimage.c

1/* 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#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11#define BUF_SIZE (1024*1024)
12
13unsigned int swap_int (unsigned int data)
14{
15  unsigned char* b=(unsigned char*)&data;
16  unsigned char t;
17  t=b[3]; b[3]=b[0]; b[0]=t;
18  t=b[2]; b[2]=b[1]; b[1]=t;
19  return data;
20}
21
22
23
24int main(int argc, char *argv[])
25{
26   FILE *file;
27   int i, j, index, size, count;
28   char *buf, *ptr, *ptr_list[64*4], text[80];
29   unsigned int *code;
30
31   if(argc < 4)
32   {
33      printf("Usage: ram_image <in.vhd> <file.bin> <out.vhd>\n");
34      printf("Usage: ram_image ram_xilinx.vhd <file.bin> ram_image.vhd\n");
35      return 0;
36   }
37
38   buf = (char*)malloc(BUF_SIZE);
39   code = (unsigned int*)malloc(BUF_SIZE);
40
41   //Read ram_xilinx.vhd
42   file = fopen(argv[1], "rb");
43   if(file == NULL)
44   {
45      printf("Can't open %s!\n", argv[1]);
46      return -1;
47   }
48   size = fread(buf, 1, BUF_SIZE, file);
49   fclose(file);
50
51   //Read binary file
52   file = fopen(argv[2], "r");
53   if(file == NULL)
54   {
55      printf("Can't open %s!\n", argv[2]);
56      return -1;
57   }
58   for(count = 0; count < 16*1024; ++count)
59   {
60      if(feof(file))
61         break;
62      fread ( &code[count], 1, sizeof(unsigned int), file);
63   }
64   fclose(file);
65
66   //Find 'INIT_00 => X"'
67   ptr = buf;
68   for(i = 0; i < 64*4; ++i)
69   {
70      sprintf(text, "INIT_%2.2X => X\"", i % 64);
71      ptr = strstr(ptr, text);
72      if(ptr == NULL)
73      {
74         printf("ERROR: Can't find '%s' in file!\n", text);
75         return -1;
76      }
77      ptr_list[i] = ptr + strlen(text);
78   }
79
80   //Modify vhdl source code
81   //ptr_list[64*4] four banks of 64 bytes: Bank0 D31-D24 Bank1 D23-D16 Bank2 D15-D8 Bank3 D7-D0
82   j = 62;
83   for(i = 0; i < count; ++i)
84   {
85      sprintf(text, "%8.8x", swap_int(code[i]));
86      index = i / 32;
87      ptr_list[index][j] = text[0]; // Bank0 D31- D28
88      ptr_list[index][j+1] = text[1]; // Bank0 D27- D24
89      ptr_list[index+64][j] = text[2]; // Bank0 D23- D20
90      ptr_list[index+64][j+1] = text[3]; // Bank0 D19- D16
91      ptr_list[index+128][j] = text[4]; // Bank0 D15- D12
92      ptr_list[index+128][j+1] = text[5]; // Bank0 D11- D8
93      ptr_list[index+192][j] = text[6]; // Bank0 D7 - D4
94      ptr_list[index+192][j+1] = text[7]; // Bank0 D3 - D0
95      j -= 2;
96      if(j < 0)
97         j = 62;
98   }
99
100   //Write ram_image.vhd
101   file = fopen(argv[3], "wb");
102   if(file == NULL)
103   {
104      printf("Can't write %s!\n", argv[3]);
105      return -1;
106   }
107   fwrite(buf, 1, size, file);
108   fclose(file);
109   free(buf);
110   free(code);
111   return 0;
112}
113

Archive Download this file

Branches:
master



interactive