;c02 library stddef.h02 assembly language subroutines
;Requires External Zero Page Variables DSTPTR, SRCPTR
;and External Variables TEMP0, TEMP1, TEMP2 (system header)

        SUBROUTINE STDDEF

;Constant Definitions
STRSIZ  EQU 128     ;Maximum String Size  
TRUE    EQU $FF     ;Returned for Success or Failure
FALSE   EQU $00     ;by some Library Routines

;savdst() - Save Destination Pointer
SAVDST: JSR GETDST  ;Load Destination Pointer
        BVC SAVRXY  ;Save X & Y Registers

;savsrc() - Save Source Pointer
SAVSRC: JSR GETSRC  ;Load Destination Pointer
        BVC SAVRXY  ;Save X & Y Registers

;Save Registers
SAVREG: STA TEMP0   ;Save Accumulater
SAVRXY: STX TEMP1   ;Save X Index
        STY TEMP2   ;Save Y Index
        RTS

;Restore Registers
RESREG: LDA TEMP0     ;Load Accumulator
RESRXY: LDX TEMP1     ;Load X Index
        LDY TEMP2     ;Load Y Index
        RTS

;Set Destination Pointer to System Buffer
SETDSB: JSR GETBFR    ;Get Buffer Address
        BVC SETDST

;Set Destination Pointer to Source Pointer
SETDSS: JSR GETSRC    ;Get Destination Pointer
        BVC SETDST    ;Store in Source Pointer

;Restore Destination Pointer
RESDST: JSR RESRXY    ;Load Address and Drop into SETDST

;Initialize Destination Pointer
SETDST: STX DSTPTR    ;Store Destination Pointer
        STY DSTPTR+1
        RTS

;Restore Source Pointer
RESSRC: JSR RESRXY    ;Load Saved Address
        BVC SETSRC    ;Set Source Pointer

;Set Source Pointer to System Buffer
SETSRB: JSR GETBFR    ;Get Buffer Address
        BVC SETSRC

;Set Source Pointer to Destination Pointer
SETSRD: JSR GETDST    ;Get Destination Point and fall into SETSRC

;Initialize Source Pointer and Index
SETSRC: STX SRCPTR    ;Store Source Pointer
        STY SRCPTR+1
        LDY #$00      ;Initialize Index Into String
        RTS

;Set List Pointer
SETLST: STX LSTPTR
        STY LSTPTR+1
        RTS

;Retrieve System Buffer Address
GETBFR: LDX #<SYSBFR
        LDY #>SYSBFR
        CLV           ;Clear Overflow Flag for BVC
        RTS

;Retrieve Source String Pointer
GETDST: LDX DSTPTR
        LDY DSTPTR+1
        CLV           ;Clear Overflow Flag for BVC
        RTS

;Retrieve Source String Pointer
GETSRC: LDX SRCPTR
        LDY SRCPTR+1
        CLV           ;Clear Overflow Flag for BVC
        RTS

;Retrieve String List Pointer
GETLST: LDX LSTPTR
        LDY LSTPTR+1
        RTS

;Add Byte A to Word YX
ADDAYX: JSR SAVRXY;   ;Store X,Y in TEMP1, TEMP2
        TAX           ;Set Y,X to 0,A
        LDY #0        ;and Execute ADDTXY
        
;Add TEMP1,TEMP2 to X,Y
ADDTXY: TXA         
        CLC
        ADC TEMP1   
        TAX
        TYA
        ADC TEMP2
        TAY
        RTS

;Add Byte A to Word YX
SUBAYX: STA TEMP1     ;Set LSB to A
        LDA #0        ;    MSB to 0
        STA TEMP2     ;and Execute SUBTXY
          
;Subtract TEMP1,TEMP2 from X,Y
SUBTXY: TXA         
        SEC
        SBC TEMP1   
        TAX
        TYA
        SBC TEMP2
        TAY
        RTS


;Subtract X,Y from TEMP1, TEMP2
;NOT CURRENTLY USED ANYWHERE
;SUBXYT: LDA TEMP1       ;Get TEMP1
;        STX TEMP1       ;
;        SEC             ;
;        SBC TEMP1       ;Subtract X 
;        TAX             ;and Store in X
;        LDA TEMP2       ;Get TEMP2
;        STY TEMP2
;        SBC TEMP2       ;Subtract Y
;        TAY             ;and Store in Y
;        RTS

;Decrement X,Y Register Pair
DECRXY: CPY #0
        BNE .SKIP
        DEY
.SKIP   DEX
        RTS

;Increment X,Y Register Pair
INCRXY: INX
        BNE .RETURN
        INY
.RETURN RTS

;Set Source Pointer to Buffer Location and Add Accumulator
SETDBA: LDA SYSBFP      ;Get Buffer Position
        JSR SETDSB      ;Set Destination to Buffer Address

;Add Accumulator to Destination Address
ADDDSA: TAX             ;Move Accumulator to Argument LSB
        LDY #0          ;Clear Argument MSB

;Add to Destination Address
;Args: Y,X = MSB,LSB of Integer to Add
;Affects: A,Y,X
ADDDST: LDA #DSTPTR     ;Set Index to Destination Pointer
        BNE ADDZPW      ;and Execute ADDZPW

;Set Source Pointer to System Buffer Position 
SETSBP: LDA SYSBFP    ;Load Position in Buffer
        BMI .RETURN   ;If Greater than 127, Return
        JSR SETSRB    ;Set Source Pointer to Buffer Address

;Add Accumulator to Zero Page Word
;Args: A = Bytre to Add
;      X = Address of Zero Page Word
ADDZPA: LDY #0        ;Set MSB to 0
        BEQ .ADDZPX   ;and add Accumulator

;Add Accumulator to Source Address
ADDSRA: TAX             ;Move Accumulator to Argument LSB
        LDY #0          ;Clear Argument MSB

;Add to Source Address
;Args: Y,X = MSB,LSB of Integer to Add
;Affects: A,Y,X
ADDSRC: LDA #SRCPTR       ;Set Index and Drop into ADDZPW

;Add to Zero Page Word
;Args: A = Address of Zero Page Word
;      Y,X = MSB,LSB of Integer to Add
;Affects: A,X,C,N,Z
ADDZPW: CLC             ;Clear Carry
ADCZPW: STA TEMP3       ;Save Zero Page Address
        TXA             ;Move Argument LSB to Accumulator
        LDX TEMP3       ;Set Index to Zero Page Address
.ADDZPX ADC 0,X         ;Add Argument LSB to Target LSB
        STA 0,X         ;and Save Result
        TYA             ;Move Argument MSB to Accumulator
        ADC 1,X         ;Add Argument MSB to Target MSB
        STA 1,X         ;and Save Result
        RTS

        ENDSUBROUTINE
