ZX81 Machine Code demo: how to read the keyboard

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
Post Reply
David G
Posts: 387
Joined: Thu Jul 17, 2014 7:58 am
Location: 21 North, 156 West

ZX81 Machine Code demo: how to read the keyboard

Post by David G »

It is easy to read the keyboard -- and specifically to look for specific key presses -- on the ZX81

Code: Select all

     SCANKEY:LD      A,(4025H);LAST_K
             INC     A
             JR      NZ,SCANKEY
     GOTKEY: LD      BC,(LAST_K)
             LD      A,C
             INC     A
             JR      Z,GOTKEY
             CALL    DECODE            //call the ZX81 ROM routine
             JR      NC,GOTKEY
             LD      A,(HL)            //retrieve the keycode
             RET
Just call this and when it RETurns the A register will have the keycode. For example, if the user presses 'Y' then A will have the code for Y which is 62d (3Eh) per the ZX81 Basic manual

The ROM call 'DECODE' makes it easy to find out which specific key was pressed

The above code has a potential problem. It only works in SLOW mode -- if you call it in FAST mode it will hang the machine as the keys cannot be read in FAST mode, and the only way to exit the code is to press a key


Voila. This version will test the SLOW/FAST mode and go into SLOW mode if necessary and then go back to FAST mode before it RETurns

Code: Select all

             //ROM SUBROUTINES
             DECODE    EQU $07BD
             ZX81ROM_FAST  EQU $0F23
             //
   GETKEY:   //this version works in SLOW mode or FAST mode
             LD      A,(CDFLAG)        //test for SLOW mode
             BIT     7,A
             PUSH    AF                //save the result so we can restore it later
             CALL    Z,SLOW_           //if needed, call SLOW
     SCANKEY:LD      A,(4025H);LAST_K
             INC     A
             JR      NZ,SCANKEY
     GOTKEY: LD      BC,(LAST_K)
             LD      A,C
             INC     A
             JR      Z,GOTKEY
             CALL    DECODE            //call the ZX81 ROM routine
             JR      NC,GOTKEY
             POP     AF                //restore test result
             LD      A,(HL)            //retrieve the keycode
             CALL    Z,ZX81ROM_FAST    //if we started in FAST mode, go back to FAST mode
             RET

   SLOW_:    SLOW_NEWROM  EQU $0F2B    //ROM routined 'SLOW' is at a different
             SLOW_OLDROM  EQU $0F28    //address for the two ZX81 ROM versions
             CALL    IS_OLDROM
             JP      Z,SLOW_NEWROM
             JP      SLOW_OLDROM
             // RET comes from the ROM calls

  IS_OLDROM: //THIS SUBROUTINE RET Z IF IMPROVED ROM
             LD A,(5942)
             CP $D9
             RET
So GETKEY is the more foolproof version. GETKEY won't hang if called from FAST mode, and it won't hang if called from the original (unimproved) ROM
David G
Posts: 387
Joined: Thu Jul 17, 2014 7:58 am
Location: 21 North, 156 West

Re: ZX81 Machine Code demo: how to read the keyboard

Post by David G »

here is a demo program. whatever key you press it will echo it back. This works with all the keys on the ZX81 keyboard, both regular and shifted
READKYBD_1K.p
'read the keyboard' demo app
(801 Bytes) Downloaded 166 times

And here is the BASIC file and the assembly for the machine code in FASMW-ZX format. Reading the keyboard is easy (subroutine GETKEY) but afterward displaying the name of every key is not so easy. It uses the ROM routine TOKEN-ADD to get the name of the token keys (e.g Shift-1 'EDIT') but there are some keys for which TOKEN-ADD just returns '?' So this quick and dirty code is a little convoluted
READKYBD_1K.ASM
Assembly/BASIC file
(7.94 KiB) Downloaded 181 times
User avatar
XavSnap
Posts: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: ZX81 Machine Code demo: how to read the keyboard

Post by XavSnap »

Xavier ...on the Facebook groupe : "Zx81 France"(fr)
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: ZX81 Machine Code demo: how to read the keyboard

Post by dr beep »

David G wrote: Thu Jan 28, 2021 10:03 am It is easy to read the keyboard -- and specifically to look for specific key presses -- on the ZX81

Code: Select all

     SCANKEY:LD      A,(4025H);LAST_K
             INC     A
             JR      NZ,SCANKEY
     GOTKEY: LD      BC,(LAST_K)
             LD      A,C
             INC     A
             JR      Z,GOTKEY
             CALL    DECODE            //call the ZX81 ROM routine
             JR      NC,GOTKEY
             LD      A,(HL)            //retrieve the keycode
             RET

If you want the most optimized version (which I use in my 1K game):

When you know on first entry that A is never 255 then you can skip the XOR A too

Code: Select all

     SCANKEY: XOR A	; not needed when A NEVER 255 on entry
W4UP     INC     A
     GOTKEY: LD      BC,(LAST_K)
             LD      A,C
             JR      NZ,W4UP
             INC     A
             JR      Z,GOTKEY
             CALL    #7BD   ; DECODE            //call the ZX81 ROM routine
             LD      A,(HL)            //retrieve the keycode
             RET
David G
Posts: 387
Joined: Thu Jul 17, 2014 7:58 am
Location: 21 North, 156 West

Re: ZX81 Machine Code demo: how to read the keyboard

Post by David G »

Interesting. I was calling LD (LAST_K) twice whereas your version calls it first, before looping, hence saving two or three bytes

Yesterday I was trying to remember where that code came from. I had a vague memory of the "Explorer's Guide" but perusing that book shows no such code
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: ZX81 Machine Code demo: how to read the keyboard

Post by dr beep »

I have optimized more routines.
A routine for checking hiscore was optimized over time.
The keyboardroutine was even further developped adding not only keycontrol in a game but also a routine to check joystickinput.
David G
Posts: 387
Joined: Thu Jul 17, 2014 7:58 am
Location: 21 North, 156 West

Re: ZX81 Machine Code demo: how to read the keyboard

Post by David G »

Optimizing the number of bytes used is an art?


XavSnap wrote: Thu Jan 28, 2021 11:00 am Zx81 Keyboard.
Type-in "Ordi-5" (fr)
Thanks XavSnap, I was wondering if the keyboard hardware lines should be mentioned. That program "Changer la signification des touches de votre clavier" (Change the meaning of keys on your keyboard?) is quite an eye-opener, interactively showing the keyboard state

touches de votre clavier ( click "Telecharger" to download)

Reading the keyboard at a low level like that is quite useful in come scenarios. In games it allows for more freedom, for example in my first game a "fire button" was specified as "F" but actually any key in the same column worked (V, F, R, 4). Additionally it allows detecting simultaneous keypresses which let Right/Left and Fire to be pressed at the same time

The DECODE routine is fine when you need to identify a single key. But is overkill for many games

game code

Code: Select all

READKEY: IN    A,(0FEH)      ;SCAN KEYBOARD	
         BIT   3,A           ;F COLUMN
         CALL  Z,FIRE
         BIT   0,A           ;P COLUMN
         JR    Z,RIGHT
         BIT   1,A           ;O COLUMN
         JR    Z,LEFT 
         BIT   4,A           ;B COLUMN
         RET   Z             ;EXIT APP
Game: VIPER
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: ZX81 Machine Code demo: how to read the keyboard

Post by dr beep »

I have written a routine to redefine controls in a game and use the IN-controls to check them.
Advantage, you can use diagonal movement or fire and move in 1 turn.
User avatar
mrtinb
Posts: 1906
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Re: ZX81 Machine Code demo: how to read the keyboard

Post by mrtinb »

I simply read LAST_K and compare the result to the 16-bit value in appendix 4 of Understanding Your ZX81 ROM by Dr. Ian Logan. I never call Decode. To compare two 16-bit values I use the SBC opcode.
Martin
https://zx.rtin.be
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube
Post Reply