mirror of
https://github.com/RevCurtisP/C02.git
synced 2025-02-19 19:31:04 +00:00
Added library 'stack'
This commit is contained in:
parent
cc82237782
commit
3f03f71857
@ -1,10 +1,12 @@
|
||||
;*** Needs to be Tested and Debugged ****
|
||||
|
||||
;C02 library block.h02 assembly language subroutines
|
||||
;Requires External Zero Page Variables
|
||||
;BLKLO, BLKHI, DSTLO, DSTHI, SRCLO, SRCHI
|
||||
;External Variables
|
||||
;BLKSLO, BLKSHI, BLKELO, BLKEHI, BLKLEN, TEMP0, TEMP1, TEMP2
|
||||
;External Routines
|
||||
;MEMCMP, MEMCPY, MEMSRC, SETDST, SETSRC
|
||||
;MEMCMP, MEMCPY, MEMSRC, RESDST, SAVDST, SETDST, SETSRC, SETSRD
|
||||
|
||||
;blkbgn(&b) - Set Block Start Address
|
||||
;Args: X,Y = Address
|
||||
@ -68,7 +70,7 @@ BLKSEX: RTS ;Return
|
||||
; A=$00 - Block Overflow
|
||||
BLKPUT: JSR SETSRC ;Save Source Pointer
|
||||
JSR MEMSRA ;Save Number of Bytes to Append
|
||||
JSR BLKSDP ;Set Destination to Block Pointer
|
||||
JSR BLKDST ;Set Destination to Block Pointer
|
||||
JMP BLKNCP ;Move Block Pointer and Copy
|
||||
|
||||
;blkget(n, &m) - Read n Bytes of Block into Array m
|
||||
@ -81,9 +83,8 @@ BLKPUT: JSR SETSRC ;Save Source Pointer
|
||||
;Updates: BLKLO, BLKHI = Block Pointer
|
||||
;Returns: A=$FF - Append Successful
|
||||
; A=$00 - Block Overflow
|
||||
BLKGET: JSR SETDST ;Set Destination Pointer
|
||||
JSR MEMSRA ;Save Number of Bytes to Append
|
||||
JSR BLKSSP ;Set Source Pointer to Block Pointer
|
||||
BLKGET: JSR MEMSRC ;Set Source Address & Number of Bytes
|
||||
JSR BLKSRC ;Set Source Pointer to Block Pointer
|
||||
BLKNCP: JSR BLKNXT ;Move Block Pointer
|
||||
BEQ BLKRTS ;If Past End of Block, Return False
|
||||
JMP BLKCPY ;Else Copy Bytes
|
||||
@ -97,7 +98,7 @@ BLKNCP: JSR BLKNXT ;Move Block Pointer
|
||||
;Returns: A=$FF - Bytes found
|
||||
; A=$00 - Bytes not found
|
||||
BLKMEM: JSR MEMSRC ;Initialize Source, Index, and Count
|
||||
JSR BLKSDB ;Set Destination Pointer To Block Start
|
||||
JSR BLKSRS ;Set Destination Pointer To Block Start
|
||||
BLKMEL: LDY #0 ;Initialize Index
|
||||
JSR MEMCML ;Compare Source to Destination
|
||||
BEQ BLKTRU ;If Equal, Exit TRUE
|
||||
@ -197,8 +198,8 @@ BLKSRT: LDA BLKLEN ;Get Segment Length
|
||||
BLKSRP: LDY #0 ;If First Byte of
|
||||
LDA (BLKLO),Y ; Current Segment is Null
|
||||
BEQ BLKRTS ; Return
|
||||
JSR BLKSSP ;Copy Block Pointer to Source Pointer
|
||||
JSR BLKSDP ;Copy Block Pointer and Destination Pointer
|
||||
JSR BLKSRC ;Copy Block Pointer to Source Pointer
|
||||
JSR BLKDST ;Copy Block Pointer and Destination Pointer
|
||||
BLKSRD: JSR BLKDSN ;Move Destination Pointer
|
||||
BCS BLKSRC ;If Not Past Block End
|
||||
LDY #0 ;
|
||||
@ -206,7 +207,7 @@ BLKSRD: JSR BLKDSN ;Move Destination Pointer
|
||||
BEQ BLKSRC ;
|
||||
JSR STRCML ; Compare Destination String with Source String
|
||||
BPL BLKSRD ; If Destination < Source
|
||||
JSR BLKSSD ; Set Source Pointer to Destination Pointer
|
||||
JSR SETSRD ; Set Source Pointer to Destination Pointer
|
||||
JMP BLKSRD ; Check Next Destination Segment
|
||||
BLKSRC: LDA SRCHI ;If Source Pointer
|
||||
CMP BLKHI ; <> Block Pointer
|
||||
@ -230,18 +231,18 @@ BLKSRN: JSR BLKNXT ;Move Block Pointer
|
||||
;Returns: A=$FF,N=1,Z=0 if Found
|
||||
; A=$00,N=0,Z=1 if Not Found
|
||||
BLKSTR: JSR MEMSRC ;Initialize Source, Index, and Count
|
||||
JSR BLKDSS ;Save Destination Pointer
|
||||
JSR BLKSDB ;Set Destination Pointer To Block Start
|
||||
JSR SAVDST ;Save Destination Pointer
|
||||
JSR BLKSRS ;Set Destination Pointer To Block Start
|
||||
BLKSTL: LDY #0 ;Initialize Index
|
||||
JSR STRCML ;Compare Source to Destination
|
||||
BEQ BLKSTS ;If Not Equal
|
||||
JSR BLKDSN ; Move Destination Pointer
|
||||
BNE BLKSTL ; If Not Past Block End, Loop
|
||||
BLKSTF: JSR BLKDSR ; Else Restore Destination String and Return
|
||||
BLKSTF: JSR RESDST ; Else Restore Destination String and Return
|
||||
LDA #$00 ; and Return FALSE
|
||||
RTS ;Else
|
||||
BLKSTS: JSR BLKSSD ; Set Source Pointer to Destination Pointer
|
||||
JSR BLKDSR ; Restore Destination String Pointer
|
||||
BLKSTS: JSR SETSRD ; Set Source Pointer to Destination Pointer
|
||||
JSR RESDST ; Restore Destination String Pointer
|
||||
BLKCPY: LDY #0 ; Initialize Index
|
||||
JSR MEMCPL ; Copy Segment to Destination String
|
||||
LDA #$FF ; Return TRUE
|
||||
@ -255,67 +256,35 @@ BLKCPY: LDY #0 ; Initialize Index
|
||||
; DSTLO, DSTHI = Pointer to Temporary Storage Array
|
||||
;Affects: A,Y,C,N,Z
|
||||
BLKSWP: JSR MEMSRC ;Set Source Address and Index
|
||||
BLKSWL: JSR BLKSDP ;Set Destination Pointer to Block Pointer
|
||||
BLKSWL: JSR BLKDST ;Set Destination Pointer to Block Pointer
|
||||
LDY #0 ;Initialize Index
|
||||
JMP MEMSWL ;Swap Bytes
|
||||
|
||||
;blkdsb() - Set Destination Pointer to Block Start Address
|
||||
;blkdst() - Set Destination Pointer to Block Pointer
|
||||
;Uses: BLKLO,BLKHI = Block Segment Pointer
|
||||
;Sets: DSTLO,DSTHI = Destination Array Pointer
|
||||
;Affects: N,Z
|
||||
;Returns: X = Block Pointer Low Byte
|
||||
; Y = Block Pointer High Byte
|
||||
BLKDST: LDX BLKLO
|
||||
LDY BLKHI
|
||||
JMP SETDST ;Set Destination and Return
|
||||
|
||||
;blksrc() - Set Source Pointer to Block Pointer
|
||||
;Uses: BLKLO,BLKHI = Block Segment Pointer
|
||||
;Sets: SRCLO,SRCHI = Source Array Pointer
|
||||
;Affects: X,N,Z
|
||||
;Returns: Y = 0
|
||||
BLKSRC: LDX BLKLO
|
||||
LDY BLKHI
|
||||
JMP SETSRC ;Set Source and Return
|
||||
|
||||
;blksrs() - Set Destination Pointer to Block Start Address
|
||||
;Uses: BLKSLO, BLKSHI = Block Start Address
|
||||
;Sets: DSTLO, DSTHI = Block Pointer
|
||||
;Affects: N,Z
|
||||
;Returns: X = Block Pointer Low Byte
|
||||
; Y = Block Pointer High Byte
|
||||
BLKSDB: LDX BLKSLO ;Load Block Start Low Byte
|
||||
BLKSRS: LDX BLKSLO ;Load Block Start Low Byte
|
||||
LDY BLKSHI ;Load Block Start High Bytes
|
||||
JMP SETDST ;Set Destination and Return
|
||||
|
||||
;blksds() - Set Source Pointer to Destination Pointer
|
||||
;Uses: SRCLO,SRCHI = Source Array Pointer
|
||||
;Sets: DSTLO,DSTHI = Destination Array Pointer
|
||||
;Affects: X,N,Z
|
||||
;Returns: Y = 0
|
||||
BLKSSD: LDX DSTLO
|
||||
LDY DSTHI
|
||||
JMP SETSRC
|
||||
|
||||
;blkssp() - Set Source Pointer to Block Pointer
|
||||
;Uses: BLKLO,BLKHI = Block Segment Pointer
|
||||
;Sets: SRCLO,SRCHI = Source Array Pointer
|
||||
;Affects: X,N,Z
|
||||
;Returns: Y = 0
|
||||
BLKSSP: LDX BLKLO
|
||||
LDY BLKHI
|
||||
JMP SETSRC ;Set Source and Return
|
||||
|
||||
;blksdp() - Set Destination Pointer to Block Pointer
|
||||
;Uses: BLKLO,BLKHI = Block Segment Pointer
|
||||
;Sets: DSTLO,DSTHI = Destination Array Pointer
|
||||
;Affects: N,Z
|
||||
;Returns: X = Block Pointer Low Byte
|
||||
; Y = Block Pointer High Byte
|
||||
BLKSDP: LDX BLKLO
|
||||
LDY BLKHI
|
||||
JMP SETDST ;Set Destination and Return
|
||||
|
||||
;blkdss() - Save Destination Pointer
|
||||
;Uses: DSTLO, DSTHI = Destination Array Pointer
|
||||
;Sets: TEMP1, TEMP2 = Temporary Storage
|
||||
;Affects: X,Y,N,Z
|
||||
BLKDSS: LDX DSTLO ;Save Destination Address
|
||||
LDY DSTHI
|
||||
;Save X and Y Registers
|
||||
BLKXYS: STX TEMP1
|
||||
STY TEMP2
|
||||
RTS
|
||||
|
||||
;blkdsr() - Restore Destination Pointer
|
||||
;Uses: TEMP1, TEMP2 = Temporary Storage
|
||||
;Sets: DSTLO, DSTHI = Destination Array Pointer
|
||||
;Affects: A,N,Z
|
||||
;Returns: X = Block Pointer Low Byte
|
||||
; Y = Block Pointer High Byte
|
||||
BLKDSR: LDX TEMP1 ;Restore Destination Address
|
||||
LDY TEMP2
|
||||
JMP SETDST ;Set Destination and Return
|
||||
|
||||
|
||||
|
@ -19,11 +19,21 @@ RANDOM EQU $0F ;Pseudo-RANDOM Number Storage
|
||||
TEMP0 EQU $10 ;Temporary Storage
|
||||
TEMP1 EQU $11
|
||||
TEMP2 EQU $12
|
||||
TEMP3 EQU $12
|
||||
|
||||
BLKSLO EQU $20 ;Block Start Address
|
||||
BLKSHI EQU $21
|
||||
BLKELO EQU $22 ;Block End Address
|
||||
BLKEHI EQU $23
|
||||
BLKLO EQU $24 ;Block Pointer
|
||||
BLKHI EQU $25
|
||||
|
||||
STKSLO EQU $28 ;Stack Start Address
|
||||
STKSHI EQU $29
|
||||
STKELO EQU $2A ;Stack End Address
|
||||
STKEHI EQU $2B
|
||||
STKLO EQU $2C ;Stack Pointer
|
||||
STKHI EQU $2D
|
||||
|
||||
ORG $0200 ;Program Start Address
|
||||
|
||||
|
@ -18,7 +18,7 @@ char dstlo,dsthi; //Secondary string pointer for String functions
|
||||
* Available for use in program code, but may *
|
||||
* be obliterated by Function Calls. *
|
||||
* May be Zero Page, but not required */
|
||||
char temp0,temp1; //Temporary variables
|
||||
char temp0,temp1,temp2,temp3; //Temporary variables
|
||||
|
||||
/* Static Library Variables *
|
||||
* Must be preserved between function calls *
|
||||
|
@ -37,14 +37,14 @@ MEMCLX: TYA ;Return Index into String
|
||||
;Affects: N,Z
|
||||
MEMDST EQU SETDST ;Aliased to System Header function
|
||||
|
||||
;Set Source Address and Number of Characters
|
||||
;Set Source Address and Number of Bytes
|
||||
;Args: X,Y = Pointer to Source Array
|
||||
;Sets: DSTLO,DSTHI = Pointer to destination Array
|
||||
; TEMP0 - Number of Characters
|
||||
;Sets: SCRLO,SRCHI = Pointer to destination Array
|
||||
; TEMP0 - Number of Bytes
|
||||
;Affects: A,N,Z
|
||||
MEMSRC: JSR SETSRC ;Initialize Source Array and Index
|
||||
MEMSRA: STA TEMP0 ;Store Number Of Character
|
||||
ORA #0 ;If Number of Characters
|
||||
ORA #0 ;If Number of Bytes
|
||||
BNE MEMSRX ; Equals 0
|
||||
PLA ; Pull Return Address off Stack
|
||||
PLA ; Aborting Calling Routine
|
||||
@ -79,12 +79,14 @@ MEMCMR: RTS ;
|
||||
; X,Y = Pointer to source Array
|
||||
;Sets: TEMP0 = Number of bytes to copy
|
||||
;Affects: A,Y,C,N,Z
|
||||
;Returns: A=Number of Bytes Copied
|
||||
MEMCPY: JSR MEMSRC ;Initialize Source, Index, and Count
|
||||
MEMCPL: LDA (SRCLO),Y ;Get Character from Source Array
|
||||
STA (DSTLO),Y ;Copy to Destination Array
|
||||
INY ; Increment Index
|
||||
CPY TEMP0 ;If < Count
|
||||
BCC MEMCPL ; Loop
|
||||
TYA ;Copy Index to Accumulater
|
||||
RTS ;Else Return
|
||||
|
||||
;memset(c, n) - Fill Destination Array
|
||||
|
@ -26,7 +26,7 @@ char memcmp();
|
||||
|
||||
/* Copy Source Array to Destination Array *
|
||||
* Args: n - Number of bytes to copy *
|
||||
* &s - Source string *
|
||||
* &s - Source array *
|
||||
* Sets: Destination string specified by memdst() */
|
||||
char memcpy();
|
||||
|
||||
|
@ -12,8 +12,8 @@ DSTLO EQU $32 ;Destination String Pointer (string.a02)
|
||||
DSTHI EQU $33
|
||||
BLKLO EQU $34 ;Block Segment Pointer (block.a02)
|
||||
BLKHI EQU $35
|
||||
PTRLO EQU $35 ;System Pointer (pointer.a02)
|
||||
PTRHI EQU $36
|
||||
STKLO EQU $36 ;Stack Pointer (stack.a02)
|
||||
STKHI EQU $37
|
||||
|
||||
RDSEED EQU $3E ;Pseudo-RANDOM Seed
|
||||
RANDOM EQU $3F ;Pseudo-RANDOM Number Storage
|
||||
@ -23,11 +23,16 @@ TEMP1 EQU $41
|
||||
TEMP2 EQU $42
|
||||
TEMP3 EQU $43
|
||||
|
||||
BLKSLO EQU $4A ;Block Start Address
|
||||
BLKSHI EQU $4B
|
||||
BLKELO EQU $4C ;Block End Address
|
||||
BLKEHI EQU $4D
|
||||
BLKLEN EQU $4E ;Block Segment Length
|
||||
BLKSLO EQU $46 ;Block Start Address
|
||||
BLKSHI EQU $47
|
||||
BLKELO EQU $48 ;Block End Address
|
||||
BLKEHI EQU $49
|
||||
BLKLEN EQU $4A ;Block Segment Length
|
||||
|
||||
STKSLO EQU $4C ;Stack Start Address
|
||||
STKSHI EQU $4D
|
||||
STKELO EQU $4E ;Stack End Address
|
||||
STKEHI EQU $4F
|
||||
|
||||
;Memory Mapped I/O
|
||||
PUTCON EQU $F001 ;Write Character to Console
|
||||
@ -89,15 +94,43 @@ PRHEX: AND #$0F ;Strip High Nybb
|
||||
CLD ;Clear Decimal Flag
|
||||
JMP PRCHR ;Print Hex Digit and Return
|
||||
|
||||
;Initialize Destination String Pointer and Index
|
||||
SETDST: STX DSTLO ;Save Destination String Pointer
|
||||
|
||||
;savdst() - Save Destination Pointer
|
||||
SAVDST: JSR GETDST ;Load Destination Pointer
|
||||
JMP 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 Accumlator
|
||||
RESRXY: STX TEMP1 ;Load X Index
|
||||
STY TEMP2 ;Load Y Index
|
||||
RTS
|
||||
|
||||
;Restore Destination Pointer
|
||||
RESDST: JSR RESRXY ;Load Address and Drop into SETDST
|
||||
|
||||
;Initialize Destination Pointer
|
||||
SETDST: STX DSTLO ;Store Destination Pointer
|
||||
STY DSTHI
|
||||
RTS
|
||||
|
||||
;Initialize Source String Pointer and Index
|
||||
SETSRC: STX SRCLO ;Save Source String Pointer
|
||||
;Set Source Pointer to Destination Pointer
|
||||
SETSRD: JSR GETDST ;Get Destination Point and fall into SETSRC
|
||||
|
||||
;Initialize Source Pointer and Index
|
||||
SETSRC: STX SRCLO ;Store Source Pointer
|
||||
STY SRCHI
|
||||
LDY #$00 ;Initialize Index Into String
|
||||
LDY #$00 ;Initialize Index Into String
|
||||
RTS
|
||||
|
||||
;Retrieve Source String Pointer
|
||||
GETDST: LDX DSTLO
|
||||
LDY DSTHI
|
||||
RTS
|
||||
|
||||
;Retrieve Source String Pointer
|
||||
|
@ -4,12 +4,14 @@
|
||||
char srclo,srchi; //Source String Pointer for Library Functions
|
||||
char dstlo,dsthi; //Destination String Pointer for Library Functions
|
||||
char blklo,blkhi; //Block Segment Pointer
|
||||
char ptrlo,ptrhi; //System Pointer
|
||||
char stklo,stkhi; //Stack Pointer
|
||||
|
||||
//System Variables
|
||||
char blkslo, blkshi; //Block Start Address
|
||||
char blkelo, blkehi; //Block End Address
|
||||
char blklen; //Block Segment Length
|
||||
char stkslo, stkshi; //Stack Start Address
|
||||
char stkelo, stkehi; //Stsck End Address
|
||||
|
||||
char temp0, temp1, temp2, temp3; //Temporary Variables
|
||||
|
||||
@ -25,6 +27,8 @@ void newlin(); //Advance cursor to beginning of next line
|
||||
void prchr(); //Print ASCII character to Console
|
||||
void prbyte(); //Print Accumulator as Hexadadecimal number
|
||||
void prhex(); //Print Low Nybble of Accumulator as Hex Digit
|
||||
void savdst(); //Save Deatination Pointer
|
||||
void savreg(); //Save Registers
|
||||
void setdst(); //Set Destination Pointer
|
||||
void setsrc(); //Set Source Pointer
|
||||
|
||||
void setsrd(); //Set Source Pointer to Destination Pointer
|
||||
|
204
include/stack.a02
Normal file
204
include/stack.a02
Normal file
@ -0,0 +1,204 @@
|
||||
;*** 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
|
||||
;External Routines
|
||||
;MEMCPL, SETDST, SETSRC, STRLEN
|
||||
|
||||
;Implementation Notes:
|
||||
;Stack starts at STKSLO,STKSHI and builds upward tp STKELO, STKEHI
|
||||
;Each entry on the stack consists of the entry data followed
|
||||
;by a single byte containing the length of the entry data
|
||||
|
||||
;stkbgn(&b) - Set Stack Start Address
|
||||
;Args: X,Y = Address
|
||||
;Sets: STKSLO, STKSHI = Stack Start Address
|
||||
;Affects: Z, N
|
||||
STKBGN: STX STKSLO ;Save Stack Start LSB
|
||||
STY STKSHI ;Save Stack Start MSB
|
||||
RTS
|
||||
|
||||
;stkend(&b) - Set Stack End Address (+1)
|
||||
;Args: X,Y = Address
|
||||
;Sets: STKELO, STKEHI = Stack End Address
|
||||
;Affects: Z, N
|
||||
STKEND: STX STKELO ;Save Stack End LSB
|
||||
STY STKEHI ;Save Stack End MSB
|
||||
RTS
|
||||
|
||||
;STKPTR() - Get Stack Pointer
|
||||
;Uses: STKLO,STKHI = Stack Pointer
|
||||
;Affects: X,N,Z
|
||||
;Returns: X = Stack Pointer LSB
|
||||
; Y = Stack Pointer MSB
|
||||
STKPTR: LDX STKLO ;Load Stack Pointer LSB
|
||||
LDY STKHI ;Load Stack Pointer MSB
|
||||
RTS
|
||||
|
||||
;STKSRC() - Set Source Pointer to Stack Pointer
|
||||
;Uses: STKLO,STKHI = Stack Pointer
|
||||
;Sets: SRCLO,SRCHI = Source Array Pointer
|
||||
;Affects: X,N,Z
|
||||
;Returns: Y = 0
|
||||
STKSRC: JSR STKPTR ;Get Stack Pointer Address
|
||||
JMP SETSRC ;Set Source/ an/d Return
|
||||
|
||||
;stkdst() - Set Destination Pointer to Stack Pointer
|
||||
;Uses: STKLO,STKHI = Stack Pointer
|
||||
;Sets: DSTLO,DSTHI = Destination Array Pointer
|
||||
;Affects: N,Z
|
||||
;Returns: X = Stack Pointer LSB
|
||||
; Y = Stack Pointer MSB
|
||||
STKDST: JSR STKPTR ;Get Stack Pointer Address
|
||||
JMP SETDST ;Set Destination and Return
|
||||
|
||||
;stkrst() - Reset Stack Pointer to Start Address
|
||||
;Uses: STKSLO, STKSHI = Stack Start Address
|
||||
;Sets: STKLO, STKHI = Stack Pointer
|
||||
;Affects: Z, N
|
||||
;Returns: X = Stack Pointer LSB
|
||||
; Y = Stack Pointer MSB
|
||||
STKRST: LDX STKSLO ;Load X with Stack Start LSB
|
||||
LDY STKSHI ;Load X with Stack Start MSB
|
||||
|
||||
;stkset() - Set Stack Pointer Address
|
||||
;Args: X,Y = Address
|
||||
;Sets: STKLO, STKHI = Stack Pointer Address
|
||||
;Affects: Z, N
|
||||
;Returns: X = Stack Pointer LSB
|
||||
; Y = Stack Pointer MSB
|
||||
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
|
||||
|
||||
;stkpsh(n, &m) - Push n Bytes of m onto Stack
|
||||
;Args: A = Number of Bytes to Append
|
||||
; Y,X = Address of Source Array
|
||||
;Uses: STKELO, STKEHI = Stack End Address
|
||||
;Sets: DSTLO, DSTHI = Pointer to Stack Entry
|
||||
; SRCHI, SRCLO = Pointer to Source Array
|
||||
; TEMP0 = Number of Bytes to Append
|
||||
;Updates: STKLO, STKHI = Stack Pointer
|
||||
;Returns: A=Number of Bytes Pushed
|
||||
; 0 = Error: Stack Underflow
|
||||
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
|
||||
|
||||
;stkcmp - Compare Stack Pointer to Stack Start Address
|
||||
;Uses: STKLO, STKHI = Stack Pointer Address
|
||||
; STKSLO, STKSHI = Stack Start Address
|
||||
;Returns: C=1,Z=1 if Stack Pointer = Stack Start
|
||||
; C=1,Z=0 if Stack Pointer > Stack Start
|
||||
; C=0,Z=0 if Stack Pointer < Stack Start
|
||||
STKCMP: LDA STKHI ;Compare MSB
|
||||
CMP STKSHI
|
||||
BCC STKCMX ;If Pointer<Start Return C=0, Z=1
|
||||
BNE STKCMX ;If Pointer>Start Return C=1. Z=0
|
||||
LDA STKLO ;If MSB Equal
|
||||
CMP STKSLO ;Compare LSB
|
||||
STKCMX: RTS
|
||||
|
||||
;stkdec(n) - Decrement Stack Pointer
|
||||
;Sets: STKLO, STKHI = Stack Pointer Address
|
||||
;Affects: X,Z,N
|
||||
STKDEC: LDX STKLO ;If LSB
|
||||
BNE STKDEL ; is Zero
|
||||
DEC STKHI ; Decrement MSB
|
||||
STKDEL: DEC STKLO ; Decrement LSB
|
||||
STKDER: RTS
|
||||
|
||||
;stkdal() - Deallocate Stack Entry
|
||||
;Uses: STKSLO, STKSHI = Stack Start Address
|
||||
;Updates: STKLO, STKHI = Stack Pointer
|
||||
;Affects: C,N,Z
|
||||
;Returns: A = Number of Bytes in Entry
|
||||
; 0 = Error: Stack Underflow
|
||||
; Y = 0
|
||||
STKDAL: LDY #0 ;Initialize Index
|
||||
JSR STKCMP ;If Stack Pointer
|
||||
BCC STKSUZ ; <= Stack Start
|
||||
BEQ STKSUZ ; Return 0
|
||||
JSR STKDEC ;Decrement Stack Pointer
|
||||
LDA (STKLO),Y ;Get Number of Bytes in Entry
|
||||
;and fall into STKSUB
|
||||
|
||||
;stksub(n) - Reduce Stack Pointer by A
|
||||
STKSUB: STA TEMP0 ;Save Number in TEMP0
|
||||
LDA STKLO
|
||||
SEC ;Subtract A from LSB
|
||||
SBC TEMP0
|
||||
STA STKLO
|
||||
LDA STKHI ;Subtract 0 from LSB
|
||||
SBC #0 ;For Borrow
|
||||
STA STKHI
|
||||
STKSUC: JSR STKCMP ;If Stack Pointer LSB
|
||||
BCC STKSUE ; < Stack Pointer
|
||||
STKSUE: LDA TEMP0 ;Retrieve Number of Bytes
|
||||
RTS
|
||||
STKSUZ: LDA #0 ;Set A to 0
|
||||
RTS
|
||||
|
||||
;stkpop(&m) - Pop Top Entry off Stack
|
||||
;Args: Y,X = Address of Source Array
|
||||
;Uses: STKSLO, STKSHI = Stack Start Address
|
||||
;Updates: STKLO, STKHI = Stack Pointer
|
||||
;Affects: C,N,Z
|
||||
;Returns: A,Y = Number of Bytes in Entry
|
||||
; 0 = Error: Stack Underflow
|
||||
STKPOP: JSR SETDST ;Set Destination Address
|
||||
JSR STKDAL ;Deallocate Stack Entry
|
||||
BEQ STKDER ;If Underflow, Return 0
|
||||
JSR STKSRC ;Set Source Address to Stack Pointer
|
||||
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
|
49
include/stack.h02
Normal file
49
include/stack.h02
Normal file
@ -0,0 +1,49 @@
|
||||
/******************************************
|
||||
* stack - Stack Memory Functions for C02 *
|
||||
******************************************/
|
||||
|
||||
/* Set Stack Start Address *
|
||||
* Args: &addr - Start Address */
|
||||
void stkbgn();
|
||||
|
||||
/* Set Destination to Stack Pointer *
|
||||
* Returns: Y,X=Pointer Address */
|
||||
void stkdst();
|
||||
|
||||
/* Set Stack End Address *
|
||||
* Args: &addr - End Address */
|
||||
void stkend();
|
||||
|
||||
/* Pop Array from Contents from Stack *
|
||||
* Args: n - Number of bytes to write *
|
||||
* &m - Array to pop contents into *
|
||||
* Returns: A=Number of bytes retrieved *
|
||||
* 0 if none */
|
||||
char stkpop();
|
||||
|
||||
/* Push Array Contents onto Stack *
|
||||
* Args: n - Number of bytes to write *
|
||||
* &m - Array containing bytes to push *
|
||||
* Returns: A=$FF if bytes were written *
|
||||
* $00 if block was overflowed */
|
||||
char stkpsh();
|
||||
|
||||
/* Get Stack Pointer Address *
|
||||
* Returns: Y,X=Pointer Address */
|
||||
void stkptr();
|
||||
|
||||
/* Reset Stack Pointer to Stack Start *
|
||||
* Returns: Y,X=Pointer Address */
|
||||
void stkrst();
|
||||
|
||||
/* Set Stack Pointer Address *
|
||||
* Args: &addr - Pointer Address */
|
||||
void stkset();
|
||||
|
||||
/* Push String onto Stack *
|
||||
* Args: &m - String to push *
|
||||
* Returns: A=$FF if bytes were written *
|
||||
* $00 if block was overflowed */
|
||||
char stkstr();
|
||||
|
||||
void stkrst();
|
@ -3,50 +3,70 @@
|
||||
; external zero page locations SRCLO and srchi
|
||||
; and external locations RANDOM, RDSEED, TEMP0, TEMP1, and TEMP2.
|
||||
|
||||
;Return ABSolute Value of Accumulator
|
||||
;abs(n) - Get ABSolute Value
|
||||
;Args: A = Number to get Absolute Value Of
|
||||
;Affects: C, N, Z
|
||||
;Returns: A = Absolute Value of Argument
|
||||
ABS: CMP #$80 ;If Negative (High Bit Set)
|
||||
BCC ABSX ; Carry will Already be Set
|
||||
EOR #$FF ; One's Complement
|
||||
ADC #$00 ; and Increment (Carry set by CMP)
|
||||
ABSX: RTS
|
||||
|
||||
;Return Higher of A and Y
|
||||
;Uses TEMP0
|
||||
;Affects A,N,Z,C
|
||||
;max(m,n) - Get MAXimum of Two Numbers
|
||||
;Args: A,Y = Numbers to Compare
|
||||
;Sets: TEMP0 = Second Argument
|
||||
;Affects: N,Z,C
|
||||
;Returns: A = Larger of the Two Arguments
|
||||
MAX: STY TEMP0 ;Save Second Parameter
|
||||
CMP TEMP0 ;If First Parameter
|
||||
BCC MAXX ; Greater than Second Parameter
|
||||
TYA ;Copy Second Parameter into Accumulator
|
||||
MAXX: RTS
|
||||
|
||||
;Return Lower of A and &
|
||||
;Uses TEMP0
|
||||
;Affects A,N,Z,C
|
||||
;min(m,n) - Get MINimum Get MAXimum of Two Numbers
|
||||
;Args: A,Y = Numbers to Compare
|
||||
;Sets: TEMP0 = Second Argument
|
||||
;Affects: N,Z,C
|
||||
;Returns: A = Smaller of the Two Arguments
|
||||
MIN: STY TEMP0 ;Save Second Parameter
|
||||
CMP TEMP0 ;If First Parameter
|
||||
BCS MINX ; Less than Second Parameter
|
||||
TYA ;Copy Second Parameter into Accumulator
|
||||
MINX: RTS
|
||||
|
||||
;Multiply A times Y
|
||||
;Uses TEMP0, TEMP1
|
||||
;Affects A,N,Z,C
|
||||
;mult(m,n) - MULTiply Two Numbers
|
||||
;Args: A = Multiplicand
|
||||
; Y = Nultiplier
|
||||
;Sets: TEMP0 = Multiplicand
|
||||
; TEMP1 = 0
|
||||
; TEMP3 = Product MSB
|
||||
;Affects: N,Z,C
|
||||
;Returns: A = Product LSB
|
||||
; Y = Product MSB
|
||||
MULT: STA TEMP0 ;Store Multiplicand
|
||||
STY TEMP1 ;Store Multiplier
|
||||
;Multiply TEMP0 times TEMP1
|
||||
MULTT: LDA #$00 ;Initialize Accumulator
|
||||
STA TEMP2 ;and Result High Byte
|
||||
BEQ MULTE ;Enter Loop
|
||||
MULTA: CLC
|
||||
ADC TEMP0 ;Add Multiplicand
|
||||
MULTL: ASL TEMP0 ;Shift Multiplicand Left
|
||||
ROL TEMP2
|
||||
MULTE: LSR TEMP1 ;Shift Multiplier Right
|
||||
BCS MULTA ;If Bit Shifted Out, Add Multiplicand
|
||||
BNE MULTL ;Loop if Any 1 Bits Left
|
||||
RTS
|
||||
|
||||
;Divide A by Y
|
||||
;Used TEMP0,TEMP1
|
||||
;Affects A,X,N,Z,C
|
||||
;div(m,n) - MULTiply Two Numbers
|
||||
;Args: A = Dividend
|
||||
; Y = Divisor
|
||||
;Sets: TEMP0 = 0
|
||||
; TEMP1 = Divisor
|
||||
;Affects: N,Z,C
|
||||
;Returns: A = Quotient
|
||||
; Y = Remainder
|
||||
DIV: STA TEMP0 ;Store Dividend
|
||||
STY TEMP1 ;Store Divisor
|
||||
;Divide TEMP0 by TEMP1
|
||||
@ -61,6 +81,7 @@ DIVL: ROL TEMP0 ;Shift Bit Out of Dividend
|
||||
DIVS: DEX ;Decrement Counter
|
||||
BPL DIVL ; and Loop
|
||||
ROL TEMP0 ;Shift Result into Dividend
|
||||
TYA ;Copy Remainder to Y Register
|
||||
LDA TEMP0 ;Load Result into Accumulator
|
||||
RTS
|
||||
|
||||
@ -87,6 +108,7 @@ RANDS: ORA #$00 ;If Passed Value not 0
|
||||
|
||||
;Return A Shifted Y Bytes to the Left
|
||||
;Affects A,Y,N,Z,C
|
||||
;Affects A,Y,N,Z,C
|
||||
SHIFTL: ASL ;Shift Byte to Left
|
||||
DEY ;Decrement Counter
|
||||
BNE SHIFTL ; and Loop if Not 0
|
||||
@ -96,12 +118,16 @@ SHIFTL: ASL ;Shift Byte to Left
|
||||
;Affects A,Y,N,Z,C
|
||||
SHIFTR: LSR ;Shift Byte to Right
|
||||
DEY ;Decrement Counter
|
||||
BNE SHIFTL ; and Loop if Not 0
|
||||
BNE SHIFTR ; and Loop if Not 0
|
||||
RTS
|
||||
|
||||
;Convert ASCII to Byte
|
||||
;Uses TEMP0, TEMP1
|
||||
;Affects A,X,Y,N,Z,C
|
||||
;atoc(&s) - ASCII string TO Character
|
||||
;Args: Y,X = Address of String to Convert
|
||||
;Uses: TEMP0, TEMP1
|
||||
;Sets: SRCLO, SRCHI = Address of String
|
||||
;Returns: A = Converted Number
|
||||
; C,N,Z based on Accumulator
|
||||
; Y = Number of Digits
|
||||
ATOC: JSR SETSRC ;Initialize Source String
|
||||
STY TEMP0 ;Initialize Result
|
||||
ATOCL: LDA (SRCLO),Y ;Get Next Character
|
||||
@ -123,9 +149,14 @@ ATOCL: LDA (SRCLO),Y ;Get Next Character
|
||||
ATOCX: LDA TEMP0 ;Load Result
|
||||
RTS ;And Return
|
||||
|
||||
;Convert Byte to ASCII String
|
||||
;Uses SRCLO, srchi, TEMP0, TEMP1, TEMP2
|
||||
;Affects A,X,Y,N,Z
|
||||
;ctoa(n) - Character TO ASCII string
|
||||
;Args: Y,X = Address of String to Populate
|
||||
;Sets: SRCLO, SRCHI = Address of String
|
||||
; TEMP0 = Ones Digit
|
||||
; TEMP1 = Tens Digit
|
||||
; TEMP2 = Hundreds Digit
|
||||
;Affects: C,N.Z
|
||||
;Returns: A,Y = Length of String
|
||||
CTOA: JSR SETSRC ;Initialize Source String
|
||||
JSR CUBCD ;Convert Accumulator to Unpacked BCD
|
||||
LDA TEMP2 ;Get High Byte
|
||||
@ -143,14 +174,17 @@ CTOA3: LDA TEMP0 ;Get Low Byte
|
||||
CTOAN: ORA #$30 ;Convert to ASCII digit
|
||||
CTOAX: STA (SRCLO),Y ;Store in StringCTOAS: STA (SRCLO),Y ;Store in String
|
||||
INY ;and Increment Offset
|
||||
TYA ;Copy String Length to Accumulator
|
||||
RTS
|
||||
|
||||
;Unpack 3-Digit BCD Number
|
||||
;Input: TEMP1 = Low Byte
|
||||
; TEMP2 = High Nybble
|
||||
;Output: TEMP0 = Ones Digit
|
||||
; TEMP1 = Tens Digit
|
||||
; TEMP2 = Hundreds Digit
|
||||
;cubcd(n) - Convert and Unpack BCD number
|
||||
;Args: A - Number to Convert
|
||||
;upbcd() - UnPack 3-digit BCD number
|
||||
;Uses: TEMP1 = Low Byte
|
||||
; TEMP2 = High Nybble
|
||||
;Sets: TEMP0 = Ones Digit
|
||||
; TEMP1 = Tens Digit
|
||||
; TEMP2 = Hundreds Digit
|
||||
;Affects: A,N,Z
|
||||
;Returns: X = 0
|
||||
CUBCD: JSR CVBCD ;Convert Accumulator to BCD
|
||||
@ -166,14 +200,19 @@ UPBCD: LDA TEMP1 ;Get Low Byte
|
||||
RTS
|
||||
|
||||
|
||||
;Convert Binary Number in Accumulator to Binary Coded Decimal
|
||||
;Uses: TEMP0
|
||||
;Returns BCD in TEMP1,temp and A,X = 0
|
||||
;ConVert number to packed Binary Coded Decimal
|
||||
;Args: A - Number to Convert
|
||||
;Destroys: TEMP0
|
||||
;Sets: TEMP1 = Tens and Ones Digit
|
||||
; TEMP2 = Hundreds Digit
|
||||
;Returns: A = Hundreds Digit
|
||||
; X = 0
|
||||
CVBCD: STA TEMP0 ;Save Binary Value
|
||||
CVBCDT: LDA #0 ;Clear BCD Bytes
|
||||
STA TEMP1
|
||||
STA TEMP2
|
||||
LDX #8 ;Process 8 bits of Binary
|
||||
PHP ;Save Status Register
|
||||
SEI ;Disable Interupts
|
||||
SED ;Set Decimal Mode
|
||||
CVBCDL: ASL TEMP0 ;Shift High Bit Into Carry
|
||||
@ -185,6 +224,5 @@ CVBCDL: ASL TEMP0 ;Shift High Bit Into Carry
|
||||
STA TEMP2 ; Effectively Multiplying It by 2
|
||||
DEX ;Decrement Counter and
|
||||
BNE CVBCDL ; Process Next Bit
|
||||
CLD ;Clear Decimal Mode
|
||||
CLI ;Enable Interrupts
|
||||
PLP ;Restore Status Register
|
||||
RTS
|
||||
|
@ -15,18 +15,20 @@ char abs();
|
||||
|
||||
/* Convert ASCII String to Unsigned Byte *
|
||||
* Args: &s - String to Convert *
|
||||
* Returns: Numeric value of string */
|
||||
* Returns: Numeric value of string *
|
||||
* Number of digits parsed */
|
||||
char atoc();
|
||||
|
||||
/* Convert Unsigned Byte to ASCII String *
|
||||
* Args: c - Unsigned Byte to Convert *
|
||||
* &s - String to populate */
|
||||
* &s - String to populate *
|
||||
* Returns: Length of string */
|
||||
void ctoa();
|
||||
|
||||
/* Divide Unsigned Bytes *
|
||||
* Args: n - Numerator *
|
||||
* d - Denominator *
|
||||
* Returns: Numerator / Denominator */
|
||||
/* Divide Unsigned Bytes *
|
||||
* Args: n - Numerator *
|
||||
* d - Denominator *
|
||||
* Returns: Quotient, Remainder */
|
||||
char div();
|
||||
|
||||
/* Maximum of Two Bytes *
|
||||
@ -41,10 +43,10 @@ char max();
|
||||
* Returns: Lesser of the Two */
|
||||
char min();
|
||||
|
||||
/* Multiply Unsigned Bytes *
|
||||
* Args: d - Multiplicand *
|
||||
* r - Multiplier *
|
||||
* Returns: Multiplicand * Multiplier */
|
||||
/* Multiply Unsigned Bytes *
|
||||
* Args: d - Multiplicand *
|
||||
* r - Multiplier *
|
||||
* Returns: Product LSB, MSB */
|
||||
char mult();
|
||||
|
||||
/* Generate Pseudo-Random Number *
|
||||
|
@ -49,14 +49,15 @@ STRCLX: LDA #$FF ;Load -1 into Accumulater
|
||||
;strlen(&s) - Return Length of String
|
||||
;Args: X,Y - Pointer to string
|
||||
;Sets: SRCLO,SRCHI = Pointer to source string
|
||||
;Affects: N,Z
|
||||
;Returns: A,Y = Length of string
|
||||
; N,Z based on A
|
||||
STRLEN: JSR SETSRC ;Initialize Source String
|
||||
STRLEL: LDA (SRCLO),Y ;Get Next Character
|
||||
BEQ STRLEX ;If <> NUL
|
||||
INY ; Increment Index
|
||||
BPL STRLEL ; and Loop if < 128
|
||||
STRLEX: TYA ;Transfer Index to Accumulator
|
||||
ORA #0 ;Set N and Z flags
|
||||
RTS ;and Return
|
||||
|
||||
;strdst(&s) - Set Destination String
|
||||
|
Loading…
x
Reference in New Issue
Block a user