Add connect command
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 15 Dec 2024 13:20:09 +0000 (14:20 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 15 Dec 2024 13:20:09 +0000 (14:20 +0100)
This makes wiring up mosfets together noticeably easier.
The bind and addnode commands may be rendered somewhat useless by this.

Makefile
inc/FET_sim.h
src/build_helper.c
src/c_connect.c [new file with mode: 0644]
src/main.c

index 9c9e94ab61128ba78b792d55b4cce42258f1f0d8..f5ae798eb1e60960e1debba7afc1e9d23c669c68 100644 (file)
--- 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                \
index 6c4a310dde994921cf5d1e71c1dffae75529f513..2914198afe7483395c167cabd838993d74066e4d 100644 (file)
@@ -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);
index 5a856f2cfabaa1495b9b83f1e821787ec61d5904..a48c3ecc94443788aa41da9e70e4b90135e3fdf5 100644 (file)
@@ -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 (file)
index 0000000..4ed338b
--- /dev/null
@@ -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);
+}
index 8cd78d801192cae7d413881f6b2ddfb3a752d134..68978cc61134667fa3ec6c9bd1a191f125587851 100644 (file)
@@ -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)