/
forth.c
313 lines (276 loc) · 6.22 KB
/
forth.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
extern void PUT32 ( unsigned int, unsigned int );
extern unsigned int GET32 ( unsigned int );
extern void dummy ( unsigned int );
#define GPFSEL1 0x20200004
#define GPSET0 0x2020001C
#define GPCLR0 0x20200028
#define GPPUD 0x20200094
#define GPPUDCLK0 0x20200098
#define AUX_ENABLES 0x20215004
#define AUX_MU_IO_REG 0x20215040
#define AUX_MU_IER_REG 0x20215044
#define AUX_MU_IIR_REG 0x20215048
#define AUX_MU_LCR_REG 0x2021504C
#define AUX_MU_MCR_REG 0x20215050
#define AUX_MU_LSR_REG 0x20215054
#define AUX_MU_MSR_REG 0x20215058
#define AUX_MU_SCRATCH 0x2021505C
#define AUX_MU_CNTL_REG 0x20215060
#define AUX_MU_STAT_REG 0x20215064
#define AUX_MU_BAUD_REG 0x20215068
#define ESCAPE_SEQ 0x20
#define MAX_WORD_LENGTH 64
#define MAX_INTEGER_LENGTH 10
#define STACK_SIZE 40
//GPIO14 TXD0 and TXD1
//GPIO15 RXD0 and RXD1
//alt function 5 for uart1
//alt function 0 for uart0
//((250,000,000/115200)/8)-1 = 270
static unsigned int si = 0;
static int stack[STACK_SIZE];
static unsigned long long types = 0;
inline char isdigit ( unsigned char c )
{
switch (c){
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57:
return 1;
break;
default:
return 0;
break;
}
}
void uart_putc ( unsigned int c )
{
while(1)
{
if(GET32(AUX_MU_LSR_REG)&0x20) break;
}
PUT32(AUX_MU_IO_REG,c);
}
//------------------------------------------------------------------------
void uart_puts ( char* str )
{
unsigned int rb = 0;
while(str[rb] != 0)
{
uart_putc(str[rb]);
rb++;
}
}
//------------------------------------------------------------------------
void hexstrings ( unsigned int d )
{
unsigned int rb;
unsigned int rc;
rb=32;
while(1)
{
rb-=4;
rc=(d>>rb)&0xF;
if(rc>9) rc+=0x37; else rc+=0x30;
uart_putc(rc);
if(rb==0) break;
}
uart_putc(0x20);
}
//------------------------------------------------------------------------
void hexstring ( unsigned int d )
{
hexstrings(d);
uart_putc(0x0D);
uart_putc(0x0A);
}
//------------------------------------------------------------------------
void uart_puti ( int n )
{
int a;
if (n < 0) {
uart_putc('-');
n = -n;
}
if( n > 9 ) {
a = n / 10;
n -= 10 * a;
uart_puti(a);
}
uart_putc('0'+n);
}
char isnumber ( unsigned char* buf, unsigned char index )
{
unsigned char scan;
if (!isdigit(buf[0]) && (buf[0] != 43) && (buf[0] != 45)){
return 0;
}
if (index > MAX_INTEGER_LENGTH){
return 0;
}
if ((index == 1) && ((buf[0] == 43) || (buf[0] == 45))){
return 0;
}
for (scan = index - 1; scan > 0 ;scan--)
{
if (!isdigit(buf[scan])) return 0;
}
return 1;
}
int parse(unsigned char * buffer, unsigned char index, unsigned char c) {
if (index > MAX_WORD_LENGTH) {
return -1;
}
if (c == ESCAPE_SEQ) {
return 1;
}
buffer[index] = c;
return 0;
}
void stack_push ( int v, char type )
{
/* if (si >= STACK_SIZE){ */
/* uart_puts("stack full. can't push.\r\n"); */
/* return; */
/* } */
stack[si++] = v;
types ^= (-type ^ types) & (1ULL << si);
}
int stack_pop ( char* type )
{
/* if (si == 0){ */
/* uart_puts("stack empty. can't pop.\r\n"); */
/* return 0; */
/* } */
*type = (types >> si) & 1ULL;
return stack[--si];
}
void handle_number ( unsigned char* buf, unsigned char index ) {
unsigned char i, neg;
int integer=0;
if (buf[0] == '-'){
i = 1;
neg = 1;
} else {
i = 0;
neg = 0;
}
for (; i < index ;i++)
{
integer = 10*integer - (buf[i] - '0');
}
if (!neg) {
integer = -integer;
}
stack_push(integer, 1);
}
void handle_symbol ( unsigned char* buf, unsigned char index ) {
char type, type2;
int v1, v2;
switch(buf[0]){
case '.':
uart_puti(stack_pop(&type));
uart_puts("\r\n");
break;
case '+':
v1 = stack_pop(&type);
stack_push(v1 + stack_pop(&type), 1);
break;
case '-':
v1 = stack_pop(&type);
stack_push(v1 - stack_pop(&type), 1);
break;
case '*':
v1 = stack_pop(&type);
stack_push(v1 * stack_pop(&type), 1);
break;
case '/':
if((v1 = stack_pop(&type)) == 0) {
uart_puts("can't divide by zero\r\n");
return;
}
stack_push(stack_pop(&type) / v1, 1);
break;
case 'd': /* dup */
v1 = stack_pop(&type);
stack_push(v1, type);
stack_push(v1, type);
break;
case 's': /* swap */
v1 = stack_pop(&type);
v2 = stack_pop(&type2);
stack_push(v1, type);
stack_push(v2, type2);
break;
}
}
void handle_word ( unsigned char* buf, unsigned char index )
{
if (isnumber(buf, index)) {
handle_number(buf, index);
} else {
handle_symbol(buf, index);
}
}
int notmain ( unsigned int earlypc )
{
unsigned int ra;
unsigned char word_index = 0;
/* unsigned char i; */
unsigned char word_buf[MAX_WORD_LENGTH];
PUT32(AUX_ENABLES,1);
PUT32(AUX_MU_IER_REG,0);
PUT32(AUX_MU_CNTL_REG,0);
PUT32(AUX_MU_LCR_REG,3);
PUT32(AUX_MU_MCR_REG,0);
PUT32(AUX_MU_IER_REG,0);
PUT32(AUX_MU_IIR_REG,0xC6);
PUT32(AUX_MU_BAUD_REG,270);
ra=GET32(GPFSEL1);
ra&=~(7<<12); //gpio14
ra|=2<<12; //alt5
ra&=~(7<<15); //gpio15
ra|=2<<15; //alt5
PUT32(GPFSEL1,ra);
PUT32(GPPUD,0);
for(ra=0;ra<150;ra++) dummy(ra);
PUT32(GPPUDCLK0,(1<<14)|(1<<15));
for(ra=0;ra<150;ra++) dummy(ra);
PUT32(GPPUDCLK0,0);
PUT32(AUX_MU_CNTL_REG,3);
uart_puts("Forth interpreter ready. Type words to list words.\r\n");
while(1)
{
while(1)
{
if(GET32(AUX_MU_LSR_REG)&0x01) break;
}
ra=GET32(AUX_MU_IO_REG);
uart_putc(ra);
switch (parse(word_buf, word_index, (unsigned char)ra)){
case 0:
/* uart_puts("PUT\r\n"); */
word_index++;
break;
case 1:
/* for (i = 0; i < word_index; i++) { */
/* uart_putc(word_buf[i]); */
/* } */
/* uart_puts("\r\n"); */
handle_word(word_buf, word_index);
word_index = 0;
break;
case -1:
uart_puts("Fatal. Resetting..\r\n");
break;
}
}
return(0);
}