8#include "helpers/formatstring.h"
21 const char* message =
"";
26 message =
"Operation succeeded";
30 message =
"Memory block is already open and cannot be opened again";
34 message =
"Memory block is closed and cannot be accessed";
38 message =
"The given name is not a recognized memory block name";
42 message =
"The handle argument does not correspond to a valid open memory block";
46 message =
"The given offset is out of bounds";
50 message =
"The block name pointer or return handle pointer argument is NULL";
54 message =
"Unrecognized error code.";
70 std::string _ConstructErrorMessage(
DDR_ErrorCode errorCode,
const char* operation)
91 const char* blockName = NULL;
119 : _initialized(false)
123 if (blockName != NULL)
128 int memorySizeInChunks = 0;
133 _ConstructErrorMessage(errorCode,
134 "Memory block not opened so cannot retrieve memory block size");
143 _ConstructErrorMessage(errorCode,
"Initializing data reader/writer");
177 "Data reader/writer is not initialized. Unable to read.");
184 int chunkOffset = byteOffset / (
sizeof(uint32_t));
186 uint32_t bufferIndex = 0;
191 int byteOffsetInChunk = byteOffset % (
sizeof(uint32_t));
192 while (bufferIndex < maxBytes)
194 data[bufferIndex] = value & 0xff;
198 if (byteOffsetInChunk ==
sizeof(uint32_t))
205 byteOffsetInChunk = 0;
209 std::string msg = _ConstructErrorMessage(errorCode,
"Reading memory");
216 if (bufferIndex > 0) {
217 if (
static_cast<uint32_t
>(bufferIndex) > maxBytes) {
218 data.resize(bufferIndex);
225 std::string msg = _ConstructErrorMessage(errorCode,
"Reading memory");
241 "Data reader/writer is not initialized. Unable to write.");
246 int chunkOffset = byteOffset /
sizeof(uint32_t);
248 int byteOffsetInChunk = byteOffset %
sizeof(uint32_t);
249 uint32_t bufferIndex = 0;
250 uint32_t byteMask = 0xff << (byteOffsetInChunk * 8);
251 if (byteOffsetInChunk != 0)
257 while (bufferIndex < maxBytes)
260 value |= ((uint32_t)data[bufferIndex]) << (byteOffsetInChunk * 8);
264 if (byteOffsetInChunk ==
sizeof(uint32_t))
270 byteOffsetInChunk = 0;
284 std::string msg = _ConstructErrorMessage(errorCode,
"Writing memory");
291 if (byteOffsetInChunk != 0)
298 std::string msg = _ConstructErrorMessage(errorCode,
"Writing memory");
304 std::string msg = _ConstructErrorMessage(errorCode,
"Reading memory in preparation to writing memory");
314 uint32_t maxBytes,
int indent)
317 std::string indentSpaces(indent,
' ');
321 size_t byteCount = maxBytes;
322 if (byteCount > data.size())
324 byteCount = data.size();
326 uint32_t bytesPerRow = 32;
327 for (uint32_t row = 0; row < maxBytes; row += bytesPerRow)
330 for (uint32_t col = 0;
331 col < bytesPerRow && (row + col) < maxBytes;
338 size_t dataIndex =
static_cast<size_t>(row) + col;
const char * BLOCK_NAME_0
Name of the first block.
DDR_ErrorCode DDR_SetDataChunk(int dataHandle, int chunkOffset, uint32_t value)
Writes a single 32-bit value to the given offset in the memory block indicated by the specified handl...
const char * BLOCK_NAME_2
Name of the third block.
DDR_ErrorCode DDR_GetDataChunk(int dataHandle, int chunkOffset, uint32_t *value)
Read a single 32-bit value at the given offset in the memory block indicated by the specified handle.
DDR_ErrorCode DDR_OpenMemoryBlock(const char *blockName, int *dataHandle)
Open access to a memory block for exclusive use, given the name of the memory block.
DDR_ErrorCode DDR_CloseMemoryBlock(int dataHandle)
Close access to a previously opened memory block, thus releasing it for others to open.
const char * BLOCK_NAME_1
Name of the second block.
DDR_ErrorCode DDR_GetMemorySize(int dataHandle, int *memorySizeInChunks)
Retrieve the number of chunks in the memory block indicated by the handle to the successfully opened ...
Declaration of the "low-level" memory block read/write functions that are provided in a separate DLL ...
DDR_ErrorCode
Represents the possible errors that can be returned from the memory block access functions.
@ DDR_ErrorCode_Block_Not_Opened
Memory block is closed and cannot be accessed.
@ DDR_ErrorCode_Block_Already_Opened
Memory block is already open and cannot be opened again.
@ DDR_ErrorCode_Null_Argument
The block name pointer or return handle pointer argument is NULL.
@ DDR_ErrorCode_Invalid_Block_Name
The given name is not a recognized memory block name.
@ DDR_ErrorCode_Invalid_Handle
The handle argument does not correspond to a valid open memory block.
@ DDR_ErrorCode_Success
Operation succeeded.
@ DDR_ErrorCode_Invalid_Offset
The given offset is out of bounds.
@ DDR_MAX_OFFSET
All offsets must from 0 to 1 less than this value.
Declaration of the DataReaderWriter class used in the Adapter Pattern.
static const char * _GetErrorMessage(DDR_ErrorCode errorCode)
Convert the given error code to a string message.
Represents an error that occurred when reading or writing data in the Data reader/writer.
uint32_t GetMemoryBlockByteSize()
Retrieve the size of the currently opened memory block in bytes.
std::string BufferToString(const ByteArray &data, uint32_t maxBytes, int indent)
Convert the specified data up to the specified number of bytes into a string by performing a "hex dum...
uint32_t _memoryBlockByteSize
void Write(int byteOffset, const ByteArray &data, uint32_t maxBytes)
Write a specified number of bytes.
const char * _GetBlockNameForBlockNumber(DataReaderWriter::MemoryBlockNumber blockNumber)
Given a block number, retrieve the corresponding block name.
ByteArray Read(int byteOffset, uint32_t maxBytes)
Read a specified number of bytes.
~DataReaderWriter()
Destructor.
MemoryBlockNumber
Represents the memory blocks that can be accessed. Hides how memory blocks are actually identified.
@ Memory_Block_0
First block.
@ Memory_Block_1
Second block.
@ Memory_Block_2
Third block.
DataReaderWriter(MemoryBlockNumber blockNumber)
Constructor.
Represents an error that occurred during initialization or shut down of the Data reader/writer.
The namespace containing all Design Pattern Examples implemented in C++.
std::vector< uint8_t > ByteArray
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....