
/***************************************************************************
**
**    $Header:   D:/ECB2S/SRC/LOG/XINIT.CPP   1.2.1.4   17 Apr 1997 16:09:50   ZJRD  $
**
**    $Log:   D:/ECB2S/SRC/LOG/XINIT.CPP  $
** 
**    Rev 1.2.1.4   17 Apr 1997 16:09:50   ZJRD
** No change.
** 
**    Rev 1.2.1.3   16 Apr 1997 10:45:50   ZJRD
** No change.
** 
**    Rev 1.2.1.2   10 Apr 1997 15:49:40   ZJRD
** 2.09F
** 
**    Rev 1.2.1.1   03 Apr 1997 15:28:42   ZJRD
** No change.
** 
**    Rev 1.2.1.0   28 Mar 1997 10:32:02   ZJRD
** easy pack sld 2.09d
** 
**    Rev 1.1   19 Mar 1997 11:41:10   ZJRD
** No change.
** 
**    Rev 1.0   12 Mar 1997 14:53:56   ZJRD
** Initial revision.
** 
**    Rev 1.7.1.0.1.2   09 Dec 1996 10:17:04   ZJRD
** EasyPack/SLD Version 2.0P
** 
**    Rev 1.7.1.0.1.0   11 Nov 1996 13:00:20   ZJRD
** EasyPack/SLD Version 2.01
** 
**    Rev 1.7.1.4   09 Sep 1996 13:20:52   ZJRD
** No change.
** 
**    Rev 1.7.1.3   05 Sep 1996 11:32:22   ZJRD
** EasyPack/SLD Version 1.9e
** 
**    Rev 1.7.1.2   02 Sep 1996 09:59:26   ZJRD
** No change.
** 
**    Rev 1.7.1.1   28 Aug 1996 15:46:22   ZJRD
** EasyPack/SLD Version 1.9b
** 
**    Rev 1.7.1.0   12 Aug 1996 10:51:18   ZJRD
** EasyPack/SLD Version 1.98
** 
**    Rev 1.5   05 Jun 1996 14:53:22   ZJRD
** EasyPack/SLD Version 1.96
** 
**    Rev 1.4   29 May 1996 09:32:02   ZJRD
** No change.
** 
**    Rev 1.3   16 May 1996 09:03:14   ZJRD
** No change.
** 
**    Rev 1.2   10 May 1996 09:12:04   ZJRD
** EasyPack/SLD Version 1.93
** 
**    Rev 1.1   02 May 1996 10:27:08   ZJRD
** EasyPack/SLD Version 1.92
** 
**    Rev 1.31   18 Apr 1996 13:00:48   Shirley
** No change.
** 
**    Rev 1.30   12 Apr 1996 10:37:52   Shirley
** EasyPack/SLD Version 1.90
** 
**    Rev 1.28   15 Feb 1996 08:52:24   Shirley
** No change.
** 
**    Rev 1.27   12 Feb 1996 14:06:56   Shirley
** No change.
** 
**    Rev 1.26   06 Feb 1996 15:33:24   Shirley
** No change.
** 
**    Rev 1.25   06 Feb 1996 13:48:18   Shirley
** No change.
** 
**    Rev 1.24   01 Feb 1996 10:12:40   Shirley
** No change.
** 
**    Rev 1.23   26 Jan 1996 09:12:22   Shirley
** No change.
** 
**    Rev 1.22   25 Jan 1996 13:13:04   Shirley
** No change.
** 
**    Rev 1.21   24 Jan 1996 10:36:40   Shirley
** No change.
** 
**    Rev 1.20   23 Jan 1996 11:27:10   Shirley
** No change.
** 
**    Rev 1.19   18 Jan 1996 10:10:26   Shirley
** No change.
** 
**    Rev 1.18   15 Jan 1996 16:11:28   Shirley
** No change.
** 
**    Rev 1.17   04 Jan 1996 11:12:26   Shirley
** EasyPack/SLD Version 0.34
** 
**    Rev 1.16   30 Nov 1995 09:11:40   Shirley
** No change.
** 
**    Rev 1.15   28 Nov 1995 15:32:18   Shirley
** No change.
** 
**    Rev 1.14   21 Nov 1995 11:22:48   Shirley
** No change.
** 
**    Rev 1.13   13 Nov 1995 09:28:08   Shirley
** No change.
** 
**    Rev 1.12   12 Nov 1995 11:29:58   Shirley
** EasyPack/SLD Version 0.24
** 
**    Rev 1.11   08 Nov 1995 16:33:10   Shirley
** No change.
** 
**    Rev 1.10   08 Nov 1995 12:45:08   Shirley
** No change.
** 
**    Rev 1.9   02 Nov 1995 10:03:40   Shirley
** No change.
** 
**    Rev 1.8   27 Oct 1995 16:46:50   Shirley
** No change.
** 
**    Rev 1.7   27 Oct 1995 13:44:22   Shirley
** No change.
** 
**    Rev 1.6   25 Oct 1995 14:26:10   Shirley
** EasyPack/SLD Version 0.1f
** 
**    Rev 1.5   18 Oct 1995 14:48:52   Shirley
** EasyPack/SLD Version 0.1e
** 
**    Rev 1.4   13 Oct 1995 13:20:08   Shirley
** EasyPack/SLD Version 0.1d
** 
**    Rev 1.3   29 Sep 1995 09:51:58   Shirley
** No change.
** 
**    Rev 1.2   20 Sep 1995 10:57:54   Shirley
** No change.
** 
**    Rev 1.1   15 Sep 1995 09:46:24   Shirley
** No change.
** 
**    Rev 1.0   07 Sep 1995 09:55:14   Shirley
** Initial revision.
**
****************************************************************************/

/////////////////////////////////////////////////////////////////////////////
//
//  File name:  XINIT.CPP
//
//  Description:The implement file of init routines
//
//  Author:     Roger Zhang
//
//  Date:       04/07/95
//
//  Modification:
//
//      1. 04/07/95, Initial version 
//
//
//  Copyright (C) 1995 Microtek International. All rights reserved.
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "cpucom.h"                   
#include "xinit.h"
#include "symblsvr.h"
#include "communic.h"
#include "abibase.h"
// add by Richard
#include "aliaspub.h"
#include "bankinit.h"
#include "cpust.h"       
#include "uicom.h"
/////////////////////////////////////////////////////////////////////////////
// External function prototypes.

void AdrSetAddrRange();

BOOL VarAllocateSpace(void);
void VarReleaseSpace(void);
BOOL StkAllocateSpace(void);
void StkReleaseSpace(void);

extern BOOL SetupCommunication(UINT pData);
extern BOOL SetCommType(int nTempCommType);

extern BOOL ReadLoadOption(void);
extern BOOL WriteLoadOption(void);

extern BOOL ReadMemWndIni(void);
extern BOOL WriteMemWndIni(void);

extern void InitRegSelect(void);

extern void DelLabel(void);
extern void NewLabel(void);

extern int InitGetCpuType(void);
extern int InitCpu(int iRoger);

extern void BankInit(void);
extern STATUS AbiAbort();

CString strAppPath;

int nPortAddr = 0x300;         
int nPortType = 0; // 0 -> Parallel(PCE)
                   // 1 -> LPT1
                   // 2 -> LPT2
                   // 3 -> LPT3          
                   // 4 -> COM1
                   // 5 -> COM2
                   // 6 -> COM3
                   // 7 -> COM4
                   
const char * DefPortAddr[8] = {
    "280",  //PCE
    "378",  //LPT1
    "3BC",  //LPT2
    "3BC",  //LPT3
    "3F8",  //COM1
    "2F8",  //COM2
    "0",    //COM3
    "0"     //COM4
};                                       

//extern char * PortAddr[8];

/////////////////////////////////////////////////////////////////////////////
//
//  Name:   ReadCommInfo
//
//  Description: Read communication information from .ini file
//
//  Input:  None
//
//  Output: None
//
//  Return: FALSE -- Fail to read.
//          TRUE  -- Read Ok.
//
/////////////////////////////////////////////////////////////////////////////
/*
BOOL ReadCommInfo(void)
{
    nPortType = AfxGetApp()->GetProfileInt("COMM", "Type", 0);
    if(nPortType < 0 || nPortType > 2) {
//        AfxMessageBox(IDS_PRM_INITCOMMTYPE);
        return FALSE;
    }
    
    CString str = AfxGetApp()->GetProfileString("COMM", "Addr", 
        DefPortAddr[nPortType]);
    sscanf(str, "%x", &nPortAddr);
    return TRUE;
}
*/
BOOL ReadCommInfo(void)
{
    nPortType = AfxGetApp()->GetProfileInt("COMM", "Type", 0);
    if(nPortType < 0 || nPortType > 7) {
        return FALSE;
    }
    
    CString str = AfxGetApp()->GetProfileString("COMM", "Addr", 
        DefPortAddr[nPortType]);
    sscanf(str, "%x", &nPortAddr);
    return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
//
//  Name:   WriteCommInfo
//
//  Description: Write communication information to .ini file
//
//  Input:  None
//
//  Output: None
//
//  Return: None
//
/////////////////////////////////////////////////////////////////////////////
void WriteCommInfo(void)
{   
    AfxGetApp()->WriteProfileInt("COMM", "Type", nPortType);
    char ach[20];
    wsprintf(ach, "%X", nPortAddr);
    AfxGetApp()->WriteProfileString("COMM", "Addr", ach);
}                    

BOOL CommDlg(void)
{
    AfxGetApp()->DoWaitCursor(-1);
    CCommunicationDialog dlg;
    int nRet = dlg.DoModal() == IDOK;
    AfxGetApp()->DoWaitCursor(1);
    return nRet;
}


                     
/////////////////////////////////////////////////////////////////////////////
//
//  Name:   Init
//
//  Description: Initial routine
//
//  Input:  None
//
//  Output: None
//
//  Return: 0 -- Initialize failure
//          1 -- Initialize Ok.
//
/////////////////////////////////////////////////////////////////////////////
int Init(void)
{   
    #ifdef _ZLR_
    GetXviewAppJohn()->m_pTheCom = new CWinSerialPICAgreeSetup;
    if (NULL == GetXviewAppJohn()->m_pTheCom) return FALSE;
    
    GetXviewAppJohn()->m_bCanTimer = TRUE;
    
    if (!(GetXviewAppJohn()->m_pTheCom)->CommInit()) {
    	AfxMessageBox("EasyPack/SLD communication failure!");
    	return FALSE;
    }	
    //added by john 96.6 to halt cpu
    CString strDos, strWindows, strVer, strFWVer, strCPU;
	void TstIdentify(CString& strCPU, CString& strFW, CString& strSW, 
	                 CString& strDOS, CString& strWin);

    TstIdentify(strCPU, strFWVer, strVer, strDos, strWindows);

//    if(ICE_OK != AbiAbort()) return FALSE;
	// Changed by Chen Jun, 08/08/96
	// Dome: Ignore incorrect CPU type.
	AbiAbort();
    
    // Select CPU Type
    int i = InitGetCpuType();
    if(i < 0) {
//     	AfxMessageBox("F/W can't support this CPU");
//     	return	0;

    // Changed by Chen Jun, 08/08/96
    // Dome told me to ignore the incorrect CPU.
    	if ( IDYES == AfxMessageBox(IDS_ERR_CPUTYPE, MB_YESNO) ) {
    		i = 0;
    	}
    	else {
    		return 0;
    	}
    	
    }
    i = InitCpu(i);
    if(!i)	return	0;

	::AdrSetAddrRange();

    //strAppPath = AfxGetApp()->GetProfileString("PATH", "ExePath");
          
//    NewLabel();
// Changed to Carl's new logo.
                
	//                                
	InitRegSelect();                                
                                
    // Read load option from the private profile      
    ReadLoadOption();                                   
    
    SymInitialize();

    //added by john 95.8
    i = InitBp();
    if(!i)  return  0;
    AfxGetApp()->DoWaitCursor(1);
    i = InitTrace();
    AfxGetApp()->DoWaitCursor(-1);
    if(!i)  return  0;
    //************************

    nCurRegs = nMaxReg;

    // For Variable window.
    if ( !::VarAllocateSpace() ) {
        return (0);
    }

    // For Stack window.
    if ( !::StkAllocateSpace() ) {
        return (0);
    }                                    
    
    // add by Richard
    MakeTable();
    
    //Added by Dragon
    //modify by chris:
extern UINT GetCpuId();
extern BOOL IsBankInit();
    if(GetCpuId()!=0x17&&IsBankInit())			//83C752: no bank, so g_nBankNum should 0
    	BankInit();
    ReadMemWndIni();
extern void GetRecentPathFromINI();
    GetRecentPathFromINI();    
    return i;
    #else

//set load path

    return 1;

#endif
}                    

/////////////////////////////////////////////////////////////////////////////
//
//  Name:   XPost
//
//  Description: Post routine on exit.
//
//  Input:  None
//
//  Output: None
//
//  Return: None
//
/////////////////////////////////////////////////////////////////////////////
void XPost(void)
{
    // Variable & Stack window.
    ::VarReleaseSpace();
    ::StkReleaseSpace();        
    
    delete GetXviewAppJohn()->m_pTheCom;
    
    WriteCommInfo();
    WriteLoadOption();
    WriteMemWndIni();
}

/////////////////////////////////(EOF)///////////////////////////////////////
