	pure
* does white/black attack position?

	entry	battack,wattack

	extrn	dir,board

r0	equ	0
r1	equ	1
r2	equ	2
r3	equ	3
r4	equ	4
r5	equ	5
r6	equ	6
sp	equ	7
rf	equ	15

uleft	equ	x'0820'
uright	equ	x'0804'
dleft	equ	x'0120'
dright	equ	x'0104'
left	equ	x'0020'
right	equ	x'0004'
up	equ	x'0800'
down	equ	x'0100'
u2r1	equ	x'0c04'
u1r2	equ	x'0806'
d1r2	equ	x'0106'
d2r1	equ	x'0184'
d2l1	equ	x'01a0'
d1l2	equ	x'0130'
u1l2	equ	x'0830'
u2l1	equ	x'0c20'

battack	equ	*
	l	r6,0(sp)		position
	slls	r6,1			board index
	la	r5,board(r6)		board position
	lh	r1,dir(r6)		dir limit

* check for knight attack
	lis	r2,2			knight
	thi	r1,u2r1
	bne	bn1
	ch	r2,-15*2(r5)
	be	batt2
bn1	equ	*
	thi	r1,u1r2
	bne	bn2
	ch	r2,-6*2(r5)
	be	batt2
bn2	equ	*
	thi	r1,d1r2
	bne	bn3
	ch	r2,10*2(r5)
	be	batt2
bn3	equ	*
	thi	r1,d2r1
	bne	bn4
	ch	r2,17*2(r5)
	be	batt2
bn4	equ	*
	thi	r1,d2l1
	bne	bn5
	ch	r2,15*2(r5)
	be	batt2
bn5	equ	*
	thi	r1,d1l2
	bne	bn6
	ch	r2,6*2(r5)
	be	batt2
bn6	equ	*
	thi	r1,u1l2
	bne	bn7
	ch	r2,-10*2(r5)
	be	batt2
bn7	equ	*
	thi	r1,u2l1
	bne	bn8
	ch	r2,-17*2(r5)
	be	batt2
bn8	equ	*

*  check diagonals, rows & columns
	bal	r5,badiag
	dc	z(uleft),z(-18)
	bal	r5,badiag
	dc	z(uright),z(-14)
	bal	r5,badiag
	dc	z(dleft),z(14)
	bal	r5,badiag
	dc	z(dright),z(18)
	bal	r5,barank
	dc	z(up),z(-16)
	bal	r5,barank
	dc	z(left),z(-2)
	bal	r5,barank
	dc	z(right),z(2)
	bal	r5,barank
	dc	z(down),z(16)

* check for pawn attack
	lh	r1,dir(r6)
	la	r5,board(r6)
	thi	r1,uleft
	bne	bp1
	lh	r0,-9*2(r5)
	chi	r0,1
	be	batt2
bp1	equ	*
	thi	r1,uright
	bne	bp2
	lh	r0,-7*2(r5)
	chi	r0,1
	be	batt2
bp2	equ	*

* return 'no attack'
	lis	r0,1
	br	rf

* return 'attack'
batt2	equ	*
	lis	r0,0
	br	rf

badiag	equ	*
	lr	r1,r6		position
	lh	r2,0(r5)	mask
	lh	r3,2(r5)	offset
	lh	r0,dir(r1)
	nr	r0,r2
	bne	bad1
	ar	r1,r3
	lh	r4,board(r1)
	be	bad2
	chi	r4,3
	be	bad9
	chi	r4,5
	be	bad9
	chi	r4,6
	be	bad9
bad1	equ	*
	b	4(r5)
bad2	equ	*
	lh	r0,dir(r1)
	nr	r0,r2
	bne	bad3
	ar	r1,r3
	lh	r4,board(r1)
	be	bad2
	chi	r4,3
	be	bad9
	chi	r4,5
	be	bad9
bad3	equ	*
	b	4(r5)

barank	equ	*
	lr	r1,r6		position
	lh	r2,0(r5)	mask
	lh	r3,2(r5)	offset
	lh	r0,dir(r1)
	nr	r0,r2
	bne	bar1
	ar	r1,r3
	lh	r4,board(r1)
	be	bar2
	chi	r4,4
	be	bar9
	chi	r4,5
	be	bar9
	chi	r4,6
	be	bar9
bar1	equ	*
	b	4(r5)
bar2	equ	*
	lh	r0,dir(r1)
	nr	r0,r2
	bne	bar3
	ar	r1,r3
	lh	r4,board(r1)
	be	bar2
	chi	r4,4
	be	bar9
	chi	r4,5
	be	bar9
bar3	equ	*
	b	4(r5)

bad9	equ	*
bar9	equ	*
	lis	r0,0
	br	rf

wattack	equ	*
	l	r6,0(sp)		position
	slls	r6,1			board index
	la	r5,board(r6)		board position
	lh	r1,dir(r6)		dir limit

* check for knight attack
	lcs	r2,2			knight
	thi	r1,u2r1
	bne	wn1
	ch	r2,-15*2(r5)
	be	watt2
wn1	equ	*
	thi	r1,u1r2
	bne	wn2
	ch	r2,-6*2(r5)
	be	watt2
wn2	equ	*
	thi	r1,d1r2
	bne	wn3
	ch	r2,10*2(r5)
	be	watt2
wn3	equ	*
	thi	r1,d2r1
	bne	wn4
	ch	r2,17*2(r5)
	be	watt2
wn4	equ	*
	thi	r1,d2l1
	bne	wn5
	ch	r2,15*2(r5)
	be	watt2
wn5	equ	*
	thi	r1,d1l2
	bne	wn6
	ch	r2,6*2(r5)
	be	watt2
wn6	equ	*
	thi	r1,u1l2
	bne	wn7
	ch	r2,-10*2(r5)
	be	watt2
wn7	equ	*
	thi	r1,u2l1
	bne	wn8
	ch	r2,-17*2(r5)
	be	watt2
wn8	equ	*

*  check diagonals, rows & columns
	bal	r5,wadiag
	dc	z(uleft),z(-18)
	bal	r5,wadiag
	dc	z(uright),z(-14)
	bal	r5,wadiag
	dc	z(dleft),z(14)
	bal	r5,wadiag
	dc	z(dright),z(18)
	bal	r5,warank
	dc	z(up),z(-16)
	bal	r5,warank
	dc	z(left),z(-2)
	bal	r5,warank
	dc	z(right),z(2)
	bal	r5,warank
	dc	z(down),z(16)

* check for pawn attack
	lh	r1,dir(r6)
	la	r5,board(r6)
	thi	r1,dleft
	bne	wp1
	lh	r0,7*2(r5)
	chi	r0,-1
	be	watt2
wp1	equ	*
	thi	r1,dright
	bne	wp2
	lh	r0,9*2(r5)
	chi	r0,-1
	be	watt2
wp2	equ	*

* return 'no attack'
	lis	r0,1
	br	rf

* return 'attack'
watt2	equ	*
	lis	r0,0
	br	rf

wadiag	equ	*
	lr	r1,r6		position
	lh	r2,0(r5)	mask
	lh	r3,2(r5)	offset
	lh	r0,dir(r1)
	nr	r0,r2
	bne	wad1
	ar	r1,r3
	lh	r4,board(r1)
	be	wad2
	chi	r4,-3
	be	wad9
	chi	r4,-5
	be	wad9
	chi	r4,-6
	be	wad9
wad1	equ	*
	b	4(r5)
wad2	equ	*
	lh	r0,dir(r1)
	nr	r0,r2
	bne	wad3
	ar	r1,r3
	lh	r4,board(r1)
	be	wad2
	chi	r4,-3
	be	wad9
	chi	r4,-5
	be	wad9
wad3	equ	*
	b	4(r5)

warank	equ	*
	lr	r1,r6		position
	lh	r2,0(r5)	mask
	lh	r3,2(r5)	offset
	lh	r0,dir(r1)
	nr	r0,r2
	bne	war1
	ar	r1,r3
	lh	r4,board(r1)
	be	war2
	chi	r4,-4
	be	war9
	chi	r4,-5
	be	war9
	chi	r4,-6
	be	war9
war1	equ	*
	b	4(r5)
war2	equ	*
	lh	r0,dir(r1)
	nr	r0,r2
	bne	war3
	ar	r1,r3
	lh	r4,board(r1)
	be	war2
	chi	r4,-4
	be	war9
	chi	r4,-5
	be	war9
war3	equ	*
	b	4(r5)

wad9	equ	*
war9	equ	*
	lis	r0,0
	br	rf
	end
