Beispiel #1
0
        public void AND_B_HandlesPFlag()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilEnd);

            m.InitCode(new byte[]
            {
                0x3E, 0x33, // LD A,33H
                0x06, 0x22, // LD B,22H
                0xA0        // AND B
            });

            // --- Act
            m.Run();

            // --- Assert
            var regs = m.Cpu.Registers;

            regs.A.ShouldBe((byte)0x22);
            regs.SFlag.ShouldBeFalse();
            regs.ZFlag.ShouldBeFalse();
            regs.PFlag.ShouldBeTrue();

            regs.HFlag.ShouldBeTrue();
            regs.NFlag.ShouldBeFalse();
            regs.CFlag.ShouldBeFalse();
            m.ShouldKeepRegisters(except: "AF, B");
            m.ShouldKeepMemory();

            regs.PC.ShouldBe((ushort)0x0005);
            m.Cpu.Tacts.ShouldBe(18L);
        }
        public void DAA_WorksAsExpected()
        {
            // --- Arrange
            var samples = new[]
            {
                //new DaaSample(0x99, false, false, false, 0x998C),
                //new DaaSample(0x99, true, false, false, 0x9F8C),
                //new DaaSample(0x7A, false, false, false, 0x8090),
                //new DaaSample(0x7A, true, false, false, 0x8090),
                //new DaaSample(0xA9, false, false, false, 0x090D),
                //new DaaSample(0x87, false, false, true, 0xE7A5),
                new DaaSample(0x87, true, false, true, 0xEDAD),
                //new DaaSample(0x1B, false, false, true, 0x8195),
                //new DaaSample(0x1B, true, false, true, 0x8195),
                //new DaaSample(0xAA, false, false, false, 0x1011),
                //new DaaSample(0xAA, true, false, false, 0x1011),
                //new DaaSample(0xC6, true, false, false, 0x2C29)
            };

            // --- Act
            foreach (var sample in samples)
            {
                var m = new Z80TestMachine(RunMode.UntilEnd);
                m.InitCode(new byte[]
                {
                    0x27  // DAA
                });
                m.Cpu.Registers.A = sample.A;
                m.Cpu.Registers.F = (byte)((sample.H ? FlagsSetMask.H : 0)
                                           | (sample.N ? FlagsSetMask.N : 0)
                                           | (sample.C ? FlagsSetMask.C : 0));

                // --- Act
                m.Run();

                // --- Assert
                var regs = m.Cpu.Registers;

                m.ShouldKeepRegisters(except: "AF");
                m.ShouldKeepMemory();

                regs.AF.ShouldBe(sample.AF);
                regs.PC.ShouldBe((ushort)0x0001);
                m.Cpu.Tacts.ShouldBe(4L);
            }
        }
        public void DJNZ_DoesNotCollectDebugInfoWhenNoJump()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilEnd);

            m.InitCode(new byte[]
            {
                0x06, 0x01, // LD B,01H
                0x10, 0x02  // DJNZ 02H
            });

            // --- Act
            m.Run();

            // --- Assert
            var brLog = m.BranchEvents;

            brLog.Count.ShouldBe(0);
        }
        public void JR_NC_DoesNotCollectDebugInfoWhenC()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilEnd);

            m.InitCode(new byte[]
            {
                0x37,       // SCF
                0x30, 0x02  // JR NC,02H
            });

            // --- Act
            m.Run();

            // --- Assert
            var brLog = m.BranchEvents;

            brLog.Count.ShouldBe(0);
        }
        public void JR_Z_DoesNotCollectDebugInfoWhenNZ()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilEnd);

            m.InitCode(new byte[]
            {
                0x3E, 0x02, // LD A,02H
                0x3D,       // DEC A
                0x28, 0x02  // JR Z,02H
            });

            // --- Act
            m.Run();

            // --- Assert
            var brLog = m.BranchEvents;

            brLog.Count.ShouldBe(0);
        }
Beispiel #6
0
        public void LDDRX_WithNoCopyWorksAsExpected()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilEnd, true);

            m.InitCode(new byte[]
            {
                0xED, 0xBC // LDDRX
            });
            var regs = m.Cpu.Registers;

            regs.A  = 0xA6;
            regs.BC = 0x0003;
            regs.HL = 0x1002;
            regs.DE = 0x2002;
            m.Memory[regs.HL - 2] = 0xA5;
            m.Memory[regs.HL - 1] = 0xA6;
            m.Memory[regs.HL]     = 0xA7;
            m.Memory[regs.DE - 1] = 0xCC;

            // --- Act
            m.Run();

            // --- Assert
            m.Memory[0x2000].ShouldBe((byte)0xA5);
            m.Memory[0x2001].ShouldBe((byte)0xCC);
            m.Memory[0x2002].ShouldBe((byte)0xA7);
            regs.BC.ShouldBe((ushort)0x0000);
            regs.HL.ShouldBe((ushort)0x0FFF);
            regs.DE.ShouldBe((ushort)0x1FFF);
            regs.PFlag.ShouldBeFalse();

            regs.HFlag.ShouldBeFalse();
            regs.NFlag.ShouldBeFalse();
            regs.CFlag.ShouldBeFalse();
            m.ShouldKeepRegisters(except: "F, BC, DE, HL");
            m.ShouldKeepMemory(except: "2000-2002");

            regs.PC.ShouldBe((ushort)0x0002);
            m.Cpu.Tacts.ShouldBe(55L);
        }
Beispiel #7
0
        public void PIXELDN_DoesNopWithNoExtendedInstructionSet()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.OneInstruction);

            m.InitCode(new byte[]
            {
                0xED, 0x93           // PIXELDN
            });
            var regs = m.Cpu.Registers;

            // --- Act
            m.Run();

            // --- Assert
            m.ShouldKeepRegisters();
            m.ShouldKeepMemory();

            regs.PC.ShouldBe((ushort)0x0002);
            m.Cpu.Tacts.ShouldBe(8L);
        }
Beispiel #8
0
        public void RST_0_SetsStepOutValues()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.OneInstruction);

            m.InitCode(new byte[]
            {
                0x3E, 0x12, // LD A,12H
                0xC7        // RST 0
            });

            // --- Act
            m.Run();
            m.Run();

            // --- Assert
            m.CallStepOutEvents.Count.ShouldBe(1);
            m.CallStepOutEvents[0].ShouldBe((ushort)0x0000);
            m.StepOutPushEvents.Count.ShouldBe(1);
            m.StepOutPushEvents[0].ShouldBe((ushort)0x0003);
        }
Beispiel #9
0
        public void LDIRX_WorksAsExpected()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilEnd, true);

            m.InitCode(new byte[]
            {
                0xED, 0xB4 // LDIRX
            });
            var regs = m.Cpu.Registers;

            regs.A                = 0x00;
            regs.BC               = 0x0003;
            regs.HL               = 0x1001;
            regs.DE               = 0x1000;
            m.Memory[regs.HL]     = 0xA5;
            m.Memory[regs.HL + 1] = 0xA6;
            m.Memory[regs.HL + 2] = 0xA7;

            // --- Act
            m.Run();

            // --- Assert
            m.Memory[0x1000].ShouldBe((byte)0xA5);
            m.Memory[0x1001].ShouldBe((byte)0xA6);
            m.Memory[0x1002].ShouldBe((byte)0xA7);
            regs.BC.ShouldBe((ushort)0x0000);
            regs.HL.ShouldBe((ushort)0x1004);
            regs.DE.ShouldBe((ushort)0x1003);
            regs.PFlag.ShouldBeFalse();

            regs.HFlag.ShouldBeFalse();
            regs.NFlag.ShouldBeFalse();
            regs.CFlag.ShouldBeFalse();
            m.ShouldKeepRegisters(except: "F, BC, DE, HL");
            m.ShouldKeepMemory(except: "1000-1002");

            regs.PC.ShouldBe((ushort)0x0002);
            m.Cpu.Tacts.ShouldBe(58L);
        }
Beispiel #10
0
        public void LDDX_WorksWithZeroedCounter()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.OneInstruction, true);

            m.InitCode(new byte[]
            {
                0xED, 0xAC // LDDX
            });
            var regs = m.Cpu.Registers;

            regs.A            = 0x00;
            regs.BC           = 0x0001;
            regs.HL           = 0x1000;
            regs.DE           = 0x1001;
            m.Memory[regs.HL] = 0xA5;
            m.Memory[regs.DE] = 0x11;

            // --- Act
            m.Run();

            // --- Assert

            m.Memory[0x1001].ShouldBe((byte)0xA5);
            regs.BC.ShouldBe((ushort)0x0000);
            regs.HL.ShouldBe((ushort)0x0FFF);
            regs.DE.ShouldBe((ushort)0x1000);
            regs.PFlag.ShouldBeFalse();

            regs.HFlag.ShouldBeFalse();
            regs.NFlag.ShouldBeFalse();
            regs.CFlag.ShouldBeFalse();
            m.ShouldKeepRegisters(except: "F, BC, DE, HL");
            m.ShouldKeepMemory(except: "1001");

            regs.PC.ShouldBe((ushort)0x0002);
            m.Cpu.Tacts.ShouldBe(16L);
        }
        public void LD_HL_NN_WorksAsExpected()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.OneInstruction);

            m.InitCode(new byte[]
            {
                0x21, 0x26, 0xA9 // LD HL,A926H
            });

            // --- Act
            m.Run();

            // --- Assert
            var regs = m.Cpu.Registers;

            m.ShouldKeepRegisters(except: "HL");
            m.ShouldKeepMemory();

            regs.HL.ShouldBe((ushort)0xA926);
            regs.PC.ShouldBe((ushort)0x0003);
            m.Cpu.Tacts.ShouldBe(10L);
        }
        public void JP_M_DoesNotCollectDebugInfoWhenP()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilHalt);

            m.InitCode(new byte[]
            {
                0x3E, 0x32,       // LD A,32H
                0x87,             // ADD A
                0xFA, 0x07, 0x00, // JP M,0007H
                0x76,             // HALT
                0x3E, 0xAA,       // LD A,AAH
                0x76              // HALT
            });

            // --- Act
            m.Run();

            // --- Assert
            var brLog = m.BranchEvents;

            brLog.Count.ShouldBe(0);
        }
        public void JP_NC_DoesNotCollectDebugInfoWhenC()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilHalt);

            m.InitCode(new byte[]
            {
                0x3E, 0x16,       // LD A,16H
                0x37,             // SCF
                0xD2, 0x07, 0x00, // JP NC,0007H
                0x76,             // HALT
                0x3E, 0xAA,       // LD A,AAH
                0x76              // HALT
            });

            // --- Act
            m.Run();

            // --- Assert
            var brLog = m.BranchEvents;

            brLog.Count.ShouldBe(0);
        }
        public void LD_L_N_WorksAsExpected()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.OneInstruction);

            m.InitCode(new byte[]
            {
                0x2E, 0x26 // LD L,26H
            });

            // --- Act
            m.Run();

            // --- Assert
            var regs = m.Cpu.Registers;

            m.ShouldKeepRegisters(except: "L");
            m.ShouldKeepMemory();

            regs.L.ShouldBe((byte)0x26);
            regs.PC.ShouldBe((ushort)0x0002);
            m.Cpu.Tacts.ShouldBe(7L);
        }
Beispiel #15
0
        public void LDIX_WithNoCopyWorksAsExpected()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.OneInstruction, true);

            m.InitCode(new byte[]
            {
                0xED, 0xA4 // LDIX
            });
            var regs = m.Cpu.Registers;

            regs.A            = 0xA5;
            regs.BC           = 0x0010;
            regs.HL           = 0x1000;
            regs.DE           = 0x1001;
            m.Memory[regs.HL] = 0xA5;
            m.Memory[regs.DE] = 0x11;

            // --- Act
            m.Run();

            // --- Assert
            m.Memory[0x1001].ShouldBe((byte)0x11);
            regs.BC.ShouldBe((ushort)0x000F);
            regs.HL.ShouldBe((ushort)0x1001);
            regs.DE.ShouldBe((ushort)0x1002);
            regs.PFlag.ShouldBeTrue();

            regs.HFlag.ShouldBeFalse();
            regs.NFlag.ShouldBeFalse();
            regs.CFlag.ShouldBeFalse();
            m.ShouldKeepRegisters(except: "F, BC, DE, HL");
            m.ShouldKeepMemory(except: "1001");

            regs.PC.ShouldBe((ushort)0x0002);
            m.Cpu.Tacts.ShouldBe(13L);
        }
Beispiel #16
0
        public void XRL_D_WorksAsExpected()
        {
            // --- Arrange
            const byte OFFS = 0x32;
            var        m    = new Z80TestMachine(RunMode.OneInstruction);

            m.InitCode(new byte[]
            {
                0xFD, 0xCB, OFFS, 0x12 // RL (IY+32H),D
            });
            var regs = m.Cpu.Registers;

            regs.IY = 0x1000;
            regs.F |= FlagsSetMask.C;
            m.Memory[regs.IY + OFFS] = 0x08;

            // --- Act
            m.Run();

            // --- Assert
            regs.D.ShouldBe(m.Memory[regs.IY + OFFS]);
            regs.D.ShouldBe((byte)0x11);

            regs.SFlag.ShouldBeFalse();
            regs.ZFlag.ShouldBeFalse();
            regs.CFlag.ShouldBeFalse();
            regs.PFlag.ShouldBeTrue();

            regs.HFlag.ShouldBeFalse();
            regs.NFlag.ShouldBeFalse();
            m.ShouldKeepRegisters(except: "F, D");
            m.ShouldKeepMemory(except: "1032");

            regs.PC.ShouldBe((ushort)0x0004);
            m.Cpu.Tacts.ShouldBe(23L);
        }
        public void HALTWorksAsExpected()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilHalt);

            m.InitCode(new byte[]
            {
                0x76        // HALT
            });

            // --- Act
            m.Run();

            // --- Assert
            var regs = m.Cpu.Registers;

            (m.Cpu.StateFlags & Z80StateFlags.Halted).ShouldBe(Z80StateFlags.Halted);

            m.ShouldKeepRegisters();
            m.ShouldKeepMemory();

            regs.PC.ShouldBe((ushort)0x0000);
            m.Cpu.Tacts.ShouldBe(4L);
        }
        public void LD_HLi_L_WorksAsExpected()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilEnd);

            m.InitCode(new byte[]
            {
                0x21, 0x22, 0x10, // LD HL,1022H
                0x75              // LD (HL),L
            });

            // --- Act
            m.Run();

            // --- Assert
            var regs = m.Cpu.Registers;

            m.Memory[0x1022].ShouldBe((byte)0x22);
            m.ShouldKeepRegisters(except: "HL");
            m.ShouldKeepMemory(except: "1022");

            regs.PC.ShouldBe((ushort)0x0004);
            m.Cpu.Tacts.ShouldBe(17L);
        }
        public void DEC_HL_WorksAsExpected1()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilEnd);

            m.InitCode(new byte[]
            {
                0x21, 0x26, 0xA9, // LD HL,A926H
                0x2B              // DEC HL
            });

            // --- Act
            m.Run();

            // --- Assert
            var regs = m.Cpu.Registers;

            m.ShouldKeepRegisters(except: "HL");
            m.ShouldKeepMemory();

            regs.HL.ShouldBe((ushort)0xA925);
            regs.PC.ShouldBe((ushort)0x0004);
            m.Cpu.Tacts.ShouldBe(16L);
        }
Beispiel #20
0
        public void PIXELDN_WorksAsExpected(int orig, int down)
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.OneInstruction, true);

            m.InitCode(new byte[]
            {
                0xED, 0x93           // PIXELDN
            });
            var regs = m.Cpu.Registers;

            regs.HL = (ushort)orig;

            // --- Act
            m.Run();

            // --- Assert
            regs.HL.ShouldBe((ushort)down);
            m.ShouldKeepRegisters(except: "HL");
            m.ShouldKeepMemory();

            regs.PC.ShouldBe((ushort)0x0002);
            m.Cpu.Tacts.ShouldBe(8L);
        }
Beispiel #21
0
        public void PUSH_NN_WorksAsExpected()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilEnd, true);

            m.InitCode(new byte[]
            {
                0xED, 0x8A, 0x52, 0x23, // PUSH 2352H
                0xE1                    // POP HL
            });

            // --- Act
            m.Run();

            // --- Assert
            var regs = m.Cpu.Registers;

            regs.HL.ShouldBe((ushort)0x2352);
            m.ShouldKeepRegisters(except: "HL");
            m.ShouldKeepMemory(except: "FFFE-FFFF");

            regs.PC.ShouldBe((ushort)0x0005);
            m.Cpu.Tacts.ShouldBe(30L);
        }
        public void JR_NZ_E_WorksWithJump()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilEnd);

            m.InitCode(new byte[]
            {
                0x3E, 0x02, // LD A,02H
                0x3D,       // DEC A
                0x20, 0x02  // JR NZ,02H
            });

            // --- Act
            m.Run();

            // --- Assert
            var regs = m.Cpu.Registers;

            m.ShouldKeepRegisters(except: "AF");
            m.ShouldKeepMemory();

            regs.PC.ShouldBe((ushort)0x0007);
            m.Cpu.Tacts.ShouldBe(23L);
        }
Beispiel #23
0
        public void CALL_M_SetsStepOutValues()
        {
            // --- Arrange
            var m = new Z80TestMachine(RunMode.UntilEnd);

            m.InitCode(new byte[]
            {
                0x00,             // NOP
                0x78,             // LD A,B
                0xC9,             // RET
                0x3E, 0xC0,       // LD A,C0H
                0x87,             // ADD A
                0xFC, 0x01, 0x00, // CALL M,0001H
            }, 0x0000, 0x0003);

            // --- Act
            m.Run();

            // --- Assert
            m.CallStepOutEvents.Count.ShouldBe(1);
            m.CallStepOutEvents[0].ShouldBe((ushort)0x0001);
            m.StepOutPushEvents.Count.ShouldBe(1);
            m.StepOutPushEvents[0].ShouldBe((ushort)0x0009);
        }