#include #include #include #include #include #include #include #include // this take these parameters. /* -o -- .c file with rom data -i -- binary file to load -c -- class of the rom -n -- internal name of the rom -d -- Human description of the rom */ int main(int argc, const char * argv[]) { const char * output = NULL; const char * class = NULL; const char * name = NULL; const char * desc = NULL; const char * input = NULL; uint8_t * data = NULL; size_t size = 0; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-o") == 0 && i < argc-1) { output = argv[++i]; } else if (strcmp(argv[i], "-i") == 0 && i < argc-1) { input = argv[++i]; } else if (strcmp(argv[i], "-c") == 0 && i < argc-1) { class = argv[++i]; } else if (strcmp(argv[i], "-n") == 0 && i < argc-1) { name = argv[++i]; } else if (strcmp(argv[i], "-d") == 0 && i < argc-1) { desc = argv[++i]; } else { fprintf(stderr, "Unknown option %s\n", argv[i]); exit(1); } } // stat the file to get the size, load it in data { struct stat st; if (stat(input, &st) < 0) { perror(input); exit(1); } size = st.st_size; if (size == 0) { fprintf(stderr, "File %s is empty, ignored\n", input); exit(1); } data = malloc(size); if (!data) { perror("malloc"); exit(1); } FILE * f = fopen(input, "rb"); if (!f) { perror(input); exit(1); } if (fread(data, 1, size, f) != size) { perror(input); exit(1); } fclose(f); } // open the output file, add a header, the data, and the mii_rom_t struct { char newoutput[1024]; snprintf(newoutput, sizeof(newoutput), "%s.tmp", output); FILE * f = fopen(newoutput, "w"); if (!f) { perror(output); exit(1); } fprintf(f, "/* this file is auto-generated by convert-rom-tcc.c */\n\n"); fprintf(f, "#include \"mii_rom.h\"\n\n"); fprintf(f, "static const uint8_t mii_rom_%s[] = {\n", name); for (size_t i = 0; i < size; i++) { fprintf(f, "0x%02x,", data[i]); if ((i % 16) == 15) fprintf(f, "\n"); } fprintf(f, "};\n"); fprintf(f, "static mii_rom_t %s_rom = {\n", name); fprintf(f, "\t.name = \"%s\",\n", name); fprintf(f, "\t.class = \"%s\",\n", class); fprintf(f, "\t.description = \"%s\",\n", desc); fprintf(f, "\t.rom = mii_rom_%s,\n", name); fprintf(f, "\t.len = %ld,\n", size); fprintf(f, "};\n"); fprintf(f, "MII_ROM(%s_rom);\n", name); fclose(f); // rename the .tmp file to the output file rename(newoutput, output); } }