Exemple #1
0
        protected byte[] ReadBytesAt(ushort address, ushort numberOfBytes, bool timing)
        {
            if (!_initialised)
            {
                throw new MemoryNotInitialisedException();
            }

            uint availableBytes = numberOfBytes;

            if (address + availableBytes >= SizeInBytes)
            {
                availableBytes = SizeInBytes - address;                                          // if this read overflows the end of memory, we can read only this many bytes
            }
            IMemorySegment segment = _map.SegmentFor(address);

            if (segment == null)
            {
                return(new byte[numberOfBytes]);                 // if memory is not allocated return all 0x00s
            }
            if (!timing && segment.SizeInBytes - AddressOffset(address, segment) >= numberOfBytes)
            {
                // if the read fits entirely within the memory segment and we aren't generating read timing, then optimise for speed
                return(segment.ReadBytesAt(AddressOffset(address, segment), numberOfBytes));
            }
            else
            {
                byte[] bytes = new byte[numberOfBytes];
                for (int i = 0; i < availableBytes; i++)
                {
                    bytes[i] = ReadByteAt((ushort)(address + i), timing);
                }
                return(bytes); // bytes beyond the available byte limit (if any) will be 0x00
            }
        }