emu48-mirror/Sources/Emu48/EMU48.C

1733 lines
50 KiB
C
Raw Normal View History

2024-03-19 22:24:30 +01:00
/*
* Emu48.c
*
* This file is part of Emu48
*
* Copyright (C) 1995 Sebastien Carlier
*
*/
#include "pch.h"
#include "resource.h"
#include "Emu48.h"
2024-03-19 22:36:03 +01:00
#include "io.h"
2024-03-19 22:24:30 +01:00
#include "kml.h"
2024-03-19 22:25:45 +01:00
#include "debugger.h"
2024-03-19 22:24:30 +01:00
#define VERSION "1.47"
2024-03-19 22:24:30 +01:00
2024-03-19 22:25:45 +01:00
// #define MONOCHROME // CF_BITMAP clipboard format
2024-03-19 22:24:30 +01:00
#ifdef _DEBUG
LPCTSTR szNoTitle = _T("Emu48 ")_T(VERSION)_T(" Debug");
2024-03-19 22:24:30 +01:00
#else
LPCTSTR szNoTitle = _T("Emu48 ")_T(VERSION);
2024-03-19 22:24:30 +01:00
#endif
2024-03-19 22:37:03 +01:00
LPTSTR szAppName = _T("Emu48"); // application name for DDE server
LPTSTR szTopic = _T("Stack"); // topic for DDE server
LPTSTR szTitle = NULL;
2024-03-19 22:38:33 +01:00
static BOOL bOwnCursor = FALSE;
static const LPCTSTR szLicence =
2024-03-19 22:37:03 +01:00
_T("This program is free software; you can redistribute it and/or modify\r\n")
_T("it under the terms of the GNU General Public License as published by\r\n")
_T("the Free Software Foundation; either version 2 of the License, or\r\n")
_T("(at your option) any later version.\r\n")
_T("\r\n")
_T("This program is distributed in the hope that it will be useful,\r\n")
_T("but WITHOUT ANY WARRANTY; without even the implied warranty of\r\n")
_T("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r\n")
_T("See the GNU General Public License for more details.\r\n")
_T("\r\n")
_T("You should have received a copy of the GNU General Public License\r\n")
_T("along with this program; if not, write to the Free Software Foundation,\r\n")
_T("Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA");
2024-03-19 22:25:45 +01:00
CRITICAL_SECTION csGDILock; // critical section for hWindowDC
CRITICAL_SECTION csLcdLock; // critical section for display update
2024-03-19 22:25:45 +01:00
CRITICAL_SECTION csKeyLock; // critical section for key scan
CRITICAL_SECTION csIOLock; // critical section for I/O access
CRITICAL_SECTION csT1Lock; // critical section for timer1 access
CRITICAL_SECTION csT2Lock; // critical section for timer2 access
2024-03-19 22:37:54 +01:00
CRITICAL_SECTION csTxdLock; // critical section for transmit byte
2024-03-19 22:36:03 +01:00
CRITICAL_SECTION csRecvLock; // critical section for receive byte
2024-03-19 22:38:33 +01:00
CRITICAL_SECTION csSlowLock; // critical section for speed slow down
2024-03-19 22:36:03 +01:00
INT nArgc; // no. of command line arguments
LPCTSTR *ppArgv; // command line arguments
LARGE_INTEGER lFreq; // high performance counter frequency
LARGE_INTEGER lAppStart; // high performance counter value at Appl. start
2024-03-19 22:25:45 +01:00
DWORD idDdeInst; // DDE server id
UINT uCF_HpObj; // DDE clipboard format
2024-03-19 22:38:33 +01:00
HANDLE hHeap;
2024-03-19 22:25:45 +01:00
HANDLE hThread;
DWORD lThreadId;
HANDLE hEventShutdn; // event handle to stop cpu thread
2024-03-19 22:24:30 +01:00
HINSTANCE hApp = NULL;
HWND hWnd = NULL;
2024-03-19 22:37:03 +01:00
HWND hDlgDebug = NULL; // handle for debugger dialog
HWND hDlgFind = NULL; // handle for debugger find dialog
2024-03-19 22:38:33 +01:00
HWND hDlgProfile = NULL; // handle for debugger profile dialog
2024-03-19 22:25:45 +01:00
HDC hWindowDC = NULL;
2024-03-19 22:24:30 +01:00
HPALETTE hPalette = NULL;
2024-03-19 22:25:45 +01:00
HPALETTE hOldPalette = NULL; // old palette of hWindowDC
2024-03-19 22:38:33 +01:00
HCURSOR hCursorArrow = NULL;
HCURSOR hCursorHand = NULL;
2024-03-19 22:24:30 +01:00
BOOL bAutoSave = FALSE;
BOOL bAutoSaveOnExit = TRUE;
BOOL bSaveDefConfirm = TRUE; // yes
BOOL bStartupBackup = FALSE;
2024-03-19 22:24:30 +01:00
BOOL bAlwaysDisplayLog = TRUE;
2024-03-19 22:38:33 +01:00
BOOL bLoadObjectWarning = TRUE;
BOOL bAlwaysOnTop = FALSE; // emulator window always on top
BOOL bActFollowsMouse = FALSE; // emulator window activation follows mouse
2024-03-19 22:24:30 +01:00
2024-03-19 22:37:03 +01:00
2024-03-19 22:24:30 +01:00
//################
//#
//# Window Status
//#
//################
2024-03-19 22:38:33 +01:00
VOID SetWindowTitle(LPCTSTR szString)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:38:33 +01:00
if (szTitle) HeapFree(hHeap,0,szTitle);
2024-03-19 22:37:03 +01:00
_ASSERT(hWnd != NULL);
2024-03-19 22:24:30 +01:00
if (szString)
{
szTitle = DuplicateString(szString);
SetWindowText(hWnd, szTitle);
}
else
{
2024-03-19 22:37:03 +01:00
szTitle = NULL;
2024-03-19 22:24:30 +01:00
SetWindowText(hWnd, szNoTitle);
}
return;
}
2024-03-19 22:37:54 +01:00
VOID UpdateWindowStatus(VOID)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
if (hWnd) // window open
{
// disable stack loading items on HP38G, HP39/40G
BOOL bStackEnable = cCurrentRomType!='6' && cCurrentRomType!='A' && cCurrentRomType!='E';
BOOL bRun = nState == SM_RUN || nState == SM_SLEEP;
2024-03-19 22:24:30 +01:00
2024-03-19 22:37:03 +01:00
UINT uStackEnable = (bRun && bStackEnable) ? MF_ENABLED : MF_GRAYED;
UINT uRun = bRun ? MF_ENABLED : MF_GRAYED;
UINT uBackup = bBackup ? MF_ENABLED : MF_GRAYED;
2024-03-19 22:24:30 +01:00
2024-03-19 22:37:03 +01:00
HMENU hMenu = GetMenu(hWnd); // get menu handle
EnableMenuItem(hMenu,ID_FILE_NEW,MF_ENABLED);
EnableMenuItem(hMenu,ID_FILE_OPEN,MF_ENABLED);
EnableMenuItem(hMenu,ID_FILE_SAVE,(bRun && szCurrentFilename[0]) ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(hMenu,ID_FILE_SAVEAS,uRun);
EnableMenuItem(hMenu,ID_FILE_CLOSE,uRun);
2024-03-19 22:25:45 +01:00
EnableMenuItem(hMenu,ID_OBJECT_LOAD,uStackEnable);
EnableMenuItem(hMenu,ID_OBJECT_SAVE,uStackEnable);
2024-03-19 22:37:03 +01:00
EnableMenuItem(hMenu,ID_VIEW_COPY,uRun);
2024-03-19 22:25:45 +01:00
EnableMenuItem(hMenu,ID_STACK_COPY,uStackEnable);
EnableMenuItem(hMenu,ID_STACK_PASTE,uStackEnable);
2024-03-19 22:37:03 +01:00
EnableMenuItem(hMenu,ID_VIEW_RESET,uRun);
EnableMenuItem(hMenu,ID_BACKUP_SAVE,uRun);
EnableMenuItem(hMenu,ID_BACKUP_RESTORE,uBackup);
EnableMenuItem(hMenu,ID_BACKUP_DELETE,uBackup);
EnableMenuItem(hMenu,ID_VIEW_SCRIPT,uRun);
EnableMenuItem(hMenu,ID_TOOL_DISASM,uRun);
2024-03-19 22:37:54 +01:00
EnableMenuItem(hMenu,ID_TOOL_DEBUG,(bRun && nDbgState == DBG_OFF) ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(hMenu,ID_TOOL_MACRO_RECORD,(bRun && nMacroState == MACRO_OFF) ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(hMenu,ID_TOOL_MACRO_PLAY,(bRun && nMacroState == MACRO_OFF) ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(hMenu,ID_TOOL_MACRO_STOP,(bRun && nMacroState != MACRO_OFF) ? MF_ENABLED : MF_GRAYED);
2024-03-19 22:24:30 +01:00
}
return;
}
VOID ForceForegroundWindow(HWND hWnd)
{
// force window to foreground
DWORD dwEmuThreadID = GetCurrentThreadId();
DWORD dwActThreadID = GetWindowThreadProcessId(GetForegroundWindow(),NULL);
AttachThreadInput(dwEmuThreadID,dwActThreadID,TRUE);
SetForegroundWindow(hWnd);
AttachThreadInput(dwEmuThreadID,dwActThreadID,FALSE);
return;
}
2024-03-19 22:24:30 +01:00
2024-03-19 22:36:03 +01:00
//################
//#
//# Clipboard Tool
//#
//################
VOID CopyItemsToClipboard(HWND hWnd) // save selected Listbox Items to Clipboard
{
LONG i;
LPINT lpnCount;
2024-03-19 22:37:54 +01:00
2024-03-19 22:36:03 +01:00
// get number of selections
if ((i = (LONG) SendMessage(hWnd,LB_GETSELCOUNT,0,0)) == 0)
2024-03-19 22:36:03 +01:00
return; // no items selected
2024-03-19 22:38:33 +01:00
if ((lpnCount = HeapAlloc(hHeap,0,i * sizeof(INT))) != NULL)
2024-03-19 22:36:03 +01:00
{
LPTSTR lpszData;
2024-03-19 22:36:03 +01:00
HANDLE hClipObj;
LONG j,lMem = 0;
// get indexes of selected items
i = (LONG) SendMessage(hWnd,LB_GETSELITEMS,i,(LPARAM) lpnCount);
2024-03-19 22:36:03 +01:00
for (j = 0;j < i;++j) // scan all selected items
{
// calculate total amount of characters
lMem += (LONG) SendMessage(hWnd,LB_GETTEXTLEN,lpnCount[j],0) + 2;
2024-03-19 22:36:03 +01:00
}
// allocate clipboard data
if ((hClipObj = GlobalAlloc(GMEM_MOVEABLE,(lMem + 1) * sizeof(*lpszData))) != NULL)
2024-03-19 22:36:03 +01:00
{
if ((lpszData = GlobalLock(hClipObj)))
2024-03-19 22:36:03 +01:00
{
for (j = 0;j < i;++j) // scan all selected items
{
lpszData += SendMessage(hWnd,LB_GETTEXT,lpnCount[j],(LPARAM) lpszData);
*lpszData++ = _T('\r');
*lpszData++ = _T('\n');
2024-03-19 22:36:03 +01:00
}
*lpszData = 0; // set EOS
2024-03-19 22:36:03 +01:00
GlobalUnlock(hClipObj); // unlock memory
}
if (OpenClipboard(hWnd))
{
if (EmptyClipboard())
#if defined _UNICODE
SetClipboardData(CF_UNICODETEXT,hClipObj);
#else
SetClipboardData(CF_TEXT,hClipObj);
#endif
2024-03-19 22:36:03 +01:00
else
GlobalFree(hClipObj);
CloseClipboard();
}
else // clipboard open failed
{
GlobalFree(hClipObj);
}
}
2024-03-19 22:38:33 +01:00
HeapFree(hHeap,0,lpnCount); // free item table
2024-03-19 22:36:03 +01:00
}
return;
}
2024-03-19 22:24:30 +01:00
//################
//#
//# Settings
//#
//################
// get R/W state of file
static BOOL IsFileWriteable(LPCTSTR szFilename)
{
DWORD dwFileAtt;
BOOL bWriteable = FALSE;
SetCurrentDirectory(szEmuDirectory);
dwFileAtt = GetFileAttributes(szFilename);
if (dwFileAtt != 0xFFFFFFFF)
bWriteable = ((dwFileAtt & FILE_ATTRIBUTE_READONLY) == 0);
SetCurrentDirectory(szCurrentDirectory);
return bWriteable;
}
// set listfield for serial combo boxes
static VOID SetCommList(HWND hDlg,LPCTSTR szWireSetting,LPCTSTR szIrSetting)
2024-03-19 22:24:30 +01:00
{
WPARAM wSelectWire,wSelectIr;
HKEY hKey;
wSelectWire = wSelectIr = 0; // set selections to disabled
SendDlgItemMessage(hDlg,IDC_WIRE,CB_ADDSTRING,0,(LPARAM) _T(NO_SERIAL));
SendDlgItemMessage(hDlg,IDC_IR ,CB_ADDSTRING,0,(LPARAM) _T(NO_SERIAL));
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_T("Hardware\\DeviceMap\\SerialComm"),
0,
KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS,
&hKey) == ERROR_SUCCESS)
2024-03-19 22:24:30 +01:00
{
HANDLE hComm;
TCHAR szBuffer[256],cKey[256],cData[256];
DWORD dwKeySize,dwDataSize;
WPARAM wSelIndexWire,wSelIndexIr;
BOOL bAddWire,bAddIr;
DWORD dwType,dwEnumVal;
LONG lRet;
wSelIndexWire = wSelIndexIr = 1; // preset selector
dwEnumVal = 0;
do
2024-03-19 22:24:30 +01:00
{
dwKeySize = ARRAYSIZEOF(cKey); // init return buffer sizes
dwDataSize = sizeof(cData);
2024-03-19 22:24:30 +01:00
lRet = RegEnumValue(hKey,dwEnumVal++,
cKey,&dwKeySize,
NULL,&dwType,
(LPBYTE) cData,&dwDataSize);
if (lRet == ERROR_SUCCESS && dwType == REG_SZ)
{
wsprintf(szBuffer,_T("\\\\.\\%s"),cData);
if ((bAddWire = (lstrcmp(&szBuffer[4],szWireSetting) == 0)))
wSelectWire = wSelIndexWire;
if ((bAddIr = (lstrcmp(&szBuffer[4],szIrSetting) == 0)))
wSelectIr = wSelIndexIr;
// test if COM port is valid
hComm = CreateFile(szBuffer,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if (hComm != INVALID_HANDLE_VALUE)
{
VERIFY(CloseHandle(hComm));
bAddWire = bAddIr = TRUE;
}
if (bAddWire) // add item to wire combobox
{
SendDlgItemMessage(hDlg,IDC_WIRE,CB_ADDSTRING,0,(LPARAM) &szBuffer[4]);
++wSelIndexWire;
}
if (bAddIr) // add item to ir combobox
{
SendDlgItemMessage(hDlg,IDC_IR,CB_ADDSTRING,0,(LPARAM) &szBuffer[4]);
++wSelIndexIr;
}
}
2024-03-19 22:24:30 +01:00
}
while (lRet == ERROR_SUCCESS);
_ASSERT(lRet == ERROR_NO_MORE_ITEMS);
RegCloseKey(hKey);
2024-03-19 22:24:30 +01:00
}
// set cursors
SendDlgItemMessage(hDlg,IDC_WIRE,CB_SETCURSEL,wSelectWire,0L);
SendDlgItemMessage(hDlg,IDC_IR ,CB_SETCURSEL,wSelectIr ,0L);
return;
2024-03-19 22:24:30 +01:00
}
static INT_PTR CALLBACK SettingsProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
2024-03-19 22:24:30 +01:00
{
HWND hWndInsertAfter;
LPCTSTR szActPort2Filename = _T("");
BOOL bPort2CfgChange = FALSE;
BOOL bPort2AttChange = FALSE;
2024-03-19 22:24:30 +01:00
switch (message)
{
case WM_INITDIALOG:
2024-03-19 22:25:45 +01:00
// init speed checkbox
2024-03-19 22:24:30 +01:00
CheckDlgButton(hDlg,IDC_REALSPEED,bRealSpeed);
CheckDlgButton(hDlg,IDC_GRAYSCALE,bGrayscale);
CheckDlgButton(hDlg,IDC_ALWAYSONTOP,bAlwaysOnTop);
CheckDlgButton(hDlg,IDC_ACTFOLLOWSMOUSE,bActFollowsMouse);
2024-03-19 22:24:30 +01:00
CheckDlgButton(hDlg,IDC_AUTOSAVE,bAutoSave);
CheckDlgButton(hDlg,IDC_AUTOSAVEONEXIT,bAutoSaveOnExit);
2024-03-19 22:38:33 +01:00
CheckDlgButton(hDlg,IDC_OBJECTLOADWARNING,bLoadObjectWarning);
2024-03-19 22:24:30 +01:00
CheckDlgButton(hDlg,IDC_ALWAYSDISPLOG,bAlwaysDisplayLog);
2024-03-19 22:25:45 +01:00
2024-03-19 22:36:03 +01:00
// set disassebler mode
CheckDlgButton(hDlg,(disassembler_mode == HP_MNEMONICS) ? IDC_DISASM_HP : IDC_DISASM_CLASS,BST_CHECKED);
// set sound slider
SendDlgItemMessage(hDlg,IDC_SOUND_SLIDER,TBM_SETRANGE,FALSE,MAKELONG(0,255));
SendDlgItemMessage(hDlg,IDC_SOUND_SLIDER,TBM_SETTICFREQ,256/8,0);
SendDlgItemMessage(hDlg,IDC_SOUND_SLIDER,TBM_SETPOS,TRUE,dwWaveVol);
// set sound radio button
CheckDlgButton(hDlg,bWaveBeep ? IDC_SOUND_WAVE : IDC_SOUND_SPEAKER,BST_CHECKED);
EnableWindow(GetDlgItem(hDlg,IDC_SOUND_SLIDER),bWaveBeep);
2024-03-19 22:25:45 +01:00
// set combobox parameter
SetCommList(hDlg,szSerialWire,szSerialIr);
2024-03-19 22:37:03 +01:00
if (bCommInit) // disable when port open
2024-03-19 22:25:45 +01:00
{
EnableWindow(GetDlgItem(hDlg,IDC_WIRE),FALSE);
EnableWindow(GetDlgItem(hDlg,IDC_IR),FALSE);
}
2024-03-19 22:36:03 +01:00
// HP48SX/GX
if (cCurrentRomType=='S' || cCurrentRomType=='G' || cCurrentRomType==0)
2024-03-19 22:25:45 +01:00
{
// init port1 enable checkbox
CheckDlgButton(hDlg,IDC_PORT1EN,(Chipset.cards_status & PORT1_PRESENT) != 0);
CheckDlgButton(hDlg,IDC_PORT1WR,(Chipset.cards_status & PORT1_WRITE) != 0);
if (nArgc < 3) // port2 filename from Emu48.ini file
{
szActPort2Filename = szPort2Filename;
}
else // port2 filename given from command line
{
szActPort2Filename = ppArgv[2];
EnableWindow(GetDlgItem(hDlg,IDC_PORT2),FALSE);
EnableWindow(GetDlgItem(hDlg,IDC_PORT2LOAD),FALSE);
}
// init port2 shared and writeable checkbox and set port2 filename
2024-03-19 22:25:45 +01:00
CheckDlgButton(hDlg,IDC_PORT2ISSHARED,bPort2IsShared);
CheckDlgButton(hDlg,IDC_PORT2WR,IsFileWriteable(szActPort2Filename));
SetDlgItemText(hDlg,IDC_PORT2,szActPort2Filename);
2024-03-19 22:37:03 +01:00
if (nState == SM_INVALID) // Invalid State
2024-03-19 22:25:45 +01:00
{
EnableWindow(GetDlgItem(hDlg,IDC_PORT1EN),FALSE);
EnableWindow(GetDlgItem(hDlg,IDC_PORT1WR),FALSE);
}
}
2024-03-19 22:37:03 +01:00
else // HP38G/HP39G/HP40G/HP49G
2024-03-19 22:24:30 +01:00
{
EnableWindow(GetDlgItem(hDlg,IDC_PORT1EN),FALSE);
EnableWindow(GetDlgItem(hDlg,IDC_PORT1WR),FALSE);
EnableWindow(GetDlgItem(hDlg,IDC_PORT2ISSHARED),FALSE);
EnableWindow(GetDlgItem(hDlg,IDC_PORT2WR),FALSE);
EnableWindow(GetDlgItem(hDlg,IDC_PORT2),FALSE);
EnableWindow(GetDlgItem(hDlg,IDC_PORT2LOAD),FALSE);
2024-03-19 22:36:03 +01:00
if (cCurrentRomType=='X') // HP49G
2024-03-19 22:25:45 +01:00
{
SendDlgItemMessage(hDlg,IDC_IR,CB_RESETCONTENT,0,0);
EnableWindow(GetDlgItem(hDlg,IDC_IR),FALSE);
}
2024-03-19 22:24:30 +01:00
}
return TRUE;
case WM_COMMAND:
2024-03-19 22:38:33 +01:00
switch (LOWORD(wParam))
2024-03-19 22:24:30 +01:00
{
case IDC_SOUND_SPEAKER:
EnableWindow(GetDlgItem(hDlg,IDC_SOUND_SLIDER),FALSE);
return TRUE;
case IDC_SOUND_WAVE:
EnableWindow(GetDlgItem(hDlg,IDC_SOUND_SLIDER),TRUE);
return TRUE;
case IDC_PORT2LOAD:
if (GetLoadObjectFilename(_T(BIN_FILTER),_T("BIN")))
{
TCHAR szFilename[MAX_PATH];
LPTSTR lpFilePart;
// check if file path and Emu48 directory path is identical
if (GetFullPathName(szBufferFilename,ARRAYSIZEOF(szFilename),szFilename,&lpFilePart))
{
*(lpFilePart-1) = 0; // devide path and name
// name is in the Emu48 directory -> use only name
if (lstrcmpi(szEmuDirectory,szFilename) == 0)
lstrcpy(szBufferFilename,lpFilePart);
}
SetDlgItemText(hDlg,IDC_PORT2,szBufferFilename);
// adjust R/W checkbox
CheckDlgButton(hDlg,IDC_PORT2WR,IsFileWriteable(szBufferFilename));
}
return TRUE;
2024-03-19 22:38:33 +01:00
case IDOK:
2024-03-19 22:25:45 +01:00
if (Chipset.Port1Size && cCurrentRomType!='X')
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
UINT nOldState = SwitchToState(SM_SLEEP);
2024-03-19 22:36:03 +01:00
// save old card status
BYTE bCardsStatus = Chipset.cards_status;
// port1 disabled?
2024-03-19 22:25:45 +01:00
Chipset.cards_status &= ~(PORT1_PRESENT | PORT1_WRITE);
2024-03-19 22:24:30 +01:00
if (IsDlgButtonChecked(hDlg, IDC_PORT1EN))
{
Chipset.cards_status |= PORT1_PRESENT;
2024-03-19 22:25:45 +01:00
if (IsDlgButtonChecked(hDlg, IDC_PORT1WR))
2024-03-19 22:24:30 +01:00
Chipset.cards_status |= PORT1_WRITE;
}
2024-03-19 22:36:03 +01:00
// changed card status in slot1?
if ( ((bCardsStatus ^ Chipset.cards_status) & (PORT1_PRESENT | PORT1_WRITE)) != 0
&& (Chipset.IORam[CARDCTL] & ECDT) != 0 && (Chipset.IORam[TIMER2_CTRL] & RUN) != 0
)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:36:03 +01:00
Chipset.HST |= MP; // set Module Pulled
IOBit(SRQ2,NINT,FALSE); // set NINT to low
Chipset.SoftInt = TRUE; // set interrupt
2024-03-19 22:24:30 +01:00
bInterrupt = TRUE;
}
SwitchToState(nOldState);
}
// HP48SX/GX port2 change settings detection
if (cCurrentRomType=='S' || cCurrentRomType=='G' || cCurrentRomType==0)
{
TCHAR szFilename[MAX_PATH];
BOOL bOldPort2IsShared = bPort2IsShared;
szActPort2Filename = (nArgc < 3) ? szPort2Filename : ppArgv[2];
// shared port
bPort2IsShared = IsDlgButtonChecked(hDlg,IDC_PORT2ISSHARED);
if (bPort2IsShared != bOldPort2IsShared)
{
bPort2CfgChange = TRUE; // slot2 configuration changed
}
if (nArgc < 3) // port2 filename from Emu48.ini file
{
// get current filename and notify difference
GetDlgItemText(hDlg,IDC_PORT2,szFilename,ARRAYSIZEOF(szFilename));
if (lstrcmp(szPort2Filename,szFilename) != 0)
{
lstrcpyn(szPort2Filename,szFilename,ARRAYSIZEOF(szPort2Filename));
bPort2CfgChange = TRUE; // slot2 configuration changed
}
}
// R/W port
if ( *szActPort2Filename != 0
&& (BOOL) IsDlgButtonChecked(hDlg,IDC_PORT2WR) != IsFileWriteable(szActPort2Filename))
{
bPort2AttChange = TRUE; // slot2 file R/W attribute changed
bPort2CfgChange = TRUE; // slot2 configuration changed
}
}
2024-03-19 22:25:45 +01:00
// get speed checkbox value
2024-03-19 22:24:30 +01:00
bRealSpeed = IsDlgButtonChecked(hDlg,IDC_REALSPEED);
bAlwaysOnTop = IsDlgButtonChecked(hDlg,IDC_ALWAYSONTOP);
bActFollowsMouse = IsDlgButtonChecked(hDlg,IDC_ACTFOLLOWSMOUSE);
bAutoSave = IsDlgButtonChecked(hDlg,IDC_AUTOSAVE);
bAutoSaveOnExit = IsDlgButtonChecked(hDlg,IDC_AUTOSAVEONEXIT);
bLoadObjectWarning = IsDlgButtonChecked(hDlg,IDC_OBJECTLOADWARNING);
bAlwaysDisplayLog = IsDlgButtonChecked(hDlg,IDC_ALWAYSDISPLOG);
2024-03-19 22:25:45 +01:00
SetSpeed(bRealSpeed); // set speed
// LCD grayscale checkbox has been changed
if (bGrayscale != (BOOL) IsDlgButtonChecked(hDlg,IDC_GRAYSCALE))
{
UINT nOldState = SwitchToState(SM_INVALID);
SetLcdMode(!bGrayscale); // set new display mode
SwitchToState(nOldState);
}
if (bPort2CfgChange) // slot2 configuration changed
2024-03-19 22:24:30 +01:00
{
UINT nOldState = SwitchToState(SM_INVALID);
UnmapPort2(); // unmap port2
if (bPort2AttChange) // slot2 R/W mode changed
{
DWORD dwFileAtt;
SetCurrentDirectory(szEmuDirectory);
dwFileAtt = GetFileAttributes(szActPort2Filename);
if (dwFileAtt != 0xFFFFFFFF)
{
if (IsDlgButtonChecked(hDlg,IDC_PORT2WR))
dwFileAtt &= ~FILE_ATTRIBUTE_READONLY;
else
dwFileAtt |= FILE_ATTRIBUTE_READONLY;
SetFileAttributes(szActPort2Filename,dwFileAtt);
}
SetCurrentDirectory(szCurrentDirectory);
}
if (cCurrentRomType) // ROM defined
{
MapPort2(szActPort2Filename);
// port2 changed and card detection enabled
if ( (bPort2AttChange || Chipset.wPort2Crc != wPort2Crc)
&& (Chipset.IORam[CARDCTL] & ECDT) != 0 && (Chipset.IORam[TIMER2_CTRL] & RUN) != 0
)
{
Chipset.HST |= MP; // set Module Pulled
IOBit(SRQ2,NINT,FALSE); // set NINT to low
Chipset.SoftInt = TRUE; // set interrupt
bInterrupt = TRUE;
}
// save fingerprint of port2
Chipset.wPort2Crc = wPort2Crc;
}
SwitchToState(nOldState);
2024-03-19 22:24:30 +01:00
}
// set disassembler mode
2024-03-19 22:36:03 +01:00
disassembler_mode = IsDlgButtonChecked(hDlg,IDC_DISASM_HP) ? HP_MNEMONICS : CLASS_MNEMONICS;
// set sound data
dwWaveVol = (DWORD) SendDlgItemMessage(hDlg,IDC_SOUND_SLIDER,TBM_GETPOS,0,0);
bWaveBeep = IsDlgButtonChecked(hDlg,IDC_SOUND_WAVE);
2024-03-19 22:25:45 +01:00
// set combobox parameter
2024-03-19 22:37:03 +01:00
GetDlgItemText(hDlg,IDC_WIRE,szSerialWire,ARRAYSIZEOF(szSerialWire));
2024-03-19 22:36:03 +01:00
if (cCurrentRomType!='X') // HP49G Ir port is not connected
2024-03-19 22:37:03 +01:00
GetDlgItemText(hDlg,IDC_IR,szSerialIr,ARRAYSIZEOF(szSerialIr));
// set window Z order
hWndInsertAfter = bAlwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST;
SetWindowPos(hWnd,hWndInsertAfter,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
if (hDlgDebug != NULL)
{
SetWindowPos(hDlgDebug,hWndInsertAfter,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
}
2024-03-19 22:38:33 +01:00
// no break
case IDCANCEL:
EndDialog(hDlg, LOWORD(wParam));
2024-03-19 22:24:30 +01:00
}
break;
}
return FALSE;
UNREFERENCED_PARAMETER(lParam);
2024-03-19 22:24:30 +01:00
}
//################
//#
//# Save Helper
//#
//################
//
// UINT SaveChanges(BOOL bAuto);
// Return code :
// IDYES File successfuly saved
// IDNO File not saved
// IDCANCEL Cancel command
//
static UINT SaveChanges(BOOL bAuto)
{
UINT uReply;
2024-03-19 22:37:54 +01:00
2024-03-19 22:24:30 +01:00
if (pbyRom == NULL) return IDNO;
2024-03-19 22:37:54 +01:00
2024-03-19 22:24:30 +01:00
if (bAuto)
uReply = IDYES;
else
{
UINT uStyle = bSaveDefConfirm ? 0 : MB_DEFBUTTON2;
uReply = YesNoCancelMessage(_T("Do you want to save changes ?"),uStyle);
}
2024-03-19 22:37:54 +01:00
2024-03-19 22:24:30 +01:00
if (uReply != IDYES) return uReply;
2024-03-19 22:37:54 +01:00
if (szCurrentFilename[0] == 0)
2024-03-19 22:24:30 +01:00
{ // Save As...
if (GetSaveAsFilename())
{
if (SaveDocumentAs(szBufferFilename))
return IDYES;
else
return IDCANCEL;
}
return IDNO;
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:37:54 +01:00
2024-03-19 22:24:30 +01:00
SaveDocument();
return IDYES;
}
//################
//#
//# Message Handlers
//#
//################
//
// WM_CREATE
//
static LRESULT OnCreate(HWND hWindow)
{
2024-03-19 22:36:03 +01:00
InitializeCriticalSection(&csGDILock);
InitializeCriticalSection(&csLcdLock);
2024-03-19 22:36:03 +01:00
InitializeCriticalSection(&csKeyLock);
InitializeCriticalSection(&csIOLock);
InitializeCriticalSection(&csT1Lock);
InitializeCriticalSection(&csT2Lock);
2024-03-19 22:37:54 +01:00
InitializeCriticalSection(&csTxdLock);
2024-03-19 22:36:03 +01:00
InitializeCriticalSection(&csRecvLock);
2024-03-19 22:38:33 +01:00
InitializeCriticalSection(&csSlowLock);
// load cursors
hCursorArrow = LoadCursor(NULL,IDC_ARROW);
hCursorHand = LoadCursor(NULL,IDC_HAND);
if (hCursorHand == NULL)
{
// for Win95, NT4.0
bOwnCursor = ((hCursorHand = CreateHandCursor()) != NULL);
}
2024-03-19 22:36:03 +01:00
2024-03-19 22:24:30 +01:00
hWnd = hWindow;
hWindowDC = GetDC(hWnd);
return 0;
}
//
// WM_DESTROY
//
static LRESULT OnDestroy(HWND hWindow)
{
2024-03-19 22:25:45 +01:00
DragAcceptFiles(hWnd,FALSE); // no WM_DROPFILES message any more
2024-03-19 22:37:54 +01:00
if (hThread) SwitchToState(SM_RETURN); // exit emulation thread
2024-03-19 22:25:45 +01:00
SetWindowTitle(NULL); // free memory of title
2024-03-19 22:37:54 +01:00
ReleaseDC(hWnd, hWindowDC);
2024-03-19 22:25:45 +01:00
hWindowDC = NULL; // hWindowDC isn't valid any more
2024-03-19 22:24:30 +01:00
hWnd = NULL;
2024-03-19 22:36:03 +01:00
2024-03-19 22:38:33 +01:00
if (bOwnCursor) // destroy hand cursor
{
DestroyCursor(hCursorHand);
bOwnCursor = FALSE;
}
2024-03-19 22:36:03 +01:00
DeleteCriticalSection(&csGDILock);
DeleteCriticalSection(&csLcdLock);
2024-03-19 22:36:03 +01:00
DeleteCriticalSection(&csKeyLock);
DeleteCriticalSection(&csIOLock);
DeleteCriticalSection(&csT1Lock);
DeleteCriticalSection(&csT2Lock);
2024-03-19 22:37:54 +01:00
DeleteCriticalSection(&csTxdLock);
2024-03-19 22:36:03 +01:00
DeleteCriticalSection(&csRecvLock);
2024-03-19 22:38:33 +01:00
DeleteCriticalSection(&csSlowLock);
2024-03-19 22:36:03 +01:00
2024-03-19 22:37:03 +01:00
#if defined _USRDLL // DLL version
DLLDestroyWnd(); // cleanup system
#else // EXE version
PostQuitMessage(0); // exit message loop
#endif
2024-03-19 22:24:30 +01:00
return 0;
UNREFERENCED_PARAMETER(hWindow);
}
//
// WM_PAINT
//
static LRESULT OnPaint(HWND hWindow)
{
PAINTSTRUCT Paint;
HDC hPaintDC;
2024-03-19 22:37:54 +01:00
2024-03-19 22:24:30 +01:00
hPaintDC = BeginPaint(hWindow, &Paint);
2024-03-19 22:25:45 +01:00
if (hMainDC != NULL)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:54 +01:00
RECT rcMainPaint = Paint.rcPaint;
rcMainPaint.left += nBackgroundX; // coordinates in source bitmap
rcMainPaint.top += nBackgroundY;
rcMainPaint.right += nBackgroundX;
rcMainPaint.bottom += nBackgroundY;
2024-03-19 22:36:03 +01:00
EnterCriticalSection(&csGDILock); // solving NT GDI problems
{
2024-03-19 22:37:03 +01:00
UINT nLines = (Chipset.lcounter == 0) ? 64 : (Chipset.lcounter + 1);
// redraw background bitmap
BitBlt(hPaintDC, Paint.rcPaint.left, Paint.rcPaint.top,
Paint.rcPaint.right-Paint.rcPaint.left, Paint.rcPaint.bottom-Paint.rcPaint.top,
2024-03-19 22:37:54 +01:00
hMainDC, rcMainPaint.left, rcMainPaint.top, SRCCOPY);
SetWindowOrgEx(hPaintDC, nBackgroundX, nBackgroundY, NULL);
2024-03-19 22:37:03 +01:00
// redraw main display area
BitBlt(hPaintDC, nLcdX, nLcdY,
2024-03-19 22:38:33 +01:00
131*nLcdZoom, nLines*nLcdZoom,
hLcdDC, Chipset.boffset*nLcdZoom, 0, SRCCOPY);
2024-03-19 22:37:03 +01:00
// redraw menu display area
BitBlt(hPaintDC, nLcdX, nLcdY+nLines*nLcdZoom,
2024-03-19 22:38:33 +01:00
131*nLcdZoom, (64-nLines)*nLcdZoom,
hLcdDC, 0, nLines*nLcdZoom, SRCCOPY);
2024-03-19 22:36:03 +01:00
GdiFlush();
}
LeaveCriticalSection(&csGDILock);
2024-03-19 22:37:03 +01:00
UpdateAnnunciators();
2024-03-19 22:37:54 +01:00
RefreshButtons(&rcMainPaint);
2024-03-19 22:24:30 +01:00
}
EndPaint(hWindow, &Paint);
return 0;
}
//
// WM_DROPFILES
//
static LRESULT OnDropFiles(HANDLE hFilesInfo)
{
2024-03-19 22:37:03 +01:00
TCHAR szFileName[MAX_PATH];
WORD wNumFiles,wIndex;
BOOL bSuccess;
2024-03-19 22:24:30 +01:00
// get number of files dropped
wNumFiles = DragQueryFile (hFilesInfo,(UINT)-1,NULL,0);
2024-03-19 22:37:54 +01:00
SuspendDebugger(); // suspend debugger
bDbgAutoStateCtrl = FALSE; // disable automatic debugger state control
// calculator off, turn on
if (!(Chipset.IORam[BITOFFSET]&DON))
2024-03-19 22:24:30 +01:00
{
// turn on HP
KeyboardEvent(TRUE,0,0x8000);
KeyboardEvent(FALSE,0,0x8000);
}
2024-03-19 22:37:03 +01:00
_ASSERT(nState == SM_RUN); // emulator must be in RUN state
2024-03-19 22:24:30 +01:00
if (WaitForSleepState()) // wait for cpu SHUTDN then sleep state
{
DragFinish (hFilesInfo);
2024-03-19 22:37:03 +01:00
InfoMessage(_T("The emulator is busy."));
2024-03-19 22:37:54 +01:00
goto cancel;
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:37:03 +01:00
_ASSERT(nState == SM_SLEEP);
2024-03-19 22:24:30 +01:00
// get each name and load it into the emulator
for (wIndex = 0;wIndex < wNumFiles;++wIndex)
{
2024-03-19 22:37:03 +01:00
DragQueryFile (hFilesInfo,wIndex,szFileName,ARRAYSIZEOF(szFileName));
2024-03-19 22:24:30 +01:00
// szFileName has file name, now try loading it
if ((bSuccess = LoadObject(szFileName)) == FALSE)
break;
}
DragFinish (hFilesInfo);
2024-03-19 22:37:03 +01:00
SwitchToState(SM_RUN); // run state
2024-03-19 22:24:30 +01:00
while (nState!=nNextState) Sleep(0);
2024-03-19 22:37:03 +01:00
_ASSERT(nState == SM_RUN);
2024-03-19 22:24:30 +01:00
if (bSuccess == FALSE) // data not copied
2024-03-19 22:37:54 +01:00
goto cancel;
2024-03-19 22:24:30 +01:00
KeyboardEvent(TRUE,0,0x8000);
Sleep(200);
2024-03-19 22:24:30 +01:00
KeyboardEvent(FALSE,0,0x8000);
// wait for sleep mode
while (Chipset.Shutdn == FALSE) Sleep(0);
2024-03-19 22:37:54 +01:00
cancel:
bDbgAutoStateCtrl = TRUE; // enable automatic debugger state control
ResumeDebugger();
2024-03-19 22:24:30 +01:00
return 0;
}
//
// ID_FILE_NEW
//
2024-03-19 22:37:54 +01:00
static LRESULT OnFileNew(VOID)
2024-03-19 22:24:30 +01:00
{
if (pbyRom)
{
2024-03-19 22:37:03 +01:00
SwitchToState(SM_INVALID);
if (IDCANCEL == SaveChanges(bAutoSave))
goto cancel;
SaveBackup();
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:37:03 +01:00
if (NewDocument()) SetWindowTitle(_T("Untitled"));
2024-03-19 22:24:30 +01:00
UpdateWindowStatus();
cancel:
2024-03-19 22:37:03 +01:00
if (pbyRom) SwitchToState(SM_RUN);
2024-03-19 22:24:30 +01:00
return 0;
}
//
// ID_FILE_OPEN
//
2024-03-19 22:37:54 +01:00
static LRESULT OnFileOpen(VOID)
2024-03-19 22:24:30 +01:00
{
if (pbyRom)
{
2024-03-19 22:37:03 +01:00
SwitchToState(SM_INVALID);
if (IDCANCEL == SaveChanges(bAutoSave))
goto cancel;
2024-03-19 22:24:30 +01:00
}
if (GetOpenFilename())
{
if (OpenDocument(szBufferFilename))
MruAdd(szCurrentFilename);
}
cancel:
if (pbyRom) SwitchToState(SM_RUN);
return 0;
}
//
// ID_FILE_MRU_FILE1
//
static LRESULT OnFileMruOpen(UINT wID)
{
LPCTSTR lpszFilename;
wID -= ID_FILE_MRU_FILE1; // zero based MRU index
lpszFilename = MruFilename(wID); // full filename from MRU list
if (lpszFilename == NULL) return 0; // MRU slot not filled
if (pbyRom)
{
SwitchToState(SM_INVALID);
if (IDCANCEL == SaveChanges(bAutoSave))
goto cancel;
}
if (!OpenDocument(lpszFilename)) // document loading failed
{
MruRemove(wID); // entry not valid any more
2024-03-19 22:24:30 +01:00
}
cancel:
2024-03-19 22:37:03 +01:00
if (pbyRom) SwitchToState(SM_RUN);
2024-03-19 22:24:30 +01:00
return 0;
}
//
// ID_FILE_SAVE
//
2024-03-19 22:37:54 +01:00
static LRESULT OnFileSave(VOID)
2024-03-19 22:24:30 +01:00
{
if (pbyRom == NULL) return 0;
2024-03-19 22:37:03 +01:00
SwitchToState(SM_INVALID);
2024-03-19 22:24:30 +01:00
SaveChanges(TRUE);
2024-03-19 22:37:03 +01:00
SwitchToState(SM_RUN);
2024-03-19 22:24:30 +01:00
return 0;
}
//
// ID_FILE_SAVEAS
//
2024-03-19 22:37:54 +01:00
static LRESULT OnFileSaveAs(VOID)
2024-03-19 22:24:30 +01:00
{
if (pbyRom == NULL) return 0;
2024-03-19 22:37:03 +01:00
SwitchToState(SM_INVALID);
2024-03-19 22:37:54 +01:00
if (GetSaveAsFilename())
2024-03-19 22:24:30 +01:00
{
if (SaveDocumentAs(szBufferFilename))
MruAdd(szCurrentFilename);
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:37:54 +01:00
2024-03-19 22:37:03 +01:00
SwitchToState(SM_RUN);
2024-03-19 22:24:30 +01:00
return 0;
}
//
// ID_FILE_CLOSE
//
2024-03-19 22:37:54 +01:00
static LRESULT OnFileClose(VOID)
2024-03-19 22:24:30 +01:00
{
if (pbyRom == NULL) return 0;
2024-03-19 22:37:03 +01:00
SwitchToState(SM_INVALID);
if (SaveChanges(bAutoSave) != IDCANCEL)
2024-03-19 22:24:30 +01:00
{
ResetDocument();
SetWindowTitle(NULL);
}
else
{
2024-03-19 22:37:03 +01:00
SwitchToState(SM_RUN);
2024-03-19 22:24:30 +01:00
}
return 0;
}
//
// ID_FILE_EXIT
//
// WM_SYS_CLOSE
//
2024-03-19 22:37:54 +01:00
static LRESULT OnFileExit(VOID)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
SwitchToState(SM_INVALID); // hold emulation thread
2024-03-19 22:24:30 +01:00
if (SaveChanges(bAutoSaveOnExit) == IDCANCEL)
{
2024-03-19 22:37:03 +01:00
SwitchToState(SM_RUN); // on cancel restart emulation thread
2024-03-19 22:24:30 +01:00
return 0;
}
DestroyWindow(hWnd);
return 0;
}
//
// ID_VIEW_COPY
//
2024-03-19 22:37:54 +01:00
static LRESULT OnViewCopy(VOID)
2024-03-19 22:24:30 +01:00
{
if (OpenClipboard(hWnd))
{
if (EmptyClipboard())
{
#if !defined MONOCHROME
2024-03-19 22:25:45 +01:00
// DIB bitmap
2024-03-19 22:24:30 +01:00
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
#define PALVERSION 0x300
BITMAP bm;
LPBITMAPINFOHEADER lpbi;
PLOGPALETTE ppal;
HBITMAP hBmp;
HDC hBmpDC;
HANDLE hClipObj;
WORD wBits;
DWORD dwLen, dwSizeImage;
2024-03-19 22:38:33 +01:00
_ASSERT(nLcdZoom == 1 || nLcdZoom == 2 || nLcdZoom == 4);
hBmp = CreateCompatibleBitmap(hLcdDC,131*nLcdZoom,64*nLcdZoom);
2024-03-19 22:24:30 +01:00
hBmpDC = CreateCompatibleDC(hLcdDC);
hBmp = SelectObject(hBmpDC,hBmp);
2024-03-19 22:38:33 +01:00
BitBlt(hBmpDC,0,0,131*nLcdZoom,64*nLcdZoom,hLcdDC,Chipset.boffset*nLcdZoom,0,SRCCOPY);
2024-03-19 22:24:30 +01:00
hBmp = SelectObject(hBmpDC,hBmp);
// fill BITMAP structure for size information
GetObject(hBmp, sizeof(bm), &bm);
wBits = bm.bmPlanes * bm.bmBitsPixel;
2024-03-19 22:37:54 +01:00
// make sure bits per pixel is valid
2024-03-19 22:24:30 +01:00
if (wBits <= 1)
wBits = 1;
else if (wBits <= 4)
wBits = 4;
else if (wBits <= 8)
wBits = 8;
2024-03-19 22:37:54 +01:00
else // if greater than 8-bit, force to 24-bit
2024-03-19 22:24:30 +01:00
wBits = 24;
dwSizeImage = WIDTHBYTES((DWORD)bm.bmWidth * wBits) * bm.bmHeight;
// calculate memory size to store CF_DIB data
dwLen = sizeof(BITMAPINFOHEADER) + dwSizeImage;
if (wBits != 24) // a 24 bitcount DIB has no color table
{
// add size for color table
dwLen += (DWORD) (1 << wBits) * sizeof(RGBQUAD);
2024-03-19 22:24:30 +01:00
}
// memory allocation for clipboard data
if ((hClipObj = GlobalAlloc(GMEM_MOVEABLE, dwLen)) != NULL)
{
lpbi = GlobalLock(hClipObj);
// initialize BITMAPINFOHEADER
lpbi->biSize = sizeof(BITMAPINFOHEADER);
lpbi->biWidth = bm.bmWidth;
lpbi->biHeight = bm.bmHeight;
lpbi->biPlanes = 1;
lpbi->biBitCount = wBits;
lpbi->biCompression = BI_RGB;
lpbi->biSizeImage = dwSizeImage;
lpbi->biXPelsPerMeter = 0;
lpbi->biYPelsPerMeter = 0;
lpbi->biClrUsed = 0;
lpbi->biClrImportant = 0;
// get bitmap color table and bitmap data
GetDIBits(hBmpDC, hBmp, 0, lpbi->biHeight, (LPBYTE)lpbi + dwLen - dwSizeImage,
(LPBITMAPINFO)lpbi, DIB_RGB_COLORS);
GlobalUnlock(hClipObj);
2024-03-19 22:37:54 +01:00
SetClipboardData(CF_DIB, hClipObj);
2024-03-19 22:24:30 +01:00
2024-03-19 22:37:54 +01:00
// get number of entries in the logical palette
2024-03-19 22:24:30 +01:00
GetObject(hPalette,sizeof(WORD),&wBits);
// memory allocation for temporary palette data
2024-03-19 22:38:33 +01:00
if ((ppal = HeapAlloc(hHeap,HEAP_ZERO_MEMORY,sizeof(LOGPALETTE) + wBits * sizeof(PALETTEENTRY))) != NULL)
2024-03-19 22:24:30 +01:00
{
ppal->palVersion = PALVERSION;
ppal->palNumEntries = wBits;
GetPaletteEntries(hPalette, 0, wBits, ppal->palPalEntry);
2024-03-19 22:37:54 +01:00
SetClipboardData(CF_PALETTE, CreatePalette(ppal));
2024-03-19 22:38:33 +01:00
HeapFree(hHeap,0,ppal);
2024-03-19 22:24:30 +01:00
}
}
DeleteDC(hBmpDC);
DeleteObject(hBmp);
#undef WIDTHBYTES
#undef PALVERSION
#else
HBITMAP hOldBmp, hBmp;
HDC hBmpDC;
2024-03-19 22:37:54 +01:00
// don't work with background index <> 0
2024-03-19 22:38:33 +01:00
_ASSERT(nLcdZoom == 1 || nLcdZoom == 2 || nLcdZoom == 4);
hBmp = CreateBitmap(131*nLcdZoom,64*nLcdZoom,1,1,NULL);
2024-03-19 22:24:30 +01:00
hBmpDC = CreateCompatibleDC(NULL);
hOldBmp = (HBITMAP)SelectObject(hBmpDC,hBmp);
2024-03-19 22:38:33 +01:00
BitBlt(hBmpDC,0,0,131*nLcdZoom,64*nLcdZoom,hLcdDC,Chipset.boffset*nLcdZoom,0,SRCCOPY);
2024-03-19 22:24:30 +01:00
SetClipboardData(CF_BITMAP,hBmp);
SelectObject(hBmpDC,hOldBmp);
DeleteDC(hBmpDC);
#endif
}
CloseClipboard();
}
return 0;
}
//
// ID_VIEW_RESET
//
2024-03-19 22:37:54 +01:00
static LRESULT OnViewReset(VOID)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
if (nState != SM_RUN) return 0;
if (YesNoMessage(_T("Are you sure you want to press the Reset Button ?"))==IDYES)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
SwitchToState(SM_SLEEP);
2024-03-19 22:36:03 +01:00
CpuReset(); // register setting after Cpu Reset
2024-03-19 22:37:03 +01:00
SwitchToState(SM_RUN);
2024-03-19 22:24:30 +01:00
}
return 0;
}
//
// ID_VIEW_SETTINGS
//
2024-03-19 22:37:54 +01:00
static LRESULT OnViewSettings(VOID)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
// not in nState = SM_INVALID or port2 file must be closed from document
_ASSERT(nState != SM_INVALID || pbyPort2 == NULL);
2024-03-19 22:25:45 +01:00
2024-03-19 22:24:30 +01:00
if (DialogBox(hApp, MAKEINTRESOURCE(IDD_SETTINGS), hWnd, (DLGPROC)SettingsProc) == -1)
2024-03-19 22:37:03 +01:00
AbortMessage(_T("Settings Dialog Creation Error !"));
2024-03-19 22:25:45 +01:00
2024-03-19 22:24:30 +01:00
WriteSettings();
return 0;
}
//
// ID_VIEW_SCRIPT
//
2024-03-19 22:37:54 +01:00
static LRESULT OnViewScript(VOID)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
BYTE cType = cCurrentRomType;
if (nState != SM_RUN)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
InfoMessage(_T("You cannot change the KML script when Emu48 is not running.\n")
_T("Use the File,New menu item to create a new calculator."));
2024-03-19 22:24:30 +01:00
return 0;
}
2024-03-19 22:37:03 +01:00
SwitchToState(SM_INVALID);
do
2024-03-19 22:24:30 +01:00
{
if (!DisplayChooseKml(cType)) break;
2024-03-19 22:24:30 +01:00
}
while (!InitKML(szCurrentKml,FALSE));
SetWindowPathTitle(szCurrentFilename); // update window title line
2024-03-19 22:37:03 +01:00
if (pbyRom) SwitchToState(SM_RUN);
2024-03-19 22:24:30 +01:00
return 0;
}
//
// ID_BACKUP_SAVE
//
2024-03-19 22:37:54 +01:00
static LRESULT OnBackupSave(VOID)
2024-03-19 22:24:30 +01:00
{
UINT nOldState;
if (pbyRom == NULL) return 0;
2024-03-19 22:37:03 +01:00
nOldState = SwitchToState(SM_INVALID);
2024-03-19 22:24:30 +01:00
SaveBackup();
SwitchToState(nOldState);
return 0;
}
//
// ID_BACKUP_RESTORE
//
2024-03-19 22:37:54 +01:00
static LRESULT OnBackupRestore(VOID)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
SwitchToState(SM_INVALID);
2024-03-19 22:24:30 +01:00
RestoreBackup();
2024-03-19 22:37:03 +01:00
if (pbyRom) SwitchToState(SM_RUN);
2024-03-19 22:24:30 +01:00
return 0;
}
//
// ID_BACKUP_DELETE
//
2024-03-19 22:37:54 +01:00
static LRESULT OnBackupDelete(VOID)
2024-03-19 22:24:30 +01:00
{
ResetBackup();
return 0;
}
//
// ID_OBJECT_LOAD
//
2024-03-19 22:37:54 +01:00
static LRESULT OnObjectLoad(VOID)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:54 +01:00
SuspendDebugger(); // suspend debugger
bDbgAutoStateCtrl = FALSE; // disable automatic debugger state control
// calculator off, turn on
if (!(Chipset.IORam[BITOFFSET]&DON))
2024-03-19 22:24:30 +01:00
{
KeyboardEvent(TRUE,0,0x8000);
KeyboardEvent(FALSE,0,0x8000);
2024-03-19 22:37:54 +01:00
// wait for sleep mode
while (Chipset.Shutdn == FALSE) Sleep(0);
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:37:03 +01:00
if (nState != SM_RUN)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
InfoMessage(_T("The emulator must be running to load an object."));
2024-03-19 22:37:54 +01:00
goto cancel;
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:37:54 +01:00
2024-03-19 22:25:45 +01:00
if (WaitForSleepState()) // wait for cpu SHUTDN then sleep state
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
InfoMessage(_T("The emulator is busy."));
2024-03-19 22:37:54 +01:00
goto cancel;
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:37:03 +01:00
_ASSERT(nState == SM_SLEEP);
2024-03-19 22:37:54 +01:00
2024-03-19 22:38:33 +01:00
if (bLoadObjectWarning)
2024-03-19 22:24:30 +01:00
{
UINT uReply = YesNoCancelMessage(
2024-03-19 22:37:03 +01:00
_T("Warning: Trying to load an object while the emulator is busy\n")
_T("will certainly result in a memory lost. Before loading an object\n")
_T("you should be sure that the calculator is not doing anything.\n")
_T("Do you want to see this warning next time you try to load an object ?"),0);
2024-03-19 22:24:30 +01:00
switch (uReply)
{
case IDYES:
break;
case IDNO:
2024-03-19 22:38:33 +01:00
bLoadObjectWarning = FALSE;
2024-03-19 22:24:30 +01:00
break;
case IDCANCEL:
2024-03-19 22:37:03 +01:00
SwitchToState(SM_RUN);
2024-03-19 22:37:54 +01:00
goto cancel;
2024-03-19 22:24:30 +01:00
}
}
2024-03-19 22:37:54 +01:00
if (!GetLoadObjectFilename(_T(HP_FILTER),_T("HP")))
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
SwitchToState(SM_RUN);
2024-03-19 22:37:54 +01:00
goto cancel;
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:37:54 +01:00
2024-03-19 22:24:30 +01:00
if (!LoadObject(szBufferFilename))
{
2024-03-19 22:37:03 +01:00
SwitchToState(SM_RUN);
2024-03-19 22:37:54 +01:00
goto cancel;
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:37:03 +01:00
SwitchToState(SM_RUN); // run state
2024-03-19 22:24:30 +01:00
while (nState!=nNextState) Sleep(0);
2024-03-19 22:37:03 +01:00
_ASSERT(nState == SM_RUN);
2024-03-19 22:24:30 +01:00
KeyboardEvent(TRUE,0,0x8000);
Sleep(200);
2024-03-19 22:24:30 +01:00
KeyboardEvent(FALSE,0,0x8000);
while (Chipset.Shutdn == FALSE) Sleep(0);
2024-03-19 22:37:54 +01:00
cancel:
bDbgAutoStateCtrl = TRUE; // enable automatic debugger state control
ResumeDebugger();
2024-03-19 22:24:30 +01:00
return 0;
}
//
// ID_OBJECT_SAVE
//
2024-03-19 22:37:54 +01:00
static LRESULT OnObjectSave(VOID)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
if (nState != SM_RUN)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
InfoMessage(_T("The emulator must be running to save an object."));
2024-03-19 22:24:30 +01:00
return 0;
}
2024-03-19 22:37:54 +01:00
2024-03-19 22:25:45 +01:00
if (WaitForSleepState()) // wait for cpu SHUTDN then sleep state
2024-03-19 22:36:03 +01:00
{
2024-03-19 22:37:03 +01:00
InfoMessage(_T("The emulator is busy."));
2024-03-19 22:24:30 +01:00
return 0;
2024-03-19 22:36:03 +01:00
}
2024-03-19 22:24:30 +01:00
2024-03-19 22:37:03 +01:00
_ASSERT(nState == SM_SLEEP);
2024-03-19 22:24:30 +01:00
if (GetSaveObjectFilename(_T(HP_FILTER),_T("HP")))
2024-03-19 22:24:30 +01:00
{
SaveObject(szBufferFilename);
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:37:54 +01:00
2024-03-19 22:37:03 +01:00
SwitchToState(SM_RUN);
2024-03-19 22:24:30 +01:00
return 0;
}
//
2024-03-19 22:25:45 +01:00
// ID_TOOL_DISASM
2024-03-19 22:24:30 +01:00
//
static INT_PTR CALLBACK Disasm(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
2024-03-19 22:24:30 +01:00
{
static DWORD dwAddress, dwAddressMax;
LONG i;
2024-03-19 22:37:03 +01:00
TCHAR *cpStop,szAddress[256] = _T("0");
2024-03-19 22:36:03 +01:00
2024-03-19 22:24:30 +01:00
switch (message)
{
case WM_INITDIALOG:
2024-03-19 22:25:45 +01:00
// set fonts & cursor
2024-03-19 22:24:30 +01:00
SendDlgItemMessage(hDlg,IDC_DISASM_MODULE,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(FALSE,0));
SendDlgItemMessage(hDlg,IDC_DISASM_MAP,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(FALSE,0));
SendDlgItemMessage(hDlg,IDC_DISASM_ROM,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(FALSE,0));
SendDlgItemMessage(hDlg,IDC_DISASM_RAM,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(FALSE,0));
SendDlgItemMessage(hDlg,IDC_DISASM_PORT1,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(FALSE,0));
SendDlgItemMessage(hDlg,IDC_DISASM_PORT2,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(FALSE,0));
SendDlgItemMessage(hDlg,IDC_DISASM_MAP,BM_SETCHECK,1,0);
SendDlgItemMessage(hDlg,IDC_ADDRESS,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(FALSE,0));
SendDlgItemMessage(hDlg,IDC_DISASM_ADR,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(FALSE,0));
SendDlgItemMessage(hDlg,IDC_DISASM_NEXT,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(FALSE,0));
SendDlgItemMessage(hDlg,IDC_DISASM_COPY,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(FALSE,0));
SendDlgItemMessage(hDlg,IDCANCEL,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(FALSE,0));
SetDlgItemText(hDlg,IDC_DISASM_ADR,szAddress);
2024-03-19 22:25:45 +01:00
disassembler_map = MEM_MAP; // disassemble with mapped modules
2024-03-19 22:37:03 +01:00
dwAddress = _tcstoul(szAddress,&cpStop,16);
2024-03-19 22:25:45 +01:00
dwAddressMax = 0x100000; // greatest address (mapped mode)
2024-03-19 22:24:30 +01:00
return TRUE;
case WM_COMMAND:
2024-03-19 22:38:33 +01:00
switch(LOWORD(wParam))
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:25:45 +01:00
// decode radio buttons
2024-03-19 22:24:30 +01:00
case IDC_DISASM_MAP:
disassembler_map = MEM_MAP;
dwAddressMax = 0x100000;
2024-03-19 22:25:45 +01:00
return TRUE;
2024-03-19 22:24:30 +01:00
case IDC_DISASM_ROM:
disassembler_map = MEM_ROM;
dwAddressMax = dwRomSize;
2024-03-19 22:25:45 +01:00
return TRUE;
2024-03-19 22:24:30 +01:00
case IDC_DISASM_RAM:
disassembler_map = MEM_RAM;
dwAddressMax = Chipset.Port0Size * 2048;
2024-03-19 22:25:45 +01:00
return TRUE;
2024-03-19 22:24:30 +01:00
case IDC_DISASM_PORT1:
disassembler_map = MEM_PORT1;
dwAddressMax = ((Chipset.cards_status & PORT1_PRESENT) != 0) ? (Chipset.Port1Size * 2048) : 0;
2024-03-19 22:25:45 +01:00
return TRUE;
2024-03-19 22:24:30 +01:00
case IDC_DISASM_PORT2:
disassembler_map = MEM_PORT2;
2024-03-19 22:37:54 +01:00
dwAddressMax = ((cCurrentRomType=='E' || cCurrentRomType=='X')
? Chipset.Port2Size
: dwPort2Size)
* 2048;
2024-03-19 22:25:45 +01:00
return TRUE;
2024-03-19 22:24:30 +01:00
case IDOK:
SendDlgItemMessage(hDlg,IDC_DISASM_ADR,EM_SETSEL,0,-1);
2024-03-19 22:37:03 +01:00
GetDlgItemText(hDlg,IDC_DISASM_ADR,szAddress,ARRAYSIZEOF(szAddress));
2024-03-19 22:24:30 +01:00
// test if valid hex address
2024-03-19 22:37:03 +01:00
for (i = 0; i < (LONG) lstrlen(szAddress); ++i)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
if (_istxdigit(szAddress[i]) == FALSE)
2024-03-19 22:24:30 +01:00
return FALSE;
}
2024-03-19 22:37:03 +01:00
dwAddress = _tcstoul(szAddress,&cpStop,16);
2024-03-19 22:24:30 +01:00
// no break
case IDC_DISASM_NEXT:
2024-03-19 22:25:45 +01:00
if (dwAddress >= dwAddressMax)
2024-03-19 22:24:30 +01:00
return FALSE;
2024-03-19 22:37:03 +01:00
i = wsprintf(szAddress,(dwAddress <= 0xFFFFF) ? _T("%05lX ") : _T("%06lX "),dwAddress);
2024-03-19 22:25:45 +01:00
dwAddress = disassemble(dwAddress,&szAddress[i],VIEW_LONG);
i = (LONG) SendDlgItemMessage(hDlg,IDC_DISASM_WIN,LB_ADDSTRING,0,(LPARAM) szAddress);
2024-03-19 22:24:30 +01:00
SendDlgItemMessage(hDlg,IDC_DISASM_WIN,LB_SELITEMRANGE,FALSE,MAKELPARAM(0,i));
SendDlgItemMessage(hDlg,IDC_DISASM_WIN,LB_SETSEL,TRUE,i);
SendDlgItemMessage(hDlg,IDC_DISASM_WIN,LB_SETTOPINDEX,i,0);
2024-03-19 22:25:45 +01:00
return TRUE;
2024-03-19 22:24:30 +01:00
case IDC_DISASM_COPY:
2024-03-19 22:36:03 +01:00
// copy selected items to clipboard
CopyItemsToClipboard(GetDlgItem(hDlg,IDC_DISASM_WIN));
2024-03-19 22:25:45 +01:00
return TRUE;
2024-03-19 22:24:30 +01:00
case IDCANCEL:
2024-03-19 22:38:33 +01:00
EndDialog(hDlg,IDCANCEL);
2024-03-19 22:25:45 +01:00
return TRUE;
2024-03-19 22:24:30 +01:00
}
break;
}
return FALSE;
UNREFERENCED_PARAMETER(lParam);
2024-03-19 22:24:30 +01:00
}
//
// ID_ABOUT
//
static INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
2024-03-19 22:24:30 +01:00
{
switch (message)
{
case WM_INITDIALOG:
SetDlgItemText(hDlg,IDC_VERSION,szNoTitle);
SetDlgItemText(hDlg,IDC_LICENSE,szLicence);
return TRUE;
case WM_COMMAND:
2024-03-19 22:38:33 +01:00
wParam = LOWORD(wParam);
2024-03-19 22:24:30 +01:00
if ((wParam==IDOK)||(wParam==IDCANCEL))
{
EndDialog(hDlg, wParam);
2024-03-19 22:25:45 +01:00
return TRUE;
2024-03-19 22:24:30 +01:00
}
break;
}
return FALSE;
UNREFERENCED_PARAMETER(lParam);
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:37:54 +01:00
static LRESULT OnToolDisasm(VOID) // disasm dialogbox call
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:37:03 +01:00
if (pbyRom) SwitchToState(SM_SLEEP);
2024-03-19 22:24:30 +01:00
if (DialogBox(hApp, MAKEINTRESOURCE(IDD_DISASM), hWnd, (DLGPROC)Disasm) == -1)
2024-03-19 22:37:03 +01:00
AbortMessage(_T("Disassembler Dialog Box Creation Error !"));
if (pbyRom) SwitchToState(SM_RUN);
2024-03-19 22:24:30 +01:00
return 0;
}
2024-03-19 22:37:54 +01:00
static LRESULT OnAbout(VOID)
2024-03-19 22:24:30 +01:00
{
if (DialogBox(hApp, MAKEINTRESOURCE(IDD_ABOUT), hWnd, (DLGPROC)About) == -1)
2024-03-19 22:37:03 +01:00
AbortMessage(_T("About Dialog Box Creation Error !"));
2024-03-19 22:24:30 +01:00
return 0;
}
static LRESULT OnLButtonDown(UINT nFlags, WORD x, WORD y)
{
if (nMacroState == MACRO_PLAY) return 0; // playing macro
2024-03-19 22:37:03 +01:00
if (nState == SM_RUN) MouseButtonDownAt(nFlags, x,y);
2024-03-19 22:24:30 +01:00
return 0;
}
2024-03-19 22:37:54 +01:00
static LRESULT OnLButtonUp(UINT nFlags, WORD x, WORD y)
2024-03-19 22:24:30 +01:00
{
if (nMacroState == MACRO_PLAY) return 0; // playing macro
2024-03-19 22:37:03 +01:00
if (nState == SM_RUN) MouseButtonUpAt(nFlags, x,y);
2024-03-19 22:24:30 +01:00
return 0;
}
static LRESULT OnMouseMove(UINT nFlags, WORD x, WORD y)
{
// emulator not active but cursor is over emulator window
if (bActFollowsMouse && GetActiveWindow() != hWnd)
{
ForceForegroundWindow(hWnd); // force emulator window to foreground
}
if (nMacroState == MACRO_PLAY) return 0; // playing macro
2024-03-19 22:37:03 +01:00
if (nState == SM_RUN) MouseMovesTo(nFlags, x,y);
2024-03-19 22:24:30 +01:00
return 0;
}
static LRESULT OnNcMouseMove(UINT nFlags, WORD x, WORD y)
{
// emulator not active but cursor is over emulator window
if (bActFollowsMouse && GetActiveWindow() != hWnd)
{
ForceForegroundWindow(hWnd); // force emulator window to foreground
}
return 0;
UNREFERENCED_PARAMETER(nFlags);
UNREFERENCED_PARAMETER(x);
UNREFERENCED_PARAMETER(y);
}
static LRESULT OnKeyDown(int nVirtKey, LPARAM lKeyData)
2024-03-19 22:24:30 +01:00
{
if (nMacroState == MACRO_PLAY) return 0; // playing macro
2024-03-19 22:25:45 +01:00
// call RunKey() only once (suppress autorepeat feature)
2024-03-19 22:37:03 +01:00
if (nState == SM_RUN && (lKeyData & 0x40000000) == 0)
2024-03-19 22:24:30 +01:00
RunKey((BYTE)nVirtKey, TRUE);
return 0;
}
static LRESULT OnKeyUp(int nVirtKey, LPARAM lKeyData)
2024-03-19 22:24:30 +01:00
{
if (nMacroState == MACRO_PLAY) return 0; // playing macro
2024-03-19 22:37:03 +01:00
if (nState == SM_RUN) RunKey((BYTE)nVirtKey, FALSE);
2024-03-19 22:24:30 +01:00
return 0;
UNREFERENCED_PARAMETER(lKeyData);
}
2024-03-19 22:25:45 +01:00
LRESULT CALLBACK MainWndProc(HWND hWindow, UINT uMsg, WPARAM wParam, LPARAM lParam)
2024-03-19 22:24:30 +01:00
{
switch (uMsg)
{
case WM_CREATE: return OnCreate(hWindow);
case WM_DESTROY: return OnDestroy(hWindow);
case WM_PAINT: return OnPaint(hWindow);
case WM_DROPFILES: return OnDropFiles((HANDLE)wParam);
case WM_ACTIVATE:
if (LOWORD(wParam)==WA_INACTIVE) break;
case WM_QUERYNEWPALETTE:
if (hPalette)
{
SelectPalette(hWindowDC, hPalette, FALSE);
if (RealizePalette(hWindowDC))
{
InvalidateRect(hWindow,NULL,TRUE);
return TRUE;
}
}
return FALSE;
case WM_PALETTECHANGED:
if ((HWND)wParam == hWindow) break;
if (hPalette)
{
SelectPalette(hWindowDC, hPalette, FALSE);
if (RealizePalette(hWindowDC))
{
// UpdateColors(hWindowDC);
InvalidateRect (hWnd, (LPRECT) (NULL), 1);
}
}
return FALSE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_FILE_NEW: return OnFileNew();
case ID_FILE_OPEN: return OnFileOpen();
case ID_FILE_SAVE: return OnFileSave();
case ID_FILE_SAVEAS: return OnFileSaveAs();
case ID_FILE_CLOSE: return OnFileClose();
case ID_FILE_EXIT: return OnFileExit();
case ID_STACK_COPY: return OnStackCopy();
case ID_STACK_PASTE: return OnStackPaste();
case ID_VIEW_COPY: return OnViewCopy();
case ID_VIEW_RESET: return OnViewReset();
case ID_VIEW_SETTINGS: return OnViewSettings();
case ID_VIEW_SCRIPT: return OnViewScript();
case ID_BACKUP_SAVE: return OnBackupSave();
case ID_BACKUP_RESTORE: return OnBackupRestore();
case ID_BACKUP_DELETE: return OnBackupDelete();
case ID_OBJECT_LOAD: return OnObjectLoad();
case ID_OBJECT_SAVE: return OnObjectSave();
case ID_TOOL_DISASM: return OnToolDisasm();
case ID_TOOL_DEBUG: return OnToolDebug();
case ID_TOOL_MACRO_RECORD: return OnToolMacroNew();
case ID_TOOL_MACRO_PLAY: return OnToolMacroPlay();
case ID_TOOL_MACRO_STOP: return OnToolMacroStop();
case ID_TOOL_MACRO_SETTINGS: return OnToolMacroSettings();
case ID_ABOUT: return OnAbout();
2024-03-19 22:24:30 +01:00
}
// check if command ID belongs to MRU file area
if ( (LOWORD(wParam) >= ID_FILE_MRU_FILE1)
&& (LOWORD(wParam) < ID_FILE_MRU_FILE1 + MruEntries()))
return OnFileMruOpen(LOWORD(wParam));
2024-03-19 22:24:30 +01:00
break;
case WM_SYSCOMMAND:
2024-03-19 22:38:33 +01:00
switch (wParam & 0xFFF0)
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:25:45 +01:00
case SC_CLOSE: return OnFileExit();
2024-03-19 22:24:30 +01:00
}
break;
2024-03-19 22:25:45 +01:00
case WM_RBUTTONDOWN:
case WM_LBUTTONDOWN: return OnLButtonDown((UINT) wParam, LOWORD(lParam), HIWORD(lParam));
case WM_LBUTTONUP: return OnLButtonUp((UINT) wParam, LOWORD(lParam), HIWORD(lParam));
case WM_MOUSEMOVE: return OnMouseMove((UINT) wParam, LOWORD(lParam), HIWORD(lParam));
case WM_NCMOUSEMOVE: return OnNcMouseMove((UINT) wParam, LOWORD(lParam), HIWORD(lParam));
2024-03-19 22:24:30 +01:00
case WM_KEYUP: return OnKeyUp((int)wParam, lParam);
case WM_KEYDOWN: return OnKeyDown((int)wParam, lParam);
}
return DefWindowProc(hWindow, uMsg, wParam, lParam);
}
2024-03-19 22:25:45 +01:00
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
2024-03-19 22:24:30 +01:00
{
MSG msg;
WNDCLASS wc;
RECT rectWindow;
HACCEL hAccel;
2024-03-19 22:25:45 +01:00
HSZ hszService, hszTopic; // variables for DDE server
DWORD_PTR dwAffMask;
LPTSTR lpFilePart;
2024-03-19 22:24:30 +01:00
hApp = hInst;
2024-03-19 22:37:03 +01:00
#if defined _UNICODE
{
ppArgv = CommandLineToArgvW(GetCommandLine(),&nArgc);
}
#else
{
nArgc = __argc; // no. of command line arguments
ppArgv = (LPCTSTR*) __argv; // command line arguments
}
#endif
2024-03-19 22:38:33 +01:00
hHeap = GetProcessHeap();
if (hHeap == NULL)
{
AbortMessage(_T("Heap creation failed."));
return FALSE;
}
2024-03-19 22:24:30 +01:00
wc.style = CS_BYTEALIGNCLIENT;
wc.lpfnWndProc = (WNDPROC)MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_EMU48));
2024-03-19 22:38:33 +01:00
wc.hCursor = NULL;
2024-03-19 22:24:30 +01:00
wc.hbrBackground = NULL;
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
2024-03-19 22:37:03 +01:00
wc.lpszClassName = _T("CEmu48");
2024-03-19 22:37:54 +01:00
2024-03-19 22:24:30 +01:00
if (!RegisterClass(&wc))
{
AbortMessage(
2024-03-19 22:37:03 +01:00
_T("CEmu48 class registration failed.\n")
_T("This application will now terminate."));
2024-03-19 22:24:30 +01:00
return FALSE;
}
2024-03-19 22:37:54 +01:00
2024-03-19 22:24:30 +01:00
// Create window
rectWindow.left = 0;
rectWindow.top = 0;
rectWindow.right = 256;
rectWindow.bottom = 0;
AdjustWindowRect(&rectWindow, WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|WS_OVERLAPPED, TRUE);
2024-03-19 22:37:54 +01:00
2024-03-19 22:37:03 +01:00
hWnd = CreateWindow(_T("CEmu48"), _T("Emu48"),
2024-03-19 22:24:30 +01:00
WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|WS_OVERLAPPED,
CW_USEDEFAULT, CW_USEDEFAULT,
rectWindow.right - rectWindow.left,
rectWindow.bottom - rectWindow.top,
NULL,NULL,hApp,NULL
);
2024-03-19 22:37:54 +01:00
2024-03-19 22:24:30 +01:00
if (hWnd == NULL)
{
2024-03-19 22:37:03 +01:00
AbortMessage(_T("Window creation failed."));
2024-03-19 22:24:30 +01:00
return FALSE;
}
2024-03-19 22:37:54 +01:00
VERIFY(hAccel = LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_MENU)));
2024-03-19 22:24:30 +01:00
// initialization
2024-03-19 22:25:45 +01:00
QueryPerformanceFrequency(&lFreq); // init high resolution counter
2024-03-19 22:36:03 +01:00
QueryPerformanceCounter(&lAppStart);
2024-03-19 22:24:30 +01:00
2024-03-19 22:37:03 +01:00
GetCurrentDirectory(ARRAYSIZEOF(szCurrentDirectory), szCurrentDirectory);
szCurrentKml[0] = 0; // no KML file selected
2024-03-19 22:24:30 +01:00
ReadSettings();
MruInit(4); // init MRU entries
2024-03-19 22:37:54 +01:00
2024-03-19 22:24:30 +01:00
UpdateWindowStatus();
2024-03-19 22:25:45 +01:00
// create auto event handle
hEventShutdn = CreateEvent(NULL,FALSE,FALSE,NULL);
if (hEventShutdn == NULL)
{
2024-03-19 22:37:03 +01:00
AbortMessage(_T("Event creation failed."));
2024-03-19 22:36:03 +01:00
DestroyWindow(hWnd);
2024-03-19 22:25:45 +01:00
return FALSE;
}
2024-03-19 22:37:03 +01:00
nState = SM_RUN; // init state must be <> nNextState
nNextState = SM_INVALID; // go into invalid state
2024-03-19 22:36:03 +01:00
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&WorkerThread, NULL, CREATE_SUSPENDED, &lThreadId);
2024-03-19 22:24:30 +01:00
if (hThread == NULL)
{
2024-03-19 22:25:45 +01:00
CloseHandle(hEventShutdn); // close event handle
2024-03-19 22:37:03 +01:00
AbortMessage(_T("Thread creation failed."));
2024-03-19 22:36:03 +01:00
DestroyWindow(hWnd);
2024-03-19 22:24:30 +01:00
return FALSE;
}
2024-03-19 22:36:03 +01:00
// on multiprocessor machines for QueryPerformanceCounter()
dwAffMask = SetThreadAffinityMask(hThread,1);
_ASSERT(dwAffMask != 0);
ResumeThread(hThread); // start thread
while (nState!=nNextState) Sleep(0); // wait for thread initialized
2024-03-19 22:37:54 +01:00
2024-03-19 22:37:03 +01:00
idDdeInst = 0; // initialize DDE server
2024-03-19 22:24:30 +01:00
if (DdeInitialize(&idDdeInst,(PFNCALLBACK) &DdeCallback,
APPCLASS_STANDARD |
CBF_FAIL_EXECUTES | CBF_FAIL_ADVISES |
CBF_SKIP_REGISTRATIONS | CBF_SKIP_UNREGISTRATIONS,0))
{
2024-03-19 22:36:03 +01:00
TerminateThread(hThread, 0); // kill emulation thread
CloseHandle(hEventShutdn); // close event handle
2024-03-19 22:37:03 +01:00
AbortMessage(_T("Could not initialize server!"));
2024-03-19 22:36:03 +01:00
DestroyWindow(hWnd);
2024-03-19 22:24:30 +01:00
return FALSE;
}
// init clipboard format and name service
uCF_HpObj = RegisterClipboardFormat(_T(CF_HPOBJ));
hszService = DdeCreateStringHandle(idDdeInst,szAppName,0);
hszTopic = DdeCreateStringHandle(idDdeInst,szTopic,0);
DdeNameService(idDdeInst,hszService,NULL,DNS_REGISTER);
2024-03-19 22:36:03 +01:00
_ASSERT(hWnd != NULL);
_ASSERT(hWindowDC != NULL);
2024-03-19 22:25:45 +01:00
if (nArgc >= 2) // use decoded parameter line
lstrcpyn(szBufferFilename,ppArgv[1],ARRAYSIZEOF(szBufferFilename));
else // use last document setting
ReadLastDocument(szBufferFilename, ARRAYSIZEOF(szBufferFilename));
2024-03-19 22:24:30 +01:00
if (szBufferFilename[0]) // given default document
2024-03-19 22:24:30 +01:00
{
2024-03-19 22:38:33 +01:00
TCHAR szTemp[MAX_PATH+8] = _T("Loading ");
RECT rectClient;
_ASSERT(hWnd != NULL);
VERIFY(GetClientRect(hWnd,&rectClient));
GetCutPathName(szBufferFilename,&szTemp[8],MAX_PATH,rectClient.right/11);
2024-03-19 22:24:30 +01:00
SetWindowTitle(szTemp);
if (OpenDocument(szBufferFilename))
{
MruAdd(szCurrentFilename);
ShowWindow(hWnd,nCmdShow);
2024-03-19 22:24:30 +01:00
goto start;
}
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:25:45 +01:00
SetWindowTitle(_T("New Document"));
ShowWindow(hWnd,nCmdShow); // show emulator menu
// no default document, ask for new one
if (NewDocument()) SetWindowTitle(_T("Untitled"));
2024-03-19 22:37:54 +01:00
2024-03-19 22:24:30 +01:00
start:
if (bStartupBackup) SaveBackup(); // make a RAM backup at startup
2024-03-19 22:37:03 +01:00
if (pbyRom) SwitchToState(SM_RUN);
2024-03-19 22:37:54 +01:00
2024-03-19 22:24:30 +01:00
while (GetMessage(&msg, NULL, 0, 0))
{
if ( !TranslateAccelerator(hWnd, hAccel, &msg)
&& (hDlgDebug == NULL || !IsDialogMessage(hDlgDebug, &msg))
&& (hDlgFind == NULL || !IsDialogMessage(hDlgFind, &msg))
&& (hDlgProfile == NULL || !IsDialogMessage(hDlgProfile, &msg)))
2024-03-19 22:25:45 +01:00
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
2024-03-19 22:24:30 +01:00
}
2024-03-19 22:25:45 +01:00
// clean up DDE server
2024-03-19 22:24:30 +01:00
DdeNameService(idDdeInst, hszService, NULL, DNS_UNREGISTER);
DdeFreeStringHandle(idDdeInst, hszService);
DdeFreeStringHandle(idDdeInst, hszTopic);
DdeUninitialize(idDdeInst);
// get full path name of szCurrentFilename
GetFullPathName(szCurrentFilename,ARRAYSIZEOF(szBufferFilename),szBufferFilename,&lpFilePart);
WriteLastDocument(szBufferFilename); // save last document setting
2024-03-19 22:36:03 +01:00
WriteSettings(); // save emulation settings
2024-03-19 22:37:54 +01:00
2024-03-19 22:37:03 +01:00
CloseHandle(hThread); // close thread handle
2024-03-19 22:25:45 +01:00
CloseHandle(hEventShutdn); // close event handle
2024-03-19 22:37:03 +01:00
_ASSERT(nState == SM_RETURN); // emulation thread down?
2024-03-19 22:24:30 +01:00
ResetDocument();
ResetBackup();
MruCleanup();
_ASSERT(pbyRom == NULL); // rom file unmapped
2024-03-19 22:25:45 +01:00
_ASSERT(pbyPort2 == NULL); // port2 file unmapped
_ASSERT(pKml == NULL); // KML script not closed
_ASSERT(szTitle == NULL); // freed allocated memory
_ASSERT(hPalette == NULL); // freed resource memory
2024-03-19 22:37:54 +01:00
return (int) msg.wParam;
2024-03-19 22:24:30 +01:00
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(hPrevInst);
}