Save include/memio.a02 before modifying MPUTS

This commit is contained in:
Curtis F Kaylor 2018-07-08 01:38:36 -04:00
parent 1741dd8f64
commit 84cf778964
7 changed files with 391 additions and 25 deletions

View File

@ -98,7 +98,7 @@ PRHEXC: ADC #$30 ;Convert to ASCII Character
;Functions to set String Pointers
;Used by memory, stdio, stdlin, string, and stringx libraries
;Initialize Destination String Pointer and Index
;Initialize Destination String Pointer
SETDST: STX DSTLO ;Save Destination String Pointer
STY DSTHI
RTS

234
include/memio.a02 Normal file
View File

@ -0,0 +1,234 @@
; C02 library memio.h02 assembly language subroutines
; Requires external routines SETDST, SETSRC
; zero page locations DSTLO, DSTHI, SRCLO and SRCHI
; location TEMP0
; and constant RTNKEY
;char maddr(mp) - Return Contents of Memory Pointer
;Args: A = Zero Page Memory Pointer
;Affects: N,Z reflect Y register
;Returns: A = Memory Pointer LSB
; Y = Memory Pointer MSB
; X = Zero Page Memory Pointet Address
MADDR: TAX ;Copy Pointer Address to X Register
LDA 0,X ;Load Pointer LSB into Accumulator
LDY 1,X ;Load Pointer MSB into Y Register
RTS
;char mopen(mp, &a) - Open Memory File: Set Memory Pointer to specified Address
;Args: A = Zero Page Memory Pointer
; X,Y = Address
;Sets: TEMP0 = Memory Pointer Address
;Affects: N,Z reflect Accumulator
;Returns: A = Zero Page Memory Pointer, otherwise
; 0 if Error Illegal Address (within Zero Page)
MOPEN: STX TEMP0 ;Save Address LSB
TAX ;Copy Memory Pointer to X register
LDA TEMP0 ;Retrieve Address LSB
STA 0,X ;Store in Memory Pointer LSB
STY 1,X ;Store Address MSB in Memory Pointer MSB
LDA 1,X ;Load Address MSB into Accumulator (Setting Flags)
BEQ MERROX ;If 0, Return as Result (Failure)
TXA ;Else
RTS ; Return Memory Pointer as Result
;char meof(mp) - Check for End of Memory File
;Args: A = Zero Page Memory Pointer
;Affects: N,Z reflect Accumulator
;Returns: A,Y = 255 if at End of File or invalid Memory Pointer
; 0 otherwise
; X = Zero Page Memory Pointer
MEOF: JSR MERROR ;Check Memory Pointer
BNE MCLOSX ;If Error, Return Else Y will contain 0
LDA (0,X) ;If NUL Character at Memory Pointer
BEQ MERROY ; Return 255
BNE MERROX ;Else Return 0
;char merror(mp) - Check for Memory File Error
;Args: A = Zero Page Memory Pointer
;Affects: N,Z reflect Accumulator
;Returns: A,Y = 255 if Memory Pointer contains Zero Page Address
; 0 if no error
; X = Zero Page Memory Pointer
MERRON: STY TEMP1 ;Alternate Entry Point - Save Byte Argument
MERROM: STA TEMP0 ;Alternate Entry Point - Save Memory Pointer
MERROR: TAX ;Copy Memory Pointer to X register
MERROA: LDY #0 ;Set Error Indicator to 0
LDA 1,X ;Load Memory Address High Byte
BEQ MERROY ;If 0
CMP #255 ; or
BNE MERROX ; 255
MERROY: DEY ; Change Error Indicator to 255
MERROX: TYA ;Copy Error Indicator to Accumulator
RTS
;char mclose(mp) - Close Memory File: Write EOF and Clear Memory Pointer
;Args: A = Zero Page Memory Pointer
;Affects: N,Z reflect Accumulator
;Returns: A,Y = 255 if Memory Pointer contains Zero Page Address
; 0 if no error
; X = Zero Page Memory Pointer
MCLOSE: JSR MFLUSH ;Check Memory Pointer
BNE MCLOSX ;If No Error
STA 0,X ; Write to Addess LSB
STA 1,X ; Write to Addess LSB
MCLOSX: RTS
;char mflush(mp) - Flush Memory File: Write NUL at Memory Pointer
;Args: A = Zero Page Memory Pointer
;Affects: N,Z reflect Accumulator
;Returns: A,Y = 255 if Memory Pointer contains Zero Page Address
; 0 if no error
; X = Zero Page Memory Pointer
MFLUSH: JSR MERROR ;Check Memory Pointer
BNE MCLOSX ;If No Error
STA (0,X) ; to Current Memory Location
MFLUSX: RTS
;char mgetc(mp) - Read Character from Memory File
;Args: A = Zero Page Memory Pointer
;Affects: N,Z reflect Accumulator
;Returns: A = Character read from memory
; Y = 255 if Error or EOF
; 0 if no error
; X = Zero Page Memory Pointer
MGETC: JSR MERROR ;Check Memory Pointer
BNE MGETCX ;If No Error
LDA (0,X) ; Load Character at Memory Location
MGETCI: PHP ; Save Flags
INC 0,X ; Increment Memory Pointer Low Byte
BNE MGETCP ; If 0
INC 1,X ; Increment Memory Pointer High Byte
MGETCP: PLP ; Restore Flags
MGETCX: RTS
;char mputc(mp, c) - Write Character to Memory File
;Args: A = Zero Page Memory Pointer
; Y = Character to Write to Memory
;Sets: TEMP0 = Character to Write
;Affects: N,Z reflect Accumulator
;Returns: A,Y = 255 if invalid Memory Pointer
; 0 if no error
; X = Zero Page Memory Pointer
MPUTC: JSR MERRON ;Save Argumebts & Check Memory Pointer
BNE MGETCX ;If Error, then Return
LDA TEMP1 ;Retrieve Character to Write
STA (0,X) ;Store in Memory
LDA #0 ;Clear Accumulator, Z and N flags
BEQ MGETCI ;and Increment Memory Pointer
;char mgets(mp, &s) - Read from Memory File into String
;Args: A = Zero Page Memory Pointer
; Y,X = Address of String to Read into
;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
; X = Zero Page Memory Pointer
MGETS: JSR SETDST ;Set String Address as Destination
JSR MERROM ;Save Memory Pointer & Check Memory Pointer
BNE MGETCX ;If Error then Return, Else Y will be 0
LDA 0,X ;Copy Memory Pointer LSB
STA SRCLO ;to Source Pointer LSB
LDA 1,X ;Copy Memory Pointer MSB
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
BPL MGETSL ; loop if less than 128
MGETSX: LDA #$00 ;Terminate String
STA (DSTLO),Y ;
BEQ MPUTSX ;Update Memory Pointer and Return String Length
;char mputs(mp, &s) - Write String to Memory File
;Args: A = Zero Page Memory Pointer
; Y,X = Address of String to Read into
;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
; X = Zero Page Memory Pointer
MPUTS: JSR SETSRC ;Set Source Pointer
JSR MERROM ;Save Memory Pointer & Check Memory Pointer
BNE MGETCX ;If Error then Return, Else Y will be 0
LDA 0,X ;Copy Memory Pointer LSB
STA DSTLO ;to Source Pointer LSB
LDA 1,X ;Copy Memory Pointer MSB
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
CLC
ADC TEMP1 ;Add String Length
STA 0,X
BCC MGETSZ ;If Carry
INC 1,X ; Increment Memory Pointer MSB
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
LDA TEMP0 ;Retrieve Memory Pointer
LDY #RTNKEY ;Load Carriage Return
JMP MPUTC ;Write to Memory File
;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
;Sets: TEMP0 = Memory Pointer
; TEMP1 = Number of characters read
;Affects: N,Z reflect Accumulator
;Returns: A,Y = 255 if invalid Memory Pointer
; otherwise number of bytes read
; X = Zero Page Memory Pointer
MREAD: JSR MERRON ;Save Argumebts & Check Memory Pointer
BNE MGETCX ;If Error then Return, Else Y will be 0
LDA 0,X ;Copy Memory Pointer LSB
STA SRCLO ;to Source Pointer LSB
LDA 1,X ;Copy Memory Pointer MSB
STA SRCHI ;to Source Pointer MSB
BNE MWRITC ;Execute Copy
;msrc(&a) - Set Source Array for mwrite()
MSRC EQU SETSRC
;char mwrite(mp, n) - Read from Memory File into Array
;Args: A = Zero Page Memory Pointer
; Y = Number of Bytes to Read
;Sets: TEMP0 = Memory Pointer
; TEMP1 = Number of characters read
;Affects: N,Z reflect Accumulator
;Returns: A,Y = 255 if invalid Memory Pointer
; otherwise number of bytes written
; X = Zero Page Memory Pointer
MWRITE: JSR MERRON ;Save Argumebts & Check Memory Pointer
BNE MGETCX ;If Error then Return, Else Y will be 0
LDA 0,X ;Copy Memory Pointer LSB
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
STA (DSTLO),Y ;Write Byte to Array
JMP MWRITL ;and Loop

110
include/memio.h02 Normal file
View File

@ -0,0 +1,110 @@
/******************************************
* 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();
/* Open Memory File *
* Sets starting address to read/write *
* Args: zp - zero page pointer address *
* &a - base address to read/write *
* Returns: memory file pointer *
* 0 if base address is invalid */
char mopen();
/* Close Memory File *
* Writes EOF at current memory byte *
* and cleares memory pointer *
* Args: mp - memory file pointer *
* Returns: 0 if successful *
255 if memory pointer invalid */
char mclose();
/* End of Memory File *
* Checks for EOF in next memory byte *
* Args: mp - memory file pointer *
* Returns: 0 if not at end of file *
255 if end of file reached */
char meof();
/* Memory File Error *
* Checks for valid memory pointer *
* Args: mp - memory file pointer *
* Returns: 0 if successful *
255 if memory pointer invalid */
char merror();
/* Flush File Buffer *
* Writes EOF at current memory byte *
* Args: mp - memory file pointer *
* Returns: 0 if successful *
255 if memory pointer invalid */
char mflush();
/* Read Character from Memory File *
* Args: mp - memory file pointer *
* Returns: ASCII value of character *
* system dependent garbage *
character if past end of file *
255 if memory pointer invalid */
char mgetc();
/* Write Character to Memory File *
* Args: mp - memory file pointer *
* c - ASCII character to write *
* Returns: 0 if successful *
255 if memory pointer invalid */
char mputc();
/* Read String from Memory File *
* Buffers up to 128 characters *
* 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 */
char mgets();
/* Write String to Memory File *
* Writes up to 128 characters of a *
* null terminated string *
* Args: mp - memory file pointer *
* &s - string to write from *
* Returns: ending position in string *
* 255 if error during write */
char mputs();
/* Write Line to Memory File *
* Write String to File followed by C/R *
* Args: mp - memory file pointer *
* &s - string to print from *
* Returns: ending position in string *
* 255 if error during write */
char mputln();
/* Set Destination Array for fread() *
* Args: &a - Destination array */
void mdst();
/* Read Bytes from Memory File *
* Reads until End of Memory is reached *
* Args: mp - memory file pointer *
* n - number of bytes to read *
* Returns: number of bytes read */
char mread();
/* Set Source Array for fwrite() *
* Args: &a - Source array */
void msrc();
/* Write Bytes to Memory File *
* Args: mp - memory file pointer *
* n - number of bytes to write *
* Returns: number of bytes written */
char mwrite();

View File

@ -2,11 +2,12 @@
;Requires External Zero Page Variables PTRLO, PTRHI
;External Routines MSETSRC
;ptrset(&a) - Set Pointer Address
;Args: X,Y = Address
;ptrset(&a) - Store Address in Pointer
;Args: A = Zero Page Pointer Locatione
; X,Y = Address
;Sets: PTRLO, PTRHI = Pointer
PTRSET: STX PTRLO ;Save Block Start Low Byte
STY PTRHI ;Save Block Start High Byte
PTRSET: STX PTRLO ;Pointer Low Byte
STY PTRHI ;Pointer Start High Byte
RTS
;ptrcmp(&a) - Compare Pointer to Address

View File

@ -2,8 +2,9 @@
* pointer - Pointer Manipulation Functions for C02 *
****************************************************/
/* Set Pointer Address *
* Args: &a - Address */
/* Set Pointer Address *
* Args: &a - Address *
* Args: zp - zero page pointer address */
void ptrset();
/* Write byte to pointer address *

View File

@ -1,5 +1,5 @@
; C02 library stdio.h02 assembly language subroutines
; Requires external routines GETKEY, PRCHR, DELCHR, and NEWLIN
; Requires external routines GETKEY, PRCHR, DELCHR, NEWLIN, and SETSRC
; external zero page locations SRCLO and SRCHI
; and external constants DELKEY, ESCKEY, and RTNKEY
;char getc()
@ -22,7 +22,7 @@ GETSE: CMP #ESCKEY ;Else If Escape
BNE GETSC ;Then
LDY #$FF ; Return -1
BNE GETSY
GETSC: CMP #RTNKEY ;Else If Not Carriage Return
GETSC: CMP #RTNKEY ;Else If Not Carriage Return
BEQ GETSX
JSR PUTC ; Echo Character
STA (SRCLO),Y ; Store Character at offset
@ -50,4 +50,4 @@ PUTSUX: TAY ;Return number of
;char putln(&s)
PUTLN: JSR PUTS ;Write string to screen
JMP NEWLIN ;Call external NEWLINe routine and return
\ JMP NEWLIN ;Execute external NEWLINe routine and return

View File

@ -1,26 +1,43 @@
; C02 library stdiox.h02 assembly language subroutines
;Print Byte as Left Justified Decimal Number
;putdel(b)
;void putdel(b)
;Args: A = number to print
;Sets: TEMP0 - ones digit
; TEMP1 - tens digit
; TEMP2 - hundreds digit
; TEMP3 - number that was printed
;putdem - Alternate Entry Point if number is already in TEMP3
;putdeh - Alternate Entry Point to print only padding spaces
PUTDEL: STA TEMP3
PUTDEM: JSR PUTDEC ;Alternate Entry Point
LDA TEMP3
PUTDEH: CMP #100
BCS PUTDET
JSR PUTSPC
LDA TEMP3
PUTDET: CMP #10
BCS PUTDEX
JSR PUTSPC
PUTDEM: JSR PUTDEC ;Print Decimal Representation of number
LDA TEMP3 ;
PUTDEH: CMP #100 ;If Number < 100
BCS PUTDET ;
JSR PUTSPC ; Print a Space
LDA TEMP3 ;
PUTDET: CMP #10 ; If Number < 10
BCS PUTDEX ;
JSR PUTSPC ; Print another Space
PUTDEX: RTS
;Print Byte as Right Justified Decimal Number
;putder(b)
;void putder(b)
;Args: A = number to print
;Sets: TEMP0 - ones digit
; TEMP1 - tens digit
; TEMP2 - hundreds digit
; TEMP3 - number that was printed
PUTDER: STA TEMP3
PUTDES: JSR PUTDEH
LDA TEMP3
;Print Byte as Decimal Number
;putdec(b)
;void putdec(b)
;Args: A = number to print
;Sets: TEMP0 - ones digit
; TEMP1 - tens digit
; TEMP2 - hundreds digit
PUTDEC: JSR CUBCD ;Convert Accumulator to Unpacked BCD
LDA TEMP2 ;Get High Byte
BEQ PUTDE1 ;If Not Zero
@ -40,7 +57,10 @@ PUTSPC: LDA #32 ;Load Space Character
JMP PRCHR ;and Print it
;Print Byte in Formatted String
;printf(b, &s)
;void printf(b, &s)
;Args: A = number to format
; Y,X = address of formatting string
;Sets: TEMP3 - number to format
PRINTF: JSR SETSRC ;Initialize Source String
STA TEMP3 ;Save Byte to Format
PRINTL: LDA (SRCLO),Y ;Read next character in string
@ -58,12 +78,12 @@ PRINTS: INY ;Increment Offset
CMP #'% ;If Percent Sign
BEQ PRINTC ; Print it and Continue
AND #$DF ;Convert to Upper Case
CMP #'L ;If "d" or "D"
CMP #'L ;If "l" or "L"
BNE PRINTR
LDA TEMP3 ; Load Byte to Format
JSR PUTDEM ; Print Left Justified
JMP PRINTY ; and Continue Printing Screen
PRINTR: CMP #'R ;If "d" or "D"
PRINTR: CMP #'R ;If "r" or "R"
BNE PRINTD
LDA TEMP3 ; Load Byte to Format
JSR PUTDES ; Print Right Justified