romcvt.c 1.3 KB
/*  convert a rom binary file into a .c text file, with a struct named
 *  RomImage
 */  




#include <stdio.h>
#include <stdlib.h>


main()
{
    FILE    *input,*output;
    int     blen,flen,rv,i,c;
    unsigned char    *dataIn;

    input = fopen("rom", "r");
    if (!input)
    {
        printf("couldn't open file rom\n");
        exit(1);
    }

    output = fopen("rom.c", "w");  
    if (!output)
    {
        printf("couldn't open file rom.c \n");
        exit(1);
    }      

    fseek(input, 0, SEEK_END);
    flen = ftell(input);
    rewind(input); 

    blen = flen;
    blen = (blen+4) & ~4;
    
    dataIn = malloc(blen);
    if(!dataIn)
    {
        printf("Unable to alloc memory for buffer\n");
        exit(1);
    }  

    fprintf(output,"int    gRomImageSize = %d;\n\n",blen);
    rv= fread(dataIn, 1, flen, input);
    if(rv!=flen)
        printf("ERROR!!, didn't read correct number of bytes, read %d\n",rv);

    fprintf(output,"unsigned char  gRomImage[] ={\n    ");

    blen--;
    
    for(i=0,c=0;i<blen;i++,c++)
    {
        if(c >= 16)
        {
            fprintf(output,"0x%x,\n    ",dataIn[i]);
            c = 0;
        }
        else
            fprintf(output,"0x%x,",dataIn[i]);
    }

    fprintf(output,"0x%x};\n",dataIn[blen+1]);
    
    fclose(input);
    fclose(output);
}