Exemple #1
0
        public Sameboy(CoreComm comm, GameInfo game, byte[] file, SameboySettings settings, SameboySyncSettings syncSettings, bool deterministic)
        {
            _serviceProvider = new BasicServiceProvider(this);

            _settings     = settings ?? new SameboySettings();
            _syncSettings = syncSettings ?? new SameboySyncSettings();

            var model = _syncSettings.ConsoleMode;

            if (model is SameboySyncSettings.GBModel.Auto)
            {
                model = game.System == VSystemID.Raw.GBC
                                        ? SameboySyncSettings.GBModel.GB_MODEL_CGB_E
                                        : SameboySyncSettings.GBModel.GB_MODEL_DMG_B;
            }

            IsCgb = model >= SameboySyncSettings.GBModel.GB_MODEL_CGB_0;

            byte[] bios;
            if (_syncSettings.EnableBIOS)
            {
                FirmwareID fwid = new(
                    IsCgb ? "GBC" : "GB",
                    _syncSettings.ConsoleMode is SameboySyncSettings.GBModel.GB_MODEL_AGB
                                        ? "AGB"
                                        : "World");
                bios = comm.CoreFileProvider.GetFirmwareOrThrow(fwid, "BIOS Not Found, Cannot Load.  Change SyncSettings to run without BIOS.");
            }
            else
            {
                bios = Util.DecompressGzipFile(new MemoryStream(IsCgb
                                        ? _syncSettings.ConsoleMode is SameboySyncSettings.GBModel.GB_MODEL_AGB ? Resources.SameboyAgbBoot.Value : Resources.SameboyCgbBoot.Value
                                        : Resources.SameboyDmgBoot.Value));
            }

            DeterministicEmulation = false;

            bool realtime = true;

            if (!_syncSettings.UseRealTime || deterministic)
            {
                realtime = false;
                DeterministicEmulation = true;
            }

            SameboyState = LibSameboy.sameboy_create(file, file.Length, bios, bios.Length, model, realtime);

            InitMemoryDomains();
            InitMemoryCallbacks();

            _samplecb = QueueSample;
            LibSameboy.sameboy_setsamplecallback(SameboyState, _samplecb);
            _inputcb = InputCallback;
            LibSameboy.sameboy_setinputcallback(SameboyState, _inputcb);
            _tracecb = MakeTrace;
            LibSameboy.sameboy_settracecallback(SameboyState, null);

            LibSameboy.sameboy_setscanlinecallback(SameboyState, null, 0);
            LibSameboy.sameboy_setprintercallback(SameboyState, null);

            const string TRACE_HEADER = "SM83: PC, opcode, registers (A, F, B, C, D, E, H, L, SP, LY, CY)";

            Tracer = new TraceBuffer(TRACE_HEADER);
            _serviceProvider.Register(Tracer);

            _disassembler = new Gameboy.GBDisassembler();
            _serviceProvider.Register <IDisassemblable>(_disassembler);

            PutSettings(_settings);

            _stateBuf = new byte[LibSameboy.sameboy_statelen(SameboyState)];

            RomDetails = $"{game.Name}\r\n{SHA1Checksum.ComputePrefixedHex(file)}\r\n{MD5Checksum.ComputePrefixedHex(file)}\r\n";
            BoardName  = MapperName(file);

            _hasAcc = BoardName is "MBC7 ROM+ACCEL+EEPROM";
            ControllerDefinition = Gameboy.Gameboy.CreateControllerDefinition(sgb: false, sub: false, tilt: _hasAcc);

            LibSameboy.sameboy_setrtcdivisoroffset(SameboyState, _syncSettings.RTCDivisorOffset);
            CycleCount = 0;
        }