📄 pollio.c
字号:
/********************************************************************//* *//* Polled I/O Program for SNDS100 *//* *//*------------------------------------------------------------------*//* Copyright (C) 1997 Samsung Electronics. *//*------------------------------------------------------------------*//* *//* Modified, programmed by qorrha (Jong H. BAIK) *//* *//********************************************************************//*************************************************************************//* *//* FILE NAME VERSION *//* *//* hwinit.c KS32C5000, KS32C50100 : version 1.0 *//* *//* COMPONENT *//* *//* InitInterrupt(); //Interrupt mode & vector table initialized *//* Rtc_init() ; //Timer0 used for Rtc clock tick *//* *//* *//* DESCRIPTION *//* *//* *//* AUTHOR *//* *//* Young Sun KIM, Samsung Electronics, Inc. *//* *//* DATA STRUCTURES *//* *//* *//* FUNCTIONS *//* *//* SNDS board initializing routine *//* *//* DEPENDENCIES *//* *//* *//* HISTORY *//* *//* NAME DATE REMARKS *//* *//* 1997-05-12 first edited *//* 1997-06-19 modified for KS32C5000 *//* 1999-01-04 modified for KS32C50100 *//* *//*************************************************************************//* *//* Modified by *//* Dmitriy Cherkashin *//* dch@ucrouter.ru *//* 2002 *//* */#include <stdarg.h>#define TEST_POLLIO#include "evm50100.h"/*---------------------------------------------------------------------*//* General Definitions *//*---------------------------------------------------------------------*/#define UCHAR unsigned char#define USHORT unsigned short#define UINT unsigned int#define ULONG unsigned long/*---------------------------------------------------------------------*//* Symbol Definitions *//*---------------------------------------------------------------------*/#define KEY_NL 0x0A#define KEY_CR 0x0D#define KEY_BSP 0x08#define KEY_ESC 0x1B#define KEY_CTRLZ 0x1A#define KEY_RUBOUT 0x7F#define LTRUE 0xFF#define FALSE 0#define NULL 0#define DEFAULT -1#define DEFAULT_P 6 /* default precision *//*---------------------------------------------------------------------*//* This structure is used to store information obtained from parsing a *//* format specifier. *//*---------------------------------------------------------------------*/typedef struct{ long width; /* field width */ long precision; /* precision specifier */ ULONG count; /* character count */ /* BOOLEAN flags with default values */ UCHAR space; /* FALSE */ UCHAR right; /* TRUE */ UCHAR alter; /* FALSE */ UCHAR plus; /* FALSE */ UCHAR zero; /* FALSE */ UCHAR mod; /* process modifier */ UCHAR type; /* conversion type */} FORMAT;static void strout(FORMAT *,UCHAR *,long);static char *percent(char *, FORMAT *, va_list *);/***********************************************************************//* read_number: Read a number from the input *//* *//* RETURNS: number *//* *//***********************************************************************/ULONG get_number(int hex, int digit){ U8 buf[16]; // Input buffer U8 pos; // current position in buffer U32 number; // result number U8 ch; // current character pos = 0; while(1) {// Get a character when it is typed in. Translate linefeed to CR. ///////////// ch = get_byte() & 0x7F; if( ch == KEY_NL || ch == KEY_CR ) {// new line or carreage return => terminate input ////////////////////////////// break; } else if( (ch == KEY_BSP) || (ch == KEY_RUBOUT) ) {// Handle backspace //////////////////////////////////////////////////////////// if(pos > 0) { put_byte('\b'); put_byte(' '); put_byte('\b'); pos--; } } else if(ch <= KEY_CTRLZ) {// Skip control characters (other than backspace and CR) /////////////////////// continue; } else if(pos + 1 >= 16) {// Don't allow the input buffer to overflow. /////////////////////////////////// pos = 16 - 1; put_byte(7); break; } else {// write character to buffer /////////////////////////////////////////////////// buf[pos++] = ch; // write byte to buffer put_byte(ch); // write byte to console if(digit != 0 && pos >= digit) {// out of digit count ////////////////////////////////////////////////////////// break; } } }/* terminating char */ buf[pos] = 0; put_byte('\n');// pos = 0;/* Skip leading spaces */ while(buf[pos] == ' ') { ++pos; } if(hex == 0) { if(buf[pos] == '$') {// process leading $ /////////////////////////////////////////////////////////// hex = 16; // hexadecimal digit pos += 1; // next position in buffer } else if(buf[pos] == 'x') {// x /////////////////////////////////////////////////////////////////////////// hex = 16; pos += 1; } else if( buf[pos] == '0' && ( buf[pos+1] == 'x' || buf[pos+1] == 'X' ) ) {// 0x or 0X //////////////////////////////////////////////////////////////////// hex = 16; pos += 2; } else { hex = 10; // decimal number } }/* Run through the string, adding the value represented by each *//* character to the total. */ number = 0;// while(buf[pos]) { ch = buf[pos++]; if ((ch >= '0') && (ch <= '9')) { ch -= '0'; } else if(hex == 16 && (ch >= 'A') && (ch <= 'F')) { ch = ch - 'A' + 10; } else if (hex == 16 && (ch >= 'a') && (ch <= 'f')) { ch = ch - 'a' + 10; } else { Print("Invalid number [%d] [%c]\n", ch, ch); return(0); } if(hex == 16) { number = (number << 4) + ch; } else { number = number * 10 + ch; } } return(number);}/***********************************************************************//* *//* This subset of printf() supports the following conversion types: *//* *//* %d, %i - output number as a signed decimal (this implementa- *//* tion only uses the low 16 bits of the argument) *//* %o - output number as an unsigned octal number *//* %x, %X - output number as unsigned hexadecimal *//* %c - output a single character *//* %s - ouput a string *//* *//* In addition, the normal width and precision specifiers may be *//* used. *//* *//***********************************************************************//***********************************************************************//* strout: Output a formatted string, adding fill characters and *//* field justification. *//* *//* INPUTS: f_flags = ptr to format data structure *//* str = ptr to string *//* len = length of string *//* *//***********************************************************************/static void strout(FORMAT *f_flags, UCHAR *str, long len){ long size; ULONG count = 0; UCHAR pad = ' '; int i;#ifdef CHECK_POLLIO if(f_flags == 0) { return; } #endif if (len == 0 || str == 0) {/* Check for a NULL string. */ str = (UCHAR *)"(null)"; }/* Calculate the field width. */ f_flags->count += len; size = (f_flags->width < len ) ? len : f_flags->width; /* If the right justified field is set, fill the leading characters *//* with blanks. */ if(f_flags->right) { for (i = size - len; i > 0; i--) { put_byte(pad); count++; } size = len; }/* Output the string. */ while (*str != '\0' && len > 0) { put_byte(*str); if(*str == '\n') put_byte('\r'); str++; len--; size--; }/* If left justified, it may be necessary to pad end of the string. */ for(i = size - len; i > 0; i--) { put_byte(pad); count++; }// f_flags->count += count;}/***********************************************************************//* charout: Output a character *//* *//* INPUTS: f_flags = ptr to format data structure *//* arg_pt = ptr to next argument in list *//* *//***********************************************************************/static void charout(FORMAT *f_flags, va_list *arg_pt){ UCHAR cbuf[2]; cbuf[0] = (UCHAR) va_arg((*arg_pt), ULONG); strout(f_flags, cbuf, 1);}/***********************************************************************//* strlen: returns length of a string *//* *//* INPUTS: str = ptr to string being measured *//* *//***********************************************************************/ static ULONG strlen(UCHAR *str){ long i; for(i = 0; *str++; i++); return i;}/***********************************************************************//* stringout: Output a string *//* *//* INPUTS: f_flags = ptr to format data structure *//* arg_pt = ptr to next argument in list *//* *//* NOTE: Strings longer than 100 bytes are truncated! *//* *//***********************************************************************/static void stringout(FORMAT *f_flags, va_list *arg_pt){ UCHAR * str; ULONG len; str = (UCHAR *)va_arg((*arg_pt), int); len = strlen(str); if(len > f_flags->precision && f_flags->precision != 0) { str = str + (len - f_flags->precision); len = f_flags->precision; } strout(f_flags, str, len);}/***********************************************************************//* decout: Output a value as a decimal number *//* *//* INPUTS: f_flags = ptr to format data structure *//* arg_pt = ptr to next argument in list *//* *//***********************************************************************/static void decout(FORMAT *f_flags, va_list *arg_pt){ UCHAR dec_buff[20]; UCHAR * pt; UCHAR neg; /* value is negative */ long val; /* abs value */ long s_length; ULONG t_count; t_count = 0; /* Get the value to output from the argument list. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -