-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.c
171 lines (153 loc) · 3.4 KB
/
loader.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
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "mem.h"
#include "vm.h"
#include "sched.h"
/* note: buf should be able to hold word_size+1 bytes, since we use scanf to read */
void read_word(char *buf)
{
size_t len;
int c;
len = 0;
if (scanf("%4s", buf) != 1 || (len=strlen(buf)) < 4) {
/* short or failed read */
fprintf(stderr, "read_word: short or failed read, trying to recover\n");
/* pull the remaining non-whitespace chars */
do {
c = fgetc(stdin);
if (!isspace(c))
buf[len++] = c;
} while (len < 4 && !feof(stdin));
}
}
/* FIXME: we need to ignore the rest of the line either here or in the dispatch func,
* but still handle headers. Probably. */
static int check_header(char *word, int *loc)
{
char buf[5];
size_t len;
size_t pid, offset;
int c;
scanf("%4s", buf);
len = strlen(buf);
if (len == 4) { /* normal word */
pid = *loc/100;
offset = *loc%100;
*loc += 2;
if (store(pid, word, offset++) != 0) {
fprintf(stderr, "check_header: store failed\n");
return 1;
}
if (offset > 99) {
fprintf(stderr, "check_header: read more than 99 words without seeing a BRAIN10 header\n");
return 1;
}
if (store(pid, buf, offset) != 0) {
fprintf(stderr, "check_header: store failed\n");
return 1;
}
} else if (strcasecmp(buf, "N10") == 0 && len == 3) { /* BRAIN10 header */
if (*loc > 0) {
/* fast forward to end of address space */
*loc += 100 - (*loc)%100;
}
return 0;
} else {
do {
c = fgetc(stdin);
if (!isspace(c))
buf[len++] = c;
} while(len < 4 && !feof(stdin));
if (feof(stdin)) {
fprintf(stderr, "check_header: Unexpected EOF\n");
return 1;
}
return 0;
}
}
static int store_word(char *word, int *loc)
{
size_t pid, offset;
pid = *loc/100;
offset = *loc%100;
(*loc)++;
return store(pid, word, offset);
}
/* notes: entries are checked in order, first match wins.
NULL string matches any word, NULL action terminates.
only the first 4 chars are checked. */
struct str_act {
char *match;
int (*action)(char *word, int *loc);
};
static struct str_act parse_table[]=
{
{"BRAI", check_header},
{"DATA", NULL},
{NULL, store_word},
};
/* returns number of programs loaded */
int load_progs()
{
struct str_act *p;
int i,addr;
char buf[5];
addr=0;
while (addr<1000) {
read_word(buf);
p = parse_table;
for (i=0; i<LEN(parse_table); i++) {
if (p->match == NULL || strncasecmp(buf, p->match, 4) == 0)
break;
p++;
}
if (i >= LEN(parse_table)) {
fprintf(stderr, "load_progs: no matching action for word\n");
return 1;
}
if (p->action == NULL) {
while (fgetc(stdin) != '\n'); /* ignore the rest of the line */
return addr/100+1;
}
if (p->action(buf, &addr) != 0) {
fprintf(stderr, "load_progs: action failed\n");
return -1;
}
while (fgetc(stdin) != '\n');
}
return addr/100+1;
}
int main()
{
struct proc *p;
int procs,i;
set_mem('0');
procs = load_progs();
if (procs < 0)
return 1;
if (sched_init() != 0) {
fprintf(stderr, "Scheduler init failed\n");
return 1;
}
msg_init();
for (i=0; i<procs; i++) {
p = &proc_table[i];
/* set up the process */
memset(p, '0', sizeof(struct proc));
p->c = 'F';
p->stack_base = 0;
p->pid = i;
int2word(i*100, p->br);
int2word(99, p->lr);
/* schedule process */
sched_reset(i);
sched_resume(i);
}
/* print_mem(); */
sched_run();
/* print_mem(); */
return 0;
}