示例#1
0
        private void Test_ReadLineButton_Click(object sender, EventArgs e)
        {
            int currentLine = CodeBox.SelectedIndices[0];

            try
            {
                CurrentCPU = CurrentASM[currentLine].Read(CurrentCPU);
            }
            catch (Exception ex)
            {
                Program.ShowError("Could not properly read selected line.", ex); return;
            }

            Pointer address = new Pointer(CurrentCPU.PC, false, true);

            if (address - 2 == CurrentASM[currentLine].Address)
            {
                CodeBox.Items[currentLine + 1].Selected = true;
                CodeBox.Select();
            }
            else
            {
                Read_ThumbRadioButton.Checked = CurrentCPU.T;
                Read_AddressBox.Value         = address;
                Try : try
                {
                    Core_Dissassemble(
                        Read_AddressBox.Value,
                        (int)Read_LengthBox.Value,
                        Read_ThumbRadioButton.Checked);
                    Core_LoadValues();
                }
                catch
                {
                    Read_LengthBox.Value += 2;
                    goto Try;
                }

                CodeBox.Items[0].Selected = true;
                CodeBox.Select();
            }
            Core_LoadValues();
        }
示例#2
0
        void Core_Dissassemble(Pointer address, int length, bool thumb)
        {
            if (address % 2 == 1)
            {
                address -= 1;
                thumb    = true;
            }

            if (thumb && length % 2 != 0)
            {
                throw new Exception("Thumb machine code is 2 bytes per instruction - length should be a multiple of 2");
            }
            if (!thumb && length % 4 != 0)
            {
                throw new Exception("ARM machine code is 4 bytes per instruction - length should be a multiple of 4");
            }

            CurrentCPU.T  = thumb;
            CurrentCPU.PC = address + Pointer.HardwareOffset;

            byte[] data = Core.ReadData(address, (int)length);

            CurrentASM = thumb ?
                         GBA.ASM.Disassemble_Thumb(data, address) :
                         GBA.ASM.Disassemble_ARM(data, address);

            CodeBox.Items.Clear();
            ListViewItem[] code = new ListViewItem[CurrentASM.Length];
            for (int i = 0; i < CurrentASM.Length; i++)
            {
                code[i] = new ListViewItem(new string[]
                {
                    CurrentASM[i].Address.ToString(),
                    Util.BytesToHex(CurrentASM[i].Data),
                    CurrentASM[i].Code
                });
            }
            CodeBox.Items.AddRange(code);
            CodeBox.Items[0].Selected = true;
            CodeBox.Select();
        }
        public async Task ProcessLine(string line)
        {
            CodeBox.Focus();
            var lineIndex  = WindowBindings.CurrentLine - 2;
            var superIndex = 0;

            for (var i = 0; i < lineIndex + 1; i++)
            {
                if (i == 0)
                {
                    CodeBox.Select(0, _codeLines[i].Length + 2);
                    superIndex += _codeLines[i].Length + 2;
                }
                else
                {
                    CodeBox.Select(superIndex, _codeLines[i].Length + 2);
                    superIndex += _codeLines[i].Length + 2;
                }
            }
            if (line.Length == 0)
            {
                return;
            }
            var commandArgs = line.Split(new[] { ' ' }, 2);

            WindowBindings.CurrentInstruction = commandArgs[0];
            if (commandArgs.Length >= 2)
            {
                WindowBindings.CurrentInstructionData = commandArgs[1];
            }
            WindowBindings.CurrentInstructionBrush = new SolidColorBrush(BlueColor);
            if ((int)DelaySizer.Value > 10)
            {
                await Task.Delay((int)DelaySizer.Value);
            }
            WindowBindings.CurrentInstructionBrush = null;
            var args = new string[] { };

            if (commandArgs.Length >= 2)
            {
                args = commandArgs[1].Replace(" ", "").Split(',');
            }
            WindowBindings.DecoderBrush = new SolidColorBrush(BlueColor);
            if ((int)DelaySizer.Value > 10)
            {
                await Task.Delay((int)DelaySizer.Value);
            }
            WindowBindings.DecoderBrush = null;
            switch (commandArgs[0])
            {
            case "LOD":
                WindowBindings.AluRegister1      = "";
                WindowBindings.AluRegister2      = "";
                WindowBindings.AluRegister1Brush = new SolidColorBrush(BlueColor);
                WindowBindings.AluRegister2Brush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluRegister1Brush = null;
                WindowBindings.AluRegister2Brush = null;
                WindowBindings.AluMethod         = "↓";
                WindowBindings.AluMethodBrush    = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluMethodBrush   = null;
                WindowBindings.Accumulator      = _memoryContainer.GetValue(args[0]);
                WindowBindings.AccumulatorBrush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AccumulatorBrush = null;
                break;

            case "STO":
                WindowBindings.AluRegister1      = "";
                WindowBindings.AluRegister2      = "";
                WindowBindings.AluRegister1Brush = new SolidColorBrush(BlueColor);
                WindowBindings.AluRegister2Brush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluRegister1Brush = null;
                WindowBindings.AluRegister2Brush = null;
                WindowBindings.AluMethod         = "↑";
                WindowBindings.AluMethodBrush    = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluMethodBrush = null;
                _memoryContainer.SetValue(args[0], WindowBindings.Accumulator);
                WindowBindings.AccumulatorBrush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AccumulatorBrush = null;
                break;

            case "ADD":
                WindowBindings.AluRegister1      = WindowBindings.Accumulator.ToString();
                WindowBindings.AluRegister1Brush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluRegister1Brush = null;
                if (args.Length > 0 && args[0][0] == '#')
                {
                    WindowBindings.AluRegister2 = args[0].Remove(0, 1);
                }
                else
                {
                    WindowBindings.AluRegister2 = _memoryContainer.GetValue(args[0]).ToString();
                }
                WindowBindings.AluRegister2Brush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluRegister2Brush = null;
                WindowBindings.AluMethod         = "+";
                WindowBindings.AluMethodBrush    = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluMethodBrush = null;
                if (args.Length > 0 && args[0][0] == '#' && int.TryParse(args[0].Remove(0, 1), out var result))
                {
                    WindowBindings.Accumulator += result;
                }
                else
                {
                    WindowBindings.Accumulator += _memoryContainer.GetValue(args[0]);
                }
                WindowBindings.AccumulatorBrush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AccumulatorBrush = null;
                break;

            case "SUB":
                WindowBindings.AluRegister1      = WindowBindings.Accumulator.ToString();
                WindowBindings.AluRegister1Brush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    if ((int)DelaySizer.Value > 10)
                    {
                        await Task.Delay((int)DelaySizer.Value);
                    }
                }
                WindowBindings.AluRegister1Brush = null;
                if (args.Length > 0 && args[0][0] == '#')
                {
                    WindowBindings.AluRegister2 = args[0].Remove(0, 1);
                }
                else
                {
                    WindowBindings.AluRegister2 = _memoryContainer.GetValue(args[0]).ToString();
                }
                WindowBindings.AluRegister2Brush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluRegister2Brush = null;
                WindowBindings.AluMethod         = "-";
                WindowBindings.AluMethodBrush    = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluMethodBrush = null;
                if (args.Length > 0 && args[0][0] == '#' && int.TryParse(args[0].Remove(0, 1), out var result2))
                {
                    WindowBindings.Accumulator -= result2;
                }
                else
                {
                    WindowBindings.Accumulator -= _memoryContainer.GetValue(args[0]);
                }
                WindowBindings.AccumulatorBrush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AccumulatorBrush = null;
                break;

            case "MUL":
                WindowBindings.AluRegister1      = WindowBindings.Accumulator.ToString();
                WindowBindings.AluRegister1Brush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluRegister1Brush = null;
                if (args.Length > 0 && args[0][0] == '#')
                {
                    WindowBindings.AluRegister2 = args[0].Remove(0, 1);
                }
                else
                {
                    WindowBindings.AluRegister2 = _memoryContainer.GetValue(args[0]).ToString();
                }
                WindowBindings.AluRegister2Brush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluRegister2Brush = null;
                WindowBindings.AluMethod         = "*";
                WindowBindings.AluMethodBrush    = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluMethodBrush = null;
                if (args.Length > 0 && args[0][0] == '#' && int.TryParse(args[0].Remove(0, 1), out var result3))
                {
                    WindowBindings.Accumulator *= result3;
                }
                else
                {
                    WindowBindings.Accumulator *= _memoryContainer.GetValue(args[0]);
                }
                WindowBindings.AccumulatorBrush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AccumulatorBrush = null;
                break;

            case "DIV":
                WindowBindings.AluRegister1      = WindowBindings.Accumulator.ToString();
                WindowBindings.AluRegister1Brush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluRegister1Brush = null;
                if (args.Length > 0 && args[0][0] == '#')
                {
                    WindowBindings.AluRegister2 = args[0].Remove(0, 1);
                }
                else
                {
                    WindowBindings.AluRegister2 = _memoryContainer.GetValue(args[0]).ToString();
                }
                WindowBindings.AluRegister2Brush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluRegister2Brush = null;
                WindowBindings.AluMethod         = "/";
                WindowBindings.AluMethodBrush    = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluMethodBrush = null;
                try {
                    if (args.Length > 0 && args[0][0] == '#' && int.TryParse(args[0].Remove(0, 1), out var result4))
                    {
                        WindowBindings.Accumulator /= result4;
                    }
                    else
                    {
                        WindowBindings.Accumulator /= _memoryContainer.GetValue(args[0]);
                    }
                }
                catch {
                    MessageBox.Show(
                        $"Failed to execute line {WindowBindings.CurrentLine - 1}:{Environment.NewLine}Attempting to divide a number by 0{Environment.NewLine}\"{line}\"",
                        "Runtime exception", MessageBoxButton.OK, MessageBoxImage.Error
                        );
                }
                WindowBindings.AccumulatorBrush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AccumulatorBrush = null;
                break;

            case "JMP":
                WindowBindings.AluRegister1      = "";
                WindowBindings.AluRegister2      = "";
                WindowBindings.AluRegister1Brush = new SolidColorBrush(BlueColor);
                WindowBindings.AluRegister2Brush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluRegister1Brush = null;
                WindowBindings.AluRegister2Brush = null;
                WindowBindings.AluMethod         = "↷";
                WindowBindings.AluMethodBrush    = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluMethodBrush = null;
                WindowBindings.CurrentLine    = int.Parse(args[0]);
                break;

            case "JMZ":
                WindowBindings.AluRegister1      = "";
                WindowBindings.AluRegister2      = "";
                WindowBindings.AluRegister1Brush = new SolidColorBrush(BlueColor);
                WindowBindings.AluRegister2Brush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluRegister1Brush = null;
                WindowBindings.AluRegister2Brush = null;
                WindowBindings.AluMethod         = "↷";
                WindowBindings.AluMethodBrush    = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluMethodBrush = null;
                if (WindowBindings.Accumulator == 0)
                {
                    WindowBindings.CurrentLine = int.Parse(args[0]);
                }
                break;

            case "HLT":
                WindowBindings.AluRegister1   = "";
                WindowBindings.AluRegister2   = "";
                WindowBindings.AluMethod      = "❌";
                WindowBindings.LinesBrush     = new SolidColorBrush(Colors.Red);
                WindowBindings.AluMethodBrush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluMethodBrush = null;
                WindowBindings.IsRunning      = false;
                break;

            case "NOP":
                WindowBindings.AluRegister1 = "";
                WindowBindings.AluRegister2 = "";
                WindowBindings.AluMethod    = "";
                break;

            default:
                MessageBox.Show(
                    $"Failed to execute line {WindowBindings.CurrentLine - 1}:{Environment.NewLine}This command is not recognized{Environment.NewLine}\"{line}\"",
                    "Runtime exception", MessageBoxButton.OK, MessageBoxImage.Error
                    );
                WindowBindings.AluMethod      = "❌";
                WindowBindings.LinesBrush     = new SolidColorBrush(Colors.Red);
                WindowBindings.AluMethodBrush = new SolidColorBrush(BlueColor);
                if ((int)DelaySizer.Value > 10)
                {
                    await Task.Delay((int)DelaySizer.Value);
                }
                WindowBindings.AluMethodBrush = null;
                WindowBindings.IsRunning      = false;
                break;
            }
        }