Exemplo n.º 1
0
        public void Test_LD_HLP_A()
        {
            byte   data = 0x42;
            ushort hl   = 0x1234;

            var actualState = new CpuState();

            actualState.Registers.A  = data;
            actualState.Registers.HL = hl;

            var expectedState = new CpuState();

            expectedState.Registers.A  = data;
            expectedState.Registers.HL = (ushort)(hl + 1);

            var memoryMock = new Mock <IRandomAccessMemory>();

            var instruction = new LD_HLP_A();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(hl, data), Times.Once);
        }
Exemplo n.º 2
0
        protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, Action<byte> write, ref int cycles)
        {
            bool carry = cpuState.IsFlagSet(CpuState.Flags.Carry);
            cpuState.SetFlag(CpuState.Flags.Carry, arg & 0x80);
            arg <<= 1;
            if (carry)
            {
                arg |= 1;
            }
            write(arg);
            cpuState.SetNegativeFlag(arg);
            cpuState.SetZeroFlag(arg);

            switch (Variants[memory[cpuState.Pc]])
            {
                case AddressingMode.Accumulator:
                    cycles = 2;
                    break;
                case AddressingMode.ZeroPage:
                    cycles = 5;
                    break;
                case AddressingMode.ZeroPageXIndexed:
                    cycles = 6;
                    break;
                case AddressingMode.Absolute:
                    cycles = 6;
                    break;
                case AddressingMode.AbsoluteX:
                    cycles = 7;
                    break;
            }
        }
Exemplo n.º 3
0
        public void Test_RST(byte opcode)
        {
            ushort resetAddress = (ushort)(opcode & 0x38);

            ushort sp = 0x4242;
            ushort pc = 0x1122;

            var expectedState = new CpuState();

            expectedState.StackPointer   = (ushort)(sp - 2);
            expectedState.ProgramCounter = resetAddress;

            var actualState = new CpuState();

            actualState.StackPointer   = sp;
            actualState.ProgramCounter = pc;

            var memoryMock = new Mock <IRandomAccessMemory>();

            var instruction = new RST();

            instruction.Initialize(opcode);

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte((ushort)(sp - 1), (byte)(pc >> 8)), Times.Once);
            memoryMock.Verify(m => m.WriteByte((ushort)(sp - 2), (byte)(pc & 0x00FF)), Times.Once);
        }
Exemplo n.º 4
0
        public void Test_JR()
        {
            ushort pc     = 0x4242;
            byte   offset = 0x11;

            var expectedState = new CpuState();

            expectedState.ProgramCounter = (ushort)(pc + 1 + (sbyte)offset);

            var actualState = new CpuState();

            actualState.ProgramCounter = pc;

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte(pc)).Returns(offset);

            var instruction = new JR();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(It.IsAny <ushort>(), It.IsAny <byte>()), Times.Never);
        }
Exemplo n.º 5
0
        public void Test_RET()
        {
            ushort sp      = 0x4242;
            byte   lsbData = 0x11;
            byte   msbData = 0x22;

            var expectedState = new CpuState();

            expectedState.StackPointer   = (ushort)(sp + 2);
            expectedState.ProgramCounter = (ushort)((msbData << 8) | lsbData);

            var actualState = new CpuState();

            actualState.StackPointer = sp;

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte(sp)).Returns(lsbData);
            memoryMock.Setup(m => m.ReadByte((ushort)(sp + 1))).Returns(msbData);

            var instruction = new RET();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(It.IsAny <ushort>(), It.IsAny <byte>()), Times.Never);
        }
        public void Test_RLR8(byte opcode)
        {
            var registerIndex = opcode & 0x07;

            var actualState = new CpuState();

            actualState.Registers[registerIndex]  = 0x08;
            actualState.Registers.ZeroFlag        = true;
            actualState.Registers.SubtractionFlag = true;
            actualState.Registers.HalfCarryFlag   = true;
            actualState.Registers.CarryFlag       = true;

            var expectedState = new CpuState();

            expectedState.Registers[registerIndex] = 0x11;

            var memoryMock = new Mock <IRandomAccessMemory>();

            var instruction = new RLR8();

            instruction.Initialize(opcode);

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(It.IsAny <ushort>(), It.IsAny <byte>()), Times.Never);
        }
Exemplo n.º 7
0
        public void Test_LDA_D16_()
        {
            ushort pc         = 0x1111;
            byte   data       = 0x42;
            byte   addressLsb = 0x23;
            byte   addressMsb = 0x01;

            var actualState = new CpuState();

            actualState.ProgramCounter = pc;

            var expectedState = new CpuState();

            expectedState.Registers.A    = data;
            expectedState.ProgramCounter = (ushort)(pc + 2);

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte(pc)).Returns(addressLsb);
            memoryMock.Setup(m => m.ReadByte((ushort)(pc + 1))).Returns(addressMsb);
            memoryMock.Setup(m => m.ReadByte((ushort)((addressMsb << 8) | addressLsb))).Returns(data);

            var instruction = new LDA_D16_();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(It.IsAny <ushort>(), It.IsAny <byte>()), Times.Never);
        }
Exemplo n.º 8
0
        public void Test_SETN_HL_(byte opcode)
        {
            byte   data = 0x00;
            ushort hl   = 0x4242;

            var actualState = new CpuState();

            actualState.Registers.HL = hl;

            var expectedState = new CpuState();

            expectedState.Registers.HL = hl;

            var bitIndex = (opcode & 0x38) >> 3;

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte(hl)).Returns(data);

            var instruction = new SETN_HL_();

            instruction.Initialize(opcode);

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            //assert
            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(hl, (byte)(data | (0x01 << bitIndex))), Times.Once);
        }
Exemplo n.º 9
0
        protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, ref int cycles)
        {
            cpuState.X = arg;

            cpuState.SetNegativeFlag(arg);
            cpuState.SetZeroFlag(arg);
        }
Exemplo n.º 10
0
        protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, Action<byte> write, ref int cycles)
        {
            ushort result = arg;
            result <<= 1;
            var byteResult = (byte) (result & 0xFF);
            write(byteResult);
            cpuState.SetNegativeFlag(byteResult);
            cpuState.SetZeroFlag(byteResult);
            cpuState.SetFlag(CpuState.Flags.Carry, result & 0xFF00);

            switch (Variants[memory[cpuState.Pc]])
            {
                case AddressingMode.Accumulator:
                    cycles = 2;
                    break;
                case AddressingMode.ZeroPage:
                    cycles = 5;
                    break;
                case AddressingMode.ZeroPageXIndexed:
                    cycles = 6;
                    break;
                case AddressingMode.Absolute:
                    cycles = 6;
                    break;
                case AddressingMode.AbsoluteX:
                    cycles = 7;
                    break;
            }
        }
Exemplo n.º 11
0
        public void Test_LDR8R8(byte opcode)
        {
            byte data = 0x42;

            var sourceIndex = opcode & 0x07;
            var targetIndex = (opcode >> 3) & 0x07;

            var expectedState = new CpuState();

            expectedState.Registers[sourceIndex] = data;
            expectedState.Registers[targetIndex] = data;

            var actualState = new CpuState();

            actualState.Registers[sourceIndex] = data;

            var memoryMock = new Mock <IRandomAccessMemory>();

            var instruction = new LDR8R8();

            instruction.Initialize(opcode);

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            //assert
            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(It.IsAny <ushort>(), It.IsAny <byte>()), Times.Never);
        }
Exemplo n.º 12
0
        public void Test_LDSPHL()
        {
            ushort hl = 0x1234;

            var actualState = new CpuState();

            actualState.Registers.HL = hl;

            var expectedState = new CpuState();

            expectedState.Registers.HL = hl;
            expectedState.StackPointer = hl;

            var memoryMock = new Mock <IRandomAccessMemory>();

            var instruction = new LDSPHL();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(It.IsAny <ushort>(), It.IsAny <byte>()), Times.Never);
        }
Exemplo n.º 13
0
        public virtual void make(CpuState cpu)
        {
            pc = cpu.pc;
            for (int i = 0; i < NUMBER_REGISTERS; i++)
            {
                gpr[i] = cpu.getRegister(i);
            }
            threadID   = Modules.ThreadManForUserModule.CurrentThreadID;
            threadName = Modules.ThreadManForUserModule.getThreadName(threadID);

            Memory mem = MemoryViewer.Memory;

            if (MemoryViewer.isAddressGood(cpu.pc))
            {
                opcode = mem.read32(cpu.pc);
                Common.Instruction insn = Decoder.instruction(opcode);
                asm = insn.disasm(cpu.pc, opcode);
            }
            else
            {
                opcode = 0;
                asm    = "?";
            }

            dirty = true;
        }
Exemplo n.º 14
0
        public void Test_LD_HL_D8()
        {
            ushort pc   = 0x1234;
            ushort hl   = 0x4321;
            byte   data = 0x42;

            var actualState = new CpuState();

            actualState.ProgramCounter = pc;
            actualState.Registers.HL   = hl;

            var expectedState = new CpuState();

            expectedState.ProgramCounter = (ushort)(actualState.ProgramCounter + 1);
            expectedState.Registers.HL   = hl;

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte(pc)).Returns(data);

            var instruction = new LD_HL_D8();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(hl, data), Times.Once);
        }
Exemplo n.º 15
0
        public void Test_LDA_HLM_()
        {
            byte   data = 0x42;
            ushort hl   = 0x1234;

            var actualState = new CpuState();

            actualState.Registers.HL = hl;

            var expectedState = new CpuState();

            expectedState.Registers.A  = data;
            expectedState.Registers.HL = (ushort)(hl - 1);

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte(hl)).Returns(data);

            var instruction = new LDA_HLM_();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(It.IsAny <ushort>(), It.IsAny <byte>()), Times.Never);
        }
Exemplo n.º 16
0
        public void Test_DAA()
        {
            byte a = 0x22;

            var actualState = new CpuState();

            actualState.Registers.A             = a;
            actualState.Registers.HalfCarryFlag = true;

            var expectedState = new CpuState();

            expectedState.Registers.A = 0x28;

            var memoryMock = new Mock <IRandomAccessMemory>();

            var instruction = new DAA();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(It.IsAny <ushort>(), It.IsAny <byte>()), Times.Never);
        }
Exemplo n.º 17
0
        public void Test_LDR8_HL_(byte opcode)
        {
            ushort hl   = 0x1212;
            byte   data = 0x12;

            var registerIndex = (opcode >> 3) & 0x7;

            var actualState = new CpuState();

            actualState.Registers.HL = hl;

            var expectedState = new CpuState();

            expectedState.Registers[registerIndex] = data;
            expectedState.Registers.HL             = hl;

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte(hl)).Returns(data);

            var instruction = new LDR8_HL_();

            instruction.Initialize(opcode);

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(It.IsAny <ushort>(), It.IsAny <byte>()), Times.Never);
        }
Exemplo n.º 18
0
        public void Test_DEC_HL_()
        {
            ushort address = 0x4242;
            byte   data    = 0x00;

            var expectedState = new CpuState();

            expectedState.Registers.HL              = address;
            expectedState.Registers.HalfCarryFlag   = true;
            expectedState.Registers.SubtractionFlag = true;

            var actualState = new CpuState();

            actualState.Registers.HL       = address;
            actualState.Registers.ZeroFlag = true;

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte(address)).Returns(data);

            var instruction = new DEC_HL_();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            //assert
            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(address, (byte)(data - 1)), Times.Once());
        }
        public void Test_SWAP_HL_()
        {
            ushort hl           = 0x4242;
            byte   actualData   = 0x42;
            byte   expectedData = 0x24;

            var actualState = new CpuState();

            actualState.Registers.ZeroFlag        = true;
            actualState.Registers.SubtractionFlag = true;
            actualState.Registers.HalfCarryFlag   = true;
            actualState.Registers.CarryFlag       = true;
            actualState.Registers.HL = hl;

            var expectedState = new CpuState();

            expectedState.Registers.HL = hl;

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte(hl)).Returns(actualData);

            var instruction = new SWAP_HL_();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(hl, expectedData), Times.Once);
        }
Exemplo n.º 20
0
 protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, Action<byte> write, ref int cycles)
 {
     var res = (byte)((cpuState.X - 1) & 0xFF);
     cpuState.X = res;
     cpuState.SetNegativeFlag(res);
     cpuState.SetZeroFlag(res);
 }
Exemplo n.º 21
0
        public void Test_LDH_D8_A()
        {
            byte   data       = 0x42;
            byte   addressLsb = 0x24;
            ushort pc         = 0x1234;

            var actualState = new CpuState();

            actualState.Registers.A    = data;
            actualState.ProgramCounter = pc;

            var expectedState = new CpuState();

            expectedState.Registers.A    = data;
            expectedState.ProgramCounter = (ushort)(pc + 1);

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte(pc)).Returns(addressLsb);

            var instruction = new LDH_D8_A();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte((ushort)((0xFF << 8) | addressLsb), data), Times.Once);
        }
Exemplo n.º 22
0
        public MonitorFrame(byte[] buf)
        {
            if (buf.Length < frame_len)
            {
                return;
            }
            frame_head_tag = BitConverter.ToUInt16(buf, 0);
            if (frame_head_tag != 0xAA50)
            {
                return;
            }

            frame_sn     = BitConverter.ToUInt16(buf, 2);
            time_stamp   = BitConverter.ToUInt32(buf, 4);
            data_type    = (MonitorDataType)BitConverter.ToUInt16(buf, 8);
            series_state = (SeriesState)buf[10];
            cpu_state    = (CpuState)buf[11];
            data_length  = BitConverter.ToUInt16(buf, 12);

            crc = BitConverter.ToUInt16(buf, 14);

            UInt16 new_crc = Crc16.GetCrc(buf, 14);

            if (new_crc != crc)
            {
                return;
            }
            is_ok = true;
            return;
        }
Exemplo n.º 23
0
        public void Test_SETNR8(byte opcode)
        {
            var expectedState = new CpuState();

            var actualState = new CpuState();

            var bitIndex      = (opcode & 0x38) >> 3;
            var registerIndex = opcode & 0x07;

            expectedState.Registers[registerIndex] = (ushort)(0x01 << bitIndex);

            var memoryMock = new Mock <IRandomAccessMemory>();

            var instruction = new SETNR8();

            instruction.Initialize(opcode);

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            //assert
            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(It.IsAny <ushort>(), It.IsAny <byte>()), Times.Never);
        }
Exemplo n.º 24
0
        public int Execute(CpuState cpuState, IMemory memory)
        {
            throw new Exception("die");

            cpuState.Interrupt(0xFFFE, memory);
            return 7;
        }
Exemplo n.º 25
0
        public void Test_LDHA_C_()
        {
            byte data       = 0x42;
            byte addressLsb = 0x24;

            var actualState = new CpuState();

            actualState.Registers.C = addressLsb;

            var expectedState = new CpuState();

            expectedState.Registers.A = data;
            expectedState.Registers.C = addressLsb;

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte((ushort)((0xFF << 8) | addressLsb))).Returns(data);

            var instruction = new LDHA_C_();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(It.IsAny <ushort>(), It.IsAny <byte>()), Times.Never);
        }
Exemplo n.º 26
0
        public void Test_LDR8D8(byte opcode)
        {
            ushort pc   = 0x1234;
            byte   data = 0x42;

            var registerIndex = opcode >> 3;

            var actualState = new CpuState();

            actualState.ProgramCounter = pc;

            var expectedState = new CpuState();

            expectedState.ProgramCounter           = (ushort)(actualState.ProgramCounter + 1);
            expectedState.Registers[registerIndex] = data;

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte(pc)).Returns(data);

            var instruction = new LDR8D8();

            instruction.Initialize(opcode);

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(It.IsAny <ushort>(), It.IsAny <byte>()), Times.Never);
        }
Exemplo n.º 27
0
        public int Execute(CpuState cpuState, IMemory memory)
        {
            // Addresses are relative to the beginning of the next instruction not
            // the beginning of this one, so we'll need to advance the program counter.
            cpuState.Pc += 2;

            if (ShouldBranch(cpuState, memory))
            {
                var offset = memory[cpuState.Pc - 1];
                ushort newPc;
                if ((offset & 0x80) != 0)
                {
                    newPc = (ushort) (cpuState.Pc - (0x100 - offset));
                }
                else
                {
                    newPc = (ushort)(cpuState.Pc + offset);
                }

                int cycles = 3;
                if ((newPc & 0xFF00) != (cpuState.Pc & 0xFF00))
                {
                    // Extra cycle if the relative branch occurs to cross a page boundary
                    ++cycles;
                }
                cpuState.Pc = newPc;
                return cycles;
            }

            return 2;
        }
Exemplo n.º 28
0
        public void Test_LD_HL_R8(byte opcode)
        {
            ushort hl   = 0x1212;
            byte   data = 0x12;

            var registerIndex = opcode & 0x07;

            var actualState = new CpuState();

            actualState.Registers[registerIndex] = data;
            actualState.Registers.HL             = hl;

            var expectedState = new CpuState();

            expectedState.Registers[registerIndex] = data;
            expectedState.Registers.HL             = hl;

            var memoryMock = new Mock <IRandomAccessMemory>();

            var instruction = new LD_HL_R8();

            instruction.Initialize(opcode);

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte(hl, data), Times.Once);
        }
Exemplo n.º 29
0
 protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, Action<byte> write, ref int cycles)
 {
     cpuState.A = cpuState.PopStack(memory);
     cpuState.SetNegativeFlag(cpuState.A);
     cpuState.SetZeroFlag(cpuState.A);
     cycles = 4;
 }
Exemplo n.º 30
0
 private void StartFetch()
 {
     _state        = new CpuFetch(_cpu);
     _currentState = CpuStates.Fetch;
     _opcodeBuilder.Clear();
     _cycles.Reset();
 }
Exemplo n.º 31
0
 protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, Action<byte> write, ref int cycles)
 {
     var breakSet = cpuState.IsFlagSet(CpuState.Flags.Break);
     cpuState.StatusRegister = cpuState.PopStack(memory);
     cpuState.StatusRegister |= 1 << 5;
     cpuState.SetFlag(CpuState.Flags.Break, breakSet);
     cycles = 4;
 }
Exemplo n.º 32
0
 protected virtual void OnInstructionPartCompleted(CpuState completedPart)
 {
     if (completedPart == null ||
         !completedPart.IsComplete)
     {
         throw Errors.InstructionPartWasNotCompleted();
     }
 }
Exemplo n.º 33
0
 public int Execute(CpuState cpuState, IMemory memory)
 {
     var ret = cpuState.Pc + 2;
     cpuState.PushStack((byte)((ret & 0xFF00) >> 8), memory);
     cpuState.PushStack((byte) (ret & 0xFF), memory);
     cpuState.Pc = memory.ReadShort(cpuState.Pc + 1);
     return 6;
 }
Exemplo n.º 34
0
 public void UpdateCpuState(CpuState state)
 {
     Registers.First(r => r.RegisterName == "PC").Value   = state.PC;
     Registers.Find(r => r.RegisterName == "Stack").Value = state.Stack;
     for (int i = 0; i < 32; i++)
     {
         Registers.Find(r => r.RegisterName == $"R{i}").Value = state.Registers[i];
     }
 }
Exemplo n.º 35
0
        protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, Action<byte> write, ref int cycles)
        {
            var comparer = cpuState.A;
            var cmp = comparer - arg;

            cpuState.SetFlag(CpuState.Flags.Carry, cmp >= 0);
            cpuState.SetNegativeFlag((byte) (cmp & 0xFF));
            cpuState.SetZeroFlag((byte) (cmp & 0xFF));
        }
Exemplo n.º 36
0
 public void PopState()
 {
     if (m_CpuStack.Count != 0)
     {
         CpuState state = m_CpuStack.Pop();
         ProgramCounter = state.Pc;
         ProcessorFlags = state.Flags;
     }
 }
Exemplo n.º 37
0
        public int Execute(CpuState cpuState, IMemory memory)
        {
            var low = cpuState.PopStack(memory);
            var high = cpuState.PopStack(memory);

            cpuState.Pc = (ushort)((high << 8) | low);
            ++cpuState.Pc;
            return 6;
        }
Exemplo n.º 38
0
        public virtual int hleKernelPrintf(CpuState cpu, PspString formatString, Logger logger)
        {
            // Format and print the message to the logger
            if (logger.InfoEnabled)
            {
                string formattedMsg = hleKernelSprintf(cpu, formatString.String, _a1);
                logger.info(formattedMsg);
            }

            return(0);
        }
Exemplo n.º 39
0
 public void Save(CpuState state)
 {
     if (_lastFilePath == null)
     {
         SaveAs(state);
     }
     else
     {
         Save(state, _lastFilePath);
     }
 }
Exemplo n.º 40
0
        public static void append(CpuState cpu)
        {
            frames[position].make(cpu);

            if (size < capacity)
            {
                size++;
            }

            position = (position + 1) % capacity;
        }
Exemplo n.º 41
0
        public virtual string hleKernelSprintf(CpuState cpu, string format, object[] formatParameters)
        {
            string formattedMsg = format;

            try
            {
                // Translate the C-like format string to a Java format string:
                // - %u or %i -> %d
                // - %4u -> %4d
                // - %lld or %ld -> %d
                // - %llx or %lx -> %x
                // - %p -> %08X
                string javaMsg = format;
                javaMsg = javaMsg.replaceAll("\\%(\\d*)l?l?[uid]", "%$1d");
                javaMsg = javaMsg.replaceAll("\\%(\\d*)l?l?([xX])", "%$1$2");
                javaMsg = javaMsg.replaceAll("\\%p", "%08X");

                // Support for "%s" (at any place and can occur multiple times)
                int index = -1;
                for (int parameterIndex = 0; parameterIndex < formatParameters.Length; parameterIndex++)
                {
                    index = javaMsg.IndexOf('%', index + 1);
                    if (index < 0)
                    {
                        break;
                    }
                    string parameterFormat = javaMsg.Substring(index);
                    if (parameterFormat.StartsWith("%s", StringComparison.Ordinal))
                    {
                        // Convert an integer address to a String by reading
                        // the String at the given address
                        int address = ((int?)formatParameters[parameterIndex]).Value;
                        if (address == 0)
                        {
                            formatParameters[parameterIndex] = "(null)";
                        }
                        else
                        {
                            formatParameters[parameterIndex] = Utilities.readStringZ(address);
                        }
                    }
                }

                // String.format: If there are more arguments than format specifiers, the extra arguments are ignored.
                formattedMsg = string.format(javaMsg, formatParameters);
            }
            catch (Exception)
            {
                // Ignore formatting exception
            }

            return(formattedMsg);
        }
Exemplo n.º 42
0
 public int Execute(CpuState cpuState, IMemory memory)
 {
     int cycles = 3;
     ushort dest = memory.ReadShort(cpuState.Pc + 1);
     if (Variants[memory[cpuState.Pc]] == AddressingMode.Indirect)
     {
         dest = memory.ReadShort(dest);
         cycles = 5;
     }
     cpuState.Pc = dest;
     return cycles;
 }
Exemplo n.º 43
0
        public int Execute(CpuState cpuState, IMemory memory)
        {
            var breakSet = cpuState.IsFlagSet(CpuState.Flags.Break);
            cpuState.StatusRegister = cpuState.PopStack(memory);
            cpuState.SetFlag(CpuState.Flags.Break, breakSet);
            cpuState.StatusRegister |= 1 << 5;

            var low = cpuState.PopStack(memory);
            var high = cpuState.PopStack(memory);

            cpuState.Pc = (ushort)((high << 8) | low);

            return 6;
        }
Exemplo n.º 44
0
        protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, Action<byte> write, ref int cycles)
        {
            var sum = (ushort) (arg + cpuState.A);
            if (cpuState.IsFlagSet(CpuState.Flags.Carry))
            {
                ++sum;
            }
            var byteSum = (byte)(sum & 0xFF);

            cpuState.SetOverflow(cpuState.A, arg, byteSum);
            cpuState.A = byteSum;
            cpuState.SetZeroFlag(cpuState.A);
            cpuState.SetNegativeFlag(cpuState.A);
            cpuState.SetFlag(CpuState.Flags.Carry, sum & 0xFF00);
        }
Exemplo n.º 45
0
 public int Execute(CpuState cpuState, IMemory memory)
 {
     int cycles = 3;
     ushort dest = memory.ReadShort(cpuState.Pc + 1);
     if (Variants[memory[cpuState.Pc]] == AddressingMode.Indirect)
     {
         // Reading the address cannot page wrap
         var secondByte = (dest + 1) & 0xFF;
         secondByte |= dest & 0xFF00;
         dest = (ushort) (memory[dest] | (memory[secondByte] << 8));
         cycles = 5;
     }
     cpuState.Pc = dest;
     return cycles;
 }
Exemplo n.º 46
0
        protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, Action<byte> write, ref int cycles)
        {
            //     A - M - C -> A                   N Z C I D V
            //                                      + + + - - +

            int a = cpuState.A;
            arg ^= 0xFF;
            a += arg;
            if (cpuState.IsFlagSet(CpuState.Flags.Carry))
            {
                ++a;
            }
            var byteResult = (byte) (a & 0xFF);
            cpuState.SetOverflow(cpuState.A, arg, byteResult);
            cpuState.A = byteResult;

            cpuState.SetNegativeFlag(cpuState.A);
            cpuState.SetZeroFlag(cpuState.A);
            cpuState.SetFlag(CpuState.Flags.Carry, (byteResult & 0x80) == 0);
        }
Exemplo n.º 47
0
        protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, Action<byte> write, ref int cycles)
        {
            var res = (byte) ((arg - 1)&0xFF);
            write(res);
            cpuState.SetNegativeFlag(res);
            cpuState.SetZeroFlag(res);

            switch (Variants[memory[cpuState.Pc]])
            {
                case AddressingMode.ZeroPage:
                    cycles = 5;
                    break;
                case AddressingMode.ZeroPageXIndexed:
                    cycles = 6;
                    break;
                case AddressingMode.Absolute:
                    cycles = 6;
                    break;
                case AddressingMode.AbsoluteX:
                    cycles = 7;
                    break;
            }
        }
Exemplo n.º 48
0
 public override void Do(CpuState state, ushort args)
 {
     state.ProgramCounter++;
     action(state, (byte)(args >> 8), (byte)(0xF & (args >> 4)));
 }
Exemplo n.º 49
0
 protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, Action<byte> write, ref int cycles)
 {
     cpuState.SetFlag(CpuState.Flags.DecimalMode, false);
 }
Exemplo n.º 50
0
        public static string Disassemble(this IInstruction instruction, byte[] args, IMemory memory, CpuState cpuState)
        {
            switch (instruction.Variants[args[0]])
            {
                case AddressingMode.Accumulator:
                    return string.Format("{0} A", instruction.Mnemonic);
                case AddressingMode.Absolute:
                    if (instruction is JMP || instruction is JSR)
                    {
                        return string.Format("{0} ${1:X2}{2:X2}", instruction.Mnemonic, args[2], args[1]);
                    }
                    else
                    {
                        return string.Format("{0} ${1:X2}{2:X2} = {3:X2}", instruction.Mnemonic, args[2], args[1], memory[(args[2] << 8) | args[1]]);
                    }
                case AddressingMode.AbsoluteX:
                {
                    ushort addr = (ushort) ((args[2] << 8) | args[1]);
                    addr += cpuState.X;

                    return string.Format("{0} ${1:X2}{2:X2},X @ {3:X4} = {4:X2}", instruction.Mnemonic, args[2], args[1], addr, memory[addr]);
                }
                case AddressingMode.AbsoluteY:
                {
                    ushort addr = (ushort) ((args[2] << 8) | args[1]);
                    addr += cpuState.Y;

                    return string.Format("{0} ${1:X2}{2:X2},Y @ {3:X4} = {4:X2}", instruction.Mnemonic, args[2], args[1], addr, memory[addr]);
                }
                case AddressingMode.Immediate:
                    return string.Format("{0} #${1:X2}", instruction.Mnemonic, args[1]);
                case AddressingMode.Implied:
                    return instruction.Mnemonic;
                case AddressingMode.Indirect:
                    return string.Format("{0} (${1:X2}{2:X2}) = {3:X4}", instruction.Mnemonic, args[2], args[1], memory.ReadShort((args[2] << 8) | args[1]));
                case AddressingMode.XIndexedIndirect:
                {
                    byte addr = (byte) (args[1] + cpuState.X);
                    var finalAddr = memory.ReadZeroPageShort(addr);
                    return string.Format("{0} (${1:X2},X) @ {2:X2} = {3:X4} = {4:X2}", instruction.Mnemonic, args[1],
                        addr, finalAddr, memory[finalAddr]);
                }

                case AddressingMode.IndirectYIndexed:
                {
                    ushort addr = memory.ReadZeroPageShort(args[1]);
                    ushort finalAddr = (ushort) (addr + cpuState.Y);
                    return string.Format("{0} (${1:X2}),Y = {2:X4} @ {3:X4} = {4:X2}", instruction.Mnemonic, args[1],
                        addr, finalAddr, memory[finalAddr]);
                }

                case AddressingMode.Relative:
                {
                    ushort newPc;
                    if ((args[1] & 0x80) != 0)
                    {
                        newPc = (ushort)(cpuState.Pc - (0x100 - args[1]));
                    }
                    else
                    {
                        newPc = (ushort)(cpuState.Pc + args[1]);
                    }
                    return string.Format("{0} ${1:X4}", instruction.Mnemonic, newPc + 2);
                }
                case AddressingMode.ZeroPage:
                    return string.Format("{0} ${1:X2} = {2:X2}", instruction.Mnemonic, args[1], memory[args[1]]);
                case AddressingMode.ZeroPageXIndexed:
                    return string.Format("{0} ${1:X2},X", instruction.Mnemonic, args[1]);
                case AddressingMode.ZeroPageYIndexed:
                    return string.Format("{0} ${1:X2},Y", instruction.Mnemonic, args[1]);
                default:
                    throw new Exception("Disassembly for addressing mode is unimplemented");
            }
        }
Exemplo n.º 51
0
 protected override bool ShouldBranch(CpuState cpuState, IMemory memory)
 {
     return !cpuState.IsFlagSet(CpuState.Flags.Zero);
 }
Exemplo n.º 52
0
 public override void Do(CpuState state, ushort args)
 {
     state.ProgramCounter = func(state, (FlowControlCondition)(args >> 10));
 }
Exemplo n.º 53
0
 protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, ref int cycles)
 {
     cpuState.SetFlag(CpuState.Flags.InterruptDisable, true);
 }
Exemplo n.º 54
0
 protected abstract bool ShouldBranch(CpuState cpuState, IMemory memory);
Exemplo n.º 55
0
 public override void Do(CpuState state, ushort args)
 {
     state.ProgramCounter++;
     func(state, args != 0);
 }
Exemplo n.º 56
0
 protected override void InternalExecute(CpuState cpuState, IMemory memory,  Action<byte> write, ref int cycles)
 {
     write(cpuState.Y);
 }
Exemplo n.º 57
0
 protected override bool ShouldBranch(CpuState cpuState, IMemory memory)
 {
     return cpuState.IsFlagSet(CpuState.Flags.Negative);
 }
Exemplo n.º 58
0
 protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, Action<byte> write, ref int cycles)
 {
     // NOP
 }
 public override void Do(CpuState state, ushort args)
 {
     state.ProgramCounter++;
     action(state);
 }
Exemplo n.º 60
0
 protected override void InternalExecute(CpuState cpuState, IMemory memory, byte arg, Action<byte> write, ref int cycles)
 {
     cpuState.PushStack(cpuState.A, memory);
     cycles = 3;
 }