1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-11-25 06:31:25 +00:00
C02/include/stackx.a02
2018-09-23 18:39:27 -04:00

81 lines
3.2 KiB
Plaintext

;C02 stack module assembly language subroutines
;Requires external zero page variables
;STKLO, STKHI, DSTLO, DSTHI, SRCLO, SRCHI
;external variables
;STKSLO, STKSHI, STKELO, STKEHI, TEMP0, TEMP1, TEMP2, TEMP3
;external routines
;MEMCPL, STKADD, STKDRN, STKPSA, STKRSP, STKSRC, STKSSP, STRLEN
;stkstr(n, &m) - Push String onto Stack
;Args: Y,X = Address of Source Array
STKSTR: JSR STRLEN ;Get Length of String
BEQ STKSTY ;If Length > 0
BMI STKSTY ; and < 128
INY ; Increment Length to
TYA ; include null terminator
STKSTY: LDY #0 ;Clear Y Index
JMP STKPSA ;Push Bytes onto Stack
;stktop(&m) - Copy Top Entry off Stack
;Args: Y,X = Address of Source Array
;Uses: STKSLO, STKSHI = Stack Start Address
;Affects: C,N,Z
;Returns: A,Y = Number of Bytes in Entry
; 0 = Error: Stack Underflow
STKTOP: JSR STKPOP ;Pop Top Entry Off Stack
BEQ STKSWR ;If Underflow, Return 0
JMP STKRSP ;Else Restore Stack Pointer and Return
;stkdup(&m) - Duplicate Top Entry on Stack
;Uses: STKSLO, STKSHI = Stack Start Address
; STKELO, STKEHI = Stack End Address
;Updates: STKLO, STKHI = Stack Pointer
;Affects: C,N,Z
;Returns: A,Y = Number of Bytes in Entry
; 0 = Error: Stack Overflow or Underflow
STKDUP: JSR STKDRP ;Deallocate Top Entry
STKDUS: JSR STKSRC ;Set Source Pointer to Stack Pointer
JSR STKRSP ;Restore Stack Pointer
JMP STKSTY ;Push Top Entry onto Stack
;stkovr(&m) - Duplicate Second Entry on Stack
;Uses: STKSLO, STKSHI = Stack Start Address
; STKELO, STKEHI = Stack End Address
;Updates: STKLO, STKHI = Stack Pointer
;Affects: C,N,Z
;Returns: A,Y = Number of Bytes in Entry
; 0 = Error: Stack Overflow or Underflow
STKOVR: JSR STKDRP ;Deallocate Top Entry
JSR STKDRN ;Deallocate Second Entry
JMP STKDUS ;Restore Pointer and Duplicate
;stkswp() - Swap Top Entry with Entry Below it
;Uses: STKSLO, STKSHI = Stack Start Address
; STKELO, STKEHI = Stack End Address
;Destroys: DSTLO,TEMP0,TEMP0,TEMP1,TEMP2,TEMP3
;Affects: C,N,Z
;Returns: A,Y = Number of Bytes in Entry
; 0 = Error: Stack Overflow or Underflow
STKSWP: JSR STKOVR ;Duplicate Second Entry
BEQ STKSWR ;Exit if Error
JSR STKDRN ;Deallocate Duplicate
STA TEMP3 ;Save Size of Duplicate
JSR STKSSP ;Save Pointer to Duplicate
JSR STKDRN ;Deallocate Top Entry
JSR STKSRC ;Set Source to Top Entry
STA DSTLO ;Save Top Entry Size
JSR STKDRN ;Deallocate Second Entry
LDA DSTLO ;Retrieve Top Entry Size
JSR STKSWC ;Copy Top Entry Down
JSR RESSRC ;Set Duplicate as Source
LDA TEMP3 ;Get Duplicate Length
STKSWC: STA TEMP0 ;Save Number of Bytes
JSR STKDST ;Set Destination to Stack Pointer
LDY #0 ;Clear Index Register
JSR MEMCPL ;Copy Bytes and Return
STA (DSTLO),Y ;Store Number of Bytes after Entry
SEC ;Add Number of Bytes plus 1
JSR STKADC ;to Stack Pointer
TYA ;Return Number of Bytes
STKSWR: RTS