/*****************************************************************************
 * Copyright (c) 1988-95 Digi International Inc., All Rights Reserved
 * 
 * This  software  contains  proprietary  and  confidential   information   of
 * Digi International  Inc.   By  accepting  transfer  of this copy, Recipient
 * agrees to retain this software in  confidence,  to  prevent  disclosure  to
 * others,  and  to make no  use of this software other than that for which it
 * was delivered.  This is an unpublished copyright work  Digi   International
 * Inc.    Except   as   permitted   by  federal  law,  17 USC 117, copying is
 * strictly prohibited.
 * 
 * Digi  International  Inc.  CONFIDENTIAL  -   (Digi    International    Inc.
 * Confidential  when   combined   with   the   aggregated  modules  for  this
 * product) OBJECT CODE ONLY SOURCE MATERIALS
 * 
 * (C) COPYRIGHT Digi International Inc. 1995
 * 
 * All Rights Reserved
 * 
 * US   Government   Users   Restricted   Rights   -   Use,   duplication   or
 * disclosure  restricted by GSA ADP Schedule Contract with Digi International
 * Inc.
 * 
 *****************************************************************************/

/*******************************************************************
 ***  Debugger data definitions.
 ***
 *** @(#) $Id: dbdata.h,v 1.1 1997/09/15 21:10:38 gary Exp $
 ***
 *******************************************************************/

#if SYNC || SMART
#  define FIRST { \
	*(ushort *)0xa300000c = 0; \
	*(ushort *)0xa300000e = 0; \
	}

#  define HERE { \
	*(ulong *)0xa3000010     = __LINE__; \
	}

#  define BUMP(i)	((ushort *)0xa3000020)[i]++;

#  define RDR(i) ((char *)0xa3000040)[(*(ushort *)0xa300000c)++ & 0x3f] = (i);
#  define TDR(i) ((char *)0xa3000090)[(*(ushort *)0xa300000e)++ & 0x3f] = (i);
#else
#  define FIRST
#  define HERE
#define	BUMP(x)	db_count_array[(x)]++ ; 
#  define RDR(i)
#  define TDR(i)
#endif



/***********************************************************************
 ***  User configuration variables.
 ***
 ***  These are initialized to their corresponding configuration
 ***  variables in the data section.   These may be patched after
 ***  startup if desired, and the patched values will govern the
 ***  dynamic behavior of the debugger.
 ***********************************************************************/

extern int db_startrun;			/* See DB_STARTRUN */

extern int db_fakestop;			/* See DB_FAKESTOP */

extern int db_realtime;			/* See DB_REALTIME */

extern int db_docheck;			/* See DB_DOCHECK */



/**********************************************************************
 ***  Data to support checksumming text and data sections.
 **********************************************************************/

extern ulong _ftext;			/* Start of text section */
extern ulong etext;			/* End of text section */
extern ulong _fdata;			/* Start of data section */
extern ulong edata;			/* End of data section */

#ifdef DBMAINDATADECLARED
ulong db_tsum;				/* Equals sum2(text) */
ulong db_dsum;				/* Equals sum2(data) */
ulong db_hsum;				/* Makes sum1(text+data) == -1 */

ulong db_tsum_err;			/* Error in text checksum */
ulong db_dsum_err;			/* Error in data checksum */

ulong db_sum_text;			/* Dummy variable, when read
					   causes the debugger to checksum
					   the text area and return the
					   computed value. When written
					   recomputes the text checksum. */

ulong db_sum_data;			/* Dummy variable, when read
					   causes the debugger to checksum
					   the data area and return the
					   computed value.  When written
					   recomputes the data checksum. */
#else /* DBMAINDATADECLARED */

extern ulong db_tsum;				/* Equals sum2(text) */
extern ulong db_dsum;				/* Equals sum2(data) */
extern ulong db_hsum;				/* Makes sum1(text+data) == -1 */

extern ulong db_tsum_err;			/* Error in text checksum */
extern ulong db_dsum_err;			/* Error in data checksum */

extern ulong db_sum_text;			/* Dummy variable, when read
					   causes the debugger to checksum
					   the text area and return the
					   computed value. When written
					   recomputes the text checksum. */

extern ulong db_sum_data;			/* Dummy variable, when read
					   causes the debugger to checksum
					   the data area and return the
					   computed value.  When written
					   recomputes the data checksum. */





#endif /* DBMAINDATADECLARED */

/***********************************************************************
 ***  Send flags.
 ***********************************************************************/

#define SEND_STATUS	0x0001		/* Send program status */
#define SEND_ECHO	0x0002		/* Echo input character */
#define SEND_PC		0x0004		/* Send program counter */

#if DBX
#define SEND_DBX_BEGIN	0x0010		/* Send DBX begin message */
#define SEND_DBX_DATA	0x0020		/* Send DBX data */
#define SEND_DBX_ACK	0x0040		/* Send DBX acknowledge */
#endif

#if GDB
#define SEND_GDB_RESP	0x0100		/* Send GDB command response */
#define SEND_GDB_PRINT	0x0200		/* Send GDB print message */
#define SEND_GDB_STOP	0x0400		/* Send GDB stop message */

#define SEND_GDB_ACK	0x0800		/* Send GDB ACK */
#define SEND_GDB_NAK	0x1000		/* Send GDB NAK */
#endif


#ifdef DBMAINDATADECLARED

ulong db_send;				/* Send data */

int db_call_allow;			/* Call db_allow in db_printc */


/***********************************************************************
 ***  Global variables.
 ***********************************************************************/

int db_spin;				/* Total exception count */
int db_cause;				/* Polled value of cause register */

int db_char;				/* Last received input character */

int db_putback;				/* Next time db_getc() is called
					   return db_char again */

int db_echo;				/* Echo character */

int db_running;				/* Program is running */

int db_waitidle;			/* Do not return until all data out */

int db_rseq;				/* Receive sequence */
int db_rbad;				/* Receive seq bad counter */

int db_tcheck;				/* Transmit checksum */
int db_rcheck;				/* Receive checksum */

int db_tseq;				/* Transmit sequence */
int db_tbad;				/* Transmit seq bad counter */

int db_tpause;				/* After next character output,
					   pause transmission until the
					   output device interrupts us */

ulong db_inst;				/* Breakpoint instruction */
ulong *db_step;				/* Instruction address */

ulong *db_lstep;			/* Last Step target */
ulong *db_llstep;			/* Last Last Step target */

#define ACKWAIT 1234567			/* ACK retransmit delay */

COCALL db_co_receive;			/* Receive routine */
char   db_co_recv_guard_band[256] ;
COCALL db_co_transmit;			/* Transmit coroutine */
char   db_co_transmit_guard_band[256] ;
COCALL db_co_main;			/* Block from main routine */
char   db_co_main_guard_band[256];

uint8 db_rbuf[512];			/* Receive buffer */
uint8 db_mbuf[256];			/* Message buffer */
uint8 db_tbuf[256];			/* Transmit buffer */

USER  db_user;				/* User data */
int	db_count_array[8] ;

/************************************************************************
 ****	Debug print variables (currently supported only by GDB)
 ************************************************************************/

#if (GDB | DBX)
int db_print_in;			/* Buffer IN pointer */
int db_print_out;			/* Buffer OUT pointer */

int db_print_lost;			/* Buffer overflow occurred */

char db_print_buf[10000];		/* Print buffer */
#endif


/************************************************************************
 ****	DBX support variables.
 ************************************************************************/

#if DBX
int dbx_begin;				/* Respond to \r with XBEGIN message,
					   and halt after the first request
					   to resume the program unless
					   db_startrun is set */

int dbx_rlen;				/* Length of receive message */
int dbx_mlen;				/* Length of response message */
int dbx_tlen;				/* Length of transmit message */

int dbx_ackwait;			/* DBX acknowledge timer */

int dbx_run;				/* DBX is waiting for a response
					   to this type of run command */
#endif


/************************************************************************
 ****	GDB support variables.
 ************************************************************************/

#if GDB
int gdb_run;				/* GDB is waiting for a STOP msg */
int gdb_saved_run;			/* Above saved over restart */

int gdb_rtype;				/* GDB response type */

int gdb_ackwait;			/* GDB acknowledge timer */

ulong gdb_mem_start;			/* GDB mem read start */
int gdb_mem_count;			/* GDB mem read count */
#endif

#else /* DBMAINDATADECLARED */

extern ulong db_send;				/* Send data */

extern int db_call_allow;			/* Call db_allow in db_printc */


/***********************************************************************
 ***  Global variables.
 ***********************************************************************/

extern int db_spin;				/* Total exception count */
extern int db_cause;				/* Polled value of cause register */

extern int db_char;				/* Last received input character */

extern int db_putback;				/* Next time db_getc() is called
					   return db_char again */

extern int db_echo;				/* Echo character */

extern int db_running;				/* Program is running */

extern int db_waitidle;			/* Do not return until all data out */

extern int db_rseq;				/* Receive sequence */
extern int db_rbad;				/* Receive seq bad counter */

extern int db_tcheck;				/* Transmit checksum */
extern int db_rcheck;				/* Receive checksum */

extern int db_tseq;				/* Transmit sequence */
extern int db_tbad;				/* Transmit seq bad counter */

extern int db_tpause;				/* After next character output,
					   pause transmission until the
					   output device interrupts us */

extern ulong db_inst;				/* Breakpoint instruction */
extern ulong *db_step;				/* Instruction address */

extern ulong *db_lstep;			/* Last Step target */
extern ulong *db_llstep;			/* Last Last Step target */

#define ACKWAIT 1234567			/* ACK retransmit delay */

extern COCALL db_co_receive;			/* Receive routine */
extern char   db_co_recv_guard_band[256] ;
extern COCALL db_co_transmit;			/* Transmit coroutine */
extern char   db_co_transmit_guard_band[256] ;
extern COCALL db_co_main;			/* Block from main routine */
extern char   db_co_main_guard_band[256];

extern uint8 db_rbuf[512];			/* Receive buffer */
extern uint8 db_mbuf[256];			/* Message buffer */
extern uint8 db_tbuf[256];			/* Transmit buffer */

extern USER  db_user;				/* User data */
/************************************************************************
 ****	Debug print variables (currently supported only by GDB)
 ************************************************************************/

#if (GDB | DBX)
extern int db_print_in;			/* Buffer IN pointer */
extern int db_print_out;			/* Buffer OUT pointer */

extern int db_print_lost;			/* Buffer overflow occurred */

extern char db_print_buf[10000];		/* Print buffer */
#endif


/************************************************************************
 ****	DBX support variables.
 ************************************************************************/

#if DBX
int dbx_begin;				/* Respond to \r with XBEGIN message,
					   and halt after the first request
					   to resume the program unless
					   db_startrun is set */

int dbx_rlen;				/* Length of receive message */
int dbx_mlen;				/* Length of response message */
int dbx_tlen;				/* Length of transmit message */

int dbx_ackwait;			/* DBX acknowledge timer */

int dbx_run;				/* DBX is waiting for a response
					   to this type of run command */
#endif


/************************************************************************
 ****	GDB support variables.
 ************************************************************************/

#if GDB

extern int gdb_run;				/* GDB is waiting for a STOP msg */
extern int gdb_saved_run;			/* Above saved over restart */

extern int gdb_rtype;				/* GDB response type */

extern int gdb_ackwait;			/* GDB acknowledge timer */

extern ulong gdb_mem_start;			/* GDB mem read start */
extern int gdb_mem_count;			/* GDB mem read count */

#endif

#endif /* DBMAINDATADECLARED */

/************************************************************************
 ****	Inline routines to inhibit/enable interrupts.
 ************************************************************************/
#if 0
#define inhibit(x) \
    ({  int ss; \
	asm("	.set	noreorder\n \
                mfc0	$24,$12\n \
                not	$25,%1\n \
	        and	%0,$24,0xff01\n \
	        and	$25,%0\n \
	        xor	$24,$25\n \
	        mtc0	$24,$12\n \
                .set	reorder" \
	    : "=r" (ss) : "r" (x) : "$24", "$25"); \
	ss; })

#define enable(x) \
        asm("	.set	noreorder\n \
                mfc0    $24,$12\n \
		and	$25,%0,0xff01\n\
                xor	$25,$24\n \
		and	$24,0xff01\n \
                xor     $25,$24\n \
		mtc0	$25,$12\n \
                .set	reorder" \
	    : : "r" (x) : "$24", "$25");

#endif
/************************************************************************
 ***	Forward declarations.
 ************************************************************************/

extern void	start(void);

extern void	db_allow(void);
extern ulong	db_check(ulong *s, ulong *e);
extern void	db_coinit(COCALL*, void (*)(), void*);
extern void	db_corestart(COCALL *);
extern void	db_coresume(COCALL*, COCALL*);
extern	int 	db_getc(void);
extern ulong	db_getcause(void);
extern ulong	db_getstatus(void);
extern void	db_init(void);
extern void	db_inval(void*);
extern void	db_main(void);
extern void	db_printc(int c);
extern void	db_printstr(char *str);
extern void	db_putc(int ch);
extern void	db_putc(int ch);
extern void	db_putnum(ulong n);
extern void	db_reboot(void);
extern void	db_receive(void);
extern void	db_restart(void);
extern void	db_send_var(char *str, ulong v);
extern void	db_stoprun(int);
extern ulong	db_target(void);
extern void	db_transmit(void);
extern int	db_uart_busy(void);
extern void	db_uart_init(void);
extern void	db_uart_poll(void);

extern void	dbx_command(void);
extern uint8   *dbx_fmtnum(uint8 *dp, ulong num);
extern int	dbx_get6(void);
extern int	dbx_getc(void);
extern void	dbx_getpak(void);
extern uint8   *dbx_grabnum(uint8 *s, ulong *num);
extern void	dbx_put6(int ch);
extern void	dbx_putc(int ch);
extern void	dbx_putpak(int ack);
extern void	dbx_status(int cmd, int type);

extern void	gdb_command(void);
extern void	gdb_getcmd(void);
extern void	gdb_sendresp(void);
extern void	gdb_sendstop(void);
extern void	gdb_sendprint(void);
