
/*
 * $Header: C:/cvsroot/Vert03/vertlib/database.h,v 1.2 2004/06/22 21:44:14 mjames Exp $ 
 *
 * $Log: database.h,v $
 * Revision 1.2  2004/06/22 21:44:14  mjames
 * Firrst build most files
 *
 * Revision 1.1.1.1  2003/11/04 23:34:56  mjames
 * Imported into local repositrory
 *
 * Revision 1.14  2003/01/02 21:37:15  mjames
 * Experiment on creating NOT_ROUTABLE_H and NOT_ROUTABLE_L
 * properties on the nets so that pin jumpers can be made without a problem.
 *
 * Still need to sort out pin assignments made to these not_routable nets
 * which will become legal in some cases so that pullups and pulldown
 * pins can be used on the FPGA.
 *
 * Revision 1.13  2002/09/30 13:26:01  MJAMES
 * Upgraded nets to include 'lhs_expression' being the range on the
 * left hand side of a signal connection to a port
 *
 * std_logic_vector (0) <= std_logic , on a port of a chip
 *
 * Revision 1.12  2002/09/09 10:14:14  mjames
 * Moved pin remapping function to pin ident editing function from
 * sorting pin name routine.
 *
 * Revision 1.11  2002/08/06 12:52:49  mjames
 * Merge in from latest version
 *
 * Revision 1.11  2002/03/21 17:12:58  mjames
 * Added search path to vertical file opening for read
 *
 * Revision 1.10  2002/01/16 11:22:42  mjames
 * database.h header file is read in first as it undefined DLL stuff irrelevant
 * to HPUX
 *
 * Revision 1.9  2001/12/20 13:53:09  mjames
 * Added a bundle width field to the chip declaration
 *
 * Revision 1.8  2001/10/31 22:20:02  mjames
 * Tidying up problematical comments caused by CVS
 * 'intelligent' comment guessing
 *
 * Revision 1.7  2001/10/23 21:12:43  mjames
 * Created preliminary 'disconnect_node' function
 *
 * Revision 1.6  2001/09/16 20:36:03  mjames
 * Second attempt to modify wire bundles to be connector rather than net
 * centric. Allows more than one connector to carry the same net,
 *
 * Revision 1.5  2001/09/16 19:49:54  mjames
 * Second attempt at bundling the pins on sockets
 *
 * Revision 1.4  2001/09/13 21:08:21  mjames
 * Added simple wire bundling data structures.
 *
 * Revision 1.3  2001/06/06 12:10:24  mjames
 * Move from HPUX
 *
 * Revision 1.2  2000/10/19 22:06:36  mjames
 * Removed spurious RCS log lines from new CVS stuff
 *
 * Revision 1.1.1.1  2000/10/19 21:58:35  mjames
 * Mike put it here
 *
 *
 *  
 */





#if !defined _DATABASE
#define _DATABASE


#if !defined _EXPRESSION 
#error "Need to include 'expression.h' before database.h"
#endif 

#if ! defined _GENERIC
#error "generic.h" must be included first
#endif

#define MAXIDLEN 1024

#define BUFPREFIX "XX_"

#if defined PC
#define VERTICAL_INI_PATH "vertical.ini"
#define VERTICAL_PATH     ".;c:/cygwin/usr/local/vert_files"
#else
#define VERTICAL_INI_PATH "vertical.ini"
#define VERTICAL_PATH     ".;$(HOME)/.caddata/vert_files"
/* DLL creation declaration mode. not used on HP */
#if ! defined __declspec
#define __declspec(x) 
#endif
#endif


/* incomplete declarations to placate 'c' */

struct connector;
struct net;
struct netref;
struct equivalent_node_set;


/* the direction list */
typedef enum { NONE,INPUT,OUTPUT,BUFFER,BIDIR,CONFIG,POWER } pindir_t;


#define Create 0
#define Search 1



#define ISNULLSTR(x) ((x == NULL) || (x && (x[0]==0)))


/* operations to manipulate the data structure */
/* from the ACF reader */

/* typedef enum { Ident,Type,Name,Value,Datatype,How_Routed } property_t; */
typedef enum { Ident,Type,Name,Value,How_Routed } property_t;

/* used in seting the routing_status attribute of a net */

typedef enum
  {
  Free,        /* name given may be reassigned */
  By_Name,     /* Routed by Name */
  By_Net,      /* Routed by already existing net */
  By_Fixed_Net,/* Routed through fixed existing net */
  By_Exact_Net,/* Routed using identical already existing net */
  By_Creating, /* Routed by creating a new net */
  By_Jumpering,/* Routed by connecting an existing net */
  By_FixPins,  /* can only go here as pins are fixed */
  Not_Routable, /* not allowed to participate in any routing */
  Not_Routable_H, /* pullup/VCC */
  Not_Routable_L  /* pulldown/GND */
  } HowRouted_t;
/* these macros are reliant on sorted ordinal values */
#define IS_NOT_ROUTABLE(x) ((x)>=Not_Routable)
#define IS_ROUTABLE(x)     ((x)<Not_Routable)


/* a type to hold a VHDL data type */
typedef struct vhdl {
  char * basetype;
  expression_t * decl_expr; /* the range given in its declaration */

  expression_t * expr; /* the expression that is used to */
                       /* define the values of high/low etc */
  expression_t * default_expr; /* default value assigned in declaration */
/*  char * text; */  /* this is the original declaration */
/*  int low;
  int high; */
  unsigned int    valid:1;     /* is the expression valid ? */
  unsigned int    is_vector:1;  /* is this port a vector */
  unsigned int    is_downto:1;  /* is the vector X TO Y or X DOWNTO Y  */
  } vhdl_t;


/* object node : point in a circuit connected a socket and a net */
typedef struct node
{
  struct node * sktnext; /* linked to a socket */
  char * identifier;      /* e.g 102, A22 */
  char * name;            /* internal signal name (actually same as netname )  */

  vhdl_t * vhdltype;      /* the subset of the net that the node is connected to */
  vhdl_t * orig_vhdltype; /* how this node was originally declared */   
  expression_t * lhs_expr;/* if the net is sliced on socket pin assignment LHS */
  pindir_t   pindir;      /* final pin type input? output? bidir? unknown? */
  int        pin_group;   /* can say 'any pin in group(n) only  is interchangeable' */
  struct net * net;       /* refer to net this node is on (if known) */

  struct net * routed_net; /* which routed net connects to this node */

  int    refcount;        /* how many nets refer to this node */
  struct socket * socket; /* refer to socket this node is part of */
  struct equivalent_node_set * equivalent_nodes; /* member of set of equivalent nodes */

/* sorting data structure */
  char * id_numeric_chars; /* the numeric part of the identifier */
  int    id_numeric_cnt;   /* the number of numeric chars in this part of the identifier */

  char * id_alpha_chars;   /* the alphabetic part of the identifier */
  int    id_alpha_cnt;     /* the number of alpha chars in this part of the ident */

  int    pin_row;
  int    pin_col;
  int    pin_index;        /*  the ordinal pin number : which would be used in a bundle */
  int    bundle_index; /* if >=0 then this pin is in a bundle */

  unsigned int fixed_pin:1;/* if fixed_pin = 1 then this pin can only be used
                              to connect to the net in a particular
                              direction */
  unsigned int in_use:1;   /* if '1' the node is used by a pin
                              if '0' the node has had a dummy name assigned*/
  unsigned int template_flag:1;   /* used in template processing */
  unsigned int net_assigned:1; /* if '1' the node (name) has a net assigned
                                  to it by routing */
  unsigned int fix_location:1; /* if set then nets referring to this pin
                                  have to be connected to it and nowhere else */
  unsigned int reservation:1;  /* mark this as a reserved pin when it is given rsvXXXX ident */


} node_t;


/* different nets may refer to the same nodes
   but dependent on whether net is desired or already routed, different rules
   apply */

typedef struct noderef
{
  struct noderef * next;
  pindir_t   pindir;      /* proposed pin type input ? output ? bidir ? unknown ? */
  node_t * node;
  struct net * join_parent; /* Which net this node reference is joined to (used in
                               alias/jumpering */
  struct net  * net;
                          /* the net that owns this node reference */
  unsigned int r_flag:1;  /* a routing flag to indicate this node has been
                             found already at this phase of routing  */
/*  char * datatype; */       /* pointer to the data type of this node */

  vhdl_t *  vhdltype;                  /* what this now has been aliased to in VHDL */
  vhdl_t *  orig_vhdltype;                  /* what this now has been aliased to in VHDL */
  expression_t * lhs_expr;  /* the expression will be copied over when routing . Bus slice on LHS of pin assignment */
  char *   orig_name;       /* if this pointer is not null then the name of this reference will be used
                             in place of the net name when listing the nets  */

  struct noderef * base_noderef; /* used in jumpering to define original node reference that will be renamed:
                                    in the original node reference this pointer is to itself */
} noderef_t;


/* the aliased net join means that each sub_net keeps its own
   signal name while its identiier is common. The Jumpered
   net join means that the new signal is to be treated
   as a new net entity */

typedef enum {
  NotJoined=0,
  Aliased,  /* names are aliased onto same physical piece of wire */
  Jumpered, /* separate wires can be joined together */
} JoinMode_t;


/* object net : a piece of wire connected to a set of nodes */



typedef struct net
{
  struct net * next;
  struct net * prev;      /* demand that previous net be referred to */
  struct net ** list_ref; /* the membership of which list:
                             routed_list unrouted_list named_list*/
  char * identifier;      /* e.g N1, N2 */
  char * name;            /* signal names */
  HowRouted_t how_routed; /* in fact this indicates how it was routed */
  int      nodecount;     /* how many nodes does this net have ? */
  noderef_t * nodes;      /* and all the other nodes on this net */
  struct net * join_parent; /* Which net this net is joined to (used in
                               alias/jumpering */
  struct net * subnets;     /* if this net has subnets , refer to them */
  struct net * joined_nets; /* other nets at this level of join */
  struct net * unrouted_fix_net; /* is there an unrouted net fixed to this one ? */
  struct net * vhdl_connect_net; /* pointer to a net connected by VHDL connection */
  JoinMode_t how_joined;  /* if subnets, is this an alias join or a
                             jumpered join */
  struct net * unrouted_reference; /* points at the net that is the unrouted
                                      original , if this is a routed net. */
  node_t * external_node    ; /* if not null and there is a jumper then print
                                 the external node name rather than the net name*/
  unsigned int nodes_reserved : 1; /* This net has been processed by the ensure_reservation function in routing.c */
  unsigned int has_external : 1 ; /* one of the net nodes (at least)
                                     is an external connection */
  unsigned int has_fix_location : 1 ; /* one of the net nodes (at least)
                                         is a FIX_LOCATION pin */
/* expanded information for partitioning */
  unsigned int leaves_partition : 1; /* '1' : crosses boundary of partition*/
  unsigned int inside_partition : 1; /* '1' : in use as internal signal
                                        in partition */
  unsigned int needs_buff_sig: 1;  /* if '1' then the external connection needs a local signal */
  unsigned int force_port: 1;  /* if '1' then the net will be forced to be a port on partition  */
  unsigned int type_defined: 1;  /* the signal type has been defined so does not need to be 
                                    patched up by guesswork in the partition calculation Oct 2000*/
 /* bundle structures : cause signal renaming when sockets
    are converted to a wire bundle leaving the 
    partition */
  unsigned int  bundle_member;  /* set if the net is a bundle member */ 
  int          bundle_index;       /* the ordinal number of the pin of the bundle */

  pindir_t ext_dir;                  /* basic boundary crossing direction*/
/*  char *   datatype;  */                 /* the string description of the data type */
  vhdl_t * vhdltype;                  /* what this looks like in VHDL */
} net_t;

/* net reference list, attched to a node */
typedef struct netref
{
  struct netref * next;
  net_t * net;
} netref_t;


/* unrouted node reference, which permits node by node pin  renaming */

typedef struct unrouted_ref
{
  struct unrouted_ref * next;
  net_t ** listref;       /* the root pointer of which list this unrouted reference has been added to */
  char * identifier;  /* used to point to the pin identifier before creating unrouted net */
  char * name;        /* this is the name by which this pin shall be known */ 
  char * orig_name;       /* if this pointer is not null then the name of this reference will be used
                             in place of the net name when creating the nets !! */
  pindir_t   pindir;      /* proposed pin type input ? output ? bidir ? unknown ? */
  int        pin_group;
  vhdl_t *  vhdltype;                  /* what this looks like in VHDL */
  vhdl_t *  orig_vhdltype;  /* place holder for the signal type as originally declared */
  expression_t * lhs_expr; /* expression is sliced on the LHS */
} unrouted_ref_t;


/* member of list of pins  that carry the same signal on a device */
typedef struct equivalent_node {
  struct equivalent_node * next;
  node_t * node;
  } equivalent_node_t;

/* pointer to list of lists of pins  that carry the same signal on a device */
typedef struct equivalent_node_set {
  struct equivalent_node_set * next;
  equivalent_node_t * nodes;
  } equivalent_node_set_t;

/* a chip socket is a socket, as are all the board connectors */
/* A lastnode pointer is provided so that new nodes can be added to the     */
/* end rather than the beginning of the list */
/* reason: so that priority of assigning nodes is kept */
/* this will be altered by the sorting of the nodes by identifier */
typedef struct socket
{
  struct socket * next;
  struct socket * template_socket; /* the VHDL component for this entity if present */
  char * identifier; /* e.g U1, U2 */
  char * type;
  char * name;
  char * value;
  node_t * nodes; /* the nodes attached to this chip */
  node_t * lastnode;
  unrouted_ref_t * unrouted_refs; /* when an unrouted node is read in the node is added here */
  generic_info_t * generics; /* look in generic.c for more info */
  generic_info_t * unrouted_generics;/* before elaboration */
  equivalent_node_set_t *  equivalent_node_set; /* these pins carry equivalent signals 
                                     FPGA context : represent inner through signals
                                     component context : represent external jumpers
                                     around pins of component. */

  int max_pin_col;     /* determine pin identifier limits for later (see sorting.c, */
  int min_pin_col;     /* sorting device pin IDs for more info.)                    */
  int max_pin_row;
  int min_pin_row;
  
  unsigned int is_FPGA:1;     /*  alters interpretation of equivalent nodes */

  unsigned int selected:1;   /* a '1' here is used in the VHDL printout */
  unsigned int is_external:1; /* a '1' here changes the rules about external ports*/
                              /* If it is external then all nets attached give their
                                 names to the pins of the socket rather than calling
                                 them 'rsvXXXXXz' */
  unsigned int is_template:1; /*   this chip is a template if it is '1' */
  unsigned int socket_type_seen:1;   /* used in verilog printout to force
                                    * printout of only one instance 
                                    * of a particular type */
  struct socket * parent_template_ref; /*   this chip is an alias template if this pointer is non-null  */
  int    route_flags;         /* any device can have its fasttrack flags set independently */
  int    named_in_use_cnt;    /* counters used by the statistics module */
  int    named_cnt;
  int    routed_cnt;
  int    unrouted_cnt;
  int    lowest_bundle; /* base for bundle indices */

  int highest_bundle ; /* non-zero will create an external net declaration using a bus with highest_bundle -1 pins on it in the top 
                          level of a partition */
  int bundle_width   ; /* number of valid pins in the bundle */ 
} socket_t;

/* case insensitive strcmp */
extern int strcmp2(char * s, char * t);



/* this is equvalent to 'strdup' but checks null pointer */
/* extern char *  allocstr(char * s); */
#define allocstr(s) ((s)==NULL ? NULL : strdup(s))




extern vhdl_t * get_vhdl_buswidth( noderef_t * ref );

extern void define_pin(net_t ** listref,
                       socket_t * chip,
                       char * name,
                       int pin_type,
                       int pin_group,
                       char * identifier,
                       vhdl_t * vhdl,
                       expression_t * lhs_expr);

/* this is a check on a socket ident : if it doesnt have one */

extern void ensure_socket_ident(socket_t * chip);

/* a string that is suffixed to names and identifiers
   when read in from the database */

extern void set_board_suffix(char * s);


/* set the socket type for further information */
extern void set_socket(socket_t * socket,
                       property_t prop,
                       char * str);

/* Tell the system about a new socket */
/* locate a socket by a property */
extern socket_t * find_socket(property_t prop,
                              char * str,
                              int srchonly,
                              socket_t ** head_ref);


extern void set_net(net_t * net,property_t prop,char * str);

extern net_t * find_net(net_t ** listref,
                        property_t prop,
                        char * str,
                        int srchonly);

/* set a node property */
extern void set_node(node_t * node,
                     property_t prop,
                     char * str);

/* locate a node on a chip by a property */
extern node_t * find_node(socket_t * socket,
                          property_t prop,
                          char * str,
                          int srchonly);

/* joins a node to a net */
extern void connect_node_net(char * orig_name, /* added to allow name aliasing (this will be the OLD name ) */
                             node_t * cnode,
                             net_t * cnet,
                             pindir_t pindir,
                             vhdl_t * vhdl,
                             vhdl_t * orig_vhdl,
                             expression_t * lhs_expr);


/* remove net name attributes from the routed list */
extern void del_net_names(char * template);


/* disconnect a node from the netlist */
extern void disconnect_node(socket_t * chip, node_t * node);


/* global variables */
/* used in setting the routing status attribute of a net */
/* these are constant strings  */
/*extern char   Routed[]  ;     */ /* "Routed"    */
/*extern char   AssignedName[] ;*/ /* "Named"     */
/*extern char   Unrouted[] ;    */ /* "Unrouted"  */


extern socket_t * socket_head;
extern socket_t * template_head;
extern net_t    * routed_list;
extern net_t    * named_list;
extern net_t    * named_tail;
extern net_t    * unrouted_list;


extern char  nullstr[];

extern char * decode_how_routed[];


extern char * decode_which_list(net_t ** list_head);


/* this points at std_logic, std_ulogic etc */
extern vhdl_t * default_vhdl_datatype;
extern vhdl_t * default_vhdl_bustype;

extern void InitialiseData(void);



#endif