/
consio.c
91 lines (75 loc) · 1.59 KB
/
consio.c
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
/*
* Console I/O routines
*/
#include "forth.h"
#include "compiler.h"
#include "stdlib.h"
extern void uart_write_bytes(int, char *, int);
extern int uart_read_bytes(int, char *, int, int);
extern void init_uart(void);
int isinteractive() { return (1); }
int isstandalone() { return (1); }
void raw_emit(unsigned char c)
{
uart_write_bytes(0, &c, 1);
}
void emit(u_char c, cell *up)
{
if (c == '\n')
raw_emit('\r');
raw_emit(c);
}
u_char key_is_avail = 0;
u_char the_key;
int key_avail(cell *up)
{
if (key_is_avail) {
return (cell)-1;
}
if(uart_read_bytes(0, &the_key, 1, 0)) {
key_is_avail = 1;
return (cell)-1;
}
return 0;
}
int key(cell *up)
{
cell this_key;
while (!key_avail(up)) {}
key_is_avail = 0;
return (cell)the_key;
}
void init_io(int argc, char **argv, cell *up)
{
key_is_avail = 0;
init_uart();
}
int caccept(char *addr, cell count, cell *up)
{
return lineedit(addr, count, up);
}
// Defines the resolution of c_puts
void output_redirect(const char *str) {
puts(str);
}
void alerror(char *str, int len, cell *up)
{
while (len--)
emit((u_char)*str++, up);
/* Sequences of calls to error() eventually end with a newline */
V(NUM_OUT) = 0;
}
// moreinput() returns 0 when the console input stream has been closed for good
int moreinput() { return (1); }
char *getmem(u_cell nbytes, cell *up)
{
return (char *)malloc(nbytes);
}
void memfree(char *ptr, cell *up)
{
free(ptr);
}
char * memresize(char *ptr, u_cell nbytes, cell *up)
{
return (char *)realloc(ptr, nbytes);
}