Ran formatting tool

This commit is contained in:
RaSCSI User 2022-03-14 02:28:40 +00:00
parent 8e48e72bf1
commit 9ba4a9243d
9 changed files with 107 additions and 102 deletions

View File

@ -108,7 +108,7 @@ void Disk::Open(const Filepath& path)
// Cache initialization // Cache initialization
assert (!disk.dcache); assert (!disk.dcache);
disk.dcache = DiskImageHandleFactory::CreateFileAccess(path, disk.size, disk.blocks, disk.image_offset); disk.dcache = DiskImageHandleFactory::CreateDiskImageHandle(path, disk.size, disk.blocks, disk.image_offset);
// Can read/write open // Can read/write open
Fileio fio; Fileio fio;

View File

@ -533,7 +533,7 @@ int SCSICD::Read(const DWORD *cdb, BYTE *buf, uint64_t block)
// Recreate the disk cache // Recreate the disk cache
Filepath path; Filepath path;
track[index]->GetPath(path); track[index]->GetPath(path);
disk.dcache = DiskImageHandleFactory::CreateFileAccess(path, GetSectorSizeShiftCount(), GetBlockCount()); disk.dcache = DiskImageHandleFactory::CreateDiskImageHandle(path, GetSectorSizeShiftCount(), GetBlockCount());
disk.dcache->SetRawMode(rawfile); disk.dcache->SetRawMode(rawfile);
// Reset data index // Reset data index

View File

@ -5,45 +5,50 @@
// //
// Copyright (C) 2022 akuker // Copyright (C) 2022 akuker
// //
// Base class for interfacing with disk images. // Base class for interfacing with disk images.
// //
// [ DiskImageHandle ] // [ DiskImageHandle ]
// //
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "disk_image/disk_image_handle.h" #include "disk_image/disk_image_handle.h"
DiskImageHandle::DiskImageHandle(const Filepath& path, int size, uint32_t blocks, off_t imgoff){ DiskImageHandle::DiskImageHandle(const Filepath &path, int size, uint32_t blocks, off_t imgoff)
{
serial = 0; serial = 0;
sec_path = path; sec_path = path;
sec_size = size; sec_size = size;
sec_blocks = blocks; sec_blocks = blocks;
imgoffset = imgoff; imgoffset = imgoff;
} }
DiskImageHandle::~DiskImageHandle(){ DiskImageHandle::~DiskImageHandle()
{
} }
off_t DiskImageHandle::GetSectorOffset(int block)
off_t DiskImageHandle::GetSectorOffset(int block){ {
int sector_num = block & 0xff; int sector_num = block & 0xff;
return (off_t)sector_num << sec_size; return (off_t)sector_num << sec_size;
} }
off_t DiskImageHandle::GetTrackOffset(int block){ off_t DiskImageHandle::GetTrackOffset(int block)
{
// Assuming that all tracks hold 256 sectors // Assuming that all tracks hold 256 sectors
int track_num = block >> 8; int track_num = block >> 8;
// Calculate offset (previous tracks are considered to hold 256 sectors) // Calculate offset (previous tracks are considered to hold 256 sectors)
off_t offset = ((off_t)track_num << 8); off_t offset = ((off_t)track_num << 8);
if (cd_raw) { if (cd_raw)
{
ASSERT(sec_size == 11); ASSERT(sec_size == 11);
offset *= 0x930; offset *= 0x930;
offset += 0x10; offset += 0x10;
} else { }
else
{
offset <<= sec_size; offset <<= sec_size;
} }

View File

@ -5,11 +5,11 @@
// //
// Copyright (C) 2022 akuker // Copyright (C) 2022 akuker
// //
// Base class for interfacing with disk images. // Base class for interfacing with disk images.
// //
// [ DiskImageHandle ] // [ DiskImageHandle ]
// //
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#pragma once #pragma once
@ -18,28 +18,26 @@
class DiskImageHandle class DiskImageHandle
{ {
public: public:
DiskImageHandle(const Filepath& path, int size, uint32_t blocks, off_t imgoff = 0); DiskImageHandle(const Filepath &path, int size, uint32_t blocks, off_t imgoff = 0);
virtual ~DiskImageHandle(); virtual ~DiskImageHandle();
void SetRawMode(bool raw) { cd_raw = raw; }; // CD-ROM raw mode setting void SetRawMode(bool raw) { cd_raw = raw; }; // CD-ROM raw mode setting
// Access // Access
virtual bool Save() = 0; // Save and release all virtual bool Save() = 0; // Save and release all
virtual bool ReadSector(BYTE *buf, int block) = 0; // Sector Read virtual bool ReadSector(BYTE *buf, int block) = 0; // Sector Read
virtual bool WriteSector(const BYTE *buf, int block) = 0; // Sector Write virtual bool WriteSector(const BYTE *buf, int block) = 0; // Sector Write
virtual bool GetCache(int index, int& track, DWORD& serial) const = 0; // Get cache information virtual bool GetCache(int index, int &track, DWORD &serial) const = 0; // Get cache information
protected: protected:
bool cd_raw = false; bool cd_raw = false;
DWORD serial; // Last serial number DWORD serial; // Last serial number
Filepath sec_path; // Path Filepath sec_path; // Path
int sec_size; // Sector Size (8=256, 9=512, 10=1024, 11=2048, 12=4096) int sec_size; // Sector Size (8=256, 9=512, 10=1024, 11=2048, 12=4096)
int sec_blocks; // Blocks per sector int sec_blocks; // Blocks per sector
off_t imgoffset; // Offset to actual data off_t imgoffset; // Offset to actual data
off_t GetTrackOffset(int block); off_t GetTrackOffset(int block);
off_t GetSectorOffset(int block); off_t GetSectorOffset(int block);
}; };

View File

@ -15,10 +15,11 @@
#include "disk_image/disk_image_handle.h" #include "disk_image/disk_image_handle.h"
enum DiskImageHandleType { enum DiskImageHandleType
{
eRamCache, eRamCache,
eMmapFile, eMmapFile,
ePosixFile, ePosixFile,
}; };
class DiskImageHandleFactory class DiskImageHandleFactory
@ -27,7 +28,7 @@ public:
static void SetFileAccessMethod(DiskImageHandleType method) { current_access_type = method; }; static void SetFileAccessMethod(DiskImageHandleType method) { current_access_type = method; };
static DiskImageHandleType GetFileAccessMethod() { return current_access_type; }; static DiskImageHandleType GetFileAccessMethod() { return current_access_type; };
static DiskImageHandle *CreateDiskImageHandle(const Filepath& path, int size, uint32_t blocks, off_t imgoff = 0); static DiskImageHandle *CreateDiskImageHandle(const Filepath &path, int size, uint32_t blocks, off_t imgoff = 0);
private: private:
static DiskImageHandleType current_access_type; static DiskImageHandleType current_access_type;

View File

@ -13,10 +13,10 @@
// //
// The operating system will do some caching of the data to prevent direct // The operating system will do some caching of the data to prevent direct
// drive access for each read/write. So, instead of implementing our own // drive access for each read/write. So, instead of implementing our own
// caching mechanism (like in disk_track_cache.h), we just rely on the // caching mechanism (like in disk_track_cache.h), we just rely on the
// operating system. // operating system.
// //
// [ MmapFileAccess ] // [ MmapFilehandle ]
// //
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -31,39 +31,42 @@
// Direct file access that will map the file into virtual memory space // Direct file access that will map the file into virtual memory space
// //
//=========================================================================== //===========================================================================
MmapFileHandle::MmapFileHandle(const Filepath& path, int size, uint32_t blocks, off_t imgoff) : DiskImageHandle(path, size, blocks, imgoff) MmapFileHandle::MmapFileHandle(const Filepath &path, int size, uint32_t blocks, off_t imgoff) : DiskImageHandle(path, size, blocks, imgoff)
{ {
ASSERT(blocks > 0); ASSERT(blocks > 0);
ASSERT(imgoff >= 0); ASSERT(imgoff >= 0);
fd = open(path.GetPath(), O_RDWR); fd = open(path.GetPath(), O_RDWR);
if(fd < 0){ if (fd < 0)
LOGWARN("Unable to open file %s. Errno:%d", path.GetPath(), errno) {
} LOGWARN("Unable to open file %s. Errno:%d", path.GetPath(), errno)
LOGWARN("%s opened %s", __PRETTY_FUNCTION__, path.GetPath()); }
LOGWARN("%s opened %s", __PRETTY_FUNCTION__, path.GetPath());
struct stat sb; struct stat sb;
if(fstat(fd, &sb) < 0){ if (fstat(fd, &sb) < 0)
LOGWARN("Unable to run fstat. Errno:%d", errno); {
} LOGWARN("Unable to run fstat. Errno:%d", errno);
printf("Size: %llu\n", (uint64_t)sb.st_size); }
printf("Size: %llu\n", (uint64_t)sb.st_size);
LOGWARN("%s mmap-ed file of size: %llu", __PRETTY_FUNCTION__, (uint64_t)sb.st_size); LOGWARN("%s mmap-ed file of size: %llu", __PRETTY_FUNCTION__, (uint64_t)sb.st_size);
// int x = EACCES; // int x = EACCES;
memory_block = (const char*)mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); memory_block = (const char *)mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
int errno_val = errno; int errno_val = errno;
if (memory_block == MAP_FAILED) { if (memory_block == MAP_FAILED)
LOGWARN("Unabled to memory map file %s", path.GetPath()); {
LOGWARN(" Errno:%d", errno_val); LOGWARN("Unabled to memory map file %s", path.GetPath());
return; LOGWARN(" Errno:%d", errno_val);
} return;
initialized = true; }
initialized = true;
} }
MmapFileHandle::~MmapFileHandle() MmapFileHandle::~MmapFileHandle()
{ {
munmap((void*)memory_block, sb.st_size); munmap((void *)memory_block, sb.st_size);
close(fd); close(fd);
// Force the OS to save any cached data to the disk // Force the OS to save any cached data to the disk
@ -97,8 +100,7 @@ bool MmapFileHandle::WriteSector(const BYTE *buf, int block)
ASSERT((block * sec_size) <= (sb.st_size + sec_size)); ASSERT((block * sec_size) <= (sb.st_size + sec_size));
memcpy((void*)&memory_block[(block * sec_size)], buf, sec_size); memcpy((void *)&memory_block[(block * sec_size)], buf, sec_size);
return true; return true;
} }

View File

@ -13,7 +13,7 @@
// //
// The operating system will do some caching of the data to prevent direct // The operating system will do some caching of the data to prevent direct
// drive access for each read/write. So, instead of implementing our own // drive access for each read/write. So, instead of implementing our own
// caching mechanism (like in disk_track_cache.h), we just rely on the // caching mechanism (like in disk_track_cache.h), we just rely on the
// operating system. // operating system.
// //
// [ MmapFileHandle ] // [ MmapFileHandle ]
@ -29,23 +29,21 @@ class MmapFileHandle : public DiskImageHandle
{ {
public: public:
MmapFileHandle(const Filepath& path, int size, uint32_t blocks, off_t imgoff = 0); MmapFileHandle(const Filepath &path, int size, uint32_t blocks, off_t imgoff = 0);
~MmapFileHandle(); ~MmapFileHandle();
void SetRawMode(BOOL raw); // CD-ROM raw mode setting void SetRawMode(BOOL raw); // CD-ROM raw mode setting
// Access // Access
bool Save() { return true; }; // Save and release all bool Save() { return true; }; // Save and release all
bool ReadSector(BYTE *buf, int block); // Sector Read bool ReadSector(BYTE *buf, int block); // Sector Read
bool WriteSector(const BYTE *buf, int block); // Sector Write bool WriteSector(const BYTE *buf, int block); // Sector Write
bool GetCache(int index, int& track, DWORD& serial) const { return true; }; // Get cache information bool GetCache(int index, int &track, DWORD &serial) const { return true; }; // Get cache information
private: private:
const char *memory_block; const char *memory_block;
struct stat sb; struct stat sb;
int fd; int fd;
bool initialized = false; bool initialized = false;
}; };

View File

@ -20,24 +20,25 @@
// Direct file access that will map the file into virtual memory space // Direct file access that will map the file into virtual memory space
// //
//=========================================================================== //===========================================================================
PosixFileHandle::PosixFileHandle(const Filepath& path, int size, uint32_t blocks, off_t imgoff) : DiskImageHandle(path, size, blocks, imgoff) PosixFileHandle::PosixFileHandle(const Filepath &path, int size, uint32_t blocks, off_t imgoff) : DiskImageHandle(path, size, blocks, imgoff)
{ {
ASSERT(blocks > 0); ASSERT(blocks > 0);
ASSERT(imgoff >= 0); ASSERT(imgoff >= 0);
fd = open(path.GetPath(), O_RDWR);
fd = open(path.GetPath(), O_RDWR); if (fd < 0)
if(fd < 0){ {
LOGWARN("Unable to open file %s. Errno:%d", path.GetPath(), errno) LOGWARN("Unable to open file %s. Errno:%d", path.GetPath(), errno)
return; return;
} }
struct stat sb; struct stat sb;
if(fstat(fd, &sb) < 0){ if (fstat(fd, &sb) < 0)
LOGWARN("Unable to run fstat. Errno:%d", errno); {
return; LOGWARN("Unable to run fstat. Errno:%d", errno);
} return;
}
LOGWARN("%s opened file of size: %llu", __PRETTY_FUNCTION__, (uint64_t)sb.st_size); LOGWARN("%s opened file of size: %llu", __PRETTY_FUNCTION__, (uint64_t)sb.st_size);
initialized = true; initialized = true;
} }
@ -52,7 +53,8 @@ PosixFileHandle::~PosixFileHandle()
bool PosixFileHandle::ReadSector(BYTE *buf, int block) bool PosixFileHandle::ReadSector(BYTE *buf, int block)
{ {
if(!initialized){ if (!initialized)
{
return false; return false;
} }
@ -67,18 +69,20 @@ bool PosixFileHandle::ReadSector(BYTE *buf, int block)
off_t offset = GetTrackOffset(block); off_t offset = GetTrackOffset(block);
offset += GetSectorOffset(block); offset += GetSectorOffset(block);
lseek(fd, offset, SEEK_SET); lseek(fd, offset, SEEK_SET);
size_t result = read(fd, buf,sector_size_bytes); size_t result = read(fd, buf, sector_size_bytes);
if(result != sector_size_bytes){ if (result != sector_size_bytes)
LOGWARN("%s only read %d bytes but wanted %d ", __PRETTY_FUNCTION__, result, sector_size_bytes); {
} LOGWARN("%s only read %d bytes but wanted %d ", __PRETTY_FUNCTION__, result, sector_size_bytes);
}
return true; return true;
} }
bool PosixFileHandle::WriteSector(const BYTE *buf, int block) bool PosixFileHandle::WriteSector(const BYTE *buf, int block)
{ {
if(!initialized){ if (!initialized)
{
return false; return false;
} }
@ -90,17 +94,15 @@ bool PosixFileHandle::WriteSector(const BYTE *buf, int block)
size_t sector_size_bytes = (size_t)1 << sec_size; size_t sector_size_bytes = (size_t)1 << sec_size;
off_t offset = GetTrackOffset(block); off_t offset = GetTrackOffset(block);
offset += GetSectorOffset(block); offset += GetSectorOffset(block);
lseek(fd, offset, SEEK_SET);
lseek(fd, offset, SEEK_SET); size_t result = write(fd, buf, sector_size_bytes);
size_t result = write(fd, buf,sector_size_bytes); if (result != sector_size_bytes)
if(result != sector_size_bytes){ {
LOGWARN("%s only wrote %d bytes but wanted %d ", __PRETTY_FUNCTION__, result, sector_size_bytes); LOGWARN("%s only wrote %d bytes but wanted %d ", __PRETTY_FUNCTION__, result, sector_size_bytes);
} }
return true; return true;
} }

View File

@ -18,19 +18,18 @@ class PosixFileHandle : public DiskImageHandle
{ {
public: public:
PosixFileHandle(const Filepath& path, int size, uint32_t blocks, off_t imgoff = 0); PosixFileHandle(const Filepath &path, int size, uint32_t blocks, off_t imgoff = 0);
~PosixFileHandle(); ~PosixFileHandle();
void SetRawMode(BOOL raw); // CD-ROM raw mode setting void SetRawMode(BOOL raw); // CD-ROM raw mode setting
// Access // Access
bool Save() { return true; }; // Save and release all bool Save() { return true; }; // Save and release all
bool ReadSector(BYTE *buf, int block); // Sector Read bool ReadSector(BYTE *buf, int block); // Sector Read
bool WriteSector(const BYTE *buf, int block); // Sector Write bool WriteSector(const BYTE *buf, int block); // Sector Write
bool GetCache(int index, int& track, DWORD& serial) const { return true; }; // Get cache information bool GetCache(int index, int &track, DWORD &serial) const { return true; }; // Get cache information
private: private:
int fd; int fd;
bool initialized = false; bool initialized = false;
}; };