Beispiel #1
0
        // https://stackoverflow.com/a/29679597
        private void UpdateSample()
        {
            // cheeky way of using the same methods for disassembling a different set of data :^)
            while (sampleTable.Count < 0x8000)
            {
                sampleTable.Add(new ROMByte());
            }

            using (MemoryStream mem = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(mem))
                {
                    List <ROMByte>             tempTable = Data.GetTable();
                    Data.ROMMapMode            tempMode = Data.GetROMMapMode();
                    Data.ROMSpeed              tempSpeed = Data.GetROMSpeed();
                    Dictionary <int, string>   tempAlias = Data.GetAllLabels(), tempComment = Data.GetAllComments();
                    LogCreator.FormatStructure tempStructure = LogCreator.structure;
                    Data.Restore(sampleTable, Data.ROMMapMode.LoROM, Data.ROMSpeed.FastROM, sampleAlias, sampleComment);
                    LogCreator.structure = LogCreator.FormatStructure.SingleFile;

                    LogCreator.CreateLog(sw, StreamWriter.Null);

                    Data.Restore(tempTable, tempMode, tempSpeed, tempAlias, tempComment);
                    LogCreator.structure = tempStructure;

                    sw.Flush();
                    mem.Seek(0, SeekOrigin.Begin);

                    textSample.Text = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
                }
        }
Beispiel #2
0
 public static int ConvertPCtoSNES(int offset)
 {
     if (Data.GetROMMapMode() == Data.ROMMapMode.LoROM)
     {
         offset = ((offset & 0x3F8000) << 1) | 0x8000 | (offset & 0x7FFF);
         if (Data.GetROMSpeed() == Data.ROMSpeed.FastROM || offset >= 0x7E0000)
         {
             offset |= 0x800000;
         }
     }
     else if (Data.GetROMMapMode() == Data.ROMMapMode.HiROM)
     {
         offset |= 0x400000;
         if (Data.GetROMSpeed() == Data.ROMSpeed.FastROM || offset >= 0x7E0000)
         {
             offset |= 0x800000;
         }
     }
     else if (Data.GetROMMapMode() == Data.ROMMapMode.ExHiROM)
     {
         if (offset < 0x40000)
         {
             offset |= 0xC00000;
         }
         else if (offset >= 0x7E0000)
         {
             offset &= 0x3FFFFF;
         }
     }
     else
     {
         if (offset >= 0x400000 && Data.GetROMMapMode() == Data.ROMMapMode.ExSA1ROM)
         {
             offset += 0x800000;
         }
         else
         {
             offset = ((offset & 0x3F8000) << 1) | 0x8000 | (offset & 0x7FFF);
             if (offset >= 0x400000)
             {
                 offset += 0x400000;
             }
         }
     }
     return(offset);
 }
Beispiel #3
0
        private static string GetMap(int offset, int length)
        {
            string s = "";

            switch (Data.GetROMMapMode())
            {
            case Data.ROMMapMode.LoROM: s = "lorom"; break;

            case Data.ROMMapMode.HiROM: s = "hirom"; break;

            case Data.ROMMapMode.ExHiROM: s = "exhirom"; break;

            case Data.ROMMapMode.SA1ROM: s = "sa1rom"; break;

            case Data.ROMMapMode.ExSA1ROM: s = "exsa1rom"; break;
            }
            return(string.Format("{0," + (length * -1) + "}", s));
        }
Beispiel #4
0
        public static int ConvertSNEStoPC(int address)
        {
            // WRAM is N/A to PC addressing
            if ((address & 0xFE0000) == 0x7E0000)
            {
                return(-1);
            }

            // WRAM mirror & PPU regs are N/A to PC addressing
            if (((address & 0x400000) == 0) && ((address & 0x8000) == 0))
            {
                return(-1);
            }

            if (Data.GetROMMapMode() == Data.ROMMapMode.LoROM)
            {
                // SRAM is N/A to PC addressing
                if (((address & 0x700000) == 0x700000) && ((address & 0x8000) == 0))
                {
                    return(-1);
                }

                return(UnmirroredOffset(((address & 0x7F0000) >> 1) | (address & 0x7FFF)));
            }
            else if (Data.GetROMMapMode() == Data.ROMMapMode.HiROM)
            {
                return(UnmirroredOffset(address & 0x3FFFFF));
            }
            else if (Data.GetROMMapMode() == Data.ROMMapMode.ExHiROM)
            {
                return(UnmirroredOffset(((~address & 0x800000) >> 1) | (address & 0x3FFFFF)));
            }
            else
            {
                // BW-RAM is N/A to PC addressing
                if (address >= 0x400000 && address <= 0x7FFFFF)
                {
                    return(-1);
                }

                if (address >= 0xC00000)
                {
                    if (Data.GetROMMapMode() == Data.ROMMapMode.ExSA1ROM)
                    {
                        return(UnmirroredOffset(address & 0x7FFFFF));
                    }
                    else
                    {
                        return(UnmirroredOffset(address & 0x3FFFFF));
                    }
                }
                else
                {
                    if (address >= 0x800000)
                    {
                        address -= 0x400000;
                    }

                    // SRAM is N/A to PC addressing
                    if (((address & 0x8000) == 0))
                    {
                        return(-1);
                    }

                    return(UnmirroredOffset(((address & 0x7F0000) >> 1) | (address & 0x7FFF)));
                }
            }
        }
Beispiel #5
0
        public static int CreateLog(StreamWriter sw, StreamWriter er)
        {
            Dictionary <int, string> tempAlias = Data.GetAllLabels();

            Data.Restore(a: new Dictionary <int, string>(tempAlias));
            AliasList.me.locked = true;
            bankSize            = Data.GetROMMapMode() == Data.ROMMapMode.LoROM ? 0x8000 : 0x10000; // todo

            AddTemporaryLabels();

            string[] split = format.Split('%');
            err        = er;
            errorCount = 0;
            usedLabels = new List <int>();

            list = new List <Tuple <string, int> >();
            for (int i = 0; i < split.Length; i++)
            {
                if (i % 2 == 0)
                {
                    list.Add(Tuple.Create(split[i], int.MaxValue));
                }
                else
                {
                    int colon = split[i].IndexOf(':');
                    if (colon < 0)
                    {
                        list.Add(Tuple.Create(split[i], parameters[split[i]].Item2));
                    }
                    else
                    {
                        list.Add(Tuple.Create(split[i].Substring(0, colon), int.Parse(split[i].Substring(colon + 1))));
                    }
                }
            }

            int pointer = 0, size = (Data.GetTable() == ExportDisassembly.sampleTable) ? 0x7B : Data.GetROMSize(), bank = -1;

            if (structure == FormatStructure.OneBankPerFile)
            {
                folder = Path.GetDirectoryName(((FileStream)sw.BaseStream).Name);
                sw.WriteLine(GetLine(pointer, "map"));
                sw.WriteLine(GetLine(pointer, "empty"));
                for (int i = 0; i < size; i += bankSize)
                {
                    sw.WriteLine(GetLine(i, "incsrc"));
                }
                sw.WriteLine(GetLine(-1, "incsrc"));
            }
            else
            {
                sw.WriteLine(GetLine(pointer, "map"));
                sw.WriteLine(GetLine(pointer, "empty"));
            }

            while (pointer < size)
            {
                int snes = Util.ConvertPCtoSNES(pointer);
                if ((snes >> 16) != bank)
                {
                    if (structure == FormatStructure.OneBankPerFile)
                    {
                        sw.Close();
                        sw = new StreamWriter(string.Format("{0}/bank_{1}.asm", folder, Util.NumberToBaseString((snes >> 16), Util.NumberBase.Hexadecimal, 2)));
                    }

                    sw.WriteLine(GetLine(pointer, "empty"));
                    sw.WriteLine(GetLine(pointer, "org"));
                    sw.WriteLine(GetLine(pointer, "empty"));
                    if ((snes % bankSize) != 0)
                    {
                        err.WriteLine("({0}) Offset 0x{1:X}: An instruction crossed a bank boundary.", ++errorCount, pointer);
                    }
                    bank = snes >> 16;
                }

                string label;
                if ((Data.GetInOutPoint(pointer) & (Data.InOutPoint.ReadPoint)) != 0 || (tempAlias.TryGetValue(pointer, out label) && label.Length > 0))
                {
                    sw.WriteLine(GetLine(pointer, "empty"));
                }
                sw.WriteLine(GetLine(pointer, null));
                if ((Data.GetInOutPoint(pointer) & (Data.InOutPoint.EndPoint)) != 0)
                {
                    sw.WriteLine(GetLine(pointer, "empty"));
                }
                pointer += GetLineByteLength(pointer);
            }

            if (structure == FormatStructure.OneBankPerFile)
            {
                sw.Close();
                sw = new StreamWriter(string.Format("{0}/labels.asm", folder));
            }
            else
            {
                sw.WriteLine(GetLine(pointer, "empty"));
            }

            foreach (KeyValuePair <int, string> pair in Data.GetAllLabels())
            {
                if (!usedLabels.Contains(pair.Key))
                {
                    sw.WriteLine(GetLine(pair.Key, "labelassign"));
                }
            }

            if (structure == FormatStructure.OneBankPerFile)
            {
                sw.Close();
            }
            Data.Restore(a: tempAlias);
            AliasList.me.locked = false;
            return(errorCount);
        }
Beispiel #6
0
        private static byte[] SaveVersion1()
        {
            int size = Data.GetROMSize();

            byte[] romSettings = new byte[31];
            romSettings[0] = (byte)Data.GetROMMapMode();
            romSettings[1] = (byte)Data.GetROMSpeed();
            Util.IntegerIntoByteArray(size, romSettings, 2);
            for (int i = 0; i < 0x15; i++)
            {
                romSettings[6 + i] = (byte)Data.GetROMByte(Util.ConvertSNEStoPC(0xFFC0 + i));
            }
            for (int i = 0; i < 4; i++)
            {
                romSettings[27 + i] = (byte)Data.GetROMByte(Util.ConvertSNEStoPC(0xFFDC + i));
            }

            // TODO put selected offset in save file

            List <byte> label = new List <byte>(), comment = new List <byte>();
            Dictionary <int, string> all_labels = Data.GetAllLabels(), all_comments = Data.GetAllComments();

            Util.IntegerIntoByteList(all_labels.Count, label);
            foreach (KeyValuePair <int, string> pair in all_labels)
            {
                Util.IntegerIntoByteList(pair.Key, label);
                for (int i = 0; i < pair.Value.Length; i++)
                {
                    label.Add((byte)pair.Value[i]);
                }
                label.Add(0);
            }

            Util.IntegerIntoByteList(all_comments.Count, comment);
            foreach (KeyValuePair <int, string> pair in all_comments)
            {
                Util.IntegerIntoByteList(pair.Key, comment);
                for (int i = 0; i < pair.Value.Length; i++)
                {
                    comment.Add((byte)pair.Value[i]);
                }
                comment.Add(0);
            }

            byte[] romLocation = Util.StringToByteArray(currentROMFile);

            byte[] data = new byte[romSettings.Length + romLocation.Length + 8 * size + label.Count + comment.Count];
            romSettings.CopyTo(data, 0);
            for (int i = 0; i < romLocation.Length; i++)
            {
                data[romSettings.Length + i] = romLocation[i];
            }
            for (int i = 0; i < size; i++)
            {
                data[romSettings.Length + romLocation.Length + i] = (byte)Data.GetDataBank(i);
            }
            for (int i = 0; i < size; i++)
            {
                data[romSettings.Length + romLocation.Length + size + i] = (byte)Data.GetDirectPage(i);
            }
            for (int i = 0; i < size; i++)
            {
                data[romSettings.Length + romLocation.Length + 2 * size + i] = (byte)(Data.GetDirectPage(i) >> 8);
            }
            for (int i = 0; i < size; i++)
            {
                data[romSettings.Length + romLocation.Length + 3 * size + i] = (byte)(Data.GetXFlag(i) ? 1 : 0);
            }
            for (int i = 0; i < size; i++)
            {
                data[romSettings.Length + romLocation.Length + 4 * size + i] = (byte)(Data.GetMFlag(i) ? 1 : 0);
            }
            for (int i = 0; i < size; i++)
            {
                data[romSettings.Length + romLocation.Length + 5 * size + i] = (byte)Data.GetFlag(i);
            }
            for (int i = 0; i < size; i++)
            {
                data[romSettings.Length + romLocation.Length + 6 * size + i] = (byte)Data.GetArchitechture(i);
            }
            for (int i = 0; i < size; i++)
            {
                data[romSettings.Length + romLocation.Length + 7 * size + i] = (byte)Data.GetInOutPoint(i);
            }
            // ???
            label.CopyTo(data, romSettings.Length + romLocation.Length + 8 * size);
            comment.CopyTo(data, romSettings.Length + romLocation.Length + 8 * size + label.Count);
            // ???

            return(data);
        }
Beispiel #7
0
        public static int ConvertSNEStoPC(int address)
        {
            // WRAM is N/A to PC addressing
            if ((address & 0xFE0000) == 0x7E0000)
            {
                return(-1);
            }

            // WRAM mirror & PPU regs are N/A to PC addressing
            if (((address & 0x400000) == 0) && ((address & 0x8000) == 0))
            {
                return(-1);
            }

            switch (Data.GetROMMapMode())
            {
            case Data.ROMMapMode.LoROM:
            {
                // SRAM is N/A to PC addressing
                if (((address & 0x700000) == 0x700000) && ((address & 0x8000) == 0))
                {
                    return(-1);
                }

                return(UnmirroredOffset(((address & 0x7F0000) >> 1) | (address & 0x7FFF)));
            }

            case Data.ROMMapMode.HiROM:
            {
                return(UnmirroredOffset(address & 0x3FFFFF));
            }

            case Data.ROMMapMode.SuperMMC:
            {
                return(UnmirroredOffset(address & 0x3FFFFF));        // todo, treated as hirom atm
            }

            case Data.ROMMapMode.SA1ROM:
            case Data.ROMMapMode.ExSA1ROM:
            {
                // BW-RAM is N/A to PC addressing
                if (address >= 0x400000 && address <= 0x7FFFFF)
                {
                    return(-1);
                }

                if (address >= 0xC00000)
                {
                    if (Data.GetROMMapMode() == Data.ROMMapMode.ExSA1ROM)
                    {
                        return(UnmirroredOffset(address & 0x7FFFFF));
                    }
                    else
                    {
                        return(UnmirroredOffset(address & 0x3FFFFF));
                    }
                }
                else
                {
                    if (address >= 0x800000)
                    {
                        address -= 0x400000;
                    }

                    // SRAM is N/A to PC addressing
                    if (((address & 0x8000) == 0))
                    {
                        return(-1);
                    }

                    return(UnmirroredOffset(((address & 0x7F0000) >> 1) | (address & 0x7FFF)));
                }
            }

            case Data.ROMMapMode.SuperFX:
            {
                // BW-RAM is N/A to PC addressing
                if (address >= 0x600000 && address <= 0x7FFFFF)
                {
                    return(-1);
                }

                if (address < 0x400000)
                {
                    return(UnmirroredOffset(((address & 0x7F0000) >> 1) | (address & 0x7FFF)));
                }
                else if (address < 0x600000)
                {
                    return(UnmirroredOffset(address & 0x3FFFFF));
                }
                else if (address < 0xC00000)
                {
                    return(0x200000 + UnmirroredOffset(((address & 0x7F0000) >> 1) | (address & 0x7FFF)));
                }
                else
                {
                    return(0x400000 + UnmirroredOffset(address & 0x3FFFFF));
                }
            }

            case Data.ROMMapMode.ExHiROM:
            {
                return(UnmirroredOffset(((~address & 0x800000) >> 1) | (address & 0x3FFFFF)));
            }

            case Data.ROMMapMode.ExLoROM:
            {
                // SRAM is N/A to PC addressing
                if (((address & 0x700000) == 0x700000) && ((address & 0x8000) == 0))
                {
                    return(-1);
                }

                return(UnmirroredOffset((((address ^ 0x800000) & 0xFF0000) >> 1) | (address & 0x7FFF)));
            }

            default:
            {
                return(-1);
            }
            }
        }