diff options
-rw-r--r-- | cpu/Makefile | 4 | ||||
-rw-r--r-- | cpu/example_cpu.ml | 11 | ||||
-rw-r--r-- | csim/load.c | 2 | ||||
-rw-r--r-- | csim/sim.c | 4 | ||||
-rw-r--r-- | monitor/disp.c | 26 | ||||
-rw-r--r-- | monitor/main.c | 25 | ||||
-rwxr-xr-x | monitor/mon | bin | 14209 -> 17231 bytes | |||
-rw-r--r-- | monitor/mon.c | 127 | ||||
-rw-r--r-- | monitor/mon.h | 14 |
9 files changed, 189 insertions, 24 deletions
diff --git a/cpu/Makefile b/cpu/Makefile index ed59bb5..a39375b 100644 --- a/cpu/Makefile +++ b/cpu/Makefile @@ -8,10 +8,10 @@ SIM=../csim/csim MON=../monitor/mon all: _build/cpu_opt.dumb - $(MON) $(SIM) -n 8 -rom ROM0 prog_rom0.rom $< + $(MON) $(SIM) -rom ROM0 prog_rom0.rom $< %.sim: _build/%.dumb - $(SIM) $< + $(SIM) -n 12 $< _build/%.dumb _build/%.snet _build/%_opt.dumb _build/%_opt.snet: _build/%.net $(SCHED) $< diff --git a/cpu/example_cpu.ml b/cpu/example_cpu.ml index 2d60bec..e626cae 100644 --- a/cpu/example_cpu.ml +++ b/cpu/example_cpu.ml @@ -10,10 +10,13 @@ let sumz n i = let p = let width = 16 in - let sum, r = sumz width (get "in") in + let sum, r = sumz width (get "input") in program - [ "in", width ] - [ "out", width, sum; - "r", 1, r ] + [ "input", width; + "ser_in", 8 ] + [ "output", width, sum; + "r", 1, r; + "ser_busy", 1, (const "0"); + "ser_out", 8, get "ser_in"; ] let () = Netlist_gen.print stdout p diff --git a/csim/load.c b/csim/load.c index 485a743..e54c0f0 100644 --- a/csim/load.c +++ b/csim/load.c @@ -41,7 +41,7 @@ void add_rom(const char *prefix, FILE *file) { eat_comment(file); if (fscanf(file, "/%lu", &(rom->data[i]))) { // ok, value is read - } else if (fscanf(file, "x%x", &(rom->data[i]))) { + } else if (fscanf(file, "x%lx", &(rom->data[i]))) { // ok, value is read } else { rom->data[i] = read_bool(file, NULL); @@ -61,6 +61,7 @@ void machine_banner(t_machine *m, FILE *stream) { m->prog->vars[m->prog->inputs[i]].name); } fprintf(stream, "\n"); + fflush(stream); } void read_inputs(t_machine *m, FILE *stream) { @@ -80,6 +81,8 @@ void read_inputs(t_machine *m, FILE *stream) { fscanf(stream, " "); if (fscanf(stream, "/%lu", &(m->var_values[var]))) { // ok, value is read + } else if (fscanf(stream, "x%lx", &(m->var_values[var]))) { + // ok, value is read } else { m->var_values[var] = read_bool(stream, NULL); } @@ -223,6 +226,7 @@ void write_outputs(t_machine *m, FILE *stream) { fprintf(stream, "\t%ld\n", m->var_values[var]); } fprintf(stream, "\n"); + fflush(stream); } diff --git a/monitor/disp.c b/monitor/disp.c index d25c04d..7d24325 100644 --- a/monitor/disp.c +++ b/monitor/disp.c @@ -72,20 +72,40 @@ void disp_display(t_mon *mon) { werase(wpstatus); - wprintw(wpstatus, "Step:\t\t%d\n", mon->step); + wprintw(wpstatus, "Step:\t\t%d\t%s\t%s\n", + mon->step, + (mon->status == MS_AUTO ? "A" : "M"), + (mon->ticker_mode == TM_SECOND ? "TS" : (mon->ticker_mode == TM_FAST ? "TF" : "TZ"))); + wprintw(wpstatus, "\nInputs:\n"); for (i = 0; i < mon->n_inputs; i++) { - wprintw(wpstatus, "\t%s (%d)\t%s\n", mon->inputs[i].name, mon->inputs[i].size, mon->inputs[i].value); + wprintw(wpstatus, " %d. %s%s\t%s (%d)\t%s\n", + i, + (i == mon->ticker_input ? "T" : ""), + (i == mon->ser_in_in ? ">" : ""), + mon->inputs[i].name, mon->inputs[i].size, mon->inputs[i].value); } if (mon->n_inputs == 0) wprintw(wpstatus, "\t(none)\n"); + wprintw(wpstatus, "\nOutputs:\n"); for (i = 0; i < mon->n_outputs; i++) { - wprintw(wpstatus, "\t%s\t%s\t%ld\n", mon->outputs[i].name, mon->outputs[i].v_bin, mon->outputs[i].v_int); + wprintw(wpstatus, " %d. %s%s\t%s\t%s\t%ld\n", i, + (i == mon->ser_out ? "<" : ""), + (i == mon->ser_in_busy_out ? "!" : ""), + mon->outputs[i].name, mon->outputs[i].v_bin, mon->outputs[i].v_int); } if (mon->n_outputs == 0) wprintw(wpstatus, "\t(none)\n"); + wprintw(wpstatus, "\nSerial buffer:\n%s\n", mon->ser_buf); + wmove(wcmdline, 0, cmd_pos + 2); wrefresh(wpstatus); + + if (mon->ser_out_buf != 0) { + wprintw(wpoutput, "%c", mon->ser_out_buf); + wrefresh(wpoutput); + mon->ser_out_buf = 0; + } } diff --git a/monitor/main.c b/monitor/main.c index 27fc1e0..1de722c 100644 --- a/monitor/main.c +++ b/monitor/main.c @@ -1,5 +1,6 @@ #include <curses.h> #include <signal.h> +#include <errno.h> #include "mon.h" @@ -23,17 +24,33 @@ int main(int argc, char *argv[]) { // Launch simulator mkfifo(MON2SIM, 0600); mkfifo(SIM2MON, 0600); + int sim_pid = fork(); if (sim_pid == 0) { // child : launch simulator - freopen(MON2SIM, "r", stdin); - freopen(SIM2MON, "w", stdout); - execv(argv[1], argv + 1); + if (freopen(SIM2MON, "w", stdout) == NULL) { + fprintf(stderr, "(simulator) Could not open sim2mon for writing (%s).\n", strerror(errno)); + return 1; + } + if (freopen(MON2SIM, "r", stdin) == NULL) { + fprintf(stderr, "(simulator) Could not open mon2sim for reading (%s).\n", strerror(errno)); + return 1; + } + execv (argv[1], argv + 1); } t_mon mon; - mon.to_sim = fopen(MON2SIM, "w"); mon.from_sim = fopen(SIM2MON, "r"); + if (mon.from_sim == NULL) { + fprintf(stderr, "(monitor) Could not open sim2mon for reading (%s).\n", strerror(errno)); + return 1; + } + mon.to_sim = fopen(MON2SIM, "w"); + if (mon.to_sim == NULL) { + fprintf(stderr, "(monitor) Could not open mon2sim for reading (%s).\n", strerror(errno)); + return 1; + } + if (err = mon_read_prologue(&mon)) { fprintf(stderr, "\nError while launching simulator (%d).\n", err); goto finish; diff --git a/monitor/mon b/monitor/mon Binary files differindex 9c7d629..03e4012 100755 --- a/monitor/mon +++ b/monitor/mon diff --git a/monitor/mon.c b/monitor/mon.c index 8ff2851..b876559 100644 --- a/monitor/mon.c +++ b/monitor/mon.c @@ -1,13 +1,34 @@ #include <stdlib.h> #include <unistd.h> +#include <string.h> #include "mon.h" +/* + 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) + 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 +*/ + int mon_read_prologue(t_mon *mon) { int i; - if (fscanf(mon->from_sim, " %d %d\n", &mon->n_inputs, &mon->n_outputs) == EOF) - return -1; + 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)); @@ -16,8 +37,8 @@ int mon_read_prologue(t_mon *mon) { 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 %s\n", &mon->inputs[i].size, mon->inputs[i].name) == EOF) - return -(i+2); + 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 @@ -31,6 +52,14 @@ int mon_read_prologue(t_mon *mon) { mon->status = MS_RUN; 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; return 0; } @@ -47,7 +76,7 @@ void mon_loop(t_mon *mon) { } } -void mon_handle_command(t_mon *mon, char *c) { +void mon_handle_command(t_mon *mon, const char *c) { if (c[0] == 0) { // empty command : run step mon_step(mon); } else if (!strcmp(c, "q")) { @@ -56,29 +85,109 @@ void mon_handle_command(t_mon *mon, char *c) { mon->status = MS_AUTO; } else if (!strcmp(c, "m")) { mon->status = MS_RUN; + } 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 = 0, v = 0, w = 0; + const char *p = c + 1; + while (isspace(*p)) p++; + while (isdigit(*p)) u = u * 10 + (*(p++) - '0'); + while (isspace(*p)) p++; + while (isdigit(*p)) v = v * 10 + (*(p++) - '0'); + while (isspace(*p)) p++; + while (isdigit(*p)) w = w * 10 + (*(p++) - '0'); + if (u >= 0 && u < mon->n_inputs && + v >= 0 && v < mon->n_outputs && + w >= 0 && w < mon->n_outputs) { + mon->ser_in_in = u; + mon->ser_in_busy_out = v; + mon->ser_out = w; + } + } + } else if (c[0] == ':') { + strcat(mon->ser_buf, c + 1); + strcat(mon->ser_buf, "\n"); } + disp_display(mon); } void mon_step(t_mon *mon) { int i = 0; // Get ticker - time_t new_clk = time(NULL); - int ticker = new_clk - mon->clk; - mon->clk = new_clk; + 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) { + 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 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 for (i = 0; i < mon->n_outputs; i++) { - fscanf(mon->from_sim, "%s %s %d\n", + 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; + } // Display mon->step++; diff --git a/monitor/mon.h b/monitor/mon.h index e08f01c..39ac37e 100644 --- a/monitor/mon.h +++ b/monitor/mon.h @@ -25,6 +25,12 @@ typedef enum { MS_AUTO } t_status; +typedef enum { + TM_SECOND, + TM_FAST, + TM_NO_TICK +} t_tick_mode; + typedef struct { FILE *to_sim, *from_sim; @@ -37,6 +43,12 @@ typedef struct { t_status status; time_t clk; + int ticker_input; + t_tick_mode ticker_mode; + + int ser_in_in, ser_in_busy_out, ser_out; + char ser_buf[256]; + char ser_out_buf; } t_mon; void disp_init(); @@ -49,6 +61,6 @@ int mon_read_prologue(t_mon *mon); // nonzero on error void mon_step(t_mon *mon); void mon_loop(t_mon *mon); -void mon_handle_command(t_mon *mon, char *c); +void mon_handle_command(t_mon *mon, const char *c); #endif |