/*
 *	Perlin Production Co. Ltd. September 1993 (c).
 *			GRAB1.C
 */

#include "stdio.h"
#include "wios.h"
#include "conio.h"
#include "grab.h"

unsigned vbase;

rd_plane(_plane)
unsigned _plane;
{
	unsigned vend;

	_plane &=3;
	seek(src,_plane * PANEL_BIAS,START);

	vbase = VIDEO_TOP + (_plane & 0x1) * PANEL_BIAS;
	vend  = vbase + PANEL_BIAS;

	for (scrpos=vbase; scrpos < vend; scrpos+=VSIZE)
	{
		if ( (read(src,scrcol,VSIZE)) != VSIZE )
		 trouble ("Read error in",srcname);
		else
		{
#asm
		di
		push b
		xra a
		out 10h
		lhld scrpos
		lxi d,scrcol
		mvi c,0FFH
WrtVRAM:	LDAX D
		MOV M,A
		INX H
		INX D
		DCR C
		JNZ WrtVRAM
		mvi a,23h
		out 10h
		pop b
		ei
#endasm
		}
	}
}



open_src(_plane)
unsigned _plane;
{
	if ((src=open(srcname,FCB_R_W)) == ERROR)
	 trouble("Can't open",srcname);

	seek(src,0,LAST);
	if ( (unsigned)tell(src) < 0x8000 )
	  trouble("Source file ",strcat (srcname," must be 32K in length!"));

	hd_csr();
	rd_plane(_plane);
	rd_plane(_plane+1);
}


shiftscr(_dir,_plane)
char	 _dir;
unsigned _plane;
{
 unsigned plane;
 int i;

 for (plane=0; plane < NPANELS; ++plane)
 {
	rd_plane(plane);

	vbase = VIDEO_TOP + (plane & 0x1) * PANEL_BIAS;

	if (_dir==LEFT) ShlVRam();
	else ShrVRam();

	seek(src,plane * PANEL_BIAS,START);

	for (i=0; i < 0x20; ++i)
	{
#asm
		di
		push b
		xra a
		out 10h
		lhld vbase
		lxi  d,scrcol
		mov  c,a
rdcol1:		mov a,m
		stax d
		inx h
		inx d
		dcr c
		jnz rdcol1
		mvi a,23h
		out 10h
		pop b
		ei
#endasm
		if (write(src,scrcol,VSIZE) != VSIZE)
			trouble("Error writing",srcname);
		vbase += VSIZE;
	}
 }
 if (close(src) == ERROR) trouble("Error closing",srcname);
 open_src(_plane);
}



ShlVRam()
{
#asm
	di
	push b
	xra a
	out 10h

	lhld vbase

ShlLin:	mov a,h
	adi 1Fh
	mov b,a

	mov a,m
	ral
	mov h,b
	Rept 32
		mov a,m
		ral
		mov m,a
		dcr h
	EndM
	inr	h
	inr	l
	jnz	ShlLin

	mvi a,23h
	out 10h
	pop b
	ei
#endasm
}


ShrVRam()
{
#asm
	di
	push b
	xra a
	out 10h

	lhld vbase

	mov b,h
ShrLin:	mov a,h
	adi 1Fh
	mov h,a

	mov a,m
	rar
	mov h,b
	Rept 32
		mov a,m
		rar
		mov m,a
		inr h
	EndM
	mov h,b
	inr l
	jnz ShrLin

	mvi a,23h
	out 10h
	pop b
	ei
#endasm
}
