
        NAM     WHICHKEY
        
***     [Sub]routine that senses which key is
*       activated.
*       (This routine was developed for use on
*       Terminal Module 8415.
*       Bit 0..3 of PIAA, connected to keypad rows,
*       corresponding to LSNibble.
*       Bit 4..7 of PIAA, connected to keypad
*       columns, corresponding to MSNibble.)
*
*       Version 1.0
*       hjoel 1987-12-18
*
*       Exit:   Coded value of pressed
*               key i AccA.
*       Other registers unchanged.
***

GOORA   EQU     %00000100       Change CRA-flags -> ORA.
DDRORA  EQU     $E020           Adress of PIA DDRA & ORA.
CRA     EQU     $E021           Adress of PIA CRA.
MASKHI  EQU     %11110000       Mask of hi nibble.
MASKLO  EQU     %00001111       DDR-comb. + mask lo nib.

        ORG     $A000
MSNIB   RMB     1

******* Sets bit 0..3 of PIA as outport, & bit 4..7
*       as inport. Writes zeroes to all columns of
*       keypad. Reads rows (=MSN) of keypad. 
*       Stores value of MostSignificantNibble.

WCHKEY  PSHS    B               Save what's in AccB.
        CLRA
        STA     CRA             Choose DataDirect.Reg.
        LDB     #MASKLO            
        STB     DDRORA          Bit0..3 out, bit4..7 in.
        LDA     #GOORA
        STA     CRA             Choose Output Reg.
        CLRA    
        STA     DDRORA          Transmit to bit0..3.
        LDA     DDRORA          Receive from bit4..7.
        ANDA    #MASKHI         Mask MSN - just to make sure.
        STA     MSNIB           Save.
        
******* Sets bit 0..3 of PIAA as inport, & bit 4..7
*       as outport. Writes zeroes to all rows of 
*       keypad. Reads columns (=LSN) of keypad.
*       Combines MSN & LSN to give coded key-value.

        CLRA    
        STA     CRA             Choose DDRA.
        COMB
        STB     DDRORA          Bit0..3 in, bit4..7 out.
        LDA     #GOORA
        STA     CRA             Choose ORA.
        CLRA
        STA     DDRORA          Write to 4..7.
        LDA     DDRORA          Read from 0..3.
        ANDA    #MASKLO         Mask LSNibble.
        ADDA    MSNIB           Combine MSN & LSN.
        PULS    B               Restore AccB.
        RTS
        END     WCHKEY
