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
|
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "mon.h"
#define ABS(x) ((x)>0?(x):-(x))
/*
Monitor commands :
(empty cmd) Send input & update output
q Quit
a Set automatic mode (send input all the time)
m Set manual mode (send input when user sends empty command)
f <freq> Set frequency mode (every second, do 'freq' steps)
i <id> <v> Set input #id to value v (v is a string, transmitted as-is to simulator)
tf Tick fast (send 1 every cycle)
ts Tick for every second
tz Tick zero all the time
t <id> Set ticker input to #id
t Set no ticker input
s <i1> <i2> <i3> Set parameters for serial I/O
i1 : input for serial input
i2 : output for serial input busy signal
i3 : output for serial output
s Set no serial input
:<text> Send text to serial
d7 x x ... Set outputs to be interpreted as 7-bar digit displayer (up to 8 outputs, use - for no output)
*/
int mon_read_prologue(t_mon *mon) {
int i;
if (fscanf(mon->from_sim, " %d", &mon->n_inputs) == EOF) return -1;
if (fscanf(mon->from_sim, " %d", &mon->n_outputs) == EOF) return -1;
mon->inputs = malloc(mon->n_inputs * sizeof(t_input));
mon->outputs = malloc(mon->n_outputs * sizeof(t_output));
// Read input description
for (i = 0; i < mon->n_inputs; i++) {
mon->inputs[i].value[0] = '0';
mon->inputs[i].value[1] = 0;
if (fscanf(mon->from_sim, " %d", &mon->inputs[i].size) == EOF) return -1;
if (fscanf(mon->from_sim, " %s", mon->inputs[i].name) == EOF) return -1;
}
// Zeroify output description
for (i = 0; i < mon->n_outputs; i++) {
mon->outputs[i].name[0] = 0;
mon->outputs[i].v_bin[0] = 0;
mon->outputs[i].v_int = 0;
}
mon->step = 0;
mon->status = MS_RUN;
mon->target_freq = 1;
mon->max_freq = 10;
mon->actual_freq = 0;
mon->calc_time_usec = 10;
mon->clk = time(NULL);
mon->ticker_input = -1;
mon->ticker_mode = TM_SECOND;
mon->ser_in_in = -1;
mon->ser_in_busy_out = -1;
mon->ser_out = -1;
mon->ser_buf[0] = 0;
mon->ser_out_buf = 0;
for (i = 0; i < 8; i++) mon->d7[i] = -1;
return 0;
}
void mon_loop(t_mon *mon) {
disp_display(mon);
time_t prev_time = time(NULL);
int steps = 0;
int displayer_steps = 0;
while (1) {
handle_kbd(mon);
if (mon->status == MS_AUTO) {
mon_step(mon);
steps++;
if (time(NULL) != prev_time) {
mon->actual_freq = steps;
if (mon->actual_freq > mon->max_freq)
mon->max_freq = mon->actual_freq;
steps = 0;
prev_time = time(NULL);
}
displayer_steps++;
if (displayer_steps >= mon->actual_freq / 100) {
disp_display(mon);
displayer_steps = 0;
}
} else if (mon->status == MS_FREQ) {
if (prev_time != time(NULL)) {
mon->actual_freq = steps;
if (mon->actual_freq > mon->max_freq)
mon->max_freq = mon->actual_freq;
steps = 0;
prev_time = time(NULL);
if (mon->target_freq != mon->actual_freq && mon->actual_freq != 0) {
if (mon->actual_freq * 100 > 130 * mon->target_freq)
mon->calc_time_usec = 1000000 / mon->max_freq;
if (mon->actual_freq * 100 > mon->target_freq * 105)
mon->calc_time_usec -= 10;
if (mon->actual_freq < mon->target_freq)
mon->calc_time_usec += 10;
}
}
mon_step(mon);
steps++;
displayer_steps++;
if (displayer_steps >= mon->actual_freq / 100) {
disp_display(mon);
displayer_steps = 0;
}
int sleep = 1000000 / mon->target_freq - mon->calc_time_usec;
if (sleep > 0) usleep(sleep);
} else if (mon->status == MS_RUN) {
usleep(10000);
} else {
break;
}
}
}
void mon_handle_command(t_mon *mon, const char *c) {
if (c[0] == 0) { // empty command : run step
mon_step(mon);
disp_display(mon);
} else if (!strcmp(c, "q")) {
mon->status = MS_FINISH;
} else if (!strcmp(c, "a")) {
mon->status = MS_AUTO;
} else if (!strcmp(c, "m")) {
mon->status = MS_RUN;
} else if (c[0] == 'f') {
const char *p = c + 1;
mon->target_freq = 0;
while (isspace(*p)) p++;
while (isdigit(*p)) mon->target_freq = 10 * mon->target_freq + (*(p++) - '0');
if (mon->target_freq == 0) mon->target_freq = 1000;
mon->actual_freq = mon->target_freq;
mon->calc_time_usec = 1000;
mon->status = MS_FREQ;
} else if (c[0] == 'i') {
const char *p = c + 1;
int a = 0;
while (isspace(*p)) p++;
while (isdigit(*p)) a = a * 10 + (*(p++) - '0');
if (a >= 0 && a < mon->n_inputs) {
strcpy(mon->inputs[a].value, p);
}
} else if (!strcmp(c, "tf")) {
mon->ticker_mode = TM_FAST;
} else if (!strcmp(c, "ts")) {
mon->ticker_mode = TM_SECOND;
} else if (!strcmp(c, "tz")) {
mon->ticker_mode = TM_NO_TICK;
} else if (c[0] == 't') {
if (c[1] == 0) {
mon->ticker_input = -1;
} else {
const char *p = c + 1;
int a = 0;
while (isspace(*p)) p++;
while (isdigit(*p)) a = a * 10 + (*(p++) - '0');
if (a >= 0 && a < mon->n_inputs) {
mon->ticker_input = a;
}
}
} else if (c[0] == 's') {
if (c[1] == 0) {
mon->ser_in_in = mon->ser_in_busy_out = mon->ser_out = -1;
} else {
int u = -1, v = -1, w = -1;
const char *p = c + 1;
while (isspace(*p)) p++;
if (*p == '-') {
p++;
} else if (*p != 0) {
u = 0;
while (isdigit(*p)) u = u * 10 + (*(p++) - '0');
}
while (isspace(*p)) p++;
if (*p == '-') {
p++;
} else if (*p != 0) {
v = 0;
while (isdigit(*p)) v = v * 10 + (*(p++) - '0');
}
while (isspace(*p)) p++;
if (*p == '-') {
p++;
} else if (*p != 0) {
w = 0;
while (isdigit(*p)) w = w * 10 + (*(p++) - '0');
}
if (u < mon->n_inputs) mon->ser_in_in = u;
if (v < mon->n_outputs) mon->ser_in_busy_out = v;
if (w < mon->n_outputs) mon->ser_out = w;
}
} else if (c[0] == ':') {
strcat(mon->ser_buf, c + 1);
strcat(mon->ser_buf, "\n");
} else if (c[0] == 'd' && c[1] == '7') {
const char *p = c + 2;
int i = 0;
for (i = 0; i < 8; i++) {
while (isspace(*p)) p++;
if (*p == 0) {
mon->d7[i] = -1;
} else if (*p == '-') {
mon->d7[i] = -1;
p++;
} else {
mon->d7[i] = 0;
while (isdigit(*p)) mon->d7[i] = 10 * mon->d7[i] + (*(p++) - '0');
if (mon->d7[i] >= mon->n_outputs) mon->d7[i] = -1;
}
}
}
disp_display(mon);
}
void mon_step(t_mon *mon) {
int i = 0;
// Get ticker
int ticker = 0;
if (mon->ticker_mode == TM_SECOND) {
time_t new_clk = time(NULL);
ticker = new_clk - mon->clk;
mon->clk = new_clk;
} else if (mon->ticker_mode == TM_FAST) {
ticker = 1;
}
if (mon->ticker_input != -1) {
if (mon->inputs[mon->ticker_input].size == 1)
ticker = (ticker != 0 ? 1 : 0);
sprintf(mon->inputs[mon->ticker_input].value, "/%d", ticker);
}
// Update serial input
if (mon->ser_in_in != -1 && mon->ser_in_busy_out != -1
&& mon->outputs[mon->ser_in_busy_out].v_int == 0) {
if (mon->ser_buf[0] != 0) {
sprintf(mon->inputs[mon->ser_in_in].value, "/%d", (int)mon->ser_buf[0]);
char *p = mon->ser_buf;
while (*p) {
p[0] = p[1];
p++;
}
} else {
sprintf(mon->inputs[mon->ser_in_in].value, "0");
}
}
// Send inputs to simulator
fprintf(mon->to_sim, "FEED\n");
for (i = 0; i < mon->n_inputs; i++) {
fprintf(mon->to_sim, "%s\n", mon->inputs[i].value);
}
fflush(mon->to_sim);
// Read outputs from simulator
int magic;
fscanf(mon->from_sim, " %x", &magic);
if (magic != 0xFED) {
mon->status = MS_PERROR;
return;
}
for (i = 0; i < mon->n_outputs; i++) {
fscanf(mon->from_sim, " %s %s %d",
mon->outputs[i].name,
mon->outputs[i].v_bin,
&mon->outputs[i].v_int);
}
// Update serial output
if (mon->ser_out != -1) {
mon->ser_out_buf = mon->outputs[mon->ser_out].v_int;
}
mon->step++;
disp_display_ser(mon);
}
|