#define AO_EN   1
#define AI_EN   1
#define CJC_EN  1
#define AI_SELF_CALI    1
#define FLOAT   1


#ifdef __TURBOC__
#include <alloc.h>

  void far interrupt     (*oldcom1_intr)(void);
  void far interrupt     (*oldcom2_intr)(void);

#else
#include <malloc.h>
  #define MK_FP(seg,ofs)  ((void far *) \
                           (((unsigned long)(seg) << 16) | (unsigned)(ofs)))
  #define enable()                _enable()
  #define disable()               _disable()
  #define outportb                outp
  #define inportb                inp
  #define inport                inpw
  #define outport                 outpw
  #define setvect                _dos_setvect
  #define getvect                _dos_getvect
  void ( interrupt far  * oldcom1_intr)(void);
  void ( interrupt far  * oldcom2_intr)(void);
#endif
#include <dos.h>
#include <stdio.h>

extern unsigned      gEepData;
extern unsigned char port[4*8];   /* direct I/O port define */
extern unsigned      ADDRESS[4];
extern unsigned char gBrd[5]; /* keep every brd brd type */
extern unsigned char gRange[4*4]; /*keep every brd AI(0 ~2b) or AO(30~32) , DIO(40h) range */
extern unsigned char       gChMask[4]; /*keep AI or AO channel mask */
extern unsigned char mask[8];
extern unsigned int  maskw[16];


/*---------- rsdrv.c -------------*/

void far interrupt com1_intr(void);
void far interrupt com2_intr(void);


int  comm_init(int buf_size);
int  comm_exit();
int  comm_open(unsigned char port, unsigned long baud,int parity, int data, int stop, int cmd_type);
int comm_get_rec_datas(unsigned *length, unsigned char ** data, unsigned char port);
int comm_get_rec_str(unsigned char ** data, unsigned char port);
int  comm_send(unsigned char send_c, unsigned char port);





/*---------- eeprdv.c -------------*/
#define AI_POS_EEPDI    1
#define AI_BIT_EEPDI    7
#define AO_POS_EEP      3
#define AO_POS_EEPDI    1
#define AO_BIT_EEPDI    7

    /*             eeprom io driver           **/
#define IO_BIT_EEP_CS   7   /* port #3 bit 7 */
#define IO_BIT_EEP_SK   6
#define IO_BIT_EEP_DO   5
#define AI_POS_EEP      2

#define EEP_AI_POS_VALID        1
#define EEP_AI_POS_RANGE        2  /* Hz(7),format(1,0), range */
#define EEP_AI_POS_CH           3
#define EEP_AI_POS_50HZ_ZERO    4
#define EEP_AI_POS_50HZ_SPAN    5
#define EEP_AI_POS_60HZ_ZERO    6
#define EEP_AI_POS_60HZ_SPAN    7
#define EEP_START_50HZ_POS_SYS_ZERO    8
#define EEP_AI_VALID            0x55b1
#define EEP_START_60HZ_POS_SYS_ZERO    36


void eepRd(int  address,int bidx);
int  eepWr(int  addr,int brd);

/************ ai io driver **********************************************/

#define AI_POS_ADI      1
#define AI_POS_MUX      2  /* bit0~2*/
#define AI_POS_ADO      3

#define AI_BIT_RFS          0
#define AI_BIT_TFS          1
#define AI_BIT_A0           2
#define AI_BIT_SK           3
#define AI_BIT_SDO          4
#define AI_BIT_ADI          6




#define BRD_TYPE_OFF    1       /* every I/O board address for read brd type */


/*-------- aodrv.c function declaration ------------*/
#define  AO_0_20MA      0x30
#define  AO_4_20MA      0x31
#define  AO_10V         0x32

#define  AO_POS_SYNC         3
#define  AO_POS_CLKH         6
#define  AO_POS_CLKL         7
#define  AO_BIT_DO           4
#define  AO_POS_CLRH         0xf

#define EEP_AO_POS_RANGE        2    /* Hz(7),format(1,0), range */
#define EEP_AO_CH1_RANGE        8

extern unsigned      gAoCnt[4*4];

#if FLOAT ==1
int  ao_set(unsigned char slot, unsigned char ch, float ao_data);
#else
int  ao_set(unsigned char slot, unsigned char ch, long ao_data);
#endif
int  ao_init(unsigned char slot, unsigned char ch);


/************ dio driver, diodrv.c **********************************************/
#define  DIO_POS_DI         2
#define  DIO_POS_DO         3
#define  DI_POS_L           2
#define  DI_POS_H           3
#define  DO_POS_L           2
#define  DO_POS_H           3


/*-------- io.c function declaration ------------*/
#define BRD_5017        0x04 /*high V AI */
#define BRD_5018        0x05  /*T/C */
#define BRD_5024        0x18 /*4ch AO*/
#define BRD_5050        0x10 /*8DI 8 DO*/
#define BRD_5051        0x11 /*16 DI */
#define BRD_5056        0x12 /*16 DO */
#define BRD_5060        0x14 /*6ch relay*/
#define BRD_5052        0x15
#define BRD_NO_EXIST    0xff
#if PC ==1
  #define SLOT_1_TYPE   BRD_5018
  #define SLOT_2_TYPE   BRD_5024
  #define SLOT_3_TYPE   BRD_5051
  #define SLOT_4_TYPE   BRD_5056
#endif

    int io_init();
    int io_read_slot_type(unsigned char slot, unsigned char *type);
    int io_update_data();
    int io_set_do(unsigned char slot, unsigned Do);
    int io_get_data_all(unsigned char *data);
    int io_get_data_single_slot(unsigned char slot,unsigned char *data);
    int io_get_ai_ch_mask(unsigned char slot, unsigned char *mask);
  #if FLOAT ==1
    int io_set_ao(unsigned char slot, unsigned char ch, float ao_data);
  #else
    int io_set_ao(unsigned char slot, unsigned char ch, long ao_data);
  #endif

void led_init();
void led1(int type);
void led2(int type);
void led3(int type);
void led4(int type);
wdt_enable();
wdt_clear();
wdt_disable();
