URL
https://opencores.org/ocsvn/forwardcom/forwardcom/trunk
Subversion Repositories forwardcom
[/] [forwardcom/] [libraries/] [getch_light.as] - Rev 96
Go to most recent revision | Compare with Previous | Blame | View Log
/********************************* getch_light.as ***************************** Author: Agner Fog* date created: 2021-05-25* Last modified: 2021-05-25* Version: 1.11* Project: ForwardCom library libc_light.li* Description: Common C functions: _kbhit, getch, getche* Nonstandard function: getch_nonblocking** This version is for small CPUs with limited capabilities* The following instructions are avoided: mul, div, push, pop, sys_call.* Input comes directly from input port 8* Output to stdout goes directly to output port 10** The functions _kbhit, getch, getche are common, but not part of the official C standard.* getch_nonblocking is my own invention. It is sadly missing in common systems** C declarations:* int _kbhit(void);* int getch(void);* int getche(void);* int getch_nonblocking(void);** Description:* _kbhit: return 1 if there is at least one character in the input buffer, 0 if buffer empty* getch: waits for input from stdin. returns one character* getche: same as getch. The input is echoed to the standard output* getch_nonblocking: reads one character from stdin without waiting. returns -1 if the input buffer is empty** Copyright 2021 GNU General Public License http://www.gnu.org/licenses*****************************************************************************/// define input and output ports%stdin_port = 8%stdin_status_port = 9%stdout_port = 10code section execute// _kbhit function// returns 0 if there is no input// returns 1 if the standard input input buffer contains at least one character_kbhit function public reguse = 1,0__kbhit function public reguse = 1,0int r0 = input(r0, stdin_status_port)// read status portint16 r0 = r0 != 0 // bit 0-15 = number of bytes in input bufferreturn// _getch function// waits for input from standard input. returns one character in the interval 0 - 0xFF// no error checking_getch function public reguse = 1,0do { // keep reading until input data validint r0 = input(r0, stdin_port) // read from input port}while (int !(r0 & 0x100)); // bit 8 = data validint8 r0 = r0 // isolate bit 0-7return// _getche function// same as _getch// echoes character to standard output_getche function public reguse = 1,0do { // keep reading until input data validint r0 = input(r0, stdin_port) // read from input port}while (int !(r0 & 0x100)); // bit 8 = data validint8 r0 = r0 // isolate bit 0-7int output(r0, r0, stdout_port) // output the same characterreturn// _getch_nonblocking function// reads input from standard input. Does not wait for input// returns one character in the interval 0 - 0xFF if input available// returns -1 if input buffer empty// future implementations may return other negative values in case of error_getch_nonblocking function public reguse = 1,0int r0 = input(r0, stdin_port) // read from input portif (int (r0 & 0x100)) { // bit 8 = data validint8 r0 = r0 // isolate bit 0-7return}int r0 = -1 // input buffer empty. return -1returncode end
Go to most recent revision | Compare with Previous | Blame | View Log
