Revert "Change t_mosfet to remember node id instead of ptr"
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Fri, 16 Feb 2024 16:34:45 +0000 (17:34 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Fri, 16 Feb 2024 16:34:45 +0000 (17:34 +0100)
This reverts commit d03a1205f99244550c784b4c8c2694ae643f0278.
The commit was not thought through as it should. In addition it contains
some minor changes that should be recommited - that's why I should do
small commits that tend to a single problem/feature.

TODO
inc/FET_sim.h
src/build_helper.c
src/c_addfet.c
src/c_bind.c
src/colors.c
src/main.c
src/sim_main.c
src/sim_node.c
src/text.c

diff --git a/TODO b/TODO
index 823f579812ee7222523e7862afcac07418832b7a..25d04891af32ae7d7a6164c80d42afb0280f39e1 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,4 +1,5 @@
 TODO
+rewrite t_fet and t_node to store indexes insted of pointers to connected parts
 rewrite command parsing (what I've created is just ugly)
 come up with better file structure
 create language to write circuits in (circuit language?) (something like MHRD language?)
@@ -12,5 +13,3 @@ pass 42 norminette
 DONE
 foundations of logic
 expand simulator commands
-rewrite t_fet and t_node to store indexes insted of pointers to connected parts
-       - t_node would not benefit from this, so only t_mosfet stores ids of nodes
index 328b45bb1a37be3ecddf11f609f4352fa9d85c5e..612fc5df5953b9323a7de4fc093428fc0b5eadf2 100644 (file)
@@ -5,8 +5,6 @@
 
 # define MAX_ARGS 3
 
-#include <sys/types.h>
-
 // 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
 typedef enum e_state
@@ -27,15 +25,12 @@ typedef enum e_terminal
        gate,
 }      t_terminal;
 
-typedef ssize_t        t_nodeid;
-
 typedef struct s_node
 {
-       t_nodeid        id;
-       int                     checked;
-       t_state         state;
-       t_state         set_state;
-       t_vec           connected;
+       int             checked;
+       t_state state;
+       t_state set_state;
+       t_vec   connected;
 }                      t_node;
 
 typedef enum e_type
@@ -46,11 +41,11 @@ typedef enum e_type
 
 typedef struct s_mosfet
 {
-       int                     is_opened;
-       t_type          type;
-       t_nodeid        gate;
-       t_nodeid        source;
-       t_nodeid        drain;
+       int             is_opened;
+       t_type  type;
+       t_node  *gate;
+       t_node  *source;
+       t_node  *drain;
 }                      t_mosfet;
 
 typedef enum e_command
@@ -100,7 +95,7 @@ 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_vec *nodes, t_mosfet *mosfet, t_nodeid newid, t_terminal terminal);
+void   bind_fet_node(t_mosfet *mosfet, t_node *node, t_terminal terminal);
 
 void   free_node(void *node);
 int            process_input(t_vec *nodes, t_vec *mosfets);
index cc5a7adfadced5dd64c12823e114f7893e1c669a..20a9f7db82e5e589ba79980685b0238ce26cbb3b 100644 (file)
@@ -1,36 +1,36 @@
 #include "FET_sim.h"
 #include "libft.h"
 
-void   bind_fet_node(t_vec *nodes, t_mosfet *mosfet, t_nodeid newid, t_terminal terminal)
+void   bind_fet_node(t_mosfet *mosfet, t_node *node, t_terminal terminal)
 {
-       t_nodeid        oldid;
+       t_node  *old;
+       size_t  index;
 
-       oldid = -1;
+       old = NULL;
        if (terminal == source)
        {
-               oldid = mosfet->source;
-               mosfet->source = newid;
-               ft_vec_append(&((t_node *)ft_vec_access(nodes, newid))->connected, &mosfet);
+               old = mosfet->source;
+               mosfet->source = node;
+               ft_vec_append(&node->connected, &mosfet);
        }
        else if (terminal == drain)
        {
-               oldid = mosfet->drain;
-               mosfet->drain = newid;
-               ft_vec_append(&((t_node *)ft_vec_access(nodes, newid))->connected, &mosfet);
+               old = mosfet->drain;
+               mosfet->drain = node;
+               ft_vec_append(&node->connected, &mosfet);
        }
        else
        {
-               mosfet->gate = newid;
+               mosfet->gate = node;
        }
-       if (oldid != -1)
+       if (old && ft_vec_find_index(&old->connected, &mosfet, &index) == success)
        {
-               ft_vec_forget(&((t_node *)ft_vec_access(nodes, oldid))->connected, oldid);
+               ft_vec_forget(&old->connected, index);
        }
        return ;
 }
 
 // memset is used to initialize struct padding
-// could be removed
 void   add_node(t_vec *nodes, t_state set_state)
 {
        t_node  node;
@@ -39,7 +39,6 @@ void  add_node(t_vec *nodes, t_state set_state)
        node.checked = 0;
        node.state = set_state;
        node.set_state = set_state;
-       node.id = nodes->size;
        ft_vec_init(&node.connected, sizeof(t_mosfet *));
        ft_vec_append(nodes, &node);
        return ;
@@ -51,9 +50,9 @@ void  add_mosfet(t_vec *mosfets, t_type type)
 
        mosfet.is_opened = 0;
        mosfet.type = type;
-       mosfet.gate = -1;
-       mosfet.drain = -1;
-       mosfet.source = -1;
+       mosfet.gate = NULL;
+       mosfet.drain = NULL;
+       mosfet.source = NULL;
        ft_vec_append(mosfets, &mosfet);
        return ;
 }
index 0e7d89cc5e6806e0795443f82f36bba8937a7959..6ce663b6263e5236f54e53bc90060cc745cc9175 100644 (file)
@@ -5,15 +5,15 @@ int   c_addfet(t_input input, t_vec *mosfets)
 {
        size_t  i;
 
-       ft_printf("Indexes of added FETs:\n\t");
        if (input.argc == 1 && input.argv[0].type == type)
        {
                add_mosfet(mosfets, input.argv[0].val.type);
-               ft_printf("%u", mosfets->size - 1);
+               ft_printf("Index of added FET: %u\n", mosfets->size - 1);
        }
        else if (input.argc == 2 && input.argv[0].type == type && input.argv[1].type == num)
        {
                i = 0;
+               ft_printf("Index of added FETs:\n\t");
                while (i < input.argv[1].val.num)
                {
                        ft_printf("%u ", mosfets->size);
@@ -21,7 +21,6 @@ int   c_addfet(t_input input, t_vec *mosfets)
                        ++i;
                }
        }
-       ft_printf("\n");
        /*else
                print_wrong_usage(input);*/
        return (1);
index 28b359f6d8d183631b81865ba412e4c02e324f43..87e412d51caae4c1d63d265303a817df0164f09b 100644 (file)
@@ -3,7 +3,7 @@
 
 int    c_bind(t_input input, t_vec *nodes, t_vec *mosfets)
 {
-       t_nodeid        nodeid;
+       t_node          *node;
        t_mosfet        *mosfet;
        t_terminal      term;
 
@@ -13,9 +13,9 @@ int   c_bind(t_input input, t_vec *nodes, t_vec *mosfets)
                print_wrong_usage(input);
                return (1);
        }*/
-       nodeid = input.argv[0].val.num;
+       node = ft_vec_access(nodes, input.argv[0].val.num);
        mosfet = ft_vec_access(mosfets, input.argv[1].val.num);
        term = input.argv[2].val.terminal;
-       bind_fet_node(nodes, mosfet, nodeid, term);
+       bind_fet_node(mosfet, node, term);
        return (1);
 }
index 855c63bda2b845af5567666a4ec4a969157f58c0..deb0795560810ac41e6e198ecbfa4823254058bf 100644 (file)
@@ -23,26 +23,27 @@ const char  *state_color_escape(t_state state)
 void   draw_single(t_vec *nodes, t_vec *mosfets, size_t i)
 {
        t_mosfet        *mosfet;
+       size_t          index;
 
        mosfet = ft_vec_access(mosfets, i);
-       if (mosfet->source != -1 &&  (size_t)mosfet->source < nodes->size)
+       if (ft_vec_find_index(nodes, mosfet->source, &index) == success)
        {
-               ft_printf("      %u\n", mosfet->source);
-               ft_printf("      %s|%s\n", state_color_escape(((t_node *)ft_vec_access(nodes, mosfet->source))->state), g_default_ac);
+               ft_printf("      %u\n", index);
+               ft_printf("      %s|%s\n", state_color_escape(mosfet->source->state), g_default_ac);
        }
        else
        {
                ft_printf("      %sNULL%s\n", g_red_ac, g_default_ac);
                ft_printf("      %s|%s\n", g_red_ac, g_default_ac);
        }
-       if (mosfet->gate != -1 &&  (size_t)mosfet->gate < nodes->size)
-               ft_printf("%4u%s--%s%c\n", mosfet->gate, state_color_escape(((t_node *)ft_vec_access(nodes, mosfet->gate))->state), g_default_ac, mosfet->type);
+       if (ft_vec_find_index(nodes, mosfet->gate, &index) == success)
+               ft_printf("%4u%s--%s%c\n", index, state_color_escape(mosfet->gate->state), g_default_ac, mosfet->type);
        else
                ft_printf("%sNULL--%s%c\n", g_red_ac, g_default_ac, mosfet->type);
-       if (mosfet->drain != -1 &&  (size_t)mosfet->drain < nodes->size)
+       if (ft_vec_find_index(nodes, mosfet->drain, &index) == success)
        {
-               ft_printf("      %s|%s\n", state_color_escape(((t_node *)ft_vec_access(nodes, mosfet->drain))->state), g_default_ac);
-               ft_printf("      %u\n\n", mosfet->drain);
+               ft_printf("      %s|%s\n", state_color_escape(mosfet->drain->state), g_default_ac);
+               ft_printf("      %u\n\n", index);
        }
        else
        {
index 38a577f50fbee928d9072b17218e3569093e7c40..55a773f86c68b9c13aed52d31b003def4c727ed9 100644 (file)
@@ -2,17 +2,15 @@
 #include "libft.h"
 #include <stdlib.h>
 
-// Does not use input
-// Just builds a single FET
 void   build_graph(__attribute__((unused)) const char *filename, t_vec *nodes, t_vec *mosfets)
 {
        add_node(nodes, off);
        add_node(nodes, off);
        add_node(nodes, pull_up);
        add_mosfet(mosfets, p);
-       bind_fet_node(nodes, ft_vec_access(mosfets, 0), 0, gate);
-       bind_fet_node(nodes, ft_vec_access(mosfets, 0), 1, drain);
-       bind_fet_node(nodes, ft_vec_access(mosfets, 0), 2, source);
+       bind_fet_node(ft_vec_access(mosfets, 0), ft_vec_access(nodes, 0), gate);
+       bind_fet_node(ft_vec_access(mosfets, 0), ft_vec_access(nodes, 1), drain);
+       bind_fet_node(ft_vec_access(mosfets, 0), ft_vec_access(nodes, 2), source);
        return ;
 }
 
@@ -121,15 +119,13 @@ int       has_correct_argc(t_input input)
 
        c = input.command;
        argc = input.argc;
-       if ((c == next || c == draw || c == help) && argc > 1)
+       if ((c == next || c == draw || c == addfet || c == help) && argc > 1)
                return (0);
        if (c == setnode && argc != 2)
                return (0);
        if (c == bind && argc != 3)
                return (0);
-       if ((c == addnode || c == addfet) && argc > 2)
-               return (0);
-       if (c == addfet && argc == 0)
+       if (c == addnode && argc > 2)
                return (0);
        return (1);
 }
@@ -153,7 +149,7 @@ int parse_arg(t_input *input, const char *str, size_t i)
        int                             res;
 
        res = 0;
-       if (is_num && (c == next || c == draw || (c == setnode && i == 0)
+       if (is_num != 0 && (c == next || c == draw || (c == setnode && i == 0)
                        || (c == addfet && i == 1) || (c == addnode && (input->argc == 1 || i == 1))
                        || (c == bind && i < 2)))
        {
@@ -241,7 +237,7 @@ int process_input(t_vec *nodes, t_vec *mosfets)
        int                             res;
        static t_input  input = {.command = help, .argc = 0};
 
-       ft_printf("FET_sim> ");
+       ft_printf("FET_sim>");
        if (!get_input(&input))
                return (1);
        res = 1;
@@ -273,12 +269,10 @@ int       main(__attribute__((unused)) int argc, char **argv)
        ft_vec_init(&nodes, sizeof(t_node));
        ft_vec_init(&mosfets, sizeof(t_mosfet));
        build_graph(argv[1], &nodes, &mosfets);
-       update_nodes(&nodes);
        while (process_input(&nodes, &mosfets))
        {
                continue ;
        }
-       ft_printf("\n");
        update_nodes(NULL);
        ft_vec_free(&nodes, free_node);
        ft_vec_free(&mosfets, NULL);
index 12f04c017a808c1267e2a61fab318c8b5ecc7423..14cc9b56b48229b098aa9aa9ad70d7c9177199d0 100644 (file)
@@ -1,16 +1,11 @@
 #include "FET_sim.h"
 #include "libft.h"
 
-static void    update_mosfet(t_vec *nodes, t_mosfet *mosfet)
+static void    update_mosfet(t_mosfet *mosfet)
 {
        t_state state;
 
-       if (mosfet->gate == -1)
-       {
-               mosfet->is_opened = 0;
-               return ;
-       }
-       state = ((t_node *)ft_vec_access(nodes, mosfet->gate))->state;
+       state = mosfet->gate->state;
        if (mosfet->type == p && state == on)
                mosfet->is_opened = 1;
        else if (mosfet->type == n && state == off)
@@ -20,7 +15,7 @@ static void   update_mosfet(t_vec *nodes, t_mosfet *mosfet)
        return ;
 }
 
-static void    update_mosfets(t_vec *nodes, t_vec *mosfets)
+static void    update_mosfets(t_vec *mosfets)
 {
        size_t          i;
        t_mosfet        *mosfet;
@@ -29,7 +24,7 @@ static void   update_mosfets(t_vec *nodes, t_vec *mosfets)
        while (i < mosfets->size)
        {
                mosfet = ft_vec_access(mosfets, i);
-               update_mosfet(nodes, mosfet);
+               update_mosfet(mosfet);
                ++i;
        }
        return ;
@@ -38,6 +33,6 @@ static void   update_mosfets(t_vec *nodes, t_vec *mosfets)
 int    sim_step(t_vec *nodes, t_vec *mosfets)
 {
        update_nodes(nodes);
-       update_mosfets(nodes, mosfets);
+       update_mosfets(mosfets);
        return (1);
 }
index 432a996b6f1bf0ee2c9f54cfe0cdb6967103c1af..eee368e46edf7c42285bde471e7f1e756faf694f 100644 (file)
@@ -2,14 +2,14 @@
 #include "libft.h"
 #include <stdlib.h>
 
-static t_nodeid        get_neighbour_nodeid(const t_mosfet *mosfet, const t_nodeid start)
+static t_node  *get_neighbour_node(const t_mosfet *mosfet, const t_node *start)
 {
        if (mosfet->source == start)
                return (mosfet->drain);
        return (mosfet->source);
 }
 
-static void    find_connected(t_vec *nodes, t_node *node, t_vec *connected_nodes)
+static void    find_connected(t_node *node, t_vec *connected_nodes)
 {
        size_t          i;
        t_node          *neigh_node;
@@ -25,8 +25,8 @@ static void   find_connected(t_vec *nodes, t_node *node, t_vec *connected_nodes)
                mosfet = *(t_mosfet **)ft_vec_access(&node->connected, i);
                if (mosfet->is_opened)
                {
-                       neigh_node = ft_vec_access(nodes, get_neighbour_nodeid(mosfet, node->id));
-                       find_connected(nodes, neigh_node, connected_nodes);
+                       neigh_node = get_neighbour_node(mosfet, node);
+                       find_connected(neigh_node, connected_nodes);
                }
                ++i;
        }
@@ -74,7 +74,7 @@ void  update_nodes(t_vec *nodes)
                cur_node = ft_vec_access(nodes, i);
                if (!cur_node->checked)
                {
-                       find_connected(nodes, cur_node, &connected_nodes);
+                       find_connected(cur_node, &connected_nodes);
                        state = resolve_state(&connected_nodes);
                        apply_state(reduce_state(state), &connected_nodes);
                        ft_vec_forget_range(&connected_nodes, connected_nodes.size, 0);
index b46ca15ba5aa94b6feabe85c1fd9306d143f7588..086351d4aa36a3ddf992f0581f0838354764374a 100644 (file)
@@ -59,13 +59,14 @@ static const char   g_help_exit_str[] = ""
 static const char      g_general_help_str[] = ""
        "This is a FET_sim - simulator of FET (Field Effect Transistor) logic.\n"
        "Version number: %s"
-       "You can use the following commands:\n\n"
+       "You can use the following commands:\n"
        "next [STEPS] \t\t- advances the simulation\n"
        "draw [IND] \t\t- draws the state of the simulation\n"
        "setnode IND STATE \t- sets the \"set_state\" of the node indexed by IND to STATE\n"
        "addnode [STATE] [NUM] \t- adds new nodes\n"
        "addfet TYPE [NUM] \t- adds new FETs\n"
-       "bind NODE FET TERMINAL \t- binds node indexed by NODE to TERMINAL of transistor indexed by FET\n"
+       "bind NODE FET TERMINAL \t- binds node indexed by NODE to TERMINAL\n \
+               \t\t\t  of transistor indexed by FET\n"
        "help [COMMAND] \t\t- shows this help or help for COMMAND\n"
        "exit [...] \t\t- exits this program\n\n";