-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgetch_light.as
More file actions
94 lines (79 loc) · 3.36 KB
/
getch_light.as
File metadata and controls
94 lines (79 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/********************************* 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 = 10
code 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,0
int r0 = input(r0, stdin_status_port)// read status port
int16 r0 = r0 != 0 // bit 0-15 = number of bytes in input buffer
return
// _getch function
// waits for input from standard input. returns one character in the interval 0 - 0xFF
// no error checking
_getch function public reguse = 1,0
do { // keep reading until input data valid
int r0 = input(r0, stdin_port) // read from input port
}
while (int !(r0 & 0x100)); // bit 8 = data valid
int8 r0 = r0 // isolate bit 0-7
return
// _getche function
// same as _getch
// echoes character to standard output
_getche function public reguse = 1,0
do { // keep reading until input data valid
int r0 = input(r0, stdin_port) // read from input port
}
while (int !(r0 & 0x100)); // bit 8 = data valid
int8 r0 = r0 // isolate bit 0-7
int output(r0, r0, stdout_port) // output the same character
return
// _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,0
int r0 = input(r0, stdin_port) // read from input port
if (int (r0 & 0x100)) { // bit 8 = data valid
int8 r0 = r0 // isolate bit 0-7
return
}
int r0 = -1 // input buffer empty. return -1
return
code end