6from ctypes
import sizeof, c_uint32
11from .adapter_backendfunctions
import *
72 if errorCode == DDR_ErrorCode.DDR_ErrorCode_Success:
75 if errorCode == DDR_ErrorCode.DDR_ErrorCode_Success:
103 "Memory block not opened so cannot retrieve memory block size")
113 block_name =
"unknown"
115 if block_number == MemoryBlock.MEMORY_BLOCK_0:
116 block_name = BLOCK_NAME_0
117 elif block_number == MemoryBlock.MEMORY_BLOCK_1:
118 block_name = BLOCK_NAME_1
119 elif block_number == MemoryBlock.MEMORY_BLOCK_2:
120 block_name = BLOCK_NAME_2
136 case DDR_ErrorCode.DDR_ErrorCode_Success:
137 message =
"Operation succeeded";
139 case DDR_ErrorCode.DDR_ErrorCode_Block_Already_Opened:
140 message =
"Memory block is already open and cannot be opened again";
142 case DDR_ErrorCode.DDR_ErrorCode_Block_Not_Opened:
143 message =
"Memory block is closed and cannot be accessed";
145 case DDR_ErrorCode.DDR_ErrorCode_Invalid_Block_Name:
146 message =
"The given name is not a recognized memory block name";
148 case DDR_ErrorCode.DDR_ErrorCode_Invalid_Handle:
149 message =
"The handle argument does not correspond to a valid open memory block";
151 case DDR_ErrorCode.DDR_ErrorCode_Invalid_Offset:
152 message =
"The given offset is out of bounds";
154 case DDR_ErrorCode.DDR_ErrorCode_Null_Argument:
155 message =
"The block name pointer or return handle pointer argument is NULL";
158 message = f
"Unrecognized error code: {errorCode}"
177 return "{0}: {1}".format(operation, msg)
192 def Read(self, byteOffset: int, maxBytes : int) -> bytearray:
196 return_data = bytearray()
198 chunkOffset = byteOffset // sizeof(c_uint32)
202 if errorCode == DDR_ErrorCode.DDR_ErrorCode_Success:
203 byteOffsetInChunk = byteOffset % sizeof(c_uint32)
204 while bufferIndex < maxBytes:
205 return_data.append(value.value & 0xff)
208 byteOffsetInChunk += 1
209 if byteOffsetInChunk == sizeof(c_uint32):
211 if chunkOffset >= DDR_MAX_OFFSET:
213 byteOffsetInChunk = 0
215 if errorCode != DDR_ErrorCode.DDR_ErrorCode_Success:
236 def Write(self, byteOffset: int, data : bytearray, maxBytes: int) ->
None:
240 errorCode = DDR_ErrorCode.DDR_ErrorCode_Success
241 chunkOffset = byteOffset // sizeof(c_uint32)
243 byteOffsetInChunk = int(byteOffset % sizeof(c_uint32))
245 byteMask = 0xff << (byteOffsetInChunk * 8)
246 if byteOffsetInChunk != 0:
248 if errorCode == DDR_ErrorCode.DDR_ErrorCode_Success:
249 while bufferIndex < maxBytes:
250 value.value = value.value & (byteMask ^ 0xffffffff)
251 value.value = value.value | (data[bufferIndex] << (byteOffsetInChunk * 8))
254 byteOffsetInChunk += 1
255 if byteOffsetInChunk == sizeof(c_uint32):
257 if errorCode == DDR_ErrorCode.DDR_ErrorCode_Success:
259 byteOffsetInChunk = 0
261 if chunkOffset >= DDR_MAX_OFFSET:
264 if errorCode != DDR_ErrorCode.DDR_ErrorCode_Success:
270 if errorCode == DDR_ErrorCode.DDR_ErrorCode_Success:
271 if byteOffsetInChunk != 0:
274 if errorCode != DDR_ErrorCode.DDR_ErrorCode_Success:
297 indent_spaces =
' ' * indent
301 if byteCount > len(data):
302 byteCount = len(data)
304 for row
in range(0, maxBytes, bytesPerRow):
305 output.write(
"{}{:04x} --".format(indent_spaces, row))
306 for col
in range(0, bytesPerRow):
307 if (col + row) >= maxBytes:
311 dataIndex = row + col
312 output.write(
"{:02x}".format(data[dataIndex]))
315 return output.getvalue()
Represents an opaque token or handle to data.
Represents a value that can be passed into or out of a function.
Represents an error that occurred when reading or writing data in the Data reader/writer.
Represents a data reader/writer to a caller.
_memoryBlockByteSize
Holds the number of bytes in the opened memory block.
str _GetErrorMessage(self, DDR_ErrorCode errorCode)
Convert the given error code to a string message.
bytearray Read(self, int byteOffset, int maxBytes)
Read a specified number of bytes.
str BufferToString(self, bytearray data, int maxBytes, int indent)
Convert the specified data up to the specified number of bytes into a string by performing a "hex dum...
def __enter__(self)
Entry function used in the with statement to initialize an instance of the reader/writer.
str _ConstructErrorMessage(self, int errorCode, str operation)
Creates a formatted error message from the given operation, using the given error code returned from ...
def __init__(self, MemoryBlock blockNumber)
Constructor.
_blockNumber
The number of the memory block to use.
None Write(self, int byteOffset, bytearray data, int maxBytes)
Write a specified number of bytes.
def MemoryBlockByteSize(self)
Retrieve the size of the memory block in bytes.
def _GetBlockNameForBlockNumber(self, MemoryBlock block_number)
Given a value from the MemoryBlock enumeration that specifies the number of the block,...
_initialized
True if this instance has been initialized to use the specified memory block (by a call to enter())
def __exit__(self, *args)
Exit function automatically called when used in the with statement.
_dataHandle
Contains the handle to the memory block after enter() has returned.
Represents an error that occurred during initialization or shut down of the Data reader/writer.
Represents the memory blocks that can be accessed.
DDR_ErrorCode ddr_getdatachunk(Handle dataHandle, int chunkOffset, ValueHandle value)
Read a single 32-bit value at the given offset in the memory block indicated by the specified handle.
DDR_ErrorCode ddr_closememoryblock(Handle dataHandle)
Close access to a memory block previously opened by ddr_openmemoryblock(), thus releasing it for othe...
DDR_ErrorCode ddr_getmemorysize(Handle dataHandle, ValueHandle memorySizeInChunks)
Retrieve the number of chunks in the memory block indicated by the handle to the successfully opened ...
DDR_ErrorCode ddr_setdatachunk(Handle dataHandle, int chunkOffset, int value)
Writes a single 32-bit value to the given offset in the memory block indicated by the specified handl...
DDR_ErrorCode ddr_openmemoryblock(blockName, Handle dataHandle)
Open access to a memory block for exclusive use, given the name of the memory block.