diff --git a/Sim/BasicTypes.h b/Sim/BasicTypes.h index 669da35..77be258 100644 --- a/Sim/BasicTypes.h +++ b/Sim/BasicTypes.h @@ -8,4 +8,14 @@ using U8 = std::uint_fast8_t; using U16 = std::uint_fast16_t; using U32 = std::uint_fast32_t; +struct tSectorCount +{ + U32 _; +}; + +struct tSectorOffset +{ + U32 _; +}; + #endif \ No newline at end of file diff --git a/Sim/Buffer/Hal/BufferHal.cpp b/Sim/Buffer/Hal/BufferHal.cpp index 1b21300..76f332f 100644 --- a/Sim/Buffer/Hal/BufferHal.cpp +++ b/Sim/Buffer/Hal/BufferHal.cpp @@ -62,14 +62,18 @@ U8* BufferHal::ToPointer(const Buffer &buffer) return nullptr; } -void BufferHal::Memcpy(U8* const dest, const Buffer &src) +void BufferHal::Memcpy(U8* const dest, const Buffer &src, const tSectorOffset& bufferOffset, const tSectorCount& sectorCount) { - memcpy(dest, ToPointer(src), src.SizeInByte); + auto byteOffset = ToByteIndexInTransfer(src.Type, bufferOffset._); + auto byteCount = ToByteIndexInTransfer(src.Type, sectorCount._); + memcpy(dest, ToPointer(src) + byteOffset, byteCount); } -void BufferHal::Memcpy(const Buffer &dest, const U8* const src) +void BufferHal::Memcpy(const Buffer &dest, const tSectorOffset& bufferOffset, const U8* const src, const tSectorCount& sectorCount) { - memcpy(ToPointer(dest), src, dest.SizeInByte); + auto byteOffset = ToByteIndexInTransfer(dest.Type, bufferOffset._); + auto byteCount = ToByteIndexInTransfer(dest.Type, sectorCount._); + memcpy(ToPointer(dest) + byteOffset, src, byteCount); } bool BufferHal::SetSectorInfo(const SectorInfo §orInfo) @@ -87,9 +91,9 @@ SectorInfo BufferHal::GetSectorInfo() const return _SectorInfo; } -U32 BufferHal::ToByteIndexInTransfer(BufferType type, const U32 §orIndex) +U32 BufferHal::ToByteIndexInTransfer(BufferType type, U32 offset) { return (type == BufferType::User && _SectorInfo.CompactMode) - ? sectorIndex * _SectorInfo.CompactSizeInByte - : sectorIndex << _SectorInfo.SectorSizeInBit; + ? offset * _SectorInfo.CompactSizeInByte + : offset << _SectorInfo.SectorSizeInBit; } \ No newline at end of file diff --git a/Sim/Buffer/Hal/BufferHal.h b/Sim/Buffer/Hal/BufferHal.h index 0427b72..fa139fa 100644 --- a/Sim/Buffer/Hal/BufferHal.h +++ b/Sim/Buffer/Hal/BufferHal.h @@ -1,43 +1,43 @@ -#ifndef __BufferHal_h__ -#define __BufferHal_h__ - -#include +#ifndef __BufferHal_h__ +#define __BufferHal_h__ + +#include #include - -#include "BasicTypes.h" + +#include "BasicTypes.h" #include "Buffer/Types.h" -constexpr SectorInfo DefaultSectorInfo({ 9, false, 9 }); - -class BufferHal -{ -public: -public: - BufferHal(); - - void PreInit(const U32 &maxBufferSizeInKB); - -public: - bool AllocateBuffer(BufferType type, const U32 §orCount, Buffer &buffer); - void DeallocateBuffer(const Buffer &buffer); - - U8* ToPointer(const Buffer &buffer); - void Memcpy(U8* const dest, const Buffer &src); - void Memcpy(const Buffer &dest, const U8* const src); - -public: - bool SetSectorInfo(const SectorInfo §orInfo); - SectorInfo GetSectorInfo() const; - U32 ToByteIndexInTransfer(BufferType type, const U32 §orIndex); - -private: - U32 _MaxBufferSizeInSector; - U32 _CurrentFreeSizeInSector; - U32 _CurrentBufferHandle; - std::unique_ptr>> _AllocatedBuffers; +constexpr SectorInfo DefaultSectorInfo({ 9, false, 9 }); + +class BufferHal +{ +public: +public: + BufferHal(); + + void PreInit(const U32 &maxBufferSizeInKB); + +public: + bool AllocateBuffer(BufferType type, const U32 §orCount, Buffer &buffer); + void DeallocateBuffer(const Buffer &buffer); + + U8* ToPointer(const Buffer &buffer); + void Memcpy(U8* const dest, const Buffer &src, const tSectorOffset& bufferOffset, const tSectorCount& sectorCount); + void Memcpy(const Buffer &dest, const tSectorOffset& bufferOffset, const U8* const src, const tSectorCount& sectorCount); + +public: + bool SetSectorInfo(const SectorInfo §orInfo); + SectorInfo GetSectorInfo() const; + U32 ToByteIndexInTransfer(BufferType type, U32 offset); + +private: + U32 _MaxBufferSizeInSector; + U32 _CurrentFreeSizeInSector; + U32 _CurrentBufferHandle; + std::unique_ptr>> _AllocatedBuffers; SectorInfo _SectorInfo; boost::interprocess::interprocess_mutex _Mutex; -}; - +}; + #endif \ No newline at end of file diff --git a/Sim/HostComm/BasicTypes.h b/Sim/HostComm/BasicTypes.h index 4c93008..9210f49 100644 --- a/Sim/HostComm/BasicTypes.h +++ b/Sim/HostComm/BasicTypes.h @@ -7,4 +7,14 @@ using U8 = std::uint_fast8_t; using U16 = std::uint_fast16_t; using U32 = std::uint_fast32_t; +struct tSectorCount +{ + U32 _; +}; + +struct tSectorOffset +{ + U32 _; +}; + #endif \ No newline at end of file diff --git a/Sim/HostComm/CustomProtocol/CustomProtocolHal.cpp b/Sim/HostComm/CustomProtocol/CustomProtocolHal.cpp index 041830b..5e85130 100644 --- a/Sim/HostComm/CustomProtocol/CustomProtocolHal.cpp +++ b/Sim/HostComm/CustomProtocol/CustomProtocolHal.cpp @@ -46,12 +46,12 @@ void CustomProtocolHal::QueueCommand(const TransferCommandDesc& command) _TransferCommandQueue->push(command); } -U8* CustomProtocolHal::GetBuffer(CustomProtocolCommand *command, const U32 §orIndex) +U8* CustomProtocolHal::GetBuffer(CustomProtocolCommand *command, const tSectorOffset& offset) { Message* msg = _MessageServer->GetMessage(command->CommandId); if (msg) { - U32 bufferIndex = _BufferHal->ToByteIndexInTransfer(BufferType::User, sectorIndex); + U32 bufferIndex = _BufferHal->ToByteIndexInTransfer(BufferType::User, offset._); assert(msg->PayloadSize > bufferIndex); return ((U8*)msg->Payload) + bufferIndex; } @@ -70,14 +70,14 @@ void CustomProtocolHal::Run() void CustomProtocolHal::ProcessTransferCommand() { TransferCommandDesc& command = _TransferCommandQueue->front(); - U8 *buffer = GetBuffer(command.Command, command.SectorIndex); + U8 *buffer = GetBuffer(command.Command, command.CommandOffset); if (command.Direction == TransferCommandDesc::Direction::In) { - _BufferHal->Memcpy(command.Buffer, buffer); + _BufferHal->Memcpy(command.Buffer, command.BufferOffset, buffer, command.SectorCount); } else { - _BufferHal->Memcpy(buffer, command.Buffer); + _BufferHal->Memcpy(buffer, command.Buffer, command.BufferOffset, command.SectorCount); } assert(command.Listener != nullptr); diff --git a/Sim/HostComm/CustomProtocol/CustomProtocolHal.h b/Sim/HostComm/CustomProtocol/CustomProtocolHal.h index 4957a61..a69f1b4 100644 --- a/Sim/HostComm/CustomProtocol/CustomProtocolHal.h +++ b/Sim/HostComm/CustomProtocol/CustomProtocolHal.h @@ -39,10 +39,12 @@ class CustomProtocolHal : public FrameworkThread }; CustomProtocolCommand *Command; - U32 SectorIndex; + tSectorOffset CommandOffset; + tSectorCount SectorCount; Direction Direction; Buffer Buffer; + tSectorOffset BufferOffset; NandHal::NandAddress NandAddress; TransferCommandListener *Listener; }; @@ -54,7 +56,7 @@ class CustomProtocolHal : public FrameworkThread virtual void Run() override; private: - U8* GetBuffer(CustomProtocolCommand *command, const U32 §orIndex); + U8* GetBuffer(CustomProtocolCommand *command, const tSectorOffset& offset); void ProcessTransferCommand(); private: diff --git a/Sim/Nand/Hal/NandHal.cpp b/Sim/Nand/Hal/NandHal.cpp index 8bde7f5..6f5d44f 100644 --- a/Sim/Nand/Hal/NandHal.cpp +++ b/Sim/Nand/Hal/NandHal.cpp @@ -50,9 +50,10 @@ bool NandHal::ReadPage(const tChannel& channel, const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, - const Buffer &outBuffer) + const Buffer &outBuffer, + const tSectorOffset& bufferOffset) { - return (_NandChannels[channel._][device._].ReadPage(block, page, sector, sectorCount, outBuffer)); + return (_NandChannels[channel._][device._].ReadPage(block, page, sector, sectorCount, outBuffer, bufferOffset)); } void NandHal::WritePage(tChannel channel, tDeviceInChannel device, tBlockInDevice block, tPageInBlock page, const Buffer &inBuffer) @@ -66,9 +67,10 @@ void NandHal::WritePage(const tChannel& channel, const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, - const Buffer &inBuffer) + const Buffer &inBuffer, + const tSectorOffset& bufferOffset) { - _NandChannels[channel._][device._].WritePage(block, page, sector, sectorCount, inBuffer); + _NandChannels[channel._][device._].WritePage(block, page, sector, sectorCount, inBuffer, bufferOffset); } void NandHal::EraseBlock(tChannel channel, tDeviceInChannel device, tBlockInDevice block) @@ -113,7 +115,7 @@ void NandHal::ProcessNandOperation() }break; case CommandDesc::Op::ReadPartial: { - if (false == ReadPage(address.Channel, address.Device, address.Block, address.Page, address.Sector, address.SectorCount, command.Buffer)) + if (false == ReadPage(address.Channel, address.Device, address.Block, address.Page, address.Sector, address.SectorCount, command.Buffer, command.BufferOffset)) { command.CommandStatus = CommandDesc::Status::Uecc; } @@ -121,7 +123,7 @@ void NandHal::ProcessNandOperation() case CommandDesc::Op::WritePartial: { // TODO: Update command status - WritePage(address.Channel, address.Device, address.Block, address.Page, address.Sector, address.SectorCount, command.Buffer); + WritePage(address.Channel, address.Device, address.Block, address.Page, address.Sector, address.SectorCount, command.Buffer, command.BufferOffset); }break; } diff --git a/Sim/Nand/Hal/NandHal.h b/Sim/Nand/Hal/NandHal.h index 4f308c6..6093624 100644 --- a/Sim/Nand/Hal/NandHal.h +++ b/Sim/Nand/Hal/NandHal.h @@ -78,8 +78,9 @@ class NandHal : public FrameworkThread Op Operation; Status CommandStatus; Buffer Buffer; + tSectorOffset BufferOffset; - U32 DescSectorIndex; + tSectorOffset DescSectorIndex; CommandListener *Listener; }; @@ -95,7 +96,8 @@ class NandHal : public FrameworkThread const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, - const Buffer &outBuffer); + const Buffer &outBuffer, + const tSectorOffset& bufferOffset); void WritePage(tChannel channel, tDeviceInChannel device, tBlockInDevice block, tPageInBlock page, const Buffer &inBuffer); void WritePage( @@ -105,7 +107,8 @@ class NandHal : public FrameworkThread const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, - const Buffer &inBuffer); + const Buffer &inBuffer, + const tSectorOffset& bufferOffset); void EraseBlock(tChannel channel, tDeviceInChannel chip, tBlockInDevice block); diff --git a/Sim/Nand/Sim/NandBlock.cpp b/Sim/Nand/Sim/NandBlock.cpp index a4e1e71..623c1fa 100644 --- a/Sim/Nand/Sim/NandBlock.cpp +++ b/Sim/Nand/Sim/NandBlock.cpp @@ -26,11 +26,16 @@ void NandBlock::WritePage(tPageInBlock page, const Buffer &inBuffer) _Buffer = std::unique_ptr(new U8[_PagesPerBlock * _TotalBytesPerPage]); } - _BufferHal->Memcpy((U8*)&_Buffer[page._ * _TotalBytesPerPage], inBuffer); + auto sectorsPerPage = _TotalBytesPerPage >> _BufferHal->GetSectorInfo().SectorSizeInBit; + tSectorCount sectorCount; + sectorCount._ = sectorsPerPage; + tSectorOffset sectorOffset; + sectorOffset._ = 0; + _BufferHal->Memcpy((U8*)&_Buffer[page._ * _TotalBytesPerPage], inBuffer, sectorOffset, sectorCount); _NandBlockTracker.WritePage(page); } -void NandBlock::WritePage(const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &inBuffer) +void NandBlock::WritePage(const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &inBuffer, const tSectorOffset& bufferOffset) { assert(sector._ >= 0); assert(_BufferHal->ToByteIndexInTransfer(inBuffer.Type, sector._ + sectorCount._) <= _TotalBytesPerPage); @@ -40,7 +45,7 @@ void NandBlock::WritePage(const tPageInBlock& page, const tSectorInPage& sector, _Buffer = std::unique_ptr(new U8[_PagesPerBlock * _TotalBytesPerPage]); } - _BufferHal->Memcpy((U8*)&_Buffer[page._ * _TotalBytesPerPage + _BufferHal->ToByteIndexInTransfer(inBuffer.Type, sector._)], inBuffer); + _BufferHal->Memcpy((U8*)&_Buffer[page._ * _TotalBytesPerPage + _BufferHal->ToByteIndexInTransfer(inBuffer.Type, sector._)], inBuffer, bufferOffset, sectorCount); _NandBlockTracker.WritePage(page); } @@ -54,11 +59,16 @@ bool NandBlock::ReadPage(tPageInBlock page, const Buffer &outBuffer) auto pData = (nullptr == _Buffer) ? &_ErasedBuffer[0] : &_Buffer[page._ * _TotalBytesPerPage]; - _BufferHal->Memcpy(outBuffer, pData); + auto sectorsPerPage = _TotalBytesPerPage >> _BufferHal->GetSectorInfo().SectorSizeInBit; + tSectorCount sectorCount; + sectorCount._ = sectorsPerPage; + tSectorOffset sectorOffset; + sectorOffset._ = 0; + _BufferHal->Memcpy(outBuffer, sectorOffset, pData, sectorCount); return (true); } -bool NandBlock::ReadPage(const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &outBuffer) +bool NandBlock::ReadPage(const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &outBuffer, const tSectorOffset& bufferOffset) { // If page is corrupted then return false to indicate ReadPage failed if (true == _NandBlockTracker.IsPageCorrupted(page)) @@ -71,6 +81,6 @@ bool NandBlock::ReadPage(const tPageInBlock& page, const tSectorInPage& sector, auto data = (nullptr == _Buffer) ? &_ErasedBuffer[0] : &_Buffer[(page._ * _TotalBytesPerPage) + _BufferHal->ToByteIndexInTransfer(outBuffer.Type, sector._)]; - _BufferHal->Memcpy(outBuffer, data); + _BufferHal->Memcpy(outBuffer, bufferOffset, data, sectorCount); return (true); } \ No newline at end of file diff --git a/Sim/Nand/Sim/NandBlock.h b/Sim/Nand/Sim/NandBlock.h index 0d15ac4..97b3679 100644 --- a/Sim/Nand/Sim/NandBlock.h +++ b/Sim/Nand/Sim/NandBlock.h @@ -20,10 +20,10 @@ class NandBlock void Erase(); void WritePage(tPageInBlock page, const Buffer &inBuffer); - void WritePage(const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &inBuffer); + void WritePage(const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &inBuffer, const tSectorOffset& bufferOffset); bool ReadPage(tPageInBlock page, const Buffer &outBuffer); - bool ReadPage(const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &outBuffer); + bool ReadPage(const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &outBuffer, const tSectorOffset& bufferOffset); public: static const U8 ERASED_PATTERN = 0xff; diff --git a/Sim/Nand/Sim/NandDevice.cpp b/Sim/Nand/Sim/NandDevice.cpp index d664d31..fa9ad29 100644 --- a/Sim/Nand/Sim/NandDevice.cpp +++ b/Sim/Nand/Sim/NandDevice.cpp @@ -14,9 +14,9 @@ bool NandDevice::ReadPage(tBlockInDevice block, tPageInBlock page, const Buffer return (_Blocks[block._].ReadPage(page, outBuffer)); } -bool NandDevice::ReadPage(const tBlockInDevice& block, const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &outBuffer) +bool NandDevice::ReadPage(const tBlockInDevice& block, const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &outBuffer, const tSectorOffset& bufferOffset) { - return (_Blocks[block._].ReadPage(page, sector, sectorCount, outBuffer)); + return (_Blocks[block._].ReadPage(page, sector, sectorCount, outBuffer, bufferOffset)); } void NandDevice::WritePage(tBlockInDevice block, tPageInBlock page, const Buffer &inBuffer) @@ -24,9 +24,9 @@ void NandDevice::WritePage(tBlockInDevice block, tPageInBlock page, const Buffer _Blocks[block._].WritePage(page, inBuffer); } -void NandDevice::WritePage(const tBlockInDevice& block, const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &inBuffer) +void NandDevice::WritePage(const tBlockInDevice& block, const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &inBuffer, const tSectorOffset& bufferOffset) { - _Blocks[block._].WritePage(page, sector, sectorCount, inBuffer); + _Blocks[block._].WritePage(page, sector, sectorCount, inBuffer, bufferOffset); } void NandDevice::EraseBlock(tBlockInDevice block) diff --git a/Sim/Nand/Sim/NandDevice.h b/Sim/Nand/Sim/NandDevice.h index 2c95269..16d1f1d 100644 --- a/Sim/Nand/Sim/NandDevice.h +++ b/Sim/Nand/Sim/NandDevice.h @@ -18,10 +18,12 @@ class NandDevice public: bool ReadPage(tBlockInDevice block, tPageInBlock page, const Buffer &outBuffer); - bool ReadPage(const tBlockInDevice& block, const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &outBuffer); + bool ReadPage(const tBlockInDevice& block, const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, + const Buffer &outBuffer, const tSectorOffset& bufferOffset); void WritePage(tBlockInDevice block, tPageInBlock page, const Buffer &inBuffer); - void WritePage(const tBlockInDevice& block, const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, const Buffer &inBuffer); + void WritePage(const tBlockInDevice& block, const tPageInBlock& page, const tSectorInPage& sector, const tSectorCount& sectorCount, + const Buffer &inBuffer, const tSectorOffset& bufferOffset); void EraseBlock(tBlockInDevice block); diff --git a/Sim/Nand/Types.h b/Sim/Nand/Types.h index 2e2ee83..d5e7e3f 100644 --- a/Sim/Nand/Types.h +++ b/Sim/Nand/Types.h @@ -28,9 +28,4 @@ struct tSectorInPage U8 _; }; -struct tSectorCount -{ - U8 _; -}; - #endif \ No newline at end of file diff --git a/Sim/SimpleFtl/SimpleFtl.cpp b/Sim/SimpleFtl/SimpleFtl.cpp index b36cad5..8e016f3 100644 --- a/Sim/SimpleFtl/SimpleFtl.cpp +++ b/Sim/SimpleFtl/SimpleFtl.cpp @@ -36,6 +36,9 @@ bool SimpleFtl::SetSectorInfo(const SectorInfo §orInfo) _TotalSectors = geometry.ChannelCount * geometry.DevicesPerChannel * geometry.BlocksPerDevice * geometry.PagesPerBlock * _SectorsPerPage; SimpleFtlTranslation::SetSectorSize(sectorInfo.SectorSizeInBit); + + _SectorsPerSegment = _SectorsPerPage; + return true; } @@ -154,9 +157,11 @@ void SimpleFtl::ReadNextLbas() while (_RemainingSectorCount > 0) { SimpleFtlTranslation::LbaToNandAddress(_CurrentLba, _RemainingSectorCount, nandAddress, nextLba, remainingSectorCount); - if (_BufferHal->AllocateBuffer(BufferType::User, nandAddress.SectorCount._, buffer)) + if (_BufferHal->AllocateBuffer(BufferType::User, _SectorsPerSegment, buffer)) { - ReadPage(nandAddress, buffer, _ProcessedSectorCount); + tSectorOffset sectorOffset; + sectorOffset._ = _ProcessedSectorCount; + ReadPage(nandAddress, buffer, sectorOffset); _ProcessedSectorCount += nandAddress.SectorCount._; _CurrentLba = nextLba; _RemainingSectorCount = remainingSectorCount; @@ -169,19 +174,21 @@ void SimpleFtl::ReadNextLbas() } } -void SimpleFtl::TransferOut(const Buffer &buffer, const NandHal::NandAddress &nandAddress, const U32 §orIndex) +void SimpleFtl::TransferOut(const Buffer &buffer, const NandHal::NandAddress &nandAddress, const tSectorOffset& commandOffset, const tSectorCount& sectorCount) { CustomProtocolHal::TransferCommandDesc transferCommand; transferCommand.Buffer = buffer; + transferCommand.BufferOffset._ = nandAddress.Sector._; //NOTE: if NAND sector and buffer sector ever differ, need a conversion transferCommand.Command = _ProcessingCommand; transferCommand.Direction = CustomProtocolHal::TransferCommandDesc::Direction::Out; - transferCommand.SectorIndex = sectorIndex; + transferCommand.CommandOffset = commandOffset; + transferCommand.SectorCount = sectorCount; transferCommand.NandAddress = nandAddress; transferCommand.Listener = this; _CustomProtocolHal->QueueCommand(transferCommand); } -void SimpleFtl::ReadPage(const NandHal::NandAddress &nandAddress, const Buffer &outBuffer, const U32 &descSectorIndex) +void SimpleFtl::ReadPage(const NandHal::NandAddress &nandAddress, const Buffer &outBuffer, const tSectorOffset& descSectorIndex) { assert((nandAddress.Sector._ + nandAddress.SectorCount._) <= _SectorsPerPage); @@ -190,6 +197,7 @@ void SimpleFtl::ReadPage(const NandHal::NandAddress &nandAddress, const Buffer & commandDesc.Operation = (nandAddress.SectorCount._ == _SectorsPerPage) ? NandHal::CommandDesc::Op::Read : NandHal::CommandDesc::Op::ReadPartial; commandDesc.Buffer = outBuffer; + commandDesc.BufferOffset._ = nandAddress.Sector._; //NOTE: if NAND sector and buffer sector ever differ, need a conversion commandDesc.DescSectorIndex = descSectorIndex; commandDesc.Listener = this; @@ -205,9 +213,11 @@ void SimpleFtl::WriteNextLbas() while (_RemainingSectorCount > 0) { SimpleFtlTranslation::LbaToNandAddress(_CurrentLba, _RemainingSectorCount, nandAddress, nextLba, remainingSectorCount); - if (_BufferHal->AllocateBuffer(BufferType::User, nandAddress.SectorCount._, buffer)) + if (_BufferHal->AllocateBuffer(BufferType::User, _SectorsPerSegment, buffer)) { - TransferIn(buffer, nandAddress, _ProcessedSectorCount); + tSectorOffset commandOffset; + commandOffset._ = _ProcessedSectorCount; + TransferIn(buffer, nandAddress, commandOffset, nandAddress.SectorCount); _ProcessedSectorCount += nandAddress.SectorCount._; _CurrentLba = nextLba; _RemainingSectorCount = remainingSectorCount; @@ -220,13 +230,15 @@ void SimpleFtl::WriteNextLbas() } } -void SimpleFtl::TransferIn(const Buffer &buffer, const NandHal::NandAddress &nandAddress, const U32 §orIndex) +void SimpleFtl::TransferIn(const Buffer &buffer, const NandHal::NandAddress &nandAddress, const tSectorOffset& commandOffset, const tSectorCount& sectorCount) { CustomProtocolHal::TransferCommandDesc transferCommand; transferCommand.Buffer = buffer; + transferCommand.BufferOffset._ = nandAddress.Sector._; //NOTE: if NAND sector and buffer sector ever differ, need a conversion transferCommand.Command = _ProcessingCommand; transferCommand.Direction = CustomProtocolHal::TransferCommandDesc::Direction::In; - transferCommand.SectorIndex = sectorIndex; + transferCommand.CommandOffset = commandOffset; + transferCommand.SectorCount = sectorCount; transferCommand.NandAddress = nandAddress; transferCommand.Listener = this; _CustomProtocolHal->QueueCommand(transferCommand); @@ -241,6 +253,7 @@ void SimpleFtl::WritePage(const NandHal::NandAddress &nandAddress, const Buffer commandDesc.Operation = (nandAddress.SectorCount._ == _SectorsPerPage) ? NandHal::CommandDesc::Op::Write : NandHal::CommandDesc::Op::WritePartial; commandDesc.Buffer = inBuffer; + commandDesc.BufferOffset._ = nandAddress.Sector._; //NOTE: if NAND sector and buffer sector ever differ, need a conversion commandDesc.Listener = this; _NandHal->QueueCommand(commandDesc); @@ -272,7 +285,7 @@ void SimpleFtl::OnNandCommandCompleted(const NandHal::CommandDesc &command) { if (CustomProtocolCommand::Code::Read == _ProcessingCommand->Command) { - TransferOut(command.Buffer, command.Address, command.DescSectorIndex); + TransferOut(command.Buffer, command.Address, command.DescSectorIndex, command.Address.SectorCount); if (NandHal::CommandDesc::Status::Success != command.CommandStatus) { diff --git a/Sim/SimpleFtl/SimpleFtl.h b/Sim/SimpleFtl/SimpleFtl.h index de79831..09f7bca 100644 --- a/Sim/SimpleFtl/SimpleFtl.h +++ b/Sim/SimpleFtl/SimpleFtl.h @@ -51,25 +51,14 @@ class SimpleFtl : public CustomProtocolHal::TransferCommandListener, public Nand bool IsProcessingCommand(); -private: - struct tSectorOffset - { - U8 _; - }; - - struct tSectorCount - { - U8 _; - }; - private: void ProcessEvent(); void ReadNextLbas(); - void TransferOut(const Buffer &buffer, const NandHal::NandAddress &nandAddress, const U32 §orIndex); - void ReadPage(const NandHal::NandAddress &nandAddress, const Buffer &outBuffer, const U32 &descSectorIndex); + void TransferOut(const Buffer &buffer, const NandHal::NandAddress &nandAddress, const tSectorOffset& commandOffset, const tSectorCount& sectorCount); + void ReadPage(const NandHal::NandAddress &nandAddress, const Buffer &outBuffer, const tSectorOffset& descSectorIndex); void WriteNextLbas(); - void TransferIn(const Buffer &buffer, const NandHal::NandAddress &nandAddress, const U32 §orIndex); + void TransferIn(const Buffer &buffer, const NandHal::NandAddress &nandAddress, const tSectorOffset& commandOffset, const tSectorCount& sectorCount); void WritePage(const NandHal::NandAddress &nandAddress, const Buffer &outBuffer); bool SetSectorInfo(const SectorInfo §orInfo); @@ -93,6 +82,8 @@ class SimpleFtl : public CustomProtocolHal::TransferCommandListener, public Nand U32 _CurrentLba; U32 _PendingCommandCount; + U8 _SectorsPerSegment; + bip::interprocess_mutex *_Mutex; std::unique_ptr> _EventQueue; diff --git a/Sim/Test/Buffer.cpp b/Sim/Test/Buffer.cpp index 1edc625..ad55504 100644 --- a/Sim/Test/Buffer.cpp +++ b/Sim/Test/Buffer.cpp @@ -22,11 +22,15 @@ TEST(BufferHal, Basic) { memset(&tempBuffer[i * 512], i, 512); } - bufferHal.Memcpy(buffer, tempBuffer); + tSectorCount sectorCount; + sectorCount._ = buffer.SizeInSector; + tSectorOffset sectorOffset; + sectorOffset._ = 0; + bufferHal.Memcpy(buffer, sectorOffset, tempBuffer, sectorCount); ASSERT_EQ(memcmp(bufferHal.ToPointer(buffer), tempBuffer, requestingBufferSizeInByte), 0); U8 tempBuffer1[requestingBufferSizeInByte]; - bufferHal.Memcpy(tempBuffer1, buffer); + bufferHal.Memcpy(tempBuffer1, buffer, sectorOffset, sectorCount); ASSERT_EQ(memcmp(tempBuffer, tempBuffer1, requestingBufferSizeInByte), 0); ASSERT_NO_THROW(bufferHal.DeallocateBuffer(buffer)); diff --git a/Sim/Test/SimpleFtl.cpp b/Sim/Test/SimpleFtl.cpp index 3015ef5..aebd165 100644 --- a/Sim/Test/SimpleFtl.cpp +++ b/Sim/Test/SimpleFtl.cpp @@ -296,7 +296,7 @@ TEST_F(SimpleFtlTest, BasicWriteReadVerify) //Write a buffer with lba and sector count constexpr U32 lba = 12345; - U32 sectorCount = 35; + U32 sectorCount = 25; U32 payloadSize = sectorCount * SectorSizeInTransfer; auto writeMessage = AllocateMessage(CustomProtocolClient, payloadSize, true); ASSERT_NE(writeMessage, nullptr); @@ -327,7 +327,20 @@ TEST_F(SimpleFtlTest, BasicWriteReadVerify) //Get message read buffer to verify with the write buffer int compareResult = std::memcmp(responseMessageWrite->Payload, responseMessageRead->Payload, payloadSize); - ASSERT_EQ(0, compareResult); + if (compareResult != 0) + { + for (auto i(0); i < sectorCount; ++i) + { + auto writeBuffer = &(static_cast(responseMessageWrite->Payload)[i * SectorSizeInTransfer]); + auto readBuffer = &(static_cast(responseMessageRead->Payload)[i * SectorSizeInTransfer]); + auto result = std::memcmp(writeBuffer, readBuffer, SectorSizeInTransfer); + if (result != 0) + { + GOUT("Miscompare at sector " << i); + } + } + FAIL(); + } //--Deallocate CustomProtocolClient->DeallocateMessage(responseMessageWrite);