From 33118c022269e08d975a6353b357f28e4cb5697f Mon Sep 17 00:00:00 2001 From: Curtis F Kaylor Date: Wed, 18 Jul 2018 23:47:50 -0400 Subject: [PATCH] Tested and debugged library memio --- include/memio.a02 | 55 ++++++++--------- include/memio.h02 | 19 +++--- py65/testmem.c02 | 122 ++------------------------------------ py65/testmio.c02 | 146 ++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 178 insertions(+), 164 deletions(-) diff --git a/include/memio.a02 b/include/memio.a02 index 32a3e50..910dc4e 100644 --- a/include/memio.a02 +++ b/include/memio.a02 @@ -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 diff --git a/include/memio.h02 b/include/memio.h02 index 033a3d5..7004fa3 100644 --- a/include/memio.h02 +++ b/include/memio.h02 @@ -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 * diff --git a/py65/testmem.c02 b/py65/testmem.c02 index aa450e5..ced20a1 100644 --- a/py65/testmem.c02 +++ b/py65/testmem.c02 @@ -1,122 +1,8 @@ -/******************************************* - * TESTMEM - Test Array Handling Functions * - *******************************************/ +/**************************************** + * TESTMIO - Test Memory File Functions * + ****************************************/ #include #include -#include - -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, <tr); "); -memcpy(10, <tr); - -puts("temp="); -putln(&temp); - -puts("memcmp(10, <tr);"); -rcmp = memcmp(10, <tr); -if (!rcmp) putln(&pass); else putln(&fail); - -puts("memcmp(11, <tr);"); -rcmp = memcmp(11, <tr); -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 diff --git a/py65/testmio.c02 b/py65/testmio.c02 index 7ec21bf..252b4e3 100644 --- a/py65/testmio.c02 +++ b/py65/testmio.c02 @@ -6,12 +6,22 @@ #include #include #include +#include #include +#include 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); }