示例#1
0
        private void UpdateCache()
        {
            Instruction instruction;
            ushort      offset, length;
            int         lineCount;
            string      text;
            bool        cacheChanged, debuggable;

            if (memory == null)
            {
                return;
            }

            debuggable = debuggableMemory != null;

            // Do not change the cache by default
            cacheChanged = false;

            // Calculate the desired offset
            offset = (ushort)(-AutoScrollPosition.Y / lineHeight);
            // Calculate the desired cache size
            lineCount = 1 + ClientRectangle.Height / lineHeight;

            // Find the desired offset in the list if possible
            for (int i = 0; i < instructionCache.Count; i++)
            {
                instruction = instructionCache[i];

                if (instruction.Offset == offset)
                {
                    if (i > 0)
                    {
                        instructionCache.RemoveRange(0, i);
                        cacheChanged = true;
                    }
                    break;
                }
                else if (instruction.Offset > offset || i == instructionCache.Count - 1 && instruction.Offset < offset)
                {
                    instructionCache.Clear();
                    cacheChanged = true;
                    break;
                }
            }

            // If the cache is not empty, change the parameters in order to fill it to the desired size
            if (instructionCache.Count > 0)
            {
                instruction = instructionCache[instructionCache.Count - 1];
                offset      = (ushort)(instruction.Offset + instruction.Length);
                lineCount  -= instructionCache.Count;
            }

            if (lineCount > 0)
            {
                cacheChanged = true;
            }

            // Fill the cache to the desired size
            for (int i = 0; i < lineCount; i++)
            {
                text = Utility.Disassemble(memory, offset, DisassembleFlags.Default, out length);
                instructionCache.Add(new Instruction(offset, length, text, debuggable && debuggableMemory.IsBreakpoint(offset)));

                if (length == 0)
                {
                    return;
                }
                offset += length;
            }

            if (cacheChanged)
            {
                Invalidate();
            }
        }