| 1 |
88 |
Agner |
/********************************* getch_light.as ****************************
|
| 2 |
|
|
* Author: Agner Fog
|
| 3 |
|
|
* date created: 2021-05-25
|
| 4 |
|
|
* Last modified: 2021-05-25
|
| 5 |
|
|
* Version: 1.11
|
| 6 |
|
|
* Project: ForwardCom library libc_light.li
|
| 7 |
|
|
* Description: Common C functions: _kbhit, getch, getche
|
| 8 |
|
|
* Nonstandard function: getch_nonblocking
|
| 9 |
|
|
*
|
| 10 |
|
|
* This version is for small CPUs with limited capabilities
|
| 11 |
|
|
* The following instructions are avoided: mul, div, push, pop, sys_call.
|
| 12 |
|
|
* Input comes directly from input port 8
|
| 13 |
|
|
* Output to stdout goes directly to output port 10
|
| 14 |
|
|
*
|
| 15 |
|
|
* The functions _kbhit, getch, getche are common, but not part of the official C standard.
|
| 16 |
|
|
* getch_nonblocking is my own invention. It is sadly missing in common systems
|
| 17 |
|
|
*
|
| 18 |
|
|
* C declarations:
|
| 19 |
|
|
* int _kbhit(void);
|
| 20 |
|
|
* int getch(void);
|
| 21 |
|
|
* int getche(void);
|
| 22 |
|
|
* int getch_nonblocking(void);
|
| 23 |
|
|
*
|
| 24 |
|
|
* Description:
|
| 25 |
|
|
* _kbhit: return 1 if there is at least one character in the input buffer, 0 if buffer empty
|
| 26 |
|
|
* getch: waits for input from stdin. returns one character
|
| 27 |
|
|
* getche: same as getch. The input is echoed to the standard output
|
| 28 |
|
|
* getch_nonblocking: reads one character from stdin without waiting. returns -1 if the input buffer is empty
|
| 29 |
|
|
*
|
| 30 |
|
|
* Copyright 2021 GNU General Public License http://www.gnu.org/licenses
|
| 31 |
|
|
*****************************************************************************/
|
| 32 |
|
|
|
| 33 |
|
|
// define input and output ports
|
| 34 |
|
|
%stdin_port = 8
|
| 35 |
|
|
%stdin_status_port = 9
|
| 36 |
|
|
%stdout_port = 10
|
| 37 |
|
|
|
| 38 |
|
|
|
| 39 |
|
|
code section execute
|
| 40 |
|
|
|
| 41 |
|
|
// _kbhit function
|
| 42 |
|
|
// returns 0 if there is no input
|
| 43 |
|
|
// returns 1 if the standard input input buffer contains at least one character
|
| 44 |
|
|
_kbhit function public reguse = 1,0
|
| 45 |
|
|
__kbhit function public reguse = 1,0
|
| 46 |
|
|
|
| 47 |
|
|
int r0 = input(r0, stdin_status_port)// read status port
|
| 48 |
|
|
int16 r0 = r0 != 0 // bit 0-15 = number of bytes in input buffer
|
| 49 |
|
|
return
|
| 50 |
|
|
|
| 51 |
|
|
|
| 52 |
|
|
// _getch function
|
| 53 |
|
|
// waits for input from standard input. returns one character in the interval 0 - 0xFF
|
| 54 |
|
|
// no error checking
|
| 55 |
|
|
_getch function public reguse = 1,0
|
| 56 |
|
|
|
| 57 |
|
|
do { // keep reading until input data valid
|
| 58 |
|
|
int r0 = input(r0, stdin_port) // read from input port
|
| 59 |
|
|
}
|
| 60 |
|
|
while (int !(r0 & 0x100)); // bit 8 = data valid
|
| 61 |
|
|
int8 r0 = r0 // isolate bit 0-7
|
| 62 |
|
|
return
|
| 63 |
|
|
|
| 64 |
|
|
|
| 65 |
|
|
// _getche function
|
| 66 |
|
|
// same as _getch
|
| 67 |
|
|
// echoes character to standard output
|
| 68 |
|
|
_getche function public reguse = 1,0
|
| 69 |
|
|
|
| 70 |
|
|
do { // keep reading until input data valid
|
| 71 |
|
|
int r0 = input(r0, stdin_port) // read from input port
|
| 72 |
|
|
}
|
| 73 |
|
|
while (int !(r0 & 0x100)); // bit 8 = data valid
|
| 74 |
|
|
int8 r0 = r0 // isolate bit 0-7
|
| 75 |
|
|
int output(r0, r0, stdout_port) // output the same character
|
| 76 |
|
|
return
|
| 77 |
|
|
|
| 78 |
|
|
|
| 79 |
|
|
// _getch_nonblocking function
|
| 80 |
|
|
// reads input from standard input. Does not wait for input
|
| 81 |
|
|
// returns one character in the interval 0 - 0xFF if input available
|
| 82 |
|
|
// returns -1 if input buffer empty
|
| 83 |
|
|
// future implementations may return other negative values in case of error
|
| 84 |
|
|
_getch_nonblocking function public reguse = 1,0
|
| 85 |
|
|
|
| 86 |
|
|
int r0 = input(r0, stdin_port) // read from input port
|
| 87 |
|
|
if (int (r0 & 0x100)) { // bit 8 = data valid
|
| 88 |
|
|
int8 r0 = r0 // isolate bit 0-7
|
| 89 |
|
|
return
|
| 90 |
|
|
}
|
| 91 |
|
|
int r0 = -1 // input buffer empty. return -1
|
| 92 |
|
|
return
|
| 93 |
|
|
|
| 94 |
|
|
code end
|