Little refactor call instruction

This commit is contained in:
Franco Colmenarez 2021-10-17 12:48:18 -05:00
parent ebadad803b
commit 31e0b2c79c

View File

@ -412,7 +412,7 @@ impl CPU {
}, },
OpcodeParameter::Register_U8(reg, val) => { OpcodeParameter::Register_U8(reg, val) => {
self.registers.increment(Register::PC, 2); self.registers.increment(Register::PC, 2);
self.registers.set(reg, self.registers.get(reg) | (val as u16)); self.registers.set(reg, self.registers.get(reg) & (val as u16));
self.registers.set_flag(FlagRegister::Zero, self.registers.get(reg) == 0); self.registers.set_flag(FlagRegister::Zero, self.registers.get(reg) == 0);
self.registers.set_flag(FlagRegister::Substract, false); self.registers.set_flag(FlagRegister::Substract, false);
self.registers.set_flag(FlagRegister::HalfCarry, true); self.registers.set_flag(FlagRegister::HalfCarry, true);
@ -535,15 +535,18 @@ impl CPU {
_ => {}, _ => {},
}, },
// CALL // CALL
Opcode::CALL(params) => match params { Opcode::CALL(params) => {
OpcodeParameter::U16(address) => { self.registers.increment(Register::PC, 3);
let pc = self.registers.get(Register::PC); match params {
self.registers.decrement(Register::SP, 2); OpcodeParameter::U16(address) => {
let sp = self.registers.get(Register::SP); let pc = self.registers.get(Register::PC);
bus.write_16bit(sp, pc + 3); self.registers.decrement(Register::SP, 2);
self.registers.set(Register::PC, address); let sp = self.registers.get(Register::SP);
}, bus.write_16bit(sp, pc);
_ => {}, self.registers.set(Register::PC, address);
},
_ => {},
};
}, },
// RST, same as Call // RST, same as Call
Opcode::RST(address) => self.exec(Opcode::CALL(OpcodeParameter::U16(address as u16)), bus), Opcode::RST(address) => self.exec(Opcode::CALL(OpcodeParameter::U16(address as u16)), bus),
@ -1202,8 +1205,8 @@ mod tests {
let mut cpu = CPU::new(); let mut cpu = CPU::new();
cpu.registers.set(Register::B, 0x1F); cpu.registers.set(Register::B, 0x1F);
cpu.exec(Opcode::AND(OpcodeParameter::Register_U8(Register::B, 0x1F)), &mut bus); cpu.exec(Opcode::AND(OpcodeParameter::Register_U8(Register::B, 0x1A)), &mut bus);
assert_eq!(cpu.registers.get(Register::B), 0x1F & 0x1F); assert_eq!(cpu.registers.get(Register::B), 0x1F & 0x1A);
assert_eq!(cpu.registers.get_flag(FlagRegister::Zero), false); assert_eq!(cpu.registers.get_flag(FlagRegister::Zero), false);
assert_eq!(cpu.registers.get_flag(FlagRegister::Substract), false); assert_eq!(cpu.registers.get_flag(FlagRegister::Substract), false);
assert_eq!(cpu.registers.get_flag(FlagRegister::HalfCarry), true); assert_eq!(cpu.registers.get_flag(FlagRegister::HalfCarry), true);