1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-06-25 14:29:27 +00:00

Finished stack module in standard library

This commit is contained in:
Curtis F Kaylor 2018-07-27 13:43:32 -04:00
parent 37842aa306
commit 1090e1e96a
4 changed files with 490 additions and 93 deletions

179
doc/stack.txt Normal file
View File

@ -0,0 +1,179 @@
Sta Functions
This library contains functions to access and manipulate a stack. The
entries on a stack are stored in contiguous bytes of memory.
Each element of the stack can vary in size from 1 to 255 bytes. Both
arrays and strings may be pushed onto and pulled off the stack.
Usage: at the beginning of the program use the directives
#include <stddef.h02>
#include <memory.h02>
#include <string.h02>
#include <stack.h02>
The following application functions are defined:
stkbgn(&b); Stack Begin: Sets the beginning of the stack
to address b.
The beginning of a stack is the first byte
of memory in the stack space.
Although stack space usually begins on a 256
byte boundary, this is not required.
Note: Sets variables stkslo and stkshi.
stkend(&e); Stack End: Sets the end of the stack to address
e. The end of a stack is the byte after the last
byte of memory in the stack space.
Although stack space usually ends on a 256
byte boundary, this is not required.
Note: Sets variables stkelo and steshi.
stkrst(); Stack Reset: Set stack pointer to stack begin
address.
This routine is called before populating a
stack with calls to the stkpsh function.
r = stkpsh(n ,&m); Stack Push: Creates a new entry at the end of
the stack consisting of n bytes of array m.
If n is 0 or the new entry would overflow the
end of the stack space, no entry is created and
a value of 0 is returned. Otherwise, the number
of bytes in the new entry is returned.
Note: Sets dstlo and dsthi to stkslo and stkhi
prior to the copy, updates stkslo and stkshi,
then calls the memcpy function.
r = stkstr(&s); Stack String: Creates a new entry at the end of
the stack consisting the characters in string
s, including the terminating ASCII null. This
ensures that when the entry is popped off the
stack, the destination string will be properly
terminated.
If the string is empty or the bew entry would
overflow the end of the stack space, no entry is
created and a value of 0 is returned. Otherwise,
the number of bytes in the new entry is returned.
Note: Calls the strlen function, then calls the
stkpsh function.
r = stkpop(&m); Stack Pop: Copies the bytes from the last entry of
the stack into array m and removes the entry from
the stack.
If the stack is empty, the value 0 is returned.
Otherwise, the number of bytes in the popped entry
is returned.
Note: Sets dstlo and dsthi to the address of m,
updates stklo and stkhi to point to the beginning
of the last entry, sets srclo and srchi to stklo
and stkhi, then calls the memcpy function.
r = stktop(&m); Stack Top: Copies the bytes from the last entry of
the stack into array m and leaves the entry on the
stack.
If the stack is empty, the value 0 is returned.
Otherwise, the number of bytes in the entry is
returned.
Note: Calls stkpop, then restores stklo and stkhi
to their prior values.
r = stkdup(); Stack Duplicate: Creates a new entry at the end of
the stack consisting of the bytes in the last entry
of the stack.
If the stack is empty or the new entry would
overflow the end of the stack space, no entry is
created and a value of 0 is returned. Otherwise,
the number of bytes in the new entry is returned.
Note: Sets dstlo and dsthi to the stklo and stkhi,
sets srclo and srchi to point to the beginning
of the last entry, updates stklo and stkhi, then
calls the memcpy function.
r = stkovr(); Stack Over: Creates a new entry at the end of
the stack consisting of the bytes in the second
to last entry of the stack.
If there are less than two entries in the stack
or the new entry would overflow the end of the
stack space, no entry is created and a value of
0 is returned. Otherwise, the number of bytes in
the new entry is returned.
Note: Sets dstlo and dsthi to the stklo and stkhi,
sets srclo and srchi to point to the beginning
of the second to last entry, updates stklo and
stkhi, then calls the memcpy function.
stkswp(n); Stack Swap: Moves the last entry in the stack to
the second to last position and the second to
last entry to the last position.
If there are less than two entries in the stack
or there is not enough room in the stack space
to make a copy of the second to last entry, the
entries are not swapped and a value of 0 is
returned. Otherwise, the number of bytes in the
new last entry is returned.
Note: Calls stkovr, creating a copy of the second
to last stack entry, restores stklo and stkhi to
their original values, then calls the memcpy twice.
*,m,l = stkptr(); Stack Pointer: Returns the address contained in
the stack pointer as the most significant byte
and the least significant byte.
This is a utility function not normally used in
application code.
Note: Gets variables stkslo and stkshi.
stkset(&d); Stack Set: Sets stack pointer to address d.
This is a utility function not normally used in
application code.
Note: Sets variables stkslo and stkshi.
Note: This library expects the following functions to be defined
memcpl Copy memory (alternate entry point)
ressrc Restore source pointer
resrxy Restore X and Y registers
savrxy Save X and Y registers
setdst Set destination pointer
setsrc Set source pointer and initialize index
strlen Get string length
along with the zero page variable pairs
srclo, srchi Source String Pointer
dstlo, dsthi Destination String Pointer
stklo, stkhi stack Pointer
the static variable
stkslo, stkshi Stack Start Address
stkelo, stkehi Stack End Address
as well as the transient variables
temp0, temp1 Temporary storage
temp2, temp3

View File

@ -1,10 +1,8 @@
;*** Needs to be Tested and Debugged ****
;C02 library stack.h02 assembly language subroutines ;C02 library stack.h02 assembly language subroutines
;Requires External Zero Page Variables ;Requires External Zero Page Variables
;STKLO, STKHI, DSTLO, DSTHI, SRCLO, SRCHI ;STKLO, STKHI, DSTLO, DSTHI, SRCLO, SRCHI
;External Variables ;External Variables
;STKSLO, STKSHI, STKELO, STKEHI, STKLEN, TEMP0, TEMP1, TEMP2 ;STKSLO, STKSHI, STKELO, STKEHI, TEMP0, TEMP1, TEMP2, TEMP3
;External Routines ;External Routines
;MEMCPL, SETDST, SETSRC, STRLEN ;MEMCPL, SETDST, SETSRC, STRLEN
@ -47,7 +45,7 @@ STKPTR: LDX STKLO ;Load Stack Pointer LSB
STKSSP: JSR STKPTR ;Get Stack Pointer STKSSP: JSR STKPTR ;Get Stack Pointer
JMP SAVRXY ;Save in TEMP1, TEMP2 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 ;Uses: STKLO,STKHI = Stack Pointer
;Sets: SRCLO,SRCHI = Source Array Pointer ;Sets: SRCLO,SRCHI = Source Array Pointer
;Affects: X,N,Z ;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 STY STKHI ;Store Y in Stack Pointer MSB
RTS ;Exit Routine RTS ;Exit Routine
;stkalc(n) - Allocate Space on Stack ;stkstr(n, &m) - Push String onto Stack
;Args: A=Number of Bytes to Allocate ;Args: Y,X = Address of Source Array
;Updates: STKLO, STKHI = Stack Pointer STKSTR: JSR STRLEN ;Get Length of String
;Affects: C,N,Z BEQ STKSTY ;If Length > 0
;Returns: A = Number of Bytes Allocated BMI STKSTY ; and < 128
; 0 if Error: Pointer Overflow or Length 0 INY ; Increment Length to
; Y=0 TYA ; include null terminator
STKALC: LDA TEMP0 ;If No Bytes STKSTY: LDY #0 ;Clear Y Index
BEQ STKINX ; Return False BEQ STKPSA ;Execute STKPSH, skippping SETSRC
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 ;stkpsh(n, &m) - Push n Bytes of m onto Stack
;Args: A = Number of Bytes to Append ;Args: A = Number of Bytes to Append
@ -142,18 +115,50 @@ STKPSH: JSR SETSRC ;Set Source Address
STKPSA: STA TEMP0 ;Save Number of Bytes STKPSA: STA TEMP0 ;Save Number of Bytes
JSR STKDST ;Set Destination to Stack Pointer JSR STKDST ;Set Destination to Stack Pointer
JSR STKALC ;Allocate Space on Stack JSR STKALC ;Allocate Space on Stack
BEQ STKDER ;If Error Return FALSE JMP MEMCPL ;Copy Bytes and Return
JMP MEMCPL ;Else Copy Bytes and Return
;stkstr(n, &m) - Push String onto Stack ;stkadd(n) - Add N Bytes to Stack Pointer
;Args: Y,X = Address of Source Array ;Args: A=Number of Bytes to Allocate
STKSTR: JSR STRLEN ;Get Length of String ;Updates: STKLO, STKHI = Stack Pointer
BEQ STKSTY ;If Length > 0 ;Affects: A,C,N,Z
BMI STKSTY ; and < 128 STKADD: CLC
INY ; Increment Length to STKADC: ADC STKLO ;Add to Stack Pointer LSB
TYA ; include null terminator STA STKLO ;and Save
STKSTY: LDY #0 ;Clear Y Index LDA STKHI ;Add Carry
BEQ STKPSA ;Execute STKPSH, skippping SETSRC 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 ;stkcmp - Compare Stack Pointer to Stack Start Address
;Uses: STKLO, STKHI = Stack Pointer Address ;Uses: STKLO, STKHI = Stack Pointer Address
@ -179,7 +184,9 @@ STKDEL: DEC STKLO ; Decrement LSB
STKDER: RTS STKDER: RTS
;stkdal() - Deallocate Stack Entry ;stkdal() - Deallocate Stack Entry
;Note: Aborts Calling Routine if Error
;Uses: STKSLO, STKSHI = Stack Start Address ;Uses: STKSLO, STKSHI = Stack Start Address
;Sets: TEMP1, TEMP2 = Original Stack Pointer
;Updates: STKLO, STKHI = Stack Pointer ;Updates: STKLO, STKHI = Stack Pointer
;Affects: Y,C,N,Z ;Affects: Y,C,N,Z
;Returns: A=Number of Bytes in Entry ;Returns: A=Number of Bytes in Entry
@ -187,8 +194,8 @@ STKDER: RTS
; Y=0 ; Y=0
STKDAL: JSR STKSSP ;Save Stack Pointer STKDAL: JSR STKSSP ;Save Stack Pointer
STKDAN: JSR STKCMP ;If Stack Pointer STKDAN: JSR STKCMP ;If Stack Pointer
BCC STKSUZ ; <= Stack Start BCC STKRPA ; <= Stack Start
BEQ STKSUZ ; Return 0 BEQ STKRPA ; Return 0
JSR STKDEC ;Decrement Stack Pointer JSR STKDEC ;Decrement Stack Pointer
LDY #0 ;Initialize Index LDY #0 ;Initialize Index
LDA (STKLO),Y ;Get Number of Bytes in Entry LDA (STKLO),Y ;Get Number of Bytes in Entry
@ -204,11 +211,13 @@ STKSUB: STA TEMP0 ;Save Number in TEMP0
SBC #0 ;For Borrow SBC #0 ;For Borrow
STA STKHI STA STKHI
JSR STKCMP ;If Stack Pointer < Start 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 STKSUE: LDA TEMP0 ;Retrieve Number of Bytes
RTS RTS
STKSUR: JSR STKRSP ;Restore Stack Pointer STKRPA: JSR STKRSP ;Restore Stack Pointer
STKSUZ: LDA #0 ;Set A to 0 STKABT: PLA ;Drop Return Address from Stack
PLA ; Aborting Calling Routine
STKZRO: LDA #0 ;Set A to 0
RTS RTS
;stkdrp(&m) - Drop Top Entry from Stack ;stkdrp(&m) - Drop Top Entry from Stack
@ -217,7 +226,8 @@ STKSUZ: LDA #0 ;Set A to 0
;Affects: C,N,Z ;Affects: C,N,Z
;Returns: A = Number of Bytes in Dropped Entry ;Returns: A = Number of Bytes in Dropped Entry
; 0 = Error: Stack Underflow ; 0 = Error: Stack Underflow
STKDRP EQU STKDAL ;Deallocate Stack Entry STKDRP JSR STKDAL ;Deallocate Stack Entry
RTS
;stkpop(&m) - Pop Top Entry off Stack ;stkpop(&m) - Pop Top Entry off Stack
;Args: Y,X = Address of Source Array ;Args: Y,X = Address of Source Array
@ -228,7 +238,6 @@ STKDRP EQU STKDAL ;Deallocate Stack Entry
; 0 = Error: Stack Underflow ; 0 = Error: Stack Underflow
STKPOP: JSR SETDST ;Set Destination Address STKPOP: JSR SETDST ;Set Destination Address
JSR STKDAL ;Deallocate Stack Entry JSR STKDAL ;Deallocate Stack Entry
BEQ STKDER ;If Underflow, Return 0
STKPOS: JSR STKSRC ;Set Source Address to Stack Pointer STKPOS: JSR STKSRC ;Set Source Address to Stack Pointer
JMP MEMCPL ;Copy Bytes and Return JMP MEMCPL ;Copy Bytes and Return
@ -242,47 +251,54 @@ STKTOP: JSR STKPOP ;Pop Top Entry Off Stack
BEQ STKDER ;If Underflow, Return 0 BEQ STKDER ;If Underflow, Return 0
JMP STKRSP ;Else Restore Stack Pointer and Return 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 ;Uses: STKSLO, STKSHI = Stack Start Address
; STKELO, STKEHI = Stack End Address ; STKELO, STKEHI = Stack End Address
;Updates: STKLO, STKHI = Stack Pointer ;Updates: STKLO, STKHI = Stack Pointer
;Affects: C,N,Z ;Affects: C,N,Z
;Returns: A,Y = Number of Bytes in Entry ;Returns: A,Y = Number of Bytes in Entry
; 0 = Error: Stack Underflow ; 0 = Error: Stack Overflow or Underflow
STKDUP: JSR STKDAL ;Deallocate Top Entry STKDUP: JSR STKDAL ;Deallocate Top Entry
BEQ STKDER ;If Underflow, Return 0 STKDUS: JSR STKSRC ;Set Source Pointer to Stack Pointer
JSR STKSRC ;Set Source Pointer to Stack Pointer
JSR STKRSP ;Restore Stack Pointer JSR STKRSP ;Restore Stack Pointer
JMP STKSTY ;Push Top Entry onto Stack JMP STKSTY ;Push Top Entry onto Stack
;stkswp(&m) - Swap Top Entry with Entry Below it ;stkovr(&m) - Duplicate Second Entry on Stack
;Args: Y,X = Address of Tempory Array
;Uses: STKSLO, STKSHI = Stack Start Address ;Uses: STKSLO, STKSHI = Stack Start Address
; STKELO, STKEHI = Stack End Address ; STKELO, STKEHI = Stack End Address
;Updates: STKLO, STKHI = Stack Pointer ;Updates: STKLO, STKHI = Stack Pointer
;Affects: C,N,Z ;Affects: C,N,Z
;Returns: A,Y = Number of Bytes in Entry ;Returns: A,Y = Number of Bytes in Entry
; 0 = Error: Stack Underflow ; 0 = Error: Stack Overflow or Underflow
STKSWP: JSR SETDST ;Set Swap Array As Destination STKOVR: JSR STKDAL ;Deallocate Top Entry
JSR STKDAL ;Deallocate Top Entry JSR STKDAN ;Deallocate Second Entry
BEQ STKDER ;If Error, Return 0 JMP STKDUS ;Restore Pointer and Duplicate
JSR STKDAN ;Deallocate with No Save of Pointer
BEQ STKDER ;If Error, Return 0 ;stkswp() - Swap Top Entry with Entry Below it
STA TEMP3 ;Save Size of Top Entry ;Uses: STKSLO, STKSHI = Stack Start Address
JSR STKSSP ;Save Pointer to Top Entry ; STKELO, STKEHI = Stack End Address
JSR STKPOS ;Copy Second Entry to Array ;Affects: C,N,Z
PHA ;Save Size of Second Entry ;Returns: A,Y = Number of Bytes in Entry
LDA DSTHI ;Save Array Address ; 0 = Error: Stack Overflow or Underflow
PHA STKSWP: JSR STKOVR ;Duplicate Second Entry
LDA DSTLO BEQ STKDER ;Exit if Error
PHA JSR STKDAN ;Deallocate Duplicate
JSR RESRXY ;Set Source String STA TEMP3 ;Save Size of Duplicate
JSR SETSRC ;to Address of Old Top Entry JSR STKSSP ;Save Pointer to Duplicate
LDA TEMP3 ;Load Old Top Entry Size JSR STKDAN ;Deallocate Top Entry
JSR STKPSA ;Push Old Top Entry JSR STKSRC ;Set Source to Top Entry
PLA ;Restore Array Address STA DSTLO ;Save Top Entry Size
TAX JSR STKDAN ;Deallocate Second Entry
PLA LDA DSTLO ;Retrieve Top Entry Size
TAY JSR STKSWC ;Copy Top Entry Down
PLA ;Restore Size of Second Entry JSR RESSRC ;Set Duplicate as Source
JMP STKPSH ;Push Array Contents onto Stack 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

View File

@ -3,6 +3,7 @@
***********************************************/ ***********************************************/
#include <py65.h02> #include <py65.h02>
#include <stddef.h02>
#include <stdlib.h02> #include <stdlib.h02>
#include <stdio.h02> #include <stdio.h02>
#include <stdiox.h02> #include <stdiox.h02>
@ -12,7 +13,7 @@
char TRUE=$FF, FALSE=$00; char TRUE=$FF, FALSE=$00;
char f,g,i; char c,f,g,i;
char aa,xx,yy; char aa,xx,yy;
char lo,hi; char lo,hi;
char r={6,7,8,9}; char r={6,7,8,9};
@ -23,43 +24,53 @@ main:
for (i=0;i<$20;i++) srclo[i]=0; //Initialize Pointers for (i=0;i<$20;i++) srclo[i]=0; //Initialize Pointers
puts("Setting Stack Start to $7FFF\t"); puts("Setting Stack Start Address\t");
puts("stkbgn($7FFF);\t");
stkbgn(&$7FFF); lo=stkslo; hi=stkshi; stkbgn(&$7FFF); lo=stkslo; hi=stkshi;
setdst("stks"); cklohi(&$7FFF); setdst("stks"); cklohi(&$7FFF);
puts("Setting Stack End to $807E\t"); puts("Setting Stack End Address\t");
puts("stkend($807E)\t");
stkend(&$807E); lo=stkelo; hi=stkehi; stkend(&$807E); lo=stkelo; hi=stkehi;
setdst("stke"); cklohi(&$807E); setdst("stke"); cklohi(&$807E);
puts("Setting Stack Pointer to $8765\t"); puts("Setting Stack Pointer Address\t");
puts("stkset($8765)\t");
stkset(&$8765); lo=stklo; hi=stkhi; stkset(&$8765); lo=stklo; hi=stkhi;
setdst("stk"); cklohi(0,&$8765); setdst("stk"); cklohi(0,&$8765);
puts("Resetting Stack Pointer\t\t"); puts("Resetting Stack Pointer\t\t");
puts("stkrst()\t");
stkrst(); lo=stklo; hi=stkhi; stkrst(); lo=stklo; hi=stkhi;
setdst("stk"); cklohi(0,&$7FFF); setdst("stk"); cklohi(0,&$7FFF);
puts("Getting Stack Pointer Address\t"); puts("Getting Stack Pointer Address\t");
puts("stkptr()\t");
aa, hi, lo = stkptr(); aa, hi, lo = stkptr();
setdst(""); cklohi(0,&$7FFF); setdst(""); cklohi(0,&$7FFF);
puts("Setting Source to Stack Pointer\t"); puts("Setting Source to Stack Pointer\t");
puts("stksrc()\t");
stksrc(); lo=srclo; hi=srchi; stksrc(); lo=srclo; hi=srchi;
setdst("src"); cklohi(0,&$7FFF); setdst("src"); cklohi(0,&$7FFF);
puts("Setting Dest. to Stack Pointer\t"); puts("Setting Dest. to Stack Pointer\t");
puts("stkdst()\t");
stkdst(); lo=dstlo; hi=dsthi; stkdst(); lo=dstlo; hi=dsthi;
setdst("src"); cklohi(0,&$7FFF); setdst("src"); cklohi(0,&$7FFF);
puts("Incrementing Stack Pointer\t"); puts("Incrementing Stack Pointer\t");
puts("stkinc()\t");
stkinc(); lo=stklo; hi=stkhi; stkinc(); lo=stklo; hi=stkhi;
setdst("stk"); cklohi(0,&$8000); setdst("stk"); cklohi(0,&$8000);
puts("Decrementing Stack Pointer\t"); puts("Decrementing Stack Pointer\t");
puts("stkdec()\t");
stkdec(); lo=stklo; hi=stkhi; stkdec(); lo=stklo; hi=stkhi;
setdst("stk"); cklohi(0,&$7FFF); setdst("stk"); cklohi(0,&$7FFF);
puts("Pushing Array onto Stack\t"); puts("Pushing Array onto Stack\t");
puts("stkpsh(@r,$r)\t");
aa,hi,lo=stkptr(); //Save Stack Pointer aa,hi,lo=stkptr(); //Save Stack Pointer
f=stkpsh(@r,&r); printf(f,"bytes=%d:"); f=stkpsh(@r,&r); printf(f,"bytes=%d:");
setdst(*,hi,lo); g=(memcmp(@r, &r)==0) ? TRUE : FALSE; setdst(*,hi,lo); g=(memcmp(@r, &r)==0) ? TRUE : FALSE;
@ -67,6 +78,7 @@ psorfl(f & g);
chkptr(&$8004); chkptr(&$8004);
puts("Duplicating Top of Stack\t"); puts("Duplicating Top of Stack\t");
puts("stkdup()\t");
aa,hi,lo=stkptr(); //Save Stack Pointer aa,hi,lo=stkptr(); //Save Stack Pointer
f=stkdup(); printf(f,"bytes=%d:"); f=stkdup(); printf(f,"bytes=%d:");
setdst(*,hi,lo); g=(memcmp(@r, &r)==0) ? TRUE : FALSE; setdst(*,hi,lo); g=(memcmp(@r, &r)==0) ? TRUE : FALSE;
@ -74,51 +86,80 @@ psorfl(f & g);
chkptr(&$8009); chkptr(&$8009);
puts("Pushing String onto Stack\t"); puts("Pushing String onto Stack\t");
puts("stkstr(&s)\t");
aa,hi,lo=stkptr(); //Save Stack Pointer aa,hi,lo=stkptr(); //Save Stack Pointer
f=stkstr(&s); printf(f,"bytes=%d:"); f=stkstr(&s); printf(f,"bytes=%d:");
setdst(*,hi,lo); g=(strcmp(&s)==0) ? TRUE : FALSE; setdst(*,hi,lo); g=(strcmp(&s)==0) ? TRUE : FALSE;
psorfl(f & g); psorfl(f & g);
chkptr(&$8016); chkptr(&$8016);
puts("Duplicating Second Stack Entry\t");
puts("stkovr()\t");
aa,hi,lo=stkptr(); //Save Stack Pointer
f=stkovr(); printf(f,"bytes=%d:");
setdst(*,hi,lo); g=(memcmp(@r, &r)==0) ? TRUE : FALSE;
psorfl(f & g);
chkptr(&$801B);
c = anykey(); if (c == #esckey) goto exit;
puts("Dropping Duplicate off Stack\t");
puts("stkdrp()\t");
f=stkdrp(); printf(f,"bytes=%d:");
g=(f==4) ? TRUE : FALSE;
psorfl(f & g);
chkptr(&$8016);
puts("Copying String from Stack\t"); puts("Copying String from Stack\t");
puts("stktop(&t)\t");
f=stktop(&t); setdst(&t); printf("\"%s\":"); f=stktop(&t); setdst(&t); printf("\"%s\":");
g=(strcmp(&s)==0) ? TRUE : FALSE; g=(strcmp(&s)==0) ? TRUE : FALSE;
psorfl(f & g); psorfl(f & g);
chkptr(&$8016); chkptr(&$8016);
puts("Popping String off Stack\t"); puts("Popping String off Stack\t");
puts("stkpop(&t)\t");
f=stkpop(&t); setdst(&t); printf("\"%s\":"); f=stkpop(&t); setdst(&t); printf("\"%s\":");
g=(strcmp(&s)==0) ? TRUE : FALSE; g=(strcmp(&s)==0) ? TRUE : FALSE;
psorfl(f & g); psorfl(f & g);
chkptr(&$8009); chkptr(&$8009);
puts("Dropping Duplicate off Stack\t"); puts("Dropping Duplicate off Stack\t");
puts("stkdrp()\t");
f=stkdrp(); printf(f,"bytes=%d:"); f=stkdrp(); printf(f,"bytes=%d:");
g=(f==4) ? TRUE : FALSE; g=(f==4) ? TRUE : FALSE;
psorfl(f & g); psorfl(f & g);
chkptr(&$8004); chkptr(&$8004);
puts("Popping Array off Stack\t\t"); puts("Popping Array off Stack\t\t");
puts("stkpop(&t)\t");
f=stkpop(&t); prtary(f); puts(": "); f=stkpop(&t); prtary(f); puts(": ");
g=(memcmp(f,&r)==0) ? TRUE : FALSE; g=(memcmp(f,&r)==0) ? TRUE : FALSE;
psorfl(f & g); psorfl(f & g);
chkptr(&$7FFF); chkptr(&$7FFF);
puts("Popping off Empty Stack\t\t"); puts("Popping off Empty Stack\t\t");
puts("stkpop(&t)\t");
f=stkpop(&t); printf(f,"bytes=%d:"); f=stkpop(&t); printf(f,"bytes=%d:");
if (f) fail(); else pass(); if (f) fail(); else pass();
chkptr(&$7FFF); chkptr(&$7FFF);
puts("Overflowing Stack\t\t"); puts("Overflowing Stack\t\t");
puts("stkpsh(255,&t)\t");
f=stkpsh(255,&t); printf(f,"bytes=%d:"); f=stkpsh(255,&t); printf(f,"bytes=%d:");
if (f) fail(); else pass(); if (f) fail(); else pass();
chkptr(&$7FFF);
putln("Tests Complete");
goto exit; goto exit;
void chkptr(aa,yy,xx) { void chkptr(aa,yy,xx) {
puts("Checking Stack Pointer Address\t"); puts("\tChecking Stack Pointer\t\t\t");
aa, hi, lo = stkptr(); puts("address=$"); prbyte(stkhi); prbyte(stklo); putc(':');
setdst(""); cklohi(*,yy,xx); xx = (stklo == xx) ? TRUE : FALSE;
yy = (stkhi == yy) ? TRUE : FALSE;
psorfl(xx & yy);
} }
void prtary(aa) { void prtary(aa) {
@ -143,3 +184,18 @@ void prtstk() {puts(" stk");if (aa) putc(aa);}
void psorfl(aa) {if (aa) pass(); else {fail(); goto exit;}} void psorfl(aa) {if (aa) pass(); else {fail(); goto exit;}}
void pass() {putln(" Pass");} void pass() {putln(" Pass");}
void fail() {putln(" Fail");} void fail() {putln(" Fail");}
puts("Swapping Top and Second Entry\t");
puts("stkswp()\t");
f=stkswp(); printf(f,"bytes=%d:");
g=(f==12) ? TRUE : FALSE;
psorfl(f & g);
chkptr(&$801B);
puts("Dropping Swapped Entry off Stack\t");
puts("stkdrp()\t");
f=stkdrp(); printf(f,"bytes=%d:");
g=(f==4) ? TRUE : FALSE;
psorfl(f & g);
chkptr(&$8016);

146
py65/testswp.c02 Normal file
View File

@ -0,0 +1,146 @@
/***********************************************
* TESTSTK - Test stkswp() from stacks library *
***********************************************/
#include <py65.h02>
#include <stddef.h02>
#include <stdlib.h02>
#include <stdio.h02>
#include <stdiox.h02>
#include <string.h02>
#include <memory.h02>
#include <stack.h02>
char c,f,g,i;
char aa,xx,yy;
char lo,hi;
char r={6,7,8,9};
char s="Test String";
char t[255];
main:
for (i=0;i<$20;i++) srclo[i]=0; //Initialize Pointers
puts("Setting Stack Start Address\t");
puts("stkbgn($7FFF);\t");
stkbgn(&$7FFF); lo=stkslo; hi=stkshi;
setdst("stks"); cklohi(&$7FFF);
puts("Setting Stack End Address\t");
puts("stkend($807E)\t");
stkend(&$807E); lo=stkelo; hi=stkehi;
setdst("stke"); cklohi(&$807E);
puts("Resetting Stack Pointer\t\t");
puts("stkrst()\t");
stkrst(); lo=stklo; hi=stkhi;
setdst("stk"); cklohi(0,&$7FFF);
puts("Getting Stack Pointer Address\t");
puts("stkptr()\t");
aa, hi, lo = stkptr();
setdst(""); cklohi(0,&$7FFF);
puts("Pushing Array onto Stack\t");
puts("stkpsh(@r,$r)\t");
aa,hi,lo=stkptr(); //Save Stack Pointer
f=stkpsh(@r,&r); printf(f,"bytes=%d:");
setdst(*,hi,lo); g=(memcmp(@r, &r)==0) ? #TRUE : #FALSE;
psorfl(f & g);
chkptr(&$8004);
puts("Swapping with only one Entry\t");
puts("stkswp()\t");
f=stkswp(); printf(f,"bytes=%d:");
g=(f==0) ? #TRUE : #FALSE;
psorfl(g);
chkptr(&$8004);
puts("Pushing String onto Stack\t");
puts("stkstr(&s)\t");
aa,hi,lo=stkptr(); //Save Stack Pointer
f=stkstr(&s); printf(f,"bytes=%d:");
setdst(*,hi,lo); g=(strcmp(&s)==0) ? #TRUE : #FALSE;
psorfl(f & g);
chkptr(&$8011);
puts("Setting Stack End Address\t");
puts("stkend($8013)\t");
stkend(&$8013 ); lo=stkelo; hi=stkehi;
setdst("stke"); cklohi(&$8013);
puts("Swapping with Unsufficient Space\t");
puts("stkswp()\t");
f=stkswp(); printf(f,"bytes=%d:");
g=(f==0) ? #TRUE : #FALSE;
psorfl(g);
chkptr(&$8011);
puts("Setting Stack End Address\t");
puts("stkend($807E)\t");
stkend(&$807E); lo=stkelo; hi=stkehi;
setdst("stke"); cklohi(&$807E);
puts("Swapping Top and Second Entry\t");
puts("stkswp()\t");
f=stkswp(); printf(f,"bytes=%d:");
g=(f==4) ? #TRUE : #FALSE;
psorfl(f & g);
chkptr(&$8011);
puts("Popping Array off Stack\t\t");
puts("stkpop(&t)\t");
f=stkpop(&t); prtary(f); puts(": ");
g=(memcmp(f,&r)==0) ? #TRUE : #FALSE;
psorfl(f & g);
chkptr(&$800C);
puts("Popping String off Stack\t");
puts("stkpop(&t)\t");
f=stkpop(&t); setdst(&t); printf("\"%s\":");
g=(strcmp(&s)==0) ? #TRUE : #FALSE;
psorfl(f & g);
chkptr(&$7FFF);
puts("Swapping with no entries\t");
puts("stkswp()\t");
f=stkswp(); printf(f,"bytes=%d:");
g=(f==0) ? #TRUE : #FALSE;
psorfl(g);
chkptr(&$7FFF);
putln("Tests Complete");
goto exit;
void chkptr(aa,yy,xx) {
puts("\tChecking Stack Pointer\t\t\t");
puts("address=$"); prbyte(stkhi); prbyte(stklo); putc(':');
xx = (stklo == xx) ? #TRUE : #FALSE;
yy = (stkhi == yy) ? #TRUE : #FALSE;
psorfl(xx & yy);
}
void prtary(aa) {
putc('{');
for (i=0;i<aa;i++) {
if (i) putc(',');
putdec(t[i]);
}
putc('}');
}
void cklohi(aa,yy,xx) {
putdst(); puts("lo=$"); prbyte(lo); putc(' ');
putdst(); puts("hi=$"); prbyte(hi); putc(':');
xx = (lo == xx) ? #TRUE : #FALSE;
yy = (hi == yy) ? #TRUE : #FALSE;
psorfl(xx & yy);
}
void prtstk() {puts(" stk");if (aa) putc(aa);}
void psorfl(aa) {if (aa) pass(); else {fail(); goto exit;}}
void pass() {putln(" Pass");}
void fail() {putln(" Fail");}