/// <summary> /// Adds a 8 bit signed value to the Stack Pointer. /// </summary> /// <param name="op">The op.</param> /// <param name="memory">The memory.</param> /// <returns>The number of ticks the operation took to complete.</returns> /// <remarks>Affected Flags: - - H C</remarks> public static int AddSpR8(Opcode op, GbMemory memory) { sbyte v = (sbyte)memory.ReadCycleI8(); memory.Update(); memory.Update(); memory.R.F = (((memory.R.Sp & 0x0F) + (v & 0x0F)) > 0x0F ? RFlags.HB : byte.MinValue) .AssignBit(RFlags.CF, ((memory.R.Sp & 0xFF) + (v & 0xFF)) > 0xFF); memory.R.Sp += v; return(4); }
/// <summary> /// Conditional Return. /// </summary> /// <param name="op">The opcode.</param> /// <param name="mem">The memory.</param> public static int RetC(Opcode op, GbMemory mem) { mem.Update(); if (GetConditionalJumpState(op.Dest, op.Src, mem.R.F)) { return(2); } byte low = mem.ReadCyclePop(); mem.R.Pc = new GbUInt16(mem.ReadCyclePop(), low); mem.Update(); return(5); }
/// <summary> /// Runs an instruction and returns the number of ticks it took to complete. /// </summary> /// <param name="memory">The memory.</param> /// <returns>The number of ticks that the instruction took to run.</returns> public static int Run(GbMemory memory) { memory.Update(1); byte opcode = memory.LdI8(); memory.Update(1); if (memory.HaltBugged) { memory.HaltBugged = false; memory.R.Pc--; } return(NmOps[opcode].Invoke(memory)); }
public static int Push(Opcode op, GbMemory mem) { GbUInt16 r = mem.R.GetR16Af(op.Dest); mem.Update(); mem.WriteCyclePush(r.HighByte); mem.WriteCyclePush(r.LowByte); return(4); }
public static int Ret(Opcode op, GbMemory mem) { byte low = mem.ReadCyclePop(); mem.R.Pc = new GbUInt16(mem.ReadCyclePop(), low); mem.Update(); mem.IME |= op.Dest != 0; // Unlike EI IME gets enabled right away. mem.NextIMEValue = mem.IME; return(4); }
public static int AddHl(Opcode op, GbMemory mem) { mem.Update(); GbUInt16 val = mem.R.GetR16Sp(op.Src); mem.R.F = mem.R.F.Res(RFlags.NF).AssignBit(RFlags.HF, (((mem.R.Hl & 0xFFF) + (val & 0xFFF)) & 0x1000) == 0x1000) .AssignBit(RFlags.CF, val + mem.R.Hl < mem.R.Hl); mem.R.Hl += val; return(2); }
public static int LdHlSpR8(Opcode op, GbMemory mem) { sbyte s = (sbyte)mem.ReadCycleI8(); mem.Update(); mem.R.F = (((mem.R.Sp & 0xFF) + (s & 0xFF)) > 0xFF ? RFlags.CB : (byte)0).AssignBit(RFlags.HF, ((mem.R.Sp & 0x0F) + (s & 0x0F)) > 0x0F); mem.R.Hl = mem.R.Sp + s; return(3); }
public static int Jr8(Opcode op, GbMemory mem) { byte val = mem.ReadCycleI8(); if (op.Src != 0 && GetConditionalJumpState(op.Dest, op.Src, mem.R.F)) { return(2); } mem.Update(); mem.R.Pc += (sbyte)val; return(3); }
public static int Call(Opcode op, GbMemory mem) { byte low = mem.ReadCycleI8(); byte high = mem.ReadCycleI8(); if (op.Src != 0 && GetConditionalJumpState(op.Dest, op.Src, mem.R.F)) { return(3); } mem.Update(); mem.Call(new GbUInt16(high, low)); return(6); }
public static int Rst(Opcode op, GbMemory mem) { mem.Update(); mem.Call(new GbUInt16(0, (byte)(op.Dest * 8))); return(4); }
/// <summary> /// Increments src. /// </summary> /// <param name="op">The op.</param> /// <param name="mem">The memory.</param> /// <returns>The number of ticks the operation took to complete.</returns> /// <remarks>Affected flags: - - - -</remarks> public static int Inc16(Opcode op, GbMemory mem) { mem.Update(); mem.R.SetR16(op.Dest, mem.R.GetR16Sp(op.Dest) + (GbUInt16)1); return(2); }
/// <summary> /// Decrements src. /// </summary> /// <param name="op">The op.</param> /// <param name="mem">The memory.</param> /// <returns>The number of ticks the operation took to complete.</returns> /// <remarks>Affected flags: - - - -</remarks> public static int Dec16(Opcode op, GbMemory mem) { mem.Update(); mem.R.SetR16(op.Dest, mem.R.GetR16Sp(op.Dest) - 1); return(2); }
public static int LdSpHl(Opcode op, GbMemory memory) { memory.Update(); memory.R.Sp = memory.R.Hl; return(2); }