This makes wiring up mosfets together noticeably easier.
The bind and addnode commands may be rendered somewhat useless by this.
c_addfet.c \
c_setnode.c \
c_bind.c \
+ c_connect.c \
c_draw.c \
c_next.c \
c_help.c \
# include "libft.h"
-# define MAX_ARGS 3
+# define MAX_ARGS 4
// zkrat means short circuit in my language. I opted for this because
// C does not permit short (it is type) and short_circuit was too long
addnode,
bind,
help,
+ connect,
exitsim,
} t_command;
void add_node(t_vec *nodes, t_state set_state);
void add_mosfet(t_vec *mosfets, t_type type);
void bind_fet_node(t_mosfet *mosfet, t_node *node, t_terminal terminal);
+t_node *get_node_of_terminal(t_mosfet *mosfet, t_terminal terminal);
+int transfer_mosfet(t_mosfet *mosfet, t_node *from, t_node *to);
+int merge_nodes(t_node *node1, t_node *node2);
void free_node(void *node);
int process_input(t_vec *nodes, t_vec *mosfets);
int c_addfet(t_input input, t_vec *mosfets);
int c_addnode(t_input input, t_vec *nodes);
int c_bind(t_input input, t_vec *nodes, t_vec *mosfets);
+int c_connect(t_input input, t_vec *nodes, t_vec *mosfets);
int c_draw(t_input input, t_vec *mosfets);
int c_help(t_input input);
int c_next(t_input input, t_vec *nodes, t_vec *mosfets);
#include "FET_sim.h"
#include "libft.h"
+int transfer_mosfet(t_mosfet *mosfet, t_node *from, t_node *to)
+{
+ if (mosfet->source == from)
+ bind_fet_node(mosfet, to, source);
+ if (mosfet->gate == from)
+ bind_fet_node(mosfet, to, gate);
+ if (mosfet->drain == from)
+ bind_fet_node(mosfet, to, drain);
+ return (0);
+}
+
+int merge_nodes(t_node *node1, t_node *node2)
+{
+ size_t i;
+ t_mosfet *mosfet;
+
+ i = 0;
+ while (i < node2->connected.size)
+ {
+ mosfet = ft_vec_access(&node2->connected, i);
+ transfer_mosfet(mosfet, node2, node1);
+ ++i;
+ }
+ return (0);
+}
+
+t_node *get_node_of_terminal(t_mosfet *mosfet, t_terminal terminal)
+{
+ if (terminal == source)
+ return (mosfet->source);
+ if (terminal == gate)
+ return (mosfet->gate);
+ if (terminal == drain)
+ return (mosfet->drain);
+ return (NULL);
+}
+
void bind_fet_node(t_mosfet *mosfet, t_node *node, t_terminal terminal)
{
t_node *old;
--- /dev/null
+#include "FET_sim.h"
+#include "libft.h"
+
+int c_connect(t_input input, t_vec *nodes, t_vec *mosfets)
+{
+ t_mosfet *mosfet1;
+ t_node *term_node1;
+ t_mosfet *mosfet2;
+ t_node *term_node2;
+ t_node *new_node;
+
+ mosfet1 = ft_vec_access(mosfets, input.argv[0].val.num);
+ term_node1 = get_node_of_terminal(mosfet1, input.argv[1].val.terminal);
+ mosfet2 = ft_vec_access(mosfets, input.argv[2].val.num);
+ term_node2 = get_node_of_terminal(mosfet2, input.argv[3].val.terminal);
+ if (term_node1 && term_node2)
+ return (!merge_nodes(term_node1, term_node2));
+ else if (term_node1)
+ bind_fet_node(mosfet2, term_node1, input.argv[3].val.terminal);
+ else if (term_node2)
+ bind_fet_node(mosfet1, term_node2, input.argv[1].val.terminal);
+ else
+ {
+ add_node(nodes, unknown);
+ new_node = ft_vec_access(nodes, nodes->size - 1);
+ bind_fet_node(mosfet1, new_node, input.argv[1].val.terminal);
+ bind_fet_node(mosfet2, new_node, input.argv[3].val.terminal);
+ }
+ return (1);
+}
*cmd = bind;
else if (!ft_strcmp(str, "h") || !ft_strcmp(str, "help"))
*cmd = help;
+ else if (!ft_strcmp(str, "c") || !ft_strcmp(str, "con") || !ft_strcmp(str, "connect"))
+ *cmd = connect;
else if (!ft_strcmp(str, "e") || !ft_strcmp(str, "exit"))
*cmd = exitsim;
else
return (0);
if (c == addfet && argc == 0)
return (0);
+ if (c == connect && argc != 4)
+ return (0);
return (1);
}
res = 0;
if (is_num && (c == next || c == draw || (c == setnode && i == 0)
|| (c == addfet && i == 1) || (c == addnode && (input->argc == 1 || i == 1))
- || (c == bind && i < 2)))
+ || (c == bind && i < 2) || (c == connect && (i == 0 || i == 2))))
{
input->argv[i].val.num = num_i;
input->argv[i].type = num;
res = parse_terminal(str, &input->argv[i].val.terminal);
input->argv[i].type = terminal;
}
+ else if (c == connect)
+ {
+ res = parse_terminal(str, &input->argv[i].val.terminal);
+ input->argv[i].type = terminal;
+ }
}
return (res);
}
res = c_addfet(input, mosfets);
else if (input.command == bind)
res = c_bind(input, nodes, mosfets);
+ else if (input.command == connect)
+ res = c_connect(input, nodes, mosfets);
else if (input.command == help)
res = c_help(input);
else if (input.command == exitsim)