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

Added functions to module stdiox

This commit is contained in:
Curtis F Kaylor 2018-07-29 20:40:31 -04:00
parent ca3723e2a9
commit 0ec3d860e2
5 changed files with 249 additions and 136 deletions

View File

@ -2,6 +2,7 @@ Standard Input/Output Functions for C02 Programs
At the beginning of the program use the directives At the beginning of the program use the directives
#include <stddef.h02>
#include <stdio.h02> #include <stdio.h02>
The following functions are defined: The following functions are defined:

View File

@ -8,109 +8,129 @@ At the beginning of the program use the directives
The following functions are defined: The following functions are defined:
c = getprc(&s); Writes the string s, followed by a blank line, c = getprc(&s); Writes the string s, waits for a key to be pressed.
to the screen, then waits for a key to be pressed. then writes blank line
Returns the ASCII value of the key that was Returns the ASCII value of the key that was
pressed. pressed.
Note: Calls the puts function, the newlin Note: Calls the puts function, the newlin
function twice, then the getc function. function twice, then the getc function.
c = getprc(&s); Writes the string "Press any key to continue...", c = anykey(); Writes the string "Press any key to continue...",
followed by a blank line, to the screen, then waits followed by a blank line, to the screen, then waits
for a key to be pressed. for a key to be pressed.
Returns the ASCII value of the key that was Returns the ASCII value of the key that was
pressed. pressed.
Note: Calls the getprc function, with a hard Note: Calls the getprc function, with a hard
coded string. coded string.
putdec(b); Writes the decimal representation of byte b to the putspc(b); Writes a space character to the screen.
screen. The output will be between one and three
digits with no leading spaces.
Note: Calls part of the ctoa routine from stdlib Note: Used by the putdel and putder functions.
which leaves the binary values of the ones, tens,
and hundreds digits in variables temp0, temp1, and
temp2, respectively.
putdel(b); Writes the decimal representation of byte b to the putdec(b); Writes the decimal representation of byte b to the
screen. The output is left justified by appending it screen. The output will be between one and three
with 1 space if b is between 10 and 99, or two spaces digits with no leading spaces.
if b is less than 10.
Note: Calls putdec and putspc. Leaves the value Note: Calls part of the ctoa routine from stdlib
of b in varible temp3. which leaves the binary values of the ones, tens,
and hundreds digits in variables temp0, temp1, and
temp2, respectively.
putder(b); Writes the decimal representation of byte b to the putdel(b); Writes the decimal representation of byte b to the
screen. The output is right justified by prepending it screen. The output is left justified by appending it
with 1 space if b is between 10 and 99, or two spaces with 1 space if b is between 10 and 99, or two spaces
if b is less than 10. if b is less than 10.
Note: Calls putdec and putspc. Leaves the value Note: Calls putdec and putspc. Leaves the value
of b in varible temp3. of b in varible temp3.
putdst(); Prints the destination string set by the setdst or putder(b); Writes the decimal representation of byte b to the
setstr functions. screen. The output is right justified by prepending it
with 1 space if b is between 10 and 99, or two spaces
if b is less than 10.
This can be used to print the results of any function Note: Calls putdec and putspc. Leaves the value
call that stores it's result in the destination string, of b in varible temp3.
such as strcat, strcpy, and strcut in string,
Note: calls putc and is called by printf when puthex(b); Writes the hexadecimal representation of byte b to the
processing a %S formatting tag. screen. The output is always two hex digits, the first
being a leading 0 if b is less than 16.
putspc(b); Writes a space character to the screen. Note: Aliased to the prbyte routine.
Note: Used by the putdel and putder functions. putwrd(&d); Writes the hexadecimal representation of address &d to
the screen. The output is always four hex digits, with
leading zeros if b is less than $1000.
r = printf(b, &s): Writes the value of byte b to screen, formatting Note: Calls savrxy, saving the X and Y registers to
the output according the contents of string s. temp1 and temp2, respectively, then calls puthex, once
for the most significant byte and again for the least
significant byte.
The output consists of the characters in s, but with putdst(); Prints the destination string set by the setdst or
any formatting tags replaced with an ASCII setstr functions.
representation of the value of b.
The recognized formatting tags are: This can be used to print the results of any function
%C - output the ASCII character represented by b call that stores it's result in the destination string,
%D - output b as a an unjustified decimal number such as strcat, strcpy, and strcut in string,
%H - output b as a two-digit hexadecimal number
%L - output b as a left justified decimal number
%R - output b as a right justified decimal number
%% - output a single % character
Unlike the printf function in standard C, only Note: calls putc and is called by printf when
one value argument may be passed and that value is processing a %S formatting tag.
used for each formatting tag in the format string.
One additional formatting tag is supported: r = printf(b, &s): Writes the value of byte b to screen, formatting
%S - output the destination string the output according the contents of string s.
The destination string is set using the setdst or The output consists of the characters in s, but with
strdst functions (from the "string" library) before any formatting tags replaced with an ASCII
calling printf. Multiple occurances of the %S tag representation of the value of b.
will cause the destination string to be repeated.
When tag types are mixed, the %S tag will output the The recognized formatting tags are:
destination string, while the other tags will output %C - output the ASCII character represented by b
the formatted byte. If only the %S tag is used, then %D - output b as a an unjustified decimal number
the byte argument may be excluded from the call. %H - output b as a two-digit hexadecimal number
%L - output b as a left justified decimal number
%R - output b as a right justified decimal number
%% - output a single % character
The letter in the formatting tag may be upper or Unlike the printf function in standard C, only
lower case with either a 0 or 1 in the high bit. one value argument may be passed and that value is
Unrecognized formatting tags are interpreted as %C. used for each formatting tag in the format string.
Note: Calls putdec, putdel, putder, prbyte, or Two additional formatting tag are supported:
putdst depending on which formatting tags are used. %S - output the destination string
The value of b is left in variable temp3. %W - output the destination address as a
as a four-digit hexadecimal number
The destination string and/or address is set using
the setdst or strdst function (from the "string"
library) before calling printf. Multiple occurances
of the %S or %w tag will cause the destination string
and/or address to be repeated.
When tag types are mixed, the %S and/or %W tags will
output the destination string and/or address, while
the other tags will output the formatted byte. If
only the %S and/or %W tags are used, then the byte
argument may be excluded from the call.
The letter in the formatting tag may be upper or
lower case with either a 0 or 1 in the high bit.
Unrecognized formatting tags are interpreted as %C.
Note: Calls putdec, putdel, putder, puthex, putwrd,
or putdst depending on which formatting tags are
used. The value of b is left in variable temp3.
Note: This library expects the following functions to be defined: Note: This library expects the following functions to be defined:
cubcd(); Convert byte to BCD and unpack into three bytes cubcd(); Convert byte to BCD and unpack into three bytes
prbyte(); Print byte to screen as hexadecimal number prbyte(); Print byte to screen as hexadecimal number
prchr(c); Print ASCII character to screen prchr(c); Print ASCII character to screen
savrxy(); Save X and Y registers.
setsrc(&s); Set source string pointer and initialize index setsrc(&s); Set source string pointer and initialize index
along with the zero page variable pairs: along with the zero page variable pairs:

View File

@ -2,42 +2,67 @@
; Requires external routines GETKEY, PRCHR, DELCHR, NEWLIN, and SETSRC ; Requires external routines GETKEY, PRCHR, DELCHR, NEWLIN, and SETSRC
; external zero page locations SRCLO and SRCHI ; external zero page locations SRCLO and SRCHI
; and external constants DELKEY, ESCKEY, and RTNKEY ; and external constants DELKEY, ESCKEY, and RTNKEY
;char getc()
;char getc() - GET Character from keyborad
;Sets: System Dependent
;Uses: System Dependent
;Affects: System Dependent
;Returns: A = Character Code of Key
GETC EQU GETKEY ;Alias to external GETKEY routine GETC EQU GETKEY ;Alias to external GETKEY routine
;void putc(c) ;void putc(c) - PUT Character to screen
;Args: Character code to display
;Sets: System Dependent
;Uses: System Dependent
;Affects: System Dependent
;Returns: System Dependent
PUTC EQU PRCHR ;Alias to external PRCHR Routine PUTC EQU PRCHR ;Alias to external PRCHR Routine
;char gets(&s) ;char gets(&s) - GET String input from keyboard
GETS: JSR SETSRC ;Initialize Source String ;Args: Y,X = Address of String
GETSL: JSR GETC ;Get Keypress ;Sets: SRCLO,SRCLHI = Address of String
CMP #DELKEY ;If Delete ;Affects: N,Z,C
BNE GETSE ;Then ;Returns: A,Y = Number of Characters in String
TYA ; If Offset is Zero GETS: JSR SETSRC ;Initialize Source String
BEQ GETSL ; Get Next Character GETSL: JSR GETC ;Get Keypress
DEY ; Else Decrement Offset CMP #DELKEY ;If Delete
JSR DELCHR ; Delete Previous Character BNE GETSE ;Then
JMP GETSL ; and Get Next Character TYA ; If Offset is Zero
GETSE: CMP #ESCKEY ;Else If Escape BEQ GETSL ; Get Next Character
BNE GETSC ;Then DEY ; Else Decrement Offset
LDY #$FF ; Return -1 JSR DELCHR ; Delete Previous Character
JMP GETSL ; and Get Next Character
GETSE: CMP #ESCKEY ;Else If Escape
BNE GETSC ;Then
LDY #$FF ; Return -1
BNE GETSY BNE GETSY
GETSC: CMP #RTNKEY ;Else If Not Carriage Return GETSC: CMP #RTNKEY ;Else If Not Carriage Return
BEQ GETSX BEQ GETSX
JSR PUTC ; Echo Character JSR PUTC ; Echo Character
STA (SRCLO),Y ; Store Character at offset STA (SRCLO),Y ; Store Character at offset
INY ; increment offset and INY ; increment offset and
BPL GETSL ; loop if less than 128 BPL GETSL ; loop if less than 128
GETSX: JSR NEWLIN ;Else Advance Cursor to Next Line GETSX: JSR NEWLIN ;Else Advance Cursor to Next Line
LDA #$00 ; Terminate String LDA #$00 ; Terminate String
STA (SRCLO),Y ; and STA (SRCLO),Y ; and
GETSY: TYA ; Return String Length GETSY: TYA ; Return String Length
RTS RTS
;char puts(&s) ;char puts(&s) - PUT String to screen
;Args: Y,X = Address of String
;Calls: PUTS
;Affects: N,Z,C
;Returns: A,Y = Number of Characters in String
PUTS: LDA #$00 ;Set Start Position to 0 PUTS: LDA #$00 ;Set Start Position to 0
;and fall into putsub ;and fall into putsub
;char putsub(n, &s)
;char putsub(n, &s) - PUT SUBstring to screen
;Args: A = Starting Position in String
; Y,X = Address of String
;Sets: SRCLO,SRCLHI = Address of String
;Calls: PUTC
;Affects: N,Z,C
;Returns: A,Y = Number of Characters in String
PUTSUB: JSR SETSRC ;Initialize Source String PUTSUB: JSR SETSRC ;Initialize Source String
TAY ;Initialize character offset TAY ;Initialize character offset
PUTSUL: LDA (SRCLO),Y ;Read next character in string PUTSUL: LDA (SRCLO),Y ;Read next character in string
@ -48,6 +73,8 @@ PUTSUL: LDA (SRCLO),Y ;Read next character in string
PUTSUX: TYA ;Return number of PUTSUX: TYA ;Return number of
RTS ; characters printed RTS ; characters printed
;char putln(&s) ;char putln(&s) - PUT LiNe to screen
;Args: Y,X = Address of String
;Calls: PUTS and NEWLIN
PUTLN: JSR PUTS ;Write string to screen PUTLN: JSR PUTS ;Write string to screen
JMP NEWLIN ;Execute external NEWLINe routine and return JMP NEWLIN ;Execute external NEWLINe routine and return

View File

@ -2,18 +2,33 @@
ANYKEP: DC "Press any key to continue...",0 ANYKEP: DC "Press any key to continue...",0
;
;char anykey() - wait for character with ANY KEY prompt
;Calls: GETCPR, NEWLIN - Print Newline to Screen
;Affects: C,N,Z
;Returns: A = Character code of keypress
ANYKEY: JSR NEWLIN ;Start at Beginning of Next Line ANYKEY: JSR NEWLIN ;Start at Beginning of Next Line
LDY #>ANYKEP ;Load Prompt High Byte LDY #>ANYKEP ;Load Prompt High Byte
LDX #<ANYKEP ;Load Prompt Low Byte LDX #<ANYKEP ;Load Prompt Low Byte
;Drop into GETCPR ;Drop into GETCPR
;Display Prompt and Wait for Character ;char getcpr(&s) - GET Character with PRompt
GETCPR: JSR PUTLN ;Print Prompt ;Args: Y,X = Address of Prompt String
;Sets: TEMP0 - Character code of keypress
;Calls: PUTS - Put String to Screen
; GETC - Get Character from Keyboard
; NEWLIN - Print Newline to Screen
;Affects: C,N,Z
;Returns: A = Character code of keypress
GETCPR: JSR PUTS ;Print Prompt
JSR GETC ;Wait for and Return Keypress
STA TEMP0 ;Save Key Code
JSR NEWLIN ;Move to Next Line
JSR NEWLIN ;Generate Blank Line JSR NEWLIN ;Generate Blank Line
JMP GETC ;Wait for and Return Keypress LDA TEMP0 ;Load Key Code
RTS
;Print Byte as Left Justified Decimal Number ;void putdel(b) - PUT DEcimal Left justified
;void putdel(b)
;Args: A = number to print ;Args: A = number to print
;Sets: TEMP0 - ones digit ;Sets: TEMP0 - ones digit
; TEMP1 - tens digit ; TEMP1 - tens digit
@ -33,8 +48,7 @@ PUTDET: CMP #10 ; If Number < 10
JSR PUTSPC ; Print another Space JSR PUTSPC ; Print another Space
PUTDEX: RTS PUTDEX: RTS
;Print Byte as Right Justified Decimal Number ;void putder(b) - PUT DEcimal Right justified
;void putder(b)
;Args: A = number to print ;Args: A = number to print
;Sets: TEMP0 - ones digit ;Sets: TEMP0 - ones digit
; TEMP1 - tens digit ; TEMP1 - tens digit
@ -44,8 +58,7 @@ PUTDER: STA TEMP3
PUTDES: JSR PUTDEH PUTDES: JSR PUTDEH
LDA TEMP3 LDA TEMP3
;Print Byte as Decimal Number ;void putdec(b) - PUT DECimal
;void putdec(b)
;Args: A = number to print ;Args: A = number to print
;Sets: TEMP0 - ones digit ;Sets: TEMP0 - ones digit
; TEMP1 - tens digit ; TEMP1 - tens digit
@ -64,15 +77,34 @@ PUTDEP: ORA #$30 ;Convert to ASCII digit
JSR PRCHR ;And Print JSR PRCHR ;And Print
RTS RTS
;puthex(&word) - PUT HEXadecimal
PUTHEX EQU PRBYTE ;Print Byte as Hexadecimal
;putwrd(&word) - PUT WoRD
;Args: Y = Word MSB
; X = Word LSB
;Calls: PUTS = Put String
; PRBYTE = Print Byte
; SAVRXY = Save X and Y Registers
;Affects: A,Y,X,N,Z,C
PUTWRD: JSR SAVRXY ;Save Address
PUTWRA: LDA TEMP2 ;Load Address MSB
JSR PRBYTE ;Print as Hexadecimal
LDA TEMP1 ;Load Address LSB
JMP PRBYTE ;Print and Return`
;Print a Space ;Print a Space
PUTSPC: LDA #32 ;Load Space Character PUTSPC: LDA #32 ;Load Space Character
JMP PRCHR ;and Print it JMP PRCHR ;and Print it
;Print Byte in Formatted String ;void printf(b, &s) - PRINT Formatted byte and/or string
;void printf(b, &s) ;Args: A = byte to format
;Args: A = number to format
; Y,X = address of formatting string ; Y,X = address of formatting string
;Sets: TEMP3 - number to format ;Uses: DSTLO,DSTHI = Address of %S string
;Sets: SRCLO,SRCHI = Address of formatting string
; TEMP3 - number to format
;Destroys: TEMP0,TEMP1,TEMP2
;Returns: A,Y = Total number of characters printed
PRINTF: JSR SETSRC ;Initialize Source String PRINTF: JSR SETSRC ;Initialize Source String
STA TEMP3 ;Save Byte to Format STA TEMP3 ;Save Byte to Format
PRINTL: LDA (SRCLO),Y ;Read next character in string PRINTL: LDA (SRCLO),Y ;Read next character in string
@ -108,14 +140,21 @@ PRINTD: CMP #'D ;'Else If "d" or "D"
PRINTH: CMP #'H ;'Else If "h" or "H" PRINTH: CMP #'H ;'Else If "h" or "H"
BNE PRINTS BNE PRINTS
LDA TEMP3 ; Load Byte to Format LDA TEMP3 ; Load Byte to Format
JSR PRBYTE ; Print as Hexadecimal JSR PUTHEX ; Print as Hexadecimal
JMP PRINTY ; and Continue Printing Screen JMP PRINTY ; and Continue Printing Screen
PRINTS: CMP #'S ;'Else If "s" or "s" PRINTS: CMP #'S ;'Else If "s" or "S"
BNE PRINTB BNE PRINTW
STY TEMP0 ; Save Index STY TEMP0 ; Save Index
JSR PUTDST ; Print Destination String JSR PUTDST ; Print Destination String
LDY TEMP0 ; Restore Index LDY TEMP0 ; Restore Index
JMP PRINTY ; JMP PRINTY ;
PRINTW: CMP #'W ;'Else If "w" or "W"
BNE PRINTB
STY TEMP0 ; Save Index
JSR SAVDST ; Save Destination Address
JSR PUTWRA ; Print MSB and LSB as Hex
LDY TEMP0 ; Restore Index
JMP PRINTY ;
PRINTB: LDA TEMP3 ;Else PRINTB: LDA TEMP3 ;Else
JMP PRINTC ; Print Raw Byte and Continue JMP PRINTC ; Print Raw Byte and Continue

View File

@ -8,7 +8,7 @@
#include <stdio.h02> #include <stdio.h02>
#include <stdiox.h02> #include <stdiox.h02>
char c, i; char c, i, j;
char s = "string"; char s = "string";
main: main:
@ -44,17 +44,43 @@ do {
} while (i); } while (i);
anykey(); anykey();
putln("prthex()");
do { do {
if (i<32) printf(" H='%h' R='%r' L='%l' D='%d'"); puthex(i);
else printf("C='%c' H='%h' R='%r' L='%l' D='%d'"); putc(':');
newlin();
i++; i++;
if (!i&15) anykey(); if (!i&15) newlin();
} while (i);
anykey();
putln("prtwrd()");
do {
j = i ^ $FF;
putwrd(*,i,j);
putc(':');
i++;
if (!i&15) newlin();
} while (i);
anykey();
do {
pfchar(i); putc($09); pfchar(i+16); newlin();
i++; if (!i&15) {i = i + 16; anykey(); }
} while (i<128); } while (i<128);
setdst(&s); setdst(&s);
printf("S=\"%s\""); printf("S=\"%s\"");
newlin(); newlin();
setdst(&$1234);
printf("&$1234=%w");
newlin();
done: done:
goto exit; goto exit;
void pfchar(c) {
if (c<32) printf(c," H='%h' R='%r' L='%l' D='%d'");
else printf(c,"C='%c' H='%h' R='%r' L='%l' D='%d'");
}