protected CPUResults Execute(byte[] memory, CPU cpu)
        {
            var memoryImplementation = new SimpleMemory(memory);

            cpu.Memory = memoryImplementation;

            return(Execute(cpu));
        }
        protected CPUResults Execute(byte[] memory, CPUConfig cpuConfig = null)
        {
            var cpu = new CPU(cpuConfig ?? new CPUConfig());

            var memoryImplementation = cpu.Memory as SimpleMemory;

            // Map the ROM into the memory space.
            if (memory.Length > memoryImplementation.Memory.Length)
            {
                memoryImplementation = new SimpleMemory(memory);
            }
            else
            {
                Array.Copy(memory, memoryImplementation.Memory, memory.Length);
            }

            return(Execute(memoryImplementation.Memory, cpu));
        }
        public void TestStepMaskableInterrupt_Mode2(byte interruptVector, byte dataBusValue, UInt16 expectedProgramCounter)
        {
            var rawMemory = new byte[16 * 1024];
            var memory    = new SimpleMemory(rawMemory);

            var initialState = new CPUConfig()
            {
                InterruptsEnabled = true,
                InterruptMode     = InterruptMode.Two,
                Registers         = new CPURegisters()
                {
                    PC = 0x1234,
                    SP = 0x3FFF,
                    I  = interruptVector,
                },
                Memory = memory,
            };

            var addressIntoVectorTable = (interruptVector << 8) | dataBusValue;

            memory.Write16(addressIntoVectorTable, expectedProgramCounter);

            var cpu = new CPU(initialState);

            var cycles = cpu.StepMaskableInterrupt(dataBusValue);

            Assert.Equal(17, cycles);

            // Interrupts should still be enabled (IFF1).
            Assert.True(cpu.InterruptsEnabled);

            // We should've jumped here; MSB from interrupt vector and LSB from data bus combine to form address.
            Assert.Equal(expectedProgramCounter, cpu.Registers.PC);

            // Ensure the previous program counter value was pushed onto the stack.
            Assert.Equal(0x3FFD, cpu.Registers.SP);
            Assert.Equal(0x12, cpu.Memory.Read(0x3FFE));
            Assert.Equal(0x34, cpu.Memory.Read(0x3FFD));
        }
 public CPUConfig()
 {
     Memory = new SimpleMemory(16 * 1024);
     EnableDiagnosticsMode = false;
 }