;***********************************
;*                                 *
;*             PENTE               *
;*                                 *
;*         implemented by          *
;*           Tis Veugen            *
;*         NL-Waalre,1988          *
;*                                 *
;*  file  Pente:PenteCalcTable.s   *
;*                                 *
;***********************************
;
code
	bra	InitTab
data
;
; bit patterns :
;
PATempty  = %00
PATwhite  = %01
PATblack  = %10
PATborder = %11
;
; codes :
;
EVC5   = %10000000	; pente
EVCn   = %01000000	; normal move
EVC5t  = %00100000	; pente threat
EVC5s  = %00010000	; pente threat solved
EVCtkt = %00001000	; take threat
EVCtks = %00000100	; take threat solved
EVCtkl = %00000010	; take left
EVCtkr = %00000001	; take right
EVCs5t = %10010011	; mask for solving pente threat
EVCrds = %10110011	; mask for reduced moves (no take solve)
EVCrdt = %10110111	; mask for reduced moves (no take threat)
EVCrdn = %10111111	; mask for reduced moves (no normal)
EVCall = %11111111	; mask for all moves
;
; evaluations
;
ZBBZ     = 512
ZBBE     =  50
ZZZB     =  40

ZZZZZ    = 8192
EZZZZE   = 1024
ZEZZZEZ  = EZZZZE
ZZEZZM   = EZZZZE
ZZZEZM   = EZZZZE
ZZZZE    = 256 + 32
ZZZEZ    = 256 + 16
ZZEZZ    = 256 - 5
ZEZZZ    = ZZZEZ
EZZZZ    = ZZZZE

EEZZZEE  = 40 + 110
EEZZZE   = 40 +  60
EEZZZ    = 40
EZZZEE   = EEZZZE
EZZZE    = EEZZZ
ZZZEE    = EEZZZ

EEZZEZE  = -5 + 8
EEZZEZ   = -5 + 3
EZZEZE   = EEZZEZ
EZZEZ    = -5
ZZEZE    = EZZEZ
EZZEZEE  = EEZZEZE
ZZEZEE   = EEZZEZ

EEZEZZE  = EZZEZEE
EEZEZZ   = ZZEZEE
EZEZZE   = EZZEZE
EZEZZ    = ZZEZE
ZEZZE    = EZZEZ
EZEZZEE  = EEZZEZE
ZEZZEE   = EEZZEZ

EEZEZEZ  = 8 + 12
EZEZEZ   = 8 + 7
ZEZEZ    = 8
EZEZEZE  = EEZEZEZ
ZEZEZEE  = EEZEZEZ
ZEZEZE   = EZEZEZ

EEZZEE   = 2 + 8
EEZZE    = 2
EZZEE    = EEZZE

EEZEZEE  = 5 + 25
EEZEZE   = 5 + 10
EEZEZ    = 5
EZEZEE   = EEZEZE
EZEZE    = EEZEZ
ZEZEE    = EEZEZ

ZBBBB    = EZZZZ - 10
BZBBB    = ZEZZZ - 10
BBZBB    = ZZEZZ - 10
BBBZB    = ZZZEZ - 10
BBBBZ    = ZZZZE - 10

EZBBBE   = 100
EBZBBE   = 100
EBBZBE   = EBZBBE
EBBBZE   = EZBBBE

EBZBE    = 25

EZZB     = - [ ZBBZ / 2 ]
BZZE     = EZZB

code
;
; representation :
;	        p a t t e r n        m a s k         		 b o a r d
;	       8 7 6 5 4 3 2 1   8 7 6 5 4 3 2 1 		 8765.4321
PAT5:
	dc.w %0000000001010101,%0000000011111111,EVC5,ZZZZZ	;....Wwwww
PAT5dt:
	dc.w %0000000001010100,%0000001111111111,EVC5t,EZZZZE	;...eWwwwe
	dc.w %0000010001010001,%0000111111111111,EVC5t,ZEZZZEZ	;..weWwwew
	dc.w %0001010001000101,%0011111111111111,EVC5t,ZZEZZM	;.wweWweww
	dc.w %0101010000010101,%1111111111111111,EVC5t,ZZZEZM	;wwweWewww
	dc.w 0
PAT5t:
	dc.w %0000000001010100,%0000000011111111,EVC5t,ZZZZE	;....Wwwwe
	dc.w %0000000001010001,%0000000011111111,EVC5t,ZZZEZ	;....Wwwew
	dc.w %0000000001000101,%0000000011111111,EVC5t,ZZEZZ	;....Wweww
	dc.w %0000000000010101,%0000000011111111,EVC5t,ZEZZZ	;....Wewww
	dc.w %0000000001010100,%0000001111111100,EVC5t,EZZZZ	;...eWwww.
	dc.w 0
PAT3www:
	dc.w %0000000001010000,%0000111111111111,EVCn,EEZZZEE	;..eeWwwee
	dc.w %0000000001010000,%0000111111111100,EVCn,EEZZZE	;..eeWwwe.
	dc.w %0000000001010000,%0000111111110000,EVCn,EEZZZ	;..eeWww..
	dc.w %0000000001010000,%0000001111111111,EVCn,EZZZEE	;...eWwwee
	dc.w %0000000001010000,%0000001111111100,EVCn,EZZZE	;...eWwwe.
	dc.w %0000000001010000,%0000000011111111,EVCn,ZZZEE	;....Wwwee
	dc.w 0
PAT3wwew:
	dc.w %0000000001000100,%0000111111111111,EVCn,EEZZEZE	;..eeWwewe
	dc.w %0000000001000100,%0000111111111100,EVCn,EEZZEZ	;..eeWwew.
	dc.w %0000000100010000,%0000111111111111,EVCn,EZZEZEE	;..ewWewee
	dc.w %0000000100010000,%0000001111111111,EVCn,ZZEZEE	;...wWewee
	dc.w %0000000001000100,%0000001111111111,EVCn,EZZEZE	;...eWwewe
	dc.w %0000000001000100,%0000001111111100,EVCn,EZZEZ	;...eWwew.
	dc.w %0000000001000100,%0000000011111111,EVCn,ZZEZE	;....Wwewe
	dc.w 0
PAT3weww:
	dc.w %0000000000010100,%0000111111111111,EVCn,EEZEZZE	;..eeWewwe
	dc.w %0000000000010100,%0000111111111100,EVCn,EEZEZZ	;..eeWeww.
	dc.w %0000010001000000,%0011111111111100,EVCn,EZEZZEE	;.eweWwee.
	dc.w %0000010001000000,%0000111111111100,EVCn,ZEZZEE	;..weWwee.
	dc.w %0000000000010100,%0000001111111111,EVCn,EZEZZE	;...eWewwe
	dc.w %0000000000010100,%0000001111111100,EVCn,EZEZZ	;...eWeww.
	dc.w %0000000000010100,%0000000011111111,EVCn,ZEZZE	;....Wewwe
	dc.w 0
PAT3wewew:
	dc.w %0000000000010001,%0000111111111111,EVCn,EEZEZEZ	;..eeWewew
	dc.w %0000000100010000,%0000111111111111,EVCn,ZEZEZEE	;..weWewee
	dc.w %0000000100010000,%0011111111111111,EVCn,EZEZEZE	;.eweWewe.
	dc.w %0000000100010000,%0000111111111100,EVCn,ZEZEZE	;..weWewe.
	dc.w %0000000000010001,%0000001111111111,EVCn,EZEZEZ	;...eWewew
	dc.w %0000000000010001,%0000000011111111,EVCn,ZEZEZ	;....Wewew
	dc.w 0
PAT2ww:
	dc.w %0000000001000000,%0000111111111100,EVCn,EEZZEE	;..eeWwee.
	dc.w %0000000001000000,%0000111111110000,EVCn,EEZZE	;..eeWwe..
	dc.w %0000000001000000,%0000001111111100,EVCn,EZZEE	;...eWwee.
	dc.w 0
PAT2wew:
	dc.w %0000000000010000,%0000111111111111,EVCn,EEZEZEE	;..eeWewee
	dc.w %0000000000010000,%0000111111111100,EVCn,EEZEZE	;..eeWewe.
	dc.w %0000000000010000,%0000111111110000,EVCn,EEZEZ	;..eeWew..
	dc.w %0000000000010000,%0000001111111111,EVCn,EZEZEE	;...eWewee
	dc.w %0000000000010000,%0000001111111100,EVCn,EZEZE	;...eWewe.
	dc.w %0000000000010000,%0000000011111111,EVCn,ZEZEE	;....Wewee
	dc.w 0
PAT5s:
	dc.w %0000000010101010,%0000000011111111,EVC5s,ZBBBB	;....Wbbbb
	dc.w %0000001010101000,%0000001111111100,EVC5s,BZBBB	;...bWbbb.
	dc.w %0000101010100000,%0000111111110000,EVC5s,BBZBB	;..bbWbb..
	dc.w %0010101010000000,%0011111111000000,EVC5s,BBBZB	;.bbbWb...
	dc.w %1010101000000000,%1111111100000000,EVC5s,BBBBZ	;bbbbW....
	dc.w 0
PAT4s:
	dc.w %0000000010101000,%0000001111111111,EVCn,EZBBBE	;...eWbbbe
	dc.w %0000001010100000,%0000111111111100,EVCn,EBZBBE	;..ebWbbe.
	dc.w %0000101010000000,%0011111111110000,EVCn,EBBZBE	;.ebbWbe..
	dc.w %0010101000000000,%1111111111000000,EVCn,EBBBZE	;ebbbWe...
	dc.w 0
PAT3s:
	dc.w %0000001010000000,%0000111111110000,EVCn,EBZBE	;..ebWbe..
PATbtk:
	dc.w %0000000001100000,%0000001111110000,EVCn,EZZB	;...eWwb..
	dc.w %0000001001000000,%0000001111110000,EVCn,BZZE	;...bWwe..
	dc.w 0
PATtkr:
	dc.w %0000000010100100,%0000000011111100,EVCtkr,ZBBZ	;....Wbbw.
PATtkl:
	dc.w %0001101000000000,%0011111100000000,EVCtkl,ZBBZ	;.wbbW....
PATtktr:
	dc.w %0000000010100000,%0000000011111100,EVCtkt,ZBBE	;....Wbbe.
PATtktl:
	dc.w %0000101000000000,%0011111100000000,EVCtkt,ZBBE	;.ebbW....
PATtksr:
	dc.w %0000000001011000,%0000000011111100,EVCtks,ZZZB	;....Wwwb.
PATtksl:
	dc.w %0010010100000000,%0011111100000000,EVCtks,ZZZB	;.bwwW....
;
MBUILDS: macro ; parameter label-end, flag
	move.w	(A2)+,D4	; pattern
	if ?2=1
	beq	?1
	endif
	move.w	(A2)+,D5	; mask
	move.w	(A2)+,D6	; code
	move.w	(A2)+,D7	; evaluation
	endm
;
MBUILDEV: macro ; parameter label-skip, label-end, flag
	move.w	D0,D1		; test-pattern -> D1
	and.w	D5,D1		; "and" with mask
	cmp.w	D4,D1		; compare with code-pattern
	bne.s	?1
	move.l	D0,D1
	add.l	D1,D1
	or.b	D6,0(A0,D0.l)	; update code
	add.w	D7,0(A1,D1.l)	; update score
	if ?3=1
	bra	?2
	endif
	endm
;
MBUILDPAT: macro ; parameters label-end, label-next-pattern
	move.w	D4,D3		; restore pattern including W
	andi.l	#$FF00,D3	; D4hi.D4lo -> D4hi.$00
	lsl.l	#2,D3		; D4hi.D4lo -> D4hi.%00.$00
	ori.w	#$0100,D3	; D4hi.D4lo -> D4hi.%01.$00 (include W)
	move.b	D4,D3		; D4hi.D4lo -> D4hi.%01.D4lo (D4 restored)
;
MBUILDPATR?0:
	lsl.l	#2,D3		; rotate extended pattern
	lsl.w	#2,D5		; rotate mask
	bcs	?1
	move.w	D3,D4		; check W in middle
	andi.w	#$0300,D4
	cmpi.w	#$0100,D4
	bne.s	MBUILDPATR?0
;
	move.l	D3,D4	; pattern found, compress pattern
	lsr.l	#2,D4	; 10.fedcba98.76543210 ->  10fedcba.98.765432
	move.b	D3,D4	; 10.fedcba98.76543210 ->  10fedcba. 76543210
	bra.s	?2
	endm
;
BUILDCODES:	; build the 64K codes for take/threat indication
;
	move.l	TableC(A5),A0	; table codes
	move.l	TableEV(A5),A1	; table evaluations
	move.l	#$FFFF,D0
BUILDL:
	move.l	D0,D1
	add.l	D1,D1
	move.b	#EVCn,0(A0,D0.l)	; initialize code
	clr.w	0(A1,D1.l)		; clear score
;
BLDS5:	lea       PAT5(PC),A2
BLD5:	MBUILDS   BLDS5dt, 0
BLDE5:	MBUILDEV  BLDP5,   BUILDN, 1
BLDP5:	MBUILDPAT BLDS5dt, BLDE5
;
BLDS5dt: lea       PAT5dt(PC),A2
BLD5dt:  MBUILDS   BLDS5t, 1
BLDE5dt: MBUILDEV  BLDP5dt, BUILDN, 1
BLDP5dt: MBUILDPAT BLD5dt,  BLDE5dt
;
BLDS5t: lea       PAT5t(PC),A2
BLD5t:  MBUILDS   BLDS3www, 1
BLDE5t: MBUILDEV  BLDP5t, BLDS5s, 1
BLDP5t: MBUILDPAT BLD5t,  BLDE5t
;
BLDS3www: lea       PAT3www(PC),A2
BLD3www:  MBUILDS   BLDS3wwew, 1
BLDE3www: MBUILDEV  BLDP3www, BLDS5s, 1
BLDP3www: MBUILDPAT BLD3www,  BLDE3www
;
BLDS3wwew: lea       PAT3wwew(PC),A2
BLD3wwew:  MBUILDS   BLDS3weww, 1
BLDE3wwew: MBUILDEV  BLDP3wwew, BLDS5s, 1
BLDP3wwew: MBUILDPAT BLD3wwew,  BLDE3wwew
;
BLDS3weww: lea       PAT3weww(PC),A2
BLD3weww:  MBUILDS   BLDS3wewew, 1
BLDE3weww: MBUILDEV  BLDP3weww, BLDS5s, 1
BLDP3weww: MBUILDPAT BLD3weww,  BLDE3weww
;
BLDS3wewew: lea       PAT3wewew(PC),A2
BLD3wewew:  MBUILDS   BLDS2ww, 1
BLDE3wewew: MBUILDEV  BLDP3wewew, BLDS5s, 1
BLDP3wewew: MBUILDPAT BLD3wewew,  BLDE3wewew
;
BLDS2ww: lea       PAT2ww(PC),A2
BLD2ww:  MBUILDS   BLDS2wew, 1
BLDE2ww: MBUILDEV  BLDP2ww, BLDS5s, 1
BLDP2ww: MBUILDPAT BLD2ww,  BLDE2ww
;
BLDS2wew: lea       PAT2wew(PC),A2
BLD2wew:  MBUILDS   BLDS5s, 1
BLDE2wew: MBUILDEV  BLDP2wew, BLDS5s, 1
BLDP2wew: MBUILDPAT BLD2wew,  BLDE2wew
;
BLDS5s: lea       PAT5s(PC),A2
BLD5s:  MBUILDS   BLDS4s, 1
BLDE5s: MBUILDEV  BLD5s, BLDS4s, 1
;
BLDS4s: lea       PAT4s(PC),A2
BLD4s:  MBUILDS   BLDS3s, 1
BLDE4s: MBUILDEV  BLD4s, BLDS3s, 1
;
BLDS3s: lea       PAT3s(PC),A2
BLD3s:  MBUILDS   BLDSbtk, 0
BLDE3s: MBUILDEV  BLDSbtk, BLDSbtk, 0
;
BLDSbtk: lea       PATbtk(PC),A2
BLDbtk:  MBUILDS   BLDStkr, 1
BLDEbtk: MBUILDEV  BLDPbtk, BLDStkr, 1
BLDPbtk: MBUILDPAT BLDbtk,  BLDEbtk
;
BLDStkr: lea       PATtkr(PC),A2
BLDtkr:  MBUILDS   BLDStkl, 0
BLDEtkr: MBUILDEV  BLDStkl, BLDStkl, 0
;
BLDStkl: lea       PATtkl(PC),A2
BLDtkl:  MBUILDS   BLDStktr, 0
BLDEtkl: MBUILDEV  BLDStktr, BLDStktr, 0
;
BLDStktr: lea       PATtktr(PC),A2
BLDtktr:  MBUILDS   BLDStktl, 0
BLDEtktr: MBUILDEV  BLDStktl, BLDStktl, 0
;
BLDStktl: lea       PATtktl(PC),A2
BLDtktl:  MBUILDS   BLDStksr, 0
BLDEtktl: MBUILDEV  BLDStksr, BLDStksr, 0
;
BLDStksr: lea       PATtksr(PC),A2
BLDtksr:  MBUILDS   BLDStksl, 0
BLDEtksr: MBUILDEV  BLDStksl, BLDStksl, 0
;
BLDStksl: lea       PATtksl(PC),A2
BLDtksl:  MBUILDS   BUILDN, 0
BLDEtksl: MBUILDEV  BUILDN, BUILDN, 0
;
BUILDN:
	dbra	D0,BUILDL
BUILDE:
	rts
;
data
	ExecBase    = 4
	OpenLib     = -552
	CloseLib    = -414
	Open        =  -30
	Close       =  -36
	Read        =  -42
	Write       =  -48
	Output      =  -60
	AllocMem    = -198
	FreeMem     = -210

	MEMF_CLEAR  = $10000
	MODE_OLD    = 1005
	MODE_NEW    = 1006

	TableCLEN  = $10000
	TableEVLEN = $20000
	TableLEN   = TableCLEN + TableEVLEN

	DosBase    =  0
	OutHandle  =  4
	TableC     =  8
	TableEV    = 12
	FileHandle = 16
code
;
MWrite:	macro	; parameter: start of text, end of text
	move.l	DosBase(A5),A6
	move.l	OutHandle(A5),D1
	lea	?1(PC),A0
	move.l	A0,D2
	move.l	#?2-?1,D3
	jsr	Write(A6)
	endm
;
InitTab:
	movem.l	D0-D7/A0-A6,-(SP)
	lea	PTRS(PC),A5
	move.l	ExecBase,A6
	lea	DosName(PC),A1
	moveq	#0,D0
	jsr	OpenLib(A6)
	move.l	D0,DosBase(A5)
	beq	EndDosE
;
	move.l	D0,A6		; Get OutHandle
	jsr	Output(A6)
	move.l	D0,OutHandle(A5)
;
	move.l	#TableLEN,D0	; Allocate memory
	move.l	#MEMF_CLEAR,D1
	move.l	ExecBase,A6
	jsr	AllocMem(A6)
	move.l	D0,TableC(A5)
	beq.s	ErrMem
	addi.l	#TableCLEN,D0
	move.l	D0,TableEV(A5)
;
	MWrite	TxtWork, TxtWorkE
	bsr	BUILDCODES
	MWrite	TxtWorkDone, TxtWorkDoneE
;
	bsr	SaveData
;
EndMem:
	move.l	#TableLEN,D0
	move.l	TableC(A5),A1
	move.l	ExecBase,A6
	jsr	FreeMem(A6)
;
EndDos:
	move.l	ExecBase,A6
	move.l	DosBase(A5),A1
	jsr	CloseLib(A6)
EndDosE:
;
EndTab:	movem.l	(SP)+,D0-D7/A0-A6
	rts
;
ErrMem:
	MWrite	TxtErrMem, TxtErrMemE
	bra.s	EndDos
;
SaveData:
	MWrite	TxtSave, TxtSaveE
	move.l	DosBase(A5),A6	; open file
	lea	FileName(PC),A0
	move.l	A0,D1
	move.l	#MODE_NEW,D2
	jsr	Open(A6)
	move.l	D0,FileHandle(A5)
	beq.s	ErrSave
;
	move.l	DosBase(A5),A6	; write data
	move.l	FileHandle(A5),D1
	move.l	TableC(A5),D2
	move.l	#TableLEN,D3
	jsr	Write(A6)
	MWrite	TxtSaveDone, TxtSaveDoneE
;
	move.l	DosBase(A5),A6	; close file
	move.l	FileHandle(A5),D1
	jsr	Close(A6)
	rts
;	
ErrSave:
	MWrite	TxtErrSave, TxtErrSaveE
	rts
;
PTRS:	blk.l	5,0
;
DosName: dc.b	"dos.library",0
	even
FileName: dc.b	"Pente:PenteTable",0
	even
;
TxtErrMem:
	dc.b	13,10,"PenteCalcTable: ERROR not enough memory !",0
TxtErrMemE:
	even
TxtWork:
	dc.b	13,10,"PenteCalcTable: Working...",0
TxtWorkE:
	even
TxtWorkDone:
	dc.b	13,10,"PenteCalcTable: Work done",0
TxtWorkDoneE:
	even
TxtSave:
	dc.b	13,10,"PenteCalcTable: Saving...",0
TxtSaveE:
	even
TxtSaveDone:
	dc.b	13,10,"PenteCalcTable: Save done",0
TxtSaveDoneE:
	even
TxtErrSave:
	dc.b	13,10,"PenteCalcTable: ERROR saving data !",0
TxtErrSaveE:
	even
;
end
