Example #1
0
        /// <summary>
        /// Load data base from file
        /// </summary>
        /// <param name="fileName">The full path of database file</param>
        public static void LoadDatabase(string fileName)
        {
            if (!File.Exists(fileName))
                return;
            //1 clear the database
            _databaseRoms.Clear();
            //2 read the xml file
            Stream databaseStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            XmlReaderSettings sett = new XmlReaderSettings();
            sett.DtdProcessing = DtdProcessing.Ignore;
            sett.IgnoreWhitespace = true;
            XmlReader XMLread = XmlReader.Create(databaseStream, sett);

            NesCartDatabaseGameInfo drom = new NesCartDatabaseGameInfo();
            // To avoid nulls ..
            drom.Cartridges = new List<NesCartDatabaseCartridgeInfo>();
            drom.Game_AltName = "";
            drom.Game_Catalog = "";
            drom.Game_Class = "";
            drom.Game_Developer = "";
            drom.Game_Name = "";
            drom.Game_Players = "";
            drom.Game_Publisher = "";
            drom.Game_Region = "";
            drom.Game_ReleaseDate = "";

            while (XMLread.Read())
            {
                //database Game info
                if (XMLread.Name == "xml" & XMLread.IsStartElement())
                {
                    if (XMLread.MoveToAttribute("version"))
                        DBVersion = XMLread.Value;
                    if (XMLread.MoveToAttribute("conformance"))
                        DBConformance = XMLread.Value;
                    if (XMLread.MoveToAttribute("agent"))
                        DBAgent = XMLread.Value;
                    if (XMLread.MoveToAttribute("author"))
                        DBAuthor = XMLread.Value;
                    if (XMLread.MoveToAttribute("timestamp"))
                        DBTimeStamp = XMLread.Value;
                }
                //Is it a game ?
                else if (XMLread.Name == "game" & XMLread.IsStartElement())
                {
                    drom = new NesCartDatabaseGameInfo();

                    if (XMLread.MoveToAttribute("name"))
                        drom.Game_Name = XMLread.Value;
                    if (XMLread.MoveToAttribute("altname"))
                        drom.Game_AltName = XMLread.Value;
                    if (XMLread.MoveToAttribute("class"))
                        drom.Game_Class = XMLread.Value;
                    if (XMLread.MoveToAttribute("catalog"))
                        drom.Game_Catalog = XMLread.Value;
                    if (XMLread.MoveToAttribute("publisher"))
                        drom.Game_Publisher = XMLread.Value;
                    if (XMLread.MoveToAttribute("developer"))
                        drom.Game_Developer = XMLread.Value;
                    if (XMLread.MoveToAttribute("region"))
                        drom.Game_Region = XMLread.Value;
                    if (XMLread.MoveToAttribute("players"))
                        drom.Game_Players = XMLread.Value;
                    if (XMLread.MoveToAttribute("date"))
                        drom.Game_ReleaseDate = XMLread.Value;

                    NesCartDatabaseCartridgeInfo crt = new NesCartDatabaseCartridgeInfo();
                    crt.PAD_h = "";
                    crt.PAD_v = "";
                    crt.PRG_crc = "";
                    crt.PRG_name = "";
                    crt.PRG_sha1 = "";
                    crt.PRG_size = "";
                    crt.chip_type = new List<string>();
                    crt.CHR_crc = "";
                    crt.CHR_name = "";
                    crt.CHR_sha1 = "";
                    crt.CHR_size = "";
                    crt.CIC_type = "";
                    crt.Board_Mapper = "";
                    crt.Board_Pcb = "";
                    crt.Board_Type = "";
                    crt.VRAM_sizes = new List<string>();
                    crt.WRAMBanks = new List<SRAMBankInfo>();
                    // Load carts
                    while (XMLread.Read())
                    {
                        //End of game info ?
                        if (XMLread.Name == "game" & !XMLread.IsStartElement())
                        {
                            _databaseRoms.Add(drom);
                            break;
                        }
                        //cartridge info
                        if (XMLread.Name == "cartridge" & XMLread.IsStartElement())
                        {
                            if (drom.Cartridges == null)
                                drom.Cartridges = new List<NesCartDatabaseCartridgeInfo>();
                            crt = new NesCartDatabaseCartridgeInfo();
                            crt.PAD_h = "";
                            crt.PAD_v = "";
                            crt.PRG_crc = "";
                            crt.PRG_name = "";
                            crt.PRG_sha1 = "";
                            crt.PRG_size = "";
                            crt.chip_type = new List<string>();
                            crt.CHR_crc = "";
                            crt.CHR_name = "";
                            crt.CHR_sha1 = "";
                            crt.CHR_size = "";
                            crt.CIC_type = "";
                            crt.Board_Mapper = "";
                            crt.Board_Pcb = "";
                            crt.Board_Type = "";
                            crt.VRAM_sizes = new List<string>();
                            crt.WRAMBanks = new List<SRAMBankInfo>();
                            if (XMLread.MoveToAttribute("system"))
                                crt.System = XMLread.Value;
                            if (XMLread.MoveToAttribute("crc"))
                                crt.CRC = XMLread.Value;
                            if (XMLread.MoveToAttribute("sha1"))
                                crt.SHA1 = XMLread.Value;
                            if (XMLread.MoveToAttribute("dump"))
                                crt.Dump = XMLread.Value;
                            if (XMLread.MoveToAttribute("dumper"))
                                crt.Dumper = XMLread.Value;
                            if (XMLread.MoveToAttribute("datedumped"))
                                crt.DateDumped = XMLread.Value;
                        }
                        else if (XMLread.Name == "cartridge" & !XMLread.IsStartElement())
                        {
                            drom.Cartridges.Add(crt); continue;
                        }
                        //board info
                        else if (XMLread.Name == "board" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("type"))
                                crt.Board_Type  = XMLread.Value;
                            if (XMLread.MoveToAttribute("pcb"))
                                crt.Board_Pcb = XMLread.Value;
                            if (XMLread.MoveToAttribute("mapper"))
                                crt.Board_Mapper = XMLread.Value;
                        }
                        //prg info
                        else if (XMLread.Name == "prg" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("name"))
                                crt.PRG_name = XMLread.Value;
                            if (XMLread.MoveToAttribute("size"))
                                crt.PRG_size = XMLread.Value;
                            if (XMLread.MoveToAttribute("crc"))
                                crt.PRG_crc = XMLread.Value;
                            if (XMLread.MoveToAttribute("sha1"))
                                crt.PRG_sha1 = XMLread.Value;
                        }
                        //chr info
                        else if (XMLread.Name == "chr" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("name"))
                                crt.CHR_name = XMLread.Value;
                            if (XMLread.MoveToAttribute("size"))
                                crt.CHR_size = XMLread.Value;
                            if (XMLread.MoveToAttribute("crc"))
                                crt.CHR_crc = XMLread.Value;
                            if (XMLread.MoveToAttribute("sha1"))
                                crt.CHR_sha1 = XMLread.Value;
                        }
                        //vram info
                        else if (XMLread.Name == "vram" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("size"))
                                crt.VRAM_sizes.Add(XMLread.Value);
                        }
                        //wram info
                        else if (XMLread.Name == "wram" & XMLread.IsStartElement())
                        {
                            string wsize = "";
                            bool battery = false;
                            int id = 0;

                            if (XMLread.MoveToAttribute("size"))
                                wsize = XMLread.Value;
                            if (XMLread.MoveToAttribute("battery"))
                                battery = XMLread.Value == "1";
                            if (XMLread.MoveToAttribute("id"))
                                int.TryParse(XMLread.Value, out id);
                            crt.WRAMBanks.Add(new SRAMBankInfo(id, wsize, battery));
                        }
                        //chip info
                        else if (XMLread.Name == "chip" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("type"))
                            {
                                if (crt.chip_type == null)
                                    crt.chip_type = new List<string>();
                                crt.chip_type.Add(XMLread.Value);
                            }
                        }
                        //cic info
                        else if (XMLread.Name == "cic" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("type"))
                                crt.CIC_type = XMLread.Value;
                        }
                        //pad info
                        else if (XMLread.Name == "pad" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("h"))
                                crt.PAD_h = XMLread.Value;
                            if (XMLread.MoveToAttribute("v"))
                                crt.PAD_v = XMLread.Value;
                        }
                    }
                }
            }
            XMLread.Close();
            databaseStream.Close();
        }
Example #2
0
        /// <summary>
        /// Load data base from file
        /// </summary>
        /// <param name="fileName">The full path of database file</param>
        public static void LoadDatabase(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return;
            }
            //1 clear the database
            _databaseRoms.Clear();
            //2 read the xml file
            Stream databaseStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            XmlReaderSettings sett = new XmlReaderSettings();

            sett.DtdProcessing    = DtdProcessing.Ignore;
            sett.IgnoreWhitespace = true;
            XmlReader XMLread = XmlReader.Create(databaseStream, sett);

            NesCartDatabaseGameInfo drom = new NesCartDatabaseGameInfo();

            // To avoid nulls ..
            drom.Cartridges       = new List <NesCartDatabaseCartridgeInfo>();
            drom.Game_AltName     = "";
            drom.Game_Catalog     = "";
            drom.Game_Class       = "";
            drom.Game_Developer   = "";
            drom.Game_Name        = "";
            drom.Game_Players     = "";
            drom.Game_Publisher   = "";
            drom.Game_Region      = "";
            drom.Game_ReleaseDate = "";

            while (XMLread.Read())
            {
                //database Game info
                if (XMLread.Name == "xml" & XMLread.IsStartElement())
                {
                    if (XMLread.MoveToAttribute("version"))
                    {
                        DBVersion = XMLread.Value;
                    }
                    if (XMLread.MoveToAttribute("conformance"))
                    {
                        DBConformance = XMLread.Value;
                    }
                    if (XMLread.MoveToAttribute("agent"))
                    {
                        DBAgent = XMLread.Value;
                    }
                    if (XMLread.MoveToAttribute("author"))
                    {
                        DBAuthor = XMLread.Value;
                    }
                    if (XMLread.MoveToAttribute("timestamp"))
                    {
                        DBTimeStamp = XMLread.Value;
                    }
                }
                //Is it a game ?
                else if (XMLread.Name == "game" & XMLread.IsStartElement())
                {
                    drom = new NesCartDatabaseGameInfo();

                    if (XMLread.MoveToAttribute("name"))
                    {
                        drom.Game_Name = XMLread.Value;
                    }
                    if (XMLread.MoveToAttribute("altname"))
                    {
                        drom.Game_AltName = XMLread.Value;
                    }
                    if (XMLread.MoveToAttribute("class"))
                    {
                        drom.Game_Class = XMLread.Value;
                    }
                    if (XMLread.MoveToAttribute("catalog"))
                    {
                        drom.Game_Catalog = XMLread.Value;
                    }
                    if (XMLread.MoveToAttribute("publisher"))
                    {
                        drom.Game_Publisher = XMLread.Value;
                    }
                    if (XMLread.MoveToAttribute("developer"))
                    {
                        drom.Game_Developer = XMLread.Value;
                    }
                    if (XMLread.MoveToAttribute("region"))
                    {
                        drom.Game_Region = XMLread.Value;
                    }
                    if (XMLread.MoveToAttribute("players"))
                    {
                        drom.Game_Players = XMLread.Value;
                    }
                    if (XMLread.MoveToAttribute("date"))
                    {
                        drom.Game_ReleaseDate = XMLread.Value;
                    }

                    NesCartDatabaseCartridgeInfo crt = new NesCartDatabaseCartridgeInfo();
                    crt.PAD_h        = "";
                    crt.PAD_v        = "";
                    crt.PRG_crc      = "";
                    crt.PRG_name     = "";
                    crt.PRG_sha1     = "";
                    crt.PRG_size     = "";
                    crt.chip_type    = new List <string>();
                    crt.CHR_crc      = "";
                    crt.CHR_name     = "";
                    crt.CHR_sha1     = "";
                    crt.CHR_size     = "";
                    crt.CIC_type     = "";
                    crt.Board_Mapper = "";
                    crt.Board_Pcb    = "";
                    crt.Board_Type   = "";
                    crt.VRAM_sizes   = new List <string>();
                    crt.WRAMBanks    = new List <SRAMBankInfo>();
                    // Load carts
                    while (XMLread.Read())
                    {
                        //End of game info ?
                        if (XMLread.Name == "game" & !XMLread.IsStartElement())
                        {
                            _databaseRoms.Add(drom);
                            break;
                        }
                        //cartridge info
                        if (XMLread.Name == "cartridge" & XMLread.IsStartElement())
                        {
                            if (drom.Cartridges == null)
                            {
                                drom.Cartridges = new List <NesCartDatabaseCartridgeInfo>();
                            }
                            crt              = new NesCartDatabaseCartridgeInfo();
                            crt.PAD_h        = "";
                            crt.PAD_v        = "";
                            crt.PRG_crc      = "";
                            crt.PRG_name     = "";
                            crt.PRG_sha1     = "";
                            crt.PRG_size     = "";
                            crt.chip_type    = new List <string>();
                            crt.CHR_crc      = "";
                            crt.CHR_name     = "";
                            crt.CHR_sha1     = "";
                            crt.CHR_size     = "";
                            crt.CIC_type     = "";
                            crt.Board_Mapper = "";
                            crt.Board_Pcb    = "";
                            crt.Board_Type   = "";
                            crt.VRAM_sizes   = new List <string>();
                            crt.WRAMBanks    = new List <SRAMBankInfo>();
                            if (XMLread.MoveToAttribute("system"))
                            {
                                crt.System = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("crc"))
                            {
                                crt.CRC = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("sha1"))
                            {
                                crt.SHA1 = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("dump"))
                            {
                                crt.Dump = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("dumper"))
                            {
                                crt.Dumper = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("datedumped"))
                            {
                                crt.DateDumped = XMLread.Value;
                            }
                        }
                        else if (XMLread.Name == "cartridge" & !XMLread.IsStartElement())
                        {
                            drom.Cartridges.Add(crt); continue;
                        }
                        //board info
                        else if (XMLread.Name == "board" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("type"))
                            {
                                crt.Board_Type = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("pcb"))
                            {
                                crt.Board_Pcb = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("mapper"))
                            {
                                crt.Board_Mapper = XMLread.Value;
                            }
                        }
                        //prg info
                        else if (XMLread.Name == "prg" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("name"))
                            {
                                crt.PRG_name = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("size"))
                            {
                                crt.PRG_size = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("crc"))
                            {
                                crt.PRG_crc = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("sha1"))
                            {
                                crt.PRG_sha1 = XMLread.Value;
                            }
                        }
                        //chr info
                        else if (XMLread.Name == "chr" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("name"))
                            {
                                crt.CHR_name = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("size"))
                            {
                                crt.CHR_size = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("crc"))
                            {
                                crt.CHR_crc = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("sha1"))
                            {
                                crt.CHR_sha1 = XMLread.Value;
                            }
                        }
                        //vram info
                        else if (XMLread.Name == "vram" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("size"))
                            {
                                crt.VRAM_sizes.Add(XMLread.Value);
                            }
                        }
                        //wram info
                        else if (XMLread.Name == "wram" & XMLread.IsStartElement())
                        {
                            string wsize   = "";
                            bool   battery = false;
                            int    id      = 0;

                            if (XMLread.MoveToAttribute("size"))
                            {
                                wsize = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("battery"))
                            {
                                battery = XMLread.Value == "1";
                            }
                            if (XMLread.MoveToAttribute("id"))
                            {
                                int.TryParse(XMLread.Value, out id);
                            }
                            crt.WRAMBanks.Add(new SRAMBankInfo(id, wsize, battery));
                        }
                        //chip info
                        else if (XMLread.Name == "chip" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("type"))
                            {
                                if (crt.chip_type == null)
                                {
                                    crt.chip_type = new List <string>();
                                }
                                crt.chip_type.Add(XMLread.Value);
                            }
                        }
                        //cic info
                        else if (XMLread.Name == "cic" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("type"))
                            {
                                crt.CIC_type = XMLread.Value;
                            }
                        }
                        //pad info
                        else if (XMLread.Name == "pad" & XMLread.IsStartElement())
                        {
                            if (XMLread.MoveToAttribute("h"))
                            {
                                crt.PAD_h = XMLread.Value;
                            }
                            if (XMLread.MoveToAttribute("v"))
                            {
                                crt.PAD_v = XMLread.Value;
                            }
                        }
                    }
                }
            }
            XMLread.Close();
            databaseStream.Close();
        }
Example #3
0
 /// <summary>
 /// Initialize the board
 /// </summary>
 /// <param name="sha1">The rom sha1 calculated without the header</param>
 /// <param name="prg_dump">The prg data dump.</param>
 /// <param name="chr_dump">The chr data dump</param>
 /// <param name="trainer_dump">The trainer data dump.</param>
 /// <param name="defaultMirroring">The default mirroring as defined in rom.</param>
 public virtual void Initialize(string sha1, byte[] prg_dump, byte[] chr_dump, byte[] trainer_dump, Mirroring defaultMirroring)
 {
     RomSHA1 = sha1;
     // Find on DB
     GameInfo = NesCartDatabase.Find(sha1, out IsGameFoundOnDB);
     //set cart info
     if (GameInfo.Cartridges != null)
     {
         foreach (NesCartDatabaseCartridgeInfo cartinf in GameInfo.Cartridges)
             if (cartinf.SHA1.ToLower() == sha1.ToLower())
             {
                 GameCartInfo = cartinf;
                 break;
             }
     }
     BoardType = "N/A";
     BoardPCB = "N/A";
     this.Chips = new List<string>();
     if (IsGameFoundOnDB)
     {
         Console.WriteLine("Game found in Database !!");
         Console.WriteLine("> Game name: " + GameInfo.Game_Name);
         Console.WriteLine("> Game alt name: " + GameInfo.Game_AltName);
         BoardType = GameCartInfo.Board_Type;
         Console.WriteLine("> Board Type: " + BoardType);
         BoardPCB = GameCartInfo.Board_Pcb;
         Console.WriteLine("> Board Pcb: " + BoardPCB);
         // Chips ... important for some boards
         if (GameCartInfo.chip_type != null)
             for (int i = 0; i < GameCartInfo.chip_type.Count; i++)
             {
                 Console.WriteLine(string.Format("> CHIP {0}: {1}", (i + 1).ToString(),
                    GameCartInfo.chip_type[i]));
                 this.Chips.Add(GameCartInfo.chip_type[i]);
             }
     }
     // Mirroring
     this.default_mirroring = defaultMirroring;
     nmt_banks = new byte[4][]
     {
         new byte[0x0400], new byte[0x0400], new byte[0x0400], new byte[0x0400]
         /*Only 2 nmt banks should be used in normal state*/
     };
     nmt_indexes = new int[4];
     SwitchNMT(defaultMirroring);
     // PRG data ***********************************************
     List<BankInfo> prgBanks = new List<BankInfo>(GetPRGRAM());
     prg_rom_bank_offset = prgBanks.Count;
     prgBanks.AddRange(GetPRGROM(prg_dump));
     SRAMSaveRequired = false;
     prg_banks = new byte[prgBanks.Count][];
     prg_enable = new bool[prgBanks.Count];
     prg_isram = new bool[prgBanks.Count];
     prg_writable = new bool[prgBanks.Count];
     prg_battery = new bool[prgBanks.Count];
     for (int i = 0; i < prgBanks.Count; i++)
     {
         prg_banks[i] = prgBanks[i].DATA;
         prg_enable[i] = prgBanks[i].Enabled;
         prg_writable[i] = prgBanks[i].Writable;
         prg_isram[i] = prgBanks[i].IsRAM;
         prg_battery[i] = prgBanks[i].IsBattery;
         if (!SRAMSaveRequired)
         {
             if (prg_battery[i])
                 SRAMSaveRequired = true;
         }
     }
     prg_indexes = new int[5];
     // ********************************************************
     // Trainer ************************************************
     // Put trainer at first ram bank.
     if (trainer_dump != null)
     {
         if (trainer_dump.Length > 0)
         {
             for (int i = 0; i < prgBanks.Count; i++)
             {
                 if (prg_isram[i])
                 {
                     trainer_dump.CopyTo(prg_banks[i], 0x1000);
                     break;
                 }
             }
         }
     }
     // ********************************************************
     // CHR data ***********************************************
     List<BankInfo> chrBanks = new List<BankInfo>(GetCHRRAM());
     chr_rom_bank_offset = chrBanks.Count;
     chrBanks.AddRange(GetCHRROM(chr_dump));
     chr_banks = new byte[chrBanks.Count][];
     chr_enable = new bool[chrBanks.Count];
     chr_isram = new bool[chrBanks.Count];
     chr_writable = new bool[chrBanks.Count];
     for (int i = 0; i < chrBanks.Count; i++)
     {
         chr_banks[i] = chrBanks[i].DATA;
         chr_enable[i] = chrBanks[i].Enabled;
         chr_writable[i] = chrBanks[i].Writable;
         chr_isram[i] = chrBanks[i].IsRAM;
     }
     chr_indexes = new int[8];
     // ********************************************************
 }