📄 sys.c
字号:
/************************************************************************
* *
* Copyright (C) SEIKO EPSON CORP. 1999 *
* *
* File name: sys.c *
* This is system file only to run demo program with db33 *
* simulated I/O function. *
* *
* Revision history *
* 1999.03.02 T.Mineshima Start. *
* 1999.03.17 T.Mineshima Function Modify. *
* *
************************************************************************/
/* Prototype */
void _exit(void);
void _init_sys(void);
void read_str(char *);
void write_str(char *);
static int read(int, char *, int);
static int write(int, char *, int);
static char num_to_asc(int);
void write_hex(unsigned long);
static void write_8hex(unsigned long, int);
/* Buffer for simulated stdin, WRITE_BUF[0] is size (1-0x40, 0 means no data)
WRITE_BUF[1-64] is buffer area for data, max 64 bytes
used in write () */
unsigned char WRITE_BUF[65];
/* Buffer for simulated stdin, READ_BUF[0] is size (1-0x40, 0 means EOF)
READ_BUF[1-64] is buffer area for data, max 64 bytes
used in read() */
unsigned char READ_BUF[65];
/* If 1: READ_BUFFER become EOF, 0: not EOF */
static unsigned char READ_EOF;
/* Definition */
#define ERR -1
/*******************************************************************************
* _exit
* Type : void
* Ret val : none
* Argument : void
* Function : exit execute infinity loop.
*******************************************************************************/
void _exit(void)
{
LOOP:
goto LOOP;
}
/*******************************************************************************
* _init_sys
* Type : void
* Ret val : none
* Argument : void
* Function : _init_sys initialize read() and write() buffer area.
*******************************************************************************/
void _init_sys(void)
{
READ_EOF = 0; /* Not EOF */
READ_BUF[0] = 0; /* BUFFER is empty */
}
/*******************************************************************************
* read_str
* Type : void
* Ret val : none
* Argument : char *str Read string buffer pointer
* Function : Caller should reserve enough area for string,
* Send pointer to read_str, and read_str set string
* with 0 terminate.
*******************************************************************************/
void read_str(char *str)
{
int iSize;
do {
iSize = read(1, str, 1); /* Read 1 char */
if (iSize == 0){
break; /* No string */
}
if (*str == '\r') {
continue; /* If CR, ignore and read again */
}
if (*str == '\n') { /* If LF, break */
break;
}
str++;
} while (1);
*str = 0; /* Instead of LF, put 0 */
return;
}
/*******************************************************************************
* write_str
* Type : void
* Ret val : none
* Argument : char *str Write string buffer pointer
* Function : Send NULL terminate string to write.
*******************************************************************************/
void write_str(char *str)
{
int i;
char *c;
i = 0;
c = str;
while (*c != 0) { /* Check string length */
c++;
i++;
}
write(1, str, i); /* Write str */
return;
}
/*******************************************************************************
* read
* Type : int
* Ret val: read() get and return required bytes with using simulated input.
* If EOF return 0.
* Argument : int fhDummy Dummy file handler
* char *psReadBuf Buffer for read data
* int iReadBytes Required byte size of read data
* Function : Read simulated input.
* READ_FLASH: Break point label for stdin
* READ_BUF: Buffer area for stdin
*******************************************************************************/
static int read(int fhDummy, char *psReadBuf, int iReadBytes)
{
int iBytes; /* Data size written to psReadBuf */
int iSize; /* Data size in READ_BUF */
int iCount; /* Counter to copy data to psReadBuf */
char *psBuf; /* Top of read buffer */
static int iNdxPos; /* Current position in READ_BUF*/
/* Start */
iBytes = 0; /* No read now */
psBuf = psReadBuf;
/* This loop repeat for each byte to copy READ_BUF to psReadBuf */
for (;;) {
/* If iReadByte become 0, return */
if (iReadBytes == 0) /* If required size become 0, return */
break; /* for() break */
/* If EOF, return 0 */
if (READ_EOF == 1)
break; /* for() break */
/* If there is data, copy 1 byte */
iSize = READ_BUF[0];
if (iSize > 0 ) {
*psBuf = READ_BUF[iNdxPos];
psBuf++;
iReadBytes--;
iNdxPos++;
iSize--;
iBytes++;
READ_BUF[0] = (unsigned char)(iSize & 0xff);
} else {
/* If no data, read 0-64 bytes from simulated input */
asm(".global READ_FLASH");
asm("READ_FLASH:"); /* Label for simulated stdin */
if (READ_BUF[0] == 0)
READ_EOF = 1; /* If size is 0, EOF */
iNdxPos = 1; /* Reset index position */
}
} /* back to for (;;) */
/* All system interrupt enable */
asm(" ld.w %r7,%psr
or %r7,0x10
ld.w %psr,%r7
");
return(iBytes);
}
/*******************************************************************************
* write
* Type : int
* Ret val: write data byte size with using simulated stdout
* Argument : int fhDummy Dummy file handler
* char *psWriteBuf Buffer for write data
* int iWriteBytes Byte size of write data
* Function : Write simulated output.
* WRITE_FLASH: Break point label for stdout
* WRITE_BUF: Buffer area for stdout
*******************************************************************************/
static int write(int fhDummy, char *psWriBuf, int iWriBytes)
{
int iBytes; /* Remain data bytes waiting WRITE_BUF */
int iSize; /* Data size to write to WRITE_BUF */
int iCount; /* Counter to copy data to WRITE_BUF */
iBytes = iWriBytes;
for (;;) { /* Repeat each 255 bytes */
/* If remain 0, return original size */
if (iBytes == 0) /* Remain size become 0, so return */
break; /* for() break */
/* If remain > 64, write size is 64 */
if (iBytes > 64) {
iSize = 64; /* 64 bytes to WRITE_BUF */
iBytes = iBytes - 64; /* Remain data */
} else { /* Not over 64 */
iSize = iBytes; /* Under 64 bytes to WRITE_BUF */
iBytes = 0; /* No remain data */
}
/* Copy psWriBuf to WRITE_BUF */
WRITE_BUF[0] = (unsigned char)(iSize & 0xff); /* Set size */
for (iCount = 1 ; iCount <= iSize ; iCount++) {
WRITE_BUF[iCount] = *psWriBuf; /* Copy data */
psWriBuf++;
}
asm(".global WRITE_FLASH");
asm("WRITE_FLASH:"); /* Label for simulated stdout */
} /* back to for (;;) */
/* All system interrupt enable */
asm(" ld.w %r7,%psr
or %r7,0x10
ld.w %psr,%r7
");
return(iWriBytes);
}
/*******************************************************************************
* num_to_asc
* Type : char
* Ret val: '0'-'f' or ERR
* Argument : int i Number value
* Function : Convert number 0x0-0xf to '0'-'f'.
* for example
* i=0 : return '0'
* i=15 : return 'f'
* i=-1 : return ERR
* i=16 : return ERR
*******************************************************************************/
static char num_to_asc(int i)
{
char c;
if ((i >= 0) && (i <= 9)) {
c = (char)(i + 0x30);
} else {
if ((i >= 10) && (i <= 15)) {
c = (char)(i + 0x61 - 10);
} else {
c = ERR; /* Error */
}
}
return(c);
}
/*******************************************************************************
* write_hex
* Type : void
* Ret val: none
* Argument : unsigned long x Hex value
* Function : Write 8 hex char for hex.
*******************************************************************************/
void write_hex(unsigned long x)
{
write_8hex(x, 0);
return;
}
/*******************************************************************************
* write_8hex
* Type : void
* Ret val: none
* Argument : unsigned long hex Hex value
* int mode 0:<LF> terminate, 1: add <SP>
* Function : Write 8 hex char for hex function.
*******************************************************************************/
static void write_8hex(unsigned long hex, int mode)
{
int i;
char c[10];
unsigned long tmp;
for (i = 7 ; i >= 0 ; i--) {
tmp = hex % 16;
hex = hex / 16;
c[i] = num_to_asc((int)tmp);
}
if (mode == 0)
c[8] = '\n'; /* add <LF> */
else
c[8] = ' '; /* add <SP> */
c[9] = 0; /* NULL terminate */
write_str(c); /* write 8 hex char */
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -