diff --git a/src/processor/mod.rs b/src/processor/mod.rs index 01204b2..97f7174 100644 --- a/src/processor/mod.rs +++ b/src/processor/mod.rs @@ -10,10 +10,10 @@ mod opcodes; #[allow(dead_code)] #[derive(PartialEq)] enum FLAGS { - Z = 7, - N = 6, - H = 5, - C = 4, + Zero = 7, + NSubtract = 6, + HalfCarry = 5, + Carry = 4, } enum Direction { @@ -69,19 +69,19 @@ impl CPU { let (mut rotated, carry) = rotate(byte, &direction); if carry { rotated += get_rotation_carry(&direction); - self.set_flag(FLAGS::C); + self.set_flag(FLAGS::Carry); } return rotated; } fn rotate(&mut self, byte: u8, direction: Direction) -> u8 { - let old_carry = self.get_flag(FLAGS::C); + let old_carry = self.get_flag(FLAGS::Carry); let (mut rotated, carry) = rotate(byte, &direction); if old_carry > 0 { rotated += get_rotation_carry(&direction); } if carry { - self.set_flag(FLAGS::C); + self.set_flag(FLAGS::Carry); } return rotated; } @@ -107,13 +107,13 @@ impl CPU { fn shift(&mut self, byte: u8, direction: Direction) -> u8 { let (rotated, carry) = rotate(byte, &direction); if carry { - self.set_flag(FLAGS::C); + self.set_flag(FLAGS::Carry); } return rotated; } fn bit(&mut self, byte: u8, bit: u8) { - self.set_or_clear_flag(FLAGS::Z, !get_bit(byte, bit)); + self.set_or_clear_flag(FLAGS::Zero, !get_bit(byte, bit)); } fn rst(&mut self, address: u16) { @@ -177,7 +177,7 @@ impl CPU { } fn set_flag(&mut self, flag: FLAGS) { - if flag == FLAGS::Z { + if flag == FLAGS::Zero { verbose_println!("setting z flag"); } unsafe { @@ -215,11 +215,11 @@ impl CPU { fn add_u8s(&mut self, first: u8, second: u8) -> u8 { let (result, carry) = first.overflowing_add(second); - self.clear_flag(FLAGS::N); - self.set_or_clear_flag(FLAGS::C, carry); - self.set_or_clear_flag(FLAGS::Z, result == 0x0); + self.clear_flag(FLAGS::NSubtract); + self.set_or_clear_flag(FLAGS::Carry, carry); + self.set_or_clear_flag(FLAGS::Zero, result == 0x0); self.set_or_clear_flag( - FLAGS::H, + FLAGS::HalfCarry, (((first & 0xF).wrapping_add(second & 0xF)) & 0x10) == 0x10, ); return result; @@ -227,11 +227,11 @@ impl CPU { fn add_u16s(&mut self, first: u16, second: u16) -> u16 { let (result, carry) = first.overflowing_add(second); - self.clear_flag(FLAGS::N); - self.set_or_clear_flag(FLAGS::C, carry); - self.set_or_clear_flag(FLAGS::Z, result == 0x0); + self.clear_flag(FLAGS::NSubtract); + self.set_or_clear_flag(FLAGS::Carry, carry); + self.set_or_clear_flag(FLAGS::Zero, result == 0x0); self.set_or_clear_flag( - FLAGS::H, + FLAGS::HalfCarry, (((first & 0xFFF).wrapping_add(second & 0xFFF)) & 0x1000) == 0x1000, ); return result; @@ -239,11 +239,11 @@ impl CPU { fn sub_u8s(&mut self, first: u8, second: u8) -> u8 { let (result, carry) = first.overflowing_sub(second); - self.set_flag(FLAGS::N); - self.set_or_clear_flag(FLAGS::C, carry); - self.set_or_clear_flag(FLAGS::Z, result == 0x0); + self.set_flag(FLAGS::NSubtract); + self.set_or_clear_flag(FLAGS::Carry, carry); + self.set_or_clear_flag(FLAGS::Zero, result == 0x0); self.set_or_clear_flag( - FLAGS::H, + FLAGS::HalfCarry, (((first & 0xF).wrapping_sub(second & 0xF)) & 0x10) == 0x10, ); return result; @@ -251,11 +251,11 @@ impl CPU { fn sub_u16s(&mut self, first: u16, second: u16) -> u16 { let (result, carry) = first.overflowing_sub(second); - self.set_flag(FLAGS::N); - self.set_or_clear_flag(FLAGS::C, carry); - self.set_or_clear_flag(FLAGS::Z, result == 0x0); + self.set_flag(FLAGS::NSubtract); + self.set_or_clear_flag(FLAGS::Carry, carry); + self.set_or_clear_flag(FLAGS::Zero, result == 0x0); self.set_or_clear_flag( - FLAGS::H, + FLAGS::HalfCarry, (((first & 0xFFF).wrapping_sub(second & 0xFFF)) & 0x1000) == 0x1000, ); return result; diff --git a/src/processor/opcodes.rs b/src/processor/opcodes.rs index cd92410..3f81dce 100644 --- a/src/processor/opcodes.rs +++ b/src/processor/opcodes.rs @@ -83,7 +83,7 @@ impl CPU { }, 0x20 => { let jump_size = self.ld_immediate_byte(); - if self.get_flag(FLAGS::Z) == 0 { + if self.get_flag(FLAGS::Zero) == 0 { verbose_println!("z flag is 0... so doing jump..."); unsafe { self.state.pc.as_u16 = self @@ -96,7 +96,7 @@ impl CPU { unsafe { verbose_println!( "not jumping! z flag is {0:#b}, flags are {1:#b} / {1:#X}", - self.get_flag(FLAGS::Z), + self.get_flag(FLAGS::Zero), self.state.af.as_u8s.right ); } @@ -118,18 +118,20 @@ impl CPU { 0x26 => self.state.hl.as_u8s.left = self.ld_immediate_byte(), 0x27 => unsafe { verbose_println!("Running DAA instruction (0x27) that I'm not too sure about..."); - if self.get_flag(FLAGS::N) == 0 { - if self.get_flag(FLAGS::C) == 1 || self.state.af.as_u8s.left > 0x99 { + if self.get_flag(FLAGS::NSubtract) == 0 { + if self.get_flag(FLAGS::Carry) == 1 || self.state.af.as_u8s.left > 0x99 { self.state.af.as_u8s.left += 0x60; } - if self.get_flag(FLAGS::H) == 1 || (self.state.af.as_u8s.left & 0x0f) > 0x09 { + if self.get_flag(FLAGS::HalfCarry) == 1 + || (self.state.af.as_u8s.left & 0x0f) > 0x09 + { self.state.af.as_u8s.left += 0x6; } } else { - if self.get_flag(FLAGS::C) == 1 { + if self.get_flag(FLAGS::Carry) == 1 { self.state.af.as_u8s.left -= 0x60; } - if self.get_flag(FLAGS::H) == 1 { + if self.get_flag(FLAGS::HalfCarry) == 1 { self.state.af.as_u8s.left -= 0x6; } } @@ -140,7 +142,7 @@ impl CPU { }, 0x28 => { let jump_size = self.ld_immediate_byte(); - if self.get_flag(FLAGS::Z) == 1 { + if self.get_flag(FLAGS::Zero) == 1 { unsafe { self.state.pc.as_u16 = self .state @@ -168,7 +170,7 @@ impl CPU { 0x2F => unsafe { self.state.af.as_u8s.left = !self.state.af.as_u8s.left }, 0x30 => { let jump_size = self.ld_immediate_byte(); - if self.get_flag(FLAGS::C) == 0 { + if self.get_flag(FLAGS::Carry) == 0 { unsafe { self.state.pc.as_u16 = self .state @@ -199,10 +201,10 @@ impl CPU { let data = self.ld_immediate_byte(); self.memory.set(self.state.hl.as_u16, data); }, - 0x37 => self.set_flag(FLAGS::C), + 0x37 => self.set_flag(FLAGS::Carry), 0x38 => { let jump_size = self.ld_immediate_byte(); - if self.get_flag(FLAGS::C) == 1 { + if self.get_flag(FLAGS::Carry) == 1 { unsafe { self.state.pc.as_u16 = self .state @@ -227,7 +229,7 @@ impl CPU { self.state.af.as_u8s.left = self.sub_u8s(self.state.af.as_u8s.left, 1) }, 0x3E => self.state.af.as_u8s.left = self.ld_immediate_byte(), - 0x3F => self.toggle_flag(FLAGS::C), + 0x3F => self.toggle_flag(FLAGS::Carry), 0x40 => {} 0x41 => unsafe { self.state.bc.as_u8s.left = self.state.bc.as_u8s.right }, 0x42 => unsafe { self.state.bc.as_u8s.left = self.state.de.as_u8s.left }, @@ -348,46 +350,46 @@ impl CPU { self.add_u8s(self.state.af.as_u8s.left, self.state.af.as_u8s.left) }, 0x88 => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.add_u8s(self.state.af.as_u8s.left, self.state.bc.as_u8s.left + f) }, 0x89 => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.add_u8s( self.state.af.as_u8s.left, self.state.bc.as_u8s.right.wrapping_add(f), ) }, 0x8A => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.add_u8s(self.state.af.as_u8s.left, self.state.de.as_u8s.left + f) }, 0x8B => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.add_u8s(self.state.af.as_u8s.left, self.state.de.as_u8s.right + f) }, 0x8C => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.add_u8s(self.state.af.as_u8s.left, self.state.hl.as_u8s.left + f) }, 0x8D => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.add_u8s(self.state.af.as_u8s.left, self.state.hl.as_u8s.right + f) }, 0x8E => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.add_u8s( self.state.af.as_u8s.left, self.memory.get(self.state.hl.as_u16).wrapping_add(f), ) }, 0x8F => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.add_u8s(self.state.af.as_u8s.left, self.state.af.as_u8s.left + f) }, @@ -426,44 +428,44 @@ impl CPU { self.sub_u8s(self.state.af.as_u8s.left, self.state.af.as_u8s.left) }, 0x98 => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.sub_u8s(self.state.af.as_u8s.left, self.state.bc.as_u8s.left + f) }, 0x99 => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.sub_u8s(self.state.af.as_u8s.left, self.state.bc.as_u8s.right + f) }, 0x9A => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.sub_u8s(self.state.af.as_u8s.left, self.state.de.as_u8s.left + f) }, 0x9B => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.sub_u8s(self.state.af.as_u8s.left, self.state.de.as_u8s.right + f) }, 0x9C => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.sub_u8s(self.state.af.as_u8s.left, self.state.hl.as_u8s.left + f) }, 0x9D => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.sub_u8s(self.state.af.as_u8s.left, self.state.hl.as_u8s.right + f) }, 0x9E => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.sub_u8s( self.state.af.as_u8s.left, self.memory.get(self.state.hl.as_u16) + f, ) }, 0x9F => unsafe { - let f = self.get_flag(FLAGS::C); + let f = self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.sub_u8s(self.state.af.as_u8s.left, self.state.af.as_u8s.left + f) }, @@ -577,42 +579,42 @@ impl CPU { }, 0xB8 => unsafe { if self.state.af.as_u8s.left == self.state.bc.as_u8s.left { - self.set_flag(FLAGS::Z); + self.set_flag(FLAGS::Zero); } }, 0xB9 => unsafe { if self.state.af.as_u8s.left == self.state.bc.as_u8s.right { - self.set_flag(FLAGS::Z); + self.set_flag(FLAGS::Zero); } }, 0xBA => unsafe { if self.state.af.as_u8s.left == self.state.de.as_u8s.left { - self.set_flag(FLAGS::Z); + self.set_flag(FLAGS::Zero); } }, 0xBB => unsafe { if self.state.af.as_u8s.left == self.state.de.as_u8s.right { - self.set_flag(FLAGS::Z); + self.set_flag(FLAGS::Zero); } }, 0xBC => unsafe { if self.state.af.as_u8s.left == self.state.hl.as_u8s.left { - self.set_flag(FLAGS::Z); + self.set_flag(FLAGS::Zero); } }, 0xBD => unsafe { if self.state.af.as_u8s.left == self.state.hl.as_u8s.right { - self.set_flag(FLAGS::Z); + self.set_flag(FLAGS::Zero); } }, 0xBE => unsafe { if self.state.af.as_u8s.left == self.memory.get(self.state.hl.as_u16) { - self.set_flag(FLAGS::Z); + self.set_flag(FLAGS::Zero); } }, - 0xBF => self.set_flag(FLAGS::Z), + 0xBF => self.set_flag(FLAGS::Zero), 0xC0 => unsafe { - if self.get_flag(FLAGS::Z) == 0 { + if self.get_flag(FLAGS::Zero) == 0 { let address = self.state.sp.as_u16; self.state.pc.as_u8s.left = self.memory.get(address); self.state.pc.as_u8s.right = self.memory.get(address + 1); @@ -627,7 +629,7 @@ impl CPU { }, 0xC2 => { let word = self.ld_immediate_word(); - if self.get_flag(FLAGS::Z) == 0 { + if self.get_flag(FLAGS::Zero) == 0 { self.state.pc = word; } } @@ -636,7 +638,7 @@ impl CPU { } 0xC4 => { let maybe_next = self.ld_immediate_word(); - if self.get_flag(FLAGS::Z) == 0 { + if self.get_flag(FLAGS::Zero) == 0 { self.push(self.state.pc); self.state.pc = maybe_next; } @@ -648,14 +650,14 @@ impl CPU { }, 0xC7 => self.rst(0x00), 0xC8 => { - if self.get_flag(FLAGS::Z) == 1 { + if self.get_flag(FLAGS::Zero) == 1 { self.state.pc = self.pop_word() } } 0xC9 => self.state.pc = self.pop_word(), 0xCA => { let maybe_next = self.ld_immediate_word(); - if self.get_flag(FLAGS::Z) == 1 { + if self.get_flag(FLAGS::Zero) == 1 { self.state.pc = maybe_next; } } @@ -665,7 +667,7 @@ impl CPU { } 0xCC => { let maybe_next = self.ld_immediate_word(); - if self.get_flag(FLAGS::Z) == 1 { + if self.get_flag(FLAGS::Zero) == 1 { self.push(self.state.pc); self.state.pc = maybe_next; } @@ -675,12 +677,12 @@ impl CPU { self.state.pc = self.ld_immediate_word(); } 0xCE => unsafe { - let t = self.ld_immediate_byte() + self.get_flag(FLAGS::C); + let t = self.ld_immediate_byte() + self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.add_u8s(self.state.af.as_u8s.left, t) }, 0xCF => self.rst(0x08), 0xD0 => unsafe { - if self.get_flag(FLAGS::C) == 0 { + if self.get_flag(FLAGS::Carry) == 0 { let address = self.state.sp.as_u16; self.state.pc.as_u8s.left = self.memory.get(address); self.state.pc.as_u8s.right = self.memory.get(address + 1); @@ -695,14 +697,14 @@ impl CPU { }, 0xD2 => { let word = self.ld_immediate_word(); - if self.get_flag(FLAGS::C) == 0 { + if self.get_flag(FLAGS::Carry) == 0 { self.state.pc = word; } } 0xD3 => undefined(0xD3), 0xD4 => { let maybe_next = self.ld_immediate_word(); - if self.get_flag(FLAGS::C) == 0 { + if self.get_flag(FLAGS::Carry) == 0 { self.push(self.state.pc); self.state.pc = maybe_next; } @@ -714,7 +716,7 @@ impl CPU { }, 0xD7 => self.rst(0x10), 0xD8 => { - if self.get_flag(FLAGS::C) == 1 { + if self.get_flag(FLAGS::Carry) == 1 { self.state.pc = self.pop_word() } } @@ -724,21 +726,21 @@ impl CPU { } 0xDA => { let maybe_next = self.ld_immediate_word(); - if self.get_flag(FLAGS::C) == 1 { + if self.get_flag(FLAGS::Carry) == 1 { self.state.pc = maybe_next; } } 0xDB => undefined(0xDB), 0xDC => { let maybe_next = self.ld_immediate_word(); - if self.get_flag(FLAGS::C) == 1 { + if self.get_flag(FLAGS::Carry) == 1 { self.push(self.state.pc); self.state.pc = maybe_next; } } 0xDD => undefined(0xDE), 0xDE => unsafe { - let t = self.ld_immediate_byte() + self.get_flag(FLAGS::C); + let t = self.ld_immediate_byte() + self.get_flag(FLAGS::Carry); self.state.af.as_u8s.left = self.sub_u8s(self.state.af.as_u8s.left, t) }, 0xDF => self.rst(0x18), @@ -845,7 +847,7 @@ impl CPU { 0xFD => undefined(0xFD), 0xFE => unsafe { if self.ld_immediate_byte() == self.state.af.as_u8s.left { - self.set_flag(FLAGS::Z) + self.set_flag(FLAGS::Zero) } }, 0xFF => self.rst(0x38),