DEV9: Cleanup ATA.

This commit is contained in:
lightningterror 2023-06-16 20:36:38 +02:00
parent 7b428bb6b0
commit c77d8b3709
5 changed files with 65 additions and 82 deletions

View File

@ -1,5 +1,5 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2020 PCSX2 Dev Team
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
@ -46,15 +46,15 @@ void ATA::WritePaddedString(u8* data, int* index, std::string value, u32 len)
void ATA::CreateHDDinfo(u64 sizeSectors)
{
const u16 sectorSize = 512;
constexpr u16 sectorSize = 512;
DevCon.WriteLn("DEV9: HddSize : %i", sizeSectors * sectorSize / (1024 * 1024));
const u64 nbSectors = sizeSectors;
DevCon.WriteLn("DEV9: nbSectors : %i", nbSectors);
memset(&identifyData, 0, sizeof(identifyData));
//Defualt CHS translation
const u16 defHeads = 16;
const u16 defSectors = 63;
constexpr u16 defHeads = 16;
constexpr u16 defSectors = 63;
u64 cylinderslong = std::min<u64>(nbSectors, 16514064) / defHeads / defSectors;
const u16 defCylinders = (u16)std::min<u64>(cylinderslong, UINT16_MAX);
@ -94,7 +94,7 @@ void ATA::CreateHDDinfo(u64 sizeSectors)
//Default Num of heads (Retired)
WriteUInt16(identifyData, &index, defHeads); //word 3
//Number of unformatted bytes per track (Retired)
WriteUInt16(identifyData, &index, (u16)(sectorSize * defSectors)); //word 4
WriteUInt16(identifyData, &index, static_cast<u16>(sectorSize * defSectors)); //word 4
//Number of unformatted bytes per sector (Retired)
WriteUInt16(identifyData, &index, sectorSize); //word 5
//Default Number of sectors per track (Retired)
@ -133,7 +133,7 @@ void ATA::CreateHDDinfo(u64 sizeSectors)
//Capabilities (0-Shall be set to one to indicate a device specific Standby timer value minimum)
index += 1 * 2; //word 50
//PIO data transfer cycle timing mode (Obsolete)
WriteUInt16(identifyData, &index, (u8)((pioMode > 2 ? pioMode : 2) << 8)); //word 51
WriteUInt16(identifyData, &index, static_cast<u8>((pioMode > 2 ? pioMode : 2) << 8)); //word 51
//DMA data transfer cycle timing mode (Obsolete)
WriteUInt16(identifyData, &index, 0); //word 52
//
@ -150,23 +150,23 @@ void ATA::CreateHDDinfo(u64 sizeSectors)
//Number of current sectors per track
WriteUInt16(identifyData, &index, curSectors); //word 56
//Current capacity in sectors
WriteUInt32(identifyData, &index, (u32)curOldsize); //word 57-58
WriteUInt32(identifyData, &index, static_cast<u32>(curOldsize)); //word 57-58
//PIO READ/WRITE Multiple setting
/*
* bit 7-0: Current setting for number of logical sectors that shall be transferred per DRQ
* data block on READ/WRITE Multiple commands
* bit 8: Multiple sector setting is valid
*/
WriteUInt16(identifyData, &index, (u16)(curMultipleSectorsSetting | (1 << 8))); //word 59
WriteUInt16(identifyData, &index, static_cast<u16>(curMultipleSectorsSetting | (1 << 8))); //word 59
//Total number of user addressable logical sectors
WriteUInt32(identifyData, &index, (u32)(nbSectors < 268435456 ? nbSectors : 268435456)); //word 60-61
WriteUInt32(identifyData, &index, static_cast<u32>(nbSectors < 268435456 ? nbSectors : 268435456)); //word 60-61
//DMA modes
/*
* bits 0-7: Singleword modes supported (0,1,2)
* bits 8-15: Transfer mode active
*/
if (sdmaMode > 0)
WriteUInt16(identifyData, &index, (u16)(0x07 | (1 << (sdmaMode + 8)))); //word 62
WriteUInt16(identifyData, &index, static_cast<u16>(0x07 | (1 << (sdmaMode + 8)))); //word 62
else
WriteUInt16(identifyData, &index, 0x07); //word 62
//DMA Modes
@ -175,7 +175,7 @@ void ATA::CreateHDDinfo(u64 sizeSectors)
* bits 8-15: Transfer mode active
*/
if (mdmaMode > 0)
WriteUInt16(identifyData, &index, (u16)(0x07 | (1 << (mdmaMode + 8)))); //word 63
WriteUInt16(identifyData, &index, static_cast<u16>(0x07 | (1 << (mdmaMode + 8)))); //word 63
else
WriteUInt16(identifyData, &index, 0x07); //word 63
//Bit 0-7-PIO modes supported (0,1,2,3,4)
@ -220,7 +220,7 @@ void ATA::CreateHDDinfo(u64 sizeSectors)
* bit 14: NOP
* bit 15: (Obsolete)
*/
WriteUInt16(identifyData, &index, (u16)((1 << 14) | (1 << 5) | /*(1 << 1) | (1 << 10) |*/ 1)); //word 82
WriteUInt16(identifyData, &index, static_cast<u16>((1 << 14) | (1 << 5) | /*(1 << 1) | (1 << 10) |*/ 1)); //word 82
//Supported Feature Sets (83)
/*
* bit 0: DOWNLOAD MICROCODE
@ -239,7 +239,7 @@ void ATA::CreateHDDinfo(u64 sizeSectors)
* bit 13: FLUSH CACHE EXT
* bit 14: 1
*/
WriteUInt16(identifyData, &index, (u16)((1 << 14) | (1 << 13) | (1 << 12) /*| (1 << 8)*/ | ((lba48Supported ? 1 : 0) << 10))); //word 83
WriteUInt16(identifyData, &index, static_cast<u16>((1 << 14) | (1 << 13) | (1 << 12) /*| (1 << 8)*/ | ((lba48Supported ? 1 : 0) << 10))); //word 83
//Supported Feature Sets (84)
/*
* bit 0: Smart error logging
@ -256,19 +256,19 @@ void ATA::CreateHDDinfo(u64 sizeSectors)
* bit 13: IDLE IMMEDIATE with UNLOAD FEATURE
* bit 14: 1
*/
WriteUInt16(identifyData, &index, (u16)((1 << 14) | (1 << 1) | 1)); //word 84
WriteUInt16(identifyData, &index, static_cast<u16>((1 << 14) | (1 << 1) | 1)); //word 84
//Command set/feature enabled/supported (See word 82)
WriteUInt16(identifyData, &index, (u16)((fetSmartEnabled << 0) | (fetSecurityEnabled << 1) | (fetWriteCacheEnabled << 5) | (fetHostProtectedAreaEnabled << 10) | (1 << 14))); //Fixed //word 85
WriteUInt16(identifyData, &index, static_cast<u16>((fetSmartEnabled << 0) | (fetSecurityEnabled << 1) | (fetWriteCacheEnabled << 5) | (fetHostProtectedAreaEnabled << 10) | (1 << 14))); //Fixed //word 85
//Command set/feature enabled/supported (See word 83)
// clang-format off
WriteUInt16(identifyData, &index, (u16)(
WriteUInt16(identifyData, &index, static_cast<u16>(
/*(1 << 8) | //SET MAX */
((lba48Supported ? 1 : 0) << 10) | //Fixed
(1 << 12) | //Fixed
(1 << 13))); //Fixed //word 86
//Command set/feature enabled/supported (See word 84)
WriteUInt16(identifyData, &index, (u16)((1 << 14) | (1 << 1) | 1));
WriteUInt16(identifyData, &index, (u16)(
WriteUInt16(identifyData, &index, static_cast<u16>((1 << 14) | (1 << 1) | 1));
WriteUInt16(identifyData, &index, static_cast<u16>(
(1) | //Fixed
((1) << 1))); //Fixed //word 87
// clang-format on
@ -278,7 +278,7 @@ void ATA::CreateHDDinfo(u64 sizeSectors)
* bits 8-15: Transfer mode active
*/
if (udmaMode > 0)
WriteUInt16(identifyData, &index, (u16)(0x7f | (1 << (udmaMode + 8)))); //word 88
WriteUInt16(identifyData, &index, static_cast<u16>(0x7f | (1 << (udmaMode + 8)))); //word 88
else
WriteUInt16(identifyData, &index, 0x7f); //word 88
//Time required for security erase unit completion
@ -306,7 +306,7 @@ void ATA::CreateHDDinfo(u64 sizeSectors)
* bit 14: 1
*/
index = 93 * 2;
WriteUInt16(identifyData, &index, (u16)(1 | (1 << 14) | 0x2000)); //word 93
WriteUInt16(identifyData, &index, static_cast<u16>(1 | (1 << 14) | 0x2000)); //word 93
//Vendors recommended acoustic management value.
//94
//Stream Minimum Request Size
@ -319,7 +319,7 @@ void ATA::CreateHDDinfo(u64 sizeSectors)
//98-99
//Total Number of User Addressable Sectors for the 48-bit Address feature set.
index = 100 * 2;
WriteUInt64(identifyData, &index, (u16)nbSectors);
WriteUInt64(identifyData, &index, static_cast<u16>(nbSectors));
index -= 2;
WriteUInt16(identifyData, &index, 0); //truncate to 48bits
//Streaming Transfer Time - PIO
@ -334,7 +334,7 @@ void ATA::CreateHDDinfo(u64 sizeSectors)
* bit 14: 1
*/
index = 106 * 2;
WriteUInt16(identifyData, &index, (u16)((1 << 14) | 0));
WriteUInt16(identifyData, &index, static_cast<u16>((1 << 14) | 0));
//Inter-seek delay for ISO-7779acoustic testing in microseconds
//107
//WNN
@ -384,7 +384,7 @@ void ATA::CreateHDDinfoCsum() //Is this correct?
counter += 0xA5;
identifyData[510] = 0xA5;
identifyData[511] = (u8)(255 - counter + 1);
identifyData[511] = static_cast<u8>(255 - counter + 1);
counter = 0;
for (int i = 0; i < (512); i++)

View File

@ -1,5 +1,5 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2020 PCSX2 Dev Team
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
@ -381,35 +381,35 @@ void ATA::Write16(u32 addr, u16 value)
//DevCon.WriteLn("DEV9: *ATA_R_FEATURE 16bit write at address %x, value %x", addr, value);
ClearHOB();
regFeatureHOB = regFeature;
regFeature = (u8)value;
regFeature = static_cast<u8>(value);
break;
case ATA_R_NSECTOR:
//DevCon.WriteLn("DEV9: *ATA_R_NSECTOR 16bit write at address %x, value %x", addr, value);
ClearHOB();
regNsectorHOB = regNsector;
regNsector = (u8)value;
regNsector = static_cast<u8>(value);
break;
case ATA_R_SECTOR:
//DevCon.WriteLn("DEV9: *ATA_R_SECTOR 16bit write at address %x, value %x", addr, value);
ClearHOB();
regSectorHOB = regSector;
regSector = (u8)value;
regSector = static_cast<u8>(value);
break;
case ATA_R_LCYL:
//DevCon.WriteLn("DEV9: *ATA_R_LCYL 16bit write at address %x, value %x", addr, value);
ClearHOB();
regLcylHOB = regLcyl;
regLcyl = (u8)value;
regLcyl = static_cast<u8>(value);
break;
case ATA_R_HCYL:
//DevCon.WriteLn("DEV9: *ATA_R_HCYL 16bit write at address %x, value %x", addr, value);
ClearHOB();
regHcylHOB = regHcyl;
regHcyl = (u8)value;
regHcyl = static_cast<u8>(value);
break;
case ATA_R_SELECT:
//DevCon.WriteLn("DEV9: *ATA_R_SELECT 16bit write at address %x, value %x", addr, value);
regSelect = (u8)value;
regSelect = static_cast<u8>(value);
//bus->ifs[0].select = (val & ~0x10) | 0xa0;
//bus->ifs[1].select = (val | 0x10) | 0xa0;
break;
@ -502,18 +502,18 @@ s64 ATA::HDD_GetLBA()
}
else
{
return ((s64)regHcylHOB << 40) |
((s64)regLcylHOB << 32) |
((s64)regSectorHOB << 24) |
((s64)regHcyl << 16) |
((s64)regLcyl << 8) |
return (static_cast<s64>(regHcylHOB) << 40) |
(static_cast<s64>(regLcylHOB) << 32) |
(static_cast<s64>(regSectorHOB) << 24) |
(static_cast<s64>(regHcyl) << 16) |
(static_cast<s64>(regLcyl) << 8) |
regSector;
}
}
else
{
regStatus |= (u8)ATA_STAT_ERR;
regError |= (u8)ATA_ERR_ABORT;
regStatus |= static_cast<u8>(ATA_STAT_ERR);
regError |= static_cast<u8>(ATA_ERR_ABORT);
Console.Error("DEV9: ATA: Tried to get LBA address while LBA mode disabled");
//(c.Nh + h).Ns+(s-1)
@ -528,19 +528,19 @@ void ATA::HDD_SetLBA(s64 sectorNum)
{
if (!lba48)
{
regSelect = (u8)((regSelect & 0xf0) | (int)((sectorNum >> 24) & 0x0f));
regHcyl = (u8)(sectorNum >> 16);
regLcyl = (u8)(sectorNum >> 8);
regSector = (u8)(sectorNum);
regSelect = static_cast<u8>((regSelect & 0xf0) | static_cast<int>((sectorNum >> 24) & 0x0f));
regHcyl = static_cast<u8>(sectorNum >> 16);
regLcyl = static_cast<u8>(sectorNum >> 8);
regSector = static_cast<u8>(sectorNum);
}
else
{
regSector = (u8)sectorNum;
regLcyl = (u8)(sectorNum >> 8);
regHcyl = (u8)(sectorNum >> 16);
regSectorHOB = (u8)(sectorNum >> 24);
regLcylHOB = (u8)(sectorNum >> 32);
regHcylHOB = (u8)(sectorNum >> 40);
regSector = static_cast<u8>(sectorNum);
regLcyl = static_cast<u8>(sectorNum >> 8);
regHcyl = static_cast<u8>(sectorNum >> 16);
regSectorHOB = static_cast<u8>(sectorNum >> 24);
regLcylHOB = static_cast<u8>(sectorNum >> 32);
regHcylHOB = static_cast<u8>(sectorNum >> 40);
}
}
else
@ -560,35 +560,27 @@ bool ATA::HDD_CanSeek()
bool ATA::HDD_CanAccess(int* sectors)
{
s64 lba;
s64 posStart;
s64 posEnd;
s64 maxLBA;
maxLBA = std::min<s64>(EmuConfig.DEV9.HddSizeSectors, hddImageSize / 512) - 1;
s64 maxLBA = std::min<s64>(EmuConfig.DEV9.HddSizeSectors, hddImageSize / 512) - 1;
if ((regSelect & 0x40) == 0) //CHS mode
maxLBA = std::min<s64>(maxLBA, curCylinders * curHeads * curSectors);
lba = HDD_GetLBA();
if (lba == -1)
const s64 posStart = HDD_GetLBA();
if (posStart == -1)
return false;
//DevCon.WriteLn("DEV9: LBA :%i", lba);
posStart = lba;
if (posStart > maxLBA)
{
*sectors = -1;
return false;
}
posEnd = posStart + *sectors;
const s64 posEnd = posStart + *sectors;
if (posEnd > maxLBA)
{
const s64 overshoot = posEnd - maxLBA;
s64 space = *sectors - overshoot;
*sectors = (int)space;
*sectors = static_cast<int>(space);
return false;
}

View File

@ -1,5 +1,5 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2020 PCSX2 Dev Team
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
@ -105,7 +105,7 @@ bool ATA::IO_Write()
return false;
}
u64 imagePos = entry.sector * 512;
const u64 imagePos = entry.sector * 512;
if (FileSystem::FSeek64(hddImage, imagePos, SEEK_SET) != 0)
{
Console.Error("DEV9: ATA: File seek error");
@ -209,12 +209,13 @@ void ATA::IO_SparseCacheLoad()
memset(&hddSparseBlock[readSize], 0, hddSparseBlockSize - readSize);
}
// Store file pointer.
const s64 orgPos = FileSystem::FTell64(hddImage);
// Flush so that we know what is allocated.
std::fflush(hddImage);
// Store file pointer.
const s64 orgPos = FileSystem::FTell64(hddImage);
pxAssert(orgPos != -1);
#ifdef _WIN32
// FlushFileBuffers is required, hddSparseBlock differs from actual file without it.
FlushFileBuffers(hddNativeHandle);
@ -234,11 +235,6 @@ void ATA::IO_SparseCacheLoad()
memset(hddSparseBlock.get(), 0, hddSparseBlockSize);
hddSparseBlockValid = true;
#if defined(PCSX2_DEBUG) || defined(PCSX2_DEVBUILD)
// Store file pointer.
const s64 orgPos = FileSystem::FTell64(hddImage);
pxAssert(orgPos != -1);
//Load into check buffer.
FileSystem::FSeek64(hddImage, HddSparseStart, SEEK_SET);
@ -281,11 +277,6 @@ void ATA::IO_SparseCacheLoad()
memset(hddSparseBlock.get(), 0, hddSparseBlockSize);
hddSparseBlockValid = true;
#if defined(PCSX2_DEBUG) || defined(PCSX2_DEVBUILD)
// Store file pointer.
const s64 orgPos = FileSystem::FTell64(hddImage);
pxAssert(orgPos != -1);
// Load into check buffer.
FileSystem::FSeek64(hddImage, HddSparseStart, SEEK_SET);
@ -512,8 +503,8 @@ bool ATA::HDD_CanAssessOrSetError()
if (!HDD_CanAccess(&nsector))
{
//Read what we can
regStatus |= (u8)ATA_STAT_ERR;
regError |= (u8)ATA_ERR_ID;
regStatus |= static_cast<u8>(ATA_STAT_ERR);
regError |= static_cast<u8>(ATA_ERR_ID);
if (nsector == -1)
{
PostCmdNoData();

View File

@ -1,5 +1,5 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2020 PCSX2 Dev Team
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
@ -53,7 +53,7 @@ void ATA::HDD_InitDevParameters()
DevCon.WriteLn("DEV9: HDD_InitDevParameters");
curSectors = regNsector;
curHeads = (u8)((regSelect & 0x7) + 1);
curHeads = static_cast<u8>((regSelect & 0x7) + 1);
PostCmdNoData();
}
@ -106,7 +106,7 @@ void ATA::HDD_SetFeatures()
return;
case 0x03: //Set transfer mode
{
const u16 xferMode = (u16)regNsector; //Set Transfer mode
const u16 xferMode = static_cast<u16>(regNsector); //Set Transfer mode
const int mode = xferMode & 0x07;
switch ((xferMode) >> 3)

View File

@ -1,5 +1,5 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2020 PCSX2 Dev Team
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
@ -42,7 +42,7 @@ void HddCreate::Start()
void HddCreate::WriteImage(std::string hddPath, u64 fileBytes, u64 zeroSizeBytes)
{
constexpr int buffsize = 4 * 1024;
u8 buff[buffsize] = {0}; // 4kb.
const u8 buff[buffsize] = {0}; // 4kb.
if (FileSystem::FileExists(hddPath.c_str()))
{