	pure
* generate moves

	entry	wgen

	extrn	pval,board,dir
	extrn	flag,lmp,wkpos
	extrn	eppos
	extrn	value

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'
rank2	equ	x'0080'
rank7	equ	x'0400'

wgen	equ	*
	lhi	r1,63
	la	r4,dir(r1,r1)
	la	r3,board(r1,r1)
	l	r2,lmp
loop	equ	*
	lis	r5,0
	sh	r5,0(r3)
	bnp	loop1
	slls	r5,2
	l	r5,btab-adc(r5)
	br	r5

	align	adc
btab	equ	*
	dc	a(pawn)
	dc	a(knight)
	dc	a(bishop)
	dc	a(rook)
	dc	a(queen)
	dc	a(king)


* pawn captures
pawn	equ	*
	lh	r0,0(r4)
	thi	r0,uleft
	bne	pawn2
	lh	r0,-9*2(r3)
	bnp	pawn3
	bal	r5,wtry
	dc	z(0),z(-18)
pawn3	equ	*
	lr	r0,r1
	sis	r0,9
	ch	r0,eppos
	bne	pawn2
	bal	r5,wtry
	dc	z(0),z(-2)
pawn2	equ	*
	lh	r0,0(r4)
	thi	r0,uright
	bne	pawn4
	lh	r0,-7*2(r3)
	bnp	pawn5
	bal	r5,wtry
	dc	z(0),z(-14)
pawn5	equ	*
	lr	r0,r1
	sis	r0,7
	ch	r0,eppos
	bne	pawn4
	bal	r5,wtry
	dc	z(0),z(2)
pawn4	equ	*

* pawn moves

	lh	r0,-8*2(r3)
	bnz	loop1
	bal	r5,wtry
	dc	z(0),z(-16)
	lh	r0,0(r4)
	thi	r0,rank2
	bz	loop1
	lh	r0,-16*2(r3)
	bnz	loop1
	bal	r5,wtry
	dc	z(0),z(-32)
	b	loop1

knight	equ	*
	bal	r5,wtry
	dc	z(u2r1),z(-30)
	bal	r5,wtry
	dc	z(u1r2),z(-12)
	bal	r5,wtry
	dc	z(d1r2),z(20)
	bal	r5,wtry
	dc	z(d2r1),z(34)
	bal	r5,wtry
	dc	z(d2l1),z(30)
	bal	r5,wtry
	dc	z(d1l2),z(12)
	bal	r5,wtry
	dc	z(u1l2),z(-20)
	bal	r5,wtry
	dc	z(u2l1),z(-34)
	b	loop1


loop1	equ	*
	sis	r4,2
	sis	r3,2
	sis	r1,1
	bnm	loop
	st	r2,lmp
	br	rf

bishop	equ	*
	bal	r5,wslide
	dc	z(uleft),z(-18)
	bal	r5,wslide
	dc	z(uright),z(-14)
	bal	r5,wslide
	dc	z(dleft),z(14)
	bal	r5,wslide
	dc	z(dright),z(18)
	b	loop1

rook	equ	*
	bal	r5,wslide
	dc	z(up),z(-16)
	bal	r5,wslide
	dc	z(down),z(16)
	bal	r5,wslide
	dc	z(left),z(-2)
	bal	r5,wslide
	dc	z(right),z(2)
	b	loop1

queen	equ	*
	bal	r5,wslide
	dc	z(uleft),z(-18)
	bal	r5,wslide
	dc	z(uright),z(-14)
	bal	r5,wslide
	dc	z(dleft),z(14)
	bal	r5,wslide
	dc	z(dright),z(18)
	bal	r5,wslide
	dc	z(up),z(-16)
	bal	r5,wslide
	dc	z(left),z(-2)
	bal	r5,wslide
	dc	z(right),z(2)
	bal	r5,wslide
	dc	z(down),z(16)
	b	loop1

king	equ	*
	bal	r5,wtry
	dc	z(uleft),z(-18)
	bal	r5,wtry
	dc	z(uright),z(-14)
	bal	r5,wtry
	dc	z(dleft),z(14)
	bal	r5,wtry
	dc	z(dright),z(18)
	bal	r5,wtry
	dc	z(up),z(-16)
	bal	r5,wtry
	dc	z(left),z(-2)
	bal	r5,wtry
	dc	z(right),z(2)
	bal	r5,wtry
	dc	z(down),z(16)
	b	loop1

wtry	equ	*
	lh	r0,0(r5)	mask
	nh	r0,0(r4)
	bne	wtry1
	lh	r6,2(r5)	offset
	lh	r6,0(r3,r6)
	bm	wtry1
	slls	r6,1
	ais	r6,12
	lh	r0,value
	sh	r0,pval(r6)
	sth	r0,0(r2)
	lh	r0,2(r5)
	srls	r0,1
	ar	r0,r1
	stb	r0,3(r2)
	stb	r1,2(r2)
	ais	r2,4
wtry1	equ	*
	b	4(r5)

wslide	equ	*
	lis	r6,0
wsloop	equ	*
	lh	r0,0(r5)	mask
	nh	r0,0(r6,r4)
	bne	wsl1
	ah	r6,2(r5)
	lh	r0,0(r6,r3)
	bm	wsl1
	bp	wsl2
	lh	r0,value
	sth	r0,0(r2)
	la	r0,0(r6,r3)
	si	r0,board
	srls	r0,1
	stb	r1,2(r2)
	stb	r0,3(r2)
	ais	r2,4
	b	wsloop
wsl2	equ	*
	la	r0,0(r3,r6)
	si	r0,board
	srls	r0,1
	stb	r1,2(r2)
	stb	r0,3(r2)
	lh	r6,0(r3,r6)
	slls	r6,1
	ais	r6,12
	lh	r0,value
	sh	r0,pval(r6)
	sth	r0,0(r2)
	ais	r2,4
wsl1	equ	*
	b	4(r5)

	end
