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);
        }
Exemplo n.º 2
0
        public bool FrameAdvance(IController controller, bool render, bool rendersound)
        {
            _controller = controller;
            _lagged     = true;
            _frame++;

            if (!IsGameGear)
            {
                PSG.Set_Panning(Settings.ForceStereoSeparation ? ForceStereoByte : (byte)0xFF);
            }

            if (Tracer.Enabled)
            {
                Cpu.TraceCallback = s => Tracer.Put(s);
            }
            else
            {
                Cpu.TraceCallback = null;
            }

            if (IsGameGear_C == false)
            {
                Cpu.NonMaskableInterrupt = controller.IsPressed("Pause");
            }
            else if (!IsGameGear && IsGameGear_C)
            {
                Cpu.NonMaskableInterrupt = controller.IsPressed("P1 Start");
            }

            if (IsGame3D && Settings.Fix3D)
            {
                render = ((Frame & 1) == 0) & render;
            }

            int scanlinesPerFrame = Vdp.DisplayType == DisplayType.NTSC ? 262 : 313;

            Vdp.SpriteLimit = Settings.SpriteLimit;
            for (int i = 0; i < scanlinesPerFrame; i++)
            {
                Vdp.ScanLine = i;

                Vdp.RenderCurrentScanline(render);

                Vdp.ProcessFrameInterrupt();
                Vdp.ProcessLineInterrupt();
                ProcessLineControls();

                for (int j = 0; j < Vdp.IPeriod; j++)
                {
                    Cpu.ExecuteOne();

                    PSG.generate_sound();

                    s_L = PSG.current_sample_L;
                    s_R = PSG.current_sample_R;

                    if (s_L != old_s_L)
                    {
                        blip_L.AddDelta(sampleclock, s_L - old_s_L);
                        old_s_L = s_L;
                    }

                    if (s_R != old_s_R)
                    {
                        blip_R.AddDelta(sampleclock, s_R - old_s_R);
                        old_s_R = s_R;
                    }

                    sampleclock++;
                }

                if (Vdp.ScanLine == scanlinesPerFrame - 1)
                {
                    Vdp.ProcessGGScreen();
                    Vdp.ProcessOverscan();
                }
            }

            if (_lagged)
            {
                _lagCount++;
                _isLag = true;
            }
            else
            {
                _isLag = false;
            }

            return(true);
        }