P********************************
*                              *
*         MACRO LIBRARY        *
*                              *
*    with 2 program examples   *
*                              *
*           10/26/85           *
*                              *
*       Use it as a guide      *
*                              *
********************************

* After stripping out the examples and other junk
* you can paste this in your program, or use it
* with the USE opcode.

GETCFG    = $FF6B         ;Get config for bank # in X
PRIMM     = $FF7D         ;Print immediate utility
CHRIN     = $FFCF
CHROUT    = $FFD2
PLOT      = $FFF0

BANK      MAC
          LDA #0          ;Rom up
          STA $FF00
          LDX #]1
          JSR GETCFG
          STA $FF00
          <<<

INCD      MAC
          INC ]1
          BNE NC
          INC ]1+1
NC        <<<

DECD      MAC
          LDA ]1
          BNE ND
          DEC ]1+1
ND        DEC ]1
          <<<

MOV       MAC
          LDA ]1
          STA ]2
          <<<

MOVD      MAC
          MOV ]1          ;]2
          MOV ]1+1        ;]2+1
          <<<

STIND     MAC
          MOV ]1          ;(]2),Y
          INY
          MOV ]1+1        ;(]2),Y
          <<<

LDIND     MAC
          MOV (]1),Y      ;]2
          INY
          MOV (]1),Y      ;]2+1
          <<<

ADD       MAC
          CLC
          LDA ]1
          ADC ]2
          STA ]3
          LDA ]1+1
          ADC ]2+1
          STA ]3+1
          <<<

SUB       MAC
          SEC
          LDA ]1
          SBC ]2
          STA ]3
          LDA ]1+1
          SBC ]2+1
          STA ]3+1
          <<<

ADDX      MAC
          TXA
          CLC
          ADC ]1
          STA ]2
          LDA ]1+1
          ADC #0
          STA ]2+1
          <<<

ADDY      MAC
          TYA
          CLC
          ADC ]1
          STA ]2
          LDA ]1+1
          ADC #0
          STA ]2+1
          <<<

ADDNUM    MAC
          LDA #]1
          CLC
          ADC ]2
          STA ]2
          BCC NI
          INC ]2+1
NI        <<<

SWAP      MAC
          LDA ]1
          PHA
          LDA ]2
          STA ]1
          PLA
          STA ]2
          <<<

CMPD      MAC
          LDA ]1
          CMP ]2
          LDA ]1+1
          SBC ]2+1
          <<<

CMPADRS   MAC
          LDA ]1
          CMP #<]2
          LDA ]1+1
          SBC #>]2
          <<<

POKE      MAC
          MOV #]2         ;]1
          <<<

SETPNT    MAC
          POKE ]1         ;]2
          POKE ]1+1       ;>]2
          <<<

CMPSTR    MAC
LP        LDA (]2),Y
          AND #%01111111
          CMP ]1,Y
          BNE OUT
          DEY
          BPL LP
          INY
OUT       <<<

PRCHR     MAC
          LDA #]1
          JSR CHROUT
          <<<

PRADRS    MAC
          LDX ]1
          LDA ]1+1
          JSR PRNTAX
          <<<

GOTOXY    MAC
          LDY #]1
          LDX #]2
          CLC
          JSR PLOT
          <<<

PRINT     MAC
          JSR PRIMM
          IF '=]1
          TXT ]1
          ELSE
          HEX ]1
          TXT ]2
          FIN
          BRK
          <<<


********** Examples ************

          EXP OFF         ;Do not print macro expansion
          TR              ;Limit to 3 bytes printed/line

SAVOBJ    KBD
EXAMPLE   KBD

********************************
*
*  Example 1
*
* (Find input ASCII string in
* memory from $1C00 to $EFFF.)
*
* To use: LOAD "SEARCH.O",8,1
* Type NEW if in direct mode,
* then  SYS 20*256.
*
* Put EXAMPLE = 1 to assemble
*
********************************

          DO EXAMPLE-1
          ELSE

PL        = $8B
QL        = $8D
LEN       = $8F
IN        = $200
START     = $1C00
END       = $F000
IMAIN     = $302

          ORG $1400

          PRINT 93        ;'text to locate:'0D
          SETPNT PL       ;START
INPUT     LDX #0
:INP      JSR CHRIN
          AND #%01111111
          STA IN,X
          CMP #$D
          BEQ :INGOT
          INX
          CPX #$59
          BLT :INP
          BGE INPUT
:INGOT    DEX
          BPL :ISINP
          JMP :EXIT       ;Leave if no input
:ISINP    STX LEN
          ADDX PL         ;QL
:NXCMP    BANK 0
          LDY LEN
          CMPSTR IN       ;PL
          BNE :NEXT
          BANK $F         ;Bank in rom to call kernel
          PRCHR $D        ;Found, print location
          PRCHR '$'
          PRADRS PL
:NEXT     INCD PL
          INCD QL
          CMPADRS QL      ;END ;Finished?
          BLT :NXCMP
          BANK $F
          PRINT 0D        ;'sys 20*256 for more'0D
:EXIT     JMP (IMAIN)

* Subroutine to print A,X in hex:

PRNTAX    JSR PRBYTE      ;Print A in hex
          TXA             ;Get X
PRBYTE    PHA             ;Subroutine to print A in hex
          LUP 4
          LSR
          --^
          JSR PRNIB       ;Print high nibble
          PLA             ;Get low nibble
          AND #$F
PRNIB     ORA #'0'
          CMP #'9'+1
          BLT :PRIT
          ADC #'a'-'9'-2
:PRIT     JMP CHROUT

          ERR ^$1C00
          DO SAVOBJ
          SAVE "@0:SEARCH"
          FIN             ;Of SAVOBJ conditional
          FIN             ;Of EXAMPLE conditional
          PAG
********************************
*
*  Example 2
*
* (Revive a dead BASIC prog.)
*
* To use: LOAD "REVIVE.O",8,1
* set BANK 15 and then SYS 20*256.
* The dead program, if any,
* will be back.
*
* Put EXAMPLE = 2 to assemble
*
********************************

          DO EXAMPLE-2
          ELSE

PL        = $8B
QL        = $8D
TXTTAB    = $2D
LINKSET   = $AF87
BASWARM   = $4003

          ORG $1400

          BANK 0
          MOVD TXTTAB     ;PL
          ADDNUM 4        ;PL
          LDY #0
FNDEND    LDA (PL),Y      ;Find end of first line
          PHP
          INCD PL
          PLP
          BNE FNDEND
          STIND PL        ;TXTTAB ;First link
:LOOP     DEY             ;Now Y=0
          LDIND PL        ;QL ;Put next link in QL
          LDA QL          ;Is link
          ORA QL+1        ;= 0 ?
          BEQ :FIXPNT
          CMPD QL         ;PL ;Validity check
          BLT :BADPROG
          MOVD QL         ;PL ;Set up for loop
          JMP :LOOP

:FIXPNT   BANK $F
          JMP LINKSET     ;Let BASIC finish it up

:BADPROG  BANK $F
          JMP BASWARM

          ERR ^$1C00
          DO SAVOBJ
          SAVE "@0:REVIVE"
          FIN             ;Of SAVOBJ conditional
          FIN             ;Of EXAMPLE conditional
