/* * ============================================================================ * De-Decle J. Zbiciak * * Quick-hack program to unpack a packed-decle program into 16-bit qtys. * * Background: ROM images are stored in the .RES file in a packed format. * The lower 8 bits of each decle are stored in the first 4/5ths of the * file, and the upper 2 bits of each decle are stored packed in the last * 1/5th of the file. * ============================================================================ */ #include "config.h" #include #include #include /* * ============================================================================ * UNPACK_DECLES -- Takes a set of packed decles, and unpacks them * * This function accepts an array of bytes that correspond to a set of * packed decles. It returns an array of shorts which correspond to the * unpacked decles. * * The output array must have at least 4/5ths as many elements as the * input array. The output array, though, is comprised of shorts, so its * size is 8/5ths the size of the original array. * ============================================================================ */ void unpack_decles ( size_t len, unsigned char i_data[], unsigned short o_data[] ) { unsigned i, decle_count; unsigned lo_half, hi_half; decle_count = 4 * len / 5; if ( (decle_count * 5 / 4) != len) { printf("Warning: len %d doesn't seem right.\n", len); } for (i = 0; i < decle_count; i++) { lo_half = i_data[i]; hi_half = (i_data[decle_count + (i >> 2)] >> ((i & 3) << 1)) & 3; o_data[i] = (hi_half << 8) | lo_half; } } /* * ============================================================================ * REPACK_DECLES -- Takes a set of unpacked decles, and repacks them * * This function accepts an array of shorts that correspond to a set of * unpacked decles. It returns an array of bytes which correspond to the * packed decles. * * The output array must have 5/4ths as many elements as the input array. * Since the output array is comprised of bytes, and the input array is * comprised of shorts, the output array should be at least 5/8th the size * of the input array. * ============================================================================ */ void repack_decles ( size_t len, unsigned short i_data[], unsigned char o_data[] ) { int i, j, decle_count; unsigned lo_half, hi_half; decle_count = len; for (i = 0, j = decle_count; i < decle_count; i++, j = decle_count + (i>>2)) { lo_half = i_data[i]; hi_half = ((i_data[i] >> 8) & 3) << ((i & 3) << 1); o_data[i] = lo_half; o_data[j] = (o_data[j] & ~(3 << ((i & 3) << 1))) | hi_half; } } /* * ============================================================================ * MAIN -- In the beginning, there was a main... * * ... and the C was without const or void, and darkness was on the face of * the programmer. And Kernighan said, "Let there be Code," and there was * Code. * ============================================================================ */ int main ( int argc, char *argv[] ) { FILE *i_file, *o_file; unsigned char *input; unsigned short *output; size_t len; unsigned foo,i; if (argc < 3) { fprintf(stderr,"Usage: %s infile outfile\n",argv[0]); exit(1); } i_file = fopen(argv[1],"r"); if (!i_file) { perror(argv[1]); exit(1); } fseek(i_file, 0, SEEK_END); len = ftell(i_file); fseek(i_file, 0, SEEK_SET); input = malloc(len); output = malloc((len * 8 + 4)/ 5); if (!input || !output) { perror("malloc()"); exit(1); } fread(input,1,len,i_file); fclose(i_file); foo = input[len-1] | (input[len-2] << 8); if ( (foo << 7) == ((len - 2) * 4 / 5)) len -= 2; unpack_decles(len, input, output); o_file = fopen(argv[2],"w"); if (!o_file) { perror(argv[2]); exit(1); } for (i=0 ; i < (4 * len) / 5; i++) { output[i] = ((output[i] & 0xFF) << 8) | ((output[i] >> 8) & 0xFF); } fwrite(output,2,(4*len)/5,o_file); fclose(o_file); return 0; } /* * ============================================================================ * Copyright (c) 1998, Joseph Zbiciak. * ============================================================================ */