Skip to content

Commit

Permalink
Supported buffer offset for memcpy in and out of buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-tran committed Mar 30, 2020
1 parent a55c69e commit fc64416
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 106 deletions.
10 changes: 10 additions & 0 deletions Sim/BasicTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 11 additions & 7 deletions Sim/Buffer/Hal/BufferHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &sectorInfo)
Expand All @@ -87,9 +91,9 @@ SectorInfo BufferHal::GetSectorInfo() const
return _SectorInfo;
}

U32 BufferHal::ToByteIndexInTransfer(BufferType type, const U32 &sectorIndex)
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;
}
72 changes: 36 additions & 36 deletions Sim/Buffer/Hal/BufferHal.h
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
#ifndef __BufferHal_h__
#define __BufferHal_h__

#include <map>
#ifndef __BufferHal_h__
#define __BufferHal_h__

#include <map>
#include <boost/interprocess/sync/interprocess_mutex.hpp>

#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 &sectorCount, 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 &sectorInfo);
SectorInfo GetSectorInfo() const;
U32 ToByteIndexInTransfer(BufferType type, const U32 &sectorIndex);

private:
U32 _MaxBufferSizeInSector;
U32 _CurrentFreeSizeInSector;
U32 _CurrentBufferHandle;
std::unique_ptr<std::map<U32, std::unique_ptr<U8[]>>> _AllocatedBuffers;
constexpr SectorInfo DefaultSectorInfo({ 9, false, 9 });

class BufferHal
{
public:
public:
BufferHal();

void PreInit(const U32 &maxBufferSizeInKB);

public:
bool AllocateBuffer(BufferType type, const U32 &sectorCount, 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 &sectorInfo);
SectorInfo GetSectorInfo() const;
U32 ToByteIndexInTransfer(BufferType type, U32 offset);

private:
U32 _MaxBufferSizeInSector;
U32 _CurrentFreeSizeInSector;
U32 _CurrentBufferHandle;
std::unique_ptr<std::map<U32, std::unique_ptr<U8[]>>> _AllocatedBuffers;
SectorInfo _SectorInfo;

boost::interprocess::interprocess_mutex _Mutex;
};

};

#endif
10 changes: 10 additions & 0 deletions Sim/HostComm/BasicTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions Sim/HostComm/CustomProtocol/CustomProtocolHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ void CustomProtocolHal::QueueCommand(const TransferCommandDesc& command)
_TransferCommandQueue->push(command);
}

U8* CustomProtocolHal::GetBuffer(CustomProtocolCommand *command, const U32 &sectorIndex)
U8* CustomProtocolHal::GetBuffer(CustomProtocolCommand *command, const tSectorOffset& offset)
{
Message<CustomProtocolCommand>* 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;
}
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions Sim/HostComm/CustomProtocol/CustomProtocolHal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -54,7 +56,7 @@ class CustomProtocolHal : public FrameworkThread
virtual void Run() override;

private:
U8* GetBuffer(CustomProtocolCommand *command, const U32 &sectorIndex);
U8* GetBuffer(CustomProtocolCommand *command, const tSectorOffset& offset);
void ProcessTransferCommand();

private:
Expand Down
14 changes: 8 additions & 6 deletions Sim/Nand/Hal/NandHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -113,15 +115,15 @@ 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;
}
}break;
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;
}

Expand Down
9 changes: 6 additions & 3 deletions Sim/Nand/Hal/NandHal.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ class NandHal : public FrameworkThread
Op Operation;
Status CommandStatus;
Buffer Buffer;
tSectorOffset BufferOffset;

U32 DescSectorIndex;
tSectorOffset DescSectorIndex;
CommandListener *Listener;
};

Expand All @@ -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(
Expand All @@ -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);

Expand Down
22 changes: 16 additions & 6 deletions Sim/Nand/Sim/NandBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ void NandBlock::WritePage(tPageInBlock page, const Buffer &inBuffer)
_Buffer = std::unique_ptr<U8[]>(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);
Expand All @@ -40,7 +45,7 @@ void NandBlock::WritePage(const tPageInBlock& page, const tSectorInPage& sector,
_Buffer = std::unique_ptr<U8[]>(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);
}

Expand All @@ -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))
Expand All @@ -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);
}
4 changes: 2 additions & 2 deletions Sim/Nand/Sim/NandBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions Sim/Nand/Sim/NandDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ 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)
{
_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)
Expand Down
6 changes: 4 additions & 2 deletions Sim/Nand/Sim/NandDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 0 additions & 5 deletions Sim/Nand/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,4 @@ struct tSectorInPage
U8 _;
};

struct tSectorCount
{
U8 _;
};

#endif
Loading

0 comments on commit fc64416

Please sign in to comment.