	pure
* generate moves

	entry	bgen

	extrn	pval,board,dir
	extrn	flag,lmp,bkpos
	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'

bgen	equ	*
	lhi	r1,63
	la	r4,dir(r1,r1)
	la	r3,board(r1,r1)
	l	r2,lmp
loop	equ	*
	lh	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,dleft
	bne	pawn2
	lh	r0,7*2(r3)
	bnm	pawn3
	bal	r5,btry
	dc	z(0),z(14)
pawn3	equ	*
	lr	r0,r1
	ais	r0,7
	ch	r0,eppos
	bne	pawn2
	bal	r5,btry
	dc	z(0),z(-2)
pawn2	equ	*
	lh	r0,0(r4)
	thi	r0,dright
	bne	pawn4
	lh	r0,9*2(r3)
	bnm	pawn5
	bal	r5,btry
	dc	z(0),z(18)
pawn5	equ	*
	lr	r0,r1
	ais	r0,9
	ch	r0,eppos
	bne	pawn4
	bal	r5,btry
	dc	z(0),z(2)
pawn4	equ	*

* pawn moves

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

knight	equ	*
	bal	r5,btry
	dc	z(u2r1),z(-30)
	bal	r5,btry
	dc	z(u1r2),z(-12)
	bal	r5,btry
	dc	z(d1r2),z(20)
	bal	r5,btry
	dc	z(d2r1),z(34)
	bal	r5,btry
	dc	z(d2l1),z(30)
	bal	r5,btry
	dc	z(d1l2),z(12)
	bal	r5,btry
	dc	z(u1l2),z(-20)
	bal	r5,btry
	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,bslide
	dc	z(uleft),z(-18)
	bal	r5,bslide
	dc	z(uright),z(-14)
	bal	r5,bslide
	dc	z(dleft),z(14)
	bal	r5,bslide
	dc	z(dright),z(18)
	b	loop1

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

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

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

btry	equ	*
	lh	r0,0(r5)	mask
	nh	r0,0(r4)
	bne	btry1
	lh	r6,2(r5)	offset
	lh	r6,0(r3,r6)
	bp	btry1
	slls	r6,1
	ais	r6,12
	lh	r0,pval(r6)
	sh	r0,value
	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
btry1	equ	*
	b	4(r5)

bslide	equ	*
	lis	r6,0
bsloop	equ	*
	lh	r0,0(r5)	mask
	nh	r0,0(r6,r4)
	bne	bsl1
	ah	r6,2(r5)
	lh	r0,0(r6,r3)
	bp	bsl1
	bm	bsl2
	lis	r0,0
	sh	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	bsloop
bsl2	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,pval(r6)
	sh	r0,value
	sth	r0,0(r2)
	ais	r2,4
bsl1	equ	*
	b	4(r5)

	end
