-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wire.c
73 lines (66 loc) · 2.18 KB
/
Wire.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
#include "Wire.h"
#include "debugmalloc.h"
Wire wire_create(Node *origin, int originPin) {
Wire wire;
wire.origin = origin;
wire.originPin = originPin;
wire.conCap = 1;
wire.conCount = 0;
wire.connections = (Connection *)malloc(sizeof(Connection));
return wire;
}
void wire_add(Wire *wire, int other, int otherPin, Node *nodes) {
if (wire->conCap <= wire->conCount) {
wire->conCap *= 2;
Connection *newCon = (Connection *)realloc(wire->connections, sizeof(Connection) * wire->conCap);
if (!newCon) {
log_error("Couldn't allocate new memory!\n");
return;
}
wire->connections = newCon;
}
wire->connections[wire->conCount].dest = other;
wire->connections[wire->conCount].pin = otherPin;
wire->connections[wire->conCount].wireComp = component_create_wire_between(
&wire->origin->component,
&nodes[other].component,
wire->originPin + wire->origin->component.funData.inC,
otherPin,
COMPSIZE,
COMPTHICKNESS,
wire->origin->renderer
);
wire->conCount++;
}
void wire_erase(Wire *wire, int id) {
for (size_t i = id + 1; i < wire->conCount; i++)
wire->connections[i - 1] = wire->connections[i];
wire->conCount--;
}
void wire_send_value(Wire *wire, bool value, Node *nodes) {
for (size_t i = 0; i < wire->conCount; i++)
node_set_inval(&nodes[wire->connections[i].dest], wire->connections[i].pin, value);
}
void wire_free(Wire *wire) {
free(wire->connections);
}
void connection_reposition(Connection *conn, Node *origin, int originPin, Node *dest) {
component_free_data(&conn->wireComp);
conn->wireComp = component_create_wire_between(
&origin->component,
&dest->component,
originPin + origin->component.funData.inC,
conn->pin,
COMPSIZE,
COMPTHICKNESS,
origin->renderer
);
}
void wire_reposition(Wire *wire, Node *nodes) {
for (size_t i = 0; i < wire->conCount; i++) {
connection_reposition(&wire->connections[i],
wire->origin,
wire->originPin,
&nodes[wire->connections[i].dest]);
}
}