Esempio n. 1
0
        public static void Import(string path, bool silent = false)
        {
            List <CodeLabel> labels = new List <CodeLabel>(1000);

            int errorCount = 0;

            foreach (string row in File.ReadAllLines(path, Encoding.UTF8))
            {
                CodeLabel label = CodeLabel.FromString(row);
                if (label == null)
                {
                    errorCount++;
                }
                else
                {
                    labels.Add(label);
                }
            }

            LabelManager.SetLabels(labels);

            if (!silent)
            {
                string message = $"Import completed with {labels.Count} labels imported";
                if (errorCount > 0)
                {
                    message += $" and {errorCount} error(s)";
                }
                MessageBox.Show(message, "Mesen-S", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Esempio n. 2
0
        public static void Import(string path, bool silent = false)
        {
            //This only works reliably for NROM games with 32kb PRG
            DebugState state = new DebugState();

            InteropEmu.DebugGetState(ref state);

            bool hasLargePrg = state.Cartridge.PrgRomSize != 0x8000;

            if (!silent && hasLargePrg)
            {
                if (MessageBox.Show($"Warning: Due to .fns file format limitations, imported labels are not reliable for games that have more than 32kb of PRG ROM.\n\nAre you sure you want to import these labels?", "Mesen", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                {
                    return;
                }
            }
            Dictionary <UInt32, CodeLabel> labels = new Dictionary <uint, CodeLabel>();

            char[] separator = new char[1] {
                '='
            };
            foreach (string row in File.ReadAllLines(path, Encoding.UTF8))
            {
                string[] rowData = row.Split(separator);
                if (rowData.Length < 2)
                {
                    //Invalid row
                    continue;
                }

                uint address;
                if (UInt32.TryParse(rowData[1].Trim().Substring(1), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out address))
                {
                    CodeLabel codeLabel;
                    if (!labels.TryGetValue(address, out codeLabel))
                    {
                        codeLabel             = new CodeLabel();
                        codeLabel.Address     = hasLargePrg ? address : (address - 0x8000);
                        codeLabel.AddressType = hasLargePrg ? AddressType.Register : AddressType.PrgRom;
                        codeLabel.Label       = "";
                        codeLabel.Comment     = "";
                        labels[address]       = codeLabel;
                    }

                    codeLabel.Label = rowData[0].Trim();
                }
            }

            LabelManager.SetLabels(labels.Values);

            if (!silent)
            {
                MessageBox.Show($"Import completed with {labels.Values.Count} labels imported.", "Mesen", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Esempio n. 3
0
        public static void Import(string path, bool silent = false)
        {
            List <CodeLabel> labels = new List <CodeLabel>(1000);

            int errorCount = 0;

            foreach (string row in File.ReadAllLines(path, Encoding.UTF8))
            {
                string lineData   = row.Trim();
                int    splitIndex = lineData.IndexOf(' ');
                UInt32 address;

                if (!UInt32.TryParse(lineData.Substring(0, splitIndex), NumberStyles.HexNumber, null, out address))
                {
                    errorCount++;
                    continue;
                }

                AddressInfo absAddress = DebugApi.GetAbsoluteAddress(new AddressInfo()
                {
                    Address = (int)address, Type = SnesMemoryType.CpuMemory
                });

                if (absAddress.Address >= 0)
                {
                    CodeLabel label = new CodeLabel();
                    label.Address    = (UInt32)absAddress.Address;
                    label.MemoryType = absAddress.Type;
                    label.Comment    = "";
                    string labelName = lineData.Substring(splitIndex + 1).Replace('.', '_');
                    if (string.IsNullOrEmpty(labelName) || !LabelManager.LabelRegex.IsMatch(labelName))
                    {
                        errorCount++;
                    }
                    else
                    {
                        label.Label = labelName;
                        labels.Add(label);
                    }
                }
            }

            LabelManager.SetLabels(labels);

            if (!silent)
            {
                string message = $"Import completed with {labels.Count} labels imported";
                if (errorCount > 0)
                {
                    message += $" and {errorCount} error(s)";
                }
                MessageBox.Show(message, "Mesen-S", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Esempio n. 4
0
        public void Import(string path, bool silent = false)
        {
            string[] fileRows = File.ReadAllLines(path);

            string basePath = Path.GetDirectoryName(path);

            foreach (string row in fileRows)
            {
                try {
                    if (LoadSegments(row) || LoadLines(row) || LoadSpans(row) || LoadFiles(row, basePath) || LoadSymbols(row))
                    {
                        continue;
                    }
                } catch {
                    _errorCount++;
                }
            }

            LoadLabels();
            LoadComments();

            LabelManager.SetLabels(_romLabels.Values);
            LabelManager.SetLabels(_ramLabels.Values);

            if (!silent)
            {
                int labelCount = _romLabels.Count + _ramLabels.Count;
                if (_errorCount > 0)
                {
                    _errorCount -= _filesNotFound.Count;
                    string message = $"Import completed with {labelCount} labels imported";
                    if (_errorCount > 0)
                    {
                        message += $"and {_errorCount} errors - please file a bug report and attach the DBG file you tried to import.";
                    }
                    if (_filesNotFound.Count > 0)
                    {
                        message += Environment.NewLine + Environment.NewLine + "The following files could not be found:";
                        foreach (string file in _filesNotFound)
                        {
                            message += Environment.NewLine + file;
                        }
                    }
                    MessageBox.Show(message, "Mesen", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show($"Import completed with {labelCount} labels imported.", "Mesen", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
Esempio n. 5
0
        public static void CreateAutomaticJumpLabels()
        {
            byte[]           cdlData     = InteropEmu.DebugGetPrgCdlData();
            List <CodeLabel> labelsToAdd = new List <CodeLabel>();

            for (int i = 0; i < cdlData.Length; i++)
            {
                if ((cdlData[i] & (byte)(CdlPrgFlags.JumpTarget | CdlPrgFlags.SubEntryPoint)) != 0)
                {
                    CodeLabel existingLabel = LabelManager.GetLabel((uint)i, AddressType.PrgRom);
                    if (existingLabel == null)
                    {
                        labelsToAdd.Add(new CodeLabel()
                        {
                            Flags       = CodeLabelFlags.AutoJumpLabel,
                            Address     = (uint)i,
                            AddressType = AddressType.PrgRom,
                            Label       = ((cdlData[i] & (byte)CdlPrgFlags.SubEntryPoint) == 0 ? "L" : "F") + i.ToString("X4"),
                            Comment     = ""
                        });
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(existingLabel.Label))
                        {
                            //A comment exists for this address, add the label to it, but keep the comment and don't mark it as a auto-label
                            labelsToAdd.Add(new CodeLabel()
                            {
                                Address     = (uint)i,
                                AddressType = AddressType.PrgRom,
                                Label       = ((cdlData[i] & (byte)CdlPrgFlags.SubEntryPoint) == 0 ? "L" : "F") + i.ToString("X4"),
                                Comment     = existingLabel.Comment
                            });
                        }
                    }
                }
            }

            if (labelsToAdd.Count > 0)
            {
                LabelManager.SetLabels(labelsToAdd, true);
            }
        }
Esempio n. 6
0
        public static DebugWorkspace GetWorkspace()
        {
            string romName = InteropEmu.GetRomInfo().GetRomName();

            if (_workspace == null || _romName != romName)
            {
                SymbolProvider = null;
                lock (_lock) {
                    if (_workspace == null || _romName != romName)
                    {
                        if (_workspace != null)
                        {
                            SaveWorkspace();
                        }
                        _romName   = InteropEmu.GetRomInfo().GetRomName();
                        _workspace = DebugWorkspace.GetWorkspace();

                        //Setup labels
                        if (_workspace.Labels.Count == 0)
                        {
                            LabelManager.ResetLabels();
                            if (!ConfigManager.Config.DebugInfo.DisableDefaultLabels)
                            {
                                LabelManager.SetDefaultLabels(InteropEmu.GetRomInfo().MapperId);
                            }
                        }
                        else
                        {
                            LabelManager.ResetLabels();
                            LabelManager.SetLabels(_workspace.Labels, true);
                        }

                        //Load watch entries
                        WatchManager.WatchEntries = _workspace.WatchValues;

                        //Load breakpoints
                        BreakpointManager.SetBreakpoints(_workspace.Breakpoints);
                    }
                }
            }
            return(_workspace);
        }
Esempio n. 7
0
        public static DebugWorkspace GetWorkspace()
        {
            string romName = InteropEmu.GetRomInfo().GetRomName();

            if (_workspace == null || _romName != romName)
            {
                lock (_lock) {
                    if (_workspace == null || _romName != romName)
                    {
                        if (_workspace != null)
                        {
                            SaveWorkspace();
                        }
                        _romName   = InteropEmu.GetRomInfo().GetRomName();
                        _workspace = DebugWorkspace.GetWorkspace();

                        //Setup labels
                        if (_workspace.Labels.Count == 0)
                        {
                            LabelManager.ResetLabels();
                            if (!ConfigManager.Config.DebugInfo.DisableDefaultLabels)
                            {
                                LabelManager.SetDefaultLabels(InteropEmu.FdsGetSideCount() > 0);
                            }
                        }
                        else
                        {
                            LabelManager.ResetLabels();
                            LabelManager.SetLabels(_workspace.Labels, false);
                        }

                        //Load watch entries
                        WatchManager.WatchEntries = _workspace.WatchValues;

                        //Load breakpoints
                        BreakpointManager.Breakpoints.Clear();
                        BreakpointManager.Breakpoints.AddRange(_workspace.Breakpoints);
                    }
                }
            }
            return(_workspace);
        }
Esempio n. 8
0
        public static void CreateAutomaticJumpLabels()
        {
            byte[]           cdlData     = InteropEmu.DebugGetPrgCdlData();
            List <CodeLabel> labelsToAdd = new List <CodeLabel>();

            for (int i = 0; i < cdlData.Length; i++)
            {
                if ((cdlData[i] & (byte)CdlPrgFlags.JumpTarget) != 0 && LabelManager.GetLabel((uint)i, AddressType.PrgRom) == null)
                {
                    labelsToAdd.Add(new CodeLabel()
                    {
                        Flags = CodeLabelFlags.AutoJumpLabel, Address = (uint)i, AddressType = AddressType.PrgRom, Label = "L" + i.ToString("X4"), Comment = ""
                    });
                }
            }
            if (labelsToAdd.Count > 0)
            {
                LabelManager.SetLabels(labelsToAdd, true);
            }
        }
Esempio n. 9
0
        public static void CreateAutomaticJumpLabels()
        {
            bool[]           jumpTargets = InteropEmu.DebugGetJumpTargets();
            List <CodeLabel> labelsToAdd = new List <CodeLabel>();

            for (int i = 0; i < jumpTargets.Length; i++)
            {
                if (jumpTargets[i] && LabelManager.GetLabel((uint)i, AddressType.PrgRom) == null)
                {
                    labelsToAdd.Add(new CodeLabel()
                    {
                        Flags = CodeLabelFlags.AutoJumpLabel, Address = (uint)i, AddressType = AddressType.PrgRom, Label = "L" + i.ToString("X4"), Comment = ""
                    });
                }
            }
            if (labelsToAdd.Count > 0)
            {
                LabelManager.SetLabels(labelsToAdd, true);
            }
        }
Esempio n. 10
0
        public static void SetupWorkspace(bool saveCurrentWorkspace = true)
        {
            string romName = InteropEmu.GetRomInfo().GetRomName();

            lock (_lock) {
                if (_workspace != null && _romName == romName)
                {
                    if (saveCurrentWorkspace)
                    {
                        SaveWorkspace();
                    }

                    //Setup labels
                    if (_workspace.Labels.Count == 0)
                    {
                        LabelManager.ResetLabels();
                        if (!ConfigManager.Config.DebugInfo.DisableDefaultLabels)
                        {
                            LabelManager.SetDefaultLabels(InteropEmu.FdsGetSideCount() > 0);
                        }
                    }
                    else
                    {
                        LabelManager.ResetLabels();
                        LabelManager.SetLabels(_workspace.Labels, false);
                    }

                    //Load watch entries
                    WatchManager.WatchEntries = _workspace.WatchValues;

                    //Load breakpoints
                    BreakpointManager.SetBreakpoints(_workspace.Breakpoints);
                }
                else
                {
                    Clear();
                }
            }
        }
Esempio n. 11
0
        public void Import(string path, bool silent = false)
        {
            RomInfo romInfo = InteropEmu.GetRomInfo();

            _headerSize = (int)romInfo.FilePrgOffset;

            DebugState state = new DebugState();

            InteropEmu.DebugGetState(ref state);
            for (int i = 0; i < state.Cartridge.PrgMemoryType.Length; i++)
            {
                if (state.Cartridge.PrgMemoryType[i] == PrgMemoryType.WorkRam)
                {
                    _workRamStart = Math.Min(_workRamStart, i * 0x100);
                    _workRamEnd   = Math.Max(_workRamEnd, i * 0x100 + 0xFF);
                }
                else if (state.Cartridge.PrgMemoryType[i] == PrgMemoryType.SaveRam)
                {
                    _saveRamStart = Math.Min(_saveRamStart, i * 0x100);
                    _saveRamEnd   = Math.Max(_saveRamEnd, i * 0x100 + 0xFF);
                }
            }

            DbgFileStamp = File.GetLastWriteTime(path);
            string[] fileRows = File.ReadAllLines(path);

            string basePath = Path.GetDirectoryName(path);

            DbgPath = basePath;
            foreach (string row in fileRows)
            {
                try {
                    if (LoadLines(row) || LoadSpans(row) || LoadSymbols(row) || LoadCSymbols(row) || LoadScopes(row) || LoadFiles(row, basePath) || LoadSegments(row))
                    {
                        continue;
                    }
                } catch {
                    _errorCount++;
                }
            }

            LoadFileData(basePath);

            BuildCdlData();

            foreach (LineInfo line in _lines.Values)
            {
                foreach (int spanID in line.SpanIDs)
                {
                    SpanInfo span;
                    if (_spans.TryGetValue(spanID, out span))
                    {
                        SegmentInfo segment;
                        if (_segments.TryGetValue(span.SegmentID, out segment) && !segment.IsRam)
                        {
                            for (int i = 0; i < span.Size; i++)
                            {
                                int prgAddress = segment.FileOffset - _headerSize + span.Offset + i;
                                if (prgAddress >= state.Cartridge.PrgRomSize)
                                {
                                    //Address is outside PRG (probably CHR ROM)
                                    continue;
                                }

                                LineInfo existingLine;
                                if (_linesByPrgAddress.TryGetValue(prgAddress, out existingLine) && existingLine.Type == LineType.External)
                                {
                                    //Give priority to lines that come from C files
                                    continue;
                                }

                                _linesByPrgAddress[prgAddress] = line;
                                if (i == 0 && spanID == line.SpanIDs[0])
                                {
                                    //Mark the first byte of the first span representing this line as the PRG address for this line of code
                                    FileInfo file = _files[line.FileID];
                                    _prgAddressByLine[file.ID.ToString() + "_" + line.LineNumber.ToString()] = prgAddress;
                                }
                            }
                        }
                    }
                }
            }

            LoadLabels();

            int labelCount = 0;

            DebugImportConfig config = ConfigManager.Config.DebugInfo.ImportConfig;

            if (config.DbgImportComments)
            {
                LoadComments();
            }
            List <CodeLabel> labels = new List <CodeLabel>(_romLabels.Count + _ramLabels.Count + _workRamLabels.Count + _saveRamLabels.Count);

            if (config.DbgImportPrgRomLabels)
            {
                labels.AddRange(_romLabels.Values);
                labelCount += _romLabels.Count;
            }
            if (config.DbgImportRamLabels)
            {
                labels.AddRange(_ramLabels.Values);
                labelCount += _ramLabels.Count;
            }
            if (config.DbgImportWorkRamLabels)
            {
                labels.AddRange(_workRamLabels.Values);
                labelCount += _workRamLabels.Count;
            }
            if (config.DbgImportSaveRamLabels)
            {
                labels.AddRange(_saveRamLabels.Values);
                labelCount += _saveRamLabels.Count;
            }

            LabelManager.SetLabels(labels, true);

            if (!silent)
            {
                if (_errorCount > 0)
                {
                    _errorCount -= _filesNotFound.Count;
                    string message = $"Import completed with {labelCount} labels imported";
                    if (_errorCount > 0)
                    {
                        message += $"and {_errorCount} errors - please file a bug report and attach the DBG file you tried to import.";
                    }
                    if (_filesNotFound.Count > 0)
                    {
                        message += Environment.NewLine + Environment.NewLine + "The following files could not be found:";
                        foreach (string file in _filesNotFound)
                        {
                            message += Environment.NewLine + file;
                        }
                    }
                    MessageBox.Show(message, "Mesen", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show($"Import completed with {labelCount} labels imported.", "Mesen", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
Esempio n. 12
0
        public static void Import(string path, bool silent = false)
        {
            Dictionary <AddressType, Dictionary <UInt32, CodeLabel> > labels = new Dictionary <AddressType, Dictionary <UInt32, CodeLabel> >()
            {
                { AddressType.InternalRam, new Dictionary <uint, CodeLabel>() },
                { AddressType.PrgRom, new Dictionary <uint, CodeLabel>() },
                { AddressType.Register, new Dictionary <uint, CodeLabel>() },
                { AddressType.SaveRam, new Dictionary <uint, CodeLabel>() },
                { AddressType.WorkRam, new Dictionary <uint, CodeLabel>() }
            };

            char[] separator = new char[1] {
                ':'
            };
            foreach (string row in File.ReadAllLines(path, Encoding.UTF8))
            {
                string[] rowData = row.Split(separator, 4);
                if (rowData.Length < 3)
                {
                    //Invalid row
                    continue;
                }
                AddressType type;
                switch (rowData[0][0])
                {
                case 'G': type = AddressType.Register; break;

                case 'R': type = AddressType.InternalRam; break;

                case 'P': type = AddressType.PrgRom; break;

                case 'S': type = AddressType.SaveRam; break;

                case 'W': type = AddressType.WorkRam; break;

                default: continue;
                }

                uint address;
                if (UInt32.TryParse(rowData[1], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out address))
                {
                    CodeLabel codeLabel;
                    if (!labels[type].TryGetValue(address, out codeLabel))
                    {
                        codeLabel             = new CodeLabel();
                        codeLabel.Address     = address;
                        codeLabel.AddressType = type;
                        codeLabel.Label       = "";
                        codeLabel.Comment     = "";
                        labels[type][address] = codeLabel;
                    }

                    if (rowData.Length > 3)
                    {
                        codeLabel.Comment = rowData[3].Replace("\\n", "\n");
                    }
                    codeLabel.Label = rowData[2].Replace("\\n", "\n").Replace("\n", "");
                }
            }

            int labelCount = 0;

            foreach (KeyValuePair <AddressType, Dictionary <UInt32, CodeLabel> > kvp in labels)
            {
                LabelManager.SetLabels(kvp.Value.Values);
                labelCount += kvp.Value.Values.Count;
            }

            if (!silent)
            {
                MessageBox.Show($"Import completed with {labelCount} labels imported.", "Mesen", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 public static void RefreshLabels()
 {
     InteropEmu.DebugDeleteLabels();
     LabelManager.SetLabels(GetLabels(), true);
 }
Esempio n. 14
0
        public static void Import(string path, bool silent = false)
        {
            const int prgBankSize  = 0x4000;
            const int wramBankSize = 0x1000;
            const int sramBankSize = 0x2000;

            List <CodeLabel> labels = new List <CodeLabel>(1000);

            int errorCount = 0;

            foreach (string row in File.ReadAllLines(path, Encoding.UTF8))
            {
                UInt32 address;
                UInt32 bank;
                string labelName;

                if (!GetBankAddressLabel(row, out address, out bank, out labelName))
                {
                    errorCount++;
                    continue;
                }
                else if (labelName == null)
                {
                    //Empty line/comment
                    continue;
                }

                UInt32      fullAddress = 0;
                AddressInfo absAddress;
                if (address <= 0x7FFF)
                {
                    fullAddress = bank * prgBankSize + (address & (prgBankSize - 1));
                    absAddress  = new AddressInfo()
                    {
                        Address = (int)fullAddress, Type = SnesMemoryType.GbPrgRom
                    };
                }
                else if (address >= 0xA000 && address <= 0xCFFF)
                {
                    fullAddress = bank * sramBankSize + (address & (sramBankSize - 1));
                    absAddress  = new AddressInfo()
                    {
                        Address = (int)fullAddress, Type = SnesMemoryType.GbCartRam
                    };
                }
                else if (address >= 0xC000 && address <= 0xDFFF)
                {
                    fullAddress = bank * wramBankSize + (address & (wramBankSize - 1));
                    absAddress  = new AddressInfo()
                    {
                        Address = (int)fullAddress, Type = SnesMemoryType.GbWorkRam
                    };
                }
                else
                {
                    absAddress = DebugApi.GetAbsoluteAddress(new AddressInfo()
                    {
                        Address = (int)address, Type = SnesMemoryType.GameboyMemory
                    });
                }

                if (absAddress.Address >= 0)
                {
                    CodeLabel label = new CodeLabel();
                    label.Address    = (UInt32)absAddress.Address;
                    label.MemoryType = absAddress.Type;
                    label.Comment    = "";
                    label.Label      = labelName;
                    labels.Add(label);
                }
                else
                {
                    errorCount++;
                }
            }

            LabelManager.SetLabels(labels);

            if (!silent)
            {
                string message = $"Import completed with {labels.Count} labels imported";
                if (errorCount > 0)
                {
                    message += $" and {errorCount} error(s)";
                }
                MessageBox.Show(message, "Mesen-S", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Esempio n. 15
0
        public void Import(string path, bool silent = false)
        {
            string[] fileRows = File.ReadAllLines(path);

            string basePath = Path.GetDirectoryName(path);

            foreach (string row in fileRows)
            {
                try {
                    if (LoadLines(row) || LoadSpans(row) || LoadSymbols(row) || LoadCSymbols(row) || LoadFiles(row, basePath) || LoadSegments(row))
                    {
                        continue;
                    }
                } catch {
                    _errorCount++;
                }
            }

            LoadFileData(basePath);

            int prgSize = InteropEmu.DebugGetMemorySize(DebugMemoryType.PrgRom);

            if (prgSize > 0)
            {
                byte[] cdlFile = new byte[prgSize];
                foreach (KeyValuePair <int, SpanInfo> kvp in _spans)
                {
                    SegmentInfo segment;
                    if (_segments.TryGetValue(kvp.Value.SegmentID, out segment))
                    {
                        if (!segment.IsRam && kvp.Value.Size != segment.Size)
                        {
                            int prgAddress = kvp.Value.Offset + segment.FileOffset - iNesHeaderSize;

                            if (prgAddress >= 0 && prgAddress < prgSize)
                            {
                                for (int i = 0; i < kvp.Value.Size; i++)
                                {
                                    if (cdlFile[prgAddress + i] == 0 && !kvp.Value.IsData && kvp.Value.Size <= 3)
                                    {
                                        cdlFile[prgAddress + i] = (byte)0x01;
                                    }
                                    else if (kvp.Value.IsData)
                                    {
                                        cdlFile[prgAddress + i] = (byte)0x02;
                                    }
                                }
                            }
                        }
                    }
                }
                InteropEmu.DebugSetCdlData(cdlFile);
            }

            foreach (LineInfo line in _lines.Values)
            {
                if (line.SpanID == null)
                {
                    continue;
                }

                FileInfo    file    = _files[line.FileID];
                SpanInfo    span    = _spans[line.SpanID.Value];
                SegmentInfo segment = _segments[span.SegmentID];
                if (!segment.IsRam)
                {
                    for (int i = 0; i < span.Size; i++)
                    {
                        int prgAddress = segment.FileOffset - iNesHeaderSize + span.Offset + i;

                        LineInfo existingLine;
                        if (_linesByPrgAddress.TryGetValue(prgAddress, out existingLine) && existingLine.Type == LineType.External)
                        {
                            //Give priority to lines that come from C files
                            continue;
                        }

                        _linesByPrgAddress[prgAddress] = line;
                        if (i == 0)
                        {
                            _prgAddressByLine[file.ID.ToString() + "_" + line.LineNumber.ToString()] = prgAddress;
                        }
                    }
                }
            }

            LoadLabels();

            int labelCount = 0;

            DebugImportConfig config = ConfigManager.Config.DebugInfo.ImportConfig;

            if (config.DbgImportComments)
            {
                LoadComments();
            }
            if (config.DbgImportPrgRomLabels)
            {
                LabelManager.SetLabels(_romLabels.Values);
                labelCount += _romLabels.Count;
            }
            if (config.DbgImportRamLabels)
            {
                LabelManager.SetLabels(_ramLabels.Values);
                labelCount += _ramLabels.Count;
            }

            if (!silent)
            {
                if (_errorCount > 0)
                {
                    _errorCount -= _filesNotFound.Count;
                    string message = $"Import completed with {labelCount} labels imported";
                    if (_errorCount > 0)
                    {
                        message += $"and {_errorCount} errors - please file a bug report and attach the DBG file you tried to import.";
                    }
                    if (_filesNotFound.Count > 0)
                    {
                        message += Environment.NewLine + Environment.NewLine + "The following files could not be found:";
                        foreach (string file in _filesNotFound)
                        {
                            message += Environment.NewLine + file;
                        }
                    }
                    MessageBox.Show(message, "Mesen", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show($"Import completed with {labelCount} labels imported.", "Mesen", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
Esempio n. 16
0
        public static void Import(string path, bool silent = false)
        {
            Dictionary <AddressType, Dictionary <UInt32, CodeLabel> > labels = new Dictionary <AddressType, Dictionary <UInt32, CodeLabel> >()
            {
                { AddressType.InternalRam, new Dictionary <uint, CodeLabel>() },
                { AddressType.PrgRom, new Dictionary <uint, CodeLabel>() },
                { AddressType.Register, new Dictionary <uint, CodeLabel>() },
                { AddressType.SaveRam, new Dictionary <uint, CodeLabel>() },
                { AddressType.WorkRam, new Dictionary <uint, CodeLabel>() }
            };

            DebugImportConfig config = ConfigManager.Config.DebugInfo.ImportConfig;

            int errorCount = 0;

            char[] separator = new char[1] {
                ':'
            };
            foreach (string row in File.ReadAllLines(path, Encoding.UTF8))
            {
                string[] rowData = row.Split(separator, 4);
                if (rowData.Length < 3)
                {
                    //Invalid row
                    continue;
                }
                AddressType type;
                bool        importLabel = false;
                switch (rowData[0][0])
                {
                case 'G': type = AddressType.Register; importLabel = config.MlbImportRegisterLabels; break;

                case 'R': type = AddressType.InternalRam; importLabel = config.MlbImportInternalRamLabels; break;

                case 'P': type = AddressType.PrgRom; importLabel = config.MlbImportPrgRomLabels; break;

                case 'S': type = AddressType.SaveRam; importLabel = config.MlbImportSaveRamLabels; break;

                case 'W': type = AddressType.WorkRam; importLabel = config.MlbImportWorkRamLabels; break;

                default: continue;
                }

                if (importLabel)
                {
                    string addressString = rowData[1];
                    uint   address       = 0;
                    uint   length        = 1;
                    if (addressString.Contains("-"))
                    {
                        uint     addressEnd;
                        string[] addressStartEnd = addressString.Split('-');
                        if (UInt32.TryParse(addressStartEnd[0], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out address) &&
                            UInt32.TryParse(addressStartEnd[1], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out addressEnd))
                        {
                            if (addressEnd > address)
                            {
                                length = addressEnd - address;
                            }
                            else
                            {
                                //Invalid label (start < end)
                                errorCount++;
                                continue;
                            }
                        }
                        else
                        {
                            //Invalid label (can't parse)
                            errorCount++;
                            continue;
                        }
                    }
                    else
                    {
                        if (!UInt32.TryParse(rowData[1], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out address))
                        {
                            //Invalid label (can't parse)
                            errorCount++;
                            continue;
                        }
                        length = 1;
                    }

                    string labelName = rowData[2];
                    if (!string.IsNullOrEmpty(labelName) && !LabelManager.LabelRegex.IsMatch(labelName))
                    {
                        //Reject labels that don't respect the label naming restrictions
                        errorCount++;
                        continue;
                    }

                    CodeLabel codeLabel;
                    if (!labels[type].TryGetValue(address, out codeLabel))
                    {
                        codeLabel             = new CodeLabel();
                        codeLabel.Address     = address;
                        codeLabel.AddressType = type;
                        codeLabel.Label       = "";
                        codeLabel.Comment     = "";
                        labels[type][address] = codeLabel;
                    }

                    if (rowData.Length > 3 && config.MlbImportComments)
                    {
                        codeLabel.Comment = rowData[3].Replace("\\n", "\n");
                    }
                    codeLabel.Label  = labelName;
                    codeLabel.Length = length;
                }
            }

            int labelCount = 0;

            foreach (KeyValuePair <AddressType, Dictionary <UInt32, CodeLabel> > kvp in labels)
            {
                labelCount += kvp.Value.Values.Count;
            }
            List <CodeLabel> codeLabels = new List <CodeLabel>();

            foreach (KeyValuePair <AddressType, Dictionary <UInt32, CodeLabel> > kvp in labels)
            {
                codeLabels.AddRange(kvp.Value.Values);
            }
            LabelManager.SetLabels(codeLabels);

            if (!silent)
            {
                string message = $"Import completed with {labelCount} labels imported";
                if (errorCount > 0)
                {
                    message += $" and {errorCount} error(s)";
                }
                MessageBox.Show(message, "Mesen", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Esempio n. 17
0
        public static void Import(string path, bool silent = false)
        {
            List <CodeLabel>  labels      = new List <CodeLabel>(1000);
            List <Breakpoint> breakpoints = new List <Breakpoint>(1000);

            int errorCount = 0;

            foreach (string row in File.ReadAllLines(path, Encoding.UTF8))
            {
                string lineData = row.Trim();
                if (lineData.StartsWith("<"))
                {
                    continue;                                           //this is a <command line>: operation and we don't want it
                }
                if (lineData.Contains(":="))
                {
                    continue;                                          //this is a "variable" we dont' want it
                }
                int splitIndex = lineData.IndexOf(' ');

                string[] parts = lineData.Substring(splitIndex + 1).Split('=');

                UInt32       address;
                string       value = parts[1].Trim();
                NumberStyles type  = NumberStyles.Integer;
                if (value.StartsWith("$"))
                {
                    type  = NumberStyles.HexNumber;
                    value = value.Substring(1);                     //remove the $
                }
                else if (value.StartsWith("%"))
                {
                    continue;                     // Binary values are not labels 99.99999999999999% of the time
                }

                if (!UInt32.TryParse(value, type, null, out address))
                {
                    errorCount++;
                    continue;
                }

                AddressInfo absAddress = DebugApi.GetAbsoluteAddress(new AddressInfo()
                {
                    Address = (int)address, Type = SnesMemoryType.CpuMemory
                });

                if (absAddress.Address >= 0)
                {
                    if (parts[0].Contains("BREAK"))
                    {
                        //we have a break point
                        Breakpoint breakpoint = new Breakpoint();
                        breakpoint.Address     = address;
                        breakpoint.AddressType = BreakpointAddressType.SingleAddress;
                        breakpoint.BreakOnExec = true;
                        breakpoint.CpuType     = CpuType.Cpu;
                        breakpoints.Add(breakpoint);
                    }
                    else if (parts[0].Contains("ASSERT_"))
                    {
                        string assert_field = parts[0].Trim().ToLower().Substring(parts[0].IndexOf("ASSERT_") + 7);
                        string cond         = string.Empty;
                        if (assert_field == "a8")
                        {
                            cond = "(PS & 32) == 32";
                        }
                        else if (assert_field == "a16")
                        {
                            cond = "(PS & 32) == 0";
                        }
                        else if (assert_field == "xy8")
                        {
                            cond = "(PS & 16) == 16";
                        }
                        else if (assert_field == "xy16")
                        {
                            cond = "(PS & 16) == 0";
                        }
                        else if (assert_field == "axy8")
                        {
                            cond = "(PS & 48) == 48";
                        }
                        else if (assert_field == "axy16")
                        {
                            cond = "(PS & 48) == 32";
                        }
                        else if (assert_field == "jsl")
                        {
                            cond = "jslf == 1";
                        }
                        else if (assert_field == "jsr")
                        {
                            cond = "jslf == 0";
                        }
                        else
                        {
                            cond = assert_field.Replace("_0x", "_$");
                            cond = cond.Replace("_eq_", "==");
                            cond = cond.Replace("_lt_", "<");
                            cond = cond.Replace("_lte_", "<=");
                            cond = cond.Replace("_gt_", ">");
                            cond = cond.Replace("_gte_", ">=");
                            cond = cond.Replace("_ne_", "!=");
                            cond = cond.Replace("_and_", "&&");
                            cond = cond.Replace("_or_", "||");
                            cond = cond.Replace("_not_", "!");
                            cond = cond.Replace("_lbrac_", "(");
                            cond = cond.Replace("_rbrac_", ")");
                            cond = cond.Replace("_", " ");
                        }

                        Breakpoint breakpoint = new Breakpoint();
                        breakpoint.Address     = address;
                        breakpoint.AddressType = BreakpointAddressType.SingleAddress;
                        breakpoint.BreakOnExec = true;
                        breakpoint.CpuType     = CpuType.Cpu;
                        breakpoint.IsAssert    = true;
                        breakpoint.Condition   = "!(" + cond + ")";
                        breakpoints.Add(breakpoint);
                    }
                    else if (parts[0].Contains("WATCH_"))
                    {
                        string[]   watchParts = parts[0].Trim().ToLower().Split('_');
                        Breakpoint breakpoint = new Breakpoint();
                        breakpoint.CpuType     = CpuType.Cpu;
                        breakpoint.IsAssert    = false;
                        breakpoint.Condition   = String.Empty;
                        breakpoint.BreakOnExec = false;
                        int range = 1;
                        for (int i = 1; i < watchParts.Length; ++i)
                        {
                            switch (watchParts[i])
                            {
                            case "load":
                            case "read":
                                breakpoint.BreakOnRead = true;
                                break;

                            case "store":
                            case "write":
                                breakpoint.BreakOnWrite = true;
                                break;

                            case "readwrite":
                            case "writeread":
                            case "loadstore":
                            case "storeload":
                                breakpoint.BreakOnRead  = true;
                                breakpoint.BreakOnWrite = true;
                                break;

                            case "word":
                                range = 2;
                                break;

                            case "long":
                                range = 3;
                                break;
                            }
                        }
                        breakpoint.EndAddress = address - 1;
                        switch (range)
                        {
                        case 1:
                            breakpoint.StartAddress = address - 1;
                            breakpoint.AddressType  = BreakpointAddressType.SingleAddress;
                            break;

                        case 2:
                            breakpoint.StartAddress = address - 2;
                            breakpoint.AddressType  = BreakpointAddressType.AddressRange;
                            break;

                        case 3:
                            breakpoint.StartAddress = address - 3;
                            breakpoint.AddressType  = BreakpointAddressType.AddressRange;
                            break;
                        }
                        breakpoint.Address = breakpoint.StartAddress;
                        breakpoints.Add(breakpoint);
                    }
                    else
                    {
                        CodeLabel label = new CodeLabel();
                        label.Address    = (UInt32)absAddress.Address;
                        label.MemoryType = absAddress.Type;
                        label.Comment    = "";
                        string labelName = parts[0].Trim();
                        if (string.IsNullOrEmpty(labelName) || !LabelManager.LabelRegex.IsMatch(labelName))
                        {
                            errorCount++;
                        }
                        else
                        {
                            label.Label = labelName;
                            labels.Add(label);
                        }
                    }
                }
            }

            LabelManager.SetLabels(labels);
            BreakpointManager.SetBreakpoints(breakpoints);

            if (!silent)
            {
                string message = $"Import completed with {labels.Count} labels imported";
                if (errorCount > 0)
                {
                    message += $" and {errorCount} error(s)";
                }
                MessageBox.Show(message, "Mesen-S", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }