Star Tip 5 (Keyboard Scanning)

by Pete Cooke

from Your Sinclair #22 (Oct.1987)

[files STARTIP5.*]





It's an astounding thought, but true, that three of the most

original and best selling games of the last 12 months, Tau

Ceti, Room Ten and Micronaut One were all programmed by the

same bloke. This talented 3Dologist is none other than Pete

Cooke, our Pitstop special guest. Pete has spent a lot of

time programming computer games, his first products coming

out on the Richard Shepherd Software label between 1983-85.

First Invincible Island, then Urban Upstart, Inferno and

finally Ski Star 2000. Moving over to CRL in 1985, he

produced Juggernaut, and the Tau Ceti range of games

(applause). Pete now works for Nexus and, having recently

wowed us all with the excellent Micronaut One, is currently

working on a top secret project. Coo-er-gosh.



The routine Pete has contributed to Pitstop is the fab

keyboard scanning routine from both the Tau Ceti games and

Micronaut One. It is an assembly routine, printed as such

because it's easier to make out whats happening that way. So

now you will be able to incorporate Pete's routines into

your games, as the first step to implementing the sort of

Windows, Icons, Mouse and Pointer type controls. This is

just the first step, the rest is up to you.





Method



To substitute different key combinations in the routine,

simply copy new values into the 5-byte table KEYTAB. The

assembly listing is pretty self- explanatory, having piles

of comment lines with details of how to use the program.



  10        org  60000

  20        ent  60000

  30 ;

  40 ;

  50 ;      YS_KEYS

  60 ;

  70 ;prog for Your Sinclair

  80 ;

  90 ;file for Hisoft GENS

 100 ;assembler but should

 110 ;be suitable for most

 120 ;assemblers on the market

 130 ;

 140 ;Reads the keyboard

 150 ;Returns with C

 160 ;holding L/R/U/D/F

 170 ;

 180 ;bit 4,C left

 190 ;bit 3,C right

 200 ;bit 2,C up

 210 ;bit 1,C down

 220 ;bit 0,C fire

 230 ;

 240 ;bit set to 1 if pressed

 250 ;

 260 ;keys are chosen by

 270 ;values in KEYTAB

 280 ;

 290 readke ld   hl,keytab

 300        ld   bc,#0500

 310 ;

 320 ;5 keys to read

 330 ;

 340 read_1 ld   a,(hl)

 350        rra

 360        rra

 370        and  #1e

 380        ld   e,a

 390        ld   d,0

 400 ;

 410 ;2*the row number

 420 ;

 430        ld   a,(hl)

 440        inc  hl

 450        push hl

 460 ;

 470 ;save place in KEYTAB

 480 ;

 490        ld   hl,keyadd

 500        add  hl,de

 510        ld   d,a

 520 ;

 530 ;index port addresses

 540 ;

 550        push bc

 560        ld   c,(hl)

 570        inc  hl

 580        ld   b,(hl)

 590        in   a,(c)

 600        inc  b

 610        dec  b

 620        jr   z,read_2

 630        cpl

 640 read_2 ld   e,a

 650 ;

 660 ;read the port

 670 ;and flip bits if not

 680 ;Kempston

 690 ;

 700        pop  bc

 710        pop  hl

 720 ;

 730 ;and get back BC+HL

 740 ;

 750        ld   a,d

 760        and  #07

 770        jr   z,read_4

 780 ;

 790 read_3 rr   e

 800        dec  a

 810        jr   nz,read_3

 820 ;

 830 ;rotate L so bit needed

 840 ;is in bit 0

 850 ;

 860 read_4 rr   e

 870        rl   c

 880 ;

 890 ;rotate the bit into C

 900 ;

 910        djnz read_1

 920        ret

 930 ;

 940 ;exit B=0

 950 ;     C=keys

 960 ;

 970 ;A,HL,DE corrupt

 980 ;

 990 ;

1000 ;port addresses of

1010 ;the keyboard rows

1020 ;

1030 keyadd defw 63486,64510,65022,65278,61438,57342,49150,32766

1040        defw 31 ;*Kempston*

1050 ;

1060 ;

1070 ;KEYTAB holds the position

1080 ;of each key as

1090 ;1/2 row number*8 plus

1100 ;distance from the edge

1110 ;eg. P = 5*8+0

1120 ;    O = 5*8+1

1130 ;    Q = 1*8+0

1140 ;    4 = 0*8+3

1150 ;

1160 keytab defb 5*8+1,5*8+0,2*8+1,3*8+2,7*8+0

1170 ;

1180 ;set for O,P,S,X,space

1190 ;

1200 ;for a Kempston joystick

1210 ;substitute

1220 ;

1230        defb 8*8+1,8*8+0,8*8+3,8*8+2,8*8+4

1240 ;

