📄 zprint.c
字号:
/*
====================================================================
Description: Print From Uart, such as printf in dos
support multiple format
Author: Zealea.Kuethy
Date: 03.01.23
Ver: 0.01V
====================================================================
*/
#include "includes.h"
#include "bsp.h"
#include <stdarg.h>
/////////////////////////////////////////////////////////////////////
// DEFINE THE PTR LENGTH
#define PTR_LENGTH_NEAR 11
#define PTR_LENGTH_FAR 13
#define PTR_LENGTH_HUGE 24
//////////////////////////////////////////////////////////////////////
#define UINT_MAX (0xffffU) // maximum value of an unsigned int
#define STDOUT (DEBUG_UART_PRINTU)
#define MAX_UART_IN 0x01
/*
===============================================================
Description: converts the given long argument to an
ascii string (the buffer is given as an argument).
This routine is used by DoPrint()
Date: 03.01.23
===============================================================
*/
char *LToA( char *p, unsigned long num, unsigned char radix )
{
register unsigned int digit;
register unsigned long newnum;
register char * q;
char * hold;
hold = p;
q = p + 15 + 2; /* should correspond to buffer size in _putnumber() in _doprint.c */
*--q = '\0'; /* be sure to terminate the string with a NULL character */
do
{
newnum = num / radix;
digit = num - (newnum * radix); /* avoid modulo operation */
if ( digit > 9 )
digit += 'A'-'0'-10; /* Convert to hex A-F */
*--q = digit + '0';
num = newnum;
} while ( newnum != 0 );
while ( (*p++ = *q++) ) ; /* move the string to the left, including the NULL character */
return hold;
}
/*
===============================================================
Description: Count Length of String
return length
Date: 03.01.23
===============================================================
*/
unsigned int StrLen( register const char *s )
{
const char *p;
for ( p = s; *s++; )
{
}
return (s - p) - 1;
}
/*
=============================================================
Description: Check Uart Send Buf(hardware) is empty?
Note: this inline function already in uartdrv.h
=============================================================
*/
/*_inline INT8U IsTxBufEmpty (INT8U UART_No)
{
if (UARTREG[UART_No].RR0 & 0x04)
return 1;
else
return 0;
}*/
/*
================================================================
Description: Put a Char From Uart that you choose by UartOut_NO
Date: 03.01.23
================================================================
*/
void CommPutChar( int c, INT8U UartOut_NO)
{
//while (!IsTxBufEmpty (UartOut_NO))
// ;
//WriteUARTReg (UartOut_NO, c);
if(MAX_UART_IN >= UartOut_NO) uarts_write_D(UartOut_NO, c);
}
/*
==================================================================
Description: Print one character in hexadecimal format
==================================================================
*/
static void CommPrintHex( unsigned char c, INT8U UartOut_NO)
{
unsigned char cha;
cha = (c >> 4) & 0x0F;
cha += cha > 9 ? 'A'-10 : '0';
CommPutChar( cha, UartOut_NO );
cha = c & 0x0F;
cha += cha > 9 ? 'A'-10 : '0';
CommPutChar( cha, UartOut_NO );
}
/*
==================================================================
Description: Print hex string
==================================================================
*/
static void CommPutStringHex( unsigned char format, register int min, register int max,
register char *str, INT8U UartOut_NO )
{
register int length = 0;
if( !max )
max = 0x100;
length = StrLen( str );
if( !(format & 0x40) ) /* right alignment ? */
{
for( ; min>length || min>max; --min )
{
CommPrintHex( ' ', UartOut_NO );
}
}
while( max-- && *str ) /* print string */
{
CommPrintHex( *str++, UartOut_NO );
if( min )
min--; /* min will contain the minimum number */
/* of characters to print afterwards */
}
while( min-- )
{
CommPrintHex( ' ', UartOut_NO ); /* padding spaces */
};
}
/*
==================================================================
Description: For Small Mode To Output String
==================================================================
*/
static void CommPutStringSmall( char * str, INT8U UartOut_NO)
{
while( *str )
{
CommPutChar( *str, UartOut_NO );
str++;
}
}
/*
==================================================================
Description: OutPut String
==================================================================
*/
static void CommPutString( unsigned char format, register int min, register int max,
register char *str, INT8U UartOut_NO )
{
register int length = 0;
if( !max )
max = 0x100;
length = StrLen( str );
if( !(format & 0x40) ) /* right alignment ? */
{
for( ; min>length || min>max; --min )
{
CommPutChar( ' ', UartOut_NO );
}
}
while( max-- && *str ) /* print string */
{
CommPutChar( *str++, UartOut_NO );
if( min )
min--; /* min will contain the minimum number */
/* of characters to print afterwards */
}
while( min-- )
{
CommPutChar( ' ', UartOut_NO ); /* padding spaces */
};
}
/*
===================================================================
Description: Put Nummber
Date: 03.01.23
===================================================================
*/
static void CommPutNumber( int min, int max, unsigned char ch, unsigned char format,
void *pvalue, INT8U UartOut_NO)
{
char buf[15+3]; /* room to print long value in */
register char *buffer; /* help variable */
register long value = 0;
register char length = 0; /* length needed to print value */
register unsigned char vsign; /* sign of the value */
if( !(format & 0x02) ) /* 'h' or default, same as --> */
/* (!(format & 0x03) || */
/* (format & 0x03) == 0x01) */
{ /* short value is the same as int */
value = *(int *)pvalue;
if( ch != 'd' && ch != 'i' ) /* not a signed integer ? */
value &= UINT_MAX; /* truncate to positive */
/* integer */
}
else if( (format & 0x03) == 0x02 ) /* 'l' */
{
value = *(long *)pvalue;
}
;
vsign = 0; /* 0 - positive value, otherwise negative value */
if( ch == 'c' )
{
buf[0] = value;
buf[1] = '\0';
}
else if( ch == 'd' || ch == 'i' )
{
if( value < 0 )
{
vsign = '-';
value = -value;
}
goto unsignedint;
}
else if( ch == 'u' )
{
unsignedint:
LToA( buf, value, 10 ); /* convert to ascii */
length = StrLen( buf );
if( !vsign )
{
if( (format & 0x0C) == 0x04 ) /* sign always */
vsign = '+';
else if( (format & 0x0C) == 0x08 ) /* sign or space */
vsign = ' ';
}
if( vsign )
{
if( min )
--min;
}
if( vsign && (format & 0x60) ) /* ( (format & 0x20) || */
{ /* (format & 0x40) ) */
CommPutChar( vsign, UartOut_NO );
vsign = 0;
}
if( !(format & 0x40) ) /* right alignment ? */
{
while( (min > max) && (min > length) )
{
CommPutChar( (format & 0x20) ? '0' : ' ', UartOut_NO );
min--;
}
}
if( vsign )
{
CommPutChar( vsign, UartOut_NO );
}
while( max-- > length )
{
CommPutChar( '0', UartOut_NO );
if( min )
min--;
}
}
else if( ch == 'o' || ch == 'x' || ch == 'X' )
{
LToA( buf, value, ch == 'o' ? 8 : 16 );
if( ch == 'x' )
{
/* convert string to lowercase */
for( buffer = buf; *buffer; ++buffer )
{ /* do not use 'tolower' */
if( *buffer >= 'A' && *buffer <= 'Z' )
*buffer += 'a' - 'A';
}
}
length = StrLen( buf );
if( ! value )
format &= 0xEF; /* clear alternate when value is '0' */
if( (format & 0x10) ) /* alternate */
{
if( format & 0x20 ) /* alternate and filling from */
{ /* the left with zeros */
if( ch != 'o' )
{
CommPutChar( '0', UartOut_NO ); /* leading 0 */
CommPutChar( ch, UartOut_NO );
}
format &= 0xEF; /* clear alternate */
}
if( ch == 'o' )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -