/*--------------------------------------------------------------------------*/
/*	Program name: 4_3_5.C                                               */
/*	Compile mode: Tiny                                                  */
/*	Image   file: None                                                  */
/*	Note        : List NewInt13() procedure and its subroutines only.   */
/*--------------------------------------------------------------------------*/


#include	<stdio.h>
#include	<stdarg.h>
#include	<string.h>
#include	<dos.h>
#include	<fcntl.h>
#include	<io.h>

struct	Reg_buf	{	unsigned	ax_1,bx_1,cx_1,dx_1;
			unsigned	ax_2,bx_2,cx_2,dx_2;
			unsigned	flag_2,bytes,names;
		}	reg_buf[100];
char	name_buf[0x1000];
int	reg_count;
int	load_count;
int	dummy_read;

/*----------------- NewInt13 -----------------------------------------------*/

void	interrupt	NewInt13( unsigned BP,unsigned DI,unsigned SI,
				  unsigned DS,unsigned ES,unsigned DX,
				  unsigned CX,unsigned BX,unsigned AX,
				  unsigned IP,unsigned CS,unsigned FLAGS )
{	static	union	REGS	regs;
	static	struct	SREGS	sregs;
	static	int	fh;
	static	char	fake_file[]="A_SECTOR.DAT";


	if ((DX%256!=0)||(AX/256==0)||(*InDos)||(Busy))	goto	i13_done;

	Busy=1;

	regs.x.ax=AX;
	regs.x.bx=BX;
	regs.x.cx=CX;
	regs.x.dx=DX;
	regs.x.si=SI;
	regs.x.di=DI;
	regs.x.flags=FLAGS;
	sregs.es=ES;
	sregs.ds=DS;

	OLD_SS=_SS;
	OLD_SP=_SP;

	disable();
	_SS=TSR_SS;
	_SP=TSR_SP;
	enable();



	if (dummy_read)
	{	if ((reg_buf[load_count].ax_1&0xff00)==0x0200)
		{
			/*------Get file name and open it-------*/

			*((int *)fake_file)=reg_buf[load_count].names;
			fh=_open(fake_file,O_RDONLY);

			/*------Read sector data from file------*/

			_SI=_DS;
			_CX=reg_buf[load_count].bytes;
			_BX=fh;
			_DX=regs.x.bx;
			_DS=sregs.es;
			_AH=0x3F;
			geninterrupt(0x21);
			_DS=_SI;

			/*------Close file----------------------*/

			_close(fh);
		}
		/*------Set register to proper value----*/

		regs.x.ax=reg_buf[load_count].ax_2;
		regs.x.bx=reg_buf[load_count].bx_2;
		regs.x.cx=reg_buf[load_count].cx_2;
		regs.x.dx=reg_buf[load_count].dx_2;
		regs.x.flags=reg_buf[load_count].flag_2;

		/*------Adjust load count---------------*/

		load_count++;
		if (load_count>=reg_count)	load_count=0;

	}
	else	/*------Get sector data from disk-------*/
	{
		/*------Record register values----------*/

		reg_buf[reg_count].ax_1=regs.x.ax;
		reg_buf[reg_count].bx_1=regs.x.bx;
		reg_buf[reg_count].cx_1=regs.x.cx;
		reg_buf[reg_count].dx_1=regs.x.dx;
		int86x(0x13,&regs,&regs,&sregs);
		reg_buf[reg_count].ax_2=regs.x.ax;
		reg_buf[reg_count].bx_2=regs.x.bx;
		reg_buf[reg_count].cx_2=regs.x.cx;
		reg_buf[reg_count].dx_2=regs.x.dx;
		reg_buf[reg_count].flag_2=regs.x.flags;
		if (reg_count<99)	reg_count++;
	}



	disable();
	_SS=OLD_SS;
	_SP=OLD_SP;
	enable();

	AX=regs.x.ax;
	BX=regs.x.bx;
	CX=regs.x.cx;
	DX=regs.x.dx;
	SI=regs.x.si;
	DI=regs.x.di;
	FLAGS=regs.x.flags;
	ES=sregs.es;
	DS=sregs.ds;

	Busy=0;

	return;




i13_done:
	_SP=_SP-6;
	_BP=_BP-6;
	BP=DS;
	DI=ES;
	SI=DX;
	DS=CX;
	ES=BX;
	DX=AX;
	CX=IP;
	BX=CS;
	AX=FLAGS;
	_BP=_BP+6;
	CX=(unsigned int)OldInt13;
	BX=(unsigned long)OldInt13 / 65536;
	AX=FLAGS;
	_BP=_BP-6;
}

void	load_reg(void)
{	int	fh;

	if ((fh=_open("REGISTER.DAT",O_RDONLY))==-1)	return;
	_read(fh,&reg_count,2);
	reg_count/=sizeof(reg_buf[0]);
	_read(fh,reg_buf,sizeof(reg_buf[0])*100);
	_close(fh);
	dummy_read=1;
}

