Exemple #1
0
        public GBHawk(CoreComm comm, GameInfo game, byte[] rom, /*string gameDbFn,*/ object settings, object syncSettings)
        {
            var ser = new BasicServiceProvider(this);

            cpu = new LR35902
            {
                ReadMemory      = ReadMemory,
                WriteMemory     = WriteMemory,
                PeekMemory      = PeekMemory,
                DummyReadMemory = ReadMemory,
                OnExecFetch     = ExecFetch,
                SpeedFunc       = SpeedFunc,
                GetIntRegs      = GetIntRegs,
                SetIntRegs      = SetIntRegs
            };

            timer      = new Timer();
            audio      = new Audio();
            serialport = new SerialPort();

            _settings       = (GBSettings)settings ?? new GBSettings();
            _syncSettings   = (GBSyncSettings)syncSettings ?? new GBSyncSettings();
            _controllerDeck = new GBHawkControllerDeck(_syncSettings.Port1);

            byte[] Bios = null;

            // Load up a BIOS and initialize the correct PPU
            if (_syncSettings.ConsoleMode == GBSyncSettings.ConsoleModeType.Auto)
            {
                if (game.System == "GB")
                {
                    Bios = comm.CoreFileProvider.GetFirmware("GB", "World", true, "BIOS Not Found, Cannot Load");
                    ppu  = new GB_PPU();
                }
                else
                {
                    Bios   = comm.CoreFileProvider.GetFirmware("GBC", "World", true, "BIOS Not Found, Cannot Load");
                    ppu    = new GBC_PPU();
                    is_GBC = true;
                }
            }
            else if (_syncSettings.ConsoleMode == GBSyncSettings.ConsoleModeType.GB)
            {
                Bios = comm.CoreFileProvider.GetFirmware("GB", "World", true, "BIOS Not Found, Cannot Load");
                ppu  = new GB_PPU();
            }
            else
            {
                Bios   = comm.CoreFileProvider.GetFirmware("GBC", "World", true, "BIOS Not Found, Cannot Load");
                ppu    = new GBC_PPU();
                is_GBC = true;
            }

            if (Bios == null)
            {
                throw new MissingFirmwareException("Missing Gamboy Bios");
            }

            _bios = Bios;

            // set up IR register to off state
            if (is_GBC)
            {
                IR_mask = 0; IR_reg = 0x3E; IR_receive = 2; IR_self = 2; IR_signal = 2;
            }

            // Here we modify the BIOS if GBA mode is set (credit to ExtraTricky)
            if (is_GBC && _syncSettings.GBACGB)
            {
                for (int i = 0; i < 13; i++)
                {
                    _bios[i + 0xF3] = (byte)((GBA_override[i] + _bios[i + 0xF3]) & 0xFF);
                }
                IR_mask = 2;
            }

            // CPU needs to know about GBC status too
            cpu.is_GBC = is_GBC;

            Buffer.BlockCopy(rom, 0x100, header, 0, 0x50);

            if (is_GBC && ((header[0x43] != 0x80) && (header[0x43] != 0xC0)))
            {
                ppu = new GBC_GB_PPU();
            }

            Console.WriteLine("MD5: " + rom.HashMD5(0, rom.Length));
            Console.WriteLine("SHA1: " + rom.HashSHA1(0, rom.Length));
            _rom = rom;
            Setup_Mapper();
            if (cart_RAM != null)
            {
                cart_RAM_vbls = new byte[cart_RAM.Length];
            }

            timer.Core      = this;
            audio.Core      = this;
            ppu.Core        = this;
            serialport.Core = this;

            ser.Register <IVideoProvider>(this);
            ser.Register <ISoundProvider>(audio);
            ServiceProvider = ser;

            _settings     = (GBSettings)settings ?? new GBSettings();
            _syncSettings = (GBSyncSettings)syncSettings ?? new GBSyncSettings();

            _tracer = new TraceBuffer {
                Header = cpu.TraceHeader
            };
            ser.Register <ITraceable>(_tracer);
            ser.Register <IStatable>(new StateSerializer(SyncState, false));
            SetupMemoryDomains();
            cpu.SetCallbacks(ReadMemory, PeekMemory, PeekMemory, WriteMemory);
            HardReset();

            iptr0 = Marshal.AllocHGlobal(VRAM.Length + 1);
            iptr1 = Marshal.AllocHGlobal(OAM.Length + 1);
            iptr2 = Marshal.AllocHGlobal(ppu.color_palette.Length * 8 * 8 + 1);
            iptr3 = Marshal.AllocHGlobal(ppu.color_palette.Length * 8 * 8 + 1);

            _scanlineCallback = null;
        }