emu48-mirror/Sources/Emu48/DDESERV.C
Gwenhael Le Moine e1230f4415
2005-06-14: Updated to version 1.37
Signed-off-by: Gwenhael Le Moine <gwenhael.le.moine@gmail.com>
2024-03-19 23:33:35 +01:00

158 lines
4.3 KiB
C

/*
* DdeServ.c
*
* This file is part of Emu48
*
* Copyright (C) 1998 Christoph Gießelink
*
*/
#include "pch.h"
#include "Emu48.h"
#include "io.h"
HDDEDATA CALLBACK DdeCallback(UINT iType,UINT iFmt,HCONV hConv,
HSZ hsz1,HSZ hsz2,HDDEDATA hData,
DWORD dwData1,DWORD dwData2)
{
TCHAR szBuffer[32];
HDDEDATA hReturn;
LPBYTE lpData,lpHeader;
DWORD dwAddress,dwSize,dwLoop,dwIndex;
BOOL bSuccess;
// disable stack loading items on HP38G, HP39/40G
BOOL bStackEnable = cCurrentRomType!='6' && cCurrentRomType!='A' && cCurrentRomType!='E';
switch (iType)
{
case XTYP_CONNECT:
DdeQueryString(idDdeInst,hsz2,szBuffer,ARRAYSIZEOF(szBuffer),0);
if (0 != lstrcmp(szBuffer,szAppName))
return (HDDEDATA) FALSE;
DdeQueryString(idDdeInst,hsz1,szBuffer,ARRAYSIZEOF(szBuffer),0);
return (HDDEDATA) (0==lstrcmp(szBuffer,szTopic));
case XTYP_POKE:
if (!bStackEnable) return NULL; // only models with stack, ignore on others
// illegal data format or not in running state
if (iFmt != uCF_HpObj || nState != SM_RUN)
return (HDDEDATA) DDE_FNOTPROCESSED;
DdeAccessData(hData,&dwSize); // fetch data size
DdeUnaccessData(hData);
// reserve memory
if ((lpData = HeapAlloc(hHeap,0,dwSize * 2)) == NULL)
return (HDDEDATA) DDE_FNOTPROCESSED;
SuspendDebugger(); // suspend debugger
bDbgAutoStateCtrl = FALSE; // disable automatic debugger state control
if (!(Chipset.IORam[BITOFFSET]&DON)) // HP off
{
// turn on HP
KeyboardEvent(TRUE,0,0x8000);
KeyboardEvent(FALSE,0,0x8000);
}
if (WaitForSleepState()) // wait for cpu SHUTDN then sleep state
{
HeapFree(hHeap,0,lpData); // free memory
hReturn = DDE_FNOTPROCESSED;
goto cancel;
}
while (nState!=nNextState) Sleep(0);
_ASSERT(nState==SM_SLEEP);
// fetch data and write to stack
DdeGetData(hData,(LPBYTE) &dwIndex,sizeof(DWORD),0L);
if (dwIndex <= dwSize - sizeof(DWORD))
dwSize = dwIndex;
dwSize = DdeGetData(hData,lpData+dwSize,dwSize,sizeof(DWORD));
bSuccess = (WriteStack(lpData,dwSize) == S_ERR_NO);
HeapFree(hHeap,0,lpData); // free memory
SwitchToState(SM_RUN); // run state
while (nState!=nNextState) Sleep(0);
_ASSERT(nState==SM_RUN);
if (bSuccess == FALSE)
{
hReturn = DDE_FNOTPROCESSED;
goto cancel;
}
KeyboardEvent(TRUE,0,0x8000);
KeyboardEvent(FALSE,0,0x8000);
// wait for sleep mode
while(Chipset.Shutdn == FALSE) Sleep(0);
hReturn = (HDDEDATA) DDE_FACK;
cancel:
bDbgAutoStateCtrl = TRUE; // enable automatic debugger state control
ResumeDebugger();
return hReturn;
case XTYP_REQUEST:
if (!bStackEnable) return NULL; // only models with stack, ignore on others
// illegal data format or not in running state
if (iFmt != uCF_HpObj || nState != SM_RUN)
return NULL;
if (WaitForSleepState()) // wait for cpu SHUTDN then sleep state
return NULL;
while (nState!=nNextState) Sleep(0);
_ASSERT(nState==SM_SLEEP);
dwAddress = RPL_Pick(1); // pick address of level1 object
if (dwAddress == 0)
{
SwitchToState(SM_RUN); // run state
return NULL;
}
dwLoop = dwSize = (RPL_SkipOb(dwAddress) - dwAddress + 1) / 2;
lpHeader = (Chipset.type != 'X') ? BINARYHEADER48 : BINARYHEADER49;
// length of binary header
dwIndex = strlen(lpHeader);
// size of objectsize + header + object
dwSize += dwIndex + sizeof(DWORD);
// reserve memory
if ((lpData = HeapAlloc(hHeap,0,dwSize)) == NULL)
{
SwitchToState(SM_RUN); // run state
return NULL;
}
// save data length
*(DWORD *)lpData = dwLoop + dwIndex;
// copy header
memcpy(lpData + sizeof(DWORD),lpHeader,dwIndex);
// copy data
for (dwIndex += sizeof(DWORD);dwLoop--;++dwIndex,dwAddress += 2)
lpData[dwIndex] = Read2(dwAddress);
// write data
hReturn = DdeCreateDataHandle(idDdeInst,lpData,dwSize,0,hsz2,iFmt,0);
HeapFree(hHeap,0,lpData);
SwitchToState(SM_RUN); // run state
while (nState!=nNextState) Sleep(0);
_ASSERT(nState==SM_RUN);
return hReturn;
}
return NULL;
UNREFERENCED_PARAMETER(hConv);
UNREFERENCED_PARAMETER(dwData1);
UNREFERENCED_PARAMETER(dwData2);
}