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

static char objfile[]="highspeedcode.65o";

int main(void)
{

	FILE *in;
	FILE *out;
	unsigned char hdr[6];
	unsigned char* buf;
	int len, objlen, rellen, origstart, origend;
	int i;

	if (!(in=fopen(objfile,"rb"))) {
		printf("error opening %s\n",objfile);
		return 1;
	}

	len=fread(hdr,1,6,in);
	if (len!=6) {
		printf("error reading header of %s\n",objfile);
		return 1;
	}
	origstart = hdr[2]+256*hdr[3];
	origend = hdr[4]+256*hdr[5];
	if (hdr[0] != 0xff || hdr[1] != 0xff || origstart>origend) {
		printf("illegal file header in %s!\n", objfile);
		return 1;
	}

	buf = (unsigned char*) malloc(origend-origstart+1);
	len=fread(buf,1,origend-origstart+1,in);

	fclose(in);
	if (len < 2 || len != origend-origstart+1) {
		printf("illegal file %s\n", objfile);
		return 1;
	}

	fprintf(stderr, "read %d bytes\n", len);
	rellen = buf[len-2] + 256*buf[len-1];
	objlen = len - 2 - rellen;
	rellen = rellen / 2;

	fprintf(stderr, "original address was 0x%4x\n", origstart);
	fprintf(stderr, "obj len is %d\n", objlen);
	fprintf(stderr, "relocator table len is %d\n", rellen);

	if (!(out = fopen("highspeedcode.h","wb"))) {
		printf("cannot create highspeedcode.h\n");
		return 1;
	}
	fprintf(out,"enum { eSIOCodeLength = %d,\n",objlen);
        fprintf(out,"       eRelocatorLength = %d,\n",rellen);
	fprintf(out,"       eOriginalAddress = 0x%04x\n",origstart);
	fprintf(out,"};\n");
	fclose(out);

	printf("successfully created highspeedcode.h\n");

	if (!(out = fopen("highspeedcode.c","wb"))) {
		printf("cannot create highspeedcode.c\n");
		return 1;
	}
	fprintf(out,"unsigned char HighSpeedSIOCode::fSIOCode[] = {");
	for (i=0;i<objlen;i++) {
		if (i % 12 == 0) {
			fprintf(out,"\n\t");
		}
		fprintf(out,"0x%02x, ", buf[i]);
	}
	fprintf(out,"\n};\n\n");
	fprintf(out,"unsigned int HighSpeedSIOCode::fRelocatorTable[] = {");
	for (i=0;i<rellen;i++) {
		if (i % 8 == 0) {
			fprintf(out,"\n\t");
		}
		fprintf(out,"0x%04x, ", (buf[i*2+objlen]+256*buf[i*2+objlen+1]) - origstart);
	}
	fprintf(out,"\n};\n");
	fclose(out);

	printf("successfully created highspeedcode.c\n");

	return 0;
}
