summaryrefslogtreecommitdiff
path: root/gate.c
blob: c196b914528a757c6c340674f097c9b21da81346 (plain)
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
#include <string.h>
#include <stdlib.h>

#include "gate.h"
#include "error.h"

#define GATE_MAX   1024
#define INPUT_MAX  1024
#define OUTPUT_MAX 1024

static size_t gate_count;
static size_t input_count;
static struct gate gates[GATE_MAX];
static struct gate inputs[INPUT_MAX];

int
count_guard(int c, int max, char *desc) {
	if (c >= max) {
		emit_error("Internal: too many %s\n", desc);
		return 1;
	}
	return 0;
}

int
gate_add_generic(struct gate *array, size_t array_index, char *name, enum BINARY (*operation)(enum BINARY, enum BINARY), struct gate *in1, struct gate *in2) {
	struct gate g;

	if (name == NULL) {
		g.name = NULL;
	} else {
		g.name = strdup(name);
		if (g.name == NULL) {
			emit_error("strdup failed");
			return 1;
		}
	}
	g.operation = operation;
	g.in1 = in1;
	g.in2 = in2;

	array[array_index] = g;

	return 0;
}

struct gate*
gate_get_input_by_name(char *name) {
	struct gate *res = NULL;
	size_t i = 0;

	for (i = 0; i < input_count; i++) {
		if (inputs[i].name == NULL) {
			emit_error("input at index %zd has NULL name, ignoring", i);
			continue;
		}
		if (strcmp(inputs[i].name, name) == 0) {
			res = &inputs[i];
			break;
		}
	}

	return res;
}

int
gate_input_add(char *name) {
	int res = 0;
	if (count_guard(input_count, INPUT_MAX, "inputs")) {
		return 1;
	}

	if (gate_get_input_by_name(name) != NULL) {
		emit_error("Already an input called \"%s\"!\n", name);
		return 1;
	}

	res = gate_add_generic(inputs, input_count, name, logic_and, NULL, NULL);
	input_count++;
	return res;
}

int
gate_add(char *name, enum BINARY (*operation)(enum BINARY, enum BINARY), struct gate *in1, struct gate *in2) {
	if (count_guard(gate_count, GATE_MAX, "gates")) {
		return 1;
	}

	gate_add_generic(gates, gate_count, name, operation, in1, in2);
	gate_count++;

	return 0;
}

void
gate_free_all() {
	size_t i = 0;
	for (i = 0; i < gate_count; i++) {
		if (gates[i].name != NULL) {
			free(gates[i].name);
		}
	}
}

int
tick(void) {
	/* FIXME */
	return 1;
}

void
gate_init(void) {
	gate_count = 0;
	input_count = 0;
	memset(gates, 0, sizeof(gates));
	memset(inputs, 0, sizeof(inputs));
}

void
gate_dump(void) {
	size_t i = 0;

	emit_info("Gates:\n");
	for (i = 0; i < gate_count; i++) {
		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(void) {
	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);
	}
}