2024-03-19 22:22:29 +01:00
|
|
|
/*
|
|
|
|
* files.c
|
|
|
|
*
|
|
|
|
* This file is part of Emu48
|
|
|
|
*
|
|
|
|
* Copyright (C) 1995 Sebastien Carlier
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "pch.h"
|
|
|
|
#include "Emu48.h"
|
2024-03-19 22:37:03 +01:00
|
|
|
#include "ops.h"
|
2024-03-19 22:36:03 +01:00
|
|
|
#include "io.h" // I/O register definitions
|
2024-03-19 22:22:29 +01:00
|
|
|
#include "kml.h"
|
2024-03-19 22:36:03 +01:00
|
|
|
#include "i28f160.h" // flash support
|
2024-03-19 22:37:03 +01:00
|
|
|
#include "debugger.h"
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
#pragma intrinsic(abs,labs)
|
|
|
|
|
2024-03-19 22:38:33 +01:00
|
|
|
TCHAR szEmuDirectory[MAX_PATH];
|
|
|
|
TCHAR szCurrentDirectory[MAX_PATH];
|
|
|
|
TCHAR szCurrentKml[MAX_PATH];
|
|
|
|
TCHAR szBackupKml[MAX_PATH];
|
|
|
|
TCHAR szCurrentFilename[MAX_PATH];
|
|
|
|
TCHAR szBackupFilename[MAX_PATH];
|
|
|
|
TCHAR szBufferFilename[MAX_PATH];
|
|
|
|
TCHAR szPort2Filename[MAX_PATH];
|
2024-03-19 22:37:03 +01:00
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
BOOL bDocumentAvail = FALSE; // document not available
|
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
BYTE cCurrentRomType = 0; // Model -> hardware
|
|
|
|
UINT nCurrentClass = 0; // Class -> derivate
|
2024-03-19 23:35:29 +01:00
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
LPBYTE Port0 = NULL;
|
|
|
|
LPBYTE Port1 = NULL;
|
|
|
|
LPBYTE Port2 = NULL;
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
LPBYTE pbyRom = NULL;
|
2024-03-19 22:37:03 +01:00
|
|
|
BOOL bRomWriteable = TRUE; // flag if ROM writeable
|
2024-03-19 23:35:29 +01:00
|
|
|
DWORD dwRomSize = 0;
|
2024-03-19 23:35:30 +01:00
|
|
|
LPBYTE pbyRomDirtyPage = NULL;
|
|
|
|
DWORD dwRomDirtyPageSize = 0;
|
2024-03-19 23:35:29 +01:00
|
|
|
WORD wRomCrc = 0; // fingerprint of patched ROM
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
LPBYTE pbyPort2 = NULL;
|
|
|
|
BOOL bPort2Writeable = FALSE;
|
|
|
|
BOOL bPort2IsShared = FALSE;
|
|
|
|
DWORD dwPort2Size = 0; // size of mapped port2
|
|
|
|
DWORD dwPort2Mask = 0;
|
2024-03-19 23:35:29 +01:00
|
|
|
WORD wPort2Crc = 0; // fingerprint of port2
|
|
|
|
|
|
|
|
BOOL bBackup = FALSE;
|
|
|
|
|
|
|
|
static HANDLE hRomFile = NULL;
|
|
|
|
static HANDLE hPort2File = NULL;
|
|
|
|
static HANDLE hPort2Map = NULL;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 22:36:03 +01:00
|
|
|
// document signatures
|
2024-03-19 22:25:45 +01:00
|
|
|
static BYTE pbySignatureA[16] = "Emu38 Document\xFE";
|
2024-03-19 22:37:03 +01:00
|
|
|
static BYTE pbySignatureB[16] = "Emu39 Document\xFE";
|
2024-03-19 22:22:29 +01:00
|
|
|
static BYTE pbySignatureE[16] = "Emu48 Document\xFE";
|
|
|
|
static BYTE pbySignatureW[16] = "Win48 Document\xFE";
|
2024-03-19 22:25:45 +01:00
|
|
|
static BYTE pbySignatureV[16] = "Emu49 Document\xFE";
|
|
|
|
static HANDLE hCurrentFile = NULL;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
|
|
|
static CHIPSET BackupChipset;
|
2024-03-19 23:35:30 +01:00
|
|
|
static LPBYTE BackupPort0;
|
|
|
|
static LPBYTE BackupPort1;
|
|
|
|
static LPBYTE BackupPort2;
|
2024-03-19 23:35:30 +01:00
|
|
|
static BOOL bRomPacked;
|
2024-03-19 22:37:54 +01:00
|
|
|
|
|
|
|
//################
|
|
|
|
//#
|
|
|
|
//# Window Position Tools
|
|
|
|
//#
|
|
|
|
//################
|
|
|
|
|
|
|
|
VOID SetWindowLocation(HWND hWnd,INT nPosX,INT nPosY)
|
|
|
|
{
|
|
|
|
WINDOWPLACEMENT wndpl;
|
|
|
|
RECT *pRc = &wndpl.rcNormalPosition;
|
|
|
|
|
|
|
|
wndpl.length = sizeof(wndpl);
|
|
|
|
GetWindowPlacement(hWnd,&wndpl);
|
|
|
|
pRc->right = pRc->right - pRc->left + nPosX;
|
|
|
|
pRc->bottom = pRc->bottom - pRc->top + nPosY;
|
|
|
|
pRc->left = nPosX;
|
|
|
|
pRc->top = nPosY;
|
|
|
|
SetWindowPlacement(hWnd,&wndpl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
//################
|
|
|
|
//#
|
|
|
|
//# Filename Title Helper Tool
|
|
|
|
//#
|
|
|
|
//################
|
|
|
|
|
|
|
|
DWORD GetCutPathName(LPCTSTR szFileName, LPTSTR szBuffer, DWORD dwBufferLength, INT nCutLength)
|
|
|
|
{
|
|
|
|
TCHAR cPath[_MAX_PATH]; // full filename
|
|
|
|
TCHAR cDrive[_MAX_DRIVE];
|
|
|
|
TCHAR cDir[_MAX_DIR];
|
|
|
|
TCHAR cFname[_MAX_FNAME];
|
|
|
|
TCHAR cExt[_MAX_EXT];
|
|
|
|
|
|
|
|
_ASSERT(nCutLength >= 0); // 0 = only drive and name
|
|
|
|
|
|
|
|
// split original filename into parts
|
|
|
|
_tsplitpath(szFileName,cDrive,cDir,cFname,cExt);
|
|
|
|
|
|
|
|
if (*cDir != 0) // contain directory part
|
|
|
|
{
|
|
|
|
LPTSTR lpFilePart; // address of file name in path
|
|
|
|
INT nNameLen,nPathLen,nMaxPathLen;
|
|
|
|
|
|
|
|
GetFullPathName(szFileName,ARRAYSIZEOF(cPath),cPath,&lpFilePart);
|
|
|
|
_tsplitpath(cPath,cDrive,cDir,cFname,cExt);
|
|
|
|
|
|
|
|
// calculate size of drive/name and path
|
|
|
|
nNameLen = lstrlen(cDrive) + lstrlen(cFname) + lstrlen(cExt);
|
|
|
|
nPathLen = lstrlen(cDir);
|
|
|
|
|
|
|
|
// maximum length for path
|
|
|
|
nMaxPathLen = nCutLength - nNameLen;
|
|
|
|
|
|
|
|
if (nPathLen > nMaxPathLen) // have to cut path
|
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
TCHAR cDirTemp[_MAX_DIR] = _T("");
|
2024-03-19 23:35:29 +01:00
|
|
|
LPTSTR szPtr;
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
// UNC name
|
|
|
|
if (cDir[0] == _T('\\') && cDir[1] == _T('\\'))
|
|
|
|
{
|
|
|
|
// skip server
|
|
|
|
if ((szPtr = _tcschr(cDir + 2,_T('\\'))) != NULL)
|
|
|
|
{
|
|
|
|
// skip share
|
|
|
|
if ((szPtr = _tcschr(szPtr + 1,_T('\\'))) != NULL)
|
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
INT nLength = (INT) (szPtr - cDir);
|
2024-03-19 23:35:29 +01:00
|
|
|
|
|
|
|
*szPtr = 0; // set EOS behind share
|
|
|
|
|
|
|
|
// enough room for \\server\\share and "\...\"
|
|
|
|
if (nLength + 5 <= nMaxPathLen)
|
|
|
|
{
|
|
|
|
lstrcpyn(cDirTemp,cDir,ARRAYSIZEOF(cDirTemp));
|
|
|
|
nMaxPathLen -= nLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lstrcat(cDirTemp,_T("\\..."));
|
|
|
|
nMaxPathLen -= 5; // need 6 chars for additional "\..." + "\"
|
2024-03-19 23:35:29 +01:00
|
|
|
if (nMaxPathLen < 0) nMaxPathLen = 0;
|
|
|
|
|
|
|
|
// get earliest possible '\' character
|
|
|
|
szPtr = &cDir[nPathLen - nMaxPathLen];
|
|
|
|
szPtr = _tcschr(szPtr,_T('\\'));
|
|
|
|
// not found
|
|
|
|
if (szPtr == NULL) szPtr = _T("");
|
|
|
|
|
|
|
|
lstrcat(cDirTemp,szPtr); // copy path with preample to dir buffer
|
|
|
|
lstrcpyn(cDir,cDirTemp,ARRAYSIZEOF(cDir));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_tmakepath(cPath,cDrive,cDir,cFname,cExt);
|
|
|
|
lstrcpyn(szBuffer,cPath,dwBufferLength);
|
|
|
|
return lstrlen(szBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID SetWindowPathTitle(LPCTSTR szFileName)
|
|
|
|
{
|
|
|
|
TCHAR cPath[MAX_PATH];
|
|
|
|
RECT rectClient;
|
|
|
|
|
|
|
|
if (*szFileName != 0) // set new title
|
|
|
|
{
|
|
|
|
_ASSERT(hWnd != NULL);
|
|
|
|
VERIFY(GetClientRect(hWnd,&rectClient));
|
2024-03-19 23:35:29 +01:00
|
|
|
GetCutPathName(szFileName,cPath,ARRAYSIZEOF(cPath),rectClient.right/11);
|
2024-03-19 23:35:29 +01:00
|
|
|
SetWindowTitle(cPath);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-03-19 22:36:03 +01:00
|
|
|
//################
|
|
|
|
//#
|
|
|
|
//# Patch
|
|
|
|
//#
|
|
|
|
//################
|
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
static __inline BYTE Asc2Nib(BYTE c)
|
2024-03-19 22:36:03 +01:00
|
|
|
{
|
|
|
|
if (c<'0') return 0;
|
|
|
|
if (c<='9') return c-'0';
|
|
|
|
if (c<'A') return 0;
|
|
|
|
if (c<='F') return c-'A'+10;
|
|
|
|
if (c<'a') return 0;
|
|
|
|
if (c<='f') return c-'a'+10;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// functions to restore ROM patches
|
|
|
|
typedef struct tnode
|
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
BOOL bPatch; // TRUE = ROM address patched
|
2024-03-19 22:36:03 +01:00
|
|
|
DWORD dwAddress; // patch address
|
|
|
|
BYTE byROM; // original ROM value
|
|
|
|
BYTE byPatch; // patched ROM value
|
2024-03-19 23:35:30 +01:00
|
|
|
struct tnode *prev; // previous node
|
2024-03-19 22:36:03 +01:00
|
|
|
struct tnode *next; // next node
|
2024-03-19 23:35:30 +01:00
|
|
|
} TREENODE, *PTREENODE;
|
2024-03-19 22:36:03 +01:00
|
|
|
|
|
|
|
static TREENODE *nodePatch = NULL;
|
|
|
|
|
|
|
|
static BOOL PatchNibble(DWORD dwAddress, BYTE byPatch)
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
PTREENODE p;
|
2024-03-19 22:36:03 +01:00
|
|
|
|
|
|
|
_ASSERT(pbyRom); // ROM defined
|
2024-03-19 23:35:30 +01:00
|
|
|
if ((p = (PTREENODE) malloc(sizeof(TREENODE))) == NULL)
|
2024-03-19 22:36:03 +01:00
|
|
|
return TRUE;
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
p->bPatch = TRUE; // address patched
|
2024-03-19 22:36:03 +01:00
|
|
|
p->dwAddress = dwAddress; // save current values
|
|
|
|
p->byROM = pbyRom[dwAddress];
|
|
|
|
p->byPatch = byPatch;
|
2024-03-19 23:35:30 +01:00
|
|
|
p->prev = NULL;
|
2024-03-19 22:36:03 +01:00
|
|
|
p->next = nodePatch; // save node
|
2024-03-19 23:35:30 +01:00
|
|
|
|
|
|
|
if (nodePatch) nodePatch->prev = p; // add as previous element
|
2024-03-19 22:36:03 +01:00
|
|
|
nodePatch = p;
|
|
|
|
|
|
|
|
pbyRom[dwAddress] = byPatch; // patch ROM
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID RestorePatches(VOID)
|
|
|
|
{
|
|
|
|
TREENODE *p;
|
|
|
|
|
|
|
|
_ASSERT(pbyRom); // ROM defined
|
|
|
|
while (nodePatch != NULL)
|
|
|
|
{
|
|
|
|
// restore original data
|
|
|
|
pbyRom[nodePatch->dwAddress] = nodePatch->byROM;
|
|
|
|
|
|
|
|
p = nodePatch->next; // save pointer to next node
|
2024-03-19 23:35:30 +01:00
|
|
|
free(nodePatch); // free node
|
2024-03-19 22:36:03 +01:00
|
|
|
nodePatch = p; // new node
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID UpdatePatches(BOOL bPatch)
|
|
|
|
{
|
|
|
|
TREENODE *p = nodePatch;
|
|
|
|
|
|
|
|
_ASSERT(pbyRom); // ROM defined
|
2024-03-19 23:35:30 +01:00
|
|
|
if (bPatch) // patch ROM
|
2024-03-19 22:36:03 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
if (p) // something in patch list
|
2024-03-19 22:36:03 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
// goto last element in list
|
|
|
|
for (; p->next != NULL; p = p->next) {}
|
2024-03-19 23:35:29 +01:00
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
do
|
2024-03-19 23:35:29 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
if (!p->bPatch) // patch only if not patched
|
|
|
|
{
|
|
|
|
// use original data for patch restore
|
|
|
|
p->byROM = pbyRom[p->dwAddress];
|
|
|
|
|
|
|
|
// restore patch data
|
|
|
|
pbyRom[p->dwAddress] = p->byPatch;
|
|
|
|
p->bPatch = TRUE; // address patched
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_ASSERT(FALSE); // call ROM patch on a patched ROM
|
|
|
|
}
|
|
|
|
|
|
|
|
p = p->prev;
|
2024-03-19 23:35:29 +01:00
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
while (p != NULL);
|
2024-03-19 22:36:03 +01:00
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
}
|
|
|
|
else // restore ROM
|
|
|
|
{
|
|
|
|
for (; p != NULL; p = p->next)
|
2024-03-19 22:36:03 +01:00
|
|
|
{
|
|
|
|
// restore original data
|
|
|
|
pbyRom[p->dwAddress] = p->byROM;
|
2024-03-19 23:35:29 +01:00
|
|
|
p->bPatch = FALSE; // address not patched
|
2024-03-19 22:36:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
BOOL PatchRom(LPCTSTR szFilename)
|
2024-03-19 22:36:03 +01:00
|
|
|
{
|
|
|
|
HANDLE hFile = NULL;
|
|
|
|
DWORD dwFileSizeLow = 0;
|
|
|
|
DWORD dwFileSizeHigh = 0;
|
|
|
|
DWORD lBytesRead = 0;
|
2024-03-19 23:35:29 +01:00
|
|
|
PSZ lpStop,lpBuf = NULL;
|
2024-03-19 22:36:03 +01:00
|
|
|
DWORD dwAddress = 0;
|
|
|
|
UINT nPos = 0;
|
2024-03-19 23:35:30 +01:00
|
|
|
BOOL bSucc = TRUE;
|
2024-03-19 22:36:03 +01:00
|
|
|
|
|
|
|
if (pbyRom == NULL) return FALSE;
|
2024-03-19 22:38:33 +01:00
|
|
|
SetCurrentDirectory(szEmuDirectory);
|
2024-03-19 22:36:03 +01:00
|
|
|
hFile = CreateFile(szFilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
|
|
|
SetCurrentDirectory(szCurrentDirectory);
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE) return FALSE;
|
|
|
|
dwFileSizeLow = GetFileSize(hFile, &dwFileSizeHigh);
|
2024-03-19 23:35:30 +01:00
|
|
|
if (dwFileSizeHigh != 0 || dwFileSizeLow == 0)
|
|
|
|
{ // file is too large or empty
|
2024-03-19 22:36:03 +01:00
|
|
|
CloseHandle(hFile);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
lpBuf = (PSZ) malloc(dwFileSizeLow+1);
|
2024-03-19 22:36:03 +01:00
|
|
|
if (lpBuf == NULL)
|
|
|
|
{
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
ReadFile(hFile, lpBuf, dwFileSizeLow, &lBytesRead, NULL);
|
|
|
|
CloseHandle(hFile);
|
|
|
|
lpBuf[dwFileSizeLow] = 0;
|
|
|
|
nPos = 0;
|
|
|
|
while (lpBuf[nPos])
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
// skip whitespace characters
|
|
|
|
nPos += (UINT) strspn(&lpBuf[nPos]," \t\n\r");
|
|
|
|
|
|
|
|
if (lpBuf[nPos] == ';') // comment?
|
2024-03-19 22:36:03 +01:00
|
|
|
{
|
2024-03-19 22:37:54 +01:00
|
|
|
do
|
2024-03-19 22:36:03 +01:00
|
|
|
{
|
|
|
|
nPos++;
|
2024-03-19 23:35:30 +01:00
|
|
|
if (lpBuf[nPos] == '\n')
|
2024-03-19 22:36:03 +01:00
|
|
|
{
|
|
|
|
nPos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (lpBuf[nPos]);
|
2024-03-19 22:37:03 +01:00
|
|
|
continue;
|
2024-03-19 22:36:03 +01:00
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
dwAddress = strtoul(&lpBuf[nPos], &lpStop, 16);
|
2024-03-19 23:35:30 +01:00
|
|
|
nPos = (UINT) (lpStop - lpBuf); // position of lpStop
|
|
|
|
|
|
|
|
if (*lpStop != 0) // data behind address
|
2024-03-19 22:36:03 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
if (*lpStop != ':') // invalid syntax
|
2024-03-19 23:35:30 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
// skip to end of line
|
|
|
|
while (lpBuf[nPos] != '\n' && lpBuf[nPos] != 0)
|
|
|
|
{
|
|
|
|
++nPos;
|
|
|
|
}
|
|
|
|
bSucc = FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (lpBuf[++nPos])
|
|
|
|
{
|
|
|
|
if (isxdigit(lpBuf[nPos]) == FALSE) break;
|
|
|
|
if (dwAddress < dwRomSize) // patch ROM
|
|
|
|
{
|
|
|
|
// patch ROM and save original nibble
|
|
|
|
PatchNibble(dwAddress, Asc2Nib(lpBuf[nPos]));
|
|
|
|
}
|
|
|
|
++dwAddress;
|
2024-03-19 23:35:30 +01:00
|
|
|
}
|
2024-03-19 22:36:03 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
_ASSERT(nPos <= dwFileSizeLow); // buffer overflow?
|
2024-03-19 23:35:30 +01:00
|
|
|
free(lpBuf);
|
2024-03-19 23:35:30 +01:00
|
|
|
return bSucc;
|
2024-03-19 22:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-03-19 22:22:29 +01:00
|
|
|
//################
|
|
|
|
//#
|
|
|
|
//# ROM
|
|
|
|
//#
|
|
|
|
//################
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
BOOL CrcRom(WORD *pwChk) // calculate fingerprint of ROM
|
2024-03-19 22:25:45 +01:00
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
DWORD *pdwData,dwSize;
|
|
|
|
DWORD dwChk = 0;
|
2024-03-19 22:25:45 +01:00
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
if (pbyRom == NULL) return TRUE; // ROM CRC isn't available
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
_ASSERT(pbyRom); // view on ROM
|
2024-03-19 23:35:29 +01:00
|
|
|
pdwData = (DWORD *) pbyRom;
|
|
|
|
|
|
|
|
_ASSERT((dwRomSize % sizeof(*pdwData)) == 0);
|
|
|
|
dwSize = dwRomSize / sizeof(*pdwData); // file size in DWORD's
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
// use checksum, because it's faster
|
2024-03-19 23:35:29 +01:00
|
|
|
while (dwSize-- > 0)
|
|
|
|
{
|
|
|
|
DWORD dwData = *pdwData++;
|
|
|
|
if ((dwData & 0xF0F0F0F0) != 0) // data packed?
|
|
|
|
return FALSE;
|
|
|
|
dwChk += dwData;
|
|
|
|
}
|
2024-03-19 22:25:45 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
*pwChk = (WORD) ((dwChk >> 16) + (dwChk & 0xFFFF));
|
|
|
|
return TRUE;
|
2024-03-19 22:25:45 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
BOOL MapRom(LPCTSTR szFilename)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
DWORD dwSize,dwFileSize,dwRead;
|
2024-03-19 22:36:03 +01:00
|
|
|
|
|
|
|
// open ROM for writing
|
2024-03-19 23:35:30 +01:00
|
|
|
BOOL bRomRW = (cCurrentRomType == 'X') ? bRomWriteable : FALSE;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
|
|
|
if (pbyRom != NULL)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 22:38:33 +01:00
|
|
|
SetCurrentDirectory(szEmuDirectory);
|
2024-03-19 23:35:30 +01:00
|
|
|
if (bRomRW) // ROM writeable
|
2024-03-19 22:36:03 +01:00
|
|
|
{
|
|
|
|
hRomFile = CreateFile(szFilename,
|
|
|
|
GENERIC_READ|GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
2024-03-19 23:35:30 +01:00
|
|
|
FILE_FLAG_SEQUENTIAL_SCAN,
|
2024-03-19 22:36:03 +01:00
|
|
|
NULL);
|
|
|
|
if (hRomFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
bRomRW = FALSE; // ROM not writeable
|
2024-03-19 22:36:03 +01:00
|
|
|
hRomFile = CreateFile(szFilename,
|
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
2024-03-19 23:35:30 +01:00
|
|
|
FILE_FLAG_SEQUENTIAL_SCAN,
|
2024-03-19 22:36:03 +01:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // writing ROM disabled
|
|
|
|
{
|
|
|
|
hRomFile = CreateFile(szFilename,
|
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
2024-03-19 23:35:30 +01:00
|
|
|
FILE_FLAG_SEQUENTIAL_SCAN,
|
2024-03-19 22:36:03 +01:00
|
|
|
NULL);
|
|
|
|
}
|
2024-03-19 22:22:29 +01:00
|
|
|
SetCurrentDirectory(szCurrentDirectory);
|
|
|
|
if (hRomFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
hRomFile = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
dwRomSize = GetFileSize(hRomFile, NULL);
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
// read the first 4 bytes
|
|
|
|
ReadFile(hRomFile,&dwSize,sizeof(dwSize),&dwRead,NULL);
|
|
|
|
if (dwRead < sizeof(dwSize))
|
2024-03-19 23:35:30 +01:00
|
|
|
{ // file is too small.
|
2024-03-19 22:22:29 +01:00
|
|
|
CloseHandle(hRomFile);
|
|
|
|
hRomFile = NULL;
|
|
|
|
dwRomSize = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
|
|
|
|
dwFileSize = dwRomSize; // calculate ROM image buffer size
|
|
|
|
bRomPacked = (dwSize & 0xF0F0F0F0) != 0; // ROM image packed
|
|
|
|
if (bRomPacked) dwRomSize *= 2; // unpacked ROM image has double size
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
pbyRom = (LPBYTE) malloc(dwRomSize);
|
2024-03-19 23:35:30 +01:00
|
|
|
if (pbyRom == NULL)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
|
|
|
CloseHandle(hRomFile);
|
|
|
|
hRomFile = NULL;
|
|
|
|
dwRomSize = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
*(DWORD *) pbyRom = dwSize; // save first 4 bytes
|
2024-03-19 23:35:30 +01:00
|
|
|
|
|
|
|
// load rest of file content
|
|
|
|
ReadFile(hRomFile,&pbyRom[sizeof(dwSize)],dwFileSize - sizeof(dwSize),&dwRead,NULL);
|
|
|
|
_ASSERT(dwFileSize - sizeof(dwSize) == dwRead);
|
|
|
|
|
|
|
|
if (bRomRW) // ROM is writeable
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
// no. of dirty pages
|
|
|
|
dwRomDirtyPageSize = dwRomSize / ROMPAGESIZE;
|
|
|
|
|
|
|
|
// alloc dirty page table
|
2024-03-19 23:35:30 +01:00
|
|
|
pbyRomDirtyPage = (LPBYTE) calloc(dwRomDirtyPageSize,sizeof(*pbyRomDirtyPage));
|
2024-03-19 23:35:30 +01:00
|
|
|
if (pbyRomDirtyPage == NULL)
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
free(pbyRom); // free ROM image
|
2024-03-19 23:35:30 +01:00
|
|
|
CloseHandle(hRomFile);
|
|
|
|
dwRomDirtyPageSize = 0;
|
|
|
|
pbyRom = NULL;
|
|
|
|
hRomFile = NULL;
|
|
|
|
dwRomSize = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 22:37:54 +01:00
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
else
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
dwRomDirtyPageSize = 0;
|
2024-03-19 22:22:29 +01:00
|
|
|
CloseHandle(hRomFile);
|
|
|
|
hRomFile = NULL;
|
2024-03-19 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
if (bRomPacked) // packed ROM image
|
2024-03-19 23:35:30 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
dwSize = dwRomSize; // destination start address
|
|
|
|
while (dwFileSize > 0) // unpack source
|
2024-03-19 23:35:30 +01:00
|
|
|
{
|
|
|
|
BYTE byValue = pbyRom[--dwFileSize];
|
|
|
|
pbyRom[--dwSize] = byValue >> 4;
|
|
|
|
pbyRom[--dwSize] = byValue & 0xF;
|
|
|
|
}
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
VOID UnmapRom(VOID)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
if (pbyRom == NULL) return; // ROM not mapped
|
|
|
|
RestorePatches(); // restore ROM patches
|
|
|
|
if (hRomFile) // ROM file still open (only in R/W case)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
_ASSERT(pbyRomDirtyPage != NULL);
|
|
|
|
|
|
|
|
// scan for every dirty page
|
|
|
|
for (i = 0; i < dwRomDirtyPageSize; ++i)
|
|
|
|
{
|
|
|
|
if (pbyRomDirtyPage[i]) // page dirty
|
|
|
|
{
|
|
|
|
DWORD dwSize,dwLinPos,dwFilePos,dwWritten;
|
|
|
|
|
|
|
|
dwLinPos = i * ROMPAGESIZE; // position inside emulator memory
|
|
|
|
|
|
|
|
dwSize = ROMPAGESIZE; // bytes to write
|
|
|
|
while (i+1 < dwRomDirtyPageSize && pbyRomDirtyPage[i+1])
|
|
|
|
{
|
|
|
|
dwSize += ROMPAGESIZE; // next page is also dirty
|
|
|
|
++i; // skip next page in outer loop
|
|
|
|
}
|
|
|
|
|
|
|
|
dwFilePos = dwLinPos; // ROM file position
|
|
|
|
|
|
|
|
if (bRomPacked) // repack data
|
|
|
|
{
|
|
|
|
LPBYTE pbySrc,pbyDest;
|
|
|
|
DWORD j;
|
|
|
|
|
|
|
|
dwSize /= 2; // adjust no. of bytes to write
|
|
|
|
dwFilePos /= 2; // linear pos in packed file
|
|
|
|
|
|
|
|
// pack data in page
|
|
|
|
pbySrc = pbyDest = &pbyRom[dwLinPos];
|
|
|
|
for (j = 0; j < dwSize; j++)
|
|
|
|
{
|
|
|
|
*pbyDest = *pbySrc++;
|
|
|
|
*pbyDest |= *pbySrc++ << 4;
|
|
|
|
pbyDest++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetFilePointer(hRomFile,dwFilePos,NULL,FILE_BEGIN);
|
|
|
|
WriteFile(hRomFile,&pbyRom[dwLinPos],dwSize,&dwWritten,NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(pbyRomDirtyPage);
|
|
|
|
CloseHandle(hRomFile);
|
|
|
|
pbyRomDirtyPage = NULL;
|
|
|
|
dwRomDirtyPageSize = 0;
|
|
|
|
hRomFile = NULL;
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
free(pbyRom); // free ROM image
|
2024-03-19 22:22:29 +01:00
|
|
|
pbyRom = NULL;
|
|
|
|
dwRomSize = 0;
|
2024-03-19 23:35:29 +01:00
|
|
|
wRomCrc = 0;
|
2024-03-19 22:22:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//################
|
|
|
|
//#
|
|
|
|
//# Port2
|
|
|
|
//#
|
|
|
|
//################
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
BOOL CrcPort2(WORD *pwCrc) // calculate fingerprint of port2
|
2024-03-19 22:24:30 +01:00
|
|
|
{
|
|
|
|
DWORD dwCount;
|
|
|
|
DWORD dwFileSize;
|
2024-03-19 23:35:29 +01:00
|
|
|
|
|
|
|
*pwCrc = 0;
|
2024-03-19 22:24:30 +01:00
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
// port2 CRC isn't available
|
2024-03-19 23:35:29 +01:00
|
|
|
if (pbyPort2 == NULL) return TRUE;
|
2024-03-19 22:25:45 +01:00
|
|
|
|
2024-03-19 22:24:30 +01:00
|
|
|
dwFileSize = GetFileSize(hPort2File, &dwCount); // get real filesize
|
|
|
|
_ASSERT(dwCount == 0); // isn't created by MapPort2()
|
|
|
|
|
|
|
|
for (dwCount = 0;dwCount < dwFileSize; ++dwCount)
|
2024-03-19 23:35:29 +01:00
|
|
|
{
|
|
|
|
if ((pbyPort2[dwCount] & 0xF0) != 0) // data packed?
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*pwCrc = (*pwCrc >> 4) ^ (((*pwCrc ^ ((WORD) pbyPort2[dwCount])) & 0xf) * 0x1081);
|
|
|
|
}
|
|
|
|
return TRUE;
|
2024-03-19 22:24:30 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
BOOL MapPort2(LPCTSTR szFilename)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
DWORD dwFileSizeLo,dwFileSizeHi;
|
2024-03-19 23:35:29 +01:00
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
if (pbyPort2 != NULL) return FALSE;
|
2024-03-19 22:22:29 +01:00
|
|
|
bPort2Writeable = TRUE;
|
2024-03-19 22:25:45 +01:00
|
|
|
dwPort2Size = 0; // reset size of port2
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 22:38:33 +01:00
|
|
|
SetCurrentDirectory(szEmuDirectory);
|
2024-03-19 22:37:03 +01:00
|
|
|
hPort2File = CreateFile(szFilename,
|
|
|
|
GENERIC_READ|GENERIC_WRITE,
|
|
|
|
bPort2IsShared ? FILE_SHARE_READ : 0,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
|
|
|
if (hPort2File == INVALID_HANDLE_VALUE)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
bPort2Writeable = FALSE;
|
2024-03-19 22:36:03 +01:00
|
|
|
hPort2File = CreateFile(szFilename,
|
2024-03-19 22:37:03 +01:00
|
|
|
GENERIC_READ,
|
|
|
|
bPort2IsShared ? (FILE_SHARE_READ|FILE_SHARE_WRITE) : 0,
|
2024-03-19 22:36:03 +01:00
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
2024-03-19 22:22:29 +01:00
|
|
|
if (hPort2File == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
SetCurrentDirectory(szCurrentDirectory);
|
|
|
|
hPort2File = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 22:37:03 +01:00
|
|
|
SetCurrentDirectory(szCurrentDirectory);
|
2024-03-19 22:22:29 +01:00
|
|
|
dwFileSizeLo = GetFileSize(hPort2File, &dwFileSizeHi);
|
2024-03-19 23:35:29 +01:00
|
|
|
|
|
|
|
// size not 32, 128, 256, 512, 1024, 2048 or 4096 KB
|
2024-03-19 23:35:30 +01:00
|
|
|
if ( dwFileSizeHi != 0
|
2024-03-19 23:35:30 +01:00
|
|
|
|| dwFileSizeLo == 0
|
|
|
|
|| (dwFileSizeLo & (dwFileSizeLo - 1)) != 0
|
|
|
|
|| (dwFileSizeLo & 0xFF02FFFF) != 0)
|
2024-03-19 23:35:29 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
UnmapPort2();
|
2024-03-19 22:22:29 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
|
2024-03-19 22:36:03 +01:00
|
|
|
hPort2Map = CreateFileMapping(hPort2File, NULL, bPort2Writeable ? PAGE_READWRITE : PAGE_READONLY,
|
|
|
|
0, dwFileSizeLo, NULL);
|
2024-03-19 22:22:29 +01:00
|
|
|
if (hPort2Map == NULL)
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
UnmapPort2();
|
2024-03-19 22:22:29 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
pbyPort2 = (LPBYTE) MapViewOfFile(hPort2Map, bPort2Writeable ? FILE_MAP_WRITE : FILE_MAP_READ, 0, 0, dwFileSizeLo);
|
2024-03-19 22:22:29 +01:00
|
|
|
if (pbyPort2 == NULL)
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
UnmapPort2();
|
2024-03-19 22:22:29 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
|
|
|
|
dwPort2Mask = (dwFileSizeLo - 1) >> 18; // mask for valid address lines of the BS-FF
|
2024-03-19 22:25:45 +01:00
|
|
|
dwPort2Size = dwFileSizeLo / 2048; // mapping size of port2
|
2024-03-19 23:35:29 +01:00
|
|
|
|
|
|
|
if (CrcPort2(&wPort2Crc) == FALSE) // calculate fingerprint of port2
|
|
|
|
{
|
|
|
|
UnmapPort2(); // free memory
|
|
|
|
AbortMessage(_T("Packed Port 2 image detected!"));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 22:22:29 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
VOID UnmapPort2(VOID)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
if (pbyPort2 != NULL)
|
|
|
|
{
|
|
|
|
UnmapViewOfFile(pbyPort2);
|
|
|
|
pbyPort2 = NULL;
|
|
|
|
}
|
|
|
|
if (hPort2Map != NULL)
|
|
|
|
{
|
|
|
|
CloseHandle(hPort2Map);
|
|
|
|
hPort2Map = NULL;
|
|
|
|
}
|
|
|
|
if (hPort2File != NULL)
|
|
|
|
{
|
|
|
|
CloseHandle(hPort2File);
|
|
|
|
hPort2File = NULL;
|
|
|
|
}
|
2024-03-19 22:25:45 +01:00
|
|
|
dwPort2Size = 0; // reset size of port2
|
2024-03-19 22:22:29 +01:00
|
|
|
dwPort2Mask = 0;
|
|
|
|
bPort2Writeable = FALSE;
|
2024-03-19 23:35:29 +01:00
|
|
|
wPort2Crc = 0;
|
2024-03-19 22:22:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//################
|
|
|
|
//#
|
|
|
|
//# Documents
|
|
|
|
//#
|
|
|
|
//################
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
static BOOL IsDataPacked(VOID *pMem, DWORD dwSize)
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
DWORD *pdwMem = (DWORD *) pMem;
|
2024-03-19 23:35:29 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
_ASSERT((dwSize % sizeof(DWORD)) == 0);
|
|
|
|
if ((dwSize % sizeof(DWORD)) != 0) return TRUE;
|
|
|
|
|
|
|
|
for (dwSize /= sizeof(DWORD); dwSize-- > 0;)
|
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
if ((*pdwMem++ & 0xF0F0F0F0) != 0)
|
2024-03-19 23:35:29 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
VOID ResetDocument(VOID)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
DisableDebugger();
|
2024-03-19 22:22:29 +01:00
|
|
|
if (szCurrentKml[0])
|
|
|
|
{
|
|
|
|
KillKML();
|
|
|
|
}
|
|
|
|
if (hCurrentFile)
|
|
|
|
{
|
|
|
|
CloseHandle(hCurrentFile);
|
|
|
|
hCurrentFile = NULL;
|
|
|
|
}
|
|
|
|
szCurrentKml[0] = 0;
|
|
|
|
szCurrentFilename[0]=0;
|
2024-03-19 23:35:30 +01:00
|
|
|
if (Port0) { free(Port0); Port0 = NULL; }
|
|
|
|
if (Port1) { free(Port1); Port1 = NULL; }
|
|
|
|
if (Port2) { free(Port2); Port2 = NULL; } else UnmapPort2();
|
2024-03-19 22:37:54 +01:00
|
|
|
ZeroMemory(&Chipset,sizeof(Chipset));
|
|
|
|
ZeroMemory(&RMap,sizeof(RMap)); // delete MMU mappings
|
|
|
|
ZeroMemory(&WMap,sizeof(WMap));
|
2024-03-19 23:35:30 +01:00
|
|
|
bDocumentAvail = FALSE; // document not available
|
2024-03-19 22:22:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
BOOL NewDocument(VOID)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
SaveBackup();
|
2024-03-19 22:22:29 +01:00
|
|
|
ResetDocument();
|
|
|
|
|
|
|
|
if (!DisplayChooseKml(0)) goto restore;
|
|
|
|
if (!InitKML(szCurrentKml,FALSE)) goto restore;
|
|
|
|
Chipset.type = cCurrentRomType;
|
2024-03-19 22:25:45 +01:00
|
|
|
|
2024-03-19 22:36:03 +01:00
|
|
|
if (Chipset.type == '6' || Chipset.type == 'A') // HP38G
|
2024-03-19 22:25:45 +01:00
|
|
|
{
|
2024-03-19 22:36:03 +01:00
|
|
|
Chipset.Port0Size = (Chipset.type == 'A') ? 32 : 64;
|
2024-03-19 22:25:45 +01:00
|
|
|
Chipset.Port1Size = 0;
|
|
|
|
Chipset.Port2Size = 0;
|
2024-03-19 22:37:03 +01:00
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
Chipset.cards_status = 0x0;
|
|
|
|
}
|
2024-03-19 22:37:03 +01:00
|
|
|
if (Chipset.type == 'E') // HP39/40G
|
|
|
|
{
|
|
|
|
Chipset.Port0Size = 128;
|
|
|
|
Chipset.Port1Size = 0;
|
|
|
|
Chipset.Port2Size = 128;
|
|
|
|
|
|
|
|
Chipset.cards_status = 0xF;
|
|
|
|
|
|
|
|
bPort2Writeable = TRUE; // port2 is writeable
|
|
|
|
}
|
2024-03-19 22:25:45 +01:00
|
|
|
if (Chipset.type == 'S') // HP48SX
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
|
|
|
Chipset.Port0Size = 32;
|
|
|
|
Chipset.Port1Size = 128;
|
2024-03-19 22:37:03 +01:00
|
|
|
Chipset.Port2Size = 0;
|
|
|
|
|
2024-03-19 22:22:29 +01:00
|
|
|
Chipset.cards_status = 0x5;
|
2024-03-19 22:25:45 +01:00
|
|
|
|
|
|
|
// use 2nd command line argument if defined
|
|
|
|
MapPort2((nArgc < 3) ? szPort2Filename : ppArgv[2]);
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
2024-03-19 22:25:45 +01:00
|
|
|
if (Chipset.type == 'G') // HP48GX
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
|
|
|
Chipset.Port0Size = 128;
|
|
|
|
Chipset.Port1Size = 128;
|
2024-03-19 22:37:03 +01:00
|
|
|
Chipset.Port2Size = 0;
|
|
|
|
|
2024-03-19 22:22:29 +01:00
|
|
|
Chipset.cards_status = 0xA;
|
2024-03-19 22:25:45 +01:00
|
|
|
|
|
|
|
// use 2nd command line argument if defined
|
|
|
|
MapPort2((nArgc < 3) ? szPort2Filename : ppArgv[2]);
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
2024-03-19 22:25:45 +01:00
|
|
|
if (Chipset.type == 'X') // HP49G
|
|
|
|
{
|
|
|
|
Chipset.Port0Size = 256;
|
|
|
|
Chipset.Port1Size = 128;
|
|
|
|
Chipset.Port2Size = 128;
|
|
|
|
|
|
|
|
Chipset.cards_status = 0xF;
|
2024-03-19 22:37:03 +01:00
|
|
|
bPort2Writeable = TRUE; // port2 is writeable
|
2024-03-19 22:36:03 +01:00
|
|
|
|
|
|
|
FlashInit(); // init flash structure
|
2024-03-19 22:25:45 +01:00
|
|
|
}
|
2024-03-19 22:37:03 +01:00
|
|
|
|
2024-03-19 22:38:33 +01:00
|
|
|
Chipset.IORam[LPE] = RST; // set ReSeT bit at power on reset
|
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
// allocate port memory
|
|
|
|
if (Chipset.Port0Size)
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
Port0 = (LPBYTE) calloc(Chipset.Port0Size*2048,sizeof(*Port0));
|
2024-03-19 23:35:30 +01:00
|
|
|
_ASSERT(Port0 != NULL);
|
2024-03-19 22:37:03 +01:00
|
|
|
}
|
|
|
|
if (Chipset.Port1Size)
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
Port1 = (LPBYTE) calloc(Chipset.Port1Size*2048,sizeof(*Port1));
|
2024-03-19 23:35:30 +01:00
|
|
|
_ASSERT(Port1 != NULL);
|
2024-03-19 22:37:03 +01:00
|
|
|
}
|
|
|
|
if (Chipset.Port2Size)
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
Port2 = (LPBYTE) calloc(Chipset.Port2Size*2048,sizeof(*Port1));
|
2024-03-19 23:35:30 +01:00
|
|
|
_ASSERT(Port2 != NULL);
|
2024-03-19 22:37:03 +01:00
|
|
|
}
|
|
|
|
LoadBreakpointList(NULL); // clear debugger breakpoint list
|
2024-03-19 22:25:45 +01:00
|
|
|
RomSwitch(0); // boot ROM view of HP49G and map memory
|
2024-03-19 23:35:30 +01:00
|
|
|
bDocumentAvail = TRUE; // document available
|
2024-03-19 22:22:29 +01:00
|
|
|
return TRUE;
|
|
|
|
restore:
|
|
|
|
RestoreBackup();
|
|
|
|
ResetBackup();
|
2024-03-19 22:25:45 +01:00
|
|
|
|
2024-03-19 22:36:03 +01:00
|
|
|
// HP48SX/GX
|
2024-03-19 23:35:29 +01:00
|
|
|
if (Chipset.type == 'S' || Chipset.type == 'G')
|
2024-03-19 22:25:45 +01:00
|
|
|
{
|
|
|
|
// use 2nd command line argument if defined
|
|
|
|
MapPort2((nArgc < 3) ? szPort2Filename : ppArgv[2]);
|
|
|
|
}
|
2024-03-19 22:22:29 +01:00
|
|
|
if (pbyRom)
|
|
|
|
{
|
|
|
|
Map(0x00,0xFF);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
BOOL OpenDocument(LPCTSTR szFilename)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
#define CHECKAREA(s,e) (offsetof(CHIPSET,e)-offsetof(CHIPSET,s)+sizeof(((CHIPSET *)NULL)->e))
|
|
|
|
|
2024-03-19 22:22:29 +01:00
|
|
|
HANDLE hFile = INVALID_HANDLE_VALUE;
|
2024-03-19 22:37:54 +01:00
|
|
|
DWORD lBytesRead,lSizeofChipset;
|
2024-03-19 22:22:29 +01:00
|
|
|
BYTE pbyFileSignature[16];
|
2024-03-19 22:25:45 +01:00
|
|
|
LPBYTE pbySig;
|
2024-03-19 22:22:29 +01:00
|
|
|
UINT ctBytesCompared;
|
|
|
|
UINT nLength;
|
|
|
|
|
|
|
|
// Open file
|
2024-03-19 23:35:29 +01:00
|
|
|
if (lstrcmpi(szCurrentFilename,szFilename) == 0)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
if (YesNoMessage(_T("Do you want to reload this document?")) == IDNO)
|
|
|
|
return TRUE;
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
|
|
|
|
SaveBackup();
|
|
|
|
ResetDocument();
|
|
|
|
|
2024-03-19 22:22:29 +01:00
|
|
|
hFile = CreateFile(szFilename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("This file is missing or already loaded in another instance of Emu48."));
|
2024-03-19 22:22:29 +01:00
|
|
|
goto restore;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and Compare signature
|
|
|
|
ReadFile(hFile, pbyFileSignature, 16, &lBytesRead, NULL);
|
|
|
|
switch (pbyFileSignature[0])
|
|
|
|
{
|
|
|
|
case 'E':
|
2024-03-19 22:25:45 +01:00
|
|
|
pbySig = (pbyFileSignature[3] == '3')
|
2024-03-19 22:37:03 +01:00
|
|
|
? ((pbyFileSignature[4] == '8') ? pbySignatureA : pbySignatureB)
|
2024-03-19 22:25:45 +01:00
|
|
|
: ((pbyFileSignature[4] == '8') ? pbySignatureE : pbySignatureV);
|
2024-03-19 22:22:29 +01:00
|
|
|
for (ctBytesCompared=0; ctBytesCompared<14; ctBytesCompared++)
|
|
|
|
{
|
2024-03-19 22:25:45 +01:00
|
|
|
if (pbyFileSignature[ctBytesCompared]!=pbySig[ctBytesCompared])
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("This file is not a valid Emu48 document."));
|
2024-03-19 22:22:29 +01:00
|
|
|
goto restore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
for (ctBytesCompared=0; ctBytesCompared<14; ctBytesCompared++)
|
|
|
|
{
|
|
|
|
if (pbyFileSignature[ctBytesCompared]!=pbySignatureW[ctBytesCompared])
|
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("This file is not a valid Win48 document."));
|
2024-03-19 22:22:29 +01:00
|
|
|
goto restore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("This file is not a valid document."));
|
2024-03-19 22:22:29 +01:00
|
|
|
goto restore;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (pbyFileSignature[14])
|
|
|
|
{
|
2024-03-19 22:25:45 +01:00
|
|
|
case 0xFE: // Win48 2.1 / Emu4x 0.99.x format
|
2024-03-19 23:35:30 +01:00
|
|
|
// read length of KML script name
|
2024-03-19 22:37:03 +01:00
|
|
|
ReadFile(hFile,&nLength,sizeof(nLength),&lBytesRead,NULL);
|
2024-03-19 23:35:30 +01:00
|
|
|
// KML script name too long for file buffer
|
|
|
|
if (nLength >= ARRAYSIZEOF(szCurrentKml)) goto read_err;
|
2024-03-19 22:37:03 +01:00
|
|
|
#if defined _UNICODE
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
LPSTR szTmp = (LPSTR) malloc(nLength);
|
2024-03-19 22:37:03 +01:00
|
|
|
if (szTmp == NULL)
|
|
|
|
{
|
|
|
|
AbortMessage(_T("Memory Allocation Failure."));
|
|
|
|
goto restore;
|
|
|
|
}
|
|
|
|
ReadFile(hFile, szTmp, nLength, &lBytesRead, NULL);
|
|
|
|
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szTmp, lBytesRead,
|
|
|
|
szCurrentKml, ARRAYSIZEOF(szCurrentKml));
|
2024-03-19 23:35:30 +01:00
|
|
|
free(szTmp);
|
2024-03-19 22:37:03 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
ReadFile(hFile, szCurrentKml, nLength, &lBytesRead, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
2024-03-19 22:22:29 +01:00
|
|
|
if (nLength != lBytesRead) goto read_err;
|
|
|
|
szCurrentKml[nLength] = 0;
|
|
|
|
break;
|
|
|
|
case 0xFF: // Win48 2.05 format
|
|
|
|
break;
|
|
|
|
default:
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("This file is for an unknown version of Emu48."));
|
2024-03-19 22:22:29 +01:00
|
|
|
goto restore;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:37:54 +01:00
|
|
|
// read chipset size inside file
|
2024-03-19 22:22:29 +01:00
|
|
|
ReadFile(hFile, &lSizeofChipset, sizeof(lSizeofChipset), &lBytesRead, NULL);
|
|
|
|
if (lBytesRead != sizeof(lSizeofChipset)) goto read_err;
|
2024-03-19 22:37:54 +01:00
|
|
|
if (lSizeofChipset <= sizeof(Chipset)) // actual or older chipset version
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 22:37:54 +01:00
|
|
|
// read chipset content
|
|
|
|
ZeroMemory(&Chipset,sizeof(Chipset)); // init chipset
|
|
|
|
ReadFile(hFile, &Chipset, lSizeofChipset, &lBytesRead, NULL);
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
2024-03-19 22:37:54 +01:00
|
|
|
else // newer chipset version
|
|
|
|
{
|
|
|
|
// read my used chipset content
|
|
|
|
ReadFile(hFile, &Chipset, sizeof(Chipset), &lBytesRead, NULL);
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 22:37:54 +01:00
|
|
|
// skip rest of chipset
|
|
|
|
SetFilePointer(hFile, lSizeofChipset-sizeof(Chipset), NULL, FILE_CURRENT);
|
|
|
|
lSizeofChipset = sizeof(Chipset);
|
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
if (lBytesRead != lSizeofChipset) goto read_err;
|
|
|
|
|
|
|
|
if (!isModelValid(Chipset.type)) // check for valid model in emulator state file
|
|
|
|
{
|
|
|
|
AbortMessage(_T("Emulator state file with invalid calculator model."));
|
|
|
|
goto restore;
|
|
|
|
}
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 22:37:54 +01:00
|
|
|
SetWindowLocation(hWnd,Chipset.nPosX,Chipset.nPosY);
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
while (TRUE)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:33:35 +01:00
|
|
|
if (szCurrentKml[0]) // KML file name
|
|
|
|
{
|
|
|
|
BOOL bOK;
|
2024-03-19 22:37:03 +01:00
|
|
|
|
2024-03-19 23:33:35 +01:00
|
|
|
bOK = InitKML(szCurrentKml,FALSE);
|
|
|
|
bOK = bOK && (cCurrentRomType == Chipset.type);
|
|
|
|
if (bOK) break;
|
2024-03-19 22:37:03 +01:00
|
|
|
|
2024-03-19 23:33:35 +01:00
|
|
|
KillKML();
|
|
|
|
}
|
2024-03-19 22:22:29 +01:00
|
|
|
if (!DisplayChooseKml(Chipset.type))
|
|
|
|
goto restore;
|
|
|
|
}
|
2024-03-19 23:33:35 +01:00
|
|
|
// reload old button state
|
2024-03-19 23:35:30 +01:00
|
|
|
ReloadButtons(Chipset.Keyboard_Row,ARRAYSIZEOF(Chipset.Keyboard_Row));
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 22:36:03 +01:00
|
|
|
FlashInit(); // init flash structure
|
|
|
|
|
2024-03-19 22:22:29 +01:00
|
|
|
if (Chipset.Port0Size)
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
Port0 = (LPBYTE) malloc(Chipset.Port0Size*2048);
|
2024-03-19 23:35:30 +01:00
|
|
|
if (Port0 == NULL)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("Memory Allocation Failure."));
|
2024-03-19 22:22:29 +01:00
|
|
|
goto restore;
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
ReadFile(hFile, Port0, Chipset.Port0Size*2048, &lBytesRead, NULL);
|
2024-03-19 22:22:29 +01:00
|
|
|
if (lBytesRead != Chipset.Port0Size*2048) goto read_err;
|
2024-03-19 23:35:29 +01:00
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
if (IsDataPacked(Port0,Chipset.Port0Size*2048)) goto read_err;
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Chipset.Port1Size)
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
Port1 = (LPBYTE) malloc(Chipset.Port1Size*2048);
|
2024-03-19 23:35:30 +01:00
|
|
|
if (Port1 == NULL)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("Memory Allocation Failure."));
|
2024-03-19 22:22:29 +01:00
|
|
|
goto restore;
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
ReadFile(hFile, Port1, Chipset.Port1Size*2048, &lBytesRead, NULL);
|
2024-03-19 22:22:29 +01:00
|
|
|
if (lBytesRead != Chipset.Port1Size*2048) goto read_err;
|
2024-03-19 23:35:29 +01:00
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
if (IsDataPacked(Port1,Chipset.Port1Size*2048)) goto read_err;
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
// HP48SX/GX
|
2024-03-19 23:35:29 +01:00
|
|
|
if (cCurrentRomType=='S' || cCurrentRomType=='G')
|
2024-03-19 22:24:30 +01:00
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
MapPort2((nArgc < 3) ? szPort2Filename : ppArgv[2]);
|
|
|
|
// port2 changed and card detection enabled
|
2024-03-19 23:35:29 +01:00
|
|
|
if ( Chipset.wPort2Crc != wPort2Crc
|
2024-03-19 22:37:03 +01:00
|
|
|
&& (Chipset.IORam[CARDCTL] & ECDT) != 0 && (Chipset.IORam[TIMER2_CTRL] & RUN) != 0
|
|
|
|
)
|
2024-03-19 22:25:45 +01:00
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
Chipset.HST |= MP; // set Module Pulled
|
|
|
|
IOBit(SRQ2,NINT,FALSE); // set NINT to low
|
|
|
|
Chipset.SoftInt = TRUE; // set interrupt
|
|
|
|
bInterrupt = TRUE;
|
2024-03-19 22:25:45 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-19 22:37:03 +01:00
|
|
|
else // HP38G, HP39/40G, HP49G
|
2024-03-19 22:25:45 +01:00
|
|
|
{
|
|
|
|
if (Chipset.Port2Size)
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
Port2 = (LPBYTE) malloc(Chipset.Port2Size*2048);
|
2024-03-19 23:35:30 +01:00
|
|
|
if (Port2 == NULL)
|
2024-03-19 22:25:45 +01:00
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("Memory Allocation Failure."));
|
2024-03-19 22:25:45 +01:00
|
|
|
goto restore;
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
ReadFile(hFile, Port2, Chipset.Port2Size*2048, &lBytesRead, NULL);
|
2024-03-19 22:25:45 +01:00
|
|
|
if (lBytesRead != Chipset.Port2Size*2048) goto read_err;
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
if (IsDataPacked(Port2,Chipset.Port2Size*2048)) goto read_err;
|
2024-03-19 23:35:29 +01:00
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
bPort2Writeable = TRUE;
|
|
|
|
Chipset.cards_status = 0xF;
|
|
|
|
}
|
2024-03-19 22:24:30 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
RomSwitch(Chipset.Bank_FF); // reload ROM view of HP49G and map memory
|
2024-03-19 22:37:54 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
if (Chipset.wRomCrc != wRomCrc) // ROM changed
|
2024-03-19 22:37:03 +01:00
|
|
|
{
|
2024-03-19 22:25:45 +01:00
|
|
|
CpuReset();
|
2024-03-19 22:37:03 +01:00
|
|
|
Chipset.Shutdn = FALSE; // automatic restart
|
|
|
|
}
|
2024-03-19 22:25:45 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
// check CPU main registers
|
|
|
|
if (IsDataPacked(Chipset.A,CHECKAREA(A,R4))) goto read_err;
|
|
|
|
|
|
|
|
LoadBreakpointList(hFile); // load debugger breakpoint list
|
|
|
|
|
2024-03-19 22:22:29 +01:00
|
|
|
lstrcpy(szCurrentFilename, szFilename);
|
2024-03-19 22:25:45 +01:00
|
|
|
_ASSERT(hCurrentFile == NULL);
|
2024-03-19 22:22:29 +01:00
|
|
|
hCurrentFile = hFile;
|
2024-03-19 22:38:33 +01:00
|
|
|
#if defined _USRDLL // DLL version
|
|
|
|
// notify main proc about current document file
|
|
|
|
if (pEmuDocumentNotify) pEmuDocumentNotify(szCurrentFilename);
|
|
|
|
#endif
|
2024-03-19 23:35:29 +01:00
|
|
|
SetWindowPathTitle(szCurrentFilename); // update window title line
|
2024-03-19 23:35:30 +01:00
|
|
|
bDocumentAvail = TRUE; // document available
|
2024-03-19 22:22:29 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
read_err:
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("This file must be truncated, and cannot be loaded."));
|
2024-03-19 22:22:29 +01:00
|
|
|
restore:
|
2024-03-19 22:25:45 +01:00
|
|
|
if (INVALID_HANDLE_VALUE != hFile) // close if valid handle
|
2024-03-19 22:22:29 +01:00
|
|
|
CloseHandle(hFile);
|
|
|
|
RestoreBackup();
|
|
|
|
ResetBackup();
|
2024-03-19 22:25:45 +01:00
|
|
|
|
2024-03-19 22:36:03 +01:00
|
|
|
// HP48SX/GX
|
2024-03-19 23:35:29 +01:00
|
|
|
if (cCurrentRomType=='S' || cCurrentRomType=='G')
|
2024-03-19 22:25:45 +01:00
|
|
|
{
|
|
|
|
// use 2nd command line argument if defined
|
|
|
|
MapPort2((nArgc < 3) ? szPort2Filename : ppArgv[2]);
|
|
|
|
}
|
2024-03-19 22:22:29 +01:00
|
|
|
return FALSE;
|
2024-03-19 23:35:29 +01:00
|
|
|
#undef CHECKAREA
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
BOOL SaveDocument(VOID)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
DWORD lBytesWritten;
|
|
|
|
DWORD lSizeofChipset;
|
|
|
|
UINT nLength;
|
|
|
|
WINDOWPLACEMENT wndpl;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
|
|
|
if (hCurrentFile == NULL) return FALSE;
|
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
_ASSERT(hWnd); // window open
|
|
|
|
wndpl.length = sizeof(wndpl); // update saved window position
|
|
|
|
GetWindowPlacement(hWnd, &wndpl);
|
|
|
|
Chipset.nPosX = (SWORD) wndpl.rcNormalPosition.left;
|
|
|
|
Chipset.nPosY = (SWORD) wndpl.rcNormalPosition.top;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
SetFilePointer(hCurrentFile,0,NULL,FILE_BEGIN);
|
2024-03-19 22:25:45 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
if (!WriteFile(hCurrentFile, pbySignatureE, sizeof(pbySignatureE), &lBytesWritten, NULL))
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("Could not write into file !"));
|
2024-03-19 22:22:29 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
CrcRom(&Chipset.wRomCrc); // save fingerprint of ROM
|
|
|
|
CrcPort2(&Chipset.wPort2Crc); // save fingerprint of port2
|
2024-03-19 22:24:30 +01:00
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
nLength = lstrlen(szCurrentKml);
|
2024-03-19 22:22:29 +01:00
|
|
|
WriteFile(hCurrentFile, &nLength, sizeof(nLength), &lBytesWritten, NULL);
|
2024-03-19 22:37:03 +01:00
|
|
|
#if defined _UNICODE
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
LPSTR szTmp = (LPSTR) malloc(nLength);
|
2024-03-19 22:37:03 +01:00
|
|
|
if (szTmp != NULL)
|
|
|
|
{
|
|
|
|
WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,
|
|
|
|
szCurrentKml, nLength,
|
|
|
|
szTmp, nLength, NULL, NULL);
|
|
|
|
WriteFile(hCurrentFile, szTmp, nLength, &lBytesWritten, NULL);
|
2024-03-19 23:35:30 +01:00
|
|
|
free(szTmp);
|
2024-03-19 22:37:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
WriteFile(hCurrentFile, szCurrentKml, nLength, &lBytesWritten, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
2024-03-19 22:22:29 +01:00
|
|
|
lSizeofChipset = sizeof(CHIPSET);
|
|
|
|
WriteFile(hCurrentFile, &lSizeofChipset, sizeof(lSizeofChipset), &lBytesWritten, NULL);
|
|
|
|
WriteFile(hCurrentFile, &Chipset, lSizeofChipset, &lBytesWritten, NULL);
|
2024-03-19 23:35:30 +01:00
|
|
|
if (Chipset.Port0Size) WriteFile(hCurrentFile, Port0, Chipset.Port0Size*2048, &lBytesWritten, NULL);
|
|
|
|
if (Chipset.Port1Size) WriteFile(hCurrentFile, Port1, Chipset.Port1Size*2048, &lBytesWritten, NULL);
|
|
|
|
if (Chipset.Port2Size) WriteFile(hCurrentFile, Port2, Chipset.Port2Size*2048, &lBytesWritten, NULL);
|
2024-03-19 22:37:03 +01:00
|
|
|
SaveBreakpointList(hCurrentFile); // save debugger breakpoint list
|
|
|
|
SetEndOfFile(hCurrentFile); // cut the rest
|
2024-03-19 22:22:29 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SaveDocumentAs(LPCTSTR szFilename)
|
|
|
|
{
|
2024-03-19 22:25:45 +01:00
|
|
|
HANDLE hFile;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 22:24:30 +01:00
|
|
|
if (hCurrentFile) // already file in use
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 22:25:45 +01:00
|
|
|
CloseHandle(hCurrentFile); // close it, even it's same, so data always will be saved
|
|
|
|
hCurrentFile = NULL;
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
2024-03-19 22:25:45 +01:00
|
|
|
hFile = CreateFile(szFilename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
|
2024-03-19 22:24:30 +01:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE) // error, couldn't create a new file
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("This file must be currently used by another instance of Emu48."));
|
2024-03-19 22:22:29 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 22:24:30 +01:00
|
|
|
lstrcpy(szCurrentFilename, szFilename); // save new file name
|
|
|
|
hCurrentFile = hFile; // and the corresponding handle
|
2024-03-19 22:38:33 +01:00
|
|
|
#if defined _USRDLL // DLL version
|
|
|
|
// notify main proc about current document file
|
|
|
|
if (pEmuDocumentNotify) pEmuDocumentNotify(szCurrentFilename);
|
|
|
|
#endif
|
2024-03-19 23:35:29 +01:00
|
|
|
SetWindowPathTitle(szCurrentFilename); // update window title line
|
2024-03-19 22:24:30 +01:00
|
|
|
return SaveDocument(); // save current content
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//################
|
|
|
|
//#
|
|
|
|
//# Backup
|
|
|
|
//#
|
|
|
|
//################
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
BOOL SaveBackup(VOID)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 22:37:54 +01:00
|
|
|
WINDOWPLACEMENT wndpl;
|
2024-03-19 22:25:45 +01:00
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
if (!bDocumentAvail) return FALSE;
|
2024-03-19 22:25:45 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
_ASSERT(nState != SM_RUN); // emulation engine is running
|
|
|
|
|
2024-03-19 22:36:03 +01:00
|
|
|
// save window position
|
2024-03-19 22:37:54 +01:00
|
|
|
_ASSERT(hWnd); // window open
|
|
|
|
wndpl.length = sizeof(wndpl); // update saved window position
|
|
|
|
GetWindowPlacement(hWnd, &wndpl);
|
|
|
|
Chipset.nPosX = (SWORD) wndpl.rcNormalPosition.left;
|
|
|
|
Chipset.nPosY = (SWORD) wndpl.rcNormalPosition.top;
|
2024-03-19 22:25:45 +01:00
|
|
|
|
2024-03-19 22:22:29 +01:00
|
|
|
lstrcpy(szBackupFilename, szCurrentFilename);
|
|
|
|
lstrcpy(szBackupKml, szCurrentKml);
|
2024-03-19 23:35:30 +01:00
|
|
|
if (BackupPort0) { free(BackupPort0); BackupPort0 = NULL; }
|
|
|
|
if (BackupPort1) { free(BackupPort1); BackupPort1 = NULL; }
|
|
|
|
if (BackupPort2) { free(BackupPort2); BackupPort2 = NULL; }
|
2024-03-19 22:22:29 +01:00
|
|
|
CopyMemory(&BackupChipset, &Chipset, sizeof(Chipset));
|
2024-03-19 23:35:30 +01:00
|
|
|
if (Port0 && Chipset.Port0Size)
|
|
|
|
{
|
|
|
|
BackupPort0 = (LPBYTE) malloc(Chipset.Port0Size*2048);
|
|
|
|
CopyMemory(BackupPort0,Port0,Chipset.Port0Size*2048);
|
|
|
|
}
|
|
|
|
if (Port1 && Chipset.Port1Size)
|
|
|
|
{
|
|
|
|
BackupPort1 = (LPBYTE) malloc(Chipset.Port1Size*2048);
|
|
|
|
CopyMemory(BackupPort1,Port1,Chipset.Port1Size*2048);
|
|
|
|
}
|
|
|
|
if (Port2 && Chipset.Port2Size) // internal port2
|
|
|
|
{
|
|
|
|
BackupPort2 = (LPBYTE) malloc(Chipset.Port2Size*2048);
|
2024-03-19 23:35:30 +01:00
|
|
|
CopyMemory(BackupPort2,Port2,Chipset.Port2Size*2048);
|
2024-03-19 22:25:45 +01:00
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
CreateBackupBreakpointList();
|
2024-03-19 22:22:29 +01:00
|
|
|
bBackup = TRUE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
BOOL RestoreBackup(VOID)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
BOOL bDbgOpen;
|
|
|
|
|
2024-03-19 22:22:29 +01:00
|
|
|
if (!bBackup) return FALSE;
|
2024-03-19 23:35:29 +01:00
|
|
|
|
|
|
|
bDbgOpen = (nDbgState != DBG_OFF); // debugger window open?
|
2024-03-19 22:22:29 +01:00
|
|
|
ResetDocument();
|
2024-03-19 22:25:45 +01:00
|
|
|
// need chipset for contrast setting in InitKML()
|
2024-03-19 22:38:33 +01:00
|
|
|
Chipset.contrast = BackupChipset.contrast;
|
2024-03-19 22:22:29 +01:00
|
|
|
if (!InitKML(szBackupKml,TRUE))
|
|
|
|
{
|
|
|
|
InitKML(szCurrentKml,TRUE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
lstrcpy(szCurrentKml, szBackupKml);
|
|
|
|
lstrcpy(szCurrentFilename, szBackupFilename);
|
|
|
|
if (szCurrentFilename[0])
|
|
|
|
{
|
|
|
|
hCurrentFile = CreateFile(szCurrentFilename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
if (hCurrentFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
hCurrentFile = NULL;
|
|
|
|
szCurrentFilename[0] = 0;
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 22:38:33 +01:00
|
|
|
CopyMemory(&Chipset, &BackupChipset, sizeof(Chipset));
|
2024-03-19 23:35:30 +01:00
|
|
|
if (BackupPort0 && Chipset.Port0Size)
|
|
|
|
{
|
|
|
|
Port0 = (LPBYTE) malloc(Chipset.Port0Size*2048);
|
|
|
|
CopyMemory(Port0,BackupPort0,Chipset.Port0Size*2048);
|
|
|
|
}
|
|
|
|
if (BackupPort1 && Chipset.Port1Size)
|
|
|
|
{
|
|
|
|
Port1 = (LPBYTE) malloc(Chipset.Port1Size*2048);
|
|
|
|
CopyMemory(Port1,BackupPort1,Chipset.Port1Size*2048);
|
|
|
|
}
|
|
|
|
if (BackupPort2 && Chipset.Port2Size) // internal port2
|
2024-03-19 22:25:45 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
Port2 = (LPBYTE) malloc(Chipset.Port2Size*2048);
|
2024-03-19 23:35:30 +01:00
|
|
|
CopyMemory(Port2,BackupPort2,Chipset.Port2Size*2048);
|
2024-03-19 22:25:45 +01:00
|
|
|
}
|
2024-03-19 22:36:03 +01:00
|
|
|
// map port2
|
2024-03-19 22:25:45 +01:00
|
|
|
else
|
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
if (cCurrentRomType=='S' || cCurrentRomType=='G') // HP48SX/GX
|
2024-03-19 22:25:45 +01:00
|
|
|
{
|
|
|
|
// use 2nd command line argument if defined
|
|
|
|
MapPort2((nArgc < 3) ? szPort2Filename : ppArgv[2]);
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
Map(0x00,0xFF); // update memory mapping
|
2024-03-19 23:35:29 +01:00
|
|
|
SetWindowPathTitle(szCurrentFilename); // update window title line
|
2024-03-19 22:37:54 +01:00
|
|
|
SetWindowLocation(hWnd,Chipset.nPosX,Chipset.nPosY);
|
2024-03-19 23:35:29 +01:00
|
|
|
RestoreBackupBreakpointList(); // restore the debugger breakpoint list
|
|
|
|
if (bDbgOpen) OnToolDebug(); // reopen the debugger
|
2024-03-19 23:35:30 +01:00
|
|
|
bDocumentAvail = TRUE; // document available
|
2024-03-19 22:22:29 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
BOOL ResetBackup(VOID)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
|
|
|
if (!bBackup) return FALSE;
|
|
|
|
szBackupFilename[0] = 0;
|
|
|
|
szBackupKml[0] = 0;
|
2024-03-19 23:35:30 +01:00
|
|
|
if (BackupPort0) { free(BackupPort0); BackupPort0 = NULL; }
|
|
|
|
if (BackupPort1) { free(BackupPort1); BackupPort1 = NULL; }
|
|
|
|
if (BackupPort2) { free(BackupPort2); BackupPort2 = NULL; }
|
2024-03-19 22:37:54 +01:00
|
|
|
ZeroMemory(&BackupChipset,sizeof(BackupChipset));
|
2024-03-19 22:22:29 +01:00
|
|
|
bBackup = FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//################
|
|
|
|
//#
|
|
|
|
//# Open File Common Dialog Boxes
|
|
|
|
//#
|
|
|
|
//################
|
|
|
|
|
|
|
|
static VOID InitializeOFN(LPOPENFILENAME ofn)
|
|
|
|
{
|
2024-03-19 22:37:54 +01:00
|
|
|
ZeroMemory((LPVOID)ofn, sizeof(OPENFILENAME));
|
2024-03-19 22:22:29 +01:00
|
|
|
ofn->lStructSize = sizeof(OPENFILENAME);
|
|
|
|
ofn->hwndOwner = hWnd;
|
|
|
|
ofn->Flags = OFN_EXPLORER|OFN_HIDEREADONLY;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
BOOL GetOpenFilename(VOID)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
TCHAR szBuffer[ARRAYSIZEOF(szBufferFilename)];
|
2024-03-19 22:22:29 +01:00
|
|
|
OPENFILENAME ofn;
|
|
|
|
|
|
|
|
InitializeOFN(&ofn);
|
2024-03-19 22:37:54 +01:00
|
|
|
ofn.lpstrFilter =
|
2024-03-19 23:35:29 +01:00
|
|
|
_T("Emu48 Files (*.e38;*.e39;*.e48;*.e49)\0")
|
|
|
|
_T("*.e38;*.e39;*.e48;*.e49\0")
|
|
|
|
_T("HP-38 Files (*.e38)\0*.e38\0")
|
|
|
|
_T("HP-39 Files (*.e39)\0*.e39\0")
|
|
|
|
_T("HP-48 Files (*.e48)\0*.e48\0")
|
|
|
|
_T("HP-49 Files (*.e49)\0*.e49\0")
|
|
|
|
_T("Win48 Files (*.w48)\0*.w48\0");
|
|
|
|
ofn.nFilterIndex = 1;
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.lpstrFile = szBuffer;
|
2024-03-19 22:22:29 +01:00
|
|
|
ofn.lpstrFile[0] = 0;
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.nMaxFile = ARRAYSIZEOF(szBuffer);
|
2024-03-19 22:22:29 +01:00
|
|
|
ofn.Flags |= OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
|
2024-03-19 23:35:29 +01:00
|
|
|
if (GetOpenFileName(&ofn) == FALSE) return FALSE;
|
|
|
|
_ASSERT(ARRAYSIZEOF(szBufferFilename) == ofn.nMaxFile);
|
2024-03-19 22:22:29 +01:00
|
|
|
lstrcpy(szBufferFilename, ofn.lpstrFile);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:25:45 +01:00
|
|
|
BOOL GetSaveAsFilename(VOID)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
TCHAR szBuffer[ARRAYSIZEOF(szBufferFilename)];
|
2024-03-19 22:22:29 +01:00
|
|
|
OPENFILENAME ofn;
|
|
|
|
|
|
|
|
InitializeOFN(&ofn);
|
2024-03-19 22:37:54 +01:00
|
|
|
ofn.lpstrFilter =
|
2024-03-19 23:35:29 +01:00
|
|
|
_T("HP-38 Files (*.e38)\0*.e38\0")
|
|
|
|
_T("HP-39 Files (*.e39)\0*.e39\0")
|
|
|
|
_T("HP-48 Files (*.e48)\0*.e48\0")
|
|
|
|
_T("HP-49 Files (*.e49)\0*.e49\0");
|
|
|
|
ofn.lpstrDefExt = _T("e48"); // HP48SX/GX
|
2024-03-19 22:37:03 +01:00
|
|
|
ofn.nFilterIndex = 3;
|
|
|
|
if (cCurrentRomType=='6' || cCurrentRomType=='A') // HP38G
|
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.lpstrDefExt = _T("e38");
|
2024-03-19 22:37:03 +01:00
|
|
|
ofn.nFilterIndex = 1;
|
2024-03-19 22:25:45 +01:00
|
|
|
}
|
2024-03-19 22:37:03 +01:00
|
|
|
if (cCurrentRomType=='E') // HP39/40G
|
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.lpstrDefExt = _T("e39");
|
2024-03-19 22:37:03 +01:00
|
|
|
ofn.nFilterIndex = 2;
|
|
|
|
}
|
|
|
|
if (cCurrentRomType=='X') // HP49G
|
2024-03-19 22:25:45 +01:00
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.lpstrDefExt = _T("e49");
|
2024-03-19 22:37:03 +01:00
|
|
|
ofn.nFilterIndex = 4;
|
2024-03-19 22:25:45 +01:00
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.lpstrFile = szBuffer;
|
2024-03-19 22:22:29 +01:00
|
|
|
ofn.lpstrFile[0] = 0;
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.nMaxFile = ARRAYSIZEOF(szBuffer);
|
2024-03-19 22:22:29 +01:00
|
|
|
ofn.Flags |= OFN_CREATEPROMPT|OFN_OVERWRITEPROMPT;
|
2024-03-19 23:35:29 +01:00
|
|
|
if (GetSaveFileName(&ofn) == FALSE) return FALSE;
|
|
|
|
_ASSERT(ARRAYSIZEOF(szBufferFilename) == ofn.nMaxFile);
|
2024-03-19 22:22:29 +01:00
|
|
|
lstrcpy(szBufferFilename, ofn.lpstrFile);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
BOOL GetLoadObjectFilename(LPCTSTR lpstrFilter,LPCTSTR lpstrDefExt)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
TCHAR szBuffer[ARRAYSIZEOF(szBufferFilename)];
|
2024-03-19 22:22:29 +01:00
|
|
|
OPENFILENAME ofn;
|
|
|
|
|
|
|
|
InitializeOFN(&ofn);
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.lpstrFilter = lpstrFilter;
|
|
|
|
ofn.lpstrDefExt = lpstrDefExt;
|
2024-03-19 22:22:29 +01:00
|
|
|
ofn.nFilterIndex = 1;
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.lpstrFile = szBuffer;
|
2024-03-19 22:22:29 +01:00
|
|
|
ofn.lpstrFile[0] = 0;
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.nMaxFile = ARRAYSIZEOF(szBuffer);
|
2024-03-19 22:22:29 +01:00
|
|
|
ofn.Flags |= OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
|
2024-03-19 23:35:29 +01:00
|
|
|
if (GetOpenFileName(&ofn) == FALSE) return FALSE;
|
|
|
|
_ASSERT(ARRAYSIZEOF(szBufferFilename) == ofn.nMaxFile);
|
2024-03-19 22:22:29 +01:00
|
|
|
lstrcpy(szBufferFilename, ofn.lpstrFile);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
BOOL GetSaveObjectFilename(LPCTSTR lpstrFilter,LPCTSTR lpstrDefExt)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
TCHAR szBuffer[ARRAYSIZEOF(szBufferFilename)];
|
2024-03-19 22:22:29 +01:00
|
|
|
OPENFILENAME ofn;
|
|
|
|
|
|
|
|
InitializeOFN(&ofn);
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.lpstrFilter = lpstrFilter;
|
2024-03-19 23:35:30 +01:00
|
|
|
ofn.lpstrDefExt = NULL;
|
2024-03-19 22:22:29 +01:00
|
|
|
ofn.nFilterIndex = 1;
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.lpstrFile = szBuffer;
|
2024-03-19 22:22:29 +01:00
|
|
|
ofn.lpstrFile[0] = 0;
|
2024-03-19 23:35:29 +01:00
|
|
|
ofn.nMaxFile = ARRAYSIZEOF(szBuffer);
|
2024-03-19 22:22:29 +01:00
|
|
|
ofn.Flags |= OFN_CREATEPROMPT|OFN_OVERWRITEPROMPT;
|
2024-03-19 23:35:29 +01:00
|
|
|
if (GetSaveFileName(&ofn) == FALSE) return FALSE;
|
|
|
|
_ASSERT(ARRAYSIZEOF(szBufferFilename) == ofn.nMaxFile);
|
2024-03-19 22:22:29 +01:00
|
|
|
lstrcpy(szBufferFilename, ofn.lpstrFile);
|
2024-03-19 23:35:30 +01:00
|
|
|
if (ofn.nFileExtension == 0) // given filename has no extension
|
|
|
|
{
|
|
|
|
// actual name length
|
|
|
|
UINT nLength = lstrlen(szBufferFilename);
|
|
|
|
|
|
|
|
// destination buffer has room for the default extension
|
|
|
|
if (nLength + 1 + lstrlen(lpstrDefExt) < ARRAYSIZEOF(szBufferFilename))
|
|
|
|
{
|
|
|
|
// add default extension
|
|
|
|
szBufferFilename[nLength++] = _T('.');
|
|
|
|
lstrcpy(&szBufferFilename[nLength], lpstrDefExt);
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 22:22:29 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//################
|
|
|
|
//#
|
|
|
|
//# Load and Save HP48 Objects
|
|
|
|
//#
|
|
|
|
//################
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
WORD WriteStack(UINT nStkLevel,LPBYTE lpBuf,DWORD dwSize) // separated from LoadObject()
|
2024-03-19 23:35:29 +01:00
|
|
|
{
|
|
|
|
BOOL bBinary;
|
|
|
|
DWORD dwAddress, i;
|
|
|
|
|
|
|
|
bBinary = ((lpBuf[dwSize+0]=='H')
|
|
|
|
&& (lpBuf[dwSize+1]=='P')
|
|
|
|
&& (lpBuf[dwSize+2]=='H')
|
|
|
|
&& (lpBuf[dwSize+3]=='P')
|
|
|
|
&& (lpBuf[dwSize+4]=='4')
|
|
|
|
&& (lpBuf[dwSize+5]==((cCurrentRomType!='X') ? '8' : '9'))
|
|
|
|
&& (lpBuf[dwSize+6]=='-'));
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
for (dwAddress = 0, i = 0; i < dwSize; i++)
|
2024-03-19 23:35:29 +01:00
|
|
|
{
|
|
|
|
BYTE byTwoNibs = lpBuf[i+dwSize];
|
2024-03-19 23:35:29 +01:00
|
|
|
lpBuf[dwAddress++] = (BYTE)(byTwoNibs&0xF);
|
|
|
|
lpBuf[dwAddress++] = (BYTE)(byTwoNibs>>4);
|
2024-03-19 23:35:29 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
dwSize = dwAddress; // unpacked buffer size
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
if (bBinary == TRUE)
|
|
|
|
{ // load as binary
|
2024-03-19 23:35:29 +01:00
|
|
|
dwSize = RPL_ObjectSize(lpBuf+16,dwSize-16);
|
|
|
|
if (dwSize == BAD_OB) return S_ERR_OBJECT;
|
2024-03-19 23:35:29 +01:00
|
|
|
dwAddress = RPL_CreateTemp(dwSize);
|
|
|
|
if (dwAddress == 0) return S_ERR_BINARY;
|
|
|
|
Nwrite(lpBuf+16,dwAddress,dwSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // load as string
|
|
|
|
dwAddress = RPL_CreateTemp(dwSize+10);
|
|
|
|
if (dwAddress == 0) return S_ERR_ASCII;
|
|
|
|
Write5(dwAddress,0x02A2C); // String
|
|
|
|
Write5(dwAddress+5,dwSize+5); // length of String
|
|
|
|
Nwrite(lpBuf,dwAddress+10,dwSize); // data
|
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
RPL_Push(nStkLevel,dwAddress);
|
2024-03-19 23:35:29 +01:00
|
|
|
return S_ERR_NO;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
BOOL LoadObject(LPCTSTR szFilename) // separated stack writing part
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
|
|
|
HANDLE hFile;
|
|
|
|
DWORD dwFileSizeLow;
|
|
|
|
DWORD dwFileSizeHigh;
|
|
|
|
LPBYTE lpBuf;
|
2024-03-19 22:24:30 +01:00
|
|
|
WORD wError;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 23:33:35 +01:00
|
|
|
hFile = CreateFile(szFilename,
|
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_FLAG_SEQUENTIAL_SCAN,
|
|
|
|
NULL);
|
2024-03-19 22:22:29 +01:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE) return FALSE;
|
|
|
|
dwFileSizeLow = GetFileSize(hFile, &dwFileSizeHigh);
|
|
|
|
if (dwFileSizeHigh != 0)
|
|
|
|
{ // file is too large.
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
lpBuf = (LPBYTE) malloc(dwFileSizeLow*2);
|
2024-03-19 22:22:29 +01:00
|
|
|
if (lpBuf == NULL)
|
|
|
|
{
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2024-03-19 22:24:30 +01:00
|
|
|
ReadFile(hFile, lpBuf+dwFileSizeLow, dwFileSizeLow, &dwFileSizeHigh, NULL);
|
2024-03-19 22:22:29 +01:00
|
|
|
CloseHandle(hFile);
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
wError = WriteStack(1,lpBuf,dwFileSizeLow);
|
|
|
|
|
|
|
|
if (wError == S_ERR_OBJECT)
|
|
|
|
AbortMessage(_T("This isn't a valid binary file."));
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 22:24:30 +01:00
|
|
|
if (wError == S_ERR_BINARY)
|
2024-03-19 22:37:54 +01:00
|
|
|
AbortMessage(_T("The calculator does not have enough\nfree memory to load this binary file."));
|
2024-03-19 22:24:30 +01:00
|
|
|
|
|
|
|
if (wError == S_ERR_ASCII)
|
2024-03-19 22:37:54 +01:00
|
|
|
AbortMessage(_T("The calculator does not have enough\nfree memory to load this text file."));
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
free(lpBuf);
|
2024-03-19 22:24:30 +01:00
|
|
|
return (wError == S_ERR_NO);
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
BOOL SaveObject(LPCTSTR szFilename) // separated stack reading part
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
|
|
|
HANDLE hFile;
|
2024-03-19 22:25:45 +01:00
|
|
|
LPBYTE pbyHeader;
|
2024-03-19 22:22:29 +01:00
|
|
|
DWORD lBytesWritten;
|
|
|
|
DWORD dwAddress;
|
|
|
|
DWORD dwLength;
|
|
|
|
|
|
|
|
dwAddress = RPL_Pick(1);
|
|
|
|
if (dwAddress == 0)
|
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("Too Few Arguments."));
|
2024-03-19 22:22:29 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
dwLength = (RPL_SkipOb(dwAddress) - dwAddress + 1) / 2;
|
|
|
|
|
|
|
|
hFile = CreateFile(szFilename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2024-03-19 22:37:03 +01:00
|
|
|
AbortMessage(_T("Cannot open file."));
|
2024-03-19 22:22:29 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
pbyHeader = (Chipset.type != 'X') ? (LPBYTE) BINARYHEADER48 : (LPBYTE) BINARYHEADER49;
|
2024-03-19 22:25:45 +01:00
|
|
|
WriteFile(hFile, pbyHeader, 8, &lBytesWritten, NULL);
|
|
|
|
|
2024-03-19 22:22:29 +01:00
|
|
|
while (dwLength--)
|
|
|
|
{
|
|
|
|
BYTE byByte = Read2(dwAddress);
|
|
|
|
WriteFile(hFile, &byByte, 1, &lBytesWritten, NULL);
|
|
|
|
dwAddress += 2;
|
|
|
|
}
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
//################
|
|
|
|
//#
|
|
|
|
//# Load Icon
|
|
|
|
//#
|
|
|
|
//################
|
|
|
|
|
|
|
|
BOOL LoadIconFromFile(LPCTSTR szFilename)
|
|
|
|
{
|
|
|
|
HANDLE hIcon;
|
|
|
|
|
|
|
|
SetCurrentDirectory(szEmuDirectory);
|
|
|
|
// not necessary to destroy because icon is shared
|
|
|
|
hIcon = LoadImage(NULL, szFilename, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE|LR_SHARED);
|
|
|
|
SetCurrentDirectory(szCurrentDirectory);
|
|
|
|
|
|
|
|
if (hIcon)
|
|
|
|
{
|
|
|
|
SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM) hIcon);
|
|
|
|
SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM) hIcon);
|
|
|
|
}
|
|
|
|
return hIcon != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID LoadIconDefault(VOID)
|
|
|
|
{
|
|
|
|
// use window class icon
|
|
|
|
SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM) NULL);
|
|
|
|
SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM) NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-03-19 22:22:29 +01:00
|
|
|
//################
|
|
|
|
//#
|
|
|
|
//# Load Bitmap
|
|
|
|
//#
|
|
|
|
//################
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
#define WIDTHBYTES(bits) ((((bits) + 31) / 32) * 4)
|
|
|
|
|
|
|
|
typedef struct _BmpFile
|
|
|
|
{
|
|
|
|
DWORD dwPos; // actual reading pos
|
|
|
|
DWORD dwFileSize; // file size
|
|
|
|
LPBYTE pbyFile; // buffer
|
|
|
|
} BMPFILE, FAR *LPBMPFILE, *PBMPFILE;
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
static __inline WORD DibNumColors(BITMAPINFOHEADER CONST *lpbi)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
if (lpbi->biClrUsed != 0) return (WORD) lpbi->biClrUsed;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 22:36:03 +01:00
|
|
|
/* a 24 bitcount DIB has no color table */
|
|
|
|
return (lpbi->biBitCount <= 8) ? (1 << lpbi->biBitCount) : 0;
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 23:33:35 +01:00
|
|
|
static HPALETTE CreateBIPalette(BITMAPINFOHEADER CONST *lpbi)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
|
|
|
LOGPALETTE* pPal;
|
|
|
|
HPALETTE hpal = NULL;
|
2024-03-19 23:35:30 +01:00
|
|
|
WORD wNumColors;
|
2024-03-19 22:22:29 +01:00
|
|
|
BYTE red;
|
|
|
|
BYTE green;
|
|
|
|
BYTE blue;
|
|
|
|
UINT i;
|
|
|
|
RGBQUAD* pRgb;
|
|
|
|
|
|
|
|
if (!lpbi)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (lpbi->biSize != sizeof(BITMAPINFOHEADER))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Get a pointer to the color table and the number of colors in it
|
2024-03-19 23:35:29 +01:00
|
|
|
pRgb = (RGBQUAD FAR *)((LPBYTE)lpbi + (WORD)lpbi->biSize);
|
2024-03-19 23:35:30 +01:00
|
|
|
wNumColors = DibNumColors(lpbi);
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
if (wNumColors)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
|
|
|
// Allocate for the logical palette structure
|
2024-03-19 23:35:30 +01:00
|
|
|
pPal = (PLOGPALETTE) malloc(sizeof(LOGPALETTE) + wNumColors * sizeof(PALETTEENTRY));
|
2024-03-19 23:35:30 +01:00
|
|
|
if (!pPal) return NULL;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
|
|
|
pPal->palVersion = 0x300;
|
2024-03-19 23:35:30 +01:00
|
|
|
pPal->palNumEntries = wNumColors;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
|
|
|
// Fill in the palette entries from the DIB color table and
|
|
|
|
// create a logical color palette.
|
2024-03-19 23:35:30 +01:00
|
|
|
for (i = 0; i < pPal->palNumEntries; i++)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
|
|
|
pPal->palPalEntry[i].peRed = pRgb[i].rgbRed;
|
|
|
|
pPal->palPalEntry[i].peGreen = pRgb[i].rgbGreen;
|
|
|
|
pPal->palPalEntry[i].peBlue = pRgb[i].rgbBlue;
|
2024-03-19 23:35:30 +01:00
|
|
|
pPal->palPalEntry[i].peFlags = 0;
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
|
|
|
hpal = CreatePalette(pPal);
|
2024-03-19 23:35:30 +01:00
|
|
|
free(pPal);
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-19 23:35:29 +01:00
|
|
|
// create halftone palette for 16, 24 and 32 bitcount bitmaps
|
|
|
|
|
|
|
|
// 16, 24 and 32 bitcount DIB's have no color table entries so, set the
|
|
|
|
// number of to the maximum value (256).
|
2024-03-19 23:35:30 +01:00
|
|
|
wNumColors = 256;
|
2024-03-19 23:35:30 +01:00
|
|
|
pPal = (PLOGPALETTE) malloc(sizeof(LOGPALETTE) + wNumColors * sizeof(PALETTEENTRY));
|
2024-03-19 23:35:29 +01:00
|
|
|
if (!pPal) return NULL;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
pPal->palVersion = 0x300;
|
2024-03-19 23:35:30 +01:00
|
|
|
pPal->palNumEntries = wNumColors;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
red = green = blue = 0;
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
// Generate 256 (= 8*8*4) RGB combinations to fill the palette
|
|
|
|
// entries.
|
|
|
|
for (i = 0; i < pPal->palNumEntries; i++)
|
|
|
|
{
|
|
|
|
pPal->palPalEntry[i].peRed = red;
|
|
|
|
pPal->palPalEntry[i].peGreen = green;
|
|
|
|
pPal->palPalEntry[i].peBlue = blue;
|
|
|
|
pPal->palPalEntry[i].peFlags = 0;
|
|
|
|
|
|
|
|
if (!(red += 32))
|
|
|
|
if (!(green += 32))
|
|
|
|
blue += 64;
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
hpal = CreatePalette(pPal);
|
2024-03-19 23:35:30 +01:00
|
|
|
free(pPal);
|
2024-03-19 22:22:29 +01:00
|
|
|
}
|
|
|
|
return hpal;
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
static HBITMAP DecodeBmp(LPBMPFILE pBmp)
|
|
|
|
{
|
|
|
|
LPBITMAPFILEHEADER pBmfh;
|
|
|
|
LPBITMAPINFO pBmi;
|
|
|
|
HBITMAP hBitmap;
|
|
|
|
DWORD dwFileSize;
|
|
|
|
|
|
|
|
hBitmap = NULL;
|
|
|
|
|
|
|
|
// size of bitmap header information
|
|
|
|
dwFileSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
|
|
|
|
if (pBmp->dwFileSize < dwFileSize) return NULL;
|
|
|
|
|
|
|
|
// check for bitmap
|
|
|
|
pBmfh = (LPBITMAPFILEHEADER) pBmp->pbyFile;
|
|
|
|
if (pBmfh->bfType != 0x4D42) return NULL; // "BM"
|
|
|
|
|
|
|
|
pBmi = (LPBITMAPINFO) (pBmp->pbyFile + sizeof(BITMAPFILEHEADER));
|
|
|
|
|
|
|
|
// size with color table
|
|
|
|
if (pBmi->bmiHeader.biCompression == BI_BITFIELDS)
|
|
|
|
{
|
|
|
|
dwFileSize += 3 * sizeof(DWORD);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dwFileSize += DibNumColors(&pBmi->bmiHeader) * sizeof(RGBQUAD);
|
|
|
|
}
|
|
|
|
if (dwFileSize != pBmfh->bfOffBits) return NULL;
|
|
|
|
|
|
|
|
// size with bitmap data
|
|
|
|
if (pBmi->bmiHeader.biCompression != BI_RGB)
|
|
|
|
{
|
|
|
|
dwFileSize += pBmi->bmiHeader.biSizeImage;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dwFileSize += WIDTHBYTES(pBmi->bmiHeader.biWidth * pBmi->bmiHeader.biBitCount)
|
|
|
|
* labs(pBmi->bmiHeader.biHeight);
|
|
|
|
}
|
|
|
|
if (pBmp->dwFileSize < dwFileSize) return NULL;
|
|
|
|
|
|
|
|
VERIFY(hBitmap = CreateDIBitmap(
|
|
|
|
hWindowDC,
|
|
|
|
&pBmi->bmiHeader,
|
|
|
|
CBM_INIT,
|
|
|
|
pBmp->pbyFile + pBmfh->bfOffBits,
|
|
|
|
pBmi, DIB_RGB_COLORS));
|
|
|
|
if (hBitmap == NULL) return NULL;
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
if (hPalette == NULL)
|
|
|
|
{
|
|
|
|
hPalette = CreateBIPalette(&pBmi->bmiHeader);
|
|
|
|
// save old palette
|
|
|
|
hOldPalette = SelectPalette(hWindowDC, hPalette, FALSE);
|
|
|
|
RealizePalette(hWindowDC);
|
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
return hBitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL ReadGifByte(LPBMPFILE pGif, INT *n)
|
|
|
|
{
|
|
|
|
// outside GIF file
|
|
|
|
if (pGif->dwPos >= pGif->dwFileSize)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
*n = pGif->pbyFile[pGif->dwPos++];
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL ReadGifWord(LPBMPFILE pGif, INT *n)
|
|
|
|
{
|
|
|
|
// outside GIF file
|
|
|
|
if (pGif->dwPos + 1 >= pGif->dwFileSize)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
*n = pGif->pbyFile[pGif->dwPos++];
|
|
|
|
*n |= (pGif->pbyFile[pGif->dwPos++] << 8);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
static HBITMAP DecodeGif(LPBMPFILE pBmp,DWORD *pdwTransparentColor)
|
2024-03-19 23:35:29 +01:00
|
|
|
{
|
|
|
|
// this implementation base on the GIF image file
|
|
|
|
// decoder engine of Free42 (c) by Thomas Okken
|
|
|
|
|
|
|
|
HBITMAP hBitmap;
|
|
|
|
|
|
|
|
typedef struct cmap
|
|
|
|
{
|
|
|
|
WORD biBitCount; // bits used in color map
|
|
|
|
DWORD biClrUsed; // no of colors in color map
|
|
|
|
RGBQUAD bmiColors[256]; // color map
|
|
|
|
} CMAP;
|
|
|
|
|
|
|
|
BOOL bHasGlobalCmap;
|
|
|
|
CMAP sGlb; // data of global color map
|
|
|
|
|
|
|
|
INT nWidth,nHeight,nInfo,nBackground,nZero;
|
|
|
|
LONG lBytesPerLine;
|
|
|
|
|
|
|
|
LPBYTE pbyPixels;
|
|
|
|
|
|
|
|
BITMAPINFO bmi; // global bitmap info
|
|
|
|
|
|
|
|
BOOL bDecoding = TRUE;
|
|
|
|
|
|
|
|
hBitmap = NULL;
|
|
|
|
|
|
|
|
pBmp->dwPos = 6; // position behind GIF header
|
|
|
|
|
|
|
|
/* Bits 6..4 of info contain one less than the "color resolution",
|
|
|
|
* defined as the number of significant bits per RGB component in
|
|
|
|
* the source image's color palette. If the source image (from
|
|
|
|
* which the GIF was generated) was 24-bit true color, the color
|
|
|
|
* resolution is 8, so the value in bits 6..4 is 7. If the source
|
|
|
|
* image had an EGA color cube (2x2x2), the color resolution would
|
|
|
|
* be 2, etc.
|
|
|
|
* Bit 3 of info must be zero in GIF87a; in GIF89a, if it is set,
|
|
|
|
* it indicates that the global colormap is sorted, the most
|
|
|
|
* important entries being first. In PseudoColor environments this
|
|
|
|
* can be used to make sure to get the most important colors from
|
|
|
|
* the X server first, to optimize the image's appearance in the
|
|
|
|
* event that not all the colors from the colormap can actually be
|
|
|
|
* obtained at the same time.
|
|
|
|
* The 'zero' field is always 0 in GIF87a; in GIF89a, it indicates
|
|
|
|
* the pixel aspect ratio, as (PAR + 15) : 64. If PAR is zero,
|
|
|
|
* this means no aspect ratio information is given, PAR = 1 means
|
|
|
|
* 1:4 (narrow), PAR = 49 means 1:1 (square), PAR = 255 means
|
|
|
|
* slightly over 4:1 (wide), etc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ReadGifWord(pBmp,&nWidth)
|
|
|
|
|| ReadGifWord(pBmp,&nHeight)
|
|
|
|
|| ReadGifByte(pBmp,&nInfo)
|
|
|
|
|| ReadGifByte(pBmp,&nBackground)
|
|
|
|
|| ReadGifByte(pBmp,&nZero)
|
|
|
|
|| nZero != 0)
|
|
|
|
goto quit;
|
|
|
|
|
|
|
|
ZeroMemory(&bmi,sizeof(bmi)); // init bitmap info
|
|
|
|
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
|
|
|
bmi.bmiHeader.biWidth = nWidth;
|
|
|
|
bmi.bmiHeader.biHeight = nHeight;
|
|
|
|
bmi.bmiHeader.biPlanes = 1;
|
2024-03-19 23:35:30 +01:00
|
|
|
bmi.bmiHeader.biBitCount = 24; // create a true color DIB
|
2024-03-19 23:35:29 +01:00
|
|
|
bmi.bmiHeader.biCompression = BI_RGB;
|
|
|
|
|
|
|
|
ZeroMemory(&sGlb,sizeof(sGlb)); // init global color map
|
|
|
|
bHasGlobalCmap = (nInfo & 0x80) != 0;
|
|
|
|
|
|
|
|
sGlb.biBitCount = (nInfo & 7) + 1; // bits used in global color map
|
|
|
|
sGlb.biClrUsed = (1 << sGlb.biBitCount); // no of colors in global color map
|
|
|
|
|
|
|
|
// color table should not exceed 256 colors
|
|
|
|
_ASSERT(sGlb.biClrUsed <= ARRAYSIZEOF(sGlb.bmiColors));
|
|
|
|
|
|
|
|
if (bHasGlobalCmap) // global color map
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; i < sGlb.biClrUsed; ++i)
|
|
|
|
{
|
|
|
|
int r, g, b;
|
|
|
|
|
|
|
|
if (ReadGifByte(pBmp,&r) || ReadGifByte(pBmp,&g) || ReadGifByte(pBmp,&b))
|
|
|
|
goto quit;
|
|
|
|
|
|
|
|
sGlb.bmiColors[i].rgbRed = r;
|
|
|
|
sGlb.bmiColors[i].rgbGreen = g;
|
|
|
|
sGlb.bmiColors[i].rgbBlue = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // no color map
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; i < sGlb.biClrUsed; ++i)
|
|
|
|
{
|
|
|
|
BYTE k = (BYTE) ((i * sGlb.biClrUsed) / (sGlb.biClrUsed - 1));
|
|
|
|
|
|
|
|
sGlb.bmiColors[i].rgbRed = k;
|
|
|
|
sGlb.bmiColors[i].rgbGreen = k;
|
|
|
|
sGlb.bmiColors[i].rgbBlue = k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// bitmap dimensions
|
|
|
|
lBytesPerLine = WIDTHBYTES(bmi.bmiHeader.biWidth * bmi.bmiHeader.biBitCount);
|
|
|
|
bmi.bmiHeader.biSizeImage = lBytesPerLine * bmi.bmiHeader.biHeight;
|
|
|
|
|
|
|
|
// create top-down DIB
|
|
|
|
bmi.bmiHeader.biHeight = -bmi.bmiHeader.biHeight;
|
|
|
|
|
|
|
|
// allocate buffer for pixels
|
|
|
|
VERIFY(hBitmap = CreateDIBSection(hWindowDC,
|
|
|
|
(LPBITMAPINFO)&bmi,
|
|
|
|
DIB_RGB_COLORS,
|
|
|
|
(VOID **)&pbyPixels,
|
|
|
|
NULL,
|
|
|
|
0));
|
|
|
|
if (hBitmap == NULL) goto quit;
|
|
|
|
|
|
|
|
// fill pixel buffer with background color
|
|
|
|
for (nHeight = 0; nHeight < labs(bmi.bmiHeader.biHeight); ++nHeight)
|
|
|
|
{
|
|
|
|
LPBYTE pbyLine = pbyPixels + nHeight * lBytesPerLine;
|
|
|
|
|
|
|
|
for (nWidth = 0; nWidth < bmi.bmiHeader.biWidth; ++nWidth)
|
|
|
|
{
|
|
|
|
*pbyLine++ = sGlb.bmiColors[nBackground].rgbBlue;
|
|
|
|
*pbyLine++ = sGlb.bmiColors[nBackground].rgbGreen;
|
|
|
|
*pbyLine++ = sGlb.bmiColors[nBackground].rgbRed;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ASSERT((DWORD) (pbyLine - pbyPixels) <= bmi.bmiHeader.biSizeImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (bDecoding)
|
|
|
|
{
|
|
|
|
INT nBlockType;
|
|
|
|
|
|
|
|
if (ReadGifByte(pBmp,&nBlockType)) goto quit;
|
|
|
|
|
|
|
|
switch (nBlockType)
|
|
|
|
{
|
|
|
|
case ',': // image
|
|
|
|
{
|
|
|
|
CMAP sAct; // data of actual color map
|
|
|
|
|
|
|
|
INT nLeft,nTop,nWidth,nHeight;
|
|
|
|
INT i,nInfo;
|
|
|
|
|
|
|
|
BOOL bInterlaced;
|
|
|
|
INT h,v;
|
|
|
|
INT nCodesize; // LZW codesize in bits
|
|
|
|
INT nBytecount;
|
|
|
|
|
|
|
|
SHORT prefix_table[4096];
|
|
|
|
SHORT code_table[4096];
|
|
|
|
|
|
|
|
INT nMaxcode;
|
|
|
|
INT nClearCode;
|
|
|
|
INT nEndCode;
|
|
|
|
|
|
|
|
INT nCurrCodesize;
|
|
|
|
INT nCurrCode;
|
|
|
|
INT nOldCode;
|
|
|
|
INT nBitsNeeded;
|
|
|
|
BOOL bEndCodeSeen;
|
|
|
|
|
|
|
|
// read image dimensions
|
|
|
|
if ( ReadGifWord(pBmp,&nLeft)
|
|
|
|
|| ReadGifWord(pBmp,&nTop)
|
|
|
|
|| ReadGifWord(pBmp,&nWidth)
|
|
|
|
|| ReadGifWord(pBmp,&nHeight)
|
|
|
|
|| ReadGifByte(pBmp,&nInfo))
|
|
|
|
goto quit;
|
|
|
|
|
|
|
|
if ( nTop + nHeight > labs(bmi.bmiHeader.biHeight)
|
|
|
|
|| nLeft + nWidth > bmi.bmiHeader.biWidth)
|
|
|
|
goto quit;
|
|
|
|
|
|
|
|
/* Bit 3 of info must be zero in GIF87a; in GIF89a, if it
|
|
|
|
* is set, it indicates that the local colormap is sorted,
|
|
|
|
* the most important entries being first. In PseudoColor
|
|
|
|
* environments this can be used to make sure to get the
|
|
|
|
* most important colors from the X server first, to
|
|
|
|
* optimize the image's appearance in the event that not
|
|
|
|
* all the colors from the colormap can actually be
|
|
|
|
* obtained at the same time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((nInfo & 0x80) == 0) // using global color map
|
|
|
|
{
|
|
|
|
sAct = sGlb;
|
|
|
|
}
|
|
|
|
else // using local color map
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
sAct.biBitCount = (nInfo & 7) + 1; // bits used in color map
|
|
|
|
sAct.biClrUsed = (1 << sAct.biBitCount); // no of colors in color map
|
|
|
|
|
|
|
|
for (i = 0; i < sAct.biClrUsed; ++i)
|
|
|
|
{
|
|
|
|
int r, g, b;
|
|
|
|
|
|
|
|
if (ReadGifByte(pBmp,&r) || ReadGifByte(pBmp,&g) || ReadGifByte(pBmp,&b))
|
|
|
|
goto quit;
|
|
|
|
|
|
|
|
sAct.bmiColors[i].rgbRed = r;
|
|
|
|
sAct.bmiColors[i].rgbGreen = g;
|
|
|
|
sAct.bmiColors[i].rgbBlue = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// interlaced image
|
|
|
|
bInterlaced = (nInfo & 0x40) != 0;
|
|
|
|
|
|
|
|
h = 0;
|
|
|
|
v = 0;
|
|
|
|
if ( ReadGifByte(pBmp,&nCodesize)
|
|
|
|
|| ReadGifByte(pBmp,&nBytecount))
|
|
|
|
goto quit;
|
|
|
|
|
|
|
|
nMaxcode = (1 << nCodesize);
|
|
|
|
|
|
|
|
// preset LZW table
|
|
|
|
for (i = 0; i < nMaxcode + 2; ++i)
|
|
|
|
{
|
|
|
|
prefix_table[i] = -1;
|
|
|
|
code_table[i] = i;
|
|
|
|
}
|
|
|
|
nClearCode = nMaxcode++;
|
|
|
|
nEndCode = nMaxcode++;
|
|
|
|
|
|
|
|
nCurrCodesize = nCodesize + 1;
|
|
|
|
nCurrCode = 0;
|
|
|
|
nOldCode = -1;
|
|
|
|
nBitsNeeded = nCurrCodesize;
|
|
|
|
bEndCodeSeen = FALSE;
|
|
|
|
|
|
|
|
while (nBytecount != 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < nBytecount; ++i)
|
|
|
|
{
|
|
|
|
INT nCurrByte;
|
|
|
|
INT nBitsAvailable;
|
|
|
|
|
|
|
|
if (ReadGifByte(pBmp,&nCurrByte))
|
|
|
|
goto quit;
|
|
|
|
|
|
|
|
if (bEndCodeSeen) continue;
|
|
|
|
|
|
|
|
nBitsAvailable = 8;
|
|
|
|
while (nBitsAvailable != 0)
|
|
|
|
{
|
|
|
|
INT nBitsCopied = (nBitsNeeded < nBitsAvailable)
|
|
|
|
? nBitsNeeded
|
|
|
|
: nBitsAvailable;
|
|
|
|
|
|
|
|
INT nBits = nCurrByte >> (8 - nBitsAvailable);
|
|
|
|
|
|
|
|
nBits &= 0xFF >> (8 - nBitsCopied);
|
|
|
|
nCurrCode |= nBits << (nCurrCodesize - nBitsNeeded);
|
|
|
|
nBitsAvailable -= nBitsCopied;
|
|
|
|
nBitsNeeded -= nBitsCopied;
|
|
|
|
|
|
|
|
if (nBitsNeeded == 0)
|
|
|
|
{
|
|
|
|
BYTE byExpanded[4096];
|
|
|
|
INT nExplen;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (nCurrCode == nEndCode)
|
|
|
|
{
|
|
|
|
bEndCodeSeen = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nCurrCode == nClearCode)
|
|
|
|
{
|
|
|
|
nMaxcode = (1 << nCodesize) + 2;
|
|
|
|
nCurrCodesize = nCodesize + 1;
|
|
|
|
nOldCode = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nCurrCode < nMaxcode)
|
|
|
|
{
|
|
|
|
if (nMaxcode < 4096 && nOldCode != -1)
|
|
|
|
{
|
|
|
|
INT c = nCurrCode;
|
|
|
|
while (prefix_table[c] != -1)
|
|
|
|
c = prefix_table[c];
|
|
|
|
c = code_table[c];
|
|
|
|
prefix_table[nMaxcode] = nOldCode;
|
|
|
|
code_table[nMaxcode] = c;
|
|
|
|
nMaxcode++;
|
|
|
|
if (nMaxcode == (1 << nCurrCodesize) && nCurrCodesize < 12)
|
|
|
|
nCurrCodesize++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
INT c;
|
|
|
|
|
|
|
|
if (nCurrCode > nMaxcode || nOldCode == -1) goto quit;
|
|
|
|
|
|
|
|
_ASSERT(nCurrCode == nMaxcode);
|
|
|
|
|
|
|
|
/* Once maxcode == 4096, we can't get here
|
|
|
|
* any more, because we refuse to raise
|
|
|
|
* nCurrCodeSize above 12 -- so we can
|
|
|
|
* never read a bigger code than 4095.
|
|
|
|
*/
|
|
|
|
|
|
|
|
c = nOldCode;
|
|
|
|
while (prefix_table[c] != -1)
|
|
|
|
c = prefix_table[c];
|
|
|
|
c = code_table[c];
|
|
|
|
prefix_table[nMaxcode] = nOldCode;
|
|
|
|
code_table[nMaxcode] = c;
|
|
|
|
nMaxcode++;
|
|
|
|
|
|
|
|
if (nMaxcode == (1 << nCurrCodesize) && nCurrCodesize < 12)
|
|
|
|
nCurrCodesize++;
|
|
|
|
}
|
|
|
|
nOldCode = nCurrCode;
|
|
|
|
|
|
|
|
// output nCurrCode!
|
|
|
|
nExplen = 0;
|
|
|
|
while (nCurrCode != -1)
|
|
|
|
{
|
|
|
|
_ASSERT(nExplen < ARRAYSIZEOF(byExpanded));
|
|
|
|
byExpanded[nExplen++] = (BYTE) code_table[nCurrCode];
|
|
|
|
nCurrCode = prefix_table[nCurrCode];
|
|
|
|
}
|
|
|
|
_ASSERT(nExplen > 0);
|
|
|
|
|
|
|
|
while (--nExplen >= 0)
|
|
|
|
{
|
|
|
|
// get color map index
|
|
|
|
BYTE byColIndex = byExpanded[nExplen];
|
|
|
|
|
|
|
|
LPBYTE pbyRgbr = pbyPixels + (lBytesPerLine * (nTop + v) + 3 * (nLeft + h));
|
|
|
|
|
|
|
|
_ASSERT(pbyRgbr + 2 < pbyPixels + bmi.bmiHeader.biSizeImage);
|
|
|
|
_ASSERT(byColIndex < sAct.biClrUsed);
|
|
|
|
|
|
|
|
*pbyRgbr++ = sAct.bmiColors[byColIndex].rgbBlue;
|
|
|
|
*pbyRgbr++ = sAct.bmiColors[byColIndex].rgbGreen;
|
|
|
|
*pbyRgbr = sAct.bmiColors[byColIndex].rgbRed;
|
|
|
|
|
|
|
|
if (++h == nWidth)
|
|
|
|
{
|
|
|
|
h = 0;
|
|
|
|
if (bInterlaced)
|
|
|
|
{
|
|
|
|
switch (v & 7)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
v += 8;
|
|
|
|
if (v < nHeight)
|
|
|
|
break;
|
|
|
|
/* Some GIF en/decoders go
|
|
|
|
* straight from the '0'
|
|
|
|
* pass to the '4' pass
|
|
|
|
* without checking the
|
|
|
|
* height, and blow up on
|
|
|
|
* 2/3/4 pixel high
|
|
|
|
* interlaced images.
|
|
|
|
*/
|
|
|
|
if (nHeight > 4)
|
|
|
|
v = 4;
|
|
|
|
else
|
|
|
|
if (nHeight > 2)
|
|
|
|
v = 2;
|
|
|
|
else
|
|
|
|
if (nHeight > 1)
|
|
|
|
v = 1;
|
|
|
|
else
|
|
|
|
bEndCodeSeen = TRUE;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
v += 8;
|
|
|
|
if (v >= nHeight)
|
|
|
|
v = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 6:
|
|
|
|
v += 4;
|
|
|
|
if (v >= nHeight)
|
|
|
|
v = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 3:
|
|
|
|
case 5:
|
|
|
|
case 7:
|
|
|
|
v += 2;
|
|
|
|
if (v >= nHeight)
|
|
|
|
bEndCodeSeen = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bEndCodeSeen)
|
|
|
|
break; // while (--nExplen >= 0)
|
|
|
|
}
|
|
|
|
else // non interlaced
|
|
|
|
{
|
|
|
|
if (++v == nHeight)
|
|
|
|
{
|
|
|
|
bEndCodeSeen = TRUE;
|
|
|
|
break; // while (--nExplen >= 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (FALSE);
|
|
|
|
|
|
|
|
nCurrCode = 0;
|
|
|
|
nBitsNeeded = nCurrCodesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReadGifByte(pBmp,&nBytecount))
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '!': // extension block
|
|
|
|
{
|
|
|
|
INT i,nFunctionCode,nByteCount,nDummy;
|
|
|
|
|
|
|
|
if (ReadGifByte(pBmp,&nFunctionCode)) goto quit;
|
|
|
|
if (ReadGifByte(pBmp,&nByteCount)) goto quit;
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
// Graphic Control Label & correct Block Size
|
|
|
|
if (nFunctionCode == 0xF9 && nByteCount == 0x04)
|
2024-03-19 23:35:29 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
INT nPackedFields,nColorIndex;
|
|
|
|
|
|
|
|
// packed fields
|
|
|
|
if (ReadGifByte(pBmp,&nPackedFields)) goto quit;
|
|
|
|
|
|
|
|
// delay time
|
|
|
|
if (ReadGifWord(pBmp,&nDummy)) goto quit;
|
|
|
|
|
|
|
|
// transparent color index
|
|
|
|
if (ReadGifByte(pBmp,&nColorIndex)) goto quit;
|
|
|
|
|
|
|
|
// transparent color flag set
|
|
|
|
if ((nPackedFields & 0x1) != 0)
|
2024-03-19 23:35:29 +01:00
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
if (pdwTransparentColor != NULL)
|
|
|
|
{
|
|
|
|
*pdwTransparentColor = RGB(sGlb.bmiColors[nColorIndex].rgbRed,
|
|
|
|
sGlb.bmiColors[nColorIndex].rgbGreen,
|
|
|
|
sGlb.bmiColors[nColorIndex].rgbBlue);
|
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
// block terminator (0 byte)
|
|
|
|
if (!(!ReadGifByte(pBmp,&nDummy) && nDummy == 0)) goto quit;
|
|
|
|
}
|
|
|
|
else // other function
|
|
|
|
{
|
|
|
|
while (nByteCount != 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < nByteCount; ++i)
|
|
|
|
{
|
|
|
|
if (ReadGifByte(pBmp,&nDummy)) goto quit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReadGifByte(pBmp,&nByteCount)) goto quit;
|
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ';': // terminator
|
|
|
|
bDecoding = FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: goto quit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:35:30 +01:00
|
|
|
_ASSERT(bDecoding == FALSE); // decoding successful
|
2024-03-19 23:35:29 +01:00
|
|
|
|
|
|
|
// normal decoding exit
|
2024-03-19 23:35:30 +01:00
|
|
|
if (hPalette == NULL)
|
|
|
|
{
|
|
|
|
hPalette = CreateBIPalette((PBITMAPINFOHEADER) &bmi);
|
|
|
|
// save old palette
|
|
|
|
hOldPalette = SelectPalette(hWindowDC, hPalette, FALSE);
|
|
|
|
RealizePalette(hWindowDC);
|
|
|
|
}
|
2024-03-19 23:35:29 +01:00
|
|
|
|
|
|
|
quit:
|
|
|
|
if (hBitmap != NULL && bDecoding) // creation failed
|
|
|
|
{
|
|
|
|
DeleteObject(hBitmap); // delete bitmap
|
|
|
|
hBitmap = NULL;
|
|
|
|
}
|
|
|
|
return hBitmap;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:37:03 +01:00
|
|
|
HBITMAP LoadBitmapFile(LPCTSTR szFilename)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
|
|
|
HANDLE hFile;
|
|
|
|
HANDLE hMap;
|
2024-03-19 23:35:29 +01:00
|
|
|
BMPFILE Bmp;
|
2024-03-19 22:22:29 +01:00
|
|
|
HBITMAP hBitmap;
|
2024-03-19 22:37:54 +01:00
|
|
|
|
2024-03-19 22:38:33 +01:00
|
|
|
SetCurrentDirectory(szEmuDirectory);
|
2024-03-19 22:22:29 +01:00
|
|
|
hFile = CreateFile(szFilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
SetCurrentDirectory(szCurrentDirectory);
|
2024-03-19 23:35:29 +01:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE) return NULL;
|
|
|
|
Bmp.dwFileSize = GetFileSize(hFile, NULL);
|
2024-03-19 22:24:30 +01:00
|
|
|
hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
|
2024-03-19 22:22:29 +01:00
|
|
|
if (hMap == NULL)
|
|
|
|
{
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
Bmp.pbyFile = (LPBYTE) MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
|
2024-03-19 23:35:29 +01:00
|
|
|
if (Bmp.pbyFile == NULL)
|
2024-03-19 22:22:29 +01:00
|
|
|
{
|
|
|
|
CloseHandle(hMap);
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-03-19 22:37:54 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
// check for bitmap file header "BM"
|
|
|
|
if (Bmp.dwFileSize >= 2 && *(WORD *) Bmp.pbyFile == 0x4D42)
|
|
|
|
{
|
|
|
|
hBitmap = DecodeBmp(&Bmp);
|
|
|
|
break;
|
|
|
|
}
|
2024-03-19 22:37:54 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
// check for GIF file header
|
|
|
|
if ( Bmp.dwFileSize >= 6
|
|
|
|
&& (memcmp(Bmp.pbyFile,"GIF87a",6) == 0 || memcmp(Bmp.pbyFile,"GIF89a",6) == 0))
|
|
|
|
{
|
2024-03-19 23:35:30 +01:00
|
|
|
hBitmap = DecodeGif(&Bmp,&dwTColor);
|
2024-03-19 23:35:29 +01:00
|
|
|
break;
|
|
|
|
}
|
2024-03-19 22:22:29 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
// unknown file type
|
|
|
|
hBitmap = NULL;
|
|
|
|
}
|
|
|
|
while (FALSE);
|
2024-03-19 22:37:54 +01:00
|
|
|
|
2024-03-19 23:35:29 +01:00
|
|
|
UnmapViewOfFile(Bmp.pbyFile);
|
2024-03-19 22:22:29 +01:00
|
|
|
CloseHandle(hMap);
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return hBitmap;
|
|
|
|
}
|
2024-03-19 23:35:30 +01:00
|
|
|
|
|
|
|
static BOOL AbsColorCmp(DWORD dwColor1,DWORD dwColor2,DWORD dwTol)
|
|
|
|
{
|
|
|
|
DWORD dwDiff;
|
|
|
|
|
|
|
|
dwDiff = (DWORD) abs((INT) (dwColor1 & 0xFF) - (INT) (dwColor2 & 0xFF));
|
|
|
|
dwColor1 >>= 8;
|
|
|
|
dwColor2 >>= 8;
|
|
|
|
dwDiff += (DWORD) abs((INT) (dwColor1 & 0xFF) - (INT) (dwColor2 & 0xFF));
|
|
|
|
dwColor1 >>= 8;
|
|
|
|
dwColor2 >>= 8;
|
|
|
|
dwDiff += (DWORD) abs((INT) (dwColor1 & 0xFF) - (INT) (dwColor2 & 0xFF));
|
|
|
|
|
|
|
|
return dwDiff > dwTol; // FALSE = colors match
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL LabColorCmp(DWORD dwColor1,DWORD dwColor2,DWORD dwTol)
|
|
|
|
{
|
|
|
|
DWORD dwDiff;
|
|
|
|
INT nDiffCol;
|
|
|
|
|
|
|
|
nDiffCol = (INT) (dwColor1 & 0xFF) - (INT) (dwColor2 & 0xFF);
|
|
|
|
dwDiff = (DWORD) (nDiffCol * nDiffCol);
|
|
|
|
dwColor1 >>= 8;
|
|
|
|
dwColor2 >>= 8;
|
|
|
|
nDiffCol = (INT) (dwColor1 & 0xFF) - (INT) (dwColor2 & 0xFF);
|
|
|
|
dwDiff += (DWORD) (nDiffCol * nDiffCol);
|
|
|
|
dwColor1 >>= 8;
|
|
|
|
dwColor2 >>= 8;
|
|
|
|
nDiffCol = (INT) (dwColor1 & 0xFF) - (INT) (dwColor2 & 0xFF);
|
|
|
|
dwDiff += (DWORD) (nDiffCol * nDiffCol);
|
|
|
|
dwTol *= dwTol;
|
|
|
|
|
|
|
|
return dwDiff > dwTol; // FALSE = colors match
|
|
|
|
}
|
|
|
|
|
|
|
|
HRGN CreateRgnFromBitmap(HBITMAP hBmp,COLORREF color,DWORD dwTol)
|
|
|
|
{
|
|
|
|
#define ADD_RECTS_COUNT 256
|
|
|
|
|
|
|
|
BOOL (*fnColorCmp)(DWORD dwColor1,DWORD dwColor2,DWORD dwTol);
|
|
|
|
|
|
|
|
HRGN hRgn;
|
|
|
|
LPRGNDATA pRgnData;
|
|
|
|
BITMAP bm;
|
|
|
|
LPBITMAPINFO bi;
|
|
|
|
LPBYTE pbyBits;
|
|
|
|
LPBYTE pbyColor;
|
|
|
|
DWORD dwAlignedWidthBytes;
|
|
|
|
DWORD dwBpp;
|
|
|
|
DWORD dwRectsCount;
|
|
|
|
LONG x,y,xleft;
|
|
|
|
BOOL bFoundLeft;
|
|
|
|
BOOL bIsMask;
|
|
|
|
HDC hDC;
|
|
|
|
|
|
|
|
if (dwTol >= 1000) // use CIE L*a*b compare
|
|
|
|
{
|
|
|
|
fnColorCmp = LabColorCmp;
|
|
|
|
dwTol -= 1000; // remove L*a*b compare selector
|
|
|
|
}
|
|
|
|
else // use Abs summation compare
|
|
|
|
{
|
|
|
|
fnColorCmp = AbsColorCmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get image properties
|
|
|
|
GetObject(hBmp,sizeof(bm),&bm);
|
|
|
|
|
|
|
|
// allocate memory for extended image information incl. RGBQUAD color table
|
|
|
|
bi = (LPBITMAPINFO) calloc(1,sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
|
|
|
|
bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
|
|
|
|
|
|
|
|
// get information about image
|
|
|
|
hDC = CreateCompatibleDC(hWindowDC);
|
|
|
|
GetDIBits(hDC,hBmp,0,bm.bmHeight,NULL,bi,DIB_RGB_COLORS);
|
|
|
|
|
|
|
|
// DWORD aligned bitmap width in BYTES
|
|
|
|
dwAlignedWidthBytes = WIDTHBYTES( bi->bmiHeader.biWidth
|
|
|
|
* bi->bmiHeader.biPlanes
|
|
|
|
* bi->bmiHeader.biBitCount);
|
|
|
|
|
|
|
|
// biSizeImage is empty
|
|
|
|
if (bi->bmiHeader.biSizeImage == 0 && bi->bmiHeader.biCompression == BI_RGB)
|
|
|
|
{
|
|
|
|
bi->bmiHeader.biSizeImage = dwAlignedWidthBytes * bi->bmiHeader.biHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate memory for image data (colors)
|
|
|
|
pbyBits = (LPBYTE) malloc(bi->bmiHeader.biSizeImage);
|
|
|
|
|
|
|
|
// fill bits buffer
|
|
|
|
GetDIBits(hDC,hBmp,0,bi->bmiHeader.biHeight,pbyBits,bi,DIB_RGB_COLORS);
|
|
|
|
DeleteDC(hDC);
|
|
|
|
|
|
|
|
// convert color if current DC is 16-bit RGB coded
|
|
|
|
if (bi->bmiHeader.biBitCount == 16)
|
|
|
|
{
|
|
|
|
// for 15 bit (5:5:5)
|
|
|
|
color = ((color & 0xF80000) >> 19)
|
|
|
|
| ((color & 0x00F800) >> 6)
|
|
|
|
| ((color & 0x0000F8) << 7);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// convert COLORREF to RGBQUAD color
|
|
|
|
color = ((color & 0xFF0000) >> 16)
|
|
|
|
| ((color & 0x00FF00) >> 0)
|
|
|
|
| ((color & 0x0000FF) << 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
dwBpp = bi->bmiHeader.biBitCount >> 3; // bytes per pixel
|
|
|
|
|
|
|
|
// DIB is bottom up image so we begin with the last scanline
|
|
|
|
pbyColor = pbyBits + (bi->bmiHeader.biHeight - 1) * dwAlignedWidthBytes;
|
|
|
|
|
|
|
|
dwRectsCount = bi->bmiHeader.biHeight; // number of rects in allocated buffer
|
|
|
|
|
|
|
|
bFoundLeft = FALSE; // set when mask has been found in current scan line
|
|
|
|
|
|
|
|
// allocate memory for region data
|
|
|
|
pRgnData = (PRGNDATA) malloc(sizeof(RGNDATAHEADER) + dwRectsCount * sizeof(RECT));
|
|
|
|
|
|
|
|
// fill it by default
|
|
|
|
ZeroMemory(&pRgnData->rdh,sizeof(pRgnData->rdh));
|
|
|
|
pRgnData->rdh.dwSize = sizeof(pRgnData->rdh);
|
|
|
|
pRgnData->rdh.iType = RDH_RECTANGLES;
|
|
|
|
SetRect(&pRgnData->rdh.rcBound,MAXLONG,MAXLONG,0,0);
|
|
|
|
|
|
|
|
for (y = 0; y < bi->bmiHeader.biHeight; ++y)
|
|
|
|
{
|
|
|
|
LPBYTE pbyLineStart = pbyColor;
|
|
|
|
|
|
|
|
for (x = 0; x < bi->bmiHeader.biWidth; ++x)
|
|
|
|
{
|
|
|
|
// get color
|
|
|
|
switch (bi->bmiHeader.biBitCount)
|
|
|
|
{
|
|
|
|
case 8:
|
|
|
|
bIsMask = fnColorCmp(*(LPDWORD)(&bi->bmiColors)[*pbyColor],color,dwTol);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
// it makes no sense to allow a tolerance here
|
|
|
|
bIsMask = (*(LPWORD)pbyColor != (WORD) color);
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
bIsMask = fnColorCmp((*(LPDWORD)pbyColor & 0x00ffffff),color,dwTol);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
bIsMask = fnColorCmp(*(LPDWORD)pbyColor,color,dwTol);
|
|
|
|
}
|
|
|
|
pbyColor += dwBpp; // shift pointer to next color
|
|
|
|
|
|
|
|
if (!bFoundLeft && bIsMask) // non transparent color found
|
|
|
|
{
|
|
|
|
xleft = x;
|
|
|
|
bFoundLeft = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bFoundLeft) // found non transparent color in scanline
|
|
|
|
{
|
|
|
|
// transparent color or last column
|
|
|
|
if (!bIsMask || x + 1 == bi->bmiHeader.biWidth)
|
|
|
|
{
|
|
|
|
// non transparent color and last column
|
|
|
|
if (bIsMask && x + 1 == bi->bmiHeader.biWidth)
|
|
|
|
++x;
|
|
|
|
|
|
|
|
// save current RECT
|
|
|
|
((LPRECT) pRgnData->Buffer)[pRgnData->rdh.nCount].left = xleft;
|
|
|
|
((LPRECT) pRgnData->Buffer)[pRgnData->rdh.nCount].top = y;
|
|
|
|
((LPRECT) pRgnData->Buffer)[pRgnData->rdh.nCount].right = x;
|
|
|
|
((LPRECT) pRgnData->Buffer)[pRgnData->rdh.nCount].bottom = y + 1;
|
|
|
|
pRgnData->rdh.nCount++;
|
|
|
|
|
|
|
|
if (xleft < pRgnData->rdh.rcBound.left)
|
|
|
|
pRgnData->rdh.rcBound.left = xleft;
|
|
|
|
|
|
|
|
if (y < pRgnData->rdh.rcBound.top)
|
|
|
|
pRgnData->rdh.rcBound.top = y;
|
|
|
|
|
|
|
|
if (x > pRgnData->rdh.rcBound.right)
|
|
|
|
pRgnData->rdh.rcBound.right = x;
|
|
|
|
|
|
|
|
if (y + 1 > pRgnData->rdh.rcBound.bottom)
|
|
|
|
pRgnData->rdh.rcBound.bottom = y + 1;
|
|
|
|
|
|
|
|
// if buffer full reallocate it with more room
|
|
|
|
if (pRgnData->rdh.nCount >= dwRectsCount)
|
|
|
|
{
|
|
|
|
dwRectsCount += ADD_RECTS_COUNT;
|
|
|
|
|
|
|
|
pRgnData = (LPRGNDATA) realloc(pRgnData,sizeof(RGNDATAHEADER) + dwRectsCount * sizeof(RECT));
|
|
|
|
}
|
|
|
|
|
|
|
|
bFoundLeft = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// previous scanline
|
|
|
|
pbyColor = pbyLineStart - dwAlignedWidthBytes;
|
|
|
|
}
|
|
|
|
// release image data
|
|
|
|
free(pbyBits);
|
|
|
|
free(bi);
|
|
|
|
|
|
|
|
// create region
|
|
|
|
hRgn = ExtCreateRegion(NULL,sizeof(RGNDATAHEADER) + pRgnData->rdh.nCount * sizeof(RECT),pRgnData);
|
|
|
|
|
|
|
|
free(pRgnData);
|
|
|
|
return hRgn;
|
|
|
|
#undef ADD_RECTS_COUNT
|
|
|
|
}
|