Ejemplo n.º 1
0
        private void SyncState(Serializer ser)
        {
            byte[] core = null;
            if (ser.IsWriter)
            {
                var ms = new MemoryStream();
                ms.Close();
                core = ms.ToArray();
            }
            _cpu.SyncState(ser);

            ser.BeginSection("Coleco");
            _vdp.SyncState(ser);
            ControllerDeck.SyncState(ser);
            PSG.SyncState(ser);
            SGM_sound.SyncState(ser);
            ser.Sync("UseSGM", ref use_SGM);
            ser.Sync("is_MC", ref is_MC);
            ser.Sync("MC_bank", ref MC_bank);
            ser.Sync("EnableSGMhigh", ref enable_SGM_high);
            ser.Sync("EnableSGMlow", ref enable_SGM_low);
            ser.Sync("Port_0x53", ref port_0x53);
            ser.Sync("Port_0x7F", ref port_0x7F);
            ser.Sync("RAM", ref _ram, false);
            ser.Sync("SGM_high_RAM", ref SGM_high_RAM, false);
            ser.Sync("SGM_low_RAM", ref SGM_low_RAM, false);
            ser.Sync("Frame", ref _frame);
            ser.Sync("LagCount", ref _lagCount);
            ser.Sync("IsLag", ref _isLag);

            ser.EndSection();

            if (ser.IsReader)
            {
                SyncAllByteArrayDomains();
            }
        }
        public bool FrameAdvance(IController controller, bool render, bool renderSound)
        {
            _controller = controller;

            // NOTE: Need to research differences between reset and power cycle
            if (_controller.IsPressed("Power"))
            {
                HardReset();
            }

            if (_controller.IsPressed("Reset"))
            {
                SoftReset();
            }

            _frame++;

            _isLag = true;
            if (_tracer.Enabled)
            {
                _cpu.TraceCallback = s => _tracer.Put(s);
            }
            else
            {
                _cpu.TraceCallback = null;
            }
            byte tempRet1 = ControllerDeck.ReadPort1(controller, true, false);
            byte tempRet2 = ControllerDeck.ReadPort2(controller, true, false);

            bool intPending = false;

            // the return values represent the controller's current state, but the sampling rate is not high enough
            // to catch all changes in wheel orientation
            // so we use the wheel variable and interpolate between frames

            // first determine how many degrees the wheels changed, and how many regions have been traversed
            float change1 = (float)(((ControllerDeck.wheel1 - ControllerDeck.temp_wheel1) % 180) / 1.25);
            float change2 = (float)(((ControllerDeck.wheel2 - ControllerDeck.temp_wheel2) % 180) / 1.25);

            // special cases
            if ((ControllerDeck.temp_wheel1 > 270) && (ControllerDeck.wheel1 < 90))
            {
                change1 = (float)((ControllerDeck.wheel1 + (360 - ControllerDeck.temp_wheel1)) / 1.25);
            }

            if ((ControllerDeck.wheel1 > 270) && (ControllerDeck.temp_wheel1 < 90))
            {
                change1 = -(float)((ControllerDeck.temp_wheel1 + (360 - ControllerDeck.wheel1)) / 1.25);
            }

            if ((ControllerDeck.temp_wheel2 > 270) && (ControllerDeck.wheel2 < 90))
            {
                change2 = (float)((ControllerDeck.wheel2 + (360 - ControllerDeck.temp_wheel2)) / 1.25);
            }

            if ((ControllerDeck.wheel2 > 270) && (ControllerDeck.temp_wheel2 < 90))
            {
                change2 = -(float)((ControllerDeck.temp_wheel2 + (360 - ControllerDeck.wheel2)) / 1.25);
            }

            int changes1 = change1 > 0 ? (int)Math.Floor(change1) : (int)Math.Ceiling(change1);
            int changes2 = change2 > 0 ? (int)Math.Floor(change2) : (int)Math.Ceiling(change2);

            for (int scanLine = 0; scanLine < 262; scanLine++)
            {
                _vdp.RenderScanline(scanLine);

                if (scanLine == 192)
                {
                    _vdp.InterruptPending = true;

                    if (_vdp.EnableInterrupts)
                    {
                        _cpu.NonMaskableInterrupt = true;
                    }
                }

                for (int i = 0; i < 228; i++)
                {
                    PSG.generate_sound(1);
                    if (use_SGM)
                    {
                        SGM_sound.generate_sound(1);
                    }
                    _cpu.ExecuteOne();

                    // pick out sound samples from the sound devices twice per scanline
                    int v = PSG.Sample();

                    if (use_SGM)
                    {
                        v += SGM_sound.Sample();
                    }

                    if (v != _latchedSample)
                    {
                        _blip.AddDelta((uint)_sampleClock, v - _latchedSample);
                        _latchedSample = v;
                    }

                    _sampleClock++;
                }

                // starting from scanline 20, changes to the wheel are added once per scanline (up to 144)
                if (scanLine > 20)
                {
                    if (changes1 != 0)
                    {
                        if (changes1 > 0)
                        {
                            ControllerDeck.temp_wheel1 = (float)((ControllerDeck.temp_wheel1 + 1.25) % 360);
                            changes1--;
                        }
                        else
                        {
                            ControllerDeck.temp_wheel1 = (float)((ControllerDeck.temp_wheel1 - 1.25) % 360);
                            changes1++;
                        }
                    }

                    if (changes2 != 0)
                    {
                        if (changes2 > 0)
                        {
                            ControllerDeck.temp_wheel2 = (float)((ControllerDeck.temp_wheel2 + 1.25) % 360);
                            changes2--;
                        }
                        else
                        {
                            ControllerDeck.temp_wheel2 = (float)((ControllerDeck.temp_wheel2 - 1.25) % 360);
                            changes2++;
                        }
                    }
                }

                tempRet1 = ControllerDeck.ReadPort1(controller, true, true);
                tempRet2 = ControllerDeck.ReadPort2(controller, true, true);

                intPending = (!tempRet1.Bit(4) && temp_1_prev) | (!tempRet2.Bit(4) && temp_2_prev);

                _cpu.FlagI = false;
                if (intPending)
                {
                    _cpu.FlagI = true;
                    intPending = false;
                }

                temp_1_prev = tempRet1.Bit(4);
                temp_2_prev = tempRet2.Bit(4);
            }

            ControllerDeck.temp_wheel1 = ControllerDeck.wheel1;
            ControllerDeck.temp_wheel2 = ControllerDeck.wheel2;

            if (_isLag)
            {
                _lagCount++;
            }

            return(true);
        }
Ejemplo n.º 3
0
        private void SyncState(Serializer ser)
        {
            int version = 4;

            ser.BeginSection(nameof(NES));
            ser.Sync(nameof(version), ref version);
            ser.Sync("Frame", ref _frame);
            ser.Sync("Lag", ref _lagcount);
            ser.Sync("IsLag", ref islag);
            cpu.SyncState(ser);
            ser.Sync(nameof(ram), ref ram, false);
            ser.Sync(nameof(CIRAM), ref CIRAM, false);
            ser.Sync(nameof(_irq_apu), ref _irq_apu);
            ser.Sync(nameof(sprdma_countdown), ref sprdma_countdown);
            ser.Sync(nameof(cpu_deadcounter), ref cpu_deadcounter);

            ser.Sync(nameof(old_s), ref old_s);

            // OAM related
            ser.Sync(nameof(oam_dma_index), ref oam_dma_index);
            ser.Sync(nameof(oam_dma_exec), ref oam_dma_exec);
            ser.Sync(nameof(oam_dma_addr), ref oam_dma_addr);
            ser.Sync(nameof(oam_dma_byte), ref oam_dma_byte);
            ser.Sync(nameof(dmc_dma_exec), ref dmc_dma_exec);
            ser.Sync(nameof(dmc_realign), ref dmc_realign);
            ser.Sync(nameof(reread_trigger), ref reread_trigger);
            ser.Sync(nameof(do_the_reread_2002), ref do_the_reread_2002);
            ser.Sync(nameof(do_the_reread_2007), ref do_the_reread_2007);
            ser.Sync(nameof(do_the_reread_cont_1), ref do_the_reread_cont_1);
            ser.Sync(nameof(do_the_reread_cont_2), ref do_the_reread_cont_2);
            ser.Sync(nameof(reread_opp_4016), ref reread_opp_4016);
            ser.Sync(nameof(reread_opp_4017), ref reread_opp_4017);

            // VS related
            ser.Sync("VS", ref _isVS);
            ser.Sync("VS_2c05", ref _isVS2c05);
            ser.Sync("VS_CHR", ref VS_chr_reg);
            ser.Sync("VS_PRG", ref VS_prg_reg);
            ser.Sync("VS_DIPS", ref VS_dips, false);
            ser.Sync("VS_Service", ref VS_service);
            ser.Sync("VS_Coin", ref VS_coin_inserted);
            ser.Sync("VS_ROM_Control", ref VS_ROM_control);

            // single cycle execution related
            ser.Sync(nameof(current_strobe), ref current_strobe);
            ser.Sync(nameof(new_strobe), ref new_strobe);

            ser.BeginSection(nameof(Board));
            Board.SyncState(ser);
            if (Board is NesBoardBase board && !board.SyncStateFlag)
            {
                throw new InvalidOperationException($"the current NES mapper didn't call base.{nameof(INesBoard.SyncState)}");
            }

            ser.EndSection();
            ppu.SyncState(ser);
            apu.SyncState(ser);

            if (version >= 2)
            {
                ser.Sync(nameof(DB), ref DB);
            }

            if (version >= 3)
            {
                ser.Sync(nameof(latched4016), ref latched4016);
                ser.BeginSection(nameof(ControllerDeck));
                ControllerDeck.SyncState(ser);
                ser.EndSection();
            }

            if (version >= 4)
            {
                ser.Sync(nameof(resetSignal), ref resetSignal);
                ser.Sync(nameof(hardResetSignal), ref hardResetSignal);
            }

            ser.EndSection();
        }
Ejemplo n.º 4
0
        private void SyncState(Serializer ser)
        {
            int version = 4;

            ser.BeginSection("NES");
            ser.Sync("version", ref version);
            ser.Sync("Frame", ref _frame);
            ser.Sync("Lag", ref _lagcount);
            ser.Sync("IsLag", ref islag);
            cpu.SyncState(ser);
            ser.Sync("ram", ref ram, false);
            ser.Sync("CIRAM", ref CIRAM, false);
            ser.Sync("_irq_apu", ref _irq_apu);
            ser.Sync("sprdma_countdown", ref sprdma_countdown);
            ser.Sync("cpu_deadcounter", ref cpu_deadcounter);

            //oam related
            ser.Sync("Oam_Dma_Index", ref oam_dma_index);
            ser.Sync("Oam_Dma_Exec", ref oam_dma_exec);
            ser.Sync("Oam_Dma_Addr", ref oam_dma_addr);
            ser.Sync("Oam_Dma_Byte", ref oam_dma_byte);
            ser.Sync("Dmc_Dma_Exec", ref dmc_dma_exec);
            ser.Sync("dmc_realign", ref dmc_realign);
            ser.Sync("IRQ_delay", ref IRQ_delay);
            ser.Sync("special_case_delay", ref special_case_delay);
            ser.Sync("do_the_reread", ref do_the_reread);

            //VS related
            ser.Sync("VS", ref _isVS);
            ser.Sync("VS_2c05", ref _isVS2c05);
            ser.Sync("VS_CHR", ref VS_chr_reg);
            ser.Sync("VS_PRG", ref VS_prg_reg);
            ser.Sync("VS_DIPS", ref VS_dips, false);
            ser.Sync("VS_Service", ref VS_service);
            ser.Sync("VS_Coin", ref VS_coin_inserted);
            ser.Sync("VS_ROM_Control", ref VS_ROM_control);

            ser.BeginSection("Board");
            Board.SyncState(ser);
            if (Board is NESBoardBase && !((NESBoardBase)Board).SyncStateFlag)
            {
                throw new InvalidOperationException("the current NES mapper didnt call base.SyncState");
            }
            ser.EndSection();
            ppu.SyncState(ser);
            apu.SyncState(ser);

            if (version >= 2)
            {
                ser.Sync("DB", ref DB);
            }
            if (version >= 3)
            {
                ser.Sync("latched4016", ref latched4016);
                ser.BeginSection("ControllerDeck");
                ControllerDeck.SyncState(ser);
                ser.EndSection();
            }
            if (version >= 4)
            {
                ser.Sync("resetSignal", ref resetSignal);
                ser.Sync("hardResetSignal", ref hardResetSignal);
            }

            ser.EndSection();
        }