Refactor of Disk.cpp and Disk.h in preparation for #543.

Changed struct Disk_t:
. Added new struct Drive_t with "has-a" relationship to Disk_t
. Split properties of drive into Drive_t and properties of disk into Disk_t
This commit is contained in:
tomcw 2018-02-25 13:38:04 +00:00
parent 70540bd6dc
commit 92504e0751
4 changed files with 226 additions and 204 deletions

View File

@ -57,8 +57,31 @@ BOOL enhancedisk = 1; // TODO: Make static & add accessor funcs
// Private ________________________________________________________________________________________ // Private ________________________________________________________________________________________
struct Drive_t
{
int phase;
int track;
DWORD spinning;
DWORD writelight;
Disk_t disk;
Drive_t()
{
clear();
}
void clear()
{
phase = 0;
track = 0;
spinning = 0;
writelight = 0;
disk.clear();
}
};
static WORD currdrive = 0; static WORD currdrive = 0;
static Disk_t g_aFloppyDisk[NUM_DRIVES]; static Drive_t g_aFloppyDrive[NUM_DRIVES];
static BYTE floppylatch = 0; static BYTE floppylatch = 0;
static BOOL floppymotoron = 0; static BOOL floppymotoron = 0;
static BOOL floppyloadmode = 0; // for efficiency this is not used; it's extremely unlikely to affect emulation (nickw) static BOOL floppyloadmode = 0; // for efficiency this is not used; it's extremely unlikely to affect emulation (nickw)
@ -82,31 +105,31 @@ static LPCTSTR DiskGetFullPathName(const int iDrive);
//=========================================================================== //===========================================================================
int DiskGetCurrentDrive(void) { return currdrive; } int DiskGetCurrentDrive(void) { return currdrive; }
int DiskGetCurrentTrack(void) { return g_aFloppyDisk[currdrive].track; } int DiskGetCurrentTrack(void) { return g_aFloppyDrive[currdrive].track; }
int DiskGetCurrentPhase(void) { return g_aFloppyDisk[currdrive].phase; } int DiskGetCurrentPhase(void) { return g_aFloppyDrive[currdrive].phase; }
int DiskGetCurrentOffset(void) { return g_aFloppyDisk[currdrive].byte; } int DiskGetCurrentOffset(void) { return g_aFloppyDrive[currdrive].disk.byte; }
int DiskGetTrack( int drive ) { return g_aFloppyDisk[ drive ].track; } int DiskGetTrack( int drive ) { return g_aFloppyDrive[ drive ].track; }
const char* DiskGetDiskPathFilename(const int iDrive) const char* DiskGetDiskPathFilename(const int iDrive)
{ {
return g_aFloppyDisk[iDrive].fullname; return g_aFloppyDrive[iDrive].disk.fullname;
} }
const char* DiskGetCurrentState(void) const char* DiskGetCurrentState(void)
{ {
if (g_aFloppyDisk[currdrive].imagehandle == NULL) if (g_aFloppyDrive[currdrive].disk.imagehandle == NULL)
return "Empty"; return "Empty";
if (!floppymotoron) if (!floppymotoron)
{ {
if (g_aFloppyDisk[currdrive].spinning > 0) if (g_aFloppyDrive[currdrive].spinning > 0)
return "Off (spinning)"; return "Off (spinning)";
else else
return "Off"; return "Off";
} }
else if (floppywritemode) else if (floppywritemode)
{ {
if (g_aFloppyDisk[currdrive].bWriteProtected) if (g_aFloppyDrive[currdrive].disk.bWriteProtected)
return "Writing (write protected)"; return "Writing (write protected)";
else else
return "Writing"; return "Writing";
@ -115,7 +138,7 @@ const char* DiskGetCurrentState(void)
{ {
/*if (floppyloadmode) /*if (floppyloadmode)
{ {
if (g_aFloppyDisk[currdrive].bWriteProtected) if (g_aFloppyDrive[currdrive].bWriteProtected)
return "Reading write protect state (write protected)"; return "Reading write protect state (write protected)";
else else
return "Reading write protect state (not write protected)"; return "Reading write protect state (not write protected)";
@ -158,7 +181,7 @@ void Disk_SaveLastDiskImage(const int iDrive)
if (!g_bSaveDiskImage) if (!g_bSaveDiskImage)
return; return;
const char *pFileName = g_aFloppyDisk[iDrive].fullname; const char *pFileName = g_aFloppyDrive[iDrive].disk.fullname;
if (iDrive == DRIVE_1) if (iDrive == DRIVE_1)
RegSaveString(TEXT(REG_PREFS), REGVALUE_PREF_LAST_DISK_1, TRUE, pFileName); RegSaveString(TEXT(REG_PREFS), REGVALUE_PREF_LAST_DISK_1, TRUE, pFileName);
@ -182,10 +205,10 @@ void Disk_SaveLastDiskImage(const int iDrive)
// Called by DiskControlMotor() & DiskEnable() // Called by DiskControlMotor() & DiskEnable()
static void CheckSpinning(const ULONG nCyclesLeft) static void CheckSpinning(const ULONG nCyclesLeft)
{ {
DWORD modechange = (floppymotoron && !g_aFloppyDisk[currdrive].spinning); DWORD modechange = (floppymotoron && !g_aFloppyDrive[currdrive].spinning);
if (floppymotoron) if (floppymotoron)
g_aFloppyDisk[currdrive].spinning = SPINNING_CYCLES; g_aFloppyDrive[currdrive].spinning = SPINNING_CYCLES;
if (modechange) if (modechange)
FrameDrawDiskLEDS( (HDC)0 ); FrameDrawDiskLEDS( (HDC)0 );
@ -204,20 +227,22 @@ static Disk_Status_e GetDriveLightStatus(const int iDrive)
{ {
if (IsDriveValid( iDrive )) if (IsDriveValid( iDrive ))
{ {
Disk_t *pFloppy = & g_aFloppyDisk[ iDrive ]; Drive_t* pDrive = &g_aFloppyDrive[ iDrive ];
if (pFloppy->spinning) if (pDrive->spinning)
{ {
if (pFloppy->bWriteProtected) if (pDrive->disk.bWriteProtected)
return DISK_STATUS_PROT; return DISK_STATUS_PROT;
if (pFloppy->writelight) if (pDrive->writelight)
return DISK_STATUS_WRITE; return DISK_STATUS_WRITE;
else else
return DISK_STATUS_READ; return DISK_STATUS_READ;
} }
else else
{
return DISK_STATUS_OFF; return DISK_STATUS_OFF;
}
} }
return DISK_STATUS_OFF; return DISK_STATUS_OFF;
@ -234,8 +259,8 @@ static bool IsDriveValid(const int iDrive)
static void AllocTrack(const int iDrive) static void AllocTrack(const int iDrive)
{ {
Disk_t * fptr = &g_aFloppyDisk[iDrive]; Drive_t* pDrive = &g_aFloppyDrive[iDrive];
fptr->trackimage = (LPBYTE)VirtualAlloc(NULL, NIBBLES_PER_TRACK, MEM_COMMIT, PAGE_READWRITE); pDrive->disk.trackimage = (LPBYTE)VirtualAlloc(NULL, NIBBLES_PER_TRACK, MEM_COMMIT, PAGE_READWRITE);
} }
//=========================================================================== //===========================================================================
@ -245,26 +270,27 @@ static void ReadTrack(const int iDrive)
if (! IsDriveValid( iDrive )) if (! IsDriveValid( iDrive ))
return; return;
Disk_t *pFloppy = &g_aFloppyDisk[ iDrive ]; Drive_t* pDrive = &g_aFloppyDrive[ iDrive ];
Disk_t* pFloppy = &pDrive->disk;
if (pFloppy->track >= ImageGetNumTracks(pFloppy->imagehandle)) if (pDrive->track >= ImageGetNumTracks(pFloppy->imagehandle))
{ {
pFloppy->trackimagedata = 0; pFloppy->trackimagedata = 0;
return; return;
} }
if (! pFloppy->trackimage) if (!pFloppy->trackimage)
AllocTrack( iDrive ); AllocTrack( iDrive );
if (pFloppy->trackimage && pFloppy->imagehandle) if (pFloppy->trackimage && pFloppy->imagehandle)
{ {
#if LOG_DISK_TRACKS #if LOG_DISK_TRACKS
LOG_DISK("track $%02X%s read\r\n", pFloppy->track, (pFloppy->phase & 1) ? ".5" : " "); LOG_DISK("track $%02X%s read\r\n", pDrive->track, (pDrive->phase & 1) ? ".5" : " ");
#endif #endif
ImageReadTrack( ImageReadTrack(
pFloppy->imagehandle, pFloppy->imagehandle,
pFloppy->track, pDrive->track,
pFloppy->phase, pDrive->phase,
pFloppy->trackimage, pFloppy->trackimage,
&pFloppy->nibbles); &pFloppy->nibbles);
@ -277,7 +303,7 @@ static void ReadTrack(const int iDrive)
void DiskFlushCurrentTrack(const int iDrive) void DiskFlushCurrentTrack(const int iDrive)
{ {
Disk_t *pFloppy = &g_aFloppyDisk[iDrive]; Disk_t* pFloppy = &g_aFloppyDrive[iDrive].disk;
if (pFloppy->trackimage && pFloppy->trackimagedirty) if (pFloppy->trackimage && pFloppy->trackimagedirty)
WriteTrack(iDrive); WriteTrack(iDrive);
@ -287,7 +313,7 @@ void DiskFlushCurrentTrack(const int iDrive)
static void RemoveDisk(const int iDrive) static void RemoveDisk(const int iDrive)
{ {
Disk_t *pFloppy = &g_aFloppyDisk[iDrive]; Disk_t* pFloppy = &g_aFloppyDrive[iDrive].disk;
if (pFloppy->imagehandle) if (pFloppy->imagehandle)
{ {
@ -299,7 +325,7 @@ static void RemoveDisk(const int iDrive)
if (pFloppy->trackimage) if (pFloppy->trackimage)
{ {
VirtualFree(pFloppy->trackimage,0,MEM_RELEASE); VirtualFree(pFloppy->trackimage, 0, MEM_RELEASE);
pFloppy->trackimage = NULL; pFloppy->trackimage = NULL;
pFloppy->trackimagedata = 0; pFloppy->trackimagedata = 0;
} }
@ -316,9 +342,10 @@ static void RemoveDisk(const int iDrive)
static void WriteTrack(const int iDrive) static void WriteTrack(const int iDrive)
{ {
Disk_t *pFloppy = &g_aFloppyDisk[ iDrive ]; Drive_t* pDrive = &g_aFloppyDrive[ iDrive ];
Disk_t* pFloppy = &pDrive->disk;
if (pFloppy->track >= ImageGetNumTracks(pFloppy->imagehandle)) if (pDrive->track >= ImageGetNumTracks(pFloppy->imagehandle))
return; return;
if (pFloppy->bWriteProtected) if (pFloppy->bWriteProtected)
@ -327,12 +354,12 @@ static void WriteTrack(const int iDrive)
if (pFloppy->trackimage && pFloppy->imagehandle) if (pFloppy->trackimage && pFloppy->imagehandle)
{ {
#if LOG_DISK_TRACKS #if LOG_DISK_TRACKS
LOG_DISK("track $%02X%s write\r\n", pFloppy->track, (pFloppy->phase & 0) ? ".5" : " "); // TODO: hard-coded to whole tracks - see below (nickw) LOG_DISK("track $%02X%s write\r\n", pDrive->track, (pDrive->phase & 0) ? ".5" : " "); // TODO: hard-coded to whole tracks - see below (nickw)
#endif #endif
ImageWriteTrack( ImageWriteTrack(
pFloppy->imagehandle, pFloppy->imagehandle,
pFloppy->track, pDrive->track,
pFloppy->phase, // TODO: this should never be used; it's the current phase (half-track), not that of the track to be written (nickw) pDrive->phase, // TODO: this should never be used; it's the current phase (half-track), not that of the track to be written (nickw)
pFloppy->trackimage, pFloppy->trackimage,
pFloppy->nibbles); pFloppy->nibbles);
} }
@ -350,7 +377,7 @@ void DiskBoot(void)
{ {
// THIS FUNCTION RELOADS A PROGRAM IMAGE IF ONE IS LOADED IN DRIVE ONE. // THIS FUNCTION RELOADS A PROGRAM IMAGE IF ONE IS LOADED IN DRIVE ONE.
// IF A DISK IMAGE OR NO IMAGE IS LOADED IN DRIVE ONE, IT DOES NOTHING. // IF A DISK IMAGE OR NO IMAGE IS LOADED IN DRIVE ONE, IT DOES NOTHING.
if (g_aFloppyDisk[0].imagehandle && ImageBoot(g_aFloppyDisk[0].imagehandle)) if (g_aFloppyDrive[0].disk.imagehandle && ImageBoot(g_aFloppyDrive[0].disk.imagehandle))
floppymotoron = 0; floppymotoron = 0;
} }
@ -376,11 +403,12 @@ static void __stdcall DiskControlMotor(WORD, WORD address, BYTE, BYTE, ULONG uEx
static void __stdcall DiskControlStepper(WORD, WORD address, BYTE, BYTE, ULONG uExecutedCycles) static void __stdcall DiskControlStepper(WORD, WORD address, BYTE, BYTE, ULONG uExecutedCycles)
{ {
Disk_t * fptr = &g_aFloppyDisk[currdrive]; Drive_t* pDrive = &g_aFloppyDrive[currdrive];
Disk_t* pFloppy = &pDrive->disk;
if (!floppymotoron) // GH#525 if (!floppymotoron) // GH#525
{ {
if (!fptr->spinning) if (!pDrive->spinning)
{ {
#if LOG_DISK_PHASES #if LOG_DISK_PHASES
LOG_DISK("stepper accessed whilst motor is off and not spinning\r\n"); LOG_DISK("stepper accessed whilst motor is off and not spinning\r\n");
@ -415,23 +443,23 @@ static void __stdcall DiskControlStepper(WORD, WORD address, BYTE, BYTE, ULONG u
// - do not move if both adjacent magnets are on // - do not move if both adjacent magnets are on
// momentum and timing are not accounted for ... maybe one day! // momentum and timing are not accounted for ... maybe one day!
int direction = 0; int direction = 0;
if (phases & (1 << ((fptr->phase + 1) & 3))) if (phases & (1 << ((pDrive->phase + 1) & 3)))
direction += 1; direction += 1;
if (phases & (1 << ((fptr->phase + 3) & 3))) if (phases & (1 << ((pDrive->phase + 3) & 3)))
direction -= 1; direction -= 1;
// apply magnet step, if any // apply magnet step, if any
if (direction) if (direction)
{ {
fptr->phase = MAX(0, MIN(79, fptr->phase + direction)); pDrive->phase = MAX(0, MIN(79, pDrive->phase + direction));
const int nNumTracksInImage = ImageGetNumTracks(fptr->imagehandle); const int nNumTracksInImage = ImageGetNumTracks(pFloppy->imagehandle);
const int newtrack = (nNumTracksInImage == 0) ? 0 const int newtrack = (nNumTracksInImage == 0) ? 0
: MIN(nNumTracksInImage-1, fptr->phase >> 1); // (round half tracks down) : MIN(nNumTracksInImage-1, pDrive->phase >> 1); // (round half tracks down)
if (newtrack != fptr->track) if (newtrack != pDrive->track)
{ {
DiskFlushCurrentTrack(currdrive); DiskFlushCurrentTrack(currdrive);
fptr->track = newtrack; pDrive->track = newtrack;
fptr->trackimagedata = 0; pFloppy->trackimagedata = 0;
g_formatTrack.DriveNotWritingTrack(); g_formatTrack.DriveNotWritingTrack();
} }
@ -446,8 +474,8 @@ static void __stdcall DiskControlStepper(WORD, WORD address, BYTE, BYTE, ULONG u
#if LOG_DISK_PHASES #if LOG_DISK_PHASES
LOG_DISK("track $%02X%s phases %d%d%d%d phase %d %s address $%4X\r\n", LOG_DISK("track $%02X%s phases %d%d%d%d phase %d %s address $%4X\r\n",
fptr->phase >> 1, pDrive->phase >> 1,
(fptr->phase & 1) ? ".5" : " ", (pDrive->phase & 1) ? ".5" : " ",
(phases >> 3) & 1, (phases >> 3) & 1,
(phases >> 2) & 1, (phases >> 2) & 1,
(phases >> 1) & 1, (phases >> 1) & 1,
@ -479,8 +507,8 @@ static void __stdcall DiskEnable(WORD, WORD address, BYTE, BYTE, ULONG uExecuted
#if LOG_DISK_ENABLE_DRIVE #if LOG_DISK_ENABLE_DRIVE
LOG_DISK("enable drive: %d\r\n", currdrive); LOG_DISK("enable drive: %d\r\n", currdrive);
#endif #endif
g_aFloppyDisk[!currdrive].spinning = 0; g_aFloppyDrive[!currdrive].spinning = 0;
g_aFloppyDisk[!currdrive].writelight = 0; g_aFloppyDrive[!currdrive].writelight = 0;
CheckSpinning(uExecutedCycles); CheckSpinning(uExecutedCycles);
} }
@ -500,39 +528,39 @@ void DiskEject(const int iDrive)
// . Used by Property Sheet Page (Disk) // . Used by Property Sheet Page (Disk)
LPCTSTR DiskGetFullName(const int iDrive) LPCTSTR DiskGetFullName(const int iDrive)
{ {
return g_aFloppyDisk[iDrive].fullname; return g_aFloppyDrive[iDrive].disk.fullname;
} }
// Return the filename // Return the filename
// . Used by Drive Buttons' tooltips // . Used by Drive Buttons' tooltips
LPCTSTR DiskGetFullDiskFilename(const int iDrive) LPCTSTR DiskGetFullDiskFilename(const int iDrive)
{ {
if (!g_aFloppyDisk[iDrive].strFilenameInZip.empty()) if (!g_aFloppyDrive[iDrive].disk.strFilenameInZip.empty())
return g_aFloppyDisk[iDrive].strFilenameInZip.c_str(); return g_aFloppyDrive[iDrive].disk.strFilenameInZip.c_str();
return DiskGetFullName(iDrive); return DiskGetFullName(iDrive);
} }
static LPCTSTR DiskGetFullPathName(const int iDrive) static LPCTSTR DiskGetFullPathName(const int iDrive)
{ {
return ImageGetPathname(g_aFloppyDisk[iDrive].imagehandle); return ImageGetPathname(g_aFloppyDrive[iDrive].disk.imagehandle);
} }
// Return the imagename // Return the imagename
// . Used by Drive Button's icons & Property Sheet Page (Save snapshot) // . Used by Drive Button's icons & Property Sheet Page (Save snapshot)
LPCTSTR DiskGetBaseName(const int iDrive) LPCTSTR DiskGetBaseName(const int iDrive)
{ {
return g_aFloppyDisk[iDrive].imagename; return g_aFloppyDrive[iDrive].disk.imagename;
} }
//=========================================================================== //===========================================================================
void DiskGetLightStatus(Disk_Status_e *pDisk1Status_, Disk_Status_e *pDisk2Status_) void DiskGetLightStatus(Disk_Status_e *pDisk1Status, Disk_Status_e *pDisk2Status)
{ {
if (pDisk1Status_) if (pDisk1Status)
*pDisk1Status_ = GetDriveLightStatus( 0 ); *pDisk1Status = GetDriveLightStatus(DRIVE_1);
if (pDisk2Status_) if (pDisk2Status)
*pDisk2Status_ = GetDriveLightStatus( 1 ); *pDisk2Status = GetDriveLightStatus(DRIVE_2);
} }
//=========================================================================== //===========================================================================
@ -541,32 +569,34 @@ void DiskInitialize(void)
{ {
int loop = NUM_DRIVES; int loop = NUM_DRIVES;
while (loop--) while (loop--)
g_aFloppyDisk[loop].clear(); g_aFloppyDrive[loop].clear();
} }
//=========================================================================== //===========================================================================
ImageError_e DiskInsert(const int iDrive, LPCTSTR pszImageFilename, const bool bForceWriteProtected, const bool bCreateIfNecessary) ImageError_e DiskInsert(const int iDrive, LPCTSTR pszImageFilename, const bool bForceWriteProtected, const bool bCreateIfNecessary)
{ {
Disk_t * fptr = &g_aFloppyDisk[iDrive]; Drive_t* pDrive = &g_aFloppyDrive[iDrive];
if (fptr->imagehandle) Disk_t* pFloppy = &pDrive->disk;
if (pFloppy->imagehandle)
RemoveDisk(iDrive); RemoveDisk(iDrive);
// Reset the drive's struct, but preserve the physical attributes (bug#18242: Platoon) // Reset the drive's struct, but preserve the physical attributes (bug#18242: Platoon)
// . Changing the disk (in the drive) doesn't affect the drive's head etc. // . Changing the disk (in the drive) doesn't affect the drive's head etc.
{ {
int track = fptr->track; int track = pDrive->track;
int phase = fptr->phase; int phase = pDrive->phase;
fptr->clear(); pDrive->clear();
fptr->track = track; pDrive->track = track;
fptr->phase = phase; pDrive->phase = phase;
} }
const DWORD dwAttributes = GetFileAttributes(pszImageFilename); const DWORD dwAttributes = GetFileAttributes(pszImageFilename);
if(dwAttributes == INVALID_FILE_ATTRIBUTES) if(dwAttributes == INVALID_FILE_ATTRIBUTES)
fptr->bWriteProtected = false; // Assume this is a new file to create pFloppy->bWriteProtected = false; // Assume this is a new file to create
else else
fptr->bWriteProtected = bForceWriteProtected ? true : (dwAttributes & FILE_ATTRIBUTE_READONLY); pFloppy->bWriteProtected = bForceWriteProtected ? true : (dwAttributes & FILE_ATTRIBUTE_READONLY);
// Check if image is being used by the other drive, and if so remove it in order so it can be swapped // Check if image is being used by the other drive, and if so remove it in order so it can be swapped
{ {
@ -585,16 +615,16 @@ ImageError_e DiskInsert(const int iDrive, LPCTSTR pszImageFilename, const bool b
} }
ImageError_e Error = ImageOpen(pszImageFilename, ImageError_e Error = ImageOpen(pszImageFilename,
&fptr->imagehandle, &pFloppy->imagehandle,
&fptr->bWriteProtected, &pFloppy->bWriteProtected,
bCreateIfNecessary, bCreateIfNecessary,
fptr->strFilenameInZip); pFloppy->strFilenameInZip);
if (Error == eIMAGE_ERROR_NONE && ImageIsMultiFileZip(fptr->imagehandle)) if (Error == eIMAGE_ERROR_NONE && ImageIsMultiFileZip(pFloppy->imagehandle))
{ {
TCHAR szText[100+MAX_PATH]; TCHAR szText[100+MAX_PATH];
szText[sizeof(szText)-1] = 0; szText[sizeof(szText)-1] = 0;
_snprintf(szText, sizeof(szText)-1, "Only the first file in a multi-file zip is supported\nUse disk image '%s' ?", fptr->strFilenameInZip.c_str()); _snprintf(szText, sizeof(szText)-1, "Only the first file in a multi-file zip is supported\nUse disk image '%s' ?", pFloppy->strFilenameInZip.c_str());
int nRes = MessageBox(g_hFrameWindow, szText, TEXT("Multi-Zip Warning"), MB_ICONWARNING | MB_YESNO | MB_SETFOREGROUND); int nRes = MessageBox(g_hFrameWindow, szText, TEXT("Multi-Zip Warning"), MB_ICONWARNING | MB_YESNO | MB_SETFOREGROUND);
if (nRes == IDNO) if (nRes == IDNO)
{ {
@ -605,8 +635,8 @@ ImageError_e DiskInsert(const int iDrive, LPCTSTR pszImageFilename, const bool b
if (Error == eIMAGE_ERROR_NONE) if (Error == eIMAGE_ERROR_NONE)
{ {
GetImageTitle(pszImageFilename, fptr->imagename, fptr->fullname); GetImageTitle(pszImageFilename, pFloppy->imagename, pFloppy->fullname);
Video_ResetScreenshotCounter(fptr->imagename); Video_ResetScreenshotCounter(pFloppy->imagename);
} }
else else
{ {
@ -689,7 +719,7 @@ void DiskNotifyInvalidImage(const int iDrive, LPCTSTR pszImageFilename, const Im
TEXT("first file (%s) in this multi-zip archive is not recognized.\n") TEXT("first file (%s) in this multi-zip archive is not recognized.\n")
TEXT("Try unzipping and using the disk images directly.\n"), TEXT("Try unzipping and using the disk images directly.\n"),
pszImageFilename, pszImageFilename,
g_aFloppyDisk[iDrive].strFilenameInZip.c_str()); g_aFloppyDrive[iDrive].disk.strFilenameInZip.c_str());
break; break;
case eIMAGE_ERROR_GZ: case eIMAGE_ERROR_GZ:
@ -745,10 +775,10 @@ bool DiskGetProtect(const int iDrive)
{ {
if (IsDriveValid(iDrive)) if (IsDriveValid(iDrive))
{ {
Disk_t *pFloppy = &g_aFloppyDisk[ iDrive ]; if (g_aFloppyDrive[iDrive].disk.bWriteProtected)
if (pFloppy->bWriteProtected)
return true; return true;
} }
return false; return false;
} }
@ -759,8 +789,7 @@ void DiskSetProtect(const int iDrive, const bool bWriteProtect)
{ {
if (IsDriveValid( iDrive )) if (IsDriveValid( iDrive ))
{ {
Disk_t *pFloppy = &g_aFloppyDisk[ iDrive ]; g_aFloppyDrive[iDrive].disk.bWriteProtected = bWriteProtect;
pFloppy->bWriteProtected = bWriteProtect;
} }
} }
@ -772,8 +801,7 @@ bool Disk_ImageIsWriteProtected(const int iDrive)
if (!IsDriveValid(iDrive)) if (!IsDriveValid(iDrive))
return true; return true;
Disk_t *pFloppy = &g_aFloppyDisk[iDrive]; return ImageIsWriteProtected(g_aFloppyDrive[iDrive].disk.imagehandle);
return ImageIsWriteProtected(pFloppy->imagehandle);
} }
//=========================================================================== //===========================================================================
@ -783,8 +811,7 @@ bool Disk_IsDriveEmpty(const int iDrive)
if (!IsDriveValid(iDrive)) if (!IsDriveValid(iDrive))
return true; return true;
Disk_t *pFloppy = &g_aFloppyDisk[iDrive]; return g_aFloppyDrive[iDrive].disk.imagehandle == NULL;
return pFloppy->imagehandle == NULL;
} }
//=========================================================================== //===========================================================================
@ -826,12 +853,13 @@ static bool LogWriteCheckSyncFF(ULONG& uCycleDelta)
static void __stdcall DiskReadWrite(WORD pc, WORD addr, BYTE bWrite, BYTE d, ULONG nCyclesLeft) static void __stdcall DiskReadWrite(WORD pc, WORD addr, BYTE bWrite, BYTE d, ULONG nCyclesLeft)
{ {
/* floppyloadmode = 0; */ /* floppyloadmode = 0; */
Disk_t * fptr = &g_aFloppyDisk[currdrive]; Drive_t* pDrive = &g_aFloppyDrive[currdrive];
Disk_t* pFloppy = &pDrive->disk;
if (!fptr->trackimagedata && fptr->imagehandle) if (!pFloppy->trackimagedata && pFloppy->imagehandle)
ReadTrack(currdrive); ReadTrack(currdrive);
if (!fptr->trackimagedata) if (!pFloppy->trackimagedata)
{ {
floppylatch = 0xFF; floppylatch = 0xFF;
return; return;
@ -841,7 +869,7 @@ static void __stdcall DiskReadWrite(WORD pc, WORD addr, BYTE bWrite, BYTE d, ULO
UINT uSpinNibbleCount = 0; UINT uSpinNibbleCount = 0;
CpuCalcCycles(nCyclesLeft); // g_nCumulativeCycles required for uSpinNibbleCount & LogWriteCheckSyncFF() CpuCalcCycles(nCyclesLeft); // g_nCumulativeCycles required for uSpinNibbleCount & LogWriteCheckSyncFF()
if (!enhancedisk && fptr->spinning) if (!enhancedisk && pDrive->spinning)
{ {
const ULONG nCycleDiff = (ULONG) (g_nCumulativeCycles - g_uDiskLastCycle); const ULONG nCycleDiff = (ULONG) (g_nCumulativeCycles - g_uDiskLastCycle);
g_uDiskLastCycle = g_nCumulativeCycles; g_uDiskLastCycle = g_nCumulativeCycles;
@ -851,13 +879,13 @@ static void __stdcall DiskReadWrite(WORD pc, WORD addr, BYTE bWrite, BYTE d, ULO
// 40 cycles for a write of a 10-bit 0xFF sync byte // 40 cycles for a write of a 10-bit 0xFF sync byte
uSpinNibbleCount = nCycleDiff >> 5; // ...but divide by 32 (not 40) uSpinNibbleCount = nCycleDiff >> 5; // ...but divide by 32 (not 40)
ULONG uWrapOffset = uSpinNibbleCount % fptr->nibbles; ULONG uWrapOffset = uSpinNibbleCount % pFloppy->nibbles;
fptr->byte += uWrapOffset; pFloppy->byte += uWrapOffset;
if (fptr->byte >= fptr->nibbles) if (pFloppy->byte >= pFloppy->nibbles)
fptr->byte -= fptr->nibbles; pFloppy->byte -= pFloppy->nibbles;
#if LOG_DISK_NIBBLES_SPIN #if LOG_DISK_NIBBLES_SPIN
UINT uCompleteRevolutions = uSpinNibbleCount / fptr->nibbles; UINT uCompleteRevolutions = uSpinNibbleCount / pFloppy->nibbles;
LOG_DISK("spin: revs=%d, nibbles=%d\r\n", uCompleteRevolutions, uWrapOffset); LOG_DISK("spin: revs=%d, nibbles=%d\r\n", uCompleteRevolutions, uWrapOffset);
#endif #endif
} }
@ -867,23 +895,23 @@ static void __stdcall DiskReadWrite(WORD pc, WORD addr, BYTE bWrite, BYTE d, ULO
// but Sherwood Forest sets shift mode and reads with the drive off, so don't check for now // but Sherwood Forest sets shift mode and reads with the drive off, so don't check for now
if (!floppywritemode) if (!floppywritemode)
{ {
floppylatch = *(fptr->trackimage + fptr->byte); floppylatch = *(pFloppy->trackimage + pFloppy->byte);
#if LOG_DISK_NIBBLES_READ #if LOG_DISK_NIBBLES_READ
#if LOG_DISK_NIBBLES_USE_RUNTIME_VAR #if LOG_DISK_NIBBLES_USE_RUNTIME_VAR
if (g_bLogDisk_NibblesRW) if (g_bLogDisk_NibblesRW)
#endif #endif
{ {
LOG_DISK("read %04X = %02X\r\n", fptr->byte, floppylatch); LOG_DISK("read %04X = %02X\r\n", pFloppy->byte, floppylatch);
} }
g_formatTrack.DecodeLatchNibbleRead(floppylatch); g_formatTrack.DecodeLatchNibbleRead(floppylatch);
#endif #endif
} }
else if (!fptr->bWriteProtected) // && floppywritemode else if (!pFloppy->bWriteProtected) // && floppywritemode
{ {
*(fptr->trackimage + fptr->byte) = floppylatch; *(pFloppy->trackimage + pFloppy->byte) = floppylatch;
fptr->trackimagedirty = 1; pFloppy->trackimagedirty = 1;
bool bIsSyncFF = false; bool bIsSyncFF = false;
#if LOG_DISK_NIBBLES_WRITE #if LOG_DISK_NIBBLES_WRITE
@ -891,7 +919,7 @@ static void __stdcall DiskReadWrite(WORD pc, WORD addr, BYTE bWrite, BYTE d, ULO
bIsSyncFF = LogWriteCheckSyncFF(uCycleDelta); bIsSyncFF = LogWriteCheckSyncFF(uCycleDelta);
#endif #endif
g_formatTrack.DecodeLatchNibbleWrite(floppylatch, uSpinNibbleCount, fptr, bIsSyncFF); // GH#125 g_formatTrack.DecodeLatchNibbleWrite(floppylatch, uSpinNibbleCount, pFloppy, bIsSyncFF); // GH#125
#if LOG_DISK_NIBBLES_WRITE #if LOG_DISK_NIBBLES_WRITE
#if LOG_DISK_NIBBLES_USE_RUNTIME_VAR #if LOG_DISK_NIBBLES_USE_RUNTIME_VAR
@ -899,20 +927,20 @@ static void __stdcall DiskReadWrite(WORD pc, WORD addr, BYTE bWrite, BYTE d, ULO
#endif #endif
{ {
if (!bIsSyncFF) if (!bIsSyncFF)
LOG_DISK("write %04X = %02X (cy=+%d)\r\n", fptr->byte, floppylatch, uCycleDelta); LOG_DISK("write %04X = %02X (cy=+%d)\r\n", pFloppy->byte, floppylatch, uCycleDelta);
else else
LOG_DISK("write %04X = %02X (cy=+%d) sync #%d\r\n", fptr->byte, floppylatch, uCycleDelta, g_uSyncFFCount); LOG_DISK("write %04X = %02X (cy=+%d) sync #%d\r\n", pFloppy->byte, floppylatch, uCycleDelta, g_uSyncFFCount);
} }
#endif #endif
} }
if (++fptr->byte >= fptr->nibbles) if (++pFloppy->byte >= pFloppy->nibbles)
fptr->byte = 0; pFloppy->byte = 0;
// Feature Request #201 Show track status // Feature Request #201 Show track status
// https://github.com/AppleWin/AppleWin/issues/201 // https://github.com/AppleWin/AppleWin/issues/201
// NB. Prevent flooding of forcing UI to redraw!!! // NB. Prevent flooding of forcing UI to redraw!!!
if( ((fptr->byte) & 0xFF) == 0 ) if( ((pFloppy->byte) & 0xFF) == 0 )
FrameDrawDiskStatus( (HDC)0 ); FrameDrawDiskStatus( (HDC)0 );
} }
@ -931,10 +959,10 @@ void DiskReset(const bool bIsPowerCycle/*=false*/)
if (bIsPowerCycle) // GH#460 if (bIsPowerCycle) // GH#460
{ {
g_aFloppyDisk[DRIVE_1].spinning = 0; g_aFloppyDrive[DRIVE_1].spinning = 0;
g_aFloppyDisk[DRIVE_1].writelight = 0; g_aFloppyDrive[DRIVE_1].writelight = 0;
g_aFloppyDisk[DRIVE_2].spinning = 0; g_aFloppyDrive[DRIVE_2].spinning = 0;
g_aFloppyDisk[DRIVE_2].writelight = 0; g_aFloppyDrive[DRIVE_2].writelight = 0;
FrameRefreshStatus(DRAW_LEDS, false); FrameRefreshStatus(DRAW_LEDS, false);
} }
@ -1012,7 +1040,7 @@ static void __stdcall DiskLoadWriteProtect(WORD, WORD, BYTE write, BYTE value, U
// . write mode doesn't prevent reading write protect (GH#537): // . write mode doesn't prevent reading write protect (GH#537):
// "If for some reason the above write protect check were entered with the READ/WRITE switch in WRITE, // "If for some reason the above write protect check were entered with the READ/WRITE switch in WRITE,
// the write protect switch would still be read correctly" (UTAIIe page 9-21) // the write protect switch would still be read correctly" (UTAIIe page 9-21)
if (g_aFloppyDisk[currdrive].bWriteProtected) if (g_aFloppyDrive[currdrive].disk.bWriteProtected)
floppylatch |= 0x80; floppylatch |= 0x80;
else else
floppylatch &= 0x7F; floppylatch &= 0x7F;
@ -1025,7 +1053,7 @@ static void __stdcall DiskSetReadMode(WORD, WORD, BYTE, BYTE, ULONG)
{ {
floppywritemode = 0; floppywritemode = 0;
g_formatTrack.DriveSwitchedToReadMode(&g_aFloppyDisk[currdrive]); g_formatTrack.DriveSwitchedToReadMode(&g_aFloppyDrive[currdrive].disk);
#if LOG_DISK_RW_MODE #if LOG_DISK_RW_MODE
LOG_DISK("rw mode: read\r\n"); LOG_DISK("rw mode: read\r\n");
@ -1038,9 +1066,9 @@ static void __stdcall DiskSetWriteMode(WORD, WORD, BYTE, BYTE, ULONG uExecutedCy
{ {
floppywritemode = 1; floppywritemode = 1;
g_formatTrack.DriveSwitchedToWriteMode(g_aFloppyDisk[currdrive].byte); g_formatTrack.DriveSwitchedToWriteMode(g_aFloppyDrive[currdrive].disk.byte);
BOOL modechange = !g_aFloppyDisk[currdrive].writelight; BOOL modechange = !g_aFloppyDrive[currdrive].writelight;
#if LOG_DISK_RW_MODE #if LOG_DISK_RW_MODE
LOG_DISK("rw mode: write (mode changed=%d)\r\n", modechange ? 1 : 0); LOG_DISK("rw mode: write (mode changed=%d)\r\n", modechange ? 1 : 0);
#endif #endif
@ -1048,7 +1076,7 @@ static void __stdcall DiskSetWriteMode(WORD, WORD, BYTE, BYTE, ULONG uExecutedCy
g_uWriteLastCycle = 0; g_uWriteLastCycle = 0;
#endif #endif
g_aFloppyDisk[currdrive].writelight = WRITELIGHT_CYCLES; g_aFloppyDrive[currdrive].writelight = WRITELIGHT_CYCLES;
if (modechange) if (modechange)
FrameDrawDiskLEDS( (HDC)0 ); FrameDrawDiskLEDS( (HDC)0 );
@ -1061,24 +1089,24 @@ void DiskUpdateDriveState(DWORD cycles)
int loop = NUM_DRIVES; int loop = NUM_DRIVES;
while (loop--) while (loop--)
{ {
Disk_t * fptr = &g_aFloppyDisk[loop]; Drive_t* pDrive = &g_aFloppyDrive[loop];
if (fptr->spinning && !floppymotoron) if (pDrive->spinning && !floppymotoron)
{ {
if (!(fptr->spinning -= MIN(fptr->spinning, cycles))) if (!(pDrive->spinning -= MIN(pDrive->spinning, cycles)))
{ {
FrameDrawDiskLEDS( (HDC)0 ); FrameDrawDiskLEDS( (HDC)0 );
FrameDrawDiskStatus( (HDC)0 ); FrameDrawDiskStatus( (HDC)0 );
} }
} }
if (floppywritemode && (currdrive == loop) && fptr->spinning) if (floppywritemode && (currdrive == loop) && pDrive->spinning)
{ {
fptr->writelight = WRITELIGHT_CYCLES; pDrive->writelight = WRITELIGHT_CYCLES;
} }
else if (fptr->writelight) else if (pDrive->writelight)
{ {
if (!(fptr->writelight -= MIN(fptr->writelight, cycles))) if (!(pDrive->writelight -= MIN(pDrive->writelight, cycles)))
{ {
FrameDrawDiskLEDS( (HDC)0 ); FrameDrawDiskLEDS( (HDC)0 );
FrameDrawDiskStatus( (HDC)0 ); FrameDrawDiskStatus( (HDC)0 );
@ -1093,7 +1121,7 @@ bool DiskDriveSwap(void)
{ {
// Refuse to swap if either Disk][ is active // Refuse to swap if either Disk][ is active
// TODO: if Shift-Click then FORCE drive swap to bypass message // TODO: if Shift-Click then FORCE drive swap to bypass message
if(g_aFloppyDisk[0].spinning || g_aFloppyDisk[1].spinning) if (g_aFloppyDrive[0].spinning || g_aFloppyDrive[1].spinning)
{ {
// 1.26.2.4 Prompt when trying to swap disks while drive is on instead of silently failing // 1.26.2.4 Prompt when trying to swap disks while drive is on instead of silently failing
int status = MessageBox( int status = MessageBox(
@ -1126,7 +1154,7 @@ bool DiskDriveSwap(void)
// Swap disks between drives // Swap disks between drives
// . NB. We swap trackimage ptrs (so don't need to swap the buffers' data) // . NB. We swap trackimage ptrs (so don't need to swap the buffers' data)
// . TODO: Consider array of Pointers: Disk_t* g_aDrive[] // . TODO: Consider array of Pointers: Disk_t* g_aDrive[]
std::swap(g_aFloppyDisk[0], g_aFloppyDisk[1]); std::swap(g_aFloppyDrive[0], g_aFloppyDrive[1]);
Disk_SaveLastDiskImage(DRIVE_1); Disk_SaveLastDiskImage(DRIVE_1);
Disk_SaveLastDiskImage(DRIVE_2); Disk_SaveLastDiskImage(DRIVE_2);
@ -1256,7 +1284,7 @@ int DiskSetSnapshot_v1(const SS_CARD_DISK2* const pSS)
for(UINT i=0; i<NUM_DRIVES; i++) for(UINT i=0; i<NUM_DRIVES; i++)
{ {
DiskEject(i); // Remove any disk & update Registry to reflect empty drive DiskEject(i); // Remove any disk & update Registry to reflect empty drive
g_aFloppyDisk[i].clear(); g_aFloppyDrive[i].clear();
} }
for(UINT i=0; i<NUM_DRIVES; i++) for(UINT i=0; i<NUM_DRIVES; i++)
@ -1287,35 +1315,36 @@ int DiskSetSnapshot_v1(const SS_CARD_DISK2* const pSS)
// //
// strcpy(g_aFloppyDisk[i].fullname, pSS->Unit[i].szFileName); // strcpy(g_aFloppyDrive[i].fullname, pSS->Unit[i].szFileName);
g_aFloppyDisk[i].track = pSS->Unit[i].track; g_aFloppyDrive[i].track = pSS->Unit[i].track;
g_aFloppyDisk[i].phase = pSS->Unit[i].phase; g_aFloppyDrive[i].phase = pSS->Unit[i].phase;
g_aFloppyDisk[i].byte = pSS->Unit[i].byte; g_aFloppyDrive[i].spinning = pSS->Unit[i].spinning;
// g_aFloppyDisk[i].writeprotected = pSS->Unit[i].writeprotected; g_aFloppyDrive[i].writelight = pSS->Unit[i].writelight;
g_aFloppyDisk[i].trackimagedata = pSS->Unit[i].trackimagedata;
g_aFloppyDisk[i].trackimagedirty = pSS->Unit[i].trackimagedirty; g_aFloppyDrive[i].disk.byte = pSS->Unit[i].byte;
g_aFloppyDisk[i].spinning = pSS->Unit[i].spinning; // g_aFloppyDrive[i].disk.writeprotected = pSS->Unit[i].writeprotected;
g_aFloppyDisk[i].writelight = pSS->Unit[i].writelight; g_aFloppyDrive[i].disk.trackimagedata = pSS->Unit[i].trackimagedata ? true : false;
g_aFloppyDisk[i].nibbles = pSS->Unit[i].nibbles; g_aFloppyDrive[i].disk.trackimagedirty = pSS->Unit[i].trackimagedirty ? true : false;
g_aFloppyDrive[i].disk.nibbles = pSS->Unit[i].nibbles;
// //
if(!bImageError) if(!bImageError)
{ {
if((g_aFloppyDisk[i].trackimage == NULL) && g_aFloppyDisk[i].nibbles) if((g_aFloppyDrive[i].disk.trackimage == NULL) && g_aFloppyDrive[i].disk.nibbles)
AllocTrack(i); AllocTrack(i);
if(g_aFloppyDisk[i].trackimage == NULL) if(g_aFloppyDrive[i].disk.trackimage == NULL)
bImageError = true; bImageError = true;
else else
memcpy(g_aFloppyDisk[i].trackimage, pSS->Unit[i].nTrack, NIBBLES_PER_TRACK); memcpy(g_aFloppyDrive[i].disk.trackimage, pSS->Unit[i].nTrack, NIBBLES_PER_TRACK);
} }
if(bImageError) if(bImageError)
{ {
g_aFloppyDisk[i].trackimagedata = 0; g_aFloppyDrive[i].disk.trackimagedata = 0;
g_aFloppyDisk[i].trackimagedirty = 0; g_aFloppyDrive[i].disk.trackimagedirty = 0;
g_aFloppyDisk[i].nibbles = 0; g_aFloppyDrive[i].disk.nibbles = 0;
} }
} }
@ -1363,21 +1392,21 @@ std::string DiskGetSnapshotCardName(void)
static void DiskSaveSnapshotDisk2Unit(YamlSaveHelper& yamlSaveHelper, UINT unit) static void DiskSaveSnapshotDisk2Unit(YamlSaveHelper& yamlSaveHelper, UINT unit)
{ {
YamlSaveHelper::Label label(yamlSaveHelper, "%s%d:\n", SS_YAML_KEY_DISK2UNIT, unit); YamlSaveHelper::Label label(yamlSaveHelper, "%s%d:\n", SS_YAML_KEY_DISK2UNIT, unit);
yamlSaveHelper.SaveString(SS_YAML_KEY_FILENAME, g_aFloppyDisk[unit].fullname); yamlSaveHelper.SaveString(SS_YAML_KEY_FILENAME, g_aFloppyDrive[unit].disk.fullname);
yamlSaveHelper.SaveUint(SS_YAML_KEY_TRACK, g_aFloppyDisk[unit].track); yamlSaveHelper.SaveUint(SS_YAML_KEY_TRACK, g_aFloppyDrive[unit].track);
yamlSaveHelper.SaveUint(SS_YAML_KEY_PHASE, g_aFloppyDisk[unit].phase); yamlSaveHelper.SaveUint(SS_YAML_KEY_PHASE, g_aFloppyDrive[unit].phase);
yamlSaveHelper.SaveHexUint16(SS_YAML_KEY_BYTE, g_aFloppyDisk[unit].byte); yamlSaveHelper.SaveHexUint16(SS_YAML_KEY_BYTE, g_aFloppyDrive[unit].disk.byte);
yamlSaveHelper.SaveBool(SS_YAML_KEY_WRITE_PROTECTED, g_aFloppyDisk[unit].bWriteProtected); yamlSaveHelper.SaveBool(SS_YAML_KEY_WRITE_PROTECTED, g_aFloppyDrive[unit].disk.bWriteProtected);
yamlSaveHelper.SaveUint(SS_YAML_KEY_SPINNING, g_aFloppyDisk[unit].spinning); yamlSaveHelper.SaveUint(SS_YAML_KEY_SPINNING, g_aFloppyDrive[unit].spinning);
yamlSaveHelper.SaveUint(SS_YAML_KEY_WRITE_LIGHT, g_aFloppyDisk[unit].writelight); yamlSaveHelper.SaveUint(SS_YAML_KEY_WRITE_LIGHT, g_aFloppyDrive[unit].writelight);
yamlSaveHelper.SaveHexUint16(SS_YAML_KEY_NIBBLES, g_aFloppyDisk[unit].nibbles); yamlSaveHelper.SaveHexUint16(SS_YAML_KEY_NIBBLES, g_aFloppyDrive[unit].disk.nibbles);
yamlSaveHelper.SaveUint(SS_YAML_KEY_TRACK_IMAGE_DATA, g_aFloppyDisk[unit].trackimagedata); yamlSaveHelper.SaveUint(SS_YAML_KEY_TRACK_IMAGE_DATA, g_aFloppyDrive[unit].disk.trackimagedata);
yamlSaveHelper.SaveUint(SS_YAML_KEY_TRACK_IMAGE_DIRTY, g_aFloppyDisk[unit].trackimagedirty); yamlSaveHelper.SaveUint(SS_YAML_KEY_TRACK_IMAGE_DIRTY, g_aFloppyDrive[unit].disk.trackimagedirty);
if (g_aFloppyDisk[unit].trackimage) if (g_aFloppyDrive[unit].disk.trackimage)
{ {
YamlSaveHelper::Label image(yamlSaveHelper, "%s:\n", SS_YAML_KEY_TRACK_IMAGE); YamlSaveHelper::Label image(yamlSaveHelper, "%s:\n", SS_YAML_KEY_TRACK_IMAGE);
yamlSaveHelper.SaveMemory(g_aFloppyDisk[unit].trackimage, NIBBLES_PER_TRACK); yamlSaveHelper.SaveMemory(g_aFloppyDrive[unit].disk.trackimage, NIBBLES_PER_TRACK);
} }
} }
@ -1408,9 +1437,9 @@ static void DiskLoadSnapshotDriveUnit(YamlLoadHelper& yamlLoadHelper, UINT unit)
bool bImageError = false; bool bImageError = false;
g_aFloppyDisk[unit].fullname[0] = 0; g_aFloppyDrive[unit].disk.fullname[0] = 0;
g_aFloppyDisk[unit].imagename[0] = 0; g_aFloppyDrive[unit].disk.imagename[0] = 0;
g_aFloppyDisk[unit].bWriteProtected = false; // Default to false (until image is successfully loaded below) g_aFloppyDrive[unit].disk.bWriteProtected = false; // Default to false (until image is successfully loaded below)
std::string filename = yamlLoadHelper.LoadString(SS_YAML_KEY_FILENAME); std::string filename = yamlLoadHelper.LoadString(SS_YAML_KEY_FILENAME);
if (!filename.empty()) if (!filename.empty())
@ -1430,22 +1459,22 @@ static void DiskLoadSnapshotDriveUnit(YamlLoadHelper& yamlLoadHelper, UINT unit)
if(DiskInsert(unit, filename.c_str(), dwAttributes & FILE_ATTRIBUTE_READONLY, IMAGE_DONT_CREATE) != eIMAGE_ERROR_NONE) if(DiskInsert(unit, filename.c_str(), dwAttributes & FILE_ATTRIBUTE_READONLY, IMAGE_DONT_CREATE) != eIMAGE_ERROR_NONE)
bImageError = true; bImageError = true;
// DiskInsert() zeros g_aFloppyDisk[unit], then sets up: // DiskInsert() zeros g_aFloppyDrive[unit], then sets up:
// . imagename // . imagename
// . fullname // . fullname
// . writeprotected // . writeprotected
} }
} }
g_aFloppyDisk[unit].track = yamlLoadHelper.LoadUint(SS_YAML_KEY_TRACK); g_aFloppyDrive[unit].track = yamlLoadHelper.LoadUint(SS_YAML_KEY_TRACK);
g_aFloppyDisk[unit].phase = yamlLoadHelper.LoadUint(SS_YAML_KEY_PHASE); g_aFloppyDrive[unit].phase = yamlLoadHelper.LoadUint(SS_YAML_KEY_PHASE);
g_aFloppyDisk[unit].byte = yamlLoadHelper.LoadUint(SS_YAML_KEY_BYTE); g_aFloppyDrive[unit].disk.byte = yamlLoadHelper.LoadUint(SS_YAML_KEY_BYTE);
yamlLoadHelper.LoadBool(SS_YAML_KEY_WRITE_PROTECTED); // Consume yamlLoadHelper.LoadBool(SS_YAML_KEY_WRITE_PROTECTED); // Consume
g_aFloppyDisk[unit].spinning = yamlLoadHelper.LoadUint(SS_YAML_KEY_SPINNING); g_aFloppyDrive[unit].spinning = yamlLoadHelper.LoadUint(SS_YAML_KEY_SPINNING);
g_aFloppyDisk[unit].writelight = yamlLoadHelper.LoadUint(SS_YAML_KEY_WRITE_LIGHT); g_aFloppyDrive[unit].writelight = yamlLoadHelper.LoadUint(SS_YAML_KEY_WRITE_LIGHT);
g_aFloppyDisk[unit].nibbles = yamlLoadHelper.LoadUint(SS_YAML_KEY_NIBBLES); g_aFloppyDrive[unit].disk.nibbles = yamlLoadHelper.LoadUint(SS_YAML_KEY_NIBBLES);
g_aFloppyDisk[unit].trackimagedata = yamlLoadHelper.LoadUint(SS_YAML_KEY_TRACK_IMAGE_DATA); g_aFloppyDrive[unit].disk.trackimagedata = yamlLoadHelper.LoadUint(SS_YAML_KEY_TRACK_IMAGE_DATA) ? true : false;
g_aFloppyDisk[unit].trackimagedirty = yamlLoadHelper.LoadUint(SS_YAML_KEY_TRACK_IMAGE_DIRTY); g_aFloppyDrive[unit].disk.trackimagedirty = yamlLoadHelper.LoadUint(SS_YAML_KEY_TRACK_IMAGE_DIRTY) ? true : false;
std::vector<BYTE> track(NIBBLES_PER_TRACK); std::vector<BYTE> track(NIBBLES_PER_TRACK);
if (yamlLoadHelper.GetSubMap(SS_YAML_KEY_TRACK_IMAGE)) if (yamlLoadHelper.GetSubMap(SS_YAML_KEY_TRACK_IMAGE))
@ -1460,20 +1489,20 @@ static void DiskLoadSnapshotDriveUnit(YamlLoadHelper& yamlLoadHelper, UINT unit)
if (!filename.empty() && !bImageError) if (!filename.empty() && !bImageError)
{ {
if ((g_aFloppyDisk[unit].trackimage == NULL) && g_aFloppyDisk[unit].nibbles) if ((g_aFloppyDrive[unit].disk.trackimage == NULL) && g_aFloppyDrive[unit].disk.nibbles)
AllocTrack(unit); AllocTrack(unit);
if (g_aFloppyDisk[unit].trackimage == NULL) if (g_aFloppyDrive[unit].disk.trackimage == NULL)
bImageError = true; bImageError = true;
else else
memcpy(g_aFloppyDisk[unit].trackimage, &track[0], NIBBLES_PER_TRACK); memcpy(g_aFloppyDrive[unit].disk.trackimage, &track[0], NIBBLES_PER_TRACK);
} }
if (bImageError) if (bImageError)
{ {
g_aFloppyDisk[unit].trackimagedata = 0; g_aFloppyDrive[unit].disk.trackimagedata = 0;
g_aFloppyDisk[unit].trackimagedirty = 0; g_aFloppyDrive[unit].disk.trackimagedirty = 0;
g_aFloppyDisk[unit].nibbles = 0; g_aFloppyDrive[unit].disk.nibbles = 0;
} }
} }
@ -1503,7 +1532,7 @@ bool DiskLoadSnapshot(class YamlLoadHelper& yamlLoadHelper, UINT slot, UINT vers
for(UINT i=0; i<NUM_DRIVES; i++) for(UINT i=0; i<NUM_DRIVES; i++)
{ {
DiskEject(i); // Remove any disk & update Registry to reflect empty drive DiskEject(i); // Remove any disk & update Registry to reflect empty drive
g_aFloppyDisk[i].clear(); g_aFloppyDrive[i].clear();
} }
DiskLoadSnapshotDriveUnit(yamlLoadHelper, DRIVE_1); DiskLoadSnapshotDriveUnit(yamlLoadHelper, DRIVE_1);

View File

@ -53,7 +53,7 @@ LPCTSTR DiskGetFullName(const int iDrive);
LPCTSTR DiskGetFullDiskFilename(const int iDrive); LPCTSTR DiskGetFullDiskFilename(const int iDrive);
LPCTSTR DiskGetBaseName(const int iDrive); LPCTSTR DiskGetBaseName(const int iDrive);
void DiskGetLightStatus (Disk_Status_e *pDisk1Status_, Disk_Status_e *pDisk2Status_); void DiskGetLightStatus (Disk_Status_e* pDisk1Status, Disk_Status_e* pDisk2Status);
ImageError_e DiskInsert(const int iDrive, LPCTSTR pszImageFilename, const bool bForceWriteProtected, const bool bCreateIfNecessary); ImageError_e DiskInsert(const int iDrive, LPCTSTR pszImageFilename, const bool bForceWriteProtected, const bool bCreateIfNecessary);
BOOL DiskIsSpinning(void); BOOL DiskIsSpinning(void);
@ -88,21 +88,17 @@ bool Disk_IsDriveEmpty(const int iDrive);
// For sharing with class FormatTrack // For sharing with class FormatTrack
struct Disk_t struct Disk_t
{ {
TCHAR imagename[ MAX_DISK_IMAGE_NAME + 1 ]; // <FILENAME> (ie. no extension) TCHAR imagename[ MAX_DISK_IMAGE_NAME + 1 ]; // <FILENAME> (ie. no extension)
TCHAR fullname [ MAX_DISK_FULL_NAME + 1 ]; // <FILENAME.EXT> or <FILENAME.zip> : This is persisted to the snapshot file TCHAR fullname [ MAX_DISK_FULL_NAME + 1 ]; // <FILENAME.EXT> or <FILENAME.zip> : This is persisted to the snapshot file
std::string strFilenameInZip; // "" or <FILENAME.EXT> std::string strFilenameInZip; // "" or <FILENAME.EXT>
ImageInfo* imagehandle; // Init'd by DiskInsert() -> ImageOpen() ImageInfo* imagehandle; // Init'd by DiskInsert() -> ImageOpen()
bool bWriteProtected; bool bWriteProtected;
// //
int track; int byte;
LPBYTE trackimage; int nibbles; // Init'd by ReadTrack() -> ImageReadTrack()
int phase; LPBYTE trackimage;
int byte; bool trackimagedata;
BOOL trackimagedata; bool trackimagedirty;
BOOL trackimagedirty;
DWORD spinning;
DWORD writelight;
int nibbles; // Init'd by ReadTrack() -> ImageReadTrack()
Disk_t() Disk_t()
{ {
@ -116,14 +112,11 @@ struct Disk_t
strFilenameInZip.clear(); strFilenameInZip.clear();
imagehandle = NULL; imagehandle = NULL;
bWriteProtected = false; bWriteProtected = false;
track = 0; //
trackimage = NULL;
phase = 0;
byte = 0; byte = 0;
trackimagedata = FALSE;
trackimagedirty = FALSE;
spinning = 0;
writelight = 0;
nibbles = 0; nibbles = 0;
trackimage = NULL;
trackimagedata = false;
trackimagedirty = false;
} }
}; };

View File

@ -84,15 +84,15 @@ void FormatTrack::DriveNotWritingTrack(void)
#endif #endif
} }
void FormatTrack::UpdateOnWriteLatch(UINT uSpinNibbleCount, const Disk_t* const fptr) void FormatTrack::UpdateOnWriteLatch(UINT uSpinNibbleCount, const Disk_t* const pFloppy)
{ {
if (fptr->bWriteProtected) if (pFloppy->bWriteProtected)
return; return;
if (m_bmWrittenSectorAddrFields == 0x0000) if (m_bmWrittenSectorAddrFields == 0x0000)
{ {
if (m_WriteTrackStartIndex == (UINT)-1) // waiting for 1st write? if (m_WriteTrackStartIndex == (UINT)-1) // waiting for 1st write?
m_WriteTrackStartIndex = fptr->byte; m_WriteTrackStartIndex = pFloppy->byte;
return; return;
} }
@ -108,8 +108,8 @@ void FormatTrack::UpdateOnWriteLatch(UINT uSpinNibbleCount, const Disk_t* const
return; return;
} }
UINT uTrackIndex = fptr->byte; UINT uTrackIndex = pFloppy->byte;
const UINT& kTrackMaxNibbles = fptr->nibbles; const UINT& kTrackMaxNibbles = pFloppy->nibbles;
// NB. spin in write mode is only max 1-2 bytes // NB. spin in write mode is only max 1-2 bytes
do do
@ -128,7 +128,7 @@ void FormatTrack::UpdateOnWriteLatch(UINT uSpinNibbleCount, const Disk_t* const
while (uSpinNibbleCount--); while (uSpinNibbleCount--);
} }
void FormatTrack::DriveSwitchedToReadMode(Disk_t* const fptr) void FormatTrack::DriveSwitchedToReadMode(Disk_t* const pFloppy)
{ {
if (m_bAddressPrologueIsDOS3_2) if (m_bAddressPrologueIsDOS3_2)
{ {
@ -154,10 +154,10 @@ void FormatTrack::DriveSwitchedToReadMode(Disk_t* const fptr)
// So need a track size between 0x18B0 (rounding down) and 0x182F // So need a track size between 0x18B0 (rounding down) and 0x182F
const UINT kShortTrackLen = 0x18B0; const UINT kShortTrackLen = 0x18B0;
LPBYTE TrackBuffer = fptr->trackimage; LPBYTE TrackBuffer = pFloppy->trackimage;
const UINT kLongTrackLen = fptr->nibbles; const UINT kLongTrackLen = pFloppy->nibbles;
UINT uWriteTrackEndIndex = fptr->byte; UINT uWriteTrackEndIndex = pFloppy->byte;
UINT uWrittenTrackSize = m_WriteTrackHasWrapped ? kLongTrackLen : 0; UINT uWrittenTrackSize = m_WriteTrackHasWrapped ? kLongTrackLen : 0;
if (m_WriteTrackStartIndex <= uWriteTrackEndIndex) if (m_WriteTrackStartIndex <= uWriteTrackEndIndex)
@ -216,10 +216,10 @@ void FormatTrack::DecodeLatchNibbleRead(BYTE floppylatch)
DecodeLatchNibble(floppylatch, false, false); DecodeLatchNibble(floppylatch, false, false);
} }
void FormatTrack::DecodeLatchNibbleWrite(BYTE floppylatch, UINT uSpinNibbleCount, const Disk_t* const fptr, bool bIsSyncFF) void FormatTrack::DecodeLatchNibbleWrite(BYTE floppylatch, UINT uSpinNibbleCount, const Disk_t* const pFloppy, bool bIsSyncFF)
{ {
DecodeLatchNibble(floppylatch, true, bIsSyncFF); DecodeLatchNibble(floppylatch, true, bIsSyncFF);
UpdateOnWriteLatch(uSpinNibbleCount, fptr); UpdateOnWriteLatch(uSpinNibbleCount, pFloppy);
} }
void FormatTrack::DecodeLatchNibble(BYTE floppylatch, bool bIsWrite, bool bIsSyncFF) void FormatTrack::DecodeLatchNibble(BYTE floppylatch, bool bIsWrite, bool bIsSyncFF)

View File

@ -35,15 +35,15 @@ public:
void Reset(void); void Reset(void);
void DriveNotWritingTrack(void); void DriveNotWritingTrack(void);
void DriveSwitchedToReadMode(Disk_t* const fptr); void DriveSwitchedToReadMode(Disk_t* const pFloppy);
void DriveSwitchedToWriteMode(UINT uTrackIndex); void DriveSwitchedToWriteMode(UINT uTrackIndex);
void DecodeLatchNibbleRead(BYTE floppylatch); void DecodeLatchNibbleRead(BYTE floppylatch);
void DecodeLatchNibbleWrite(BYTE floppylatch, UINT uSpinNibbleCount, const Disk_t* const fptr, bool bIsSyncFF); void DecodeLatchNibbleWrite(BYTE floppylatch, UINT uSpinNibbleCount, const Disk_t* const pFloppy, bool bIsSyncFF);
void SaveSnapshot(class YamlSaveHelper& yamlSaveHelper); void SaveSnapshot(class YamlSaveHelper& yamlSaveHelper);
void LoadSnapshot(class YamlLoadHelper& yamlLoadHelper); void LoadSnapshot(class YamlLoadHelper& yamlLoadHelper);
private: private:
void UpdateOnWriteLatch(UINT uSpinNibbleCount, const Disk_t* const fptr); void UpdateOnWriteLatch(UINT uSpinNibbleCount, const Disk_t* const pFloppy);
void DecodeLatchNibble(BYTE floppylatch, bool bIsWrite, bool bIsSyncFF); void DecodeLatchNibble(BYTE floppylatch, bool bIsWrite, bool bIsSyncFF);
BYTE m_VolTrkSecChk[4]; BYTE m_VolTrkSecChk[4];