-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgates.c
412 lines (333 loc) · 8.94 KB
/
gates.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* gates.c: Implementation of gates.h header.
* @author: rmNULL
* Version: 0.10
* LICENSE: MIT, refer LICENSE for more.
* Description: see README.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ds/slist.h"
#include "gates.h"
/* default gate input method */
#define NUL '\0'
#define TAG_LEN 31
struct gate_input_t {
union {
bool pin;
struct {
struct gate *gate;
int pin;
} from;
} ip;
char type;
};
struct gate {
char *name;
int class;
int current_pin;
size_t set_pins;
size_t total_pins;
struct gate_input_t *pins;
/* keeps track of unpinned slots. */
SListEntry *unset_slots;
bool op;
};
/*
* Helper Macro to allocate memory to different parts of the gate.
* let user deal with failed memory allocations.
*/
#define ALLOC_WITH_FAILURE_HANDLE(ptr, size) do {\
(ptr) = malloc(size); \
if ((ptr) == NULL) { \
/* inorder to ensure that no memory leaks happen when
* allocation fails, for e.g, allocation might fail for
* "gate->name", in such a case the memory previously allocate
* for "gate" and "gate->pins" need to be freed. */ \
cleanup(gate);\
return NULL; \
}\
} while(0)
/* checks if all the input pins of the given gate are set. */
#define ARE_PINS_SET(gate) ((gate->set_pins) == (gate->total_pins))
/* destructor */
void cleanup(struct gate *gate)
{
if (gate == NULL)
return;
while (gate->unset_slots) {
free(slist_data(gate->unset_slots));
slist_remove_entry(&gate->unset_slots, gate->unset_slots);
}
if (gate->name) free(gate->name);
if (gate->pins) free(gate->pins);
if (gate) free(gate);
}
/* helper function for creating gates. */
static struct gate *create_gate(const char *tag, size_t total_ip_pins)
{
struct gate *gate;
/* +1 for the NUL byte. */
size_t len = strnlen(tag, TAG_LEN) + 1;
ALLOC_WITH_FAILURE_HANDLE(gate, sizeof(struct gate));
ALLOC_WITH_FAILURE_HANDLE(gate->name, sizeof(char) * len);
strncpy(gate->name, tag, len);
/* let all the gates have no type(NUL) by default. */
ALLOC_WITH_FAILURE_HANDLE(gate->pins, sizeof(struct gate_input_t) * total_ip_pins);
for (int i = 0; i < total_ip_pins; ++i)
gate->pins[i].type = NUL;
gate->total_pins = total_ip_pins;
gate->op = false;
gate->current_pin = 0;
gate->set_pins = 0;
gate->unset_slots = NULL;
return gate;
}
/* mdfn: macro defined function(s).
* macro to (auto) define functions for creation of gates.
*
* defines a function with gatename as the return type and taking total_ip_pins
* as the argument; Minimum of 2 input pins need to be present. Returns NULL on
* allocation failure.
*/
#define mdfn(gatename, cls) \
gatename create_##gatename##_gate(const char *tag, size_t total_ip_pins) \
{\
if (total_ip_pins < 2 || total_ip_pins > 64) {\
fprintf(stderr, "Error: Unable to create ['%s' %s] gate.", tag, gate_names[cls]);\
fprintf(stderr, " %lu not in range [2..64].\n", total_ip_pins);\
return NULL;\
}\
struct gate *gate = create_gate(tag, total_ip_pins);\
if (gate == NULL)\
return NULL;\
\
gate->class = cls;\
return gate; \
}
/*
* Semi-colons are not suffixed to maintain compatibilty with ISO C(sorry).
*
* e.g: defines a function for creating And gate of the form, (And Gate Example)
* ƒ: And create_And_gate(char *gate_name, size_t total_ip_pins);
*/
mdfn(And, AND)
mdfn(Or, OR)
mdfn(Xor, XOR)
mdfn(Xnor, XNOR)
mdfn(Nor, NOR)
mdfn(Nand, NAND)
#undef mdfn
/*
* This function is not auto-generated by mdfn(macro) because
* Not gate stands out from the crowd as it doesn't have variable number of
* input pins. do a dirty hack and patch it in there, maybe?
*/
Not create_Not_gate(const char *tag)
{
/* Not gates have single input slot.*/
Not gate = create_gate(tag, 1);
if (gate == NULL)
return NULL;
gate->class = NOT;
return gate;
}
/*
* find_pin_slot:
* Description:
* Helper function that finds an empty pin slot for usage.
* Input:
* struct gate * variable;
* Return Value:
* Pin number on successful run.
* '-1' when an invalid gate is passed.
* '-2' when no empty slots.
*/
static int find_pin_slot(struct gate *const gate)
{
int current_pin;
if (gate == NULL)
return -1;
if (ARE_PINS_SET(gate)) {
return -2;
}
if (gate->unset_slots != NULL) {
current_pin = *((int *) slist_data(gate->unset_slots));
/* free malloced pin number in unset pins */
free(slist_data(gate->unset_slots));
slist_remove_entry(&gate->unset_slots, gate->unset_slots);
} else {
do {
current_pin = gate->current_pin++;
} while (gate->pins[current_pin].type != NUL
&& current_pin < gate->total_pins);
}
return current_pin;
}
int set_pin(struct gate *const gate, bool truth)
{
int current_pin;
if ((current_pin = find_pin_slot(gate)) == -1
|| current_pin == -2)
return current_pin;
gate->pins[current_pin].ip.pin = truth;
gate->pins[current_pin].type = INTERNAL;
gate->set_pins++;
return current_pin;
}
int unset_pin(struct gate *const gate, int pin_number)
{
if (gate == NULL || gate->pins[pin_number].type == NUL
|| pin_number < 0 || pin_number >= gate->total_pins)
return 0;
int *x = malloc(sizeof(int));
*x = pin_number;
gate->pins[pin_number].type = NUL;
slist_prepend(&gate->unset_slots, x);
gate->set_pins--;
return 1;
}
void short_pins(struct gate *const dst, int dst_pin,
struct gate *const src, int src_pin,
char type)
{
/* ERROR handling, hehe, more like error avoiding. */
if (src == NULL || dst == NULL) {
fputs("Pass a valid pointer.\n", stderr);
return ;
}
if ((type) != SHORT_OUT && (type) != SHORT_IN) {
fputs("Unknown type specified.\n", stderr);
return;
}
if (src == dst) {
fputs("WARNING: you are shorting the port to itself,"
" let me snatch that freedom from you.\n", stderr);
return;
}
if (src_pin >= src->total_pins) {
fprintf(stderr, "Error: 'src' pin number out of range."
"Pin number should be in the range [0...%lu]"
"(exclusive).\n", src->total_pins);
return;
}
if (dst_pin >= dst->total_pins) {
fprintf(stderr, "Error: 'dst' pin number out of range."
"Pin number should be in the range [0...%lu]"
"(exclusive).\n", dst->total_pins);
return;
}
/* woof, we're safe. */
dst->pins[dst_pin].ip.from.gate = src;
dst->pins[dst_pin].ip.from.pin = src_pin;
dst->pins[dst_pin].type = type;
dst->set_pins++;
/* return 0; */
}
/* logic function generator. */
#define define_op_fn(logic_name, sym) \
static int logic_name##_op(const struct gate *gate) \
{\
if (gate == NULL)\
return -1;\
\
bool truth = get_pin_value(gate, 0);\
\
for (int i = 1; i < gate->total_pins; ++i)\
truth sym##= get_pin_value(gate, i);\
\
return truth;\
}\
define_op_fn(and, &)
define_op_fn(or, |)
define_op_fn(xor, ^)
#undef define_op_fn
/* inverted function generator. */
#define define_op_fn(logic_name, base_op) \
static int logic_name##_op(const struct gate *gate) \
{\
int truth = base_op;\
return truth == -1 ? truth : !truth;\
}\
define_op_fn(not, get_pin_value(gate, 0))
define_op_fn(nor, or_op(gate))
define_op_fn(nand, and_op(gate))
define_op_fn(xnor, xor_op(gate))
#undef define_op_fn
/* end of generated functions */
/*
* only calculates the gate output when all the inputs are set. Default case is
* return faulty value.
*/
static bool calculate_output(struct gate *gate)
{
if (gate == NULL || !ARE_PINS_SET(gate))
return false;
bool truth =
gate->class == OR ? or_op(gate) :
gate->class == AND ? and_op(gate) :
gate->class == NOT ? not_op(gate) :
gate->class == NOR ? nor_op(gate) :
gate->class == XOR ? xor_op(gate) :
gate->class == XNOR ? xnor_op(gate) :
gate->class == NAND ? nand_op(gate) :
false;
return truth;
}
/*
* Calls calculate_output() to perform the logic and sets the
* result of the call to the output pin(gate->op) of the given gate.
*/
bool get_output(struct gate *gate)
{
bool output = calculate_output(gate);
gate->op = output;
return output;
}
int get_pin_value(const struct gate *gate, unsigned int pin_number)
{
if (gate == NULL)
return -1;
if (gate->pins[pin_number].type == NUL)
return -2;
if (gate->pins[pin_number].type == INTERNAL)
return gate->pins[pin_number].ip.pin;
else if (gate->pins[pin_number].type == SHORT_OUT) {
return get_output(gate->pins[pin_number].ip.from.gate);
} else {
int next_pin = gate->pins[pin_number].ip.from.pin;
return get_pin_value(gate->pins[pin_number].ip.from.gate, next_pin);
}
}
/*
* ginfo (gate info) functions to peek into gate's members.
*/
short ginfo_capacity(const struct gate *gate)
{
return gate ? gate->total_pins : -1;
}
short ginfo_class(const struct gate *gate)
{
return gate ? gate->class : -1;
}
char *ginfo_tag(const struct gate *gate)
{
if (gate == NULL)
return NULL;
size_t len = strnlen(gate->name, TAG_LEN) + 1; /* for the NUL byte */
char *tag = calloc(len, sizeof(char));
if (!tag)
return NULL;
strncpy(tag, gate->name, len);
return tag;
}
bool ginfo_is_pin_set(const struct gate *gate, unsigned int pin)
{
if (gate == NULL || (pin < 0 && pin >= 64))
return false;
if (gate->class == NOT)
pin = 0;
return gate->pins[pin].type != NUL;
}