Esempio n. 1
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. 2
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. 3
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. 4
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;

            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;
                }

                uint address;
                if (importLabel && 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 && config.MlbImportComments)
                    {
                        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);
            }
        }