From f51e39657f5ea31b6a3b2ea010591056e5737d03 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Sun, 15 Dec 2024 14:20:09 +0100 Subject: [PATCH] Add connect command This makes wiring up mosfets together noticeably easier. The bind and addnode commands may be rendered somewhat useless by this. --- Makefile | 1 + inc/FET_sim.h | 7 ++++++- src/build_helper.c | 37 +++++++++++++++++++++++++++++++++++++ src/c_connect.c | 30 ++++++++++++++++++++++++++++++ src/main.c | 13 ++++++++++++- 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/c_connect.c diff --git a/Makefile b/Makefile index 9c9e94a..f5ae798 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,7 @@ SOURCES := main.c \ c_addfet.c \ c_setnode.c \ c_bind.c \ + c_connect.c \ c_draw.c \ c_next.c \ c_help.c \ diff --git a/inc/FET_sim.h b/inc/FET_sim.h index 6c4a310..2914198 100644 --- a/inc/FET_sim.h +++ b/inc/FET_sim.h @@ -3,7 +3,7 @@ # 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 @@ -59,6 +59,7 @@ typedef enum e_command addnode, bind, help, + connect, exitsim, } t_command; @@ -97,6 +98,9 @@ typedef struct s_input 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); @@ -128,6 +132,7 @@ t_state reduce_state(t_state state); 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); diff --git a/src/build_helper.c b/src/build_helper.c index 5a856f2..a48c3ec 100644 --- a/src/build_helper.c +++ b/src/build_helper.c @@ -1,6 +1,43 @@ #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; diff --git a/src/c_connect.c b/src/c_connect.c new file mode 100644 index 0000000..4ed338b --- /dev/null +++ b/src/c_connect.c @@ -0,0 +1,30 @@ +#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); +} diff --git a/src/main.c b/src/main.c index 8cd78d8..68978cc 100644 --- a/src/main.c +++ b/src/main.c @@ -55,6 +55,8 @@ int parse_command(const char *str, t_command *cmd) *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 @@ -124,6 +126,8 @@ int has_correct_argc(t_input input) return (0); if (c == addfet && argc == 0) return (0); + if (c == connect && argc != 4) + return (0); return (1); } @@ -148,7 +152,7 @@ int parse_arg(t_input *input, const char *str, size_t i) 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; @@ -176,6 +180,11 @@ int parse_arg(t_input *input, const char *str, size_t i) 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); } @@ -223,6 +232,8 @@ int process_input(t_vec *nodes, t_vec *mosfets) 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) -- 2.30.2