From c77d8b3709d9748890feecb011d8dfd72496018a Mon Sep 17 00:00:00 2001 From: lightningterror <18107717+lightningterror@users.noreply.github.com> Date: Fri, 16 Jun 2023 20:36:38 +0200 Subject: [PATCH] DEV9: Cleanup ATA. --- pcsx2/DEV9/ATA/ATA_Info.cpp | 46 ++++++++-------- pcsx2/DEV9/ATA/ATA_State.cpp | 66 ++++++++++------------- pcsx2/DEV9/ATA/ATA_Transfer.cpp | 25 +++------ pcsx2/DEV9/ATA/Commands/ATA_CmdNoData.cpp | 6 +-- pcsx2/DEV9/ATA/HddCreate.cpp | 4 +- 5 files changed, 65 insertions(+), 82 deletions(-) diff --git a/pcsx2/DEV9/ATA/ATA_Info.cpp b/pcsx2/DEV9/ATA/ATA_Info.cpp index 7ed61149b4..d28c4d6a08 100644 --- a/pcsx2/DEV9/ATA/ATA_Info.cpp +++ b/pcsx2/DEV9/ATA/ATA_Info.cpp @@ -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(nbSectors, 16514064) / defHeads / defSectors; const u16 defCylinders = (u16)std::min(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(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((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(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(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(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(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(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((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((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((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((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( /*(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((1 << 14) | (1 << 1) | 1)); + WriteUInt16(identifyData, &index, static_cast( (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(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(1 | (1 << 14) | 0x2000)); //word 93 //Vendor’s 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(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((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(255 - counter + 1); counter = 0; for (int i = 0; i < (512); i++) diff --git a/pcsx2/DEV9/ATA/ATA_State.cpp b/pcsx2/DEV9/ATA/ATA_State.cpp index 3bfaecea7b..3021a15e06 100644 --- a/pcsx2/DEV9/ATA/ATA_State.cpp +++ b/pcsx2/DEV9/ATA/ATA_State.cpp @@ -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(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(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(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(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(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(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(regHcylHOB) << 40) | + (static_cast(regLcylHOB) << 32) | + (static_cast(regSectorHOB) << 24) | + (static_cast(regHcyl) << 16) | + (static_cast(regLcyl) << 8) | regSector; } } else { - regStatus |= (u8)ATA_STAT_ERR; - regError |= (u8)ATA_ERR_ABORT; + regStatus |= static_cast(ATA_STAT_ERR); + regError |= static_cast(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((regSelect & 0xf0) | static_cast((sectorNum >> 24) & 0x0f)); + regHcyl = static_cast(sectorNum >> 16); + regLcyl = static_cast(sectorNum >> 8); + regSector = static_cast(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(sectorNum); + regLcyl = static_cast(sectorNum >> 8); + regHcyl = static_cast(sectorNum >> 16); + regSectorHOB = static_cast(sectorNum >> 24); + regLcylHOB = static_cast(sectorNum >> 32); + regHcylHOB = static_cast(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(EmuConfig.DEV9.HddSizeSectors, hddImageSize / 512) - 1; + s64 maxLBA = std::min(EmuConfig.DEV9.HddSizeSectors, hddImageSize / 512) - 1; if ((regSelect & 0x40) == 0) //CHS mode maxLBA = std::min(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(space); return false; } diff --git a/pcsx2/DEV9/ATA/ATA_Transfer.cpp b/pcsx2/DEV9/ATA/ATA_Transfer.cpp index 6805fbff51..185d4730af 100644 --- a/pcsx2/DEV9/ATA/ATA_Transfer.cpp +++ b/pcsx2/DEV9/ATA/ATA_Transfer.cpp @@ -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(ATA_STAT_ERR); + regError |= static_cast(ATA_ERR_ID); if (nsector == -1) { PostCmdNoData(); diff --git a/pcsx2/DEV9/ATA/Commands/ATA_CmdNoData.cpp b/pcsx2/DEV9/ATA/Commands/ATA_CmdNoData.cpp index 680625d9fe..417088f860 100644 --- a/pcsx2/DEV9/ATA/Commands/ATA_CmdNoData.cpp +++ b/pcsx2/DEV9/ATA/Commands/ATA_CmdNoData.cpp @@ -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((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(regNsector); //Set Transfer mode const int mode = xferMode & 0x07; switch ((xferMode) >> 3) diff --git a/pcsx2/DEV9/ATA/HddCreate.cpp b/pcsx2/DEV9/ATA/HddCreate.cpp index bb23d31d40..4855d9eab3 100644 --- a/pcsx2/DEV9/ATA/HddCreate.cpp +++ b/pcsx2/DEV9/ATA/HddCreate.cpp @@ -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())) {