xh2ucf.c
1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* xh2ucf.c v1 Frank Berndt
* convert hex file to xilinx RAMB4 ucf parameters;
* to be included in .ucf file to initialize xilinx RAMB4*;
*
* xh2ucf instance-name hex-file
*/
#include <stdio.h>
#include <stdlib.h>
char *Cmd;
char *Inst;
char *Hfile;
FILE *hfp;
/*
* print usage message;
*/
void
Usage(void)
{
fprintf(stderr, "Usage: %s instance-name hex-file\n", Cmd);
exit(1);
}
/*
* main entry;
*/
int
main(int argc, char **argv)
{
int n, i, nc, word;
char *s;
char vstr[8];
char valstr[68];
Cmd = argv[0];
if(argc != 3)
Usage();
Inst = argv[1];
Hfile = argv[2];
if( !(hfp = fopen(Hfile, "r"))) {
fprintf(stderr, "%s: cannot open %s\n", Cmd, Hfile);
exit(2);
}
/*
* there must be 16 line of 256 bits each;
* the hex file must contain one 16-bit word per line;
* unspecified bytes are filled with 0s;
*/
printf("# %s.ucf\n# generated file;\n\n", Inst);
for(n = 0; n < 16; n++) {
s = valstr + 64;
*s = 0;
for(i = 0; i < 16; i++) {
nc = fscanf(hfp, "%x", &word);
if((nc != 1) | (word >= 0x10000))
word = 0;
sprintf(vstr, "%04x", word);
*--s = vstr[3];
*--s = vstr[2];
*--s = vstr[1];
*--s = vstr[0];
}
printf("INST \"%s\" INIT_%02X=%s;\n", Inst, n, valstr);
}
printf("\n");
close(hfp);
exit(0);
}