📄 stdio.c
字号:
/*
CodeVisionAVR C Compiler
(C) 1998-2006 Pavel Haiduc, HP InfoTech S.R.L.
Standard I/O functions
*/
#include <io.h>
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
#ifndef NULL
#define NULL 0
#endif
#ifndef EOF
#define EOF -1
#endif
#ifndef _DEBUG_TERMINAL_IO_
#ifndef _ALTERNATE_GETCHAR_
#define RXC 7
#define RHR 0
#if defined _CHIP_ATTINY13_ | \
defined _CHIP_ATTINY22_ | defined _CHIP_ATTINY22L_ | \
defined _CHIP_ATTINY24_ | defined _CHIP_ATTINY25_ | \
defined _CHIP_ATTINY26_ | defined _CHIP_ATTINY26L_ | \
defined _CHIP_ATTINY261_ | defined _CHIP_ATTINY261V_ | \
defined _CHIP_ATTINY44_ | defined _CHIP_ATTINY45_ | \
defined _CHIP_ATTINY461_ | defined _CHIP_ATTINY461V_ | \
defined _CHIP_ATTINY84_ | defined _CHIP_ATTINY85_ | \
defined _CHIP_ATTINY861_ | defined _CHIP_ATTINY861V_ | \
defined _CHIP_AT90S2343_ | defined _CHIP_AT90LS2343_ | \
defined _CHIP_AT90C8534_ | \
defined _CHIP_AT86RF401_ | defined _CHIP_AT43USB355_
// issue an error message if getchar is used and the chip doesn't have an U(S)ART
#if funcused getchar
#error the getchar function can't be used as the chip doesn't have an U(S)ART
#endif
#elif defined UCSRA
char getchar(void)
{
while ((UCSRA & (1<<RXC))==0);
return UDR;
}
#elif defined UCSR0A
char getchar(void)
{
while ((UCSR0A & (1<<RXC))==0);
return UDR0;
}
#elif defined US0_CSR
char getchar(void)
{
while ((US0_CSR & (1<<RHR))==0);
return US0_RHR;
}
#else
char getchar(void)
{
#asm
sbis usr,rxc
rjmp _getchar
in r30,udr
#endasm
}
#endif
#endif
#ifndef _ALTERNATE_PUTCHAR_
#define UDRE 5
#define THR 1
#if defined _CHIP_ATTINY13_ | \
defined _CHIP_ATTINY22_ | defined _CHIP_ATTINY22L_ | \
defined _CHIP_ATTINY24_ | defined _CHIP_ATTINY25_ | \
defined _CHIP_ATTINY26_ | defined _CHIP_ATTINY26L_ | \
defined _CHIP_ATTINY261_ | defined _CHIP_ATTINY261V_ | \
defined _CHIP_ATTINY44_ | defined _CHIP_ATTINY45_ | \
defined _CHIP_ATTINY461_ | defined _CHIP_ATTINY461V_ | \
defined _CHIP_ATTINY84_ | defined _CHIP_ATTINY85_ | \
defined _CHIP_ATTINY861_ | defined _CHIP_ATTINY861V_ | \
defined _CHIP_AT90S2343_ | defined _CHIP_AT90LS2343_ | \
defined _CHIP_AT90C8534_ | \
defined _CHIP_AT86RF401_ | defined _CHIP_AT43USB355_
// issue an error message if putchar is used and the chip doesn't have an U(S)ART
#if funcused putchar
#error the putchar function can't be used as the chip doesn't have an U(S)ART
#endif
#elif defined UCSRA
void putchar(char c)
{
while ((UCSRA & (1<<UDRE))==0);
UDR=c;
}
#elif defined UCSR0A
void putchar(char c)
{
while ((UCSR0A & (1<<UDRE))==0);
UDR0=c;
}
#elif defined US0_CSR
void putchar(char c)
{
while ((US0_CSR & (1<<THR))==0);
US0_THR=c;
}
#else
void putchar(char c)
{
#asm
sbis usr,udre
rjmp _putchar
ld r30,y
out udr,r30
#endasm
}
#endif
#endif
#endif
#if funcused printf | funcused sprintf | funcused snprintf | funcused vprintf | funcused vsprintf | funcused vsnprintf \
| funcused scanf | funcused sscanf
typedef struct {char *ps;} ps_s;
#endif
#if funcused printf | funcused sprintf | funcused snprintf | funcused vprintf | funcused vsprintf | funcused vsnprintf
#ifdef _MODEL_TINY_
static void _put(char k,ps_s *ps_ptr,unsigned char *size_ptr)
#else
static void _put(char k,ps_s *ps_ptr,unsigned int *size_ptr)
#endif
{
#ifdef _MODEL_TINY_
unsigned char s;
#else
unsigned int s;
#endif
if (ps_ptr->ps)
{
if ((s=*size_ptr) == 0) goto _put_k;
if (s > 1)
{
*size_ptr= --s;
_put_k:
*(ps_ptr->ps++)=k;
};
}
#if defined _CHIP_ATTINY13_ | \
defined _CHIP_ATTINY22_ | defined _CHIP_ATTINY22L_ | \
defined _CHIP_ATTINY24_ | defined _CHIP_ATTINY25_ | \
defined _CHIP_ATTINY26_ | defined _CHIP_ATTINY26L_ | \
defined _CHIP_ATTINY261_ | defined _CHIP_ATTINY261V_ | \
defined _CHIP_ATTINY44_ | defined _CHIP_ATTINY45_ | \
defined _CHIP_ATTINY461_ | defined _CHIP_ATTINY461V_ | \
defined _CHIP_ATTINY84_ | defined _CHIP_ATTINY85_ | \
defined _CHIP_ATTINY861_ | defined _CHIP_ATTINY861V_ | \
defined _CHIP_AT90S2343_ | defined _CHIP_AT90LS2343_ | \
defined _CHIP_AT90C8534_ | \
defined _CHIP_AT86RF401_ | defined _CHIP_AT43USB355_
#ifdef _ALTERNATE_PUTCHAR_
else putchar(k);
#elif funcused printf
#error the printf function can't be used as the chip doesn't have an U(S)ART
#elif funcused vprintf
#error the vprintf function can't be used as the chip doesn't have an U(S)ART
#endif
#else
else putchar(k);
#endif
}
#define TEST_FORMAT 0
#define GET_FLAGS 1
#define GET_PAD_CHAR 2
#define GET_WIDTH 3
#define GET_PRECISION 4
#define DO_PRINT 5
#if defined _PRINTF_INT_ | defined _PRINTF_INT_WIDTH_
static flash unsigned tbl10[]={10000,1000,100,10,1};
static flash unsigned tbl16[]={0x1000,0x100,0x10,1};
#endif
union ptr_u {
char *p;
char flash *pf;
};
#if defined _PRINTF_INT_
#define F_SIGNED 1
#define F_CAPS 2
#define F_PAD_CHR0 0x10 // pad char is '0'
#ifdef _MODEL_TINY_
static void _print(char flash *fmtstr,va_list argptr,ps_s *ps_ptr, unsigned char max_size)
#else
static void _print(char flash *fmtstr,va_list argptr,ps_s *ps_ptr, unsigned int max_size)
#endif
{
unsigned char l=TEST_FORMAT,flags,k,s;
unsigned n,i;
union ptr_u pp;
while (k=*fmtstr++)
switch (l)
{
case TEST_FORMAT: if (k=='%') l=GET_FLAGS; else _put(k,ps_ptr,&max_size);
break;
case GET_FLAGS: if (k=='%') {_put(k,ps_ptr,&max_size); l=TEST_FORMAT; break;};
l=GET_PAD_CHAR;
s=0;
flags=0;
if (k=='+') {s='+'; break;};
if (k==' ') {s=' '; break;};
case GET_PAD_CHAR:
if (k=='0') {flags|=F_PAD_CHR0; l=DO_PRINT; break;}
case DO_PRINT:
switch (k)
{
case 'c':
_put(va_arg(argptr,char),ps_ptr,&max_size);
goto next;
case 's':
pp.p=va_arg(argptr,char *);
while (k=*pp.p++) _put(k,ps_ptr,&max_size);
goto next;
case 'p':
pp.pf=va_arg(argptr,char flash *);
while (k=*pp.pf++) _put(k,ps_ptr,&max_size);
goto next;
case 'd':
case 'i':
flags|=F_SIGNED;
case 'u':
pp.pf=(char flash *)tbl10;
goto get_arg;
case 'X':
flags|=F_CAPS;
case 'x':
pp.pf=(char flash *) tbl16;
get_arg:
if (flags & F_SIGNED)
{
n=va_arg(argptr,int);
if ((int)n<0)
{
n=-(int) n;
s='-';
};
if (s) _put(s,ps_ptr,&max_size);
}
else n=va_arg(argptr,unsigned);
do
{
k='0';
i=*(unsigned flash *) pp.pf++; //R30,R31=i
while (n>=i)
{
++k;
n-=i;
};
if ((flags & F_PAD_CHR0) || (k>'0') || (i==1))
{
flags|=F_PAD_CHR0;
if (k>'9')
{
if (flags & F_CAPS) k+=7;
else k+=0x27;
};
_put(k,ps_ptr,&max_size);
};
}
while (i>1);
default:
next:
l=TEST_FORMAT;
};
};
}
#elif defined _PRINTF_INT_WIDTH_
#define F_LEFT_JUSTIFY 1
#define F_STRING 2
#define F_SIGNED 4
#define F_CAPS 8
#define F_STRING_FLASH 8
#define F_NON_ZERO 0x10
#define F_PAD_CHR0 0x80 // pad char is '0'
#ifdef _MODEL_TINY_
static void _print(char flash *fmtstr,va_list argptr,ps_s *ps_ptr, unsigned char max_size)
#else
static void _print(char flash *fmtstr,va_list argptr,ps_s *ps_ptr, unsigned int max_size)
#endif
{
unsigned char l=TEST_FORMAT,flags,j,k,width,s;
unsigned n,i;
union ptr_u pp;
while (k=*fmtstr++)
switch (l)
{
case TEST_FORMAT: if (k=='%') l=GET_FLAGS; else _put(k,ps_ptr,&max_size);
break;
case GET_FLAGS: if (k=='%') {_put(k,ps_ptr,&max_size); l=TEST_FORMAT; break;};
l=GET_PAD_CHAR;
s=0;
flags=0;
if (k=='-') {flags=F_LEFT_JUSTIFY; break;};
if (k=='+') {s='+'; break;};
if (k==' ') {s=' '; break;};
case GET_PAD_CHAR:
width=0;
l=GET_WIDTH;
if (k=='0') {flags|=F_PAD_CHR0; break;}
case GET_WIDTH:
if ((k>='0') && (k<('9'+1)))
{
#ifdef _ENHANCED_CORE_
width*=10;
#else
j=width;
width<<=2;
width+=j;
width<<=1;
#endif
width+=k-'0';
break;
};
switch (k)
{
case 'c':
_put(va_arg(argptr,char),ps_ptr,&max_size);
goto next;
case 's':
pp.p=va_arg(argptr,char *);
l=strlen(pp.p);
goto disp_string;
case 'p':
pp.pf=va_arg(argptr,char flash *);
l=strlenf(pp.pf);
flags|=F_STRING_FLASH;
disp_string:
flags|=F_STRING;
flags&=~F_PAD_CHR0;
j=0;
goto pad_left;
case 'd':
case 'i':
flags|=F_SIGNED;
case 'u':
pp.pf=(char flash *) tbl10; l=5;
goto get_arg;
case 'X':
flags|=F_CAPS;
case 'x':
pp.pf=(char flash *) tbl16; l=4;
get_arg:
if (flags & F_SIGNED)
{
n=va_arg(argptr,int);
if ((int)n<0)
{
n=-(int) n;
s='-';
};
if (s) ++l;
else flags&=~F_SIGNED;
}
else n=va_arg(argptr,unsigned);
pad_left:
if ((flags & F_LEFT_JUSTIFY)==0)
while (width>l)
{
if (flags & F_PAD_CHR0)
{
if (flags & F_SIGNED)
{
flags&=~F_SIGNED;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -