	pure
	entry	bmove,bremove

	extrn	board,pval,amp,flag,eppos,value,bkpos
	extrn	game

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
bmove	equ	*
	l	r4,amp
	lb	r2,2(sp)		from
	lb	r3,3(sp)		to
	lh	r0,value
	sth	r0,0(r4)
	lh	0,flag
	sth	0,2(r4)
	lh	0,eppos
	sth	0,4(r4)
	sth	r2,6(r4)
	sth	r3,8(r4)
	slls	r2,1		from index
	slls	r3,1		to index
	lh	r5,board(r3)	captured piece
	sth	r5,10(r4)
	be	bmov1
	slls	r5,1
	ais	r5,12
	lh	r0,value
	sh	r0,pval(r5)
	sth	r0,value
bmov1	equ	*
	lh	r5,board(r2)	moved piece
	sth	r5,board(r3)
	lis	r0,0
	sth	r0,board(r2)
	lcs	r0,1
	sth	r0,eppos
	slls	r5,2
	l	r5,brtab-adc(r5)
	br	r5

	align	adc
brtab	equ	*
	dc	a(pmov)
	dc	a(nmov)
	dc	a(bmov)
	dc	a(rmov)
	dc	a(qmov)
	dc	a(kmov)

pmov	equ	*
	sr	r2,r3
	bnm	pm1
	lis	r0,0
	sr	r0,r2
	lr	r2,r0
pm1	equ	*
	chi	r2,2*1		ep capture
	bne	pm2
	lis	r0,0
	sth	r0,board(r3)
	lhi	r5,2*8
	lis	r0,1
	sth	r0,board(r5,r3)
	lis	r0,4
	sth	r0,12(r4)
	ais	r4,14
	st	r4,amp
	br	rf
pm2	equ	*
	chi	r2,2*16		double move
	bne	pm3
	lb	r2,2(sp)
	ais	r2,8
	st	r2,eppos
	b	move
pm3	equ	*
	chi	r3,40*2
	bl	move
	lhi	r0,25
	ahm	r0,value
	chi	r3,48*2
	bl	move
	lhi	r0,50
	ahm	r0,value
	chi	r3,56*2
	bl	move
	lhi	r0,625
	ahm	r0,value
	lhi	r0,5
	sth	r0,board(r3)
	sth	r0,12(r4)
	ais	r4,14
	st	r4,amp
	br	rf

rmov	equ	*
	chi	r2,2*7
	bne	rm1
	lh	r0,flag
	nhi	r0,x'ffff'-x'08'
	sth	r0,flag
	b	move
rm1	equ	*
	lr	r2,r2
	bnz	move
	lh	r0,flag
	nhi	r0,x'ffff'-x'10'
	sth	r0,flag
	b	move

kmov	equ	*
	srls	r3,1
	sth	r3,bkpos
	lh	r0,flag
	nhi	r0,x'ffff'-x'18'
	sth	r0,flag
	chi	r2,2*4
	bne	km2
	chi	r3,6		kingside castle
	bne	km1
	lis	r0,1
	ahm	r0,value
	la	r5,board
	lis	r0,4
	sth	r0,10(r5)
	lis	r0,0
	sth	r0,2*7(r5)
	lis	r0,2
	sth	r0,12(r4)
	ais	r4,14
	st	r4,amp
	br	rf
km1	equ	*
	chi	r3,2		queenside castle
	bne	km2
	lis	r0,1
	ahm	r0,value
	la	r5,board
	lis	r0,4
	sth	r0,2*3(r5)
	lis	r0,0
	sth	r0,2*0(r5)
	lis	r0,3
	sth	r0,12(r4)
	ais	r4,14
	st	r4,amp
	br	rf
km2	equ	*		king move
	lh	r0,game
	bnz	km3
	lcs	r0,2
	ahm	r0,value
km3	equ	*
	lis	r0,0
	sth	r0,12(r4)
	ais	r4,14
	st	r4,amp
	br	rf

qmov	equ	*
	lh	r0,game
	bnz	move
	lcs	r1,1
	ahm	r1,value
	b	move

nmov	equ	*
bmov	equ	*
move	equ	*
	lis	r0,1
	sth	r0,12(r4)
	ais	r4,14
	st	r4,amp
	br	rf

bremove	equ	*
	l	r4,amp
	sis	r4,14
	lh	r6,12(r4)
	lh	r1,10(r4)
	lh	r3,8(r4)
	lh	r2,6(r4)
	lh	r0,4(r4)
	sth	r0,eppos
	lh	r0,2(r4)
	sth	r0,flag
	lh	r0,0(r4)
	sth	r0,value
	st	r4,amp
	slls	r2,1
	slls	r3,1
	lh	r0,board(r3)
	sth	r0,board(r2)
	sth	r1,board(r3)
	slls	r6,2
	l	r6,brtab1(r6)
	br	r6

	align	adc
brtab1	equ	*
	dc	a(movek)
	dc	a(movex)
	dc	a(moveo)
	dc	a(moveoo)
	dc	a(movep)
	dc	a(moveq)

movek	equ	*
	srls	r2,1
	sth	r2,bkpos

movex	equ	*
	br	rf

moveo	equ	*
	la	r5,board
	lis	r0,4
	sth	r0,2*7(r5)
	sth	r0,bkpos
	lis	r0,0
	sth	r0,2*5(r5)
	br	rf

moveoo	equ	*
	la	r5,board
	lis	r0,4
	sth	r0,2*0(r5)
	sth	r0,bkpos
	lis	r0,0
	sth	r0,2*3(r5)
	br	rf

movep	equ	*
	la	r5,board
	lis	r0,1
	sth	r0,board(r2)
	lis	r0,0
	sth	r0,2*8(r5,r3)
	br	rf

moveq	equ	*
	lis	r0,1
	sth	r0,board(r2)
	br	rf

	end
