summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--gate.c46
-rw-r--r--gate.h20
-rw-r--r--simulator.c4
-rw-r--r--wire.c106
-rw-r--r--wire.h27
6 files changed, 70 insertions, 137 deletions
diff --git a/Makefile b/Makefile
index 651c53c..68ca80d 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,6 @@ CFLAGS += "-std=c99"
all: simulator
-simulator: simulator.o wire.o logic.o
+simulator: simulator.o gate.o logic.o
-parser: parser.o wire.o logic.o
+parser: parser.o gate.o logic.o
diff --git a/gate.c b/gate.c
new file mode 100644
index 0000000..0d4337a
--- /dev/null
+++ b/gate.c
@@ -0,0 +1,46 @@
+#include <string.h>
+
+#include "gate.h"
+#include "error.h"
+
+#define GATE_MAX 1024
+
+static size_t gate_count;
+static struct gate gates[GATE_MAX];
+
+int
+gate_count_guard(void) {
+ if (gate_count >= GATE_MAX) {
+ emit_error("Internal: too many gates\n");
+ }
+ return 1;
+}
+
+int
+gate_add(struct gate *in1, struct gate *in2) {
+ struct gate g = {0};
+
+ if (gate_count_guard()) {
+ return 1;
+ }
+
+ g.in1 = in1;
+ g.in2 = in2;
+
+ gates[gate_count++] = g;
+
+ return 0;
+}
+
+int
+tick(void) {
+ /* FIXME */
+ return 1;
+}
+
+void
+gate_init(void) {
+ gate_count = 0;
+ memset(gates, 0, sizeof(gates));
+}
+
diff --git a/gate.h b/gate.h
new file mode 100644
index 0000000..fcc84bb
--- /dev/null
+++ b/gate.h
@@ -0,0 +1,20 @@
+#ifndef GATE_H
+#define GATE_H
+
+#include <stddef.h>
+
+#include "logic.h"
+
+struct gate {
+ enum BINARY (*operation)(enum BINARY, enum BINARY);
+ enum BINARY output;
+ struct gate *in1;
+ struct gate *in2;
+};
+
+void gate_update_output(struct gate *);
+int gate_add(struct gate *in1, struct gate *in2);
+int tick(void);
+void gate_init(void);
+
+#endif
diff --git a/simulator.c b/simulator.c
index 389adc3..b8da648 100644
--- a/simulator.c
+++ b/simulator.c
@@ -1,8 +1,8 @@
-#include "wire.h"
+#include "gate.h"
#include "logic.h"
int main(int argc, char **argv) {
logic_test();
- wire_init();
+ gate_init();
return 0;
}
diff --git a/wire.c b/wire.c
deleted file mode 100644
index e181d83..0000000
--- a/wire.c
+++ /dev/null
@@ -1,106 +0,0 @@
-#include <string.h>
-
-#include "wire.h"
-#include "error.h"
-
-#define WIRE_MAX 1024
-#define NODE_MAX 1024
-
-static size_t wire_count;
-static size_t node_count;
-
-static struct wire wires[WIRE_MAX];
-static struct node nodes[NODE_MAX];
-
-int
-wire_count_guard(void) {
- if (wire_count >= WIRE_MAX) {
- emit_error("Internal: too many wires\n");
- }
- return 1;
-}
-
-/* check if the nodes in a wire are allowed to be joined together */
-int
-wire_nodes_valid(struct wire *w) {
- /* FIXME needs to be more explicit if more than NODE_OUTPUT and NODE_INPUT
- * out are added to node types */
- if (w->b->type == w->b->type) {
- emit_error("Cannot wire %sput to node of same type\n",
- w->a->type == NODE_OUTPUT ? "out" : "in" );
- return 1;
- }
- return 0;
-}
-
-int
-wire_add(size_t aoffs, size_t boffs) {
- struct wire w = {0};
-
- if (wire_count_guard()) {
- return 1;
- }
-
- if (aoffs >= NODE_MAX || boffs >= NODE_MAX) {
- emit_error("Internal: cannot wire between node(s) outside allowable range\n");
- return 1;
- }
-
- /* FIXME check that nodes are initialised? */
-
- w.a = &nodes[aoffs];
- w.b = &nodes[boffs];
-
- if (wire_nodes_valid(&w)) {
- return 1;
- }
-
- wires[wire_count++] = w;
-
- return 0;
-}
-
-int
-node_update(void) {
- size_t i = 0;
- struct node *na;
- struct node *nb;
-
- if (wire_count_guard()) {
- return 1;
- }
-
- for (i = 0; i < wire_count; i++) {
- na = wires[i].a;
- nb = wires[i].b;
- if (na->type == NODE_OUTPUT && nb->type == NODE_INPUT) {
- nb->value = na->value;
- }
-
- if (na->type == NODE_INPUT && nb->type == NODE_OUTPUT) {
- na->value = nb->value;
- }
- }
- return 0;
-}
-
-
-int
-tick(void) {
- nodes[0].value = logic_not(nodes[0].value);
- node_update();
- return 0;
-}
-
-void
-wire_init(void) {
- wire_count = 0;
- memset(wires, 0, sizeof(wires));
- memset(nodes, 0, sizeof(nodes));
-
- /* zeroth node is the clock source */
- node_count = 1;
- nodes[0].type = NODE_OUTPUT;
- nodes[0].value = 0;
-}
-
diff --git a/wire.h b/wire.h
deleted file mode 100644
index 74ff29b..0000000
--- a/wire.h
+++ /dev/null
@@ -1,27 +0,0 @@
-#ifndef WIRE_H
-#define WIRE_H
-
-#include <stddef.h>
-
-#include "logic.h"
-
-enum NODE_TYPE {
- NODE_OUTPUT,
- NODE_INPUT
-};
-
-struct node {
- enum NODE_TYPE type;
- enum BINARY value;
-};
-
-struct wire {
- struct node *a;
- struct node *b;
-};
-
-int wire_add(size_t aoffs, size_t boffs);
-int tick(void);
-void wire_init(void);
-
-#endif