mirror of
https://github.com/RevCurtisP/C02.git
synced 2026-04-20 10:16:47 +00:00
Finished stack module in standard library
This commit is contained in:
+102
-86
@@ -1,10 +1,8 @@
|
||||
;*** Needs to be Tested and Debugged ****
|
||||
|
||||
;C02 library stack.h02 assembly language subroutines
|
||||
;Requires External Zero Page Variables
|
||||
;STKLO, STKHI, DSTLO, DSTHI, SRCLO, SRCHI
|
||||
;External Variables
|
||||
;STKSLO, STKSHI, STKELO, STKEHI, STKLEN, TEMP0, TEMP1, TEMP2
|
||||
;STKSLO, STKSHI, STKELO, STKEHI, TEMP0, TEMP1, TEMP2, TEMP3
|
||||
;External Routines
|
||||
;MEMCPL, SETDST, SETSRC, STRLEN
|
||||
|
||||
@@ -47,7 +45,7 @@ STKPTR: LDX STKLO ;Load Stack Pointer LSB
|
||||
STKSSP: JSR STKPTR ;Get Stack Pointer
|
||||
JMP SAVRXY ;Save in TEMP1, TEMP2
|
||||
|
||||
;STKSRC() - Set Source Pointer to Stack Pointer
|
||||
;stksrc() - Set Source Pointer to Stack Pointer
|
||||
;Uses: STKLO,STKHI = Stack Pointer
|
||||
;Sets: SRCLO,SRCHI = Source Array Pointer
|
||||
;Affects: X,N,Z
|
||||
@@ -93,40 +91,15 @@ STKSET: STX STKLO ;Store X in Stack Pointer LSB
|
||||
STY STKHI ;Store Y in Stack Pointer MSB
|
||||
RTS ;Exit Routine
|
||||
|
||||
;stkalc(n) - Allocate Space on Stack
|
||||
;Args: A=Number of Bytes to Allocate
|
||||
;Updates: STKLO, STKHI = Stack Pointer
|
||||
;Affects: C,N,Z
|
||||
;Returns: A = Number of Bytes Allocated
|
||||
; 0 if Error: Pointer Overflow or Length 0
|
||||
; Y=0
|
||||
STKALC: LDA TEMP0 ;If No Bytes
|
||||
BEQ STKINX ; Return False
|
||||
CLC
|
||||
ADC STKLO ;Add to Stack Pointer LSB
|
||||
STA STKLO ;and Save
|
||||
LDA STKHI ;Add Carry
|
||||
ADC #0 ;to Stack Pointer MSB
|
||||
STA STKHI ;and Save
|
||||
LDA STKEHI ;If Stack End MSB
|
||||
CMP STKHI ; < Stack Pointer MSB
|
||||
BCC STKSUZ ; Return False
|
||||
BNE STKALX ;Else if Not Equal
|
||||
LDA STKELO ;and Stack End LSB
|
||||
CMP STKLO ; < Stack Pointer LSB
|
||||
BCC STKSUZ ; Return False
|
||||
STKALX: LDA TEMP0 ;Get Number of Bytes from X Register
|
||||
LDY #0 ;Initialize Index
|
||||
STA (STKLO),Y ;Store after Allocated Area
|
||||
;and fall inro STKINC
|
||||
|
||||
;stkinc(n) - Increment Stack Pointer
|
||||
;Sets: STKLO, STKHI = Stack Pointer Address
|
||||
;Affects: Y,Z,N
|
||||
STKINC: INC STKLO ;Increment LSB
|
||||
BNE STKINX ;If Zero
|
||||
INC STKHI ; Increment MSB
|
||||
STKINX: RTS
|
||||
;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
|
||||
BEQ STKPSA ;Execute STKPSH, skippping SETSRC
|
||||
|
||||
;stkpsh(n, &m) - Push n Bytes of m onto Stack
|
||||
;Args: A = Number of Bytes to Append
|
||||
@@ -142,18 +115,50 @@ STKPSH: JSR SETSRC ;Set Source Address
|
||||
STKPSA: STA TEMP0 ;Save Number of Bytes
|
||||
JSR STKDST ;Set Destination to Stack Pointer
|
||||
JSR STKALC ;Allocate Space on Stack
|
||||
BEQ STKDER ;If Error Return FALSE
|
||||
JMP MEMCPL ;Else Copy Bytes and Return
|
||||
JMP MEMCPL ;Copy Bytes and Return
|
||||
|
||||
;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
|
||||
BEQ STKPSA ;Execute STKPSH, skippping SETSRC
|
||||
;stkadd(n) - Add N Bytes to Stack Pointer
|
||||
;Args: A=Number of Bytes to Allocate
|
||||
;Updates: STKLO, STKHI = Stack Pointer
|
||||
;Affects: A,C,N,Z
|
||||
STKADD: CLC
|
||||
STKADC: ADC STKLO ;Add to Stack Pointer LSB
|
||||
STA STKLO ;and Save
|
||||
LDA STKHI ;Add Carry
|
||||
ADC #0 ;to Stack Pointer MSB
|
||||
STA STKHI ;and Save
|
||||
RTS
|
||||
|
||||
;stkalc(n) - Allocate Space on Stack
|
||||
;Args: A=Number of Bytes to Allocate
|
||||
;Updates: STKLO, STKHI = Stack Pointer
|
||||
;Affects: C,N,Z
|
||||
;Returns: A = Number of Bytes Allocated
|
||||
; 0 if Error: Pointer Overflow or Length 0
|
||||
; Y=0
|
||||
STKALC: JSR STKSSP ;Save Stack Pointer
|
||||
LDA TEMP0 ;If No Bytes
|
||||
BEQ STKABT ; Abort Calling Routine
|
||||
JSR STKADD ;Add to Stack Pointer
|
||||
LDA STKEHI ;If Stack End MSB
|
||||
CMP STKHI ; < Stack Pointer MSB
|
||||
BCC STKRPA ; Abort Calling Routine
|
||||
BNE STKALX ;Else if Not Equal
|
||||
LDA STKELO ;and Stack End LSB
|
||||
CMP STKLO ; < Stack Pointer LSB
|
||||
BCC STKRPA ; Abort Calling Routine
|
||||
STKALX: LDA TEMP0 ;Get Number of Bytes from X Register
|
||||
LDY #0 ;Initialize Index
|
||||
STA (STKLO),Y ;Store after Allocated Area
|
||||
;and fall inro STKINC
|
||||
|
||||
;stkinc(n) - Increment Stack Pointer
|
||||
;Sets: STKLO, STKHI = Stack Pointer Address
|
||||
;Affects: Y,Z,N
|
||||
STKINC: INC STKLO ;Increment LSB
|
||||
BNE STKINX ;If Zero
|
||||
INC STKHI ; Increment MSB
|
||||
STKINX: RTS
|
||||
|
||||
;stkcmp - Compare Stack Pointer to Stack Start Address
|
||||
;Uses: STKLO, STKHI = Stack Pointer Address
|
||||
@@ -179,7 +184,9 @@ STKDEL: DEC STKLO ; Decrement LSB
|
||||
STKDER: RTS
|
||||
|
||||
;stkdal() - Deallocate Stack Entry
|
||||
;Note: Aborts Calling Routine if Error
|
||||
;Uses: STKSLO, STKSHI = Stack Start Address
|
||||
;Sets: TEMP1, TEMP2 = Original Stack Pointer
|
||||
;Updates: STKLO, STKHI = Stack Pointer
|
||||
;Affects: Y,C,N,Z
|
||||
;Returns: A=Number of Bytes in Entry
|
||||
@@ -187,8 +194,8 @@ STKDER: RTS
|
||||
; Y=0
|
||||
STKDAL: JSR STKSSP ;Save Stack Pointer
|
||||
STKDAN: JSR STKCMP ;If Stack Pointer
|
||||
BCC STKSUZ ; <= Stack Start
|
||||
BEQ STKSUZ ; Return 0
|
||||
BCC STKRPA ; <= Stack Start
|
||||
BEQ STKRPA ; Return 0
|
||||
JSR STKDEC ;Decrement Stack Pointer
|
||||
LDY #0 ;Initialize Index
|
||||
LDA (STKLO),Y ;Get Number of Bytes in Entry
|
||||
@@ -204,11 +211,13 @@ STKSUB: STA TEMP0 ;Save Number in TEMP0
|
||||
SBC #0 ;For Borrow
|
||||
STA STKHI
|
||||
JSR STKCMP ;If Stack Pointer < Start
|
||||
BCC STKSUR ; Restore Pointer and Return 0
|
||||
BCC STKRPA ; Restore Pointer and Return 0
|
||||
STKSUE: LDA TEMP0 ;Retrieve Number of Bytes
|
||||
RTS
|
||||
STKSUR: JSR STKRSP ;Restore Stack Pointer
|
||||
STKSUZ: LDA #0 ;Set A to 0
|
||||
STKRPA: JSR STKRSP ;Restore Stack Pointer
|
||||
STKABT: PLA ;Drop Return Address from Stack
|
||||
PLA ; Aborting Calling Routine
|
||||
STKZRO: LDA #0 ;Set A to 0
|
||||
RTS
|
||||
|
||||
;stkdrp(&m) - Drop Top Entry from Stack
|
||||
@@ -217,7 +226,8 @@ STKSUZ: LDA #0 ;Set A to 0
|
||||
;Affects: C,N,Z
|
||||
;Returns: A = Number of Bytes in Dropped Entry
|
||||
; 0 = Error: Stack Underflow
|
||||
STKDRP EQU STKDAL ;Deallocate Stack Entry
|
||||
STKDRP JSR STKDAL ;Deallocate Stack Entry
|
||||
RTS
|
||||
|
||||
;stkpop(&m) - Pop Top Entry off Stack
|
||||
;Args: Y,X = Address of Source Array
|
||||
@@ -228,7 +238,6 @@ STKDRP EQU STKDAL ;Deallocate Stack Entry
|
||||
; 0 = Error: Stack Underflow
|
||||
STKPOP: JSR SETDST ;Set Destination Address
|
||||
JSR STKDAL ;Deallocate Stack Entry
|
||||
BEQ STKDER ;If Underflow, Return 0
|
||||
STKPOS: JSR STKSRC ;Set Source Address to Stack Pointer
|
||||
JMP MEMCPL ;Copy Bytes and Return
|
||||
|
||||
@@ -242,47 +251,54 @@ STKTOP: JSR STKPOP ;Pop Top Entry Off Stack
|
||||
BEQ STKDER ;If Underflow, Return 0
|
||||
JMP STKRSP ;Else Restore Stack Pointer and Return
|
||||
|
||||
;stkdup(&m) - Duplicate Top Entry off Stack
|
||||
;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 Underflow
|
||||
; 0 = Error: Stack Overflow or Underflow
|
||||
STKDUP: JSR STKDAL ;Deallocate Top Entry
|
||||
BEQ STKDER ;If Underflow, Return 0
|
||||
JSR STKSRC ;Set Source Pointer to Stack Pointer
|
||||
STKDUS: JSR STKSRC ;Set Source Pointer to Stack Pointer
|
||||
JSR STKRSP ;Restore Stack Pointer
|
||||
JMP STKSTY ;Push Top Entry onto Stack
|
||||
|
||||
;stkswp(&m) - Swap Top Entry with Entry Below it
|
||||
;Args: Y,X = Address of Tempory Array
|
||||
;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 Underflow
|
||||
STKSWP: JSR SETDST ;Set Swap Array As Destination
|
||||
JSR STKDAL ;Deallocate Top Entry
|
||||
BEQ STKDER ;If Error, Return 0
|
||||
JSR STKDAN ;Deallocate with No Save of Pointer
|
||||
BEQ STKDER ;If Error, Return 0
|
||||
STA TEMP3 ;Save Size of Top Entry
|
||||
JSR STKSSP ;Save Pointer to Top Entry
|
||||
JSR STKPOS ;Copy Second Entry to Array
|
||||
PHA ;Save Size of Second Entry
|
||||
LDA DSTHI ;Save Array Address
|
||||
PHA
|
||||
LDA DSTLO
|
||||
PHA
|
||||
JSR RESRXY ;Set Source String
|
||||
JSR SETSRC ;to Address of Old Top Entry
|
||||
LDA TEMP3 ;Load Old Top Entry Size
|
||||
JSR STKPSA ;Push Old Top Entry
|
||||
PLA ;Restore Array Address
|
||||
TAX
|
||||
PLA
|
||||
TAY
|
||||
PLA ;Restore Size of Second Entry
|
||||
JMP STKPSH ;Push Array Contents onto Stack
|
||||
; 0 = Error: Stack Overflow or Underflow
|
||||
STKOVR: JSR STKDAL ;Deallocate Top Entry
|
||||
JSR STKDAN ;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
|
||||
;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 STKDER ;Exit if Error
|
||||
JSR STKDAN ;Deallocate Duplicate
|
||||
STA TEMP3 ;Save Size of Duplicate
|
||||
JSR STKSSP ;Save Pointer to Duplicate
|
||||
JSR STKDAN ;Deallocate Top Entry
|
||||
JSR STKSRC ;Set Source to Top Entry
|
||||
STA DSTLO ;Save Top Entry Size
|
||||
JSR STKDAN ;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
|
||||
RTS
|
||||
|
||||
Reference in New Issue
Block a user