xh2v.c 1.3 KB
/* xh2v.c v1 Frank Berndt
 * convert hex file to xilinx RAMB4 verilog init parameters;
 * to be included in .v files that instantiate xilinx RAMB4*;
 *
 * xh2v 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.init\n// generated file;\n\n", Inst);
	printf("`ifdef SIM\n");
	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("\tdefparam %s.INIT_%02X = 256'h%s;\n", Inst, n, valstr);
	}
	printf("`endif // SIM\n");
	close(hfp);
	exit(0);
}