
/*
 * $Id: acf_fns.c,v 1.1.1.1 2003/11/04 23:34:56 mjames Exp $
 *
 * $Log: acf_fns.c,v $
 * Revision 1.1.1.1  2003/11/04 23:34:56  mjames
 * Imported into local repositrory
 *
 * Revision 1.5  2001/11/09 22:17:00  mjames
 * changed some comments
 *
 * Revision 1.4  2001/10/31 22:19:56  mjames
 * Tidying up problematical comments caused by CVS
 * 'intelligent' comment guessing
 *
 */

#include "acf_fns.h"

#include "cmdlog.h"
#include "cmdparse.h"
#include "database.h"
#include "expression.h"
#include "generic.h"
#include "jumpering.h"
#include "lx_support.h"
#include "routing.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

socket_t *current_chip;
node_t *current_node;
net_t *current_net;
net_t **current_list;

/* used in net joining in the YACC parser */

net_t **join_context;
JoinMode_t join_mode;
net_t *top_net;

char curr_pin_name[MAXIDLEN];
char curr_pin_ident[MAXIDLEN];
int curr_pin_dir;

int chip_context;

node_context_t node_context;

/* if the user types something like U1(10) or U1(22) */
/* this determines the node and socket */
void handle_jumper_node_text (char *text)
{
        if (top_net && current_chip)
        {
                /* find node itself */
                current_node = find_node (current_chip, Ident, text, Search);
                if (!current_node)
                {
                        Log (
                            LOG_GENERAL,
                            "-- Cannot locate node '%s(%s)'\n",
                            current_chip->identifier,
                            text);
                        return;
                } /* use net that node is on */

                if (!current_node->net)
                {
                        Log (
                            LOG_GENERAL,
                            "-- Cannot locate net for node '%s(%s)'\n",
                            current_chip->identifier,
                            text);
                        return;
                }

                /* this node is a normal jumper of a routed list node e.g. IC1(100) */

                if (current_node->net->list_ref == &routed_list)
                {
                        /* mark this as a net referred to by node ID not net name */
                        if (transfer_net_to_subnet (join_context, top_net, current_node->net))
                        {
                                /* if a net has an external connection then this node is the
                                 * one that provides it */
                                current_node->net->external_node = current_node;
                        }
                        else
                        {
                                Log (
                                    LOG_GENERAL,
                                    "-- node '%s(%s)' is not on routed list\n",
                                    current_chip->identifier,
                                    text);
                                return;
                        }
                }
        }
}

void handle_alias_node_text (char *text)
{
        noderef_t *unlink_noderef;
        node_t *unlink_node;
        net_t *unlink_net;
        char *newname;
        Log (LOG_GENERAL, "-- in handle_alias_node_text --\n");

        if (current_chip && top_net)
        {
                /* this is a jumper of the type IC.name which is a way of connecting a net to a
                 * named list */

                Log (LOG_GENERAL, "-- Looking for net '%s'\n", text);
                current_net = find_net (&unrouted_list, Ident, text, Search);
                if (current_net)
                {
                        Log (
                            LOG_GENERAL,
                            "-- alias of type chip.pin found :%s.%s\n",
                            current_chip->name,
                            text);
                        /* locate the appropriate node reference from unrouted list net */
                        unlink_noderef = remove_noderef_from_list (current_net, current_chip);
                        if (unlink_noderef)
                        {
                                /* now we have a node reference, we need to create a new net */
                                unlink_node = unlink_noderef->node;
                                /*
                                          newname =
                                   malloc(strlen(current_chip->name)+strlen(current_net->identifier)+2);
                                          sprintf
                                   (newname,"%s_%s",current_chip->name,current_net->identifier);
                                */
                                newname = malloc (
                                    strlen (current_chip->name) +
                                    strlen (current_net->identifier) + 2);
                                sprintf (
                                    newname,
                                    "%s.%s",
                                    current_chip->name,
                                    current_net->identifier);
                                unlink_net = find_net (&unrouted_list, Ident, newname, Create);
                                Log (LOG_GENERAL, "-- created new net '%s'\n", newname);
                                /* set new net identifier */
                                unlink_net->name = allocstr (text);

                                unlink_noderef->net = unlink_net; /* this node reference refers
                                                                     back to its parent net */

                                unlink_net->nodecount = 1; /* one more node on this net */

                                /* link node  reference to the net */
                                unlink_noderef->next = NULL;
                                unlink_net->nodes =
                                    unlink_noderef; /* place reference on head of list */
                                unlink_net->how_joined = Aliased_Pin;

                                transfer_net_to_subnet (&unrouted_list, top_net, unlink_net);
                                unlink_net->how_joined = Aliased_Pin;

                                free (newname);
                        }
                        else
                                Log (LOG_GENERAL, "-- failed to remove node from list\n");
                }
        }
        Log (LOG_GENERAL, "-- leaving handle_alias_node_text --\n\n");
}
