*
*``````````````````````````````*
* REQUIRED.LIB                 *
*-                            -*
* GLOBAL ROUTINES AND          *
* VARIABLES EITHER USED BY THE *
* LIBRARY OR PROVIDED FOR DEMO *
* USE OR DEBUGGING PURPOSES.   *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
********************************
*                              *
* GLOBAL ROUTINES              *
*                              *
********************************
*
*``````````````````````````````*
* __GETRET:         GET RETURN *
*-                            -*
* COPIES THE DATA IN [RETURN]  *
* TO THE SPECIFIED LOCATION.   *
* LENGTH IS DETERMINED BY      *
* VALUE OF RETLEN.             *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
__GETRET
*
         PLA
         STA   :RET
         PLA
         STA   :RET+1
         PLA
         STA   ADDR1
         PLA
         STA   ADDR1+1
         LDY   #255
*
:LP
         INY
         LDA   RETURN,Y
         STA   (ADDR1),Y
         CPY   RETLEN
         BNE   :LP
         LDA   :RET+1
         PHA
         LDA   :RET
         PHA
         RTS
:RET     DS    2
*
*``````````````````````````````*
* __SETPARM:     SET PARAMETER *
*-                            -*
* COPIES DATA FROM SPECIFIED   *
* ADDRESS TO THE [PARAM]       *
* LOCATION FOR PASSING TO      *
* A ROUTINE.                   *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
__SETPARM
*
         LDY   #255
         PLA
         STA   :RET
         PLA
         STA   :RET+1
         PLA
         STA   ADDR1
         PLA
         STA   ADDR1+1
*
:LP
         INY
         LDA   (ADDR1),Y
         STA   PARAM,Y
         CPY   PARLEN
         BNE   :LP
         LDA   :RET+1
         PHA
         LDA   :RET
         PHA
         RTS
:RET     DS    2
*
*``````````````````````````````*
* __DUMP:            DUMP DATA *
*-                            -*
* OUTPUTS DATA LOCATED AT THE  *
* SPECIFIED ADDRESS IN HEX     *
* FORMAT FOR SPECIFIED NUMBER  *
* OF BYTES.                    *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
__DUMP
*
         PLA
         STA   :RET
         PLA
         STA   :RET+1
         PLA
         STA   :LENGTH
         PLA
         STA   ADDR3
         PLA
         STA   ADDR3+1
*
         LDA   #$8D
         JSR   $FDF0
         LDA   ADDR3+1
         AND   #$F0
         LSR
         LSR
         LSR
         LSR
         TAX
         LDA   HEXTAB,X
         JSR   $FDF0
         LDA   ADDR3+1
         AND   #$0F
         TAX
         LDA   HEXTAB,X
         JSR   $FDF0
         LDA   ADDR3
         AND   #$F0
         LSR
         LSR
         LSR
         LSR
         TAX
         LDA   HEXTAB,X
         JSR   $FDF0
         LDA   ADDR3
         AND   #$0F
         TAX
         LDA   HEXTAB,X
         JSR   $FDF0
         LDA   #186       ; :
         JSR   $FDF0
         LDA   #160       ; SPC
         JSR   $FDF0
*
         LDY   #0
:LP
         LDA   (ADDR3),Y
         AND   #$F0             ; MASK RIGHT
         LSR
         LSR
         LSR
         LSR
         STA   :LEFT
         LDA   (ADDR3),Y
         AND   #$0F             ; MASK LEFT
         STA   :RIGHT
         LDX   :LEFT
         LDA   HEXTAB,X
         JSR   $FDF0
         LDX   :RIGHT
         LDA   HEXTAB,X
         JSR   $FDF0
         LDA   #160
         JSR   $FDF0
         INY
         CPY   :LENGTH
         BNE   :LP
*
*LDA #$8D
*JSR $FDF0
         LDA   :RET+1
         PHA
         LDA   :RET
         PHA
*
         RTS
*
:RET     DS    2
:RIGHT   DS    1
:LEFT    DS    1
:LENGTH  DS    1
*
*``````````````````````````````*
* __P:     PRINT FOLLOWING ASC *
*-                            -*
* THIS IS THE XPRINT ROUTINE   *
* FROM STDIO.LIB, BUT STRIPPED *
* OF COMMENTS. FOR DEBUG.      *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
__P
         PLA
         STA   ADDR1
         PLA
         STA   ADDR1+1
         LDY   #1
:LP      LDA   (ADDR1),Y
         BEQ   :DONE
         JSR   $FDF0
         INY
         BNE   :LP
:DONE    CLC
         TYA
         ADC   ADDR1
         STA   ADDR1
         LDA   ADDR1+1
         ADC   #0
         PHA
         LDA   ADDR1
         PHA
         RTS
*
*``````````````````````````````*
* __W:    WAIT FOR KEYPRESS    *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
__W
         JSR   $FD0C
         RTS
*
*``````````````````````````````*
* __RSAV:     REGISTRY SAVE    *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
__RSAV
         STA   ERRA
         STX   ERRX
         STY   ERRY
:C       BCC   :C0
         BCS   :C1
:Z       BEQ   :Z1
         BNE   :Z0
:N       BMI   :N1
         BPL   :N0
:O       BVC   :O0
         BVS   :O1
         JMP   :EXIT
:C0      LDA   #0
         STA   ERRCARRY
         JMP   :Z
:C1      LDA   #1
         STA   ERRCARRY
         JMP   :Z
:Z1      LDA   #1
         STA   ERRZERO
         JMP   :N
:Z0      LDA   #0
         STA   ERRZERO
         JMP   :N
:N1      LDA   #1
         STA   ERRNEG
         JMP   :O
:N0      LDA   #0
         STA   ERRNEG
         JMP   :O
:O0      LDA   #0
         STA   ERROVF
         JMP   :EXIT
:O1      LDA   #1
         STA   ERROVF
:EXIT
         RTS
*
*``````````````````````````````*
* __RDMP:     REGISTRY DUMP    *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
__RDMP
         _PRNT " ",8D8D
         _PRNT "REGISTRY DUMP",8D
         _PRNT "=============",8D
         _PRNT "A: "
         LDA   ERRA
         AND   #$F0             ; MASK RIGHT
         LSR
         LSR
         LSR
         LSR
         STA   :LEFT
         LDA   ERRA
         AND   #$0F             ; MASK LEFT
         STA   :RIGHT
         LDX   :LEFT
         LDA   HEXTAB,X
         JSR   $FDF0
         LDX   :RIGHT
         LDA   HEXTAB,X
         JSR   $FDF0
         _PRNT " ",8D
         _PRNT "X: "
         LDA   ERRX
         AND   #$F0             ; MASK RIGHT
         LSR
         LSR
         LSR
         LSR
         STA   :LEFT
         LDA   ERRX
         AND   #$0F             ; MASK LEFT
         STA   :RIGHT
         LDX   :LEFT
         LDA   HEXTAB,X
         JSR   $FDF0
         LDX   :RIGHT
         LDA   HEXTAB,X
         JSR   $FDF0
         _PRNT " ",8D
         _PRNT "Y: "
         LDA   ERRY
         AND   #$F0             ; MASK RIGHT
         LSR
         LSR
         LSR
         LSR
         STA   :LEFT
         LDA   ERRY
         AND   #$0F             ; MASK LEFT
         STA   :RIGHT
         LDX   :LEFT
         LDA   HEXTAB,X
         JSR   $FDF0
         LDX   :RIGHT
         LDA   HEXTAB,X
         JSR   $FDF0
         _PRNT " ",8D8D
         _PRNT "STATUS FLAGS",8D
         _PRNT "============",8D8D
         LDA   #0
         CMP   ERRCARRY
         BEQ   :CARCLR
         _PRNT "CARRY: SET",8D
         JMP   :TESTN
:CARCLR  _PRNT "CARRY: CLEAR",8D
:TESTN   LDA   #0
         CMP   ERRNEG
         BEQ   :NEGCLR
         _PRNT "NEGATIVE: SET",8D
         JMP   :TESTZ
:NEGCLR  _PRNT "NEGATIVE: CLEAR",8D
:TESTZ   LDA   #0
         CMP   ERRZERO
         BEQ   :ZCLR
         _PRNT "ZERO: SET",8D
         JMP   :TESTO
:ZCLR    _PRNT "ZERO: CLEAR",8D
:TESTO
         LDA   #0
         CMP   ERROVF
         BEQ   :OCLR
         _PRNT "OVERFLOW: SET",8D
         JMP   :FIN
:OCLR    _PRNT "OVERFLOW: CLEAR",8D8D8D
:FIN
         RTS
*
** DATA
*
:LEFT    DS    1
:RIGHT   DS    1
HEXTAB   ASC   "0123456789ABCDEF"
*
*``````````````````````````````*
* __ERR: ERROR HANDLING SUB    *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
__ERR
*
** SAVE RETADR OF LAST CALL
*
         LDY   #0
         LDA   (RETADR),Y
         STA   :RET
         INY
         LDA   (RETADR),Y
         STA   :RET+1
*
         JSR   __RSAV
         PLA
         STA   RETADR
         PLA
         STA   RETADR+1
         PLA
         STA   :DUMPLEN   ; LENGTH OF DUMP
         PLA
         STA   :DUMP      ; DUMP ADDRESS LO
         PLA
         STA   :DUMP+1    ; DUMP HI
         PLA
         STA   :DMPMSG
         PLA
         STA   :DMPMSG+1
         PLA
         STA   :MESG      ; ERROR MESSAGE ADDR LO
         PLA
         STA   :MESG+1    ; HI
         PLA
         STA   :SRID      ; SUBROUTINE ID ADDR LO
         PLA
         STA   :SRID+1    ; SUB ID HI
*
         _PRNT " ",87878787878D8D
         _PRNT "ERROR!",8D8D
         _PRNT "SUBROUTINE: "
         LDY   #0
         LDA   :SRID
         STA   ADDR1
         LDA   :SRID+1
         STA   ADDR1+1
:LP1
         LDA   (ADDR1),Y
         BEQ   :LPX1
         JSR   $FDF0
         INY
         BNE   :LP1
:LPX1
         _PRNT " ",8D
         _PRNT "MESSAGE: "
         LDY   #0
         LDA   :MESG
         STA   ADDR1
         LDA   :MESG+1
         STA   ADDR1+1
:LP2
         LDA   (ADDR1),Y
         BEQ   :LPX2
         JSR   $FDF0
         INY
         BNE   :LP2
:LPX2
         _PRNT " ",8D8D
         _WAIT
         LDY   #0
         LDA   :DMPMSG
         STA   ADDR1
         LDA   :DMPMSG+1
         STA   ADDR1+1
:LP3
         LDA   (ADDR1),Y
         BEQ   :LPX3
         JSR   $FDF0
         INY
         BNE   :LP3
:LPX3
         _PRNT " ",8D
         LDA   :DUMP+1
         PHA
         LDA   :DUMP
         PHA
         LDA   :DUMPLEN
         PHA
         JSR   __DUMP
         _WAIT
         LDA   RETADR+1
         PHA
         LDA   RETADR
         PHA
         _PRNT " ",8D8D
         _WAIT
         _RDUMP
         _WAIT
         LDA   ERRSTOP
         CMP   #1
         BEQ   :KILL
         RTS
:KILL
         _PRNT " ",8D8D
         _PRNT "BREAK AT: "
         DUMP  #:RET;#2
*
         JMP   $3D0
*
** DATA
*
:DUMPLEN DS    1
:DUMP    DS    2
:DMPMSG  DS    2
:MESG    DS    2
:SRID    DS    2
:RET     DS    2
*
*``````````````````````````````*
* __ERRH : HANDLE ERROR @ ADDR *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
__ERRH
         PLA
         STA   :RET
         PLA
         STA   :RET+1
         PLA
         STA   ADDR1
         PLA
         STA   ADDR1+1
*
         LDA   #$FFC
         STA   $D8
*
         LDY   #0
         LDA   (ADDR1),Y
         STA   $9D5A
         INY
         LDA   (ADDR1),Y
         STA   $9D5B
*
         LDA   :RET+1
         PHA
         LDA   :RET
         PHA
*
         RTS
:RET     DS    2
*
