public int GetAllocatedMemorySize(FarPtr ptr) { // ptr should have 0 offset, but we need to reconvert back to segment 0x1000 base. var adjustedPtr = new FarPtr(_heapBaseSegment, (ushort)(ptr.Offset + ((ptr.Segment - _heapBaseSegment) << 4))); return(_memoryAllocator.GetAllocatedMemorySize(adjustedPtr)); }
/// <summary> /// Returns a newly allocated Segment in "Real Mode" memory /// </summary> /// <returns></returns> public FarPtr AllocateRealModeSegment(ushort segmentSize = ushort.MaxValue) { _currentRealModePointer.Segment++; var realModeSegment = new FarPtr(_currentRealModePointer); AddSegment(realModeSegment.Segment, segmentSize); return(realModeSegment); }
public MemoryCore() { _variablePointerDictionary = new Dictionary <string, FarPtr>(); _currentVariablePointer = new FarPtr(VARIABLE_BASE_SEGMENT, 0); _currentRealModePointer = new FarPtr(REALMODE_BASE_SEGMENT, 0); _bigMemoryBlocks = new PointerDictionary <Dictionary <ushort, FarPtr> >(); //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 FarPtr(VARIABLE_BASE_SEGMENT, 0); _currentRealModePointer = new FarPtr(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 FarPtr pointer) { if (!_variablePointerDictionary.TryGetValue(name, out var result)) { pointer = null; return(false); } pointer = result; return(true); }
public override void Free(FarPtr ptr) { if (ptr.IsNull()) { return; } // ptr should have 0 offset, but we need to reconvert back to segment 0x1000 base. var adjustedPtr = new FarPtr(_heapBaseSegment, (ushort)(ptr.Offset + ((ptr.Segment - _heapBaseSegment) << 4))); _memoryAllocator.Free(adjustedPtr); }
/// <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 FarPtr AllocateVariable(string name, ushort size, bool declarePointer = false) { if (!string.IsNullOrEmpty(name) && _variablePointerDictionary.ContainsKey(name)) { _logger.Warn($"Attempted 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 FarPtr(_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.Data); } } return(newPointer); }
/// <summary> /// Writes the specified byte the specified number of times starting at the specified pointer /// </summary> /// <param name="pointer"></param> /// <param name="count"></param> /// <param name="value"></param> public void FillArray(FarPtr pointer, ushort count, byte value) => FillArray(pointer.Segment, pointer.Offset, count, value);
public override FarPtr GetBigMemoryBlock(FarPtr block, ushort index) => throw new NotImplementedException();
public uint GetDWord(FarPtr pointer) => GetWord(pointer.Segment, pointer.Offset);
public FarPtr GetPointer(FarPtr pointer) => new FarPtr(GetArray(pointer, 4));
public void SetZero(FarPtr pointer, int length) { var destinationSpan = new Span <byte>(_memorySegments[pointer.Segment], pointer.Offset, length); destinationSpan.Fill(0); }
public FarPtr GetBigMemoryBlock(FarPtr block, ushort index) => _bigMemoryBlocks[block.Offset][index];
public byte GetByte(FarPtr pointer) => GetByte(pointer.Segment, pointer.Offset);
public void SetPointer(ushort segment, ushort offset, FarPtr value) => SetArray(segment, offset, value.Data);
public virtual bool TryGetVariablePointer(string name, out FarPtr pointer) { throw new NotImplementedException(); }
public virtual int GetAllocatedMemorySize(FarPtr ptr) { throw new NotImplementedException(); }
public void SetArray(FarPtr pointer, ReadOnlySpan <byte> array) => SetArray(pointer.Segment, pointer.Offset, array);
public ReadOnlySpan <byte> GetArray(FarPtr pointer, ushort count) => GetArray(pointer.Segment, pointer.Offset, count);
public void SetDWord(FarPtr pointer, uint value) => SetDWord(pointer.Segment, pointer.Offset, value);
public void SetByte(FarPtr pointer, byte value) => SetByte(pointer.Segment, pointer.Offset, value);
public ReadOnlySpan <byte> GetString(FarPtr pointer, bool stripNull = false) => GetString(pointer.Segment, pointer.Offset, stripNull);
public virtual void Free(FarPtr ptr) { throw new NotImplementedException(); }
public void SetPointer(FarPtr pointer, FarPtr value) => SetArray(pointer, value.Data);
public virtual FarPtr GetBigMemoryBlock(FarPtr block, ushort index) { throw new NotImplementedException(); }
public void SetPointer(string variableName, FarPtr value) => SetArray(GetVariablePointer(variableName), value.Data);