コード例 #1
0
        internal static void SetDisassembly(Sim6502 processor, HostLogger logger)
        {
            if (logger == null || !logger.IsDebugEnabled)
            {
                return;
            }
            EAddressingMode addressMode           = Sim6502Utility.GetAddressingMode(processor.RegisterIR);
            int             currentProgramCounter = processor.RegisterPC;

            currentProgramCounter = processor.WrapProgramCounter(++currentProgramCounter);
            int?address1 = processor.ReadMemoryValueWithoutCycle(currentProgramCounter);

            currentProgramCounter = processor.WrapProgramCounter(++currentProgramCounter);
            int?address2 = processor.ReadMemoryValueWithoutCycle(currentProgramCounter);

            string disassembledStep = string.Empty;

            switch (addressMode)
            {
            case EAddressingMode.Absolute:
                disassembledStep = string.Format("${0}{1}", address2.Value.ToString("X").PadLeft(2, '0'), address1.Value.ToString("X").PadLeft(2, '0'));
                break;

            case EAddressingMode.AbsoluteX:
                disassembledStep = string.Format("${0}{1},X", address2.Value.ToString("X").PadLeft(2, '0'), address1.Value.ToString("X").PadLeft(2, '0'));
                break;

            case EAddressingMode.AbsoluteY:
                disassembledStep = string.Format("${0}{1},Y", address2.Value.ToString("X").PadLeft(2, '0'), address1.Value.ToString("X").PadLeft(2, '0'));
                break;

            case EAddressingMode.Accumulator:
                address1         = null;
                address2         = null;
                disassembledStep = "A";
                break;

            case EAddressingMode.Immediate:
                disassembledStep = string.Format("#${0}", address1.Value.ToString("X").PadLeft(4, '0'));
                address2         = null;
                break;

            case EAddressingMode.Implicit:
                address1 = null;
                address2 = null;
                break;

            case EAddressingMode.Indirect:
                disassembledStep = string.Format("(${0}{1})", address2.Value.ToString("X").PadLeft(2, '0'), address1.Value.ToString("X").PadLeft(2, '0'));
                break;

            case EAddressingMode.IndirectX:
                address2         = null;
                disassembledStep = string.Format("(${0},X)", address1.Value.ToString("X").PadLeft(2, '0'));
                break;

            case EAddressingMode.IndirectY:
                address2         = null;
                disassembledStep = string.Format("(${0}),Y", address1.Value.ToString("X").PadLeft(2, '0'));
                break;

            case EAddressingMode.Relative:
                var valueToMove       = (byte)address1.Value;
                var movement          = valueToMove > 127 ? (valueToMove - 255) : valueToMove;
                var newProgramCounter = processor.RegisterPC + movement;
                //This makes sure that we always land on the correct spot for a positive number
                if (movement >= 0)
                {
                    newProgramCounter++;
                }
                var stringAddress = processor.RegisterPC.ToString("X").PadLeft(4, '0');
                // address1 = int.Parse(stringAddress.Substring(0, 2), NumberStyles.AllowHexSpecifier);
                address2         = null; // int.Parse(stringAddress.Substring(2, 2), NumberStyles.AllowHexSpecifier);
                disassembledStep = string.Format("${0}", newProgramCounter.ToString("X").PadLeft(4, '0'));
                break;

            case EAddressingMode.ZeroPage:
                address2         = null;
                disassembledStep = string.Format("${0}", address1.Value.ToString("X").PadLeft(2, '0'));
                break;

            case EAddressingMode.ZeroPageX:
                address2         = null;
                disassembledStep = string.Format("${0},X", address1.Value.ToString("X").PadLeft(2, '0'));
                break;

            case EAddressingMode.ZeroPageY:
                address2         = null;
                disassembledStep = string.Format("${0},Y", address1.Value.ToString("X").PadLeft(4, '0'));
                break;

            default:
                throw new InvalidEnumArgumentException("Invalid Addressing Mode");
            }

            string opcodeAsString = processor.RegisterIR.ConvertOpCodeIntoString();

            logger.Debug("{0} : {1} {2} {3} {4} {5} A: {6} X: {7} Y: {8} SP {9} N: {10} V: {11} B: {12} D: {13} I: {14} Z: {15} C: {16} $0D={17}",
                         processor.RegisterPC.ToString("X4"),
                         processor.RegisterIR.ToString("X2"),
                         address1.HasValue ? address1.Value.ToString("X").PadLeft(2, '0') : "  ",
                         address2.HasValue ? address2.Value.ToString("X").PadLeft(2, '0') : "  ",

                         opcodeAsString,
                         disassembledStep.PadRight(13 - opcodeAsString.Length, ' '),

                         processor.RegisterA.ToString("X").PadLeft(3, '0'),
                         processor.RegisterX.ToString("X").PadLeft(3, '0'),
                         processor.RegisterY.ToString("X").PadLeft(3, '0'),
                         processor.RegisterSP.ToString("X").PadLeft(3, '0'),
                         Convert.ToInt16(processor.FlagN),
                         Convert.ToInt16(processor.FlagV),
                         0,
                         Convert.ToInt16(processor.FlagD),
                         Convert.ToInt16(processor.FlagI),
                         Convert.ToInt16(processor.FlagZ),
                         Convert.ToInt16(processor.FlagC),
                         processor.ReadMemoryValueWithoutCycle(0x0D).ToString("X"));
        }