n2choose.asm
Choosing a move: every legal play scored by the network (the filter's best 99 if there are more), then the best few, while time allows, each tried against all 21 of the opponent's rolls. Download · All the source
; ------------------------------------------------------------- n2choose; decide: the play from ROOT with dice DHI >= DLO -> RESULT, and A = 0 if; there is none (RESULT = ROOT). The chooser of NET2_FORMAT.md, step for; step, its budget counted as the reference counts it (SPENT: 6,600; T-states a play generated, 126,000 an evaluation):;; 1. The plays generated (counted, and one bearing off the last checker; taken at once); with more than 99, the filter's best 99.; 2. Each scored by the main net (the opponent's equity, from the root's; accumulators and the play's recorded rows), the best NLA kept.; 3. Those within MARGIN of the best looked at a ply deeper while the; budget allows: over the opponent's 21 rolls, their filter's best; NREPLY replies scored by the net, the worst for us.;; Lists are a count, a capacity, then 10 bytes an entry: a key (high byte; first, the sign bit flipped: larger is better) and the play's steps (4; points, $ff after the last, and their dice). Plays arrive in generation; order, so a new entry goes after those with the same key.T_GEN equ 8100 ; the cost model (n2ref.ax), T-statesT_MAIN equ 176000T_BUDGET equ 29750000 ; 8.5 s at 3.5 MHzMARGIN equ 2458 ; 0.3 of a point; (NLA, candidates looked at at most, and NREPLY, the opponent's replies; scored a roll, are in n2ram.asm, which sizes the lists by them.)decide: di ld hl,ROOT call offs ld hl,ROOT call fbase xor a ld (WIN),a ld h,a ld l,a ld (SPENT),hl ld (SPENT+2),hl ; the plays counted: the first kept, and the first bearing off the ; last checker ld hl,hcount call genroot call spendg ld hl,(GCOUNT) ld a,h or l ret z ; none: W (RESULT) is ROOT ld de,WINSTEPS ld a,(WIN) or a jp nz,.done dec hl ld a,h or l jp z,.done ; more than 99: the filter's best 99, by the 99th's score and how ; many of that score to take (those equal to it at the end) xor a ld (THRON),a ld (EQN),a ld de,99 ; (HL = the plays less 1) sbc hl,de jr c,.few ld hl,SC99 ld (hl),a inc hl ld (hl),99 ld a,2 ld (LSZ),a ld hl,hscore call genroot ld hl,SC99+2+2*98+1 ld e,(hl) dec hl ld d,(hl) ld (THR),de ld b,1.t: dec hl ld a,(hl) dec hl cp e jr nz,.te ld a,(hl) cp d jr nz,.te inc b jr .t.te: ld a,b ld (NEED),a ld (THRON),a.few: ; the root's accumulators, from the opponent's side ld hl,ROOT ld a,1 call accscan ; every play kept scored by the net, the best NLA kept xor a ld (TOPC),a ld a,10 ld (LSZ),a inc a ld (GREC),a ld hl,hplay call genroot ; the candidates: the first of the best NLA within the margin ld hl,(TOPC+2) ld d,l ld e,h ld hl,-MARGIN add hl,de ex de,hl ld a,(TOPC) ld b,a ld c,0 ld hl,TOPC+2.mg: ld a,(hl) inc hl push hl ld l,(hl) ld h,a or a sbc hl,de pop hl jr c,.mx inc c ld a,9 call addhl djnz .mg.mx: ld a,c ld (NCAND),a xor a ld (BESTI),a ld a,c cp 2 jr c,.pick ; each looked at a ply deeper while the budget allows ld hl,SPENT ld de,ST ld bc,4 ldir xor a.cj: ld (CJ),a ld hl,NCAND cp (hl) jr nc,.pick or a call nz,budget jr c,.pick call look ld hl,BEST ld de,BL ld bc,4 ldir ld hl,TOTAL ld de,BL call sub32 ld a,(CJ) ld c,a ld a,(BL+3) rla ; carry: TOTAL > BEST jr c,.nb inc c dec c jr nz,.nx.nb: ld hl,TOTAL ld de,BEST ld bc,4 ldir ld a,(CJ) ld (BESTI),a.nx: ld a,(CJ) inc a jr .cj.pick: ld a,(BESTI) call csteps.done: ; W (RESULT) = ROOT after the play at DE ld hl,ROOT ld (GROOT),hl ex de,hl call replay ld a,1 ret; genroot: the plays from ROOT to the handler at HL.genroot: ld (USERFN),hl ld hl,ROOT jp gen; csteps: DE -> the steps of TOPC's entry A.csteps: ld hl,TOPC+4 ld de,10 inc a.l: dec a jr z,.x add hl,de jr .l.x: ex de,hl ret; budget: carry if candidate CJ (> 0) is not to be looked at: CJ SPENT +; (SPENT - ST) would pass CJ x the budget.budget: ld hl,BL ld b,8.z: ld (hl),0 inc hl djnz .z ld a,(CJ) ld b,a.l: ld hl,SPENT ld de,BL call add32 ld hl,TBUDGET ld de,BR call add32 djnz .l ld hl,SPENT ld de,BL call add32 ld hl,ST ld de,BL call sub32 ld hl,BL ld de,BR jp sub32TBUDGET: dw T_BUDGET & $ffff,T_BUDGET >> 16; look: TOTAL = candidate CJ's value: over the opponent's 21 rolls; (non-doubles twice), our equity after their reply. ROOT2 = the; candidate's position from the opponent's side, ACC2 its accumulators; from ours.look: ld a,(CJ) call csteps ld hl,ROOT ld (GROOT),hl ex de,hl call replay ld hl,W ld de,ROOT2 call turn ld hl,ROOT2 ld a,1 call accscan ld hl,ROOT2 call fbase ld hl,0 ld (TOTAL),hl ld (TOTAL+2),hl inc l ld h,l.roll: ld (DHI),hl call reply call addtot ld a,(DHI) ld b,a ld a,(DLO) cp b call nz,addtot ld hl,(DHI) ld a,h cp l inc h jr c,.roll ld h,1 inc l ld a,l cp 7 jr c,.roll ret; addtot: TOTAL += HL (signed). Keeps HL.addtot: push hl ld a,h rla sbc a,a ld c,a ex de,hl ld hl,(TOTAL) add hl,de ld (TOTAL),hl ld hl,(TOTAL+2) ld b,c adc hl,bc ld (TOTAL+2),hl pop hl ret; reply: HL = our value after the opponent's roll DHI, DLO from ROOT2.reply: call TICK ; the caller's, each roll (any registers) xor a ld (WIN),a ld (TOP2),a ld hl,hreply ld (USERFN),hl ld hl,ROOT2 call gen call spendg ld hl,(GCOUNT) ld a,h or l jr nz,.some ; they cannot move: our equity on roll as we are call spende xor a ld (GTARGET),a ld hl,ACC2 call evrec ld hl,ROOT2+OPPOFF ld de,ROOT2 jp evalhd.some: ld a,(WIN) or a jr z,.pick ; they bear off their last checker: a single, gammon or backgammon ld hl,WINSTEPS call replay ld a,(W+OPPOFF+25) or a ld a,(VW+3) jr nz,.v ld hl,W+OPPOFF+18 ld b,7 xor a.bk: or (hl) inc hl djnz .bk ld a,(VW+5) jr nz,.v ld a,(VW+4).v: ld h,a ld l,0 ret.pick: ; their filter's best NREPLY: the lowest of our equities after them ld hl,$ffff ld (RWORST),hl ld a,1 ld (GREC),a ld a,(TOP2) ld (RCNT),a ld hl,TOP2+4.r: push hl call replay ld hl,ACC2 call evalw ld a,h xor $80 ld d,a ld e,l ld hl,(RWORST) or a sbc hl,de jr c,.k ld (RWORST),de.k: call spende pop hl ld de,10 add hl,de ex de,hl ld hl,RCNT dec (hl) ex de,hl jr nz,.r xor a ld (GREC),a ld hl,(RWORST) ld a,h xor $80 ld h,a ret; evalw: HL = the equity of the side to roll after the play in W, from the; accumulators at HL and the play's rows. evalhd: of the side whose half; of a board is at HL, the other's at DE, from EVACC.evalw: call evrec ld hl,W+OPPOFF ld de,Wevalhd: ld (EVON),hl ld (EVOTH),de jp evaluate; The handlers of generated plays. hcount: the first play, or the first; bearing off the last checker, into WINSTEPS.hcount: ld a,(WIN) or a ret nz ld a,(W+25) cp 15 jr z,hwin ld hl,(GCOUNT) dec hl ld a,h or l ret nz jr hsavehwin: ld a,1 ld (WIN),ahsave: call mksteps ld hl,NEWSTEPS ld de,WINSTEPS ld bc,8 ldir ret; hscore: the play's filter score into SC99.hscore: call fscore ld hl,SC99 jp linsert; hplay: a root play kept (all, or the filter's best 99) scored by the; net from the opponent's side, into TOPC.hplay: ld a,(THRON) or a jr z,.ev call fscore ld hl,(THR) ex de,hl or a sbc hl,de ret c jr nz,.ev ld hl,EQN ld a,(NEED) cp (hl) ret z inc (hl).ev: call spende ld hl,ACC2 call evalw xor a sub l ld e,a sbc a,a sub h xor $80 ld d,a push de call mksteps pop de ld hl,TOPC jp linsert; hreply: an opponent's play: taken if it bears off their last checker,; else into TOP2 by their filter's score.hreply: ld a,(WIN) or a ret nz ld a,(W+25) cp 15 jr z,hwin call fscore ld a,(TOP2) cp NREPLY jr c,.in ld a,(TOP2+2+10*(NREPLY-1)) ; a full list: better than its last only cp d jr c,.in ret nz ld a,(TOP2+3+10*(NREPLY-1)) cp e ret nc.in: push de call mksteps pop de ld hl,TOP2 jp linsert; linsert: DE = a key, into the list at HL (entries of LSZ bytes: the; key, then LSZ - 2 bytes from NEWSTEPS) after those with keys not lower;; the last falls off a full list.linsert: push hl pop iy inc hl inc hl ld b,0.f: ld a,b cp (iy+0) jr z,.at ld a,(hl) cp d jr c,.at jr nz,.nx inc hl ld a,(hl) dec hl cp e jr c,.at.nx: ld a,(LSZ) call addhl inc b jr .f.at: ld a,b cp (iy+1) ret nc ld a,(iy+1) dec a cp (iy+0) jr c,.full inc (iy+0) ld a,(iy+0) dec a.full: sub b jr z,.put push de push hl ld hl,LSZ ld e,(hl) ld d,0 call mul ; the bytes to move down ld b,h ld c,l pop hl push hl add hl,bc dec hl push hl add hl,de ex de,hl pop hl lddr pop hl pop de.put: ld (hl),d inc hl ld (hl),e inc hl ex de,hl ld a,(LSZ) sub 2 ret z ld c,a ld b,0 ld hl,NEWSTEPS ldir ret; mksteps: NEWSTEPS = the play's steps, $ff for those not taken, and; their dice.mksteps: ld hl,NEWSTEPS ld b,4.c: ld (hl),$ff inc hl djnz .c ld a,(GTARGET) ld b,a ld de,STEPS ld hl,NEWSTEPS.s: ld a,(de) ld (hl),a inc e inc e inc hl djnz .s ld hl,GDIE ld de,NEWSTEPS+4 ld bc,4 ldir ret; replay: HL -> a play's steps: W = the board at GROOT after them (each; step's transformer rows recorded with GREC), GTARGET = their number.replay: push hl call wload pop hl ld c,0.s: ld a,(hl) cp $ff jr z,.x push hl push bc ld b,a ld de,4 add hl,de ld e,(hl) call step pop hl ld c,l push bc call chg pop bc pop hl inc hl inc c ld a,c cp 4 jr nz,.s.x: ld a,c ld (GTARGET),a ret; ---------------------------------------------------------------- filter; chg: after a step from B by E on W (A = 1 if it hit; C = the steps; before it): the filter's sums FSUMC and FSUMR (the mover's view) changed; by the slots it changed, and with GREC their transformer rows (the other; side's view) recorded at REC + 16 C. Keeps BC, DE.chg: push bc push de ld (FHIT),a ld a,b ld (FFROM),a ld a,e ld (FDIE),a ld a,c add a,a add a,a add a,a add a,a add a,low REC ld l,a ld h,high REC ld (RECP),hl ld (hl),0 ; off the point it leaves (the level: its count before) ld h,high W ld l,b ld a,(hl) inc a ld d,a ld c,b ld b,0 ld e,b call slotch ; onto the point it reaches, or borne off (its count after) ld a,(FFROM) ld hl,FDIE sub (hl) jr nc,.on ld a,25.on: ld c,a ld l,a ld h,high W ld d,(hl) ld b,0 ld e,1 call slotch ld a,(FHIT) or a jr z,.x ; a blot hit: off the other side's point 23 - (B - E), to its bar ld a,(FDIE) ld hl,FFROM sub (hl) add a,23 ld c,a ld b,1 ld d,b ld e,0 call slotch ld a,(W+OPPOFF+24) ld d,a ld bc,1*256+24 ld e,1 call slotch.x: pop de pop bc ret; slotch: slot C of the mover's (B = 0) or the other side's (B = 1) gains; (E = 1) or loses (E = 0) a checker, D the level (the count reached, or; left): the filter's sums, and with GREC the transformer's row recorded.slotch: ld a,b or a ld a,c jr z,.m add a,26.m: ld l,a ; the filter's row: 6 (26 B + C) + min(D, 6) - 1 add a,a add a,l ld l,a ld h,0 add hl,hl ld a,d cp 6 jr c,.l ld a,6.l: dec a call addhl push bc push de ld bc,N2_LIN add hl,bc ld c,(hl) ; the contact table's weight ld de,312 add hl,de ld b,(hl) ; the race table's pop de ld a,e or a ld hl,(FSUMC) jr z,.dn ld a,c add a,l ld l,a jr nc,$+3 inc h ld (FSUMC),hl ld hl,(FSUMR) ld a,b add a,l ld l,a jr nc,.st inc h jr .st.dn: ld a,l sub c ld l,a jr nc,$+3 dec h ld (FSUMC),hl ld hl,(FSUMR) ld a,l sub b ld l,a jr nc,.st dec h.st: ld (FSUMR),hl pop bc ld a,(GREC) or a ret z ld a,b xor 1 ld b,a call trow ex de,hl ; DE = the row, L = 1 to add ld a,l push af ld hl,(RECP) inc (hl) ld a,(hl) ld c,a add a,a add a,c sub 2 add a,l ld l,a ld (hl),e inc l ld (hl),d inc l pop af ld (hl),a ret; fscore: DE = the filter's score of W, the mover's view (its sign bit; flipped): the race table's sum if none of the mover's checkers is behind; one of the other side's, else the contact table's.fscore: ld hl,W call highest ld c,a ld hl,W+OPPOFF call highest ld de,(FSUMR) inc a jr z,.k dec a ld b,a ld a,c inc a jr z,.k dec a add a,b cp 24 jr c,.k ld de,(FSUMC).k: ld a,d xor $80 ld d,a ret; fbase: FS0C, FS0R = the filter's sums of the board at HL, its first; side's view. Leaves GREC 0.fbase: push hl ld hl,0 ld (FSUMC),hl ld (FSUMR),hl xor a ld (GREC),a ld hl,.row ld (SCANFN),hl pop hl call scan ld hl,(FSUMC) ld (FS0C),hl ld hl,(FSUMR) ld (FS0R),hl ret.row: ld e,1 jp slotch; ------------------------------------------------------------------ util; spendg: SPENT += T_GEN x GCOUNT; spende: += T_MAIN; spend: += AHL.spendg: ld bc,(GCOUNT).l: ld a,b or c ret z dec bc push bc ld hl,T_GEN xor a call spend pop bc jr .lspende: ld hl,T_MAIN & $ffff ld a,T_MAIN >> 16spend: ld de,(SPENT) add hl,de ld (SPENT),hl ld e,a ld d,0 ld hl,(SPENT+2) adc hl,de ld (SPENT+2),hl ret; add32, sub32: the 4 bytes at DE +=, -= those at HL; carry out. Keep B.add32: push bc ld b,4 or a.l: ld a,(de) adc a,(hl) ld (de),a inc hl inc de djnz .l pop bc retsub32: push bc ld b,4 or a.l: ld a,(de) sbc a,(hl) ld (de),a inc hl inc de djnz .l pop bc ret; addhl: HL += A.addhl: add a,l ld l,a ret nc inc h ret; turn: HL -> a board, DE -> 64 bytes: the board from the other side.turn: push hl ld bc,OPPOFF add hl,bc ld c,32 ldir pop hl ld c,32 ldir ret; init: the work area's pages cleared, the hash values made, the lists'; capacities, the outcomes' weights for money, and the dense layer's; blocks from their template.init: ld hl,WORK ld de,WORK+1 ld bc,WORKEND-WORK-1 ld (hl),0 ldir ld hl,1 ld de,RTAB ld b,102.r: ld a,h rra ld a,l rra xor h ld h,a ld a,l rra ld a,h rra xor l ld l,a xor h ld h,a ex de,hl ld (hl),e inc hl ld (hl),d inc hl ex de,hl djnz .r ld a,NLA ld (TOPC+1),a ld a,NREPLY ld (TOP2+1),a ld hl,N2_VMONEY ld de,VW ld bc,6 ldir ld de,BLK ld b,NB.b: push bc ld hl,tmpl ld bc,BSIZE ldir pop bc djnz .b ret