public void FrameAdvance(bool render, bool rendersound)
        {
            _lagged = true;

            Cpu.Debug = Tracer.Enabled;

            if (Cpu.Debug && Cpu.Logger == null)             // TODO, lets not do this on each frame. But lets refactor CoreComm/CoreComm first
            {
                Cpu.Logger = (s) => Tracer.Put(s);
            }

            //I eyeballed this speed
            for (int i = 0; i < 5; i++)
            {
                _onPressed = Controller.IsPressed("ON");

                //and this was derived from other emus
                Cpu.ExecuteCycles(10000);
                Cpu.Interrupt = true;
            }

            Frame++;

            if (_lagged)
            {
                _lagCount++;
            }

            _isLag = _lagged;
        }
Example #2
0
        public void ExecuteFrame(bool Int_pending)
        {
            for (int scanLine = 0; scanLine < 262; scanLine++)
            {
                RenderScanline(scanLine);

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

                    if (EnableInterrupts)
                    {
                        Cpu.NonMaskableInterrupt = true;
                    }
                }

                Cpu.ExecuteCycles(228);

                Cpu.Interrupt = false;
                if (Int_pending && scanLine == 50)
                {
                    if (EnableInterrupts)
                    {
                        Cpu.Interrupt = true;
                        Int_pending   = false;
                    }
                }
            }
        }
Example #3
0
        public void ExecuteFrame()
        {
            for (int scanLine = 0; scanLine < 262; scanLine++)
            {
                RenderScanline(scanLine);

                if (scanLine == 192)
                {
                    InterruptPending = true;
                    if (EnableInterrupts)
                    {
                        Cpu.NonMaskableInterrupt = true;
                    }
                }

                Cpu.ExecuteCycles(228);
            }
        }
Example #4
0
        public void FrameAdvance(bool render, bool rendersound)
        {
            _lagged = true;

            //I eyeballed this speed
            for (int i = 0; i < 5; i++)
            {
                _onPressed = Controller.IsPressed("ON");

                //and this was derived from other emus
                Cpu.ExecuteCycles(10000);
                Cpu.Interrupt = true;
            }

            Frame++;

            if (_lagged)
            {
                _lagCount++;
            }

            _isLag = _lagged;
        }
Example #5
0
        private void TestFile_(string fileName)
        {
            var lcd = new Lcd {
                Active = false
            };
            var serialBus   = new SerialBus();
            var ioAddresses = new IoAddresses(serialBus);
            var memoryMap   = new MemoryMap(ioAddresses);
            var registers   = new Registers();
            var memory      = new Mmu(memoryMap, registers);
            var cpu         = new Cpu(lcd, memory, new Opcodes(memory));

            ushort initialPc = 0;
            ushort finalPc   = 0;

            var maxCycleCount = 100000000;

            LinkedList <Expected?>?expecteds = null;

            if (BlarggTest.COMPARE_TO_BINJGB_)
            {
                var expectedTracePath =
                    "R:/Documents/CSharpWorkspace/FinCSharp/FinCSharpTests/tst/emulation/gb/blargg/" +
                    fileName +
                    ".txt";
                expecteds = new LinkedList <Expected?>();
                try {
                    using (var sr = new StreamReader(expectedTracePath)) {
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            var words = line.Split(' ');

                            var expectedAHex = words[0].Substring(2);
                            var expectedA    = byte.Parse(expectedAHex, NumberStyles.HexNumber);

                            var expectedFText = words[1].Substring(2);
                            var expectedF     = 0;
                            if (expectedFText[0] != '-')
                            {
                                expectedF |= 1 << 7;
                            }
                            if (expectedFText[1] != '-')
                            {
                                expectedF |= 1 << 6;
                            }
                            if (expectedFText[2] != '-')
                            {
                                expectedF |= 1 << 5;
                            }
                            if (expectedFText[3] != '-')
                            {
                                expectedF |= 1 << 4;
                            }

                            var expectedAf = ByteMath.MergeBytes(expectedA, (byte)expectedF);

                            var expectedBcHex = words[2].Substring(3);
                            var expectedBc    = ushort.Parse(expectedBcHex,
                                                             NumberStyles.HexNumber);

                            var expectedDeHex = words[3].Substring(3);
                            var expectedDe    = ushort.Parse(expectedDeHex,
                                                             NumberStyles.HexNumber);

                            var expectedHlHex = words[4].Substring(3);
                            var expectedHl    = ushort.Parse(expectedHlHex,
                                                             NumberStyles.HexNumber);

                            var expectedPcHex = words[6].Substring(3);
                            //var expectedPcHex = line.Substring(47, 4);
                            var expectedPc = ushort.Parse(expectedPcHex,
                                                          NumberStyles.HexNumber);

                            var expectedCyclesText = line.Substring(57,
                                                                    line.IndexOf(
                                                                        ')',
                                                                        57) -
                                                                    57);
                            var expectedCycles = int.Parse(expectedCyclesText);

                            var expectedSclText = words[9].Substring(4);
                            var expectedScl     = int.Parse(expectedSclText);

                            var expectedPpuModeText = words[12].Substring(5);
                            var expectedPpuMode     = int.Parse(expectedPpuModeText);

                            expecteds.AddLast(new Expected(expectedAf,
                                                           expectedBc,
                                                           expectedDe,
                                                           expectedHl,
                                                           expectedPc,
                                                           expectedCycles,
                                                           expectedScl,
                                                           expectedPpuMode));
                        }
                    }
                } catch (Exception e) {
                    expecteds.Clear();
                    expecteds = null;
                }
            }

            var romPath =
                "R:/Documents/CSharpWorkspace/FinCSharp/FinCSharpTests/tst/emulation/gb/blargg/" +
                fileName +
                ".gb";

            var romFile = LocalFile.At(romPath);
            var romData = LocalFileUtil.ReadBytes(romFile);

            memoryMap.Rom = new Rom(romData);

            var output = "";

            serialBus.Bytes.Subscribe(b => { output += Convert.ToChar(b); });

            var outputPath =
                "R:/Documents/CSharpWorkspace/FinCSharp/FinCSharpTests/tst/emulation/gb/blargg/output_" +
                fileName +
                ".txt";

            using var writer = (BlarggTest.LOG_TRACE_)
                             ? new StreamWriter(outputPath)
                             : null;

            var pc = registers.Pc;

            initialPc = 0;
            finalPc   = 0;

            var shouldGetPpuMode =
                BlarggTest.LOG_TRACE_ || BlarggTest.COMPARE_TO_BINJGB_;

            var lcdc = ioAddresses.Lcdc;
            var ly   = ioAddresses.Ly;

            var instruction = 0;
            var cycles      = 0;
            //try {
            var enumerator = expecteds?.GetEnumerator();

            for (var i = 0; i < maxCycleCount; ++i)
            {
                var expected = enumerator?.Current;
                enumerator?.MoveNext();

                var ppuMode = shouldGetPpuMode ? cpu.PpuMode : 0;
                initialPc = pc.Value;

                if (BlarggTest.LOG_TRACE_)
                {
                    StringBuilder line = new StringBuilder();

                    line.AppendFormat("0x{0:x4}:  ", initialPc);
                    line.AppendFormat("{0:x2} |", memoryMap[initialPc]);
                    line.AppendFormat(" af={0:x4}", registers.Af.Value);
                    line.AppendFormat(" bc={0:x4}", registers.Bc.Value);
                    line.AppendFormat(" de={0:x4}", registers.De.Value);
                    line.AppendFormat(" hl={0:x4}", registers.Hl.Value);
                    line.AppendFormat(" sp={0:x4}", registers.Sp.Value);
                    line.AppendFormat(" pc={0:x4} |", registers.Pc.Value);

                    line.AppendFormat(" tot={0} |", cycles);

                    line.AppendFormat(" scl={0}", cpu.UpwardScanlineCycleCounter);
                    line.AppendFormat(" st={0}", cpu.PpuModeCycleCount);
                    line.AppendFormat(" cnt={0} |", cpu.ScanlineCycleCounter / 2);

                    line.AppendFormat(" lcdc={0:x2}", lcdc.Value);
                    line.AppendFormat(" ly={0:x2}", ly.Value);
                    line.AppendFormat(" ppu={0:x1} |", ppuMode);

                    line.AppendFormat(" div={0:x2}", ioAddresses.Div);
                    line.AppendFormat(" tima={0:x2}", ioAddresses.Tima);
                    line.AppendFormat(" tma={0:x2}", ioAddresses.Tma);
                    line.AppendFormat(" tac={0:x2}", ioAddresses.Tac);
                    writer.WriteLine(line.ToString());
                }

                if (expected != null &&
                    (expected.Pc != initialPc ||
                     expected.Cycles != cycles ||
                     expected.Af != registers.Af.Value ||
                     expected.Bc != registers.Bc.Value ||
                     expected.De != registers.De.Value ||
                     expected.Hl != registers.Hl.Value ||
                     expected.Scl != cpu.UpwardScanlineCycleCounter ||
                     expected.PpuMode != ppuMode))
                {
                    var errorBuilder = new StringBuilder();
                    errorBuilder.Append("Difference at instruction: " +
                                        instruction +
                                        "\n");
                    errorBuilder.AppendFormat("Af: {0:x4}/{1:x4}\n",
                                              expected.Af,
                                              registers.Af.Value);
                    errorBuilder.AppendFormat("Bc: {0:x4}/{1:x4}\n",
                                              expected.Bc,
                                              registers.Bc.Value);
                    errorBuilder.AppendFormat("De: {0:x4}/{1:x4}\n",
                                              expected.De,
                                              registers.De.Value);
                    errorBuilder.AppendFormat("Hl: {0:x4}/{1:x4}\n",
                                              expected.Hl,
                                              registers.Hl.Value);
                    errorBuilder.AppendFormat("Pc: {0:x4}/{1:x4}\n",
                                              expected.Pc,
                                              initialPc);
                    errorBuilder.AppendFormat("Cycles: {0}/{1}\n",
                                              expected.Cycles,
                                              cycles);
                    errorBuilder.AppendFormat("Scl: {0}/{1}\n",
                                              expected.Scl,
                                              cpu.UpwardScanlineCycleCounter);
                    errorBuilder.AppendFormat("Ppu Mode: {0}/{1}\n",
                                              expected.PpuMode,
                                              ppuMode);
                    Assert.Fail(errorBuilder.ToString());
                }

                cycles += cpu.ExecuteCycles(1);

                instruction++;
                finalPc = pc.Value;

                if (initialPc == finalPc && memory.HaltState != HaltState.HALTED)
                {
                    break;
                }
            }

            /*} catch (Exception e) {
             * output = cycles + ", " + registers.Pc.Value + ": " + output;
             * throw e;
             * }*/

            output = cycles + ", " + registers.Pc.Value + ": " + output;

            if (!output.Contains("Passed"))
            {
                Asserts.Fail(output);
            }
        }