#include <math.h>
#include <stdio.h>
#include <ctype.h>

#define unchar	unsigned char
#define unshort unsigned short

#define ON	1
#define OFF	0

FILE *fpr0,*fpw0;

main( argc,argv )
	int	argc;
	char	**argv;
{
	/****** ram set ******/
	char	f_name[100];
	char	x_offset,y_offset;

	char	obj_data[64][128][6];

	int	i,j,k;

	/****** input data check ******/
	if( argc!=4 ){
		printf( "こら！ 入力データがちゃうやないけ、いてまうど！！\n" );
		printf( "こうや、（ファイル名）（Ｘ加算値）（Ｙ加算値）\n" );
		printf( "覚えとけ！！\n" );
		exit(1);
	}

	/****** OBJ file open ******/
	strcpy( f_name,argv[1] );
	strcat( f_name,".OBX" );
	if( (fpr0=fopen(f_name,"r")) == 0 ){
		printf("ＯＢＪファイルが読まれへんやんけ！！(%s)\n",f_name);
		exit(1);
	}
	x_offset = (char)strtol( argv[2], (char **)NULL, 16 );
	y_offset = (char)strtol( argv[3], (char **)NULL, 16 );

	/****** obj file convert ******/
	if ( fread(&obj_data[0][0][0],sizeof(unchar),64*128*6,fpr0) != 64*128*6 ){
		printf( "このＯＢＪファイル、足らへん！！\n" );
		exit(1);
	}

	/****** OBJ file close ******/
	fclose( fpr0 );

	/****** OBJ data file open ******/
	if( (fpw0=fopen("eobj.dat","w")) == 0 ){
		printf("変換したデータを出力するファイルがオープンでけへん！！\n");
		exit(1);
	}

	for( i=0;i<64;i++ ){
		for( j=0,k=0;j<128;j++ ){
			if( obj_data[i][j][0] & 0x80 ){
				fprintf( fpw0,"\t\tBYTE\t%03XH",(unchar)(obj_data[i][j][3]+x_offset) );
				fprintf( fpw0,",%03XH",		(unchar)(obj_data[i][j][2]+y_offset) );
				if( !k ) fprintf( fpw0,"\t;[%02X]",i );
				fprintf( fpw0,"\n" );
				fprintf( fpw0,"\t\tHEX\t%02X",(unchar)obj_data[i][j][5] );
				fprintf( fpw0,",%02X",	      (unchar)(obj_data[i][j][4] & 0xcf) );
				fprintf( fpw0,",%02X\n",      (obj_data[i][j][0] & 0x01) << 1 );
				k=1;
			}
		}
		if( k ) fprintf( fpw0,";\n" );
	}

	/****** OBJ data file close ******/
	fclose( fpw0 );

	printf("  < eobj.dat > ちゅうファイルに入れたったで！！\n");

}

