/***************************************************************************
**
** File name : sub86.c
**
**
**
** Changing :
**
** A. Date -- 10/22/1992 By Cheerson
**
**    0. Received from Matthew as the initial version.
**    1. Gather and sort externals/includes/local definitions ..etc.
**    2. Program source code alignment, follow the "coding standard".
**
**
**
**    Copyright (C) 1992 Microtek International, Inc.
**    All Rights Reserved
**
****************************************************************************/

/***************************************************************************
**

**    Include files
**
***************************************************************************/

#include <conio.h>
#include <time.h>

#ifndef _COMM_
#include "comm.h"
#endif

#ifndef _ABI_DEF_
#include "abidef.h"
#endif

#ifndef _REG86_DEFINE_
#include "reg86.h"
#endif

#ifndef _ABI_GLOBAL_
#include "abiglbs.h"
#endif

#include <string.h>

extern U8 BASE_TIME;
/**************************************************************************
**
** Local define
**
***************************************************************************/

/**************************************************************************
**
** Execution codes
**
**************************************************************************/

/**************************************************************************
**
** Name : emuDiagnostic()
**
** Function :
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/
RETCODE EXPORT iceDiagnostic(VOID) {
int lp=0;
RETCODE status;

   SaveId(DIAGNOSTIC,lp);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME*3);
   if (status == ICE_REC_TIME_OUT) return(status);
   return(*((U16 *)&inputStream[0]));
}

/**************************************************************************
**
** Name : emuDiagnostic()
**
** Function :
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/
RETCODE EXPORT DiagnosticFunction(DIAG_ID diagID, U32 *retData,U8 *retID) {
int lp=0;
RETCODE status;

   SaveId(diagID,lp);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME*3);
   if (status == ICE_REC_TIME_OUT) return(status);
   status = *((U16 *)&inputStream[0]);
   if (status != ICE_OK) {
      if ((diagID >= TEST_BC_RDATA_BUS) && (diagID <= TEST_SEQ_RAM)) {
         _fmemcpy(retData,&inputStream[3],inputStream[2]);
         _fmemcpy(retID,&inputStream[8],inputStream[7]);
      } else {
         switch (diagID) {
         case MCE_MEMORY_TEST :
         case INITIAL_TEST :
            _fmemcpy(retData,&inputStream[3],inputStream[2]);
            break;
         case MCE_CONNECT_TEST :
            _fmemcpy(&(errorMceConnTest.ConnectorError),&inputStream[3],2);
            _fmemcpy(&(errorMceConnTest.Working[0]),&inputStream[5],sizeof(ERROR_MCE_WORK_TEST)*48);
            SendStream(outputStream,lp);
            status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME*3);
            _fmemcpy(&(errorMceConnTest.Working[48]),&inputStream[3],sizeof(ERROR_MCE_WORK_TEST)*48);
            break;
         }
      }
   }
   return(status);

}

/**************************************************************************
**
** Name : emuSelfTest()
**
** Function :
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/
S16 EXPORT iceSelfTest(U16 cmdId) {
RETCODE status;
int lp=0;
U16 varWidth;

   SaveId(FUNCTION_TEST,lp);

   varWidth = sizeof(cmdId);
   SaveVar(cmdId,lp,varWidth);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME*3);
   if (status == ICE_REC_TIME_OUT) return(status);
   return(*((S16 *)&inputStream[0]));
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/
RETCODE EXPORT iceSetTimerReg(U16 regNo) {
RETCODE status;
int lp=0;
U16 varWidth;

   SaveId(SET_TIMER_REG,lp);

   varWidth = sizeof(regNo);
   SaveVar(regNo,lp,varWidth);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   return(*((U16 *)&inputStream[0]));
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/

RETCODE EXPORT iceSetAccessSize(U16 sizeFlag) {
RETCODE status;
int lp=0;
U16 varWidth;

   SaveId(SET_SIZE,lp);

   varWidth = sizeof(sizeFlag);
   SaveVar(sizeFlag,lp,varWidth);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   return(*((U16 *)&inputStream[0]));
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/

RETCODE EXPORT iceGetAccessSize(U16 *sizeFlag) {
RETCODE status;
int lp=0;

   SaveId(GET_SIZE,lp);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   status = *((U16 *)&inputStream[0]);
   if (status == ICE_OK) *sizeFlag = inputStream[3];
   return(status);
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/
RETCODE EXPORT iceGetStatus(VOID) {
RETCODE status;
int lp;

   lp = 0;
   SaveId(GET_STAT,lp);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   return(*((U16 *)&inputStream[0]));
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/

RETCODE EXPORT iceSetReady(U16 readyFlag) {
RETCODE status;
int lp=0;
U16 varWidth;

   SaveId(SET_READY,lp);

   varWidth = sizeof(readyFlag);
   SaveVar(readyFlag,lp,varWidth);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   return(*((U16 *)&inputStream[0]));
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/

RETCODE EXPORT iceGetReady(U16 *readyFlag) {
RETCODE status;
int lp=0;

   SaveId(GET_READY,lp);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   status = *((U16 *)&inputStream[0]);
   if (status == ICE_OK) *readyFlag = inputStream[3];
   return(status);
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/

RETCODE EXPORT iceSetControl(U16 signals) {
RETCODE status;
int lp=0;
U16 varWidth;

   SaveId(SET_CONTROL,lp);

   varWidth = sizeof(signals);
   SaveVar(signals,lp,varWidth);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   return(*((U16 *)&inputStream[0]));
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/

RETCODE EXPORT iceGetControl(U16 *signals) {
int lp=0;
RETCODE status;

   SaveId(GET_CONTROL,lp);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   status = *((U16 *)&inputStream[0]);
   if (status == ICE_OK) _fmemcpy(signals,&inputStream[3],inputStream[2]);
   return(status);
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/
RETCODE EXPORT iceGetMode(U16 *modeFlag) {
int lp;
RETCODE status;

   lp = 0;
   SaveId(GET_MODE,lp);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   status = *((U16 *)&inputStream[0]);
   if (status == ICE_OK) *modeFlag = inputStream[3];
   return(status);
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/

RETCODE EXPORT iceSetSync(U16 syncFlag) {
RETCODE status;
int lp=0;
U16 varWidth;

   SaveId(SET_SYNC,lp);

   varWidth = sizeof(syncFlag);
   SaveVar(syncFlag,lp,varWidth);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   return(*((U16 *)&inputStream[0]));
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/
RETCODE EXPORT iceGetSync(U16 *sync) {
int lp=0;
RETCODE status;

   SaveId(GET_SYNC,lp);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   status = *((U16 *)&inputStream[0]);
   if (status == ICE_OK) *sync = inputStream[3];
   return(status);
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/
RETCODE EXPORT iceSetVerify(U16 verifyFlag) {
RETCODE status;
int lp=0;
U16 varWidth;

   SaveId(SET_VERIFY,lp);

   varWidth = sizeof(verifyFlag);
   SaveVar(verifyFlag,lp,varWidth);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   return(*((U16 *)&inputStream[0]));
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/
RETCODE EXPORT iceGetVerify(U16 *verifyFlag) {
int lp=0;
RETCODE status;

   SaveId(GET_VERIFY,lp);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   status = *((U16 *)&inputStream[0]);
   if (status == ICE_OK) *verifyFlag = inputStream[3];
   return(status);
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/
RETCODE EXPORT iceSetWait(U16 count) {
RETCODE status;
int lp=0;
U16 varWidth;

   SaveId(SET_WAIT,lp);

   varWidth = sizeof(count);
   SaveVar(count,lp,varWidth);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   return(*((U16 *)&inputStream[0]));
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/
RETCODE EXPORT iceGetWait(U16 *count) {
int lp=0;
RETCODE status;

   SaveId(GET_WAIT,lp);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   status = *((U16 *)&inputStream[0]);
   if (status == ICE_OK) *count = inputStream[3];
   return(status);
}


/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/
RETCODE EXPORT iceSetID(U16 idFlag) {
RETCODE status;
int lp=0;
U16 varWidth;

   SaveId(SET_ID,lp);

   varWidth = sizeof(idFlag);
   SaveVar(idFlag,lp,varWidth);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
//   if (status == ICE_REC_TIME_OUT) return(status);
   return(*(U16 *)&inputStream[0]);
}

/**************************************************************************
**
** Name :
**
** Function
**
**    Input  :
**
**    Output :
**
** Notes:
**
**************************************************************************/

RETCODE EXPORT iceGetID(U8 *ID_Info) {
int lp=0;
RETCODE status;

   SaveId(GET_ID,lp);

   outputStream[lp++] = 0;

   SendStream(outputStream,lp);
   status = ReceiveStream(inputStream,&inputStreamLen,BASE_TIME);
   if (status == ICE_REC_TIME_OUT) return(status);
   status = *((U16 *)&inputStream[0]);
   if (status == ICE_OK) _fmemcpy(ID_Info,&inputStream[3],inputStream[2]);  // kkk
   return(status);
}
/**************************** End of File **********************************/
