forked from organix/pijFORTHos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspberry.c
285 lines (260 loc) · 5.94 KB
/
raspberry.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
/*
* raspberry.c -- Raspberry Pi kernel routines written in C
*/
#include "raspi.h"
#include "timer.h"
#include "serial.h"
#include "xmodem.h"
/* Declare symbols from FORTH */
extern void jonesforth();
/* Exported procedures (force full register discipline) */
extern void k_start(u32 sp);
extern void monitor();
extern int putchar(int c);
extern int getchar();
extern void hexdump(const u8* p, int n);
extern void dump256(const u8* p);
/* Private data structures */
static char linebuf[256]; // line editing buffer
static int linepos = 0; // read position
static int linelen = 0; // write position
static const char* hex = "0123456789abcdef"; // hexadecimal map
/*
* Print u32 in hexadecimal to serial port
*/
void
serial_hex32(u32 w)
{
serial_write(hex[0xF & (w >> 28)]);
serial_write(hex[0xF & (w >> 24)]);
serial_write(hex[0xF & (w >> 20)]);
serial_write(hex[0xF & (w >> 16)]);
serial_write(hex[0xF & (w >> 12)]);
serial_write(hex[0xF & (w >> 8)]);
serial_write(hex[0xF & (w >> 4)]);
serial_write(hex[0xF & w]);
}
/*
* Print u8 in hexadecimal to serial port
*/
void
serial_hex8(u8 b)
{
serial_write(hex[0xF & (b >> 4)]);
serial_write(hex[0xF & b]);
}
/*
* Pretty-printed memory dump
*/
void
hexdump(const u8* p, int n)
{
int i;
int c;
while (n > 0) {
serial_hex32((u32)p);
serial_write(' ');
for (i = 0; i < 16; ++i) {
if (i == 8) {
serial_write(' ');
}
if (i < n) {
serial_write(' ');
serial_hex8(p[i]);
} else {
serial_rep(' ', 3);
}
}
serial_rep(' ', 2);
serial_write('|');
for (i = 0; i < 16; ++i) {
if (i < n) {
c = p[i];
if ((c >= ' ') && (c < 0x7F)) {
serial_write(c);
} else {
serial_write('.');
}
} else {
serial_write(' ');
}
}
serial_write('|');
serial_eol();
p += 16;
n -= 16;
}
}
/*
* Dump 256 bytes (handy for asm debugging, just load r0)
*/
void
dump256(const u8* p)
{
hexdump(p, 256);
}
/*
* Traditional single-character "cooked" output
*/
int
putchar(int c)
{
if (c == '\n') {
serial_eol();
} else {
serial_write(c);
}
return c;
}
/*
* Single-character "cooked" input (unbuffered)
*/
static int
_getchar()
{
int c;
c = serial_read();
if (c == '\r') {
c = '\n';
}
return c;
}
/*
* Traditional single-character input (buffered)
*/
int
getchar()
{
char* editline();
while (linepos >= linelen) {
editline();
}
return linebuf[linepos++];
}
/*
* Get single line of edited input
*/
char*
editline()
{
int c;
linelen = 0; // reset write position
while (linelen < (sizeof(linebuf) - 1)) {
c = _getchar();
if (c == '\b') {
if (--linelen < 0) {
linelen = 0;
continue; // no echo
}
} else {
linebuf[linelen++] = c;
}
putchar(c); // echo input
if (c == '\n') {
break; // end-of-line
}
}
linebuf[linelen] = '\0'; // ensure NUL termination
linepos = 0; // reset read position
return linebuf;
}
/*
* Wait for whitespace character from keyboard
*/
int
wait_for_kb()
{
int c;
for (;;) {
c = _getchar();
if ((c == '\r') || (c == '\n') || (c == ' ')) {
return c;
}
}
}
#define KERNEL_ADDR (0x00008000)
#define UPLOAD_ADDR (0x00010000)
#define UPLOAD_LIMIT (0x00007F00)
/*
* Simple bootstrap monitor
*/
void
monitor()
{
int c;
int z = 0;
int len = 0;
// display banner
serial_eol();
serial_puts("^D=exit-monitor ^Z=toggle-hexadecimal ^L=xmodem-upload");
serial_eol();
// echo console input to output
for (;;) {
if (z) { // "raw" mode
c = serial_read();
serial_hex8(c); // display as hexadecimal value
serial_write('=');
if ((c > 0x20) && (c < 0x7F)) { // echo printables
serial_write(c);
} else {
serial_write(' ');
}
serial_write(' ');
} else { // "cooked" mode
c = _getchar();
putchar(c);
}
if (c == 0x04) { // ^D to exit monitor loop
break;
}
if (c == 0x1A) { // ^Z toggle hexadecimal substitution
z = !z;
}
if (c == 0x0C) { // ^L xmodem file upload
serial_eol();
serial_puts("START XMODEM...");
len = rcv_xmodem((u8*)UPLOAD_ADDR, UPLOAD_LIMIT);
putchar(wait_for_kb());
if (len < 0) {
serial_puts("UPLOAD FAILED!");
serial_eol();
} else {
hexdump((u8*)UPLOAD_ADDR, 128); // show first block
serial_rep('.', 3);
serial_eol();
hexdump((u8*)UPLOAD_ADDR + (len - 128), 128); // and last block
serial_puts("0x");
serial_hex32(len);
serial_puts(" BYTES RECEIVED."); // and length
serial_eol();
serial_puts("^W=boot-uploaded-image");
serial_eol();
}
}
if ((c == 0x17) && (len > 0)) { // ^W copy upload and boot
serial_eol();
BRANCH_TO(UPLOAD_ADDR); // should not return...
}
}
serial_eol();
serial_puts("OK ");
}
/*
* Entry point for C code
*/
void
k_start(u32 sp)
{
timer_init();
serial_init();
// wait for initial interaction
serial_puts(";-) ");
putchar(wait_for_kb());
// display banner
serial_puts("pijFORTHos 0.1.8 ");
serial_puts("sp=0x");
serial_hex32(sp);
serial_eol();
// jump to FORTH entry-point
jonesforth();
}