zxGammon

n2net.asm

The neural network, in integers and without multiplication (products from a table of quarter squares): its first layer kept by adding and taking away a row as each checker moves, then products of its units in pairs and the head for the kind of position (contact, crashed or race), to the chances of each result and an equity. Download · All the source

; ---------------------------------------------------------------- n2net; The net2-style evaluation in the integers of NET2_FORMAT.md, on the; tables of n2data.asm (their geometry from its constants): accumulators; kept by adding and taking away transformer rows, units clipped, pair; products, the head's three dense layers, and the softmax's equity.;; Products are by quarter squares: x w = G[x + w'] - G[w' - x] (w' = w +; 128, G the table divided by 4), G[w' - x] read as G[256 + x - w'] (G is; symmetric about 128), so that only G[0..511] is kept: its low bytes from; N2_G0 (a page), its high bytes 512 on. Weights are POPped: interrupts must be off.HP      equ N2_F/2                      ; pair productsH_T1    equ N2_L1*(HP+2)                ; a head: its layers' rows and shiftsH_L2    equ H_T1+N2_L1H_T2    equ H_L2+N2_L2*(N2_L1+2)H_L3    equ H_T2+N2_L2H_T3    equ H_L3+6*(N2_L2+2)N2_RPS  equ 24*N2_RPT+N2_RBAR+N2_ROFF   ; rows of a side's slots; ------------------------------------------------------------------ rows; rowadd: HL -> F accumulators (low, high; within a page), DE -> a row of; F weights (plus 128), B = F / ROWU: the row added. rowsub takes it away.ROWU    equ 4                   ; weights an iterationrowadd:        ld (SPSAVE),sp        ex de,hl        ld sp,hl        ex de,hl.l:        pop de        ld a,e        add a,(hl)        ld (hl),a        inc l        jr nc,$+3        inc (hl)        inc l        ld a,d        add a,(hl)        ld (hl),a        inc l        jr nc,$+3        inc (hl)        inc l        pop de        ld a,e        add a,(hl)        ld (hl),a        inc l        jr nc,$+3        inc (hl)        inc l        ld a,d        add a,(hl)        ld (hl),a        inc l        jr nc,$+3        inc (hl)        inc l        djnz .l        ld sp,(SPSAVE)        retrowsub:        ld (SPSAVE),sp        ex de,hl        ld sp,hl        ex de,hl.l:        pop de        ld a,(hl)        sub e        ld (hl),a        inc l        jr nc,$+3        dec (hl)        inc l        ld a,(hl)        sub d        ld (hl),a        inc l        jr nc,$+3        dec (hl)        inc l        pop de        ld a,(hl)        sub e        ld (hl),a        inc l        jr nc,$+3        dec (hl)        inc l        ld a,(hl)        sub d        ld (hl),a        inc l        jr nc,$+3        dec (hl)        inc l        djnz .l        ld sp,(SPSAVE)        ret; trow: B = the view (0 the slot's own side's, 1 the other's), C = a slot; (0..25), D = a level (1..15) -> HL = its transformer row: a slot's levels; from its row count on share its last row. Keeps BC, DE.trow:        push bc        push de        ld hl,0        ld a,b        or a        jr z,.r0        ld hl,N2_RPS.r0:    ld a,c        ld e,N2_RBAR        ld bc,24*N2_RPT        cp 24        jr z,.k        ld e,N2_ROFF        ld bc,24*N2_RPT+N2_RBAR        jr nc,.k        ld e,N2_RPT        ld b,a        xor a        inc b.m:     dec b        jr z,.md        add a,N2_RPT        jr .m.md:    ld c,a.k:     add hl,bc        ld a,d        cp e        jr c,.lv        ld a,e.lv:    dec a        ld c,a        ld b,0        add hl,bc        ex de,hl        ld a,N2_F        call mul        ld bc,N2_ROWS        add hl,bc        pop de        pop bc        ret; scan: HL -> a board, A = the view of its first side (0 or 1): (SCANFN); called with B = the view, C = the slot, D = the level, for each level of; each slot of both sides (any registers).scan:        ld (SCANB),hl        ld (SCANR),a        call .half        ld hl,(SCANB)        ld de,OPPOFF        add hl,de        ld (SCANB),hl        ld hl,SCANR        ld a,(hl)        xor 1        ld (hl),a.half:  ld c,0.sl:    ld hl,(SCANB)        ld a,c        call index        ld e,a        ld d,1.lv:    ld a,e        cp d        jr c,.ns        push bc        push de        ld a,(SCANR)        ld b,a        call .fn        pop de        pop bc        inc d        jr .lv.ns:    inc c        ld a,c        cp 26        jr nz,.sl        ret.fn:    ld hl,(SCANFN)        jp (hl); accscan: ACC2 = the accumulators of the board at HL, A = the view of; its first side: the biases, and each checker's rows.accscan:        push hl        push af        ld hl,N2_FTBIAS        ld de,ACC2        ld bc,2*N2_F        ldir        ld hl,.row        ld (SCANFN),hl        pop af        pop hl        jp scan.row:   call trow        ex de,hl        ld hl,ACC2        ld b,N2_F/ROWU        jp rowadd; evrec: EVACC = the accumulators at HL with the rows recorded for the; GTARGET steps of a play added and taken away.evrec:        ld de,EVACC        ld bc,2*N2_F        ldir        ld a,(GTARGET)        or a        ret z        ld b,a        ld hl,REC.s:     push bc        push hl        ld b,(hl)        inc hl.e:     push bc        ld e,(hl)        inc hl        ld d,(hl)        inc hl        ld a,(hl)        inc hl        push hl        ld hl,EVACC        ld b,N2_F/ROWU        or a        jr z,.sub        call rowadd        jr .n.sub:   call rowsub.n:     pop hl        pop bc        djnz .e        pop hl        ld de,16        add hl,de        pop bc        djnz .s        ret; ----------------------------------------------------------------- units; uclamp: DE = a sum (signed), A = a shift k, C = a ceiling T -> A =; clamp(DE >> k, 0, T) (DE << -k for k < 0). Keeps HL.uclamp:        bit 7,d        jr nz,.z        or a        jr z,.c        jp m,.lft.r:     srl d        rr e        dec a        jr nz,.r.c:     ld a,d        or a        jr nz,.sat        ld a,e        cp c        ret c.sat:   ld a,c        ret.z:     xor a        ret.lft:   ld b,a.l:     ld a,d        or a        jr nz,.sat        sla e        rl d        inc b        jr nz,.l        jr .c; pairs: PAIRS[j] for j < F/2: the units u = j and v = j + F/2 of EVACC; (clamp(acc >> k, 0, T)), P = G[u + v] - G[v - u] + 32 u, and p = (P +; 2^(s-1)) >> s, made as the high byte of (P << (8 - s)) + 128 (s <= 8);; 0 if either unit is.pairs:        ld ix,N2_USHIFT         ; T at -F, s at +F        ld hl,EVACC        exx        ld hl,PAIRS        exx        ld b,HP.j:     push bc        ld e,(hl)        inc l        ld d,(hl)        inc l        ld c,(ix-N2_F)        ld a,(ix+0)        call uclamp        or a        jr z,.st        ld (PU),a        push hl        ld a,l        add a,2*HP-2        ld l,a        ld e,(hl)        inc l        ld d,(hl)        ld c,(ix+HP-N2_F)        ld a,(ix+HP)        call uclamp        or a        jr z,.zp        ld c,a                  ; v        ld a,(PU)        ld b,a                  ; u        add a,c        ld l,a        ld a,high N2_G0        adc a,0        ld h,a        ld e,(hl)        inc h        inc h        ld d,(hl)               ; G[u + v]        ld a,b        sub c        ld l,a        ld a,high N2_G0 + 1        sbc a,0        ld h,a        ld a,e        sub (hl)        ld e,a        inc h        inc h        ld a,d        sbc a,(hl)        ld d,a                  ; - G[256 + u - v] (= G[v - u])        ld a,b        rrca        rrca        rrca        ld l,a        and $1f        ld h,a        xor l        ld l,a        add hl,de               ; + 32 u        ld a,8        sub (ix+N2_F)        jr z,.r        ld b,a.l:     add hl,hl        djnz .l.r:     ld a,l        add a,128        ld a,h        adc a,0.zp:    pop hl.st:    exx        ld (hl),a        inc hl        exx        inc ix        pop bc        djnz .j        ret; ----------------------------------------------------------------- dense; dense: a head layer: DN inputs (bytes) at DX, DL outputs by the rows at; DW (a 16-bit bias, then DN weights plus 128), into DY: with DMODE 0; bytes clamp(S >> t, 0, 255), with 1 the 16-bit S >> t, its sign bit; flipped; t from DT. Each nonzero input gets a block of; code (the template below, its place, x and -x patched in); NB of them; at a time are run for every output, its row at IX and its sum in IY,; the sums kept in PART between such passes.dense:        ld a,(DN)        ld b,a        add a,2        ld (dnext+1),a          ; the row's length        xor a        ld (DPASS),a        ld hl,(DX)        ld c,2                  ; the input's place in a rowdgath:  ld de,BSIZE        ld iy,BLK        exx        ld b,NB        exx.g:     ld a,(hl)        inc hl        or a        jr z,.z        ld (iy+2),c        ld (iy+4),a        ld (iy+12),a        add iy,de        exx        dec b        exx        jr z,.full.z:     inc c        djnz .g        xor a                   ; the last pass        jr drun.full:  inc c        dec b        ld (DXP),hl        ld (DBC),bc        xor a                   ; the last unless a nonzero input is left        inc b        jr .t.n:     or (hl)        inc hl.t:     djnz .n; drun: the blocks to IY run for every output; A = 0 on the last pass.drun:   ld (DLASTF),a        ld (DJP),iy        ld (iy+0),$c3           ; jp dret (its block's LD C,(IX+d) back after)        ld (iy+1),low dret        ld (iy+2),high dret        ld hl,dbias        ld a,(DPASS)        or a        jr z,.a        ld hl,dpop.a:     ld (dout+1),hl        inc a        ld (DPASS),a        ld hl,dpush        ld a,(DLASTF)        or a        jr nz,.b        ld hl,dhid        ld a,(DMODE)        or a        jr z,.b        ld hl,dlog.b:     ld (dret+1),hl        ld ix,(DW)        ld a,(DL)        ld (DCNT),a        exx        ld hl,PART        ld de,(DY)        ld bc,(DT)        exxdout:   jp dbias                ; or dpopdbias:  ld l,(ix+0)        ld h,(ix+1)        jr dgodpop:   exx        push hl        exx        pop hl        ld a,(hl)        inc hl        ld h,(hl)        ld l,adgo:    push hl        pop iy        ld b,0        jp BLKdret:   jp dpush                ; or dhid, dlogdpush:  push iy        exx        pop de        ld (hl),e        inc hl        ld (hl),d        dec hl        exx        jr dnextdhid:   push iy        pop hl        exx        ld a,(bc)        inc bc        exx        bit 7,h        jr nz,.z        neg        add a,8                 ; the high byte after 8 - t left shifts        jr z,.h        ld b,a.l:     add hl,hl        jr c,.s        djnz .l.h:     ld a,h        jr .st.s:     ld a,255        jr .st.z:     xor a.st:    exx        ld (de),a        inc de        exx        jr dnextdlog:   push iy        pop hl        exx        ld a,(bc)        inc bc        push de        exx        ld b,a        inc b        jr .t.sh:    sra h        rr l.t:     djnz .sh        ld a,h        xor $80        ld d,a        ld e,l        pop hl        ld (hl),e        inc hl        ld (hl),d        inc hl        push hl        exx        pop de        exxdnext:  ld de,0                 ; the row's length        add ix,de        exx        inc hl        inc hl        exx        ld hl,DCNT        dec (hl)        jp nz,dout        ld hl,(DJP)        ld (hl),$dd        inc hl        ld (hl),$4e        ld a,(DLASTF)        or a        ret z        ld hl,(DXP)        ld bc,(DBC)        jp dgath; One block: B = 0, IX -> the output's row, IY = the sum.tmpl:   ld c,(ix+2)             ; +2: 2 + i; C = w'        ld hl,N2_G0             ; +4: x        add hl,bc               ; (no carry)        ld e,(hl)        inc h        inc h        ld d,(hl)               ; DE = G[x + w']        ld hl,N2_G0 + 256       ; +12: x        sbc hl,bc        ld a,e        sub (hl)        ld e,a        inc h        inc h        ld a,d        sbc a,(hl)        ld d,a                  ; - G[256 + x - w'] (= G[w' - x]): x w        add iy,detmplend:                        ; (BSIZE, in n2ram.asm, is tmplend - tmpl); ------------------------------------------------------------------ smeq; smeq: HL = the equity (8192 a point) of the logits at LOGZ (their sign; bits flipped) with the weights at EVV: d = max - z, e = EXP[d mod 2^U]; >> (d >> U) (0 from 16 on), then sign(N) floor(|N| 256 / D) with; N = sum v e and D = sum e.smeq:        ld hl,LOGZ        ld de,0        ld b,6.m:     ld a,(hl)        inc hl        push hl        ld h,(hl)        ld l,a        or a        sbc hl,de        jr c,.mn        add hl,de        ex de,hl.mn:    pop hl        inc hl        djnz .m        ld hl,0        ld (NUM),hl        ld (DEN),hl        xor a        ld (NUM+2),a        ld ix,LOGZ        ld iy,EVV        ld b,6.e:     push bc        push de        ld l,(ix+0)        ld h,(ix+1)        ex de,hl        or a        sbc hl,de               ; d        ld a,l        and (1 << N2_U) - 1        ld c,a        ld b,8 - N2_U.s:     add hl,hl        jr c,.nx        djnz .s        ld a,h                  ; d >> U        cp 16        jr nc,.nx        ld b,a        ld h,high N2_EXPT        ld l,c        ld e,(hl)        ld a,l        add a,1 << N2_U        ld l,a        ld d,(hl)        inc b        jr .t.ts:    srl d        rr e.t:     djnz .ts                ; e        ld hl,(DEN)        add hl,de        ld (DEN),hl        ld a,(iy+0)        call mulve        ex de,hl        ld hl,(NUM)        add hl,de        ld (NUM),hl        ld a,(NUM+2)        adc a,c        ld (NUM+2),a.nx:    inc ix        inc ix        inc iy        pop de        pop bc        djnz .e        ; |N| x 256 / D        ld a,(NUM)        ld d,a        ld e,0        ld hl,(NUM+1)        ld a,h        or a        push af        jp p,.pos        xor a        sub d        ld d,a        ld a,0        sbc a,l        ld l,a        ld a,0        sbc a,h        ld h,a.pos:   ld bc,(DEN)        ld a,16.dv:    sla e        rl d        adc hl,hl        sbc hl,bc        jr nc,.o        add hl,bc        dec a        jr nz,.dv        jr .q.o:     inc e        dec a        jr nz,.dv.q:     pop af        ex de,hl        ret p        xor a        sub l        ld l,a        sbc a,a        sub h        ld h,a        ret; mulve: C:HL = A x DE (A signed, DE < 4096): A unsigned times DE, less; 256 DE for a negative A.mulve:  ld hl,0        ld c,l        ld b,8        push af.l:     add hl,hl        rl c        rla        jr nc,.n        add hl,de        jr nc,.n        inc c.n:     djnz .l        pop af        or a        ret p        ld a,h        sub e        ld h,a        ld a,c        sbc a,d        ld c,a        ret; ------------------------------------------------------------- evaluate; crashed: carry if the side at HL (its off count at +25) is crashed by; gnubg's rule: at most 6 checkers in play, not counting those deep on its; 1- and 2-points.crashed:        ld bc,25        add hl,bc        ld a,15        sub (hl)                ; checkers in play        sbc hl,bc        cp 7        ret c        ld c,a        ld e,(hl)        inc hl        ld d,(hl)        ld a,e        cp 2        jr c,.b1        add a,6        cp c        ccf        ret c        ld a,d        cp 2        jr c,.no        ld a,c        inc a        sub e        jr c,.yes        sub d        jr c,.yes        cp 7        ret.b1:    ld a,d        or a        ret z        add a,5        cp c        ccf        ret.yes:   scf        ret.no:    or a        ret; evaluate: HL = the equity (8192 a point) of the side on roll, its half; of a board at EVON and the other's at EVOTH, from the accumulators at; EVACC (its view).evaluate:        ; the head: gnubg's class (contact, crashed, race)        ld hl,(EVON)        call highest        ld c,a        ld hl,(EVOTH)        call highest        ld b,2        cp $ff        jr z,.cl        ld e,a        ld a,c        cp $ff        jr z,.cl        add a,e        cp 23        jr c,.cl        dec b        push bc        ld hl,(EVON)        call crashed        jr c,.cr        ld hl,(EVOTH)        call crashed.cr:    pop bc        jr c,.cl        dec b.cl:    ld a,b        add a,a        ld hl,HEADS        call index        ld e,a        inc hl        ld d,(hl)        ld (EHEAD),de        ld hl,VW        call setevv        call pairs        ld hl,LAYERS        ld b,3.ly:    push bc        ld de,DX        ld bc,11        ldir        push hl        ld hl,(DW)        ld de,(EHEAD)        add hl,de        ld (DW),hl        ld hl,(DT)        add hl,de        ld (DT),hl        call dense        pop hl        pop bc        djnz .ly        jp smeq; setevv: HL -> 6 outcomes' weights -> EVV, with no gammons for a side; (EVON, EVOTH) with a checker off; HL left after them.setevv: ld de,EVV        ld bc,6        ldir        push hl        ld hl,(EVON)        ld c,25        add hl,bc        ld a,(hl)        or a        jr z,.w1        ld a,(EVV+3)        ld (EVV+4),a        ld (EVV+5),a.w1:    ld hl,(EVOTH)        add hl,bc        ld a,(hl)        or a        jr z,.w2        ld a,(EVV+0)        ld (EVV+1),a        ld (EVV+2),a.w2:    pop hl        ret; stats: after an evaluation (its logits in LOGZ), SPW = the evaluated; side's chances (8192 for 1): of winning, and the extra points of its; gammons and backgammons won, and lost (n2ref.ax's `stats`).stats:  ld hl,WSTAT        ld de,SPW        ld b,3.s:     push bc        push de        call setevv        push hl        call smeq        ex de,hl        pop bc        pop hl        ld (hl),e        inc hl        ld (hl),d        inc hl        ex de,hl        ld h,b        ld l,c        pop bc        djnz .s        retWSTAT:  db 32,32,32,0,0,0        db 0,32,64,0,0,0        db 0,0,0,0,32,64HEADS:  dw N2_CONTACT_L1,N2_CRASHED_L1,N2_RACE_L1; Each layer: inputs, their count, rows (from the head), outputs, where to,; shifts (from the head), mode.LAYERS: dw PAIRS        db HP        dw 0        db N2_L1        dw H1,H_T1        db 0        dw H1        db N2_L1        dw H_L2        db N2_L2        dw H2,H_T2        db 0        dw H2        db N2_L2        dw H_L3        db 6        dw LOGZ,H_T3        db 1