#ifndef VARS_H
#define VARS_H


typedef unsigned long dword;
typedef unsigned short word;
typedef unsigned char byte;

#define MAXPORTS 1
#define MAXDIGITS 30
#define MAXREGS 60
#define PATHLEN 80
#define MAXFUNC 0x1b
#define MSGLEN 180
#define MAXINFO 10



/* === compiler stuff */

#ifdef __ZTC__
#define _FAR _far
#define _PASCAL _pascal
#else
#define _FAR far
#define _PASCAL pascal
#endif

#ifdef __TURBOC__
void __emit__();
#endif
#ifdef __ZTC__
#define __emit__(a) asm(a)
#endif

#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif

#ifdef disable
#undef disable
#endif
#ifdef _MSC_VER
#define disable() _disable ()
#else
#define disable() __emit__( (char)(0xfa) )
#endif

#ifdef enable
#undef enable
#endif
#define enable() { if (!_in_event) { check_cli (); _sti (); } }
#ifdef _MSC_VER
#define _sti() _enable ()
#else
#define _sti()  __emit__( (char)(0xfb) )
#endif

#ifndef MK_FP
#ifdef __TURBOC__
#define MK_FP(seg,ofs)  ((void _seg *)(seg) + (void near *)(ofs))
#else
#define MK_FP(seg, ofs)  (void _FAR *) (((((unsigned long) seg) << 16) & 0xffff0000UL) | ((unsigned long) ofs & 0xffffUL))
#endif
#endif

#ifndef FP_SEG
// #ifdef _MSC_VER
// #define FP_SEG(fp)      (*((unsigned _FAR *)&(fp)+1))
// #else
#define FP_SEG(fp)      ((unsigned)((unsigned long)(fp) >> 16))
// #endif
#endif

#ifndef FP_OFF
// #ifdef _MSC_VER
// #define FP_OFF(fp)      (*((unsigned _FAR *)&(fp)))
// #else
#define FP_OFF(fp)      ((unsigned)(fp))
// #endif
#endif

#ifndef FALSE
#define FALSE 0
#define TRUE (~FALSE)
#endif

#ifndef NULL
#define NULL ((void *) 0)
#endif



/* --- NDEBUG stuff */

#ifdef NDEBUG
#define CHECK_CLI()
#define DUMP()

#else
#define CHECK_CLI() check_cli()
#define DUMP() dump (msgbuf, msgword (0))
#endif



/* --- statstr offsets */

/*
cFos>  C-B3  ACOD  R:0000  T:0000  C:00000  V110 19200 8N1  02289111041
cFos>  C-B3  ACOD  0R:0000  0T:0000  C:000  X75 ISO8208  B  02289111041
*/

#define STAT_STATE      7
#define STAT_LED_A      13
#define STAT_LED_C      14
#define STAT_LED_O      15
#define STAT_LED_D      16
#define STAT_RX         22
#define STAT_TX         31
#define STAT_CHARGE     37
#define STAT_PROT       44
#define STAT_BUNDLE     57
#define STAT_NB         60



/* --- misc stuff */

#define SH_COMPAT       0x00
#define SH_DENYRW       0x10
#define SH_DENYWR       0x20
#define SH_DENYRD       0x30
#define SH_DENYNO       0x40

#define SEEK_SET        0       /* seek from start of file      */
#define SEEK_CUR        1       /* relative to current position */
#define SEEK_END        2       /* relative to end of file      */

struct WORDREGS { unsigned ax,bx,cx,dx,si,di,cflag,flags; };
struct BYTEREGS {unsigned char al,ah,bl,bh,cl,ch,dl,dh; };
union REGS { struct WORDREGS x; struct BYTEREGS h; };
struct SREGS { unsigned es,cs,ss,ds; };

void c_timer (void);
char * _FAR _PASCAL memcpy3 (void _FAR *, void _FAR *, word);
void _FAR * capi_event_entry (void);
word get_flags (void);
word mycli (void);
word mysti (void);
void myseti (word);
word _FAR myint86x (word, union REGS _FAR *, union REGS _FAR *, struct SREGS _FAR *);
word _FAR mycallf (void _FAR *, union REGS _FAR *, union REGS _FAR *, struct SREGS _FAR *);
void _FAR mysegread (struct SREGS _FAR *segregs);
void reboot (word);
void debug_funcs (word);
void check_cli (void);
void _FAR int08_entry ();
void _FAR int14_entry ();
void _FAR old_int08 ();                 /* to force C compiler to accept any segment */
void _FAR old_int14 ();
void _FAR ptr2segs ();



/* --- c.asm stuff */

extern byte _osmajor;
extern byte _osminor;
extern unsigned _psp;
extern char _FAR *_indos;
extern byte _FAR resident_end;
extern word _next_stack;
extern word _stackbottom;
extern word _stack_size, _stack_num;
extern byte _vsnnum;
extern word aenvseg;
extern char ascii_id[];
extern word numticks;
extern dword _timerval;



/* --- hayes.c stuff */

#define SREG_X1 9               /* first X bitmapped register */

#define HAYES_OK                0
#define HAYES_CONNECT           1
#define HAYES_RING              2
#define HAYES_NO_CARRIER        3
#define HAYES_ERROR             4
#define HAYES_NO_DIAL_TONE      6
#define HAYES_BUSY              7
#define HAYES_NO_ANSWER         8
#define HAYES_RINGING           11
#define HAYES_VOICE             12
#define HAYES_CONNECT_9600      13

#define HAYES_X75               1               /* CONNECT */
#define HAYES_V110              2

#define HAYES_B2_ERR            1               /* ERROR */
#define HAYES_B3_ERR            2


typedef struct {
   byte x;
   byte spv;
   byte echo;
   byte hangup;
   byte quiet;
   byte verbal;
   byte debug;
   byte connecting;
   byte eaz;
} MODEM_SETTINGS;

extern MODEM_SETTINGS hayes[MAXPORTS];
extern word sreg[MAXPORTS][MAXREGS];
extern char dialnumber[MAXPORTS][MAXDIGITS+1];
extern byte diallen[MAXPORTS];
extern char profile[PATHLEN];
extern word sreg_bounds [MAXREGS][2];

void hayes_init_port (short i);
void hayes_init (void);
void trans_to_hayes (unsigned short cport, unsigned char c);
void disconnect_wait (word cport);
void hayes_result (word cport, int number, ...);
void disp_stat (int cport);
int hayes_read_profile (int cport, int i);



/* --- disp.c stuff */

#define _COMP_IBMPC     1
#define _COMP_PEOPLE    2

#define _DISP_DIRIO     1
#define _DISP_BIOS      2
#define _DISP_ANSI      4

#define DISP_MAX_ATB 16

void disp_init (void);
void _disp_char (char c);
// void disp (char *text);
void mygotoxy (short x, short y);
void _set_atb (short atb);
void set_atb (short atb);
void update_cursor (void);



/* --- printf.c stuff */

char *_ultoa (unsigned long value, char *string, int radix);
int _output (int (*)(int, int), int, char *, const char *, char *);
int myprintf (const char *format, ...);
int printf (const char *format, ...);
int mysprintf (char *dest, const char *format, ...);
void myputchar (int c);



/* --- debug.c stuff */

// extern short disp_alternate;

int adisp_init (void);
void _adisp_char (char c);
void myagotoxy (short x, short y);
// void aset_atb (short atb);
int myaprintf (const char *format, ...);
void adisp (char *text);



/* --- fossil.c stuff */

extern int errno;
extern char _FAR *exename;
extern word bios_emul;

extern byte portnum [MAXPORTS];
extern byte offhook[MAXPORTS];
extern byte initialized [MAXPORTS];
extern byte ctrl_k_check [MAXPORTS];
extern byte ctrl_k_seen [MAXPORTS];
extern byte carrier_detect [MAXPORTS];
extern byte command_mode [MAXPORTS];
extern byte dtr_high [MAXPORTS];
extern word pendcnt [MAXPORTS];
extern word pendrem [MAXPORTS];
extern byte watchdog [MAXPORTS];

extern unsigned short rxbufsize [MAXPORTS];
extern unsigned char _FAR * rxbuf [MAXPORTS];
extern unsigned short rxrem [MAXPORTS];
extern unsigned short rxcnt [MAXPORTS];
extern byte rx_overflow [MAXPORTS];

extern unsigned short txbufsize [MAXPORTS];
extern unsigned char _FAR * txbuf [MAXPORTS];
extern unsigned short txrem [MAXPORTS];
extern unsigned short txcnt [MAXPORTS];
extern byte tx_enabled [MAXPORTS];

void stuff_char (unsigned char c, unsigned short cport);
int fossil_init (void);
void purge_txbuf (unsigned short cport);



/* --- cstates.c stuff */

extern long timediff;
extern int in_event;
extern word cstate [MAXPORTS];
extern word ncci [MAXPORTS];
extern word plci [MAXPORTS];
extern int  disco [MAXPORTS];
extern byte b3number [MAXPORTS];
extern byte msgbits [256];
extern byte callerid [MAXPORTS][MSGLEN+1];
extern char *statstr [MAXPORTS];
extern int  disptimer [MAXPORTS];
extern word numofconnections;
extern word disconnectinfo [MAXPORTS];
extern word disconnectb3info [MAXPORTS];
extern byte eaz [MAXPORTS];
extern byte rservice [MAXPORTS];
extern byte rserviceadd [MAXPORTS];
extern byte dreason [MAXPORTS];
extern word infocnt [MAXPORTS];
extern byte infoelement [MAXINFO] [MAXPORTS] [MSGLEN];
extern long charge [MAXPORTS];
extern dword rxbytes [MAXPORTS], rxblocks [MAXPORTS];
extern dword txbytes [MAXPORTS], txblocks [MAXPORTS];
extern word transcnt [MAXPORTS];
extern word transrem [MAXPORTS];
extern int  txtimer [MAXPORTS];
extern int  rxtimer [MAXPORTS];
extern word bytesintrans [MAXPORTS];
extern int fast_event;
extern byte sb2prot [MAXPORTS], sservice [MAXPORTS], sservadd [MAXPORTS];

void initstates (void);
void trig_transmitter (word cport);
int connect (word cport, int spv);
void disconnect (word cport, byte reason);
void c_states (void);
void init_cstates (void);
void dump (byte *buf, int len);
void checkpending (word cport, int ispurge);



/* --- api.c stuff */

void api_putmsg (void);
word api_getmsg (void);
char *capi_cause (word number);
int api_installed (void);
int api_register (void);
word api_release (void);
word api_set_signal (void _FAR *ptr);
char *api_getinfos (int number);
void api_reregister (void);



/* --- capi.c stuff */

#define capi_get_b3_params_req(ncci) simplemessage (0x85, ncci)
#define capi_get_param_req(plci) simplemessage (0x06, plci)

extern word _FAR applid;
extern byte smsgbuf[2][MSGLEN];
extern byte msgbuf[MSGLEN+1];
extern word smsgnr;

void simplemessage (word cmdsub, word xdata);
void capi_listen_b3_req (word plci);
void capi_select_b2_protocol_req (word cport, byte prot, byte usrrate);
void capi_select_b3_protocol_req (word cport, word plci);
void capi_connect_b3_req (word cport, word plci);
void capi_disconnect_b3_req (word cport, word ncci);
void capi_disconnect_req (word plci, byte reason);
void capi_data_b3_req (word ncci, word cnt, void _FAR * p, byte num, word flags);
void capi_listen_req (void);
void capi_connect_req (word cport, int spv);
// void capi_connect_info_req (word cport, word plci);
void capi_resp (word cport, word msgcmd);
void capi_ans (word plci, byte val);
void capi_info_req (word plci);
void capi_data_b3_resp (word cport);



/* --- myutil.c stuff */

word myint86 (word intno, union REGS _FAR *inregs, union REGS _FAR *outregs);
word myintdosx (union REGS _FAR *inregs, union REGS _FAR *outregs, struct SREGS _FAR *sregs);
word myintdos (union REGS _FAR *inregs, union REGS _FAR *outregs);
word mybdos (word dosfn, word dosdx, word dosal);
char *get_currdir (char *drive);
int myread (int handle, void _FAR * p, word len);
int mywrite (int handle, void _FAR * p, word len);
long mylseek (int handle, long offset, int from);
int myclose (int handle);
int myopen (char _FAR * path, int mode);
int toupper (int c);
int isdigit (int c);
int mystrnicmp (char _FAR *s1, char _FAR *s2, int len);
dword unixdate (word year, word mon, word day, word hour, word min, word sec);
dword dosdate (void);
void set_dosdate (dword date);
int kbhit (void);
int getch (void);
int dos_free (void _FAR * vp);
void _FAR * dos_malloc (long bytes);



/* --- main.c stuff */

#define MAN_TELES       1

extern word capi_int;
extern word capi_maxmess;
extern word capi_maxb3;
extern word capi_maxwin;
extern word capi_framelen;
extern word fos_rxsize;
extern word fos_txsize;
extern word segadr[MAXPORTS*2+1+1+1], segadrcnt;
extern void _FAR *api_memptr;
extern int is_tranx;
extern int manufact, is_bundle, is_v110, use_mpx, use_xms;



#endif

/* --- eof vars.h */
