/// <summary> /// Returns a newly allocated Segment in "Real Mode" memory /// </summary> /// <returns></returns> public IntPtr16 AllocateRealModeSegment(ushort segmentSize = ushort.MaxValue) { _currentRealModePointer.Segment++; var realModeSegment = new IntPtr16(_currentRealModePointer); AddSegment(realModeSegment.Segment, segmentSize); return(realModeSegment); }
public MemoryCore() { _variablePointerDictionary = new Dictionary <string, IntPtr16>(); _currentVariablePointer = new IntPtr16(VARIABLE_BASE_SEGMENT, 0); _currentRealModePointer = new IntPtr16(REALMODE_BASE_SEGMENT, 0); _bigMemoryBlocks = new PointerDictionary <Dictionary <ushort, IntPtr16> >(); //Add Segment 0 by default, stack segment AddSegment(0); }
/// <summary> /// Deletes all defined Segments from Memory /// </summary> public void Clear() { Array.Clear(_memorySegments, 0, _memorySegments.Length); Array.Clear(_segments, 0, _segments.Length); Array.Clear(_decompiledSegments, 0, _decompiledSegments.Length); _variablePointerDictionary.Clear(); _currentVariablePointer = new IntPtr16(VARIABLE_BASE_SEGMENT, 0); _currentRealModePointer = new IntPtr16(REALMODE_BASE_SEGMENT, 0); _bigMemoryBlocks.Clear(); }
/// <summary> /// Safe retrieval of a pointer to a defined variable /// /// Returns false if the variable isn't defined /// </summary> /// <param name="name"></param> /// <param name="pointer"></param> /// <returns></returns> public bool TryGetVariablePointer(string name, out IntPtr16 pointer) { if (!_variablePointerDictionary.TryGetValue(name, out var result)) { pointer = null; return(false); } pointer = result; return(true); }
public MemoryCore() { _memorySegments = new Dictionary <ushort, byte[]>(); _segments = new Dictionary <ushort, Segment>(); _decompiledSegments = new Dictionary <ushort, Dictionary <ushort, Instruction> >(); _variablePointerDictionary = new Dictionary <string, IntPtr16>(); _currentVariablePointer = new IntPtr16(VARIABLE_BASE, 0); _bigMemoryBlocks = new PointerDictionary <Dictionary <ushort, IntPtr16> >(); //Add Segment 0 by default, stack segment AddSegment(0); }
/// <summary> /// Allocates the specified variable name with the desired size /// </summary> /// <param name="name"></param> /// <param name="size"></param> /// <param name="declarePointer"></param> /// <returns></returns> public IntPtr16 AllocateVariable(string name, ushort size, bool declarePointer = false) { if (!string.IsNullOrEmpty(name) && _variablePointerDictionary.ContainsKey(name)) { _logger.Warn($"Attmped to re-allocate variable: {name}"); return(_variablePointerDictionary[name]); } //Do we have enough room in the current segment? //If not, declare a new segment and start there if (size + _currentVariablePointer.Offset >= ushort.MaxValue) { _currentVariablePointer.Segment++; _currentVariablePointer.Offset = 0; AddSegment(_currentVariablePointer.Segment); } if (!HasSegment(_currentVariablePointer.Segment)) { AddSegment(_currentVariablePointer.Segment); } #if DEBUG _logger.Debug( $"Variable {name ?? "NULL"} allocated {size} bytes of memory in Host Memory Segment {_currentVariablePointer.Segment:X4}:{_currentVariablePointer.Offset:X4}"); #endif var currentOffset = _currentVariablePointer.Offset; _currentVariablePointer.Offset += (ushort)(size + 1); var newPointer = new IntPtr16(_currentVariablePointer.Segment, currentOffset); if (declarePointer && string.IsNullOrEmpty(name)) { throw new Exception("Unsupported operation, declaring pointer type for NULL named variable"); } if (!string.IsNullOrEmpty(name)) { _variablePointerDictionary[name] = newPointer; if (declarePointer) { var variablePointer = AllocateVariable($"*{name}", 0x4, false); SetArray(variablePointer, newPointer.ToSpan()); } } return(newPointer); }
/// <summary> /// Returns the specified block by index in the desired memory block /// </summary> /// <param name="block"></param> /// <param name="index"></param> /// <returns></returns> public IntPtr16 GetBigMemoryBlock(IntPtr16 block, ushort index) => _bigMemoryBlocks[block.Offset][index];
/// <summary> /// Returns an array containing the cstring stored at the specified pointer /// </summary> /// <param name="pointer"></param> /// <param name="stripNull"></param> /// <returns></returns> public ReadOnlySpan <byte> GetString(IntPtr16 pointer, bool stripNull = false) => GetString(pointer.Segment, pointer.Offset, stripNull);
/// <summary> /// Sets the specified pointer value at the desired segment:offset /// </summary> /// <param name="segment"></param> /// <param name="offset"></param> /// <param name="value"></param> public void SetPointer(ushort segment, ushort offset, IntPtr16 value) => SetArray(segment, offset, value.Data);
/// <summary> /// Zeroes out the memory at the specified pointer for the desired number of bytes /// </summary> /// <param name="pointer"></param> /// <param name="length"></param> public void SetZero(IntPtr16 pointer, int length) { var destinationSpan = new Span <byte>(_memorySegments[pointer.Segment], pointer.Offset, length); destinationSpan.Fill(0); }
/// <summary> /// Sets the specified pointer value at the desired pointer /// </summary> /// <param name="pointer"></param> /// <param name="value"></param> public void SetPointer(IntPtr16 pointer, IntPtr16 value) => SetArray(pointer, value.Data);
/// <summary> /// Sets the specified pointer value at the defined variable /// </summary> /// <param name="variableName"></param> /// <param name="value"></param> public void SetPointer(string variableName, IntPtr16 value) => SetArray(GetVariablePointer(variableName), value.Data);
/// <summary> /// Sets the specified word at the desired pointer /// </summary> /// <param name="pointer"></param> /// <param name="value"></param> public void SetWord(IntPtr16 pointer, ushort value) => SetWord(pointer.Segment, pointer.Offset, value);
/// <summary> /// Sets the specified array at the desired pointer /// </summary> /// <param name="pointer"></param> /// <param name="array"></param> public void SetArray(IntPtr16 pointer, ReadOnlySpan <byte> array) => SetArray(pointer.Segment, pointer.Offset, array);
/// <summary> /// Sets the specified byte at the desired pointer /// </summary> /// <param name="pointer"></param> /// <param name="value"></param> public void SetByte(IntPtr16 pointer, byte value) => SetByte(pointer.Segment, pointer.Offset, value);
/// <summary> /// Returns a single byte from the specified pointer /// </summary> /// <param name="pointer"></param> /// <returns></returns> public byte GetByte(IntPtr16 pointer) => GetByte(pointer.Segment, pointer.Offset);
/// <summary> /// Returns an array with desired count from the specified pointer /// </summary> /// <param name="pointer"></param> /// <param name="count"></param> /// <returns></returns> public ReadOnlySpan <byte> GetArray(IntPtr16 pointer, ushort count) => GetArray(pointer.Segment, pointer.Offset, count);
/// <summary> /// Returns an unsigned word from the specified pointer /// </summary> /// <param name="pointer"></param> /// <returns></returns> public ushort GetWord(IntPtr16 pointer) => GetWord(pointer.Segment, pointer.Offset);
/// <summary> /// Returns a pointer stored at the specified pointer /// </summary> /// <param name="pointer"></param> /// <returns></returns> public IntPtr16 GetPointer(IntPtr16 pointer) => new IntPtr16(GetArray(pointer, 4));