/*--------------------------------------------------------------------------*/
/*	Program name: 5_2_6.C                                               */
/*	Compile mode: Small                                                 */
/*	Image   file: 5_2_6.EXE                                             */
/*	Note        : Hole Lock checking program.                           */
/*--------------------------------------------------------------------------*/


#include	<stdio.h>
#include	<conio.h>
#include	<dos.h>

unsigned	int	Start_Track,End_Track;
unsigned	char	buffer[512],buffer1[512];

/*--------------------------------- UTILITY --------------------------------*/

disk_reset()
{	_AX=0;
	geninterrupt(0x13);
}

/*--------------------------------- CHECK ----------------------------------*/

sec_io(int c,int h,int r,int mode,int *ax,char far *buf)
{	int	es;

	es=_ES;
	_ES=FP_SEG(buf);
	_BX=FP_OFF(buf);
	_AX=mode;
	_CH=c;
	_CL=r;
	_DH=h;
	_DL=0;
	geninterrupt(0x13);
	*ax=_AX;
	_ES=es;
	return(_FLAGS&1);
}

verify(int c,int h,int r)
{	int	i,ax,count;

	printf("Compare 1:  ");
	sec_io(c,h,r,0x0301,&ax,(char far *)buffer);
	printf("Write State:%04X  ",ax);
	sec_io(c,h,r,0x0201,&ax,(char far *)buffer1);
	printf("Read State:%04X\n\r",ax);
	for(i=count=0;i<512;i++)
		if (buffer[i]!=buffer1[i])
		{	count++;
			if (count>5)
			{	printf("Too many differences...\n\r");
				break;
			}
			printf("Offset:%04X  write:%02X  read:%02X\n\r",
				i,buffer[i],buffer1[i]);
		}
	printf("Compare 2:  ");
	sec_io(c,h,r,0x0301,&ax,(char far *)buffer1);
	printf("Write State:%04X  ",ax);
	sec_io(c,h,r,0x0201,&ax,(char far *)buffer);
	printf("Read State:%04X\n\r",ax);
	for(i=count=0;i<512;i++)
		if (buffer[i]!=buffer1[i])
		{	count++;
			if (count>5)
			{	printf("Too many differences...\n\r");
				break;
			}
			printf("Offset:%04X  write:%02X  read:%02X\n\r",
				i,buffer1[i],buffer[i]);
		}
}

check()
{	int	i,j,k,es,ax;

	disk_reset();
	for(i=Start_Track;i<=End_Track;i++)
	    for(j=1;j<=9;j++)
		for(k=0;k<=1;k++)
		{	printf("\rTrack:%02d  Head:%d  Sector:%d  ",i,k,j);
			if (sec_io(i,k,j,0x0201,&ax,(char far *)buffer))
			{	printf("AX=%04X\n",ax);
				if (ax==0x1001) verify(i,k,j);
			}
			if (kbhit())
			{	if (getch()==27)
				{	printf("Program break!\n\r");
					return(1);
				}
			}
		}
	printf("\rDone!                                       \n\r");
}

/*---------------------------------- MAIN ----------------------------------*/

parameter_check()
{	extern	int	_argc;
	extern	char	**_argv;

	if (_argc>3)
	{	printf("Too many parameters.\n\r");
		return(1);
	}
	if (_argc==1)
	{	printf("Usage: CHKHOLE Start_Track End_Track\n\r");
		return(1);
	}
	if (_argc<3)
	{	printf("Too few parameters.\n\r");
		return(1);
	}
	sscanf(_argv[1],"%d",&Start_Track);
	sscanf(_argv[2],"%d",&End_Track);
	printf("Press any key to continue...\n\r");
	getch();
	return(0);
}

holelock()
{	char	ch;

	if (parameter_check())	return(1);
	if (check())		return(1);
	printf("\n\n\rAll done!\n\r");
}

main()
{	holelock();
}
