1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-06-26 05:29:32 +00:00

Updated library 'stack'

This commit is contained in:
Curtis F Kaylor 2018-07-26 10:33:03 -04:00
parent 3f03f71857
commit 8ab138777c
3 changed files with 272 additions and 22 deletions

View File

@ -29,15 +29,24 @@ STKEND: STX STKELO ;Save Stack End LSB
STY STKEHI ;Save Stack End MSB
RTS
;STKPTR() - Get Stack Pointer
;stkptr() - Get Stack Pointer
;Uses: STKLO,STKHI = Stack Pointer
;Affects: X,N,Z
;Affects: 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
;stkssp() - Save Stack Pointer
;Uses: STKLO,STKHI = Stack Pointer
;Sets: TEMP1.TEMP2 = Stack Pointer
;Affects: N,Z
;Returns: X = Stack Pointer LSB
; Y = Stack Pointer MSB
STKSSP: JSR STKPTR ;Get Stack Pointer
JMP SAVRXY ;Save in TEMP1, TEMP2
;STKSRC() - Set Source Pointer to Stack Pointer
;Uses: STKLO,STKHI = Stack Pointer
;Sets: SRCLO,SRCHI = Source Array Pointer
@ -63,6 +72,16 @@ STKDST: JSR STKPTR ;Get Stack Pointer Address
; Y = Stack Pointer MSB
STKRST: LDX STKSLO ;Load X with Stack Start LSB
LDY STKSHI ;Load X with Stack Start MSB
JMP STKSET ;Store in Stack Pointer
;stkrsp() - Restore Stack Pointer
;Uses: TEMP1.TEMP2 = Stack Pointer
;Sets: STKLO,STKHI = Stack Pointer
;Affects: N,Z
;Returns: X = Stack Pointer LSB
; Y = Stack Pointer MSB
STKRSP: JSR RESRXY ;Get TEMP1, TEMP2
;and fall into STKSET
;stkset() - Set Stack Pointer Address
;Args: X,Y = Address
@ -74,7 +93,7 @@ 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
;stkalc(n) - Allocate Space on Stack
;Args: A=Number of Bytes to Allocate
;Updates: STKLO, STKHI = Stack Pointer
;Affects: C,N,Z
@ -126,6 +145,16 @@ STKPSA: STA TEMP0 ;Save Number of Bytes
BEQ STKDER ;If Error Return FALSE
JMP MEMCPL ;Else 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
;stkcmp - Compare Stack Pointer to Stack Start Address
;Uses: STKLO, STKHI = Stack Pointer Address
; STKSLO, STKSHI = Stack Start Address
@ -152,15 +181,16 @@ 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
;Affects: Y,C,N,Z
;Returns: A=Number of Bytes in Entry
; 0=Error: Stack Underflow
; Y=0
STKDAL: JSR STKSSP ;Save Stack Pointer
STKDAN: JSR STKCMP ;If Stack Pointer
BCC STKSUZ ; <= Stack Start
BEQ STKSUZ ; Return 0
JSR STKDEC ;Decrement Stack Pointer
LDY #0 ;Initialize Index
LDA (STKLO),Y ;Get Number of Bytes in Entry
;and fall into STKSUB
@ -173,13 +203,22 @@ STKSUB: STA TEMP0 ;Save Number in TEMP0
LDA STKHI ;Subtract 0 from LSB
SBC #0 ;For Borrow
STA STKHI
STKSUC: JSR STKCMP ;If Stack Pointer LSB
BCC STKSUE ; < Stack Pointer
JSR STKCMP ;If Stack Pointer < Start
BCC STKSUR ; 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
RTS
;stkdrp(&m) - Drop Top Entry from Stack
;Uses: STKSLO, STKSHI = Stack Start Address
;Updates: STKLO, STKHI = Stack Pointer
;Affects: C,N,Z
;Returns: A = Number of Bytes in Dropped Entry
; 0 = Error: Stack Underflow
STKDRP EQU STKDAL ;Deallocate Stack Entry
;stkpop(&m) - Pop Top Entry off Stack
;Args: Y,X = Address of Source Array
;Uses: STKSLO, STKSHI = Stack Start Address
@ -190,15 +229,60 @@ STKSUZ: LDA #0 ;Set A to 0
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
STKPOS: JSR STKSRC ;Set Source Address to Stack Pointer
JMP MEMCPL ;Copy Bytes and Return
;stkstr(n, &m) - Push String onto Stack
;stktop(&m) - Copy Top Entry off 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
;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 STKDER ;If Underflow, Return 0
JMP STKRSP ;Else Restore Stack Pointer and Return
;stkdup(&m) - Duplicate Top Entry off 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
STKDUP: JSR STKDAL ;Deallocate Top Entry
BEQ STKDER ;If Underflow, Return 0
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
;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

View File

@ -6,15 +6,30 @@
* Args: &addr - Start Address */
void stkbgn();
/* Drop Top Entry from Stack *
* Returns: A=Number of bytes dropped *
* 0 if none */
char stkdrp();
/* Set Destination to Stack Pointer *
* Returns: Y,X=Pointer Address */
void stkdst();
/* Duplicate Top Stack Entry *
* Returns: A=Number of bytes retrieved *
* 0 if none or overflow */
char stkdup();
/* Set Stack End Address *
* Args: &addr - End Address */
void stkend();
/* Pop Array from Contents from Stack *
/* Duplicate Entry Under Top of Stack *
* Returns: A=Number of bytes retrieved *
* 0 if none or overflow */
char stkovr();
/* Pop Top Stack Entry into Array *
* Args: n - Number of bytes to write *
* &m - Array to pop contents into *
* Returns: A=Number of bytes retrieved *
@ -46,4 +61,10 @@ void stkset();
* $00 if block was overflowed */
char stkstr();
void stkrst();
/* Copy Top Stack Entry into Array *
* Args: n - Number of bytes to write *
* &m - Array to pop contents into *
* Returns: A=Number of bytes retrieved *
* 0 if none */
char stktop();

145
py65/teststk.c02 Normal file
View File

@ -0,0 +1,145 @@
/***********************************************
* TESTSTK - Test Stack Manipulation Functions *
***********************************************/
#include <py65.h02>
#include <stdlib.h02>
#include <stdio.h02>
#include <stdiox.h02>
#include <string.h02>
#include <memory.h02>
#include <stack.h02>
char TRUE=$FF, FALSE=$00;
char 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 to $7FFF\t");
stkbgn(&$7FFF); lo=stkslo; hi=stkshi;
setdst("stks"); cklohi(&$7FFF);
puts("Setting Stack End to $807E\t");
stkend(&$807E); lo=stkelo; hi=stkehi;
setdst("stke"); cklohi(&$807E);
puts("Setting Stack Pointer to $8765\t");
stkset(&$8765); lo=stklo; hi=stkhi;
setdst("stk"); cklohi(0,&$8765);
puts("Resetting Stack Pointer\t\t");
stkrst(); lo=stklo; hi=stkhi;
setdst("stk"); cklohi(0,&$7FFF);
puts("Getting Stack Pointer Address\t");
aa, hi, lo = stkptr();
setdst(""); cklohi(0,&$7FFF);
puts("Setting Source to Stack Pointer\t");
stksrc(); lo=srclo; hi=srchi;
setdst("src"); cklohi(0,&$7FFF);
puts("Setting Dest. to Stack Pointer\t");
stkdst(); lo=dstlo; hi=dsthi;
setdst("src"); cklohi(0,&$7FFF);
puts("Incrementing Stack Pointer\t");
stkinc(); lo=stklo; hi=stkhi;
setdst("stk"); cklohi(0,&$8000);
puts("Decrementing Stack Pointer\t");
stkdec(); lo=stklo; hi=stkhi;
setdst("stk"); cklohi(0,&$7FFF);
puts("Pushing Array onto Stack\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("Duplicating Top of Stack\t");
aa,hi,lo=stkptr(); //Save Stack Pointer
f=stkdup(); printf(f,"bytes=%d:");
setdst(*,hi,lo); g=(memcmp(@r, &r)==0) ? TRUE : FALSE;
psorfl(f & g);
chkptr(&$8009);
puts("Pushing String onto Stack\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(&$8016);
puts("Copying String from Stack\t");
f=stktop(&t); setdst(&t); printf("\"%s\":");
g=(strcmp(&s)==0) ? TRUE : FALSE;
psorfl(f & g);
chkptr(&$8016);
puts("Popping String off Stack\t");
f=stkpop(&t); setdst(&t); printf("\"%s\":");
g=(strcmp(&s)==0) ? TRUE : FALSE;
psorfl(f & g);
chkptr(&$8009);
puts("Dropping Duplicate off Stack\t");
f=stkdrp(); printf(f,"bytes=%d:");
g=(f==4) ? TRUE : FALSE;
psorfl(f & g);
chkptr(&$8004);
puts("Popping Array off Stack\t\t");
f=stkpop(&t); prtary(f); puts(": ");
g=(memcmp(f,&r)==0) ? TRUE : FALSE;
psorfl(f & g);
chkptr(&$7FFF);
puts("Popping off Empty Stack\t\t");
f=stkpop(&t); printf(f,"bytes=%d:");
if (f) fail(); else pass();
chkptr(&$7FFF);
puts("Overflowing Stack\t\t");
f=stkpsh(255,&t); printf(f,"bytes=%d:");
if (f) fail(); else pass();
goto exit;
void chkptr(aa,yy,xx) {
puts("Checking Stack Pointer Address\t");
aa, hi, lo = stkptr();
setdst(""); cklohi(*,yy,xx);
}
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");}