added file I/O test program
git-svn-id: svn://svn.cc65.org/cc65/trunk@56 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
108
testcode/lib/ft.c
Normal file
108
testcode/lib/ft.c
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* simple file i/o test
|
||||||
|
*
|
||||||
|
* 12-Jun-2000, Christian Groessler
|
||||||
|
*
|
||||||
|
* please compile with
|
||||||
|
* cl65 -tsystem ft.c getsp.s -o ft.com
|
||||||
|
*
|
||||||
|
* The program asks for a filename (if it hasn't
|
||||||
|
* got one from argv). I then opens the file,
|
||||||
|
* reads the the first 16 bytes and displays them
|
||||||
|
* (as hex values).
|
||||||
|
* The values of sp (cc65 runtime stack pointer)
|
||||||
|
* are displayed at some places. The displayed
|
||||||
|
* value should always be the same.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <conio.h>
|
||||||
|
|
||||||
|
extern int getsp(void); /* is provided in getsp.s */
|
||||||
|
|
||||||
|
#ifdef __ATARI__ /* Atari's fd indirection table */
|
||||||
|
extern char fd_index[];
|
||||||
|
struct fd_t {
|
||||||
|
char usage;
|
||||||
|
char iocb;
|
||||||
|
char dev;
|
||||||
|
char flag;
|
||||||
|
};
|
||||||
|
extern struct fd_t fd_table[];
|
||||||
|
#endif /* #ifdef __ATARI__ */
|
||||||
|
|
||||||
|
int main(int argc,char **argv)
|
||||||
|
{
|
||||||
|
char *filename,*x;
|
||||||
|
char buf[20];
|
||||||
|
int i,l,lr;
|
||||||
|
int fd;
|
||||||
|
int csp;
|
||||||
|
|
||||||
|
if (argc >= 2) {
|
||||||
|
filename = *(argv+1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("\nfilename: ");
|
||||||
|
x = fgets(buf,19,stdin);
|
||||||
|
printf("\n");
|
||||||
|
if (!x) {
|
||||||
|
printf("nothing read\n");
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
#if 0
|
||||||
|
l = strlen(x);
|
||||||
|
printf("read: ");
|
||||||
|
for (i=0; i<l; i++) printf("%02X ",*(x+i)); printf("\n");
|
||||||
|
#endif
|
||||||
|
filename = x;
|
||||||
|
}
|
||||||
|
printf("using filename \"%s\"\n",filename);
|
||||||
|
csp = getsp();
|
||||||
|
printf("now opening file... sp = %d\n",csp);
|
||||||
|
fd = open(filename,O_RDONLY);
|
||||||
|
csp = getsp();
|
||||||
|
if (fd == -1) {
|
||||||
|
char x1 = _oserror;
|
||||||
|
printf("open failed: os: %d,\n\terrno: %d, sp = %d\n",x1,errno,csp);
|
||||||
|
cgetc();
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
printf("open success -- handle = $%x, sp = %d\n",fd,csp);
|
||||||
|
#ifdef __ATARI__
|
||||||
|
printf("fd_index:\n ");
|
||||||
|
for (i=0; i<12; i++) printf("%02X ",fd_index[i]);
|
||||||
|
printf("\nfd_table:\n");
|
||||||
|
for (i=0; i<8; i++) {
|
||||||
|
printf(" usa: %d, iocb: %02X, dev: %02X\n",
|
||||||
|
fd_table[i].usage,
|
||||||
|
fd_table[i].iocb,
|
||||||
|
fd_table[i].dev);
|
||||||
|
}
|
||||||
|
#endif /* #ifdef __ATARI__ */
|
||||||
|
lr = read(fd,buf,16); /* read first 16 bytes */
|
||||||
|
csp = getsp();
|
||||||
|
if (lr == -1) {
|
||||||
|
printf("read failed: %d (sp = %d)\n",errno,csp);
|
||||||
|
cgetc();
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
l = close(fd);
|
||||||
|
if (l == -1) {
|
||||||
|
printf("close failed: %d\n",errno);
|
||||||
|
cgetc();
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
csp = getsp();
|
||||||
|
printf("\n\nThe data read: (%d bytes, sp = %d)\n",lr,csp);
|
||||||
|
for (i=0; i<lr; i++) {
|
||||||
|
printf("%02X ",buf[i]);
|
||||||
|
if (!((i+1) & 7)) printf("\n");
|
||||||
|
}
|
||||||
|
printf("\n\npress return to exit...");
|
||||||
|
getchar();
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
12
testcode/lib/getsp.s
Normal file
12
testcode/lib/getsp.s
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
.export _getsp
|
||||||
|
.importzp sp
|
||||||
|
|
||||||
|
.proc _getsp
|
||||||
|
|
||||||
|
ldx sp+1
|
||||||
|
lda sp
|
||||||
|
rts
|
||||||
|
|
||||||
|
.endproc
|
||||||
|
|
||||||
Reference in New Issue
Block a user