Added convenience methods for setting/getting sector size in bytes

This commit is contained in:
Uwe Seimet 2021-08-25 10:51:45 +02:00
parent 999dbdf492
commit 7110ea532a
8 changed files with 114 additions and 102 deletions

View File

@ -1752,16 +1752,46 @@ bool Disk::GetStartAndCount(SASIDEV *controller, uint64_t& start, uint32_t& coun
return true;
}
int Disk::GetSectorSizeInBytes() const
{
return 1 << disk.size;
}
void Disk::SetSectorSizeInBytes(int size)
{
switch (size) {
case 256:
disk.size = 8;
break;
case 512:
disk.size = 9;
break;
case 1024:
disk.size = 10;
break;
case 2048:
disk.size = 11;
break;
case 4096:
disk.size = 12;
break;
default:
assert(false);
disk.size = 9;
break;
}
}
int Disk::GetSectorSize() const
{
return disk.size;
}
void Disk::SetSectorSize(int size)
{
disk.size = size;
}
bool Disk::IsSectorSizeConfigurable() const
{
return !sector_sizes.empty();
@ -1777,9 +1807,16 @@ int Disk::GetConfiguredSectorSize() const
return configured_sector_size;
}
void Disk::SetConfiguredSectorSize(int configured_sector_size)
bool Disk::SetConfiguredSectorSize(int configured_sector_size)
{
if (configured_sector_size != 512 && configured_sector_size != 1024 &&
configured_sector_size != 2048 && configured_sector_size != 4096) {
return false;
}
this->configured_sector_size = configured_sector_size;
return true;
}
uint32_t Disk::GetBlockCount() const

View File

@ -123,13 +123,14 @@ public:
int SelectCheck(const DWORD *cdb); // SELECT check
int SelectCheck10(const DWORD *cdb); // SELECT(10) check
int GetSectorSizeInBytes() const;
void SetSectorSizeInBytes(int);
int GetSectorSize() const;
void SetSectorSize(int);
bool IsSectorSizeConfigurable() const;
vector<int> GetSectorSizes() const;
void SetSectorSizes(const vector<int>&);
int GetConfiguredSectorSize() const;
void SetConfiguredSectorSize(int);
bool SetConfiguredSectorSize(int);
uint32_t GetBlockCount() const;
void SetBlockCount(DWORD);
bool GetStartAndCount(SASIDEV *, uint64_t&, uint32_t&, access_mode);

View File

@ -74,7 +74,7 @@ DiskTrack::~DiskTrack()
void DiskTrack::Init(int track, int size, int sectors, BOOL raw, off_t imgoff)
{
ASSERT(track >= 0);
ASSERT((size >= 8) && (size <= 11));
ASSERT((size >= 8) && (size <= 12));
ASSERT((sectors > 0) && (sectors <= 0x100));
ASSERT(imgoff >= 0);
@ -129,7 +129,7 @@ BOOL DiskTrack::Load(const Filepath& path)
int length = dt.sectors << dt.size;
// Allocate buffer memory
ASSERT((dt.size >= 8) && (dt.size <= 11));
ASSERT((dt.size >= 8) && (dt.size <= 12));
ASSERT((dt.sectors > 0) && (dt.sectors <= 0x100));
if (dt.buffer == NULL) {
@ -237,7 +237,7 @@ BOOL DiskTrack::Save(const Filepath& path)
// Need to write
ASSERT(dt.buffer);
ASSERT(dt.changemap);
ASSERT((dt.size >= 8) && (dt.size <= 11));
ASSERT((dt.size >= 8) && (dt.size <= 12));
ASSERT((dt.sectors > 0) && (dt.sectors <= 0x100));
// Writing in RAW mode is not allowed
@ -329,7 +329,7 @@ BOOL DiskTrack::Read(BYTE *buf, int sec) const
// Copy
ASSERT(dt.buffer);
ASSERT((dt.size >= 8) && (dt.size <= 11));
ASSERT((dt.size >= 8) && (dt.size <= 12));
ASSERT((dt.sectors > 0) && (dt.sectors <= 0x100));
memcpy(buf, &dt.buffer[(off_t)sec << dt.size], (off_t)1 << dt.size);
@ -364,7 +364,7 @@ BOOL DiskTrack::Write(const BYTE *buf, int sec)
// Compare
ASSERT(dt.buffer);
ASSERT((dt.size >= 8) && (dt.size <= 11));
ASSERT((dt.size >= 8) && (dt.size <= 12));
ASSERT((dt.sectors > 0) && (dt.sectors <= 0x100));
if (memcmp(buf, &dt.buffer[offset], length) == 0) {
// 同じものを書き込もうとしているので、正常終了
@ -393,7 +393,7 @@ BOOL DiskTrack::Write(const BYTE *buf, int sec)
//---------------------------------------------------------------------------
DiskCache::DiskCache(const Filepath& path, int size, int blocks, off_t imgoff)
{
ASSERT((size >= 8) && (size <= 11));
ASSERT((size >= 8) && (size <= 12));
ASSERT(blocks > 0);
ASSERT(imgoff >= 0);

View File

@ -17,6 +17,7 @@
#include "xm6.h"
#include "fileio.h"
#include "exceptions.h"
#include <sstream>
//===========================================================================
//
@ -73,7 +74,7 @@ void SASIHD::Open(const Filepath& path)
// 20M(22437888 BS=1024 C=21912)
if (size == 0x1566000) {
// Sector size and number of blocks
SetSectorSize(10);
SetSectorSizeInBytes(1024);
SetBlockCount((DWORD)(size >> 10));
Disk::Open(path);
@ -83,8 +84,10 @@ void SASIHD::Open(const Filepath& path)
#if defined(REMOVE_FIXED_SASIHD_SIZE)
// Must be in 256-byte units
if (size & 0xff) {
throw io_exception("File size must be a multiple of 256 bytes");
if (size % 256) {
stringstream error;
error << "File size must be a multiple of 256 bytes but is " << size << " bytes";
throw io_exception(error.str());
}
// 10MB or more
@ -118,7 +121,7 @@ void SASIHD::Open(const Filepath& path)
#endif // REMOVE_FIXED_SASIHD_SIZE
// Sector size 256 bytes and number of blocks
SetSectorSize(8);
SetSectorSizeInBytes(256);
SetBlockCount((DWORD)(size >> 8));
// Call the base class

View File

@ -18,6 +18,7 @@
#include "scsicd.h"
#include "fileio.h"
#include "exceptions.h"
#include <sstream>
//===========================================================================
//
@ -341,7 +342,7 @@ void SCSICD::Open(const Filepath& path)
ASSERT(GetBlockCount() > 0);
// Sector size 2048 bytes
SetSectorSize(11);
SetSectorSizeInBytes(2048);
Disk::Open(path);
FileSupport::SetPath(path);
@ -420,8 +421,10 @@ void SCSICD::OpenIso(const Filepath& path)
if (rawfile) {
// Size must be a multiple of 2536 and less than 700MB
if (size % 0x930) {
throw io_exception("Raw ISO CD-ROM file size must be a multiple of 2536 bytes");
if (size % 2536) {
stringstream error;
error << "Raw ISO CD-ROM file size must be a multiple of 2536 bytes but is " << size << " bytes";
throw io_exception(error.str());
}
if (size > 912579600) {
throw io_exception("Raw ISO CD-ROM file size must not exceed 700 MB");
@ -431,7 +434,7 @@ void SCSICD::OpenIso(const Filepath& path)
SetBlockCount((DWORD)(size / 0x930));
} else {
// Size must be a multiple of 2048 and less than 700MB
if (size & 0x7ff) {
if (size % 2048) {
throw io_exception("ISO CD-ROM file size must be a multiple of 2048 bytes");
}
if (size > 0x2bed5000) {

View File

@ -70,9 +70,15 @@ void SCSIHD::Open(const Filepath& path)
off_t size = fio.GetFileSize();
fio.Close();
// Must be a multiple of 512 bytes
if (size & 0x1ff) {
throw io_exception("File size must be a multiple of 512 bytes");
// sector size (default 512 bytes) and number of blocks
SetSectorSizeInBytes(GetConfiguredSectorSize() ? GetConfiguredSectorSize() : 512);
SetBlockCount((DWORD)(size >> GetSectorSize()));
// File size must be a multiple of the sector size
if (size % GetSectorSizeInBytes()) {
stringstream error;
error << "File size must be a multiple of " << GetSectorSizeInBytes() << " bytes but is " << size << " bytes";
throw io_exception(error.str());
}
// 2TB is the current maximum
@ -80,10 +86,6 @@ void SCSIHD::Open(const Filepath& path)
throw io_exception("File size must not exceed 2 TB");
}
// sector size (default 512 bytes) and number of blocks
SetSectorSize(GetConfiguredSectorSize() ? GetConfiguredSectorSize() : 9);
SetBlockCount((DWORD)(size >> GetSectorSize()));
// Set the default product name based on the drive capacity
stringstream product;
product << DEFAULT_PRODUCT << " " << (GetBlockCount() >> 11) << " MB";

View File

@ -57,35 +57,35 @@ void SCSIMO::Open(const Filepath& path)
// 128MB
case 0x797f400:
// 512 bytes per sector
SetSectorSize(9);
SetSectorSizeInBytes(512);
SetBlockCount(248826);
break;
// 230MB
case 0xd9eea00:
// 512 bytes per sector
SetSectorSize(9);
SetSectorSizeInBytes(512);
SetBlockCount(446325);
break;
// 540MB
case 0x1fc8b800:
// 512 bytes per sector
SetSectorSize(9);
SetSectorSizeInBytes(512);
SetBlockCount(1041500);
break;
// 640MB
case 0x25e28000:
// 2048 bytes per sector
SetSectorSize(11);
SetSectorSizeInBytes(2048);
SetBlockCount(310352);
break;
// Other (this is an error)
default:
throw io_exception("Invalid MO file size, supported sizes are 127398912 bytes (128 MB), "
"228518400 bytes (230 MB), 533248000 bytes (540 MB), 635600896 bytes (640 MB)");
throw io_exception("Invalid MO file size, supported sizes are 127398912 bytes (128 MB, 512 BPS), "
"228518400 bytes (230 MB, 512 BPS), 533248000 bytes (540 MB, 512 BPS), 635600896 bytes (640 MB, 2048 BPS)");
}
Disk::Open(path);
@ -257,38 +257,42 @@ int SCSIMO::AddVendor(int page, BOOL change, BYTE *buf)
unsigned bands = 0;
uint32_t blocks = GetBlockCount();
if (GetSectorSize() == 9) switch (blocks) {
// 128MB
case 248826:
spare = 1024;
bands = 1;
break;
if (GetSectorSizeInBytes() == 512) {
switch (blocks) {
// 128MB
case 248826:
spare = 1024;
bands = 1;
break;
// 230MB
case 446325:
spare = 1025;
bands = 10;
break;
// 230MB
case 446325:
spare = 1025;
bands = 10;
break;
// 540MB
case 1041500:
spare = 2250;
bands = 18;
break;
// 540MB
case 1041500:
spare = 2250;
bands = 18;
break;
}
}
if (GetSectorSize() == 11) switch (blocks) {
// 640MB
case 310352:
spare = 2244;
bands = 11;
break;
if (GetSectorSizeInBytes() == 2048) {
switch (blocks) {
// 640MB
case 310352:
spare = 2244;
bands = 11;
break;
// 1.3GB (lpproj: not tested with real device)
case 605846:
spare = 4437;
bands = 18;
break;
// 1.3GB (lpproj: not tested with real device)
case 605846:
spare = 4437;
bands = 18;
break;
}
}
buf[2] = 0; // format mode

View File

@ -304,28 +304,7 @@ const PbDevices GetDevices()
status->set_locked(device->IsLocked());
const Disk *disk = dynamic_cast<Disk*>(device);
if (disk) {
switch (disk->GetSectorSize()) {
case 9:
pbDevice->set_block_size(512);
break;
case 10:
pbDevice->set_block_size(1024);
break;
case 11:
pbDevice->set_block_size(2048);
break;
case 12:
pbDevice->set_block_size(4096);
break;
default:
break;
}
}
pbDevice->set_block_size(disk->GetSectorSizeInBytes());
const FileSupport *fileSupport = dynamic_cast<FileSupport *>(device);
if (fileSupport) {
@ -720,24 +699,7 @@ bool ProcessCmd(int fd, const PbDeviceDefinition& pbDevice, const PbOperation cm
if (pbDevice.block_size()) {
Disk *disk = dynamic_cast<Disk *>(device);
if (disk && disk->IsSectorSizeConfigurable()) {
switch (pbDevice.block_size()) {
case 512:
disk->SetConfiguredSectorSize(9);
break;
case 1024:
disk->SetConfiguredSectorSize(10);
break;
case 2048:
disk->SetConfiguredSectorSize(11);
break;
case 4096:
disk->SetConfiguredSectorSize(12);
break;
default:
if (!disk->SetConfiguredSectorSize(pbDevice.block_size())) {
error << "Invalid block size " << pbDevice.block_size();
return ReturnStatus(fd, false, error);
}