#define EMPTY 0		
#define EMPTY2 8	/* 8 is also considered empty */
#define PAWN 1
#define KNIGHT 2
#define BISHOP 3
#define ROOK 4
#define QUEEN 5
#define KING 6

#define WPAWN 1
#define WP 1
#define WKNIGHT 2
#define WN 2
#define WBISHOP 3
#define WB 3
#define WROOK 4
#define WR 4
#define WQUEEN 5
#define WQ 5
#define WKING 6
#define WK 6
#define BPAWN 9
#define BP 9
#define BKNIGHT 10
#define BN 10
#define BBISHOP 11
#define BB 11
#define BROOK 12
#define BR 12
#define BQUEEN 13
#define BQ 13
#define BKING 14
#define BK 14
#define UNDEFINED 100
#define UNDEF 100

/* Output status bits, part of normal move generation
 * Bit 31: HasMove bit, if unsigned, not a psuedo-legal move; i.e., if (int) move < 0 --> valid move
 * Bit 30: ~HasVictim (including kings). if this bit is zero & 31 is also zero --> defender is in check
 *	   that is, if (move & CHECKMASK) == 0, then defender is in check, or parent move is illegal
 * Bit 27: promotion bit, if not set, the current move is not a promotion
 */
#define PARENTLEGAL (1<<30)

#define INCHECK	15

#define HASMOVEMASK	0x80000000
#define CHECKMASK	0xc0000000	/* negative logic */
#define INCHECKMASK	(1<<INCHECK)	/* positive logic */
#define MASKSTATUS	0x3ff03ff	/* the move only */
#define STATUSMASK	0xc8000000
#define MOVE19MASK	0x1ff03ff
#define MOVEMASK	0x81ff03ff	/* hasmove bit & 19 move bits */
#define FMOVEMASK	0x83ff03ff	/* and the 20th bit */

#define PROM 27
#define PROM_CAPT(move) (move & ((1<<PROM)|(mask(3)<<PC)))

#define EQ_MOVE(mv1,mv2) !((mv1^mv2)& MOVE19MASK)	/* equal moves */

#define NOMOVE 0			/* illegal move value used
					to terminate move lists */

/* special move masks
 * BFIRST & WFIRST are mask to get rid of the promotion moves, backward compatibility
 * Bit 26: FIRSTMOVE
 * Bit 9: Side to move, 0--black, 1--white (reverse of move format)
 * Bit 27: Promotion only
 * Bit 0-9: Aggressor mask (piece--6:9, sq--0:5)
 * Bit 16-25: Victim mask (piece--16:19, sq--10:15)
 * Notice that bit 25 is the inverse of bit 9
 */
#define PBFIRST	0xe000000	/* Black to move, locate first move */
#define PWFIRST	0xc000200	/* White to move, locate first move */
#define BFIRST	0x8000380	/* promotion, black king to 0 */
#define WFIRST	0xa000180	/* promotion, white king to 0 */
#define PROONLY 0x8000000	/* proonly bit */

#define EP_FR		1
#define EP_FL		2
#define WSCastle	4
#define BSCastle	8
#define WLCastle	16
#define BLCastle	32

/* shift counts
 */
#define PM	6	/* mover */
#define PC	22	/* victim */
#define FR	0	/* from square */
#define TO	16	/* to square */

#define MVMRK 28	/* move bit used in software only */

#ifdef HARDWARE
/* long command words of interest */
#define L_LCA		0	/* LCA prog mode */
#define L_PIECE		0x7	/* piece/~prog/ext_addr */
#define L_WP		0x0e	/* ~prog/ext_addr/wp */
#define L_BP		0x16	/* ~prog/ext_addr/bp */
#define L_WPP		0x26	/* ~prog/ext_addr/wpp */
#define L_BPP		0x46	/* ~prog/ext_addr/bpp */
#define L_ROOK		0x86	/* ~prog/ext_addr/rook */
#define L_WCS		0x82	/* ~prog/rook --> this disables move generator operations */
#define L_Nothing	0x2	/* ~prog */
#endif HARDWARE

/* VME addresses of interest */
#define MOVELOC1	eng1loc
#define UNMOVELOC1	(eng1loc + 1)
#define MASKLOC1	(eng1loc + 2)
#define TESTLOC1	(eng1loc + 3)
#define SRCHLOC1	(eng1loc + 4)
#define REGLOC1		(eng1loc + 5)

#define eng2loc (eng1loc+8)
#define reng2loc (reng1loc+8)

#define MOVELOC2	eng2loc
#define UNMOVELOC2	(eng2loc + 1)
#define MASKLOC2	(eng2loc + 2)
#define TESTLOC2	(eng2loc + 3)
#define SRCHLOC2	(eng2loc + 4)
#define REGLOC2		(eng2loc + 5)


/* to be hidden */
#ifdef HARDWARE
#define LWP_LOC		cmdloc
#define LBP_LOC		(cmdloc + 1)
#define VALUELOC	(cmdloc + 2)
#define WCSLOC		(cmdloc + 3)
#define LCMDLOC		(cmdloc + 4)		/* read resets the odd/even pal */
#define SHFTLOC		(cmdloc + 5)
#define LCALOC		(cmdloc + 6)
#define LCARSTLOC	(cmdloc + 7)

#define VME_RESET()	{int i; i = *LCMDLOC;}	/* reset the odd/even pal */
#define CASTLE_SET()	{*LCMDLOC = L_WCS; *LCMDLOC = L_Nothing;}
#endif HARDWARE

#define NotSpecial(move) (move & (0x7 << PM))
#define IsPromotion(move) (move & PROONLY)
#define NotEp(move) (move & 0x1fc)

#define ENP_CAST(mv) (!NotSpecial(mv))	/* En-passant or castling move */

extern	int MAIN,MLOAD,TLOAD,S_SETBOARD;
extern int LCA[],LCAcnt;
extern int WPTable[8192],BPTable[8192],WPPTable[8192],BPPTable[8192],RookTable[2048],PieceTable[1024];
extern int *eng1loc;
/*extern int *eng2loc;*/
extern int sq[64];
extern char *sqname[64];
extern int invsq[64];
extern char *piecename[16];
extern int char2piece[256]; /* case insensitive */
extern int chartopiece[256];/* case sensitive */

extern int sunboard[64];	/* the sun's board representation */
