Skip to content

Commit

Permalink
DriveAPI cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkwhoffmann committed Jul 6, 2024
1 parent ce73bf2 commit c9b595b
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 98 deletions.
2 changes: 1 addition & 1 deletion Emulator/Peripherals/Drive/Drive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Drive::toggleWriteProtection()
if (!hasDisk()) return;

// Toggle the protection flag
setProtectionFlag(hasUnprotectedDisk());
setFlag(FLAG_PROTECTED, !getFlag(FLAG_PROTECTED));
}

}
9 changes: 8 additions & 1 deletion Emulator/Peripherals/Drive/Drive.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#pragma once

#include "Drive.h"
#include "DriveTypes.h"
#include "SubComponent.h"
#include "IOUtils.h"

Expand Down Expand Up @@ -59,6 +59,13 @@ class Drive : public SubComponent {
virtual bool hasDisk() const = 0;
virtual bool hasModifiedDisk() const = 0;
virtual bool hasProtectedDisk() const = 0;

// Gets or sets a disk flag
virtual bool getFlag(DiskFlags mask) const = 0;
virtual void setFlag(DiskFlags mask, bool value) = 0;
void setFlag(DiskFlags mask) { setFlag(mask, true); }
void clearFlag(DiskFlags mask) { setFlag(mask, false); }

bool hasUnmodifiedDisk() const { return hasDisk() && !hasModifiedDisk(); }
bool hasUnprotectedDisk() const { return hasDisk() && !hasProtectedDisk(); }
void toggleWriteProtection();
Expand Down
31 changes: 31 additions & 0 deletions Emulator/Peripherals/Drive/DriveTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@
#include "Aliases.h"
#include "Reflection.h"

//
// Enumerations
//

enum_long(FLAG_DISK)
{
FLAG_PROTECTED = 1,
FLAG_MODIFIED = 2
};
typedef FLAG_DISK DiskFlags;

#ifdef __cplusplus
struct DiskFlagsEnum : util::Reflection<DiskFlagsEnum, DiskFlags>
{
static constexpr long minVal = FLAG_PROTECTED;
static constexpr long maxVal = FLAG_MODIFIED;
static bool isValid(auto val) { return val >= minVal && val <= maxVal; }

static const char *prefix() { return "FLAG"; }
static const char *_key(long value)
{
switch (value) {

case FLAG_PROTECTED: return "PROTECTED";
case FLAG_MODIFIED: return "MODIFIED";
}
return "???";
}
};
#endif


//
// Structures
Expand Down
2 changes: 2 additions & 0 deletions Emulator/Peripherals/Drive/FloppyDisk.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include "FloppyDiskTypes.h"
#include "DriveTypes.h"
#include "CoreComponent.h"

namespace vamiga {
Expand Down Expand Up @@ -181,6 +182,7 @@ class FloppyDisk : public CoreObject {
bool isModified() const { return flags & FLAG_MODIFIED; }
void setModified(bool value) { value ? flags |= FLAG_MODIFIED : flags &= ~FLAG_MODIFIED; }

bool getFlag(DiskFlags mask) { return (flags & mask) == mask; }
void setFlag(DiskFlags mask, bool value) { value ? flags |= mask : flags &= ~mask; }
void setFlag(DiskFlags flag) { setFlag(flag, true); }
void clearFlag(DiskFlags flag) { setFlag(flag, false); }
Expand Down
27 changes: 0 additions & 27 deletions Emulator/Peripherals/Drive/FloppyDiskTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,6 @@ struct DensityEnum : util::Reflection<DensityEnum, Density>
};
#endif

enum_long(FLAG_DISK)
{
FLAG_PROTECTED = 1,
FLAG_MODIFIED = 2
};
typedef FLAG_DISK DiskFlags;

#ifdef __cplusplus
struct DiskFlagsEnum : util::Reflection<DiskFlagsEnum, DiskFlags>
{
static constexpr long minVal = FLAG_PROTECTED;
static constexpr long maxVal = FLAG_MODIFIED;
static bool isValid(auto val) { return val >= minVal && val <= maxVal; }

static const char *prefix() { return "FLAG"; }
static const char *_key(long value)
{
switch (value) {

case FLAG_PROTECTED: return "PROTECTED";
case FLAG_MODIFIED: return "MODIFIED";
}
return "???";
}
};
#endif


//
// Structures
Expand Down
12 changes: 12 additions & 0 deletions Emulator/Peripherals/Drive/FloppyDrive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,18 @@ FloppyDrive::hasProtectedDisk() const
return hasDisk() ? disk->isWriteProtected() : false;
}

bool
FloppyDrive::getFlag(DiskFlags mask) const
{
return disk ? disk->getFlag(mask) : false;
}

void
FloppyDrive::setFlag(DiskFlags mask, bool value)
{
if (disk) disk->setFlag(mask, value);
}

void
FloppyDrive::setModificationFlag(bool value)
{
Expand Down
4 changes: 4 additions & 0 deletions Emulator/Peripherals/Drive/FloppyDrive.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ class FloppyDrive : public Drive, public Inspectable<FloppyDriveInfo> {
bool hasDisk() const override;
bool hasModifiedDisk() const override;
bool hasProtectedDisk() const override;

bool getFlag(DiskFlags mask) const override;
void setFlag(DiskFlags mask, bool value) override;

void setModificationFlag(bool value) override;
void setProtectionFlag(bool value) override;

Expand Down
34 changes: 22 additions & 12 deletions Emulator/Peripherals/Drive/HardDrive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ HardDrive::init()
ptable.clear();
drivers.clear();
head = {};
modified = bool(FORCE_HDR_MODIFIED);
setFlag(FLAG_MODIFIED, FORCE_HDR_MODIFIED);
}

void
Expand Down Expand Up @@ -189,7 +189,7 @@ HardDrive::_initialize()
void
HardDrive::_didReset(bool hard)
{
if (FORCE_HDR_MODIFIED) { modified = true; }
if (FORCE_HDR_MODIFIED) { setFlag(FLAG_MODIFIED, true); }
}

i64
Expand Down Expand Up @@ -332,10 +332,8 @@ HardDrive::_dump(Category category, std::ostream& os) const
os << std::endl;
os << tab("State");
os << HardDriveStateEnum::key(state) << std::endl;
os << tab("Modified");
os << bol(modified) << std::endl;
os << tab("Write protected");
os << bol(writeProtected) << std::endl;
os << tab("Flags");
os << DiskFlagsEnum::key(flags) << std::endl;
os << tab("Bootable");
if (bootable) {
os << bol(*bootable) << std::endl;
Expand Down Expand Up @@ -405,27 +403,39 @@ HardDrive::hasDisk() const
return data.ptr != nullptr;
}

bool
HardDrive::getFlag(DiskFlags mask) const
{
return (flags & mask) == mask;
}

void
HardDrive::setFlag(DiskFlags mask, bool value)
{
value ? flags |= mask : flags &= ~mask;
}

bool
HardDrive::hasModifiedDisk() const
{
return hasDisk() ? modified : false;
return hasDisk() ? getFlag(FLAG_MODIFIED) : false;
}

bool
HardDrive::hasProtectedDisk() const
{
return hasDisk() && writeProtected;
return hasDisk() && getFlag(FLAG_PROTECTED);
}

void
HardDrive::setModificationFlag(bool value)
{
if (hasDisk()) modified = value;
if (hasDisk()) setFlag(FLAG_MODIFIED, value);
}
void
HardDrive::setProtectionFlag(bool value)
{
if (hasDisk()) writeProtected = value;
if (hasDisk()) setFlag(FLAG_PROTECTED, value);
}

void
Expand Down Expand Up @@ -595,7 +605,7 @@ HardDrive::write(isize offset, isize length, u32 addr)
// Move the drive head to the specified location
moveHead(offset / geometry.bsize);

if (!writeProtected) {
if (!getFlag(FLAG_PROTECTED)) {

// Perform the write operation
mem.spypeek <ACCESSOR_CPU> (addr, length, data.ptr + offset);
Expand All @@ -606,7 +616,7 @@ HardDrive::write(isize offset, isize length, u32 addr)
wtStream[objid].write((char *)(data.ptr + offset), length);
}

modified = true;
setFlag(FLAG_PROTECTED, true);
}

// Inform the GUI
Expand Down
18 changes: 12 additions & 6 deletions Emulator/Peripherals/Drive/HardDrive.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ class HardDrive : public Drive, public Inspectable<HardDriveInfo> {
HardDriveState state = HDR_IDLE;

// Disk state flags
bool modified = false;
bool writeProtected = false;
DiskFlags flags = 0;
[[deprecated]] bool modified = false;
[[deprecated]] bool writeProtected = false;
optional <bool> bootable;

// Indicates if write-through mode is enabled
Expand Down Expand Up @@ -173,8 +174,9 @@ class HardDrive : public Drive, public Inspectable<HardDriveInfo> {
<< ptable
<< drivers
<< data
<< modified
<< writeProtected
<< flags
// << modified
// << writeProtected
<< bootable;

} SERIALIZERS(serialize);
Expand Down Expand Up @@ -207,6 +209,10 @@ class HardDrive : public Drive, public Inspectable<HardDriveInfo> {
isize currentOffset() const override { return head.offset; }

bool hasDisk() const override;

bool getFlag(DiskFlags mask) const override;
void setFlag(DiskFlags mask, bool value) override;

bool hasModifiedDisk() const override;
bool hasProtectedDisk() const override;
void setModificationFlag(bool value) override;
Expand Down Expand Up @@ -255,8 +261,8 @@ class HardDrive : public Drive, public Inspectable<HardDriveInfo> {
HardDriveState getState() const { return state; }

// Gets or sets the 'modification' flag
bool isModified() const { return modified; }
void setModified(bool value) { modified = value; }
bool isModified() const { return flags & FLAG_MODIFIED; }
void setModified(bool value) { value ? flags |= FLAG_MODIFIED : flags &= ~FLAG_MODIFIED; }

// Returns the current controller state
HdcState getHdcState();
Expand Down
47 changes: 47 additions & 0 deletions Emulator/VAmiga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,53 @@ FloppyDriveAPI::getCachedInfo() const
return drive->getCachedInfo();
}

bool
FloppyDriveAPI::getFlag(DiskFlags mask)
{
return drive->getFlag(mask);
}

void
FloppyDriveAPI::setFlag(DiskFlags mask, bool value)
{
drive->setFlag(mask, value);
}


//
// Peripherals (HardDrive)
//

const HardDriveConfig &
HardDriveAPI::getConfig() const
{
return drive->getConfig();
}

const HardDriveInfo &
HardDriveAPI::getInfo() const
{
return drive->getInfo();
}

const HardDriveInfo &
HardDriveAPI::getCachedInfo() const
{
return drive->getCachedInfo();
}

bool
HardDriveAPI::getFlag(DiskFlags mask)
{
return drive->getFlag(mask);
}

void
HardDriveAPI::setFlag(DiskFlags mask, bool value)
{
drive->setFlag(mask, value);
}


//
// Miscellaneous (Debugger)
Expand Down
16 changes: 16 additions & 0 deletions Emulator/VAmiga.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ struct FloppyDriveAPI : API {
*/
const FloppyDriveInfo &getInfo() const;
const FloppyDriveInfo &getCachedInfo() const;

/** @brief Queries a disk flag
*/
bool getFlag(DiskFlags mask);

/** @brief Sets or clears one or more disk flags
*/
void setFlag(DiskFlags mask, bool value);
};

struct HardDriveAPI : API {
Expand All @@ -290,6 +298,14 @@ struct HardDriveAPI : API {
*/
const HardDriveInfo &getInfo() const;
const HardDriveInfo &getCachedInfo() const;

/** @brief Queries a disk flag
*/
bool getFlag(DiskFlags mask);

/** @brief Sets or clears one or more disk flags
*/
void setFlag(DiskFlags mask, bool value);
};

struct JoystickAPI : API {
Expand Down
4 changes: 2 additions & 2 deletions GUI/Dialogs/DiskExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class DiskExporter: DialogController {
fatalError()
}

dfn!.markDiskAsUnmodified()
dfn!.setFlag(.MODIFIED, value: false)
myAppDelegate.noteNewRecentlyExportedDiskURL(url, df: dfn!.info.nr)

hide()
Expand Down Expand Up @@ -409,7 +409,7 @@ class DiskExporter: DialogController {
fatalError()
}

hdn!.markDiskAsUnmodified()
hdn!.setFlag(.MODIFIED, value: false)
myAppDelegate.noteNewRecentlyExportedHdrURL(url, hd: hdn!.nr)

hide()
Expand Down
Loading

0 comments on commit c9b595b

Please sign in to comment.