1 |
65 |
motilito |
//---------------------------------------------------------------------------------------
|
2 |
|
|
//
|
3 |
|
|
// ihex2vlog.c by Moti Litochevski, Nov 12, 2011
|
4 |
|
|
// This program reads an Intel HEX file and generates memory Verilog module or
|
5 |
|
|
// Xilinx RAMB16/RAMB4 verilog initialization vectors.
|
6 |
|
|
//
|
7 |
|
|
// This program uses the ihex.c functions by Paul Stoffregen.
|
8 |
|
|
//
|
9 |
|
|
// The project was compiled using the Tiny C Compiler using the following command line:
|
10 |
|
|
// tcc ihex2vlog.c ihex.c
|
11 |
|
|
//
|
12 |
|
|
//---------------------------------------------------------------------------------------
|
13 |
|
|
//
|
14 |
|
|
// This file is released to the public domain under the BSD 2-clause license.
|
15 |
|
|
//
|
16 |
|
|
// Copyright (c) 2012, Moti Litochevski
|
17 |
|
|
// All rights reserved.
|
18 |
|
|
//
|
19 |
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
20 |
|
|
// permitted provided that the following conditions are met:
|
21 |
|
|
// o Redistributions of source code must retain the above copyright notice, this list
|
22 |
|
|
// of conditions and the following disclaimer.
|
23 |
|
|
// o Redistributions in binary form must reproduce the above copyright notice, this
|
24 |
|
|
// list of conditions and the following disclaimer in the documentation and/or
|
25 |
|
|
// other materials provided with the distribution.
|
26 |
|
|
//
|
27 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
28 |
|
|
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
29 |
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
30 |
|
|
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
31 |
|
|
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
32 |
|
|
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
33 |
|
|
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
34 |
|
|
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
35 |
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
36 |
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
37 |
|
|
//
|
38 |
|
|
//---------------------------------------------------------------------------------------
|
39 |
|
|
|
40 |
|
|
#include <stdio.h>
|
41 |
|
|
#include <stdlib.h>
|
42 |
|
|
#include <string.h>
|
43 |
|
|
|
44 |
|
|
// constants
|
45 |
|
|
#define MAX_BUF_SIZE 65536
|
46 |
|
|
|
47 |
|
|
/* this loads an intel hex file into the memory[] array */
|
48 |
|
|
int load_file(char *filename);
|
49 |
|
|
|
50 |
|
|
// the loaded memory is stored in a global variable with maximum size of 64K bytes
|
51 |
|
|
int memory[MAX_BUF_SIZE];
|
52 |
|
|
|
53 |
|
|
// Xilinx RAMB16 default parameters
|
54 |
|
|
// total size of RAM memory block in bytes
|
55 |
|
|
#define RAM_BLOCK_SIZE 2048
|
56 |
|
|
// maxmimum number of memory blocks - each memory block contains RAM_BLOCK_SIZE bytes
|
57 |
|
|
#define RAM_BLOCKS 8
|
58 |
|
|
// number of rows in RAM block initialization vectors
|
59 |
|
|
#define RAM_ROWS 64
|
60 |
|
|
// number of bytes per row in RAM block initialization vectors
|
61 |
|
|
#define RAM_BYTEPERROW 32
|
62 |
|
|
// Xilinx RAMB4 parameters
|
63 |
|
|
#define RAMB4_BLOCK_SIZE 512
|
64 |
|
|
#define RAMB4_BLOCKS 32
|
65 |
|
|
#define RAMB4_ROWS 16
|
66 |
|
|
#define RAMB4_BYTEPERROW 32
|
67 |
|
|
|
68 |
|
|
//------------------------------------------------------------------------------
|
69 |
|
|
int main (int argc, char *argv[])
|
70 |
|
|
{
|
71 |
|
|
FILE *file;
|
72 |
|
|
int index, hex_len, block_num, iblock, irow;
|
73 |
|
|
int address, value, argi;
|
74 |
|
|
int block_size, blocks_num, raws_num, bytes_num;
|
75 |
|
|
char *argstr, modname[24];
|
76 |
|
|
|
77 |
|
|
// init block size to zero to sign generic Verilog code
|
78 |
|
|
block_size = 0;
|
79 |
|
|
// default address width
|
80 |
|
|
raws_num = 16;
|
81 |
|
|
bytes_num = 0;
|
82 |
|
|
// set default module name
|
83 |
|
|
strcpy(modname, "ram_image");
|
84 |
|
|
|
85 |
|
|
// announce program start
|
86 |
|
|
printf("ihex2vlog conversion tool:\n");
|
87 |
|
|
|
88 |
|
|
// check program usage
|
89 |
|
|
if (argc < 3) {
|
90 |
|
|
printf("\n");
|
91 |
|
|
printf("ERROR: incorrect usage of program.\n");
|
92 |
|
|
printf("\n");
|
93 |
|
|
printf("Usage: ihex2vlog [-a/s/m/4/16] <in.hex> <out.v>\n");
|
94 |
|
|
printf("optional parameters:\n");
|
95 |
|
|
printf(" -a<width> generate initialization vectors for generic Verilog\n");
|
96 |
|
|
printf(" code with specified address bus width. value should be\n");
|
97 |
|
|
printf(" in the range 8 to 16.\n");
|
98 |
|
|
printf(" this is the default option with width = 16\n");
|
99 |
|
|
printf(" -s<value> set size of generic verilog memory size.\n");
|
100 |
|
|
printf(" value should be in the range 256 to 65536.\n");
|
101 |
|
|
printf(" default value is 2**<width> (address width defined above).\n");
|
102 |
|
|
printf(" -m<name> set module name for generic verilog memory.\n");
|
103 |
|
|
printf(" default value is \"ram_image\".\n");
|
104 |
|
|
printf(" -4 generate initialization vectors for Xilinx RAMB4.\n");
|
105 |
|
|
printf(" -16 generate initialization vectors for Xilinx RAMB16.\n");
|
106 |
|
|
printf("\n");
|
107 |
|
|
printf("Example: ihex2vlog test.ihx ram_image.v\n");
|
108 |
|
|
return -1;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
// clear the memory array
|
112 |
|
|
for (index = 0; index < MAX_BUF_SIZE; index++) {
|
113 |
|
|
memory[index] = 0;
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
// check optional options
|
117 |
|
|
argi = 1;
|
118 |
|
|
argstr = argv[argi];
|
119 |
|
|
while (argstr[0]=='-') {
|
120 |
|
|
// check Xilinx RAMB4 option
|
121 |
|
|
if (argstr[1] == '4') {
|
122 |
|
|
// init block definition values for Xilinx RAMB4 block size
|
123 |
|
|
block_size = RAMB4_BLOCK_SIZE;
|
124 |
|
|
blocks_num = RAMB4_BLOCKS;
|
125 |
|
|
raws_num = RAMB4_ROWS;
|
126 |
|
|
bytes_num = RAMB4_BYTEPERROW;
|
127 |
|
|
}
|
128 |
|
|
else if ((argstr[1] == '1') & (argstr[2] == '6')) {
|
129 |
|
|
// init block definition values for Xilinx RAMB16 block size
|
130 |
|
|
block_size = RAM_BLOCK_SIZE;
|
131 |
|
|
blocks_num = RAM_BLOCKS;
|
132 |
|
|
raws_num = RAM_ROWS;
|
133 |
|
|
bytes_num = RAM_BYTEPERROW;
|
134 |
|
|
}
|
135 |
|
|
else if (argstr[1] == 'a') {
|
136 |
|
|
// for generic infered RAM Verilog code this option specifies the
|
137 |
|
|
// address bus width
|
138 |
|
|
sscanf(&argstr[2], "%d", &raws_num);
|
139 |
|
|
if ((raws_num < 8) | (raws_num > 16)) {
|
140 |
|
|
printf("\nERROR: Address width value error (%d)\n\n", raws_num);
|
141 |
|
|
return -1;
|
142 |
|
|
}
|
143 |
|
|
//check if memory length should be calculated
|
144 |
|
|
if (bytes_num == 0) {
|
145 |
|
|
// calculate the actual memory size
|
146 |
|
|
bytes_num=1;
|
147 |
|
|
for (index=0; index<raws_num; index++)
|
148 |
|
|
bytes_num=bytes_num*2;
|
149 |
|
|
}
|
150 |
|
|
}
|
151 |
|
|
else if (argstr[1] == 's') {
|
152 |
|
|
// set memory size option
|
153 |
|
|
sscanf(&argstr[2], "%d", &bytes_num);
|
154 |
|
|
if ((bytes_num < 256) | (bytes_num > MAX_BUF_SIZE)) {
|
155 |
|
|
printf("\nERROR: Memory size value error (%d)\n\n", bytes_num);
|
156 |
|
|
return -1;
|
157 |
|
|
}
|
158 |
|
|
}
|
159 |
|
|
else if (argstr[1] == 'm') {
|
160 |
|
|
// set generic verilog memory module name
|
161 |
|
|
strcpy(modname, &argstr[2]);
|
162 |
|
|
}
|
163 |
|
|
else
|
164 |
|
|
printf("\nERROR: Unsupported option \"%s\"\n\n", argstr);
|
165 |
|
|
// update parameter index
|
166 |
|
|
argi++;
|
167 |
|
|
argstr = argv[argi];
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
// read input hex file into the memory array
|
171 |
|
|
hex_len = load_file(argv[argi]);
|
172 |
|
|
printf("HEX memory top address %d\n", hex_len);
|
173 |
|
|
// check if file loaded OK
|
174 |
|
|
if (hex_len < 1) {
|
175 |
|
|
printf("ERROR: Can't read '%s'!\n", argv[argi]);
|
176 |
|
|
return -1;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
// announce output file name
|
180 |
|
|
printf("Writing output file to: %s\n", argv[argi+1]);
|
181 |
|
|
// open output file
|
182 |
|
|
file = fopen(argv[argi+1], "wt");
|
183 |
|
|
if (file == NULL) {
|
184 |
|
|
printf("ERROR: Can't write '%s'!\n", argv[argi+1]);
|
185 |
|
|
return -1;
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
// check if Xilinx RAMB memory is used or generic verilog RAM
|
189 |
|
|
if (block_size) {
|
190 |
|
|
// calculate the number of required RAM blocks
|
191 |
|
|
block_num = hex_len / block_size;
|
192 |
|
|
printf("HEX file requires %d RAM blocks\n", block_num+1);
|
193 |
|
|
|
194 |
|
|
// write file header
|
195 |
|
|
fprintf(file, "// RAM image for input code file: %s\n", argv[argi]);
|
196 |
|
|
// write memory block defines to enable only required memory blocks
|
197 |
|
|
fprintf(file, "// enable memory blocks \n");
|
198 |
|
|
fprintf(file, "`ifdef EN_ALL_BLOCKS\n");
|
199 |
|
|
for (iblock = 0; iblock < blocks_num; iblock++) {
|
200 |
|
|
fprintf(file, "`define EN_BLOCK%d 1 \n", iblock);
|
201 |
|
|
}
|
202 |
|
|
fprintf(file, "`else\n");
|
203 |
|
|
// write the memory block enable flags
|
204 |
|
|
for (iblock = 0; iblock < block_num+1; iblock++) {
|
205 |
|
|
fprintf(file, "`define EN_BLOCK%d 1 \n", iblock);
|
206 |
|
|
}
|
207 |
|
|
fprintf(file, "`endif\n");
|
208 |
|
|
fprintf(file, "\n");
|
209 |
|
|
|
210 |
|
|
// write memory blocks
|
211 |
|
|
for (iblock = 0; iblock <= block_num; iblock++) {
|
212 |
|
|
// write memory block header
|
213 |
|
|
fprintf(file, "// block %d \n", iblock);
|
214 |
|
|
|
215 |
|
|
// loop though block rows
|
216 |
|
|
for (irow = 0; irow < raws_num; irow++) {
|
217 |
|
|
// write start of line
|
218 |
|
|
fprintf(file, "defparam mem%d.INIT_%X%X = 256'h", iblock, irow/16, irow & 0xf);
|
219 |
|
|
|
220 |
|
|
// write memory bytes
|
221 |
|
|
for (index = 0; index < bytes_num; index++) {
|
222 |
|
|
address = iblock*block_size + irow*bytes_num + bytes_num - index - 1;
|
223 |
|
|
|
224 |
|
|
if (address < hex_len)
|
225 |
|
|
value = memory[address] & 0xff;
|
226 |
|
|
else
|
227 |
|
|
value = 0;
|
228 |
|
|
|
229 |
|
|
fprintf(file, "%x%x", value/16, value & 0xf);
|
230 |
|
|
}
|
231 |
|
|
fprintf(file, ";\n");
|
232 |
|
|
}
|
233 |
|
|
}
|
234 |
|
|
}
|
235 |
|
|
else {
|
236 |
|
|
// generate generic Verilog RAM code
|
237 |
|
|
printf("Generate generic Verilog RAM code.\n");
|
238 |
|
|
|
239 |
|
|
// write output file header
|
240 |
|
|
fprintf(file, "//-----------------------------------------------------------------------------\n");
|
241 |
|
|
fprintf(file, "//\n");
|
242 |
|
|
fprintf(file, "// RAM image for input code file: %s\n", argv[argi]);
|
243 |
|
|
fprintf(file, "//\n");
|
244 |
|
|
fprintf(file, "//-----------------------------------------------------------------------------\n");
|
245 |
|
|
fprintf(file, "module %s\n", modname);
|
246 |
|
|
fprintf(file, "(\n");
|
247 |
|
|
fprintf(file, " clk, addr, \n");
|
248 |
|
|
fprintf(file, " we, din, dout\n");
|
249 |
|
|
fprintf(file, ");\n");
|
250 |
|
|
fprintf(file, "//-----------------------------------------------------------------------------\n");
|
251 |
|
|
fprintf(file, "input clk;\n");
|
252 |
|
|
fprintf(file, "input [%d:0] addr;\n", raws_num-1);
|
253 |
|
|
fprintf(file, "input we;\n");
|
254 |
|
|
fprintf(file, "input [7:0] din;\n");
|
255 |
|
|
fprintf(file, "output [7:0] dout;\n");
|
256 |
|
|
fprintf(file, "//-----------------------------------------------------------------------------\n");
|
257 |
|
|
fprintf(file, "reg [7:0] dout;\n");
|
258 |
|
|
fprintf(file, "reg [7:0] ram [%d:0];\n", bytes_num-1);
|
259 |
|
|
fprintf(file, "//-----------------------------------------------------------------------------\n");
|
260 |
|
|
fprintf(file, "initial \n");
|
261 |
|
|
fprintf(file, "begin\n");
|
262 |
|
|
// dump memory values as RAM init values
|
263 |
|
|
for (index=0; index<bytes_num; index++) {
|
264 |
|
|
if ((index&3) == 0) fprintf(file, " ");
|
265 |
|
|
fprintf(file, "ram[%d] = 8\'h%x%x; ", index, (memory[index]/16)&0xf, memory[index]&0xf);
|
266 |
|
|
if ((index&3) == 3) fprintf(file, "\n");
|
267 |
|
|
}
|
268 |
|
|
fprintf(file, "end\n");
|
269 |
|
|
fprintf(file, "\n");
|
270 |
|
|
fprintf(file, "//-----------------------------------------------------------------------------\n");
|
271 |
|
|
fprintf(file, "always @(posedge clk)\n");
|
272 |
|
|
fprintf(file, "begin\n");
|
273 |
|
|
fprintf(file, " if (we)\n");
|
274 |
68 |
motilito |
fprintf(file, " begin\n");
|
275 |
65 |
motilito |
fprintf(file, " ram[addr] <= din;\n");
|
276 |
68 |
motilito |
fprintf(file, " dout <= din;\n");
|
277 |
|
|
fprintf(file, " end\n");
|
278 |
|
|
fprintf(file, " else\n");
|
279 |
|
|
fprintf(file, " dout <= ram[addr];\n");
|
280 |
65 |
motilito |
fprintf(file, "end\n");
|
281 |
|
|
fprintf(file, "\n");
|
282 |
|
|
fprintf(file, "endmodule\n");
|
283 |
|
|
fprintf(file, "//-----------------------------------------------------------------------------\n");
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
// close output file
|
287 |
|
|
fclose(file);
|
288 |
|
|
return 0;
|
289 |
|
|
}
|
290 |
|
|
//------------------------------------------------------------------------------
|