
        NAM     NUMREG
        
*       Version 1.0     (H-19-terminal version)
*       hjoel 1987-12-18


FLEX    EQU     $CD03


ESC     EQU     $1B
SPACE   EQU     $20
ASCII   EQU     $30
NUMLEN  EQU     11
EONUM   EQU     $0F
EXHIB   EQU     $80
KEEP    EQU     $C0

        ORG     $0100
        
USTACK  RMB     $20
USP     EQU     *
MEMS    RMB     110

PROMPT  FCC     $0A,$0D,ESC,/p * Hello Services, Inc. * /
        FCC     ESC,/q  /,STRSTP
EXHTXT  FCC     /Memory #/,STRSTP
STOTXT  FCC     /Saving in memory #/,STRSTP





        NAM WRITCHAR
        
***     Writes one character to terminal.
*
*       Version 1.0
*       hjoel 1987-12-04
*
*       Entry:  ASCII-code of character-to-be-
*                 typed in AccA.
*       AccB changed during operation.
*       All other registers unchanged.
***

ACIACR  EQU     $E004           Control-reg-addr of ACIA.
ACIADR  EQU     $E005           Data-reg-addr of ACIA.

        ORG     $BE10
WRITCH  LDB     ACIACR          ACIA ready to receive?
        RORB
        RORB
        BCC     WRITCH
        STA     ACIADR          Char. from AccA to ACIA.
        RTS
        
      

        NAM     WRITESTR
        
***     Writes a string of characters to terminal.
*
*       Version 1.0
*       hjoel 1987-12-10
*
*       Entry:  IX contains beginaddress of consecutive
*         memory-cells containing ASCII-codes of
*         characters to be written.
*       IX and AccA changed during operation.
*       All other registers unchanged.
***

PUTCHR  EQU     $CD18           S. char. system putout-
*                                 subroutine-addr.
STRSTP  EQU     $04             String-end mark.

        ORG     $BE30
GO      JSR     PUTCHR          Writes one char.
WRISTR  LDA     ,X+             ASCII of char into AccA.
        CMPA    #STRSTP         String-end?
        BNE     GO    
        RTS
        

        NAM WRITSTR2
        
*       Version 1.0
*       hjoel 1987-12-21
*
*       Entry:  IX contains adress of first value
*                 to be written.
*               AccB contains value of stopsign.
*       IX and AccA changed during operation.
*       All other registers unchanged.
***

        ORG     $BE50
EOSTR2  RMB     1

GETON   ADDA    #ASCII
        JSR     WRITCH
WRSTR2  STB     EOSTR2
        LDA     ,X+
        CMPA    EOSTR2          Stopsign?        
        BNE     GETON           If not, get one more.
        RTS
        
    
  

        
***     [Sub]routine that senses which key is
*       activated.
*       (This routine was developed for use on
*       Terminal Module 8415.
*       LSNibble = bit 0..3 of PIAA is connected 
*       to keypad columns.
*       MSNibble = bit 4..7 of PIAA is connected 
*       to keypad rows.)
*
*       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
        
      

***     [Sub]routine that makes a bounce-free
*       check that no keys are activated
*       by scanning keypad or keyboard #MANY
*       times in succession.
*
*       Version 1.0
*       hjoel 1987-12-21
*
*       Exit:   $FF in AccA.
***
                

ALLUP   EQU     $FF             Keypad-value when
*                               all keys are up.

        ORG     $A040
NOKEY   LDB     #MANY
SPINN   JSR     WCHKEY
        CMPA    #ALLUP
        BNE     NOKEY 
        DECB
        BNE     SPINN
        RTS        
    
  


***     [Sub]routine that does its best to
*       circumvent contact-bouncing problems
*       on keypad or keyboard thus:
*       Gets key-value, which is compared #MANY
*       times with successive key-scans.        
*       Utilizes subroutine WCHKEY.
*
*       Version 1.0
*       hjoel 1987-12-18
*
*       Exit:   Bounce-liberated coded key-value
*                 in AccA.
***


MANY    EQU     $A0             No. of key-scan loops.

        ORG     $A050
CHECK   RMB     1               Storage of key-value to test.

MAKSUR  JSR     WCHKEY
        STA     CHECK
        LDB     #MANY
TWIRL   JSR     WCHKEY
        CMPA    CHECK
        BNE     MAKSUR
        DECB
        BNE     TWIRL
        RTS        



***     [Sub]routine that checks incoming key-value
*       against key-code-table and delivers
*       corresponding definition-value.
*
*       Version 1.0
*       hjoel 1987-12-20
*
*       Entry:  Coded key-value in AccA.
*       Exit:   Decoded key-value in AccA.
*       IX changed during operation.
*       All other registers unchanged.
***

        ORG     $A070
        
KEYCOD  FCB     $77,$7B,$7D,$7E         Table of
        FCB     $B7,$BB,$BD,$BE         coded key-
        FCB     $D7,$DB,$DD,$DE         pad key-
        FCB     $E7,$EB,$ED,$EE         values.
        
KEYDEF  FCB     $07,$08,$09,$80         Table of
        FCB     $04,$05,$06,$0F         defined
        FCB     $01,$02,$03,$0F         key-values.
        FCB     $0F,$00,$D0,$C0
        
DECODE  LDX     #KEYCOD
WUNMOR  CMPA    ,X+             Which key was pressed?
        BEQ     FOUND
        CMPX    #KEYDEF         Keycode-table covered?
        BNE     WUNMOR          If not, 1 more round.
        LEAS    2,S             Adjust system stack
        JMP     GETKEY          before scanning keys again.
FOUND   LDA     15,X            Decoded key-value.
        RTS



*       Exit:   Decoded key-value in AccA.


GETKEY  JSR     NOKEY
        JSR     MAKSUR
        JSR     DECODE
        RTS



        


        NAM     INBOUNDS
        
*       Entry:  Value-to-be-checked in AccA.
*               Possible branch-addr in IY.
*       Exit:   Unchanged entry-value in AccA.


LO      EQU     0
HI      EQU     9
PLING   EQU     $07
BACKA   EQU     $08

        ORG     $A0B0
        
BOUNDS  CMPA    #LO
        BLO     OUT
        CMPA    #HI
        BHI     OUT
        RTS
        
OUT     LDA     #BACKA          Backspace.
        JSR     WRITCH
        LDA     #PLING
        JSR     WRITCH
        LEAS    2,S
        TFR     Y,PC
        




        NAM     INITIX
        
*       Entry:  IX contains a point-of-orien-
*                 tation-address.
*               AccA contains one of the ad-
*                 justment operands (NUMLEN
*                 below is the other.)
*       Exit:   IX initiated and adjusted to
*                 point to required location.


        ORG     $A0D0
INITIX  PSHS    A
        LDB     #NUMLEN
        MUL
        LEAX    D,X
        PULS    A
        RTS
        



        NAM     INITSTRG
        
*       Version 1.0
*       hjoel 1987-12-21
*
*       Entry:  IX points to beginning of
*                 stringlocation.
*               AccB contains no. of bytes
*                 to be initialized.
*               AccA contains the value 
*                 that the entire string
*                 is to be initialized to.
*       AccB and Ix are changed during operation.
*       All other registers unchanged.
***

        ORG     $A0E0
INISTR  STA     ,X+             Write to IX-pointed RAM-byte.
        DECB
        BNE     INISTR          End of string?
        RTS
        


        NAM     STORESTR
        
*       Version 2.0
*       hjoel 1987-12-30
*
*       Entry:  IX points to beginning of
*                 string location.
*               AccB contains value of stopsign.
*       Acca, AccB & IX changed during operation.
***

        ORG     $A100


ASCHAR  EQU     $20

EON     RMB     1

STOSTR  STB     EON   
NEWNUM  JSR     GETKEY  
        CMPA    EON             Stopsign?
        BEQ     BEGIN           If so, get out.
        LDY     #NEWNUM
        JSR     BOUNDS
        STA     ,X+             Store sign value.
        CMPA    #ASCHAR         ASCII character?
        BMI     WRITE
        ADDA    #ASCII
WRITE   JSR     WRITCH        
        BRA     NEWNUM
        
BEGIN   RTS




        ORG     $0300
        
NUMREG  LDU     #USP
        LDX     #PROMPT
        JSR     WRISTR          Splash out the prompt.
        
        JSR     GETKEY
        CMPA    #EXHIB          Display requested?
        BNE     STORE           If not, go for storage.
        
SHOW    LDX     #EXHTXT
        JSR     WRISTR          Display exhibit-mode-text.        
******  Next key-value must be a digit.        
MEMNO1  JSR     GETKEY
        PSHU    A
        LDY     #MEMNO1
        JSR     BOUNDS
        ADDA    #ASCII
        JSR     WRITCH
        LDA     #SPACE
        JSR     WRITCH
        PULU    A
        
        LDX     #MEMS      
        JSR     INITIX
        LDB     #EONUM
        JSR     WRSTR2
        BRA     NUMREG
        
STORE   CMPA    #KEEP
        BNE     UT_OS 
        LDX     #STOTXT
        JSR     WRISTR
        
MEMNO2  JSR     GETKEY
        PSHU    A
        LDY     #MEMNO2
        JSR     BOUNDS
        ADDA    #ASCII
        JSR     WRITCH
        PULU    A
        
        LDX     #MEMS
        JSR     INITIX
        PSHU    X
        LDB     #NUMLEN
        LDA     #EONUM
        JSR     INISTR
        
        PULU    X
        LDB     #EONUM
        JSR     STOSTR
        
        BRA     NUMREG
        
UT_OS   CMPA    #$D0
        BNE     NUMREG
        JMP     FLEX
        END     NUMREG
        
