2006-02-25 20:50:29 +00:00
|
|
|
#pragma once
|
|
|
|
|
2010-12-18 20:58: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
|
2019-04-16 19:30:54 +00:00
|
|
|
Copyright (C) 2006-2019, Tom Charlesworth, Michael Pohoreski, Nick Westgate
|
2010-12-18 20:58: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
|
|
|
|
*/
|
|
|
|
|
2019-12-19 19:42:30 +00:00
|
|
|
#include "Card.h"
|
2019-04-07 14:32:24 +00:00
|
|
|
#include "DiskLog.h"
|
|
|
|
#include "DiskFormatTrack.h"
|
2010-01-03 18:43:08 +00:00
|
|
|
#include "DiskImage.h"
|
2006-02-25 20:50:29 +00:00
|
|
|
|
2010-01-03 18:43:08 +00:00
|
|
|
enum Drive_e
|
|
|
|
{
|
|
|
|
DRIVE_1 = 0,
|
|
|
|
DRIVE_2,
|
|
|
|
NUM_DRIVES
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool IMAGE_USE_FILES_WRITE_PROTECT_STATUS = false;
|
|
|
|
const bool IMAGE_FORCE_WRITE_PROTECTED = true;
|
|
|
|
const bool IMAGE_DONT_CREATE = false;
|
|
|
|
const bool IMAGE_CREATE = true;
|
2006-02-25 20:50:29 +00:00
|
|
|
|
2019-04-16 19:19:35 +00:00
|
|
|
class FloppyDisk
|
2019-04-07 14:32:24 +00:00
|
|
|
{
|
2019-04-16 19:19:35 +00:00
|
|
|
public:
|
2019-04-14 15:58:49 +00:00
|
|
|
FloppyDisk()
|
2019-04-07 14:32:24 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2019-04-16 19:19:35 +00:00
|
|
|
~FloppyDisk(){}
|
|
|
|
|
2019-04-07 14:32:24 +00:00
|
|
|
void clear()
|
|
|
|
{
|
2019-09-07 08:02:39 +00:00
|
|
|
m_imagename.clear();
|
|
|
|
m_fullname.clear();
|
2019-04-14 15:58:49 +00:00
|
|
|
m_strFilenameInZip.clear();
|
|
|
|
m_imagehandle = NULL;
|
|
|
|
m_bWriteProtected = false;
|
2019-04-07 14:32:24 +00:00
|
|
|
//
|
2019-04-14 15:58:49 +00:00
|
|
|
m_byte = 0;
|
|
|
|
m_nibbles = 0;
|
2019-07-05 22:01:19 +00:00
|
|
|
m_bitOffset = 0;
|
|
|
|
m_bitCount = 0;
|
|
|
|
m_bitMask = 1 << 7;
|
|
|
|
m_extraCycles = 0.0;
|
2019-04-14 15:58:49 +00:00
|
|
|
m_trackimage = NULL;
|
|
|
|
m_trackimagedata = false;
|
|
|
|
m_trackimagedirty = false;
|
2019-04-07 14:32:24 +00:00
|
|
|
}
|
2019-04-16 19:19:35 +00:00
|
|
|
|
|
|
|
public:
|
2019-09-07 08:02:39 +00:00
|
|
|
std::string m_imagename; // <FILENAME> (ie. no extension)
|
|
|
|
std::string m_fullname; // <FILENAME.EXT> or <FILENAME.zip> : This is persisted to the snapshot file
|
2019-04-16 19:19:35 +00:00
|
|
|
std::string m_strFilenameInZip; // "" or <FILENAME.EXT>
|
|
|
|
ImageInfo* m_imagehandle; // Init'd by InsertDisk() -> ImageOpen()
|
|
|
|
bool m_bWriteProtected;
|
2019-07-05 22:01:19 +00:00
|
|
|
int m_byte; // byte offset
|
|
|
|
int m_nibbles; // # nibbles in track / Init'd by ReadTrack() -> ImageReadTrack()
|
|
|
|
UINT m_bitOffset; // bit offset
|
|
|
|
UINT m_bitCount; // # bits in track
|
|
|
|
BYTE m_bitMask;
|
|
|
|
double m_extraCycles;
|
2019-04-16 19:19:35 +00:00
|
|
|
LPBYTE m_trackimage;
|
|
|
|
bool m_trackimagedata;
|
|
|
|
bool m_trackimagedirty;
|
2019-04-07 14:32:24 +00:00
|
|
|
};
|
|
|
|
|
2019-04-16 19:19:35 +00:00
|
|
|
class FloppyDrive
|
2019-04-07 14:32:24 +00:00
|
|
|
{
|
2019-04-16 19:19:35 +00:00
|
|
|
public:
|
2019-04-14 15:47:41 +00:00
|
|
|
FloppyDrive()
|
2019-04-07 14:32:24 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2019-04-16 19:19:35 +00:00
|
|
|
~FloppyDrive(){}
|
|
|
|
|
2019-04-07 14:32:24 +00:00
|
|
|
void clear()
|
|
|
|
{
|
2020-12-12 20:05:29 +00:00
|
|
|
m_isConnected = true;
|
2019-07-05 22:01:19 +00:00
|
|
|
m_phasePrecise = 0;
|
2019-04-14 15:47:41 +00:00
|
|
|
m_phase = 0;
|
2019-07-05 22:01:19 +00:00
|
|
|
m_lastStepperCycle = 0;
|
2020-12-12 20:05:29 +00:00
|
|
|
m_motorOnCycle = 0;
|
2019-07-05 22:01:19 +00:00
|
|
|
m_headWindow = 0;
|
2019-04-14 15:47:41 +00:00
|
|
|
m_spinning = 0;
|
|
|
|
m_writelight = 0;
|
|
|
|
m_disk.clear();
|
2019-04-07 14:32:24 +00:00
|
|
|
}
|
2019-04-16 19:19:35 +00:00
|
|
|
|
|
|
|
public:
|
2020-12-12 20:05:29 +00:00
|
|
|
bool m_isConnected;
|
2019-07-05 22:01:19 +00:00
|
|
|
float m_phasePrecise; // Phase precise to half a phase (aka quarter track)
|
|
|
|
int m_phase; // Integral phase number
|
|
|
|
unsigned __int64 m_lastStepperCycle;
|
2020-12-12 20:05:29 +00:00
|
|
|
unsigned __int64 m_motorOnCycle;
|
2019-07-05 22:01:19 +00:00
|
|
|
BYTE m_headWindow;
|
2019-04-16 19:19:35 +00:00
|
|
|
DWORD m_spinning;
|
|
|
|
DWORD m_writelight;
|
|
|
|
FloppyDisk m_disk;
|
2019-04-07 14:32:24 +00:00
|
|
|
};
|
|
|
|
|
2019-12-19 19:42:30 +00:00
|
|
|
class Disk2InterfaceCard : public Card
|
2019-04-07 13:22:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-05-25 19:21:36 +00:00
|
|
|
Disk2InterfaceCard(UINT slot);
|
2019-12-19 19:42:30 +00:00
|
|
|
virtual ~Disk2InterfaceCard(void);
|
|
|
|
|
|
|
|
virtual void Init(void) {};
|
|
|
|
virtual void Reset(const bool powerCycle);
|
2019-04-07 13:22:05 +00:00
|
|
|
|
2019-04-08 09:41:47 +00:00
|
|
|
void Initialize(LPBYTE pCxRomPeripheral, UINT uSlot);
|
|
|
|
void Destroy(void); // no, doesn't "destroy" the disk image. DiskIIManagerShutdown()
|
|
|
|
|
|
|
|
void Boot(void);
|
2019-04-08 15:54:11 +00:00
|
|
|
void FlushCurrentTrack(const int drive);
|
2019-04-08 09:41:47 +00:00
|
|
|
|
2019-09-07 08:02:39 +00:00
|
|
|
const std::string & GetFullDiskFilename(const int drive);
|
|
|
|
const std::string & GetFullName(const int drive);
|
|
|
|
const std::string & GetBaseName(const int drive);
|
2020-10-25 17:14:23 +00:00
|
|
|
void GetFilenameAndPathForSaveState(std::string& filename, std::string& path);
|
2019-04-08 09:41:47 +00:00
|
|
|
void GetLightStatus (Disk_Status_e* pDisk1Status, Disk_Status_e* pDisk2Status);
|
|
|
|
|
2019-04-08 15:54:11 +00:00
|
|
|
ImageError_e InsertDisk(const int drive, LPCTSTR pszImageFilename, const bool bForceWriteProtected, const bool bCreateIfNecessary);
|
|
|
|
void EjectDisk(const int drive);
|
2020-12-12 20:49:46 +00:00
|
|
|
void UnplugDrive(const int drive);
|
2019-04-08 09:41:47 +00:00
|
|
|
|
|
|
|
bool IsConditionForFullSpeed(void);
|
2019-04-08 15:54:11 +00:00
|
|
|
void NotifyInvalidImage(const int drive, LPCTSTR pszImageFilename, const ImageError_e Error);
|
|
|
|
bool GetProtect(const int drive);
|
|
|
|
void SetProtect(const int drive, const bool bWriteProtect);
|
2020-02-22 11:38:25 +00:00
|
|
|
UINT GetCurrentFirmware(void) { return m_is13SectorFirmware ? 13 : 16; }
|
2019-04-08 09:41:47 +00:00
|
|
|
int GetCurrentDrive(void);
|
2019-07-05 22:01:19 +00:00
|
|
|
int GetCurrentTrack(void);
|
|
|
|
float GetCurrentPhase(void);
|
2019-04-08 09:41:47 +00:00
|
|
|
int GetCurrentOffset(void);
|
2019-07-05 22:01:19 +00:00
|
|
|
BYTE GetCurrentLSSBitMask(void);
|
|
|
|
double GetCurrentExtraCycles(void);
|
|
|
|
int GetTrack(const int drive);
|
|
|
|
std::string GetCurrentTrackString(void);
|
|
|
|
std::string GetCurrentPhaseString(void);
|
2019-04-08 09:41:47 +00:00
|
|
|
LPCTSTR GetCurrentState(void);
|
2019-04-08 15:54:11 +00:00
|
|
|
bool UserSelectNewDiskImage(const int drive, LPCSTR pszFilename="");
|
2019-04-09 17:13:05 +00:00
|
|
|
void UpdateDriveState(DWORD cycles);
|
2019-04-08 09:41:47 +00:00
|
|
|
bool DriveSwap(void);
|
2020-12-12 20:05:29 +00:00
|
|
|
bool IsDriveConnected(int drive) { return m_floppyDrive[drive].m_isConnected; }
|
2019-04-08 09:41:47 +00:00
|
|
|
|
2019-12-19 19:42:30 +00:00
|
|
|
static std::string GetSnapshotCardName(void);
|
2019-04-08 09:41:47 +00:00
|
|
|
void SaveSnapshot(class YamlSaveHelper& yamlSaveHelper);
|
|
|
|
bool LoadSnapshot(class YamlLoadHelper& yamlLoadHelper, UINT slot, UINT version);
|
|
|
|
|
2019-04-08 15:54:11 +00:00
|
|
|
void LoadLastDiskImage(const int drive);
|
|
|
|
void SaveLastDiskImage(const int drive);
|
2019-04-08 09:41:47 +00:00
|
|
|
|
2019-04-08 15:54:11 +00:00
|
|
|
bool IsDiskImageWriteProtected(const int drive);
|
|
|
|
bool IsDriveEmpty(const int drive);
|
2019-04-08 09:41:47 +00:00
|
|
|
|
|
|
|
bool GetEnhanceDisk(void);
|
|
|
|
void SetEnhanceDisk(bool bEnhanceDisk);
|
|
|
|
|
|
|
|
static BYTE __stdcall IORead(WORD pc, WORD addr, BYTE bWrite, BYTE d, ULONG nExecutedCycles);
|
|
|
|
static BYTE __stdcall IOWrite(WORD pc, WORD addr, BYTE bWrite, BYTE d, ULONG nExecutedCycles);
|
2019-04-07 13:22:05 +00:00
|
|
|
|
|
|
|
private:
|
2019-07-05 22:01:19 +00:00
|
|
|
void ResetSwitches(void);
|
2020-12-12 20:05:29 +00:00
|
|
|
void CheckSpinning(const bool stateChanged, const ULONG uExecutedCycles);
|
2019-04-08 15:54:11 +00:00
|
|
|
Disk_Status_e GetDriveLightStatus(const int drive);
|
|
|
|
bool IsDriveValid(const int drive);
|
2019-12-19 19:42:30 +00:00
|
|
|
void EjectDiskInternal(const int drive);
|
2019-12-31 12:07:45 +00:00
|
|
|
void AllocTrack(const int drive, const UINT minSize=NIBBLES_PER_TRACK);
|
2019-07-05 22:01:19 +00:00
|
|
|
void ReadTrack(const int drive, ULONG uExecutedCycles);
|
2019-04-08 15:54:11 +00:00
|
|
|
void WriteTrack(const int drive);
|
2019-09-07 08:02:39 +00:00
|
|
|
const std::string & DiskGetFullPathName(const int drive);
|
2019-07-05 22:01:19 +00:00
|
|
|
void ResetLogicStateSequencer(void);
|
2020-02-09 21:23:15 +00:00
|
|
|
UINT GetBitCellDelta(const ULONG uExecutedCycles);
|
2019-07-05 22:01:19 +00:00
|
|
|
void UpdateBitStreamPosition(FloppyDisk& floppy, const ULONG bitCellDelta);
|
|
|
|
void UpdateBitStreamOffsets(FloppyDisk& floppy);
|
2020-02-09 21:23:15 +00:00
|
|
|
__forceinline void IncBitStream(FloppyDisk& floppy);
|
2019-08-03 17:10:39 +00:00
|
|
|
void DataLatchReadWOZ(WORD pc, WORD addr, UINT bitCellRemainder);
|
2020-02-09 21:23:15 +00:00
|
|
|
void DataLoadWriteWOZ(WORD pc, WORD addr, UINT bitCellRemainder);
|
|
|
|
void DataShiftWriteWOZ(WORD pc, WORD addr, ULONG uExecutedCycles);
|
2019-09-30 20:29:58 +00:00
|
|
|
void SetSequencerFunction(WORD addr);
|
2019-08-03 16:51:19 +00:00
|
|
|
void DumpSectorWOZ(FloppyDisk floppy);
|
|
|
|
void DumpTrackWOZ(FloppyDisk floppy);
|
2021-01-19 20:37:43 +00:00
|
|
|
bool GetFirmware(WORD lpNameId, BYTE* pDst);
|
2020-02-22 11:38:25 +00:00
|
|
|
void InitFirmware(LPBYTE pCxRomPeripheral);
|
2020-12-12 20:05:29 +00:00
|
|
|
void UpdateLatchForEmptyDrive(FloppyDrive* pDrive);
|
2019-08-03 16:51:19 +00:00
|
|
|
|
2019-07-05 22:01:19 +00:00
|
|
|
void SaveSnapshotFloppy(YamlSaveHelper& yamlSaveHelper, UINT unit);
|
|
|
|
void SaveSnapshotDriveUnit(YamlSaveHelper& yamlSaveHelper, UINT unit);
|
|
|
|
bool LoadSnapshotFloppy(YamlLoadHelper& yamlLoadHelper, UINT unit, UINT version, std::vector<BYTE>& track);
|
|
|
|
bool LoadSnapshotDriveUnitv3(YamlLoadHelper& yamlLoadHelper, UINT unit, UINT version, std::vector<BYTE>& track);
|
|
|
|
bool LoadSnapshotDriveUnitv4(YamlLoadHelper& yamlLoadHelper, UINT unit, UINT version, std::vector<BYTE>& track);
|
|
|
|
void LoadSnapshotDriveUnit(YamlLoadHelper& yamlLoadHelper, UINT unit, UINT version);
|
2019-04-08 09:41:47 +00:00
|
|
|
|
|
|
|
void __stdcall ControlStepper(WORD, WORD address, BYTE, BYTE, ULONG uExecutedCycles);
|
|
|
|
void __stdcall ControlMotor(WORD, WORD address, BYTE, BYTE, ULONG uExecutedCycles);
|
|
|
|
void __stdcall Enable(WORD, WORD address, BYTE, BYTE, ULONG uExecutedCycles);
|
2019-07-05 22:01:19 +00:00
|
|
|
void __stdcall ReadWrite(WORD pc, WORD addr, BYTE bWrite, BYTE d, ULONG uExecutedCycles);
|
2020-02-09 21:23:15 +00:00
|
|
|
void __stdcall DataLatchReadWriteWOZ(WORD pc, WORD addr, BYTE bWrite, ULONG uExecutedCycles);
|
2019-04-08 09:41:47 +00:00
|
|
|
void __stdcall LoadWriteProtect(WORD, WORD, BYTE write, BYTE value, ULONG);
|
|
|
|
void __stdcall SetReadMode(WORD, WORD, BYTE, BYTE, ULONG);
|
|
|
|
void __stdcall SetWriteMode(WORD, WORD, BYTE, BYTE, ULONG uExecutedCycles);
|
2019-04-07 13:22:05 +00:00
|
|
|
|
2019-04-07 14:32:24 +00:00
|
|
|
#if LOG_DISK_NIBBLES_WRITE
|
2019-04-08 09:41:47 +00:00
|
|
|
bool LogWriteCheckSyncFF(ULONG& uCycleDelta);
|
2019-04-07 14:32:24 +00:00
|
|
|
#endif
|
2018-01-14 18:01:22 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
|
2020-02-22 11:38:25 +00:00
|
|
|
static const UINT DISK2_FW_SIZE = 256;
|
|
|
|
BYTE m_13SectorFirmware[DISK2_FW_SIZE];
|
|
|
|
BYTE m_16SectorFirmware[DISK2_FW_SIZE];
|
|
|
|
bool m_is13SectorFirmware;
|
|
|
|
|
2019-04-07 14:54:26 +00:00
|
|
|
WORD m_currDrive;
|
2019-04-14 15:47:41 +00:00
|
|
|
FloppyDrive m_floppyDrive[NUM_DRIVES];
|
2019-04-07 14:54:26 +00:00
|
|
|
BYTE m_floppyLatch;
|
|
|
|
BOOL m_floppyMotorOn;
|
2019-07-05 22:01:19 +00:00
|
|
|
|
|
|
|
// Although the magnets are a property of the drive, their state is a property of the controller card,
|
|
|
|
// since the magnets will only be on for whichever of the 2 drives is currently selected.
|
|
|
|
WORD m_magnetStates; // state bits for stepper motor magnet states (phases 0 - 3)
|
|
|
|
|
2019-04-07 14:54:26 +00:00
|
|
|
bool m_saveDiskImage;
|
|
|
|
UINT m_slot;
|
|
|
|
unsigned __int64 m_diskLastCycle;
|
|
|
|
unsigned __int64 m_diskLastReadLatchCycle;
|
|
|
|
FormatTrack m_formatTrack;
|
|
|
|
bool m_enhanceDisk;
|
2019-04-07 14:32:24 +00:00
|
|
|
|
2019-07-05 22:01:19 +00:00
|
|
|
static const UINT SPINNING_CYCLES = 1000*1000; // 1M cycles = ~1.000s
|
|
|
|
static const UINT WRITELIGHT_CYCLES = 1000*1000; // 1M cycles = ~1.000s
|
2020-12-12 20:05:29 +00:00
|
|
|
static const UINT MOTOR_ON_UNTIL_LSS_STABLE_CYCLES = 0x2EC; // ~0x2EC-0x990 cycles (depending on card). See GH#864
|
2019-07-05 22:01:19 +00:00
|
|
|
|
|
|
|
// Logic State Sequencer (for WOZ):
|
|
|
|
BYTE m_shiftReg;
|
|
|
|
int m_latchDelay;
|
|
|
|
bool m_resetSequencer;
|
2020-02-09 21:23:15 +00:00
|
|
|
bool m_writeStarted;
|
2019-10-05 08:53:02 +00:00
|
|
|
|
2020-02-09 21:23:15 +00:00
|
|
|
enum SEQFUNC {readSequencing=0, dataShiftWrite, checkWriteProtAndInitWrite, dataLoadWrite}; // UTAIIe 9-14
|
2019-10-13 15:01:00 +00:00
|
|
|
union SEQUENCER_FUNCTION
|
2019-10-05 08:53:02 +00:00
|
|
|
{
|
2019-10-13 15:01:00 +00:00
|
|
|
struct
|
2019-10-05 08:53:02 +00:00
|
|
|
{
|
2019-10-13 15:01:00 +00:00
|
|
|
UINT writeMode : 1; // $C08E/F,X
|
|
|
|
UINT loadMode : 1; // $C08C/D,X
|
2019-10-05 08:53:02 +00:00
|
|
|
};
|
2019-10-13 15:01:00 +00:00
|
|
|
SEQFUNC function;
|
2019-10-05 08:53:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SEQUENCER_FUNCTION m_seqFunc;
|
2019-07-05 22:01:19 +00:00
|
|
|
UINT m_dbgLatchDelayedCnt;
|
2019-04-07 14:32:24 +00:00
|
|
|
|
|
|
|
// Debug:
|
|
|
|
#if LOG_DISK_NIBBLES_USE_RUNTIME_VAR
|
2019-04-07 14:54:26 +00:00
|
|
|
bool m_bLogDisk_NibblesRW; // From VS Debugger, change this to true/false during runtime for precise nibble logging
|
2019-04-07 14:32:24 +00:00
|
|
|
#endif
|
|
|
|
#if LOG_DISK_NIBBLES_WRITE
|
2019-04-07 14:54:26 +00:00
|
|
|
UINT64 m_uWriteLastCycle;
|
|
|
|
UINT m_uSyncFFCount;
|
2019-04-07 14:32:24 +00:00
|
|
|
#endif
|
2018-01-14 18:01:22 +00:00
|
|
|
};
|