Tested and debugged library memio

This commit is contained in:
Curtis F Kaylor 2018-07-18 23:47:50 -04:00
parent 4dbeeb5274
commit 33118c0222
4 changed files with 178 additions and 164 deletions

View File

@ -124,8 +124,9 @@ MPUTC: JSR MERRON ;Save Argumebts & Check Memory Pointer
;Sets: TEMP0 = Memory Pointer
; TEMP1 = Number of characters read
;Affects: N,Z reflect Accumulator
;Returns: A,Y = 255 if invalid Memory Pointer
; otherwise length of string read
;Returns: A = 255 if invalid Memory Pointer
; otherwise length of string read
; Y = Last character in string
; X = Zero Page Memory Pointer
MGETS: JSR SETDST ;Set String Address as Destination
JSR MERROM ;Save Memory Pointer & Check Memory Pointer
@ -136,14 +137,13 @@ MGETS: JSR SETDST ;Set String Address as Destination
STA SRCHI ;to Source Pointer MSB
MGETSL: LDA (SRCLO),Y ;Get Character from Memory
BEQ MGETSX ;If Not NUL (End of File)
CMP #$0A ; If Control Character
BNE MGETSS
INY ; Increment Past Carriage Return
BNE MGETSX ; Else
MGETSS: STA (DSTLO),Y ; Store Character in String
INY ; increment offset and
CMP #32 ; Check for Control Character
MGETSS: STA (DSTLO),Y ; Store Character in String
INY ; increment offset and
BCC MGETSX ; If Not Control character
BPL MGETSL ; loop if less than 128
MGETSX: LDA #$00 ;Terminate String
MGETSX: TAX ;Copy Final Character to X
LDA #$00 ;Terminate String
STA (DSTLO),Y ;
BEQ MPUTSX ;Update Memory Pointer and Return String Length
@ -153,8 +153,9 @@ MGETSX: LDA #$00 ;Terminate String
;Sets: TEMP0 = Memory Pointer
; TEMP1 = Number of characters written
;Affects: N,Z reflect Accumulator
;Returns: A,Y = 255 if invalid Memory Pointer
; otherwise length of string written
;Returns: A = 255 if invalid Memory Pointer
; otherwise length of string written
; Y = Last character in string
; X = Zero Page Memory Pointer
MPUTS: JSR SETSRC ;Set Source Pointer
JSR MERROM ;Save Memory Pointer & Check Memory Pointer
@ -165,12 +166,15 @@ MPUTS: JSR SETSRC ;Set Source Pointer
STA DSTHI ;to Source Pointer MSB
MPUTSL: LDA (SRCLO),Y ;Get Character from Memory
BEQ MPUTSX ;If Not End of String
STA (DSTLO),Y ; Store Character in Memory
INY ; increment offset and
BPL MPUTSL ; loop if less than 128
MPUTSX: LDX TEMP0 ;Retrieve Memory Pointer
STY TEMP1 ;Copy String Length to Accumulator
MPUTSY: LDA 0,X ;Get to Memory Pointer LSB
STA (DSTLO),Y ; Store Character in Memory
TAX ; and copy into X Register
INY ; Increment offset and
BPL MPUTSL ; loop if less than 128
MPUTSX: TXA ;Copy Final Character into Accumulator
LDX TEMP0 ;Retrieve Memory Pointer
STY TEMP1 ;Save String Length in TEMP1
TAY ;Copy Final Character into Y-Register
MPUTSY: LDA 0,X ;Get Memory Pointer LSB
CLC
ADC TEMP1 ;Add String Length
STA 0,X
@ -180,7 +184,7 @@ MGETSZ: LDA TEMP1 ;Load String Length and Set Flags
RTS
;char mputln(&s) - Write Line to Memory File
MPUTLN: JSR PUTS ;Write string to screen
MPUTLN: JSR MPUTS ;Write String to Memory File
LDA TEMP0 ;Retrieve Memory Pointer
LDY #RTNKEY ;Load Carriage Return
JMP MPUTC ;Write to Memory File
@ -188,9 +192,6 @@ MPUTLN: JSR PUTS ;Write string to screen
;mdst(&a) - Set Destination Array for mread()
MDST EQU SETDST
;msrc(&a) - Set Source Array for mwrite()
MSRC EQU SETSRC
;char mread(mp, n) - Read from Memory File into Array
;Args: A = Zero Page Memory Pointer
; Y = Number of Bytes to Read
@ -206,7 +207,7 @@ MREAD: JSR MERRON ;Save Argumebts & Check Memory Pointer
STA SRCLO ;to Source Pointer LSB
LDA 1,X ;Copy Memory Pointer MSB
STA SRCHI ;to Source Pointer MSB
BNE MWRITC ;Execute Copy
BNE MWRITL ;Execute Copy
;msrc(&a) - Set Source Array for mwrite()
MSRC EQU SETSRC
@ -226,9 +227,9 @@ MWRITE: JSR MERRON ;Save Argumebts & Check Memory Pointer
STA DSTLO ;to Destination Pointer LSB
LDA 1,X ;Copy Memory Pointer MSB
STA DSTHI ;to Destination Pointer MSB
MWRITC: LDY TEMP1 ;Set Index to Number of Bytes to Read
MWRITL: DEY ;Decrement Index
BEQ MPUTSY ;If Zero, Update Memory Pointer and Return
LDA (SRCLO),Y ;Read Byte from Memory
MWRITL: LDA (SRCLO),Y ;Read Byte from Memory
STA (DSTLO),Y ;Write Byte to Array
JMP MWRITL ;and Loop
INY ;Increment Index
CPY TEMP1 ;If Not Equal to Number of Bytes to Read
BNE MWRITL ; Loop
BEQ MPUTSY ;Else Update Memory Pointer

View File

@ -1,13 +1,13 @@
/******************************************
* memio - Functions to read/write memory *
/*****************+*************************
* memio - Functions to read/write memory *
******************************************/
/* Return Memory File Pointer Contents *
* Args: mp - memory file pointer *
* Returns: address contained in pointer *
LSB, MSB as a tuple */
char mclose();
* Returns: Tuple *
memory pointer address LSB *
memory pointer address MSB */
char maddr();
/* Open Memory File *
* Sets starting address to read/write *
@ -66,8 +66,10 @@ char mputc();
* until C/R or EOF is found *
* Args: mp - memory file pointer *
* &s - string read from memory *
* Returns: length of string *
* 255 if error during read */
* Returns: Tuple *
* length of string *
* (255 if error during read) *
* last character read */
char mgets();
/* Write String to Memory File *
@ -102,7 +104,6 @@ char mread();
* Args: &a - Source array */
void msrc();
/* Write Bytes to Memory File *
* Args: mp - memory file pointer *
* n - number of bytes to write *

View File

@ -1,122 +1,8 @@
/*******************************************
* TESTMEM - Test Array Handling Functions *
*******************************************/
/****************************************
* TESTMIO - Test Memory File Functions *
****************************************/
#include <py65.h02>
#include <stdio.h02>
#include <memory.h02>
char TRUE = $FF, FALSE = 0;
char c, d, f, i, n, p;
char rlen, rcmp, rpos;
char byts[255];
char lttr[255];
char nmbr[255];
char temp[255];
char dest[255];
char less = "less";
char more = "more";
char most = "most";
char fail = " Fail ";
char pass = " Pass ";
void prtemp() {
puts("temp: ");
for (i=0; i<255; i++) {
prbyte(temp[i]);
//putchr(' ');
}
}
main:
//Populate arrays
i = 0; c = 'a'; d = '0';
do {
byts[i] = i;
lttr[i] = c;
c++; if (c>'z') c = 'a';
nmbr[i] = d;
d++; if (d>'9') d = '0';
i++;
} while (i>0);
//Test memchr()
putln("memdst(&byts);"); memdst(&byts);
puts("memchr(i, 255);");
p = TRUE;
for (i=0; i<255; i++) {
//prbyte(i); prchr('='); prbyte(byts[i]); prchr(',');
c = memchr(i,255);
//prbyte(c); prchr(' '); prchr(' ');
if (c <> i) p = FALSE;
}
if (p) putln(&pass); else putln(&fail);
newlin();
//Test memcmp()
puts("less="); puts(&less);
puts(" more="); puts(&more);
puts(" most="); putln(&most);
putln("memdst(&more);");
memdst(&more);
puts("memcmp(2, &most):");
rcmp = memcmp(2, &most);
if (!rcmp) puts(&pass); else puts(&fail);
puts("memcmp(4, &most):");
rcmp = memcmp(4, &most);
if (rcmp :-) putln(&pass); else putln(&fail);
puts("memcmp(3, &more):");
rcmp = memcmp(3, &more);
if (!rcmp) puts(&pass); else puts(&fail);
puts("memcmp(3, &less):");
rcmp = memcmp(3, &less);
if (rcmp > 0) putln(&pass); else putln(&fail);
newlin();
//Test memset() and memcpy()
putln("memdst(&temp);");
memdst(&temp);
puts("memset(0,20); ");
memset(0,20);
puts("temp=");
putln(&temp);
puts("memset('@',20); ");
memset('@',20);
puts("temp=");
putln(&temp);
puts("memcpy(10, &lttr); ");
memcpy(10, &lttr);
puts("temp=");
putln(&temp);
puts("memcmp(10, &lttr);");
rcmp = memcmp(10, &lttr);
if (!rcmp) putln(&pass); else putln(&fail);
puts("memcmp(11, &lttr);");
rcmp = memcmp(11, &lttr);
if (rcmp > 0) putln(&pass); else putln(&fail);
newlin();
memdst(&temp); memcpy("ABCDEF");
memdst(&dest); memcpy("123456");
puts("memdst(&dest);memswp(3,&temp);");
memswp(3,&temp);
rcmp = memcmp(6,"ABC456");
memdst(&temp); rcmp = memcmp(6,"123DEF") | rcmp;
if (rcmp) putln(&fail); else putln(&pass);
goto exit;
#include <mio.h02>

View File

@ -6,12 +6,22 @@
#include <stdlib.h02>
#include <stdio.h02>
#include <stdiox.h02>
#include <memory.h02>
#include <memio.h02>
#include <string.h02>
char zp = $80; //Zero Page Location for Memory Pointer
char lsb, msb; //Memory Pointer Contents
char passed; //Flags
char c, e, i, mp, s[128];
char f[255]; //Array to use as file
char fp,mp,op; //Memory File Pointers
char c, e, i; //Testing Variables
char s[128]; //Array for String Read/Writes
char digits = "0123456789";
char upcase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char locase = "abcdefghijklmnopqrstuvwxyz";
alias char outfil = $9000; //Output File
main:
@ -37,20 +47,136 @@ e = merror(mp);
prbyte(e);
if (e) { puts(" Yes"); fail(); }
else { puts(" No "); pass(); }
newlin();
putln("Reading File using mgetc()");
do {
lsb, msb = maddr(mp);
while (!meof(mp)) {
c = mgetc(mp);
e = meof(mp);
prbyte(msb); prbyte(lsb); puts(": ");
prbyte(c); putc(' ');
if (c>' ') putc(c); else putc(' ');
puts(" EOF: "); prbyte(e); newlin();
} while (!e);
if (c < 32 ) c = 32;
putc(c);
}
newlin();
anykey();
putln("Reading File using mgets()");
mp = mopen(zp, &$8000);
while (!meof(mp)) {
lsb, msb = maddr(mp);
prbyte(msb); prbyte(lsb);
i, c = mgets(mp, &s);
printf(i, " %r");
printf(c, " %h: ");
puts(&s);
if (c <> $0A) newlin();
}
anykey();
/* Test mputc() with mopen() to array */
putln("Opening array f as memory file");
fp = mopen(zp+2, &f);
puts(" Memory pointer: "); prbyte(fp);
if (merror(fp)) fail(); else pass();
putln("Writing file using mgetc()");
for (i=32; i<127; i++) {putc(i); mputc(fp,i);}
newlin(); mputc(fp, 13); //Carriage Return
putln("Checking Array Contents");
passed = 1;
for (i=0; i<95; i++) {c = f[i]; putc(c); if (i+32<>c) passed = 0;}
newlin(); //if (f[95]<>13) passed = 0;
if (passed) pass(); else fail();
anykey();
/* Test mputs(), mputln(), and mclose() with mopen() to alias */
putln("Filling memory area outfil");
for (i = 0; i<255; i++) outfil[i] = '@';
putln("Opening location outfil as memory file");
op = mopen(zp+4, &outfil);
puts(" Memory pointer: "); prbyte(op);
if (merror(op)) fail(); else pass();
puts("Writing upcase to file using mputln():");
mputln(op, &upcase);
if (merror(op)) fail(); else pass();
puts("Writing locase to file using mputln():");
mputln(op, &locase);
if (merror(op)) fail(); else pass();
puts("Writing digits to file using mputs():");
mputs(op, &digits);
if (merror(op)) fail(); else pass();
puts("Flushing file:");
if (mflush(op)) fail(); else pass();
anykey();
/* Test mgetc(), and mgets() with mopen() to alias */
putln("Opening location outfil as memory file");
op = mopen(zp+4, &outfil);
puts(" Memory pointer: "); prbyte(op);
if (merror(op)) fail(); else pass();
puts("Reading from file using mgetc(): ");
i=0; while () {c=mgetc(op);s[i]=c;if (c<' ') break; i++;} s[i]=0;
puts(&s); strdst(&upcase); if (strcmp(&s)) fail(); else pass();
puts("Reading from file using mgets(): ");
i,c = mgets(op, &s); putln(&s);
printf(i, " Characters read: %d"); if (strlen(&locase)+1 == i) pass(); else fail();
printf(c, " Last char read: %h"); if (c == 13) pass(); else fail();
puts("Reading from file using mgets(): ");
i,c = mgets(op, &s); putln(&s);
printf(i, " Characters read: %d"); if (strlen(&digits) == i) pass(); else fail();
printf(c, " Last char read: %h"); if (c) fail(); else pass();
puts("Checking for End of File: ");
e = meof(op); putdec(e); if (e) pass(); else fail();
puts("Closing File:");
mclose(op); if (merror(op)) pass(); else fail();
anykey();
putln("Clearing memory at $9000.");
memdst(&$9000);
memset(0, 255);
mp = 0; //Initialize Memory Pointer
putln("Opening memory file at $9000.");
mp = mopen(zp, &$9000);
putln("Writing to memory file using mwrite()");
msrc(&digits); mwrite(mp, 10);
msrc(&upcase); mwrite(mp, 26);
msrc(&locase); mwrite(mp, 26);
newlin();
putln("Opening memory file at $9000.");
mp = mopen(zp, &$9000);
putln("Reading from memory file using mread()");
iarray(&s); mread(mp, 10); puts(&s); putc(':'); if (strcmp(&digits)) pass(); else fail();
iarray(&s); mread(mp, 26); puts(&s); putc(':'); if (strcmp(&upcase)) pass(); else fail();
iarray(&s); mread(mp, 26); puts(&s); putc(':'); if (strcmp(&locase)) pass(); else fail();
mread(mp,0); putdec(s); putc(':'); if (s) fail(); else pass();
goto exit;
void anykey() {newlin(); putln("Press any key..."); getc(); newlin();}
void pass() { putln(" Passed"); }
void fail() { putln(" Failed"); }
void iarray() { memdst(); memset(0, 255); }