aboutsummaryrefslogtreecommitdiff
path: root/src/tools/mkgenome.c
blob: 3bf153388863074f3eec9bfabdacf9a142326eae (plain) (blame)
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
/* turns a genome image (little-endian 64-bit words on stdin, as written by
   src/tools/build.zc) into genome.h */
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif
#include "memmap.h"
int main(int argc, char **argv)
{
	if (argc == 2 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))) {
		fputs("usage: mkgenome [--help|-h] < image.bin > genome.h\n"
		      "converts little-endian 64-bit image words into a c genome header.\n"
		      "the image must match the memory layout selected when mkgenome is built.\n"
		      "options:\n  --help, -h  show this help and exit\n", stdout);
		return 0;
	}
	if (argc != 1) { fputs("mkgenome: unexpected arguments; use --help for usage\n", stderr); return 2; }
#ifdef _WIN32
	_setmode(_fileno(stdin), _O_BINARY);
	_setmode(_fileno(stdout), _O_BINARY);
#endif
	unsigned char b[8]; int n = 0; size_t got;
	printf("#define GORG %d\nstatic const int64_t genome[] = {", GORG);
	while ((got = fread(b, 1, 8, stdin)) == 8) {
		uint64_t u = (uint64_t)b[0] | (uint64_t)b[1] << 8 | (uint64_t)b[2] << 16 | (uint64_t)b[3] << 24
			| (uint64_t)b[4] << 32 | (uint64_t)b[5] << 40 | (uint64_t)b[6] << 48 | (uint64_t)b[7] << 56;
		if ((n == 0 && u != (uint64_t)FTOP * 2 + 1) ||
		    (n == 1 && u != (uint64_t)ZFP * 2 + 1) || (n == 2 && u != 4)) {
			fprintf(stderr, "mkgenome: input is not a genome for this map\n"); return 1;
		}
		printf("%s%lld", !n ? "\n" : n % 12 ? "," : ",\n", (long long)u);
		n++;
	}
	if (got || ferror(stdin) || n < 7) { fprintf(stderr, "mkgenome: incomplete genome\n"); return 1; }
	printf("\n};\n#define GLEN %d\n", n);
	return n ? 0 : 1;
}