summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--csim/sim.c9
-rw-r--r--monitor/main.c6
-rw-r--r--monitor/mon.c13
-rw-r--r--monitor/mon.h1
4 files changed, 27 insertions, 2 deletions
diff --git a/csim/sim.c b/csim/sim.c
index 1dd2791..4451d0e 100644
--- a/csim/sim.c
+++ b/csim/sim.c
@@ -74,6 +74,13 @@ void read_inputs(t_machine *m, FILE *stream) {
t_id var;
t_program *p = m->prog;
+ int magic;
+ fscanf(stream, " %x", &magic);
+ if (magic != 0xFEED) {
+ fprintf(stderr, "(simulator) Protocol error.\n");
+ exit(1);
+ }
+
if (p->n_inputs == 0) return; // nothing to do
for (i = 0; i < p->n_inputs; i++) {
@@ -213,6 +220,8 @@ void write_outputs(t_machine *m, FILE *stream) {
t_value v, mask;
t_program *p = m->prog;
+ fprintf(stream, "FED\n");
+
for (i = 0; i < p->n_outputs; i++) {
var = p->outputs[i];
fprintf(stream, "%s\t", p->vars[var].name);
diff --git a/monitor/main.c b/monitor/main.c
index 1de722c..ff48dcf 100644
--- a/monitor/main.c
+++ b/monitor/main.c
@@ -36,6 +36,7 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "(simulator) Could not open mon2sim for reading (%s).\n", strerror(errno));
return 1;
}
+ fprintf(stderr, "(simulator) Syncronization with monitor apparently OK.\n");
execv (argv[1], argv + 1);
}
@@ -55,6 +56,7 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "\nError while launching simulator (%d).\n", err);
goto finish;
}
+ fprintf(stderr, "(monitor) Syncronization with simulator apparently OK.\n");
// Setup display
disp_init();
@@ -65,6 +67,10 @@ int main(int argc, char *argv[]) {
// clean up
disp_finish();
+ if (mon.status == MS_PERROR) {
+ fprintf(stderr, "(monitor) Exiting due to protocol error.\n");
+ }
+
finish:
kill(sim_pid, SIGTERM);
waitpid(sim_pid);
diff --git a/monitor/mon.c b/monitor/mon.c
index baef98b..780e8ba 100644
--- a/monitor/mon.c
+++ b/monitor/mon.c
@@ -82,7 +82,7 @@ void mon_loop(t_mon *mon) {
int displayer_steps = 0;
- while (mon->status != MS_FINISH) {
+ while (1) {
handle_kbd(mon);
if (mon->status == MS_AUTO) {
mon_step(mon);
@@ -125,8 +125,10 @@ void mon_loop(t_mon *mon) {
}
int sleep = 1000000 / mon->target_freq - mon->calc_time_usec;
if (sleep > 0) usleep(sleep);
- } else {
+ } else if (mon->status == MS_RUN) {
usleep(10000);
+ } else {
+ break;
}
}
}
@@ -266,12 +268,19 @@ void mon_step(t_mon *mon) {
}
// 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,
diff --git a/monitor/mon.h b/monitor/mon.h
index 32d60c4..4430e5f 100644
--- a/monitor/mon.h
+++ b/monitor/mon.h
@@ -20,6 +20,7 @@ typedef struct {
} t_input;
typedef enum {
+ MS_PERROR,
MS_FINISH,
MS_RUN,
MS_AUTO,