// shared.h - shared variables and constants

// shared between bport_tx.c and bport_rx.c

int comma_table[2];         //table of K28.5 characters (see table 10-8)
int control_table[16];      //table mapping scrambled control values to control characters (see table 10-9)
int data_table[256][2];     //table of data characters (see table 10-7)
#ifdef unsubscripted
boolean sync_error_signal;  // receiver error monitor has detected loss of synchronization
boolean sync_lost_signal;   // receiver is in a resynchronization state
#else
boolean sync_error_signal[NPORT];
boolean sync_lost_signal[NPORT];
#endif

// shared between speed.c and dsport_rx.c

speedCode ds_portRspeed;    // Filtered and latched receive speed for indicated port

// shared between port.c and node.c

#ifdef unsubscripted
const Beta_mode_only_port;  // TRUE if the port does not support DS mode, used to indicate
                            // functions which may be omitted on Beta-only port implementations
boolean force_disconnect;   // if a restore attempt fails, set true in P10 to force
                            // connection_status to cause a disconnection
                            // also if a loop in a Legacy cloud is detected
boolean in_standby;         // Port is in State P9:Standby or P10:Restore
boolean port_under_test;    // port currently being tested to ensure no loop
port_state_type port_state;
boolean rx_ok;              // In DS mode indicates the reception of a debounced TpBias signal
                            // In Beta mode, indicates synchronization with the peer port
boolean sending_tone;       // true whilst a tone is being transmitted
boolean untested;           // port is connected but untested
#else
const Beta_mode_only_port[NPORT];
boolean force_disconnect[NPORT];
boolean in_standby[NPORT];
boolean port_under_test[NPORT];
port_state_type port_state[NPORT];
boolean rx_ok[NPORT];
boolean sending_tone[NPORT];
boolean untested[NPORT];
#endif

boolean all_ports_suspended; // TRUE if suspended ports > 0 and active ports == 0
boolean found_loop;         // Flag used by the loop tester to force the tested port into
                            // the Loop Disabled state

// shared between port.c, node.c and bport_rx.c

#ifdef unsubscripted
boolean untested_state;     // port is in state P11:Untested
#else
boolean untested_state[NPORT];
#endif

// shared between port.c, node.c and bport_tx.c

#ifdef unsubscripted
boolean bport_sync_ok;      // Port machine sets this to indicate to Connection Management 
                            // that a port has achieved synchronization with peer port.
#else
boolean bport_sync_ok[NPORT];
#endif

// shared between port.c, bport_rx.c and bport_tx.c

#ifdef unsubscripted
boolean bport_on;           // Set to true to instruct the Beta port to commence operating
                            // Set to false to instruct the Beta port to cease operating
#else
boolean bport_on[NPORT];
#endif

// shared between port.c, dsport_rx.c and dsport_tx.c

#ifdef unsubscripted
boolean dsport_on;          // Set to true to instruct the DS port to commence operating 
                            // Set to false to instruct the DS port to cease operating
#else
boolean dsport_on[NPORT];
#endif

// shared between the main arb state machine and dsport_tx.c

boolean non_null_packet;    // TRUE if current packet contains a data payload and therefore
                            // requires the ending sequence of a legacy packet to include
                            // time for dribble bits

// shared between the main arb state machine and node.c

int gap_count;
boolean gap_count_reset_disable; // If set, a bus reset will not force the gap_count to the maximum
boolean max_arb_state_timeout(); // See definition of All:R0b in Clause 13
                                 // implicitly set by main arb state machine
                                 
speedCode min_operating_speed; // min port speed of all active Beta mode ports
boolean need_new_LTP;       // TRUE if need to send a new LTP packet either during this round of testing
                            // or at the start of the next round
int physical_ID;
int reset_count;            // count resets during this time
boolean root;               // TRUE if root node
int standby_phy_ID[NPORT];  // information to be saved in case the peer port 
boolean T0_timeout;         // TRUE if Tree_ID detects a loop
int test_port;

// shared between the main arb state machine and port.c

#ifdef unsubscripted
boolean disable_request;    // Requests transmission of TX_DISABLE at the end of the next packet
boolean receive_ok;         // In DS_mode indicates the reception of a debounced TpBias signal
  // In Beta_mode, indicates the reception of a continuous electrically valid signal. 
  // Note, is set to false during the time that only connection tones are detected in Beta mode.
  // This is maintained as a read-only bit in the port register map (supersedes the Bias bit in 1394a).
boolean reset_notify;
boolean resume_fault;       // Set when peer port fails to complete resume
boolean standby_fault;
boolean suspend_fault;      // Set when peer port fails to complete suspend
#else
boolean disable_request[NPORT];
boolean receive_ok[NPORT];
boolean reset_notify[NPORT];
boolean resume_fault[NPORT];
boolean standby_fault[NPORT];
boolean suspend_fault[NPORT];
#endif

boolean ibr;             		// Set when a long bus reset is needed
timer max_occupancy_timer;  // for a timeout on how long loop free build can hold the bus
boolean OK_to_detect_reset; // TRUE if a resuming port is permitted to monitor for BUS RESET
boolean random_bool();      // Not defined in C code - returns a random TRUE or FALSE value
boolean resumption_done;    // Resumption by at least one resuming port forces all to be done
int senior_port;            // port number of the port pointing towards proxy_root 
boolean signaled;           // Indicate transmission of TX_DISABLE_NOTIFY, TX_SUSPEND or TX_STANDBY

// shared between the main arb state machine, node.c and port.c

#ifdef unsubscripted
boolean attach;             // Indicates that the untested beta port is ready to attach with the 
                            // next bus reset  
boolean connected;          // Connection established with a peer port, and (Beta mode only) 
                            // operating speed negotiation completed. 
                            // This is maintained as a read-only bit in the port register map.
boolean disabled;           // PHY register bit, set to disable a port under software control, 
                            // and indicates that the port is in state P6:Disabled
boolean do_standby;         // TRUE when the port has been commanded to be a standby initiator
boolean loop_disabled;      // Port is in State P12: Loop_disabled - set when the port loses synchronization
                            // during loop testing or a loop is detected
boolean proxy;              // When true, instructs the arb state machine to generate a proxy self_ID 
                            // for the peer node on this port (which can only be a leaf node).
boolean resume;             // Indicates that the port is resuming, also causes a suspended port 
                            // to resume.
boolean suspend_request;    // Set when PHY port is requested to suspend
#else
boolean attach[NPORT];
boolean connected[NPORT]; 
boolean disabled[NPORT];
boolean do_standby[NPORT];
boolean loop_disabled[NPORT];
boolean proxy[NPORT];
boolean resume[NPORT];      
boolean suspend_request[NPORT];
#endif
boolean border_node;        // TRUE if any active DS ports or Legacy link
boolean boundary_node;      // with at least one active port and at least one suspended
boolean bus_initialize_active; // Set while the PHY is initializing the bus
int HR_G;                   // Generation - alternates between 0 and 1
LTP_mode_type HR_mode;      // mode of the packet to be transmitted
int HR_test_value;          // test value to be transmitted
boolean in_control;         // TRUE if won arbitration
boolean isbr_OK;            // Set when asynchronous or immediate arbitration conditions permit
                            // an arbitrated (short) bus reset to be commenced
boolean isolated_node;      // Set if no ports active
boolean loop_to_detect;     // TRUE during reset up to S1 or S2
boolean send_attach;        // TRUE to request the port to send attach requests
timer test_interval_timer;  // for a timeout on test pattern return

// shared between the main arb state machine, port.c, node.c, bport_rx.c and bport_tx.c
#ifdef unsubscripted
speedCode port_speed;       // Negotiated operating speed of the port. Encoding as max_port_speed. 
                            // The value of Negotiated_speed in the port register map is set to 
                            // this value. Value established on connection in Beta_mode, or  
                            // during self_ID when in DS mode.
#else
speedCode port_speed[NPORT];
#endif

// shared between the main arb state machine, port.c, node.c, speed.c and dsport_rx.c
#ifdef unsubscripted
boolean bias;               // If equal to one, debounced incoming TpBias is detected. 
#else
boolean bias[NPORT];
#endif

// shared between the main arb state machine, port.c, node.c, bport_tx.c and dsport_tx.c
#ifdef unsubscripted
portSymbol portT;           // symbol to be sent - port sends the current contents whenever
                            // it is ready to transmit a symbol
#else
portSymbol portT[NPORT];
#endif

// shared between the main arb state machine and process_req.c

pktType a_format;
pktType a_link_format;
speedCode a_speed;
boolean accelerating;       // no imminent cycle start, so no danger of starving it
                            // required to be set false on a PHY/Link interface disable/reset
boolean advance_OK[NPORT];  // TRUE if can advance to the next arb state in the FIFO
boolean arb_reset;
boolean async_pending;
boolean B_bus;              // TRUE if no Legacy nodes (including Legacy Links) on bus
boolean B_node_root;        // TRUE if the root is in the local B cloud 
                            // (NB, FALSE if root has a Legacy link)
boolean collision[NPORT];   // set TRUE when there's a colliding packet from the port
                            // cleared when the collision is resolved by TX of a null packet
pktType current_pkt[NPORT]; // most recently read packet type from fifo for port
boolean current_request;
pktType cyc_start_format;
pktType cyc_start_link_format;
speedCode cyc_start_speed;
boolean cycle_start_request; // TRUE if cycle start request from link
boolean DS_stuck;           // TRUE if there might be a DS port stuck in DP somewhere on the bus
pktType i_format;
pktType i_link_format;
speedCode i_speed;          // information about the current link request;
pktType imm_format;
pktType imm_link_format;
speedCode imm_speed;
boolean immediate_link_request;  // TRUE if immediate request from link
boolean immediate_request;  // TRUE if immediate request from either PHY or link
boolean iso_cycle;          // TRUE if the bus is in the isochronous period
boolean isoch_even_request;
boolean isoch_odd_request;
boolean isoch_pending;
boolean link_CS_indications; // TRUE if link gives indications of cycle start due or decelerate 
                            // (Legacy link) - set to FALSE on PHY/Link interface reset or disable
timer max_beta_timer;       // to prevent driving DP into a DS cloud provoking an arb state timeout
                            // needed only in border nodes
speedCode max_Legacy_path_speed;
boolean LPS;                // tracks state of LPS
boolean next_arb[NPORT];    // arb state machine requests next symbol from fifo for local port
boolean next_even_request;
boolean next_odd_request;
boolean odd_isoch_phase;    // TRUE if the last fully completed isochronous interval began with
                            // CYCLE_START_EVEN.  (Upcoming or current interval will be ODD.)
BetaRequestCode own_req;
boolean packet_ending[NPORT]; // current arb state terminates the packet - used for FIFO control
int pending_port;           // port for pending request (NPORT if from the local (Beta) link)
PHY_state_type PHY_state; 
boolean proxy_root;         // TRUE if senior_border in the cloud containing root or root in a B-bus
boolean resolve_collision_request;  // set to make a Legacy ISO priority request to keep the 
                                    // bus busy and send a null packet
speedCode portRspeed[NPORT]; // most recently read speed code from the fifo for the port
int receive_port;

speedCode req_speed;        // Speed signaled by the link across the PHY/link interface
boolean send_async_start_token;
boolean sent_arb_reset;
boolean sent_async_start_token;
boolean senior_border;      // TRUE if this node is the senior border for the local B cloud

// shared between the main arb state machine, port.c and process_req.c
boolean immediate_phy_request;  // TRUE if immediate request from the PHY
boolean odd_async_phase;    // TRUE if last received arb reset was ARB_RESET_ODD


// shared between the main arb state machine, port.c, node.c and process_req.c
boolean immediate_restore_request;  // TRUE if immediate request to restore on the nephew
boolean isbr;               // Set when an arbitrated (short) bus reset should be attempted
boolean restore_request;    // TRUE if requested the bus in order to complete a port restore
#ifdef unsubscripted
boolean active;             // Indicates that the port is in state P2:Active 
                            // (set by state machine transitions to/from P2)
boolean Beta_mode;          // Set if the port has determined that it is operating in Beta mode, 
                            // unset otherwise 
                            // (i.e. when operating in DS-Mode, or when in P0:disconnected). 
                            // This is maintained as a read-only bit in the port register map.
ArbState portRarb;          // most recently read arb state from fifo for port
boolean restore;
#else
boolean active[NPORT];
boolean Beta_mode[NPORT+1]; // Beta_mode[NPORT] always false
ArbState portRarb[NPORT];
boolean restore[NPORT];
#endif

// shared between the main arb state machine, node.c and process_req.c
#ifdef unsubscripted
byte current_data;          // most recently read data from the fifo for the port
#else
byte current_data[NPORT];
#endif

breq_type breq;
linkType link;              // global status variable set by the PHY/Link interface
                            // note, this does not change during normal operation
                            // if this changes, then the PHY is "power reset"
                            // Note also that changes to LPS do NOT affect this variable
BetaRequestCode receive_req[NPORT]; //track latest requests received on each port


// shared between node.c and process_req.c
boolean loop_test_request;  // TRUE if requested the bus in order to test for loops

// shared between the main arb state machine, bport_rx.c, and process_req.c
#define FIFO_DEPTH 5        // depth of receive FIFO

// shared between the main arb state machine, bport_rx.c, dsport_rx.c and process_req.c
#ifdef unsubscripted
int fifo_rd_ptr;            // pointer to received character in fifo
int fifo_wr_ptr;            // only updated by the appropriate port
#else
int fifo_rd_ptr[NPORT];
int fifo_wr_ptr[NPORT];
#endif

// shared between bport_rx.c, dsport_rx.c and process_req.c
#ifdef unsubscripted
portSymbol portR[FIFO_DEPTH]; // symbol received by port machine
#else
portSymbol portR[NPORT][FIFO_DEPTH];
#endif

// shared between bport_tx.c, port.c and process_req.c

#ifdef unsubscripted
BetaRequestCode arbreqT;    // arbitration request to send
#else
BetaRequestCode arbreqT[NPORT];
#endif


// shared between all modules

boolean power_reset;        // TRUE during power reset, goes false at the end of power reset

#include "proto.h"          // last header, get the prototypes for C code checking purposes
