aboutsummaryrefslogtreecommitdiff
path: root/src/tools/mkgenome.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/mkgenome.c')
-rw-r--r--src/tools/mkgenome.c50
1 files changed, 33 insertions, 17 deletions
diff --git a/src/tools/mkgenome.c b/src/tools/mkgenome.c
index 3bf1533..9720760 100644
--- a/src/tools/mkgenome.c
+++ b/src/tools/mkgenome.c
@@ -1,7 +1,12 @@
-/* turns a genome image (little-endian 64-bit words on stdin, as written by
- src/tools/build.zc) into genome.h */
+/* checks a genome image (little-endian 64-bit words on stdin, as written by
+ zc0 or src/tools/build.zc) against the memory map it was built with, and
+ copies it to stdout unchanged. the cores #embed the result (src/core/genome.h),
+ so this is the gate that keeps a compiler's diagnostics out of a core: when
+ the input is not an image for this map, nothing is written and the input is
+ shown on stderr, since it is usually the compiler's error report. */
#include <stdio.h>
#include <stdint.h>
+#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <fcntl.h>
@@ -11,8 +16,9 @@
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"
+ fputs("usage: mkgenome [--help|-h] < image.bin > genome.bin\n"
+ "checks little-endian 64-bit image words against this memory map and copies them\n"
+ "to stdout. input that is not an image is reported on stderr and nothing is written.\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;
@@ -22,19 +28,29 @@ int main(int argc, char **argv)
_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;
+ size_t n = 0, cap = 1 << 20, got;
+ unsigned char *b = malloc(cap);
+ if (!b) { fputs("mkgenome: out of memory\n", stderr); return 1; }
+ while ((got = fread(b + n, 1, cap - n, stdin)) > 0) {
+ n += got;
+ if (n == cap) {
+ unsigned char *next = cap > ((size_t)1 << 30) ? 0 : realloc(b, cap * 2);
+ if (!next) { fputs("mkgenome: image too large\n", stderr); return 1; }
+ b = next; cap *= 2;
}
- 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;
+ if (ferror(stdin)) { fputs("mkgenome: read failed\n", stderr); return 1; }
+ int ok = n >= 8 * 7 && n % 8 == 0;
+ for (int i = 0; ok && i < 3; i++) {
+ uint64_t u = 0;
+ for (int k = 0; k < 8; k++) u |= (uint64_t)b[i * 8 + k] << (k * 8);
+ if (u != (i == 0 ? (uint64_t)FTOP * 2 + 1 : i == 1 ? (uint64_t)ZFP * 2 + 1 : 4)) ok = 0;
+ }
+ if (!ok) {
+ if (n >= 4 && !memcmp(b, "zc: ", 4)) fwrite(b, 1, n, stderr);
+ fprintf(stderr, n % 8 || n < 8 * 7 ? "mkgenome: incomplete genome\n" : "mkgenome: input is not a genome for this map\n");
+ return 1;
+ }
+ if (fwrite(b, 1, n, stdout) != n || fflush(stdout)) { fputs("mkgenome: write failed\n", stderr); return 1; }
+ return 0;
}