From b12b8c9cb7a4d36fa6c0e2e9ec14ce5dfba6f78f Mon Sep 17 00:00:00 2001 From: David Phillips Date: Thu, 28 Dec 2017 16:30:11 +1300 Subject: Support first working prototype without nested gates --- gate.c | 34 +++++++++++++++++++++++++++++++++- gate.h | 1 + parser.c | 9 +++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/gate.c b/gate.c index fb5ff41..67d9c08 100644 --- a/gate.c +++ b/gate.c @@ -125,6 +125,38 @@ gate_dump(void) { emit_info("Gates:\n"); for (i = 0; i < gate_count; i++) { - emit_info("gate '%s'\n", gates[i].name); + emit_info("gate '%s': %s\n", gates[i].name, gates[i].output == LOGIC_HIGH ? "HIGH" : "LOW"); } } + +int +gate_set_input(char *name, enum BINARY value) { + struct gate *g = NULL; + + if (NULL == (g = gate_get_input_by_name(name))) { + emit_error("No such input %s", name); + return 1; + } + + g->output = value; + + return 0; +} + +void gate_update() { + size_t i = 0; + struct gate *g = NULL; + enum BINARY in1 = LOGIC_LOW; + enum BINARY in2 = LOGIC_LOW; + + /* FIXME should be input->output flow path */ + /* FIXME data flow doesn't matter yet because gate chaining isn't allowed */ + + for (i = 0; i < gate_count; i++) { + g = &gates[i]; + in1 = g->in1->output; + in2 = g->in2->output; + g->output = (g->operation)(in1, in2); + } +} + diff --git a/gate.h b/gate.h index 1838913..86b2e48 100644 --- a/gate.h +++ b/gate.h @@ -25,5 +25,6 @@ int gate_add(char *name, enum BINARY (*operation)(enum BINARY, enum BINARY), str int tick(void); void gate_init(void); void gate_dump(void); +int gate_set_input(char *name, enum BINARY value); #endif diff --git a/parser.c b/parser.c index 19610b9..a34744a 100644 --- a/parser.c +++ b/parser.c @@ -187,6 +187,15 @@ int main(void) { } gate_dump(); + gate_set_input("a", LOGIC_LOW); + gate_set_input("b", LOGIC_LOW); + + gate_update(); + + gate_dump(); + + + return 0; } -- cgit v1.1