1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-11-22 01:31:33 +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
#include <stddef.h02>
#include <stdio.h02>
The following functions are defined:

View File

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

View File

@ -2,42 +2,67 @@
; 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()
;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
;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
;char gets(&s)
GETS: JSR SETSRC ;Initialize Source String
GETSL: JSR GETC ;Get Keypress
CMP #DELKEY ;If Delete
BNE GETSE ;Then
TYA ; If Offset is Zero
BEQ GETSL ; Get Next Character
DEY ; Else Decrement Offset
JSR DELCHR ; Delete Previous Character
JMP GETSL ; and Get Next Character
GETSE: CMP #ESCKEY ;Else If Escape
BNE GETSC ;Then
LDY #$FF ; Return -1
;char gets(&s) - GET String input from keyboard
;Args: Y,X = Address of String
;Sets: SRCLO,SRCLHI = Address of String
;Affects: N,Z,C
;Returns: A,Y = Number of Characters in String
GETS: JSR SETSRC ;Initialize Source String
GETSL: JSR GETC ;Get Keypress
CMP #DELKEY ;If Delete
BNE GETSE ;Then
TYA ; If Offset is Zero
BEQ GETSL ; Get Next Character
DEY ; Else Decrement Offset
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
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
INY ; increment offset and
BPL GETSL ; loop if less than 128
GETSX: JSR NEWLIN ;Else Advance Cursor to Next Line
LDA #$00 ; Terminate String
STA (SRCLO),Y ; and
GETSY: TYA ; Return String Length
JSR PUTC ; Echo Character
STA (SRCLO),Y ; Store Character at offset
INY ; increment offset and
BPL GETSL ; loop if less than 128
GETSX: JSR NEWLIN ;Else Advance Cursor to Next Line
LDA #$00 ; Terminate String
STA (SRCLO),Y ; and
GETSY: TYA ; Return String Length
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
;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
TAY ;Initialize character offset
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
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
JMP NEWLIN ;Execute external NEWLINe routine and return

View File

@ -2,18 +2,33 @@
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
LDY #>ANYKEP ;Load Prompt High Byte
LDX #<ANYKEP ;Load Prompt Low Byte
LDY #>ANYKEP ;Load Prompt High Byte
LDX #<ANYKEP ;Load Prompt Low Byte
;Drop into GETCPR
;Display Prompt and Wait for Character
GETCPR: JSR PUTLN ;Print Prompt
;char getcpr(&s) - GET Character with 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
JMP GETC ;Wait for and Return Keypress
LDA TEMP0 ;Load Key Code
RTS
;Print Byte as Left Justified Decimal Number
;void putdel(b)
;void putdel(b) - PUT DEcimal Left justified
;Args: A = number to print
;Sets: TEMP0 - ones digit
; TEMP1 - tens digit
@ -33,8 +48,7 @@ PUTDET: CMP #10 ; If Number < 10
JSR PUTSPC ; Print another Space
PUTDEX: RTS
;Print Byte as Right Justified Decimal Number
;void putder(b)
;void putder(b) - PUT DEcimal Right justified
;Args: A = number to print
;Sets: TEMP0 - ones digit
; TEMP1 - tens digit
@ -44,8 +58,7 @@ PUTDER: STA TEMP3
PUTDES: JSR PUTDEH
LDA TEMP3
;Print Byte as Decimal Number
;void putdec(b)
;void putdec(b) - PUT DECimal
;Args: A = number to print
;Sets: TEMP0 - ones digit
; TEMP1 - tens digit
@ -64,15 +77,34 @@ PUTDEP: ORA #$30 ;Convert to ASCII digit
JSR PRCHR ;And Print
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
PUTSPC: LDA #32 ;Load Space Character
JMP PRCHR ;and Print it
;Print Byte in Formatted String
;void printf(b, &s)
;Args: A = number to format
;void printf(b, &s) - PRINT Formatted byte and/or string
;Args: A = byte to format
; 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
STA TEMP3 ;Save Byte to Format
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"
BNE PRINTS
LDA TEMP3 ; Load Byte to Format
JSR PRBYTE ; Print as Hexadecimal
JSR PUTHEX ; Print as Hexadecimal
JMP PRINTY ; and Continue Printing Screen
PRINTS: CMP #'S ;'Else If "s" or "s"
BNE PRINTB
PRINTS: CMP #'S ;'Else If "s" or "S"
BNE PRINTW
STY TEMP0 ; Save Index
JSR PUTDST ; Print Destination String
LDY TEMP0 ; Restore Index
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
JMP PRINTC ; Print Raw Byte and Continue

View File

@ -8,7 +8,7 @@
#include <stdio.h02>
#include <stdiox.h02>
char c, i;
char c, i, j;
char s = "string";
main:
@ -44,17 +44,43 @@ do {
} while (i);
anykey();
putln("prthex()");
do {
if (i<32) printf(" H='%h' R='%r' L='%l' D='%d'");
else printf("C='%c' H='%h' R='%r' L='%l' D='%d'");
newlin();
puthex(i);
putc(':');
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);
setdst(&s);
printf("S=\"%s\"");
newlin();
setdst(&$1234);
printf("&$1234=%w");
newlin();
done:
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'");
}