private bool CALL(ushort instruction) { Stack.Push(InstructionPointer); InstructionPointer = ParserFunctions.GetAddress(instruction); return(false); }
private bool SKP(ushort instruction) { var value = ParserFunctions.GetValue(instruction); var x = ParserFunctions.GetX(instruction); var key = Register[x]; switch (value) { case 0x9e: if (keyboard.GetValue(key)) { InstructionPointer += 2; } break; case 0xa1: if (!keyboard.GetValue(key)) { InstructionPointer += 2; break; } break; default: throw new InvalidOperationException(); } return(false); }
public bool ProcessInstruction(ushort instruction) { var opcode = ParserFunctions.GetOpCode(instruction); var func = InstructionSet[opcode]; return(func(instruction)); }
private bool LD(ushort instruction) { byte x = ParserFunctions.GetX(instruction); var value = ParserFunctions.GetValue(instruction); Register[x] = value; return(false); }
private bool ADD(ushort instruction) { int register = ParserFunctions.GetX(instruction); byte value = ParserFunctions.GetValue(instruction); Register[register] += value; return(false); }
private bool RND(ushort instruction) { byte randomValue = RandomFunc(); var register = ParserFunctions.GetX(instruction); var mask = ParserFunctions.GetValue(instruction); Register[register] = (byte)(randomValue & mask); return(false); }
private bool JPV(ushort instruction) { byte offset = Register[0]; ushort address = ParserFunctions.GetAddress(instruction); ushort ipaddress = (ushort)(offset + address); InstructionPointer = ipaddress; return(true); }
private bool REG(ushort instruction) { byte subcommand = ParserFunctions.GetSubCommand(instruction); var command = RegisterCommands[subcommand]; var regX = ParserFunctions.GetX(instruction); var regY = ParserFunctions.GetY(instruction); command(regX, regY); return(false); }
private bool SE(ushort instruction) { byte x = ParserFunctions.GetX(instruction); byte regValue = Register[x]; byte opValue = ParserFunctions.GetValue(instruction); if (regValue == opValue) { InstructionPointer += 2; } return(false); }
private bool SNEV(ushort instruction) { var regX = ParserFunctions.GetX(instruction); var regY = ParserFunctions.GetY(instruction); var regValueX = Register[regX]; var regValueY = Register[regY]; if (regValueX != regValueY) { InstructionPointer += 2; } return(true); }
private bool SEV(ushort instruction) { byte x = ParserFunctions.GetX(instruction); byte regValueX = Register[x]; byte y = ParserFunctions.GetY(instruction); byte regValueY = Register[y]; if (regValueX == regValueY) { InstructionPointer += 2; } return(false); }
private bool SYS(ushort instruction) { if (ParserFunctions.GetAddress(instruction) == 0xee) { return(RET()); } if (ParserFunctions.GetAddress(instruction) == 0x00) { return(true); } if (ParserFunctions.GetAddress(instruction) == 0xe0) { display.Screen.Initialize(); } return(false); }
private bool DRW(ushort instruction) { var Vx = ParserFunctions.GetX(instruction); var Vy = ParserFunctions.GetY(instruction); var x = Register[Vx]; var y = Register[Vy]; var count = ParserFunctions.GetSubCommand(instruction); byte[] sprite = new byte[count]; for (ushort i = 0; i < count; i++) { sprite[i] = memory.GetValue((ushort)(AddressRegister + i)); } var collision = SetSprite(x, y, sprite, count); Register[0xF] = (byte)collision; // Draw screen display.Print(); return(false); }
public string ParseInstructon(ushort instruction) { ParserFunctions.ParseResult result = ParserFunctions.ParseInstruction(instruction); if (result.OpCode == 0x0) { if (result.Value == 0xe0) { return("CLS"); } if (result.Value == 0xee) { return("RET"); } } if (result.OpCode == 0x1) { return(string.Format("JP 0x{0:x3}", result.Address)); } if (result.OpCode == 0x2) { return(string.Format("CALL 0x{0:x3}", result.Address)); } if (result.OpCode == 0x3) { return(string.Format("SE V{0:x}, 0x{1:x2}", result.X, result.Value)); } if (result.OpCode == 0x4) { return(string.Format("SNE V{0:x}, 0x{1:x2}", result.X, result.Value)); } if (result.OpCode == 0x5) { return(string.Format("SE V{0:x}, V{1:x}", result.X, result.Y)); } if (result.OpCode == 0x3) { return(string.Format("LD V{0:x}, 0x{1:x2}", result.X, result.Value)); } if (result.OpCode == 0x7) { return(string.Format("ADD V{0:x}, 0x{1:x2}", result.X, result.Value)); } if (result.OpCode == 0x8) { if (result.SubCommand == 0x0) { return(string.Format("LD V{0:x}, V{1:x}", result.X, result.Y)); } if (result.SubCommand == 0x1) { return(string.Format("OR V{0:x}, V{1:x}", result.X, result.Y)); } if (result.SubCommand == 0x2) { return(string.Format("AND V{0:x}, V{1:x}", result.X, result.Y)); } if (result.SubCommand == 0x3) { return(string.Format("XOR V{0:x}, V{1:x}", result.X, result.Y)); } if (result.SubCommand == 0x4) { return(string.Format("ADD V{0:x}, V{1:x}", result.X, result.Y)); } if (result.SubCommand == 0x5) { return(string.Format("SUB V{0:x}, V{1:x}", result.X, result.Y)); } if (result.SubCommand == 0x6) { return(string.Format("SHR V{0:x}", result.X)); } if (result.SubCommand == 0x7) { return(string.Format("SUBN V{0:x}, V{1:x}", result.X, result.Y)); } if (result.SubCommand == 0xe) { return(string.Format("SHL V{0:x}", result.X)); } } if (result.OpCode == 0x9) { return(string.Format("SNE V{0:x}, V{1:x}", result.X, result.Y)); } if (result.OpCode == 0xa) { return(string.Format("LD I, 0x{0:x3}", result.Address)); } if (result.OpCode == 0xb) { return(string.Format("JP V0, 0x{0:x3}", result.Address)); } if (result.OpCode == 0xc) { return(string.Format("RND V{0:x}, 0x{1:x2}", result.X, result.Value)); } if (result.OpCode == 0xd) { return(string.Format("DRW V{0:x}, V{1:x}, 0x{2:x}", result.X, result.Y, result.SubCommand)); } if (result.OpCode == 0xe) { if ((instruction & 0xff) == 0x9e) { return(string.Format("SKP V{0:x}", result.X)); } if ((instruction & 0xff) == 0xa1) { return(string.Format("SKNP V{0:x}", result.X)); } } if (result.OpCode == 0xf) { if ((instruction & 0xff) == 0x07) { return(string.Format("LD V{0:x}, DT", result.X)); } if ((instruction & 0xff) == 0x0a) { return(string.Format("LD V{0:x}, K", result.X)); } if ((instruction & 0xff) == 0x15) { return(string.Format("LD DT, V{0:x}", result.X)); } if ((instruction & 0xff) == 0x18) { return(string.Format("LD ST, V{0:x}", result.X)); } if ((instruction & 0xff) == 0x1E) { return(string.Format("ADD I, V{0:x}", result.X)); } if ((instruction & 0xff) == 0x29) { return(string.Format("LD F, V{0:x}", result.X)); } if ((instruction & 0xff) == 0x33) { return(string.Format("LD B, V{0:x}", result.X)); } if ((instruction & 0xff) == 0x55) { return(string.Format("LD [I], V{0:x}", result.X)); } if ((instruction & 0xff) == 0x65) { return(string.Format("LD V{0:x}, [I]", result.X)); } } return(""); }
bool LDS(ushort instruction) { var register = ParserFunctions.GetX(instruction); var subcommand = ParserFunctions.GetValue(instruction); switch (subcommand) { case 0x15: DelayTimer.Value = Register[register]; break; case 0x07: Register[register] = DelayTimer.Value; break; case 0x18: SoundTimer.Value = Register[register]; break; case 0x1e: AddressRegister += Register[register]; break; case 0x29: AddressRegister = (ushort)(Register[register] * SPRITE_SIZE); break; case 0x33: byte value = Register[register]; byte hundreds = (byte)(value / 100); byte tens = (byte)((value - (hundreds * 100)) / 10); byte ones = (byte)(value - ((hundreds * 100) + (tens * 10))); memory.SetValue(AddressRegister, hundreds); memory.SetValue((ushort)(AddressRegister + 1), tens); memory.SetValue((ushort)(AddressRegister + 2), ones); break; case 0x55: for (int i = 0; i <= register; i++) { ushort address = (ushort)(AddressRegister + i); memory.SetValue(address, Register[i]); } break; case 0x0a: var key = keyboard.WaitForValue(); Register[register] = key; break; case 0x65: for (int i = 0; i <= register; i++) { ushort address = (ushort)(AddressRegister + i); Register[i] = memory.GetValue(address); } break; default: throw new NotImplementedException(string.Format("Subcommand {0:x} not implemented", subcommand)); } return(false); }
private bool LDI(ushort instruction) { AddressRegister = ParserFunctions.GetAddress(instruction); return(false); }
private bool JP(ushort instruction) { InstructionPointer = ParserFunctions.GetAddress(instruction); return(false); }