zxGammon

input.asm

The keyboard, the beeper and the dice. Download · All the source

; ---------------------------------------------------------------- input; The keyboard read directly (interrupts stay off: the engine pops weights; off the stack pointer), the beeper, and the dice.; scan: A = the key held (0: none). The shifts are passed over, so CAPS; SHIFT with 6 or 7 (the cursor keys) reads as 6 or 7.scan:   push de        ld hl,KEYTAB        ld bc,$fefe.r:     in a,(c)        cpl        and $1f        ld e,5.b:     rra        jr nc,.n        ld d,a        ld a,(hl)        or a        jr nz,.got        ld a,d.n:     inc hl        dec e        jr nz,.b        rlc b        jr c,.r        xor a.got:   pop de        ret; getk: getkey, the keys it will take (for KSET) in A.getk:   ld (KSET),a; getkey: A = the next key pressed (after the last is let go). The time; waited stirs the dice. I is not returned: it shows or hides the pip; counts. KWAIT is 1 while it waits; KSET is left at K_ANY.getkey: call scan        or a        jr nz,getkey        ld b,20        call pause        ld a,1        ld (KWAIT),a.w:     ld hl,(SEED)        inc hl        ld a,h        or l        jr nz,.s        inc hl.s:     ld (SEED),hl        call scan        or a        jr z,.w        push af        xor a        ld (KWAIT),a        call click        pop af        ; I, anywhere: the pip counts shown or hidden        cp 'I'        jr z,.pips        push af        xor a                   ; K_ANY        ld (KSET),a        pop af        ret.pips:  ld a,(PIPOFF)        xor 1        ld (PIPOFF),a        ld a,(UP)        or a        call nz,drawpips        jr getkeyKEYTAB: db 0,'Z','X','C','V'        db 'A','S','D','F','G'        db 'Q','W','E','R','T'        db '1','2','3','4','5'        db '0','9','8','7','6'        db 'P','O','I','U','Y'        db 13,'L','K','J','H'        db ' ',0,'M','N','B'; yesno: carry for Y, not for N; A = the keys' set (K_TAKE or K_AGAIN).yesno:  push af        call getk        pop bc        cp 'Y'        scf        ret z        cp 'N'        ld a,b        jr nz,yesno        or a        ret; pause: B fiftieths of a second, about.pause:  push bc        ld bc,2600.l:     dec bc        ld a,b        or c        jr nz,.l        pop bc        djnz pause        ret; tone: B cycles of a square wave, C its half period.tone:   ld a,$10.l:     out ($fe),a        ld e,c.d:     dec e        jr nz,.d        xor $10        djnz .l        xor a        out ($fe),a        retclick:  ld bc,$0660        jr tone; tune: HL -> pairs of half period and cycles, ended by 0.tune:   ld c,(hl)        inc hl        ld a,c        or a        ret z        ld b,(hl)        inc hl        push hl        call tone        pop hl        jr tune; rand: A = a random byte (xorshift).rand:   ld hl,(SEED)        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        ld (SEED),hl        ret; rand6: A = a die, 1 to 6.rand6:  call rand        and 7        cp 6        jr nc,rand6        inc a        ret; roll: the dice rolled for SHOWN, rattling.roll:   ld b,7.r:     push bc        call rand6        ld (DIE1),a        call rand6        ld (DIE2),a        call drawdice        call rand        and $3f        or $20        ld c,a        ld b,4        call tone        ld b,3        call pause        pop bc        djnz .r        ; shown larger first, but the opening roll's (one die each)        ld a,(SHOWN)        cp 3        jr z,setdice        ld a,(DIE1)        ld b,a        ld a,(DIE2)        cp b        jr c,setdice        jr z,setdice        ld (DIE1),a        ld a,b        ld (DIE2),a; setdice: DHI >= DLO from DIE1, DIE2, larger first for the engine.setdice:        ld a,(DIE1)        ld b,a        ld a,(DIE2)        cp b        jr c,.lo        ld (DHI),a        ld a,b        ld (DLO),a        ret.lo:    ld (DLO),a        ld a,b        ld (DHI),a        retT_WIN:  db $50,40,$40,50,$30,60,$28,120,0T_LOSE: db $30,40,$40,40,$60,40,$80,80,0T_DBL:  db $30,30,$28,30,$30,30,$28,60,0