2006-02-25 20:50:29 +00:00
|
|
|
/*
|
|
|
|
AppleWin : An Apple //e emulator for Windows
|
|
|
|
|
|
|
|
Copyright (C) 1994-1996, Michael O'Brien
|
|
|
|
Copyright (C) 1999-2001, Oliver Schmidt
|
|
|
|
Copyright (C) 2002-2005, Tom Charlesworth
|
2010-01-03 18:43:08 +00:00
|
|
|
Copyright (C) 2006-2010, Tom Charlesworth, Michael Pohoreski
|
2006-02-25 20:50:29 +00:00
|
|
|
|
|
|
|
AppleWin is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
AppleWin is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with AppleWin; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Description: Disk Image
|
|
|
|
*
|
|
|
|
* Author: Various
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdAfx.h"
|
2014-08-14 16:48:38 +00:00
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
#include "DiskImage.h"
|
|
|
|
#include "DiskImageHelper.h"
|
2010-02-14 21:11:26 +00:00
|
|
|
|
2006-02-25 20:50:29 +00:00
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
static CDiskImageHelper sg_DiskImageHelper;
|
2016-09-19 21:14:57 +00:00
|
|
|
static CHardDiskImageHelper sg_HardDiskImageHelper;
|
2006-02-25 20:50:29 +00:00
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
// Pre: *pWriteProtected_ already set to file's r/w status - see DiskInsert()
|
|
|
|
ImageError_e ImageOpen( LPCTSTR pszImageFilename,
|
2016-10-25 20:09:48 +00:00
|
|
|
ImageInfo** ppImageInfo,
|
2015-02-13 22:40:53 +00:00
|
|
|
bool* pWriteProtected,
|
2010-01-03 18:43:08 +00:00
|
|
|
const bool bCreateIfNecessary,
|
2015-02-13 22:40:53 +00:00
|
|
|
std::string& strFilenameInZip,
|
2016-10-25 21:26:28 +00:00
|
|
|
const bool bExpectFloppy /*=true*/)
|
2009-02-17 22:53:52 +00:00
|
|
|
{
|
2016-10-25 21:26:28 +00:00
|
|
|
if (bExpectFloppy && sg_DiskImageHelper.GetWorkBuffer() == NULL)
|
|
|
|
return eIMAGE_ERROR_BAD_POINTER;
|
|
|
|
|
|
|
|
if (! (pszImageFilename && ppImageInfo && pWriteProtected))
|
2010-01-03 18:43:08 +00:00
|
|
|
return eIMAGE_ERROR_BAD_POINTER;
|
2009-02-24 22:13:46 +00:00
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
// CREATE A RECORD FOR THE FILE
|
|
|
|
*ppImageInfo = (ImageInfo*) VirtualAlloc(NULL, sizeof(ImageInfo), MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
if (*ppImageInfo == NULL)
|
2010-01-03 18:43:08 +00:00
|
|
|
return eIMAGE_ERROR_BAD_POINTER;
|
2009-02-17 22:53:52 +00:00
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
ZeroMemory(*ppImageInfo, sizeof(ImageInfo));
|
|
|
|
ImageInfo* pImageInfo = *ppImageInfo;
|
2015-02-13 22:40:53 +00:00
|
|
|
pImageInfo->bWriteProtected = *pWriteProtected;
|
2016-10-25 21:26:28 +00:00
|
|
|
if (bExpectFloppy) pImageInfo->pImageHelper = &sg_DiskImageHelper;
|
|
|
|
else pImageInfo->pImageHelper = &sg_HardDiskImageHelper;
|
2009-02-24 22:13:46 +00:00
|
|
|
|
2016-10-25 21:26:28 +00:00
|
|
|
ImageError_e Err = pImageInfo->pImageHelper->Open(pszImageFilename, pImageInfo, bCreateIfNecessary, strFilenameInZip);
|
2010-01-03 18:43:08 +00:00
|
|
|
if (Err != eIMAGE_ERROR_NONE)
|
2009-02-24 22:13:46 +00:00
|
|
|
{
|
2016-10-25 20:09:48 +00:00
|
|
|
ImageClose(*ppImageInfo, true);
|
|
|
|
*ppImageInfo = NULL;
|
2015-02-13 22:40:53 +00:00
|
|
|
return Err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pImageInfo->pImageType && pImageInfo->pImageType->GetType() == eImageHDV)
|
|
|
|
{
|
|
|
|
if (bExpectFloppy)
|
|
|
|
Err = eIMAGE_ERROR_UNSUPPORTED_HDV;
|
2010-01-03 18:43:08 +00:00
|
|
|
return Err;
|
2009-02-17 22:53:52 +00:00
|
|
|
}
|
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
// THE FILE MATCHES A KNOWN FORMAT
|
2009-02-17 22:53:52 +00:00
|
|
|
|
2016-10-25 21:26:28 +00:00
|
|
|
_ASSERT(bExpectFloppy);
|
|
|
|
if (!bExpectFloppy)
|
|
|
|
return eIMAGE_ERROR_UNSUPPORTED;
|
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
pImageInfo->uNumTracks = sg_DiskImageHelper.GetNumTracksInImage(pImageInfo->pImageType);
|
2009-02-17 22:53:52 +00:00
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
for (UINT uTrack = 0; uTrack < pImageInfo->uNumTracks; uTrack++)
|
|
|
|
pImageInfo->ValidTrack[uTrack] = (pImageInfo->uImageSize > 0) ? 1 : 0;
|
2006-02-25 20:50:29 +00:00
|
|
|
|
2015-02-13 22:40:53 +00:00
|
|
|
*pWriteProtected = pImageInfo->bWriteProtected;
|
2006-02-25 20:50:29 +00:00
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
return eIMAGE_ERROR_NONE;
|
2006-02-25 20:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
void ImageClose(ImageInfo* const pImageInfo, const bool bOpenError /*=false*/)
|
2010-01-03 18:43:08 +00:00
|
|
|
{
|
|
|
|
bool bDeleteFile = false;
|
2006-02-25 20:50:29 +00:00
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
if (!bOpenError)
|
|
|
|
{
|
2016-10-25 20:09:48 +00:00
|
|
|
for (UINT uTrack = 0; uTrack < pImageInfo->uNumTracks; uTrack++)
|
2010-01-03 18:43:08 +00:00
|
|
|
{
|
2016-10-25 20:09:48 +00:00
|
|
|
if (!pImageInfo->ValidTrack[uTrack])
|
2010-01-03 18:43:08 +00:00
|
|
|
{
|
2010-02-14 21:11:26 +00:00
|
|
|
// TODO: Comment using info from this URL:
|
|
|
|
// http://groups.google.de/group/comp.emulators.apple2/msg/7a1b9317e7905152
|
2010-01-03 18:43:08 +00:00
|
|
|
bDeleteFile = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-02-25 20:50:29 +00:00
|
|
|
|
2016-10-25 21:26:28 +00:00
|
|
|
pImageInfo->pImageHelper->Close(pImageInfo, bDeleteFile);
|
2006-02-25 20:50:29 +00:00
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
VirtualFree(pImageInfo, 0, MEM_RELEASE);
|
2006-02-25 20:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
BOOL ImageBoot(ImageInfo* const pImageInfo)
|
2009-02-17 22:53:52 +00:00
|
|
|
{
|
2010-01-03 18:43:08 +00:00
|
|
|
BOOL result = 0;
|
2006-02-25 20:50:29 +00:00
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
if (pImageInfo->pImageType->AllowBoot())
|
|
|
|
result = pImageInfo->pImageType->Boot(pImageInfo);
|
2006-02-25 20:50:29 +00:00
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
if (result)
|
2016-10-25 20:09:48 +00:00
|
|
|
pImageInfo->bWriteProtected = 1;
|
2006-02-25 20:50:29 +00:00
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
return result;
|
2006-02-25 20:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
void ImageDestroy(void)
|
|
|
|
{
|
|
|
|
VirtualFree(sg_DiskImageHelper.GetWorkBuffer(), 0, MEM_RELEASE);
|
|
|
|
sg_DiskImageHelper.SetWorkBuffer(NULL);
|
2006-02-25 20:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
void ImageInitialize(void)
|
|
|
|
{
|
|
|
|
LPBYTE pBuffer = (LPBYTE) VirtualAlloc(NULL, TRACK_DENIBBLIZED_SIZE*2, MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
sg_DiskImageHelper.SetWorkBuffer(pBuffer);
|
2006-02-25 20:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
void ImageReadTrack( ImageInfo* const pImageInfo,
|
2010-01-03 18:43:08 +00:00
|
|
|
const int nTrack,
|
|
|
|
const int nQuarterTrack,
|
|
|
|
LPBYTE pTrackImageBuffer,
|
|
|
|
int* pNibbles)
|
2006-03-09 21:40:16 +00:00
|
|
|
{
|
2010-01-03 18:43:08 +00:00
|
|
|
_ASSERT(nTrack >= 0);
|
|
|
|
if (nTrack < 0)
|
|
|
|
return;
|
2006-03-09 21:40:16 +00:00
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
if (pImageInfo->pImageType->AllowRW() && pImageInfo->ValidTrack[nTrack])
|
2006-03-09 21:40:16 +00:00
|
|
|
{
|
2016-10-25 20:09:48 +00:00
|
|
|
pImageInfo->pImageType->Read(pImageInfo, nTrack, nQuarterTrack, pTrackImageBuffer, pNibbles);
|
2006-03-09 21:40:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-03 18:43:08 +00:00
|
|
|
for (*pNibbles = 0; *pNibbles < NIBBLES_PER_TRACK; (*pNibbles)++)
|
|
|
|
pTrackImageBuffer[*pNibbles] = (BYTE)(rand() & 0xFF);
|
2006-03-09 21:40:16 +00:00
|
|
|
}
|
2010-01-03 18:43:08 +00:00
|
|
|
}
|
2006-03-09 21:40:16 +00:00
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
//===========================================================================
|
2006-03-09 21:40:16 +00:00
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
void ImageWriteTrack( ImageInfo* const pImageInfo,
|
2010-01-03 18:43:08 +00:00
|
|
|
const int nTrack,
|
|
|
|
const int nQuarterTrack,
|
|
|
|
LPBYTE pTrackImage,
|
|
|
|
const int nNibbles)
|
|
|
|
{
|
|
|
|
_ASSERT(nTrack >= 0);
|
|
|
|
if (nTrack < 0)
|
|
|
|
return;
|
2006-03-09 21:40:16 +00:00
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
if (pImageInfo->pImageType->AllowRW() && !pImageInfo->bWriteProtected)
|
2010-01-03 18:43:08 +00:00
|
|
|
{
|
2016-10-25 20:09:48 +00:00
|
|
|
pImageInfo->pImageType->Write(pImageInfo, nTrack, nQuarterTrack, pTrackImage, nNibbles);
|
|
|
|
pImageInfo->ValidTrack[nTrack] = 1;
|
2010-01-03 18:43:08 +00:00
|
|
|
}
|
2006-02-25 20:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
bool ImageReadBlock( ImageInfo* const pImageInfo,
|
2015-02-13 22:40:53 +00:00
|
|
|
UINT nBlock,
|
|
|
|
LPBYTE pBlockBuffer)
|
|
|
|
{
|
|
|
|
bool bRes = false;
|
2016-10-25 20:09:48 +00:00
|
|
|
if (pImageInfo->pImageType->AllowRW())
|
|
|
|
bRes = pImageInfo->pImageType->Read(pImageInfo, nBlock, pBlockBuffer);
|
2015-02-13 22:40:53 +00:00
|
|
|
|
|
|
|
return bRes;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
bool ImageWriteBlock( ImageInfo* const pImageInfo,
|
2015-02-13 22:40:53 +00:00
|
|
|
UINT nBlock,
|
|
|
|
LPBYTE pBlockBuffer)
|
|
|
|
{
|
|
|
|
bool bRes = false;
|
2016-10-25 20:09:48 +00:00
|
|
|
if (pImageInfo->pImageType->AllowRW() && !pImageInfo->bWriteProtected)
|
|
|
|
bRes = pImageInfo->pImageType->Write(pImageInfo, nBlock, pBlockBuffer);
|
2015-02-13 22:40:53 +00:00
|
|
|
|
|
|
|
return bRes;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
int ImageGetNumTracks(ImageInfo* const pImageInfo)
|
2010-01-03 18:43:08 +00:00
|
|
|
{
|
2016-10-25 20:09:48 +00:00
|
|
|
return pImageInfo ? pImageInfo->uNumTracks : 0;
|
2006-02-25 20:50:29 +00:00
|
|
|
}
|
2009-05-29 21:39:13 +00:00
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
bool ImageIsWriteProtected(ImageInfo* const pImageInfo)
|
2010-01-03 18:43:08 +00:00
|
|
|
{
|
2016-10-25 20:09:48 +00:00
|
|
|
return pImageInfo ? pImageInfo->bWriteProtected : true;
|
2010-01-03 18:43:08 +00:00
|
|
|
}
|
2009-05-29 21:39:13 +00:00
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
bool ImageIsMultiFileZip(ImageInfo* const pImageInfo)
|
2009-05-29 21:39:13 +00:00
|
|
|
{
|
2016-10-25 20:09:48 +00:00
|
|
|
return pImageInfo ? (pImageInfo->uNumEntriesInZip > 1) : false;
|
2009-05-29 21:39:13 +00:00
|
|
|
}
|
2015-02-13 22:40:53 +00:00
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
const char* ImageGetPathname(ImageInfo* const pImageInfo)
|
2015-02-13 22:40:53 +00:00
|
|
|
{
|
2018-02-24 15:12:40 +00:00
|
|
|
static const char* szEmpty = "";
|
2016-10-25 20:09:48 +00:00
|
|
|
return pImageInfo ? pImageInfo->szFilename : szEmpty;
|
2015-02-13 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 20:09:48 +00:00
|
|
|
UINT ImageGetImageSize(ImageInfo* const pImageInfo)
|
2015-02-13 22:40:53 +00:00
|
|
|
{
|
2016-10-25 20:09:48 +00:00
|
|
|
return pImageInfo ? pImageInfo->uImageSize : 0;
|
2015-02-13 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetImageTitle(LPCTSTR pPathname, TCHAR* pImageName, TCHAR* pFullName)
|
|
|
|
{
|
|
|
|
TCHAR imagetitle[ MAX_DISK_FULL_NAME+1 ];
|
|
|
|
LPCTSTR startpos = pPathname;
|
|
|
|
|
|
|
|
// imagetitle = <FILENAME.EXT>
|
|
|
|
if (_tcsrchr(startpos, TEXT('\\')))
|
|
|
|
startpos = _tcsrchr(startpos, TEXT('\\'))+1;
|
|
|
|
|
|
|
|
_tcsncpy(imagetitle, startpos, MAX_DISK_FULL_NAME);
|
|
|
|
imagetitle[MAX_DISK_FULL_NAME] = 0;
|
|
|
|
|
|
|
|
// if imagetitle contains a lowercase char, then found=1 (why?)
|
|
|
|
BOOL found = 0;
|
|
|
|
int loop = 0;
|
|
|
|
while (imagetitle[loop] && !found)
|
|
|
|
{
|
|
|
|
if (IsCharLower(imagetitle[loop]))
|
|
|
|
found = 1;
|
|
|
|
else
|
|
|
|
loop++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!found) && (loop > 2))
|
|
|
|
CharLowerBuff(imagetitle+1, _tcslen(imagetitle+1));
|
|
|
|
|
|
|
|
// pFullName = <FILENAME.EXT>
|
|
|
|
_tcsncpy( pFullName, imagetitle, MAX_DISK_FULL_NAME );
|
|
|
|
pFullName[ MAX_DISK_FULL_NAME ] = 0;
|
|
|
|
|
|
|
|
if (imagetitle[0])
|
|
|
|
{
|
|
|
|
LPTSTR dot = imagetitle;
|
|
|
|
if (_tcsrchr(dot, TEXT('.')))
|
|
|
|
dot = _tcsrchr(dot, TEXT('.'));
|
|
|
|
if (dot > imagetitle)
|
|
|
|
*dot = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pImageName = <FILENAME> (ie. no extension)
|
|
|
|
_tcsncpy( pImageName, imagetitle, MAX_DISK_IMAGE_NAME );
|
|
|
|
pImageName[ MAX_DISK_IMAGE_NAME ] = 0;
|
|
|
|
}
|