#use80286
#jumptomain SHORT
#resize FALSE
#parsecommandline FALSE
#pragma option X
#pragma option LST

// *********************************************
// data
word Px = 145; // x coord of your racket
word CPUx = 145; // x coord of enemy racket
word x = 50; // x ccord of the ball
word y = 100; // y coord of the ball
word dtx = 1; // delta x
word dty = 1; // delta y
//word dir = 0; // direction for your racket
word cell1 = 0;
word cell2 = 0;

byte sball[] = {0,0,4,4,0,0,0,4,12,12,4,0,4,12,14,14,12,4,4,12,14,14,12,4,0,4,12,12,4,0,0,0,4,4,0,0};
byte srckt1[] = {2,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,2,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0};
byte srckt2[] = {0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,0,7,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,7};

// *********************************************
// post data
byte bball[36];
byte brckt1[36];
byte brckt2[36];

// *********************************************
// prototypes
void PutBar();
void fastcall CalcDI(BX, DX);
void fastcall ShowRoquet(BX, DX, CX, SI);
void fastcall HideRoquet(BX, DX, SI);
void fastcall GetSprite(AX, DX, BX);
void fastcall PutSprite(DL, DH, SI);
byte fastcall GetBlue(AL);
void fastcall SetBlue(AL, BL);

// *********************************************
// main
void main()
{

    AX = 0x13; $int 0x10; ES = 0xa000;

    CX = 50; BX = 0;
    loop(CX) {
	BX++; SetBlue(50 + 132 - CL, BL);
    }

    CX = 50;
    loop(CX) {
	BX--; SetBlue(50 + 182 - CL, BL);
    }

    CX = 99; AX = 132;
    loop(CX) {
	$push cx; PutBar(); $pop cx;
	AX++; cell1++; cell2++;
    }

    AX = 0; $int 0x16;

    // main loop
    loop() {
	GetBlue(132);
	//AH = 0;
	$push ax

	// rotate palette left
	CX = 99;
	loop(CX) {
	    BL = GetBlue(99 - CL + 133);
	    SetBlue(99 - CL + 132, BL);
	}

	$pop bx
	SetBlue(231, BL);

	CalcDI(x, y);

	$push di
	GetSprite(314, 6, #bball);
	$pop di

	PutSprite(6, DL, #sball);
	ShowRoquet(Px, 198, #brckt1, #srckt1);
	ShowRoquet(CPUx, 0, #brckt2, #srckt2);

	// vblank
	$ mov	  dx,03DAh
WR1:
	$ in	  al,dx
	$ and	  al,08h
	$ jnz	  WR1

WR2:
	$ in	  al,dx
	$ and	  al,08h
	$ jz	  WR2


	CalcDI(x, y);

	SI = #bball; DX = 6;
	do {
	    CX = 3; $rep; $movsw; DI += 314; DX--;
	} while(NOTZEROFLAG);

	HideRoquet(Px, 198, #brckt1);
	HideRoquet(CPUx, 0, #brckt2);

	AH = 1; $int 0x16;
	IF(NOTZEROFLAG) {
	    AH = 0; $int 0x16;

	    IF(AH == 1) goto Quit; // esc
	    IF(AH == 75) {	   // left
		AX = Px - 4;
		IF(int AX < 0) AX = 0;
		Px = AX;
	    } ELSE		   // right
	    IF(AH == 77) {
		AX = Px + 4;
		IF(int AX > 302) AX = 302;
		Px = AX;
	    }
	}

	AX = CPUx + 6;

	IF(AX > x) {
	    AX -= 6;
	    IF(AX > 1) CPUx--;
	} ELSE {
	    AX -= 6;
	    IF(AX < 300) CPUx++;
	}

	AX = x + dtx;
	IF(char AX >= 313) -dtx;
	IF(char AX <= 0) -dtx;
	x = AX;

	BX = AX;

	AX = y + dty;
	IF(char AX >= 192) {

	    BX += 3;
	    IF(BX < Px) GOTO Quit;

	    BX -= 24;
	    IF(BX > Px) GOTO Quit;

	    -dty;
	}

	IF(char AX <= 2) {

	    BX += 3;
	    IF(BX < CPUx) GOTO Quit;

	    BX -= 24;
	    IF(BX > CPUx) GOTO Quit;

	    -dty;
	}

	y = AX;

     }

Quit:

      AX = 3; $int 0x10;
}

// *****************************************
// other procedures
void PutBar()
{

    CalcDI(cell1, cell2);

    BX = cell2 << 1;
    DX = 200 - BX;
    BX = cell1 << 1;

    do {
	CX = 320 - BX;

	$push di
	$rep $stosb
	$pop di
	DI += 320;
	DX--;

    } while(NOTZEROFLAG);
}

void fastcall ShowRoquet(BX, DX, CX, SI)
{
    CalcDI(BX, DX);

    BX = CX;

    $push di
    GetSprite(302, 2, BX);
    $pop di

    PutSprite(18, 2, SI);
}


void fastcall HideRoquet(BX, DX, SI)
{

    CalcDI(BX, DX);

    DX = 2;
    do {
	CX = 9;
	$rep $movsw
	DI += 302;
	DX--;
    } while(NOTZEROFLAG);
}



void fastcall CalcDI(BX, DX)
{
    DI = BX;
    BX = DX << 6;
    DX <<= 8;
    DX += BX;
    DI += DX;
}


void fastcall GetSprite(AX, DX, BX)
{
    DH = DL;

    do {
	CX = 320 - AX;

	loop(CX) {
	    DL = ESBYTE[DI];
	    DSBYTE[BX] = DL;
	    DI++;
	    BX++;
	}

	DI += AX;
	DH--;
    } while(NOTZEROFLAG);
}


void fastcall PutSprite(DL, DH, SI)
{
    AH = 0;
    BX = 320;
    AL = DL;
    BX -= AX;


    do {
	CL = DL;

	loop(CX) {
	    $lodsb
	    IF(AL != 0) $stosb; ELSE DI++;
	}

	DI += BX;
	DH--;
    } while(NOTZEROFLAG);

}


byte fastcall GetBlue(AL)
{
      $ mov	dx,03C7h
      $ out	dx,al
      $ mov	dx,03C9h
      $ in	al,dx
      $ in	al,dx
      $ in	al,dx
}

void fastcall SetBlue(AL, BL)
{
      $ mov	dx,03C8h
      $ out	dx,al
      $ inc	dx
      $ xor	al,al
      $ out	dx,al
      $ out	dx,al
      $ mov	al,bl
      $ out	dx,al
}
