Exemplo n.º 1
0
 public HeapPointer(GlobalHeap heap, uint ptr, bool forWrite)
 {
     if (ptr != 0)
     {
         // Get the buffer
         var buf = heap.GetBuffer(ptr, forWrite, out _ofs);
         _pin = GCHandle.Alloc(buf, GCHandleType.Pinned);
     }
 }
Exemplo n.º 2
0
        public ushort GetDosEnvironmentSegment()
        {
            if (_hEnvironment == 0)
            {
                // Build environment strings
                var env = string.Join("\0", Environment.Select(x => x.Key + "\0" + x.Value)) + "\0";

                // Convert to byte array
                var bytes = Encoding.GetEncoding(437).GetBytes(env);

                // Allocate environment string
                _hEnvironment = GlobalHeap.Alloc("DOS Environment Strings", Win16.GMEM_FIXED, (uint)bytes.Length);

                // Write it
                this.WriteBytes(_hEnvironment, 0, bytes);
            }

            return(_hEnvironment);
        }
Exemplo n.º 3
0
        public LocalHeap(GlobalHeap globalHeap, ushort globalHandle, ushort baseOffset, ushort maxSize)
        {
            _globalHeap   = globalHeap;
            _globalHandle = globalHandle;

            // Dont allocate at zero
            if (baseOffset == 0)
            {
                baseOffset = 16;
            }

            // Work out top of heap
            uint topOfHeap = _globalHeap.Size(globalHandle);
            uint heapLimit = baseOffset + (uint)(maxSize == 0 ? 0x10000 : maxSize);

            if (heapLimit > topOfHeap)
            {
                heapLimit = topOfHeap;
            }

            _allocator      = new RangeAllocator <LocalAllocation>((int)heapLimit);
            _freeHandles    = new List <ushort>();
            _handleOrPtrMap = new Dictionary <ushort, LocalAllocation>();

            var reservedPage = _allocator.Alloc(baseOffset, false, false);

            reservedPage.User = new LocalAllocation()
            {
                flags       = Win16.LMEM_FIXED,
                handleOrPtr = 0,
                range       = reservedPage,
            };

            _allocator.MoveListener = this;
            _baseOffset             = baseOffset;
        }
Exemplo n.º 4
0
        public Machine()
        {
            if (!System.Diagnostics.Debugger.IsAttached)
            {
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.UnhandledException += UnhandledException;
            }
            else
            {
                enableDebugger = true;
                logExecution   = true;
            }

            _pathMapper        = new PathMapper(this);
            _globalHeap        = new GlobalHeap(this);
            _stringHeap        = new StringHeap(this);
            _moduleManager     = new ModuleManager(this);
            _messaging         = new Messaging(this);
            _variableResolver  = new VariableResolver();
            _expressionContext = new ExpressionContext(this);
            _symbolResolver    = new SymbolResolver(this);
            _stackWalker       = new StackWalker(this);
            _expressionContext.PushSymbolScope(_symbolResolver);

            this.MemoryBus = _globalHeap;

            RegisterVariables();

            // Create system heaps
            _systemCodeSelector = _globalHeap.Alloc("System Thunks", 0, 0x10000);
            _globalHeap.SetSelectorAttributes(_systemCodeSelector, true, true);
            _systemDataHeap = _globalHeap.CreateLocalHeap("System Local Heap", 0);
            _globalHeap.SetSelectorAttributes(_systemDataHeap.GlobalHandle, false, false);

            // Initialise the system return thunk
            CreateSysRetThunk();

            // Creae DOS Api handler
            _dos = new DosApi(this, this);

            // Load standard modules
            _kernel = _moduleManager.LoadModule(new Kernel()) as Kernel;
            _user   = _moduleManager.LoadModule(new User()) as User;
            _moduleManager.LoadModule(new Gdi());
            _moduleManager.LoadModule(new MMSystem());
            _moduleManager.LoadModule(new Keyboard());
//            _moduleManager.LoadModule(new Shell());
            _moduleManager.LoadModule(new DdeML());
            _moduleManager.LoadModule(new Sound());

            _disassembler = new Disassembler(this);

            this.InstructionHook = () =>
            {
                if (logExecution)
                {
                    _disassembled = null;
                    Log.WriteLine(_variableResolver.ResolveTokenizedString(_logExecutionFormat));
                }
            };
        }