-
Notifications
You must be signed in to change notification settings - Fork 3
/
gate-machine.c
291 lines (261 loc) · 9.67 KB
/
gate-machine.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
/**
* I Love Jesus <3
* The Network is the Machine.
*
* This code performs the processing of the logic gates and consequently the
* transistors. Making the values generated in its operations resemble voltages
* and electrical currents applied to the circuit.
*/
#include "gate-machine.h"
#include "gate-construction.h"
#include "transistor-creation.h"
static void process_transistor (transistor_t *t, int type);
static void process_gate_connection (gate_t *gate, wire_t *w);
static void process_connections (gate_t *gate, wire_t *w, transistor_t *t);
static void process_transistor_source (gate_t *gate, wire_t *w, transistor_t *t);
static void process_transistor_drain (gate_t *gate, wire_t *w, transistor_t *t);
static void process_gate_direct (gate_t *gate);
static void process_gate_elements (gate_t *gate);
static void prepare_subgate_vdd (gate_t *gate);
static void prepare_subgate_input (gate_t *gate);
static void prepare_subgate_ground (gate_t *gate);
static void prepare_gate_connections (gate_t *gate);
void process_gate (gate_t *gate)
{
if (gate->subgate)
process_gate_elements(gate);
else
process_gate_direct(gate);
}
/**
* MOSFET transistors P-TYPE and N-TYPE.
*/
static void process_transistor (transistor_t *t, int type)
{
switch (type) {
case TYPE_P:
if (t->source == 1 && t->gate == 1)
t->drain = 0;
else if (t->source == 1 && t->gate == 0)
t->drain = 1;
else if (t->source == 0 && t->gate == 0)
t->drain = 0;
else if (t->source == 0 && t->gate == 1)
t->drain = 0;
break;
case TYPE_N:
if (t->gate == 1 && t->drain == 1)
t->source = 1;
else if (t->gate == 1 && t->drain == 0)
t->source = 0;
else if (t->gate == 0 && t->drain == 1)
t->source = 0;
else if (t->gate == 0 && t->drain == 0)
t->source = 0;
break;
}
}
static void process_gate_connection (gate_t *gate, wire_t *w)
{
for (transistor_t *t=gate->transistors; t != NULL; t=t->next) {
if (w->output_id == t->id) {
switch (w->output_pin) {
case PIN_TRANSISTOR_GATE:
t->gate = 1;
break;
}
}
}
}
static void process_gate_elements (gate_t *gate)
{
prepare_subgate_vdd(gate);
prepare_subgate_input(gate);
prepare_gate_connections(gate);
prepare_subgate_ground(gate);
}
static void prepare_gate_connections (gate_t *gate)
{
for (wire_t *w=gate->wires; w != NULL; w=w->next) {
switch (w->input_id) {
case GATE_SUB1_OUTPUT: process_gate_direct(gate->sub1); break;
case GATE_SUB2_OUTPUT: process_gate_direct(gate->sub2); break;
case GATE_SUB3_OUTPUT: process_gate_direct(gate->sub3); break;
case GATE_SUB4_OUTPUT: process_gate_direct(gate->sub4); break;
}
switch (w->output_id) {
case GATE_SUB1_INPUT1: attr_transistor_input(gate->sub1->input1); break;
case GATE_SUB1_INPUT2: attr_transistor_input(gate->sub1->input2); break;
case GATE_SUB2_INPUT1: attr_transistor_input(gate->sub2->input1); break;
case GATE_SUB2_INPUT2: attr_transistor_input(gate->sub2->input2); break;
case GATE_SUB3_INPUT1: attr_transistor_input(gate->sub3->input1); break;
case GATE_SUB3_INPUT2: attr_transistor_input(gate->sub3->input2); break;
case GATE_SUB4_INPUT1: attr_transistor_input(gate->sub4->input1); break;
case GATE_SUB4_INPUT2: attr_transistor_input(gate->sub4->input2); break;
case GATE_PIN_OUTPUT: attr_transistor_input(gate->output); break;
}
}
}
static void prepare_subgate_ground (gate_t *gate)
{
for (wire_t *w=gate->wires; w != NULL; w=w->next) {
switch (w->input_id) {
case GATE_PIN_GROUND:
switch (w->output_id) {
case GATE_SUB1_GROUND:
if (gate->sub1->ground)
gate->ground = gate->sub1->ground;
break;
case GATE_SUB2_GROUND:
if (gate->sub2->ground)
gate->ground = gate->sub2->ground;
break;
case GATE_SUB3_GROUND:
if (gate->sub3->ground)
gate->ground = gate->sub3->ground;
break;
case GATE_SUB4_GROUND:
if (gate->sub4->ground)
gate->ground = gate->sub4->ground;
break;
}
break;
}
}
}
static void prepare_subgate_input (gate_t *gate)
{
for (wire_t *w=gate->wires; w != NULL; w=w->next) {
if (w->input_id == GATE_PIN_INPUT1 || w->input_id == GATE_PIN_INPUT2) {
switch (w->output_id) {
case GATE_SUB1_INPUT1: set_sub(gate->sub1->input1); break;
case GATE_SUB1_INPUT2: set_sub(gate->sub1->input2); break;
case GATE_SUB2_INPUT1: set_sub(gate->sub2->input1); break;
case GATE_SUB2_INPUT2: set_sub(gate->sub2->input2); break;
case GATE_SUB3_INPUT1: set_sub(gate->sub3->input1); break;
case GATE_SUB3_INPUT2: set_sub(gate->sub3->input2); break;
case GATE_SUB4_INPUT1: set_sub(gate->sub4->input1); break;
case GATE_SUB4_INPUT2: set_sub(gate->sub4->input2); break;
}
}
}
}
static void prepare_subgate_vdd (gate_t *gate)
{
// The Vdd always connects with the Vdd of the sub-gates.
for (wire_t *w=gate->wires; w != NULL; w=w->next) {
if (w->input_id == GATE_PIN_VDD) {
switch (w->output_id) {
case GATE_SUB1_VDD:
gate->sub1->vdd = gate->vdd;
break;
case GATE_SUB2_VDD:
gate->sub2->vdd = gate->vdd;
break;
case GATE_SUB3_VDD:
gate->sub3->vdd = gate->vdd;
break;
case GATE_SUB4_VDD:
gate->sub4->vdd = gate->vdd;
break;
}
}
}
}
static void process_gate_direct (gate_t *gate)
{
for (wire_t *w=gate->wires; w != NULL; w=w->next) {
// Vdd will only connect to some source or drain port.
if (w->input_id == GATE_PIN_VDD) {
if (gate->vdd == 1) {
for (transistor_t *t=gate->transistors; t != NULL; t=t->next) {
if (w->output_id == t->id) {
switch (w->output_pin) {
case PIN_TRANSISTOR_SOURCE:
t->source = 1;
break;
case PIN_TRANSISTOR_DRAIN:
t->drain = 1;
break;
}
}
}
}
}
// The input will always connect to some gate.
else if (w->input_id == GATE_PIN_INPUT1) {
if (gate->input1 == 1)
process_gate_connection(gate, w);
} else if (w->input_id == GATE_PIN_INPUT2) {
if (gate->input2 == 1)
process_gate_connection(gate, w);
}
// Connections that come out of transistors, can only come out of drain or source.
// And connect to the output, ground, or a gate of some other transistor.
else {
for (transistor_t *t=gate->transistors; t != NULL; t=t->next) {
if (w->input_id == t->id)
process_connections(gate, w, t);
}
}
}
}
static void process_connections (gate_t *gate, wire_t *w, transistor_t *t)
{
switch (w->input_pin) {
case PIN_TRANSISTOR_SOURCE:
process_transistor_source(gate, w, t);
break;
case PIN_TRANSISTOR_DRAIN:
process_transistor_drain(gate, w, t);
break;
}
}
static void process_transistor_source (gate_t *gate, wire_t *w, transistor_t *t)
{
switch (w->output_id) {
case GATE_PIN_GROUND:
switch (t->type) {
case TYPE_N:
process_transistor(t, TYPE_N);
gate->ground = t->source;
break;
}
break;
default:
switch (t->type) {
case TYPE_N:
process_transistor(t, TYPE_N);
for (transistor_t *t2=gate->transistors; t2 != NULL; t2=t2->next) {
if (w->output_id == t2->id) {
switch (t2->type) {
case TYPE_N:
t2->drain = t->source;
break;
}
}
}
break;
}
break;
}
}
static void process_transistor_drain (gate_t *gate, wire_t *w, transistor_t *t)
{
switch (w->output_id) {
case GATE_PIN_OUTPUT:
switch (t->type) {
case TYPE_P:
process_transistor(t, TYPE_P);
if (t->drain)
gate->output = t->drain;
break;
case TYPE_N:
t->drain = gate->output;
break;
}
break;
case GATE_PIN_GROUND: break;
default: break;
}
}