fixes & all opcodes!!
This commit is contained in:
parent
8f3cf515a9
commit
4fcde0fd82
1 changed files with 420 additions and 19 deletions
439
src/processor.rs
439
src/processor.rs
|
@ -48,9 +48,7 @@ impl CPU {
|
|||
self.state.bc.as_u8s.left = self.sub_u8s(self.state.bc.as_u8s.left, 1)
|
||||
},
|
||||
0x06 => self.state.bc.as_u8s.left = self.ld_immediate_byte(),
|
||||
0x07 => unsafe {
|
||||
self.state.af.as_u8s.left = self.rlc(self.state.af.as_u8s.left, Direction::Left)
|
||||
},
|
||||
0x07 => unsafe { self.state.af.as_u8s.left = self.rlc(self.state.af.as_u8s.left) },
|
||||
0x08 => unsafe {
|
||||
let address = self.ld_immediate_word().as_u16;
|
||||
let word = self.state.sp;
|
||||
|
@ -68,9 +66,7 @@ impl CPU {
|
|||
self.state.bc.as_u8s.right = self.sub_u8s(self.state.bc.as_u8s.right, 0x1)
|
||||
},
|
||||
0x0E => self.state.bc.as_u8s.right = self.ld_immediate_byte(),
|
||||
0x0F => unsafe {
|
||||
self.state.af.as_u8s.left = self.rlc(self.state.af.as_u8s.left, Direction::Right)
|
||||
},
|
||||
0x0F => unsafe { self.state.af.as_u8s.left = self.rrc(self.state.af.as_u8s.left) },
|
||||
0x10 => panic!("STOP instruction"),
|
||||
0x11 => self.state.de = self.ld_immediate_word(),
|
||||
0x12 => unsafe {
|
||||
|
@ -87,7 +83,7 @@ impl CPU {
|
|||
},
|
||||
0x16 => self.state.de.as_u8s.left = self.ld_immediate_byte(),
|
||||
0x17 => unsafe {
|
||||
self.state.af.as_u8s.left = self.rl(self.state.af.as_u8s.left, Direction::Left);
|
||||
self.state.af.as_u8s.left = self.rl(self.state.af.as_u8s.left);
|
||||
},
|
||||
0x18 => unsafe {
|
||||
let t = (as_signed(self.ld_immediate_byte()) as i16) as u16;
|
||||
|
@ -109,7 +105,7 @@ impl CPU {
|
|||
},
|
||||
0x1E => self.state.de.as_u8s.right = self.ld_immediate_byte(),
|
||||
0x1F => unsafe {
|
||||
self.state.af.as_u8s.left = self.rl(self.state.af.as_u8s.left, Direction::Right);
|
||||
self.state.af.as_u8s.left = self.rr(self.state.af.as_u8s.left);
|
||||
},
|
||||
0x20 => {
|
||||
let jump_size = self.ld_immediate_byte();
|
||||
|
@ -165,7 +161,9 @@ impl CPU {
|
|||
}
|
||||
}
|
||||
}
|
||||
0x29 => unsafe { self.state.hl.as_u16 *= 2 },
|
||||
0x29 => unsafe {
|
||||
self.state.hl.as_u16 = self.add_u16s(self.state.hl.as_u16, self.state.hl.as_u16)
|
||||
},
|
||||
0x2A => unsafe {
|
||||
self.state.af.as_u8s.left = self.memory.get(self.state.hl.as_u16);
|
||||
self.state.hl.as_u16 = self.add_u16s(self.state.hl.as_u16, 1);
|
||||
|
@ -859,13 +857,356 @@ impl CPU {
|
|||
|
||||
fn cb_subop(&mut self, subop: u8) {
|
||||
match subop {
|
||||
0x11 => unsafe {
|
||||
self.state.bc.as_u8s.right = self.rl(self.state.bc.as_u8s.right, Direction::Left)
|
||||
0x00 => unsafe { self.state.bc.as_u8s.left = self.rlc(self.state.bc.as_u8s.left) },
|
||||
0x01 => unsafe { self.state.bc.as_u8s.right = self.rlc(self.state.bc.as_u8s.right) },
|
||||
0x02 => unsafe { self.state.de.as_u8s.left = self.rlc(self.state.de.as_u8s.left) },
|
||||
0x03 => unsafe { self.state.de.as_u8s.right = self.rlc(self.state.de.as_u8s.right) },
|
||||
0x04 => unsafe { self.state.hl.as_u8s.left = self.rlc(self.state.hl.as_u8s.left) },
|
||||
0x05 => unsafe { self.state.hl.as_u8s.right = self.rlc(self.state.hl.as_u8s.right) },
|
||||
0x06 => unsafe {
|
||||
let rotated = self.rlc(self.memory.get(self.state.hl.as_u16));
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0x7C => unsafe {
|
||||
self.set_or_clear_flag(FLAGS::Z, !get_bit(self.state.hl.as_u8s.left, 7))
|
||||
0x07 => unsafe { self.state.af.as_u8s.left = self.rlc(self.state.af.as_u8s.left) },
|
||||
0x08 => unsafe { self.state.bc.as_u8s.left = self.rrc(self.state.bc.as_u8s.left) },
|
||||
0x09 => unsafe { self.state.bc.as_u8s.right = self.rrc(self.state.bc.as_u8s.right) },
|
||||
0x0A => unsafe { self.state.de.as_u8s.left = self.rrc(self.state.de.as_u8s.left) },
|
||||
0x0B => unsafe { self.state.de.as_u8s.right = self.rrc(self.state.de.as_u8s.right) },
|
||||
0x0C => unsafe { self.state.hl.as_u8s.left = self.rrc(self.state.hl.as_u8s.left) },
|
||||
0x0D => unsafe { self.state.hl.as_u8s.right = self.rrc(self.state.hl.as_u8s.right) },
|
||||
0x0E => unsafe {
|
||||
let rotated = self.rrc(self.memory.get(self.state.hl.as_u16));
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
_ => panic!("Unimplemented sub-opcode: {:#X}", subop),
|
||||
0x0F => unsafe { self.state.af.as_u8s.left = self.rrc(self.state.af.as_u8s.left) },
|
||||
0x10 => unsafe { self.state.bc.as_u8s.left = self.rl(self.state.bc.as_u8s.left) },
|
||||
0x11 => unsafe { self.state.bc.as_u8s.right = self.rl(self.state.bc.as_u8s.right) },
|
||||
0x12 => unsafe { self.state.de.as_u8s.left = self.rl(self.state.de.as_u8s.left) },
|
||||
0x13 => unsafe { self.state.de.as_u8s.right = self.rl(self.state.de.as_u8s.right) },
|
||||
0x14 => unsafe { self.state.hl.as_u8s.left = self.rl(self.state.hl.as_u8s.left) },
|
||||
0x15 => unsafe { self.state.hl.as_u8s.right = self.rl(self.state.hl.as_u8s.right) },
|
||||
0x16 => unsafe {
|
||||
let rotated = self.rl(self.memory.get(self.state.hl.as_u16));
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0x17 => unsafe { self.state.af.as_u8s.left = self.rl(self.state.af.as_u8s.left) },
|
||||
0x18 => unsafe { self.state.bc.as_u8s.left = self.rr(self.state.bc.as_u8s.left) },
|
||||
0x19 => unsafe { self.state.bc.as_u8s.right = self.rr(self.state.bc.as_u8s.right) },
|
||||
0x1A => unsafe { self.state.de.as_u8s.left = self.rr(self.state.de.as_u8s.left) },
|
||||
0x1B => unsafe { self.state.de.as_u8s.right = self.rr(self.state.de.as_u8s.right) },
|
||||
0x1C => unsafe { self.state.hl.as_u8s.left = self.rr(self.state.hl.as_u8s.left) },
|
||||
0x1D => unsafe { self.state.hl.as_u8s.right = self.rr(self.state.hl.as_u8s.right) },
|
||||
0x1E => unsafe {
|
||||
let rotated = self.rr(self.memory.get(self.state.hl.as_u16));
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0x1F => unsafe { self.state.af.as_u8s.left = self.rr(self.state.af.as_u8s.left) },
|
||||
0x20 => unsafe { self.state.bc.as_u8s.left = self.sla(self.state.bc.as_u8s.left) },
|
||||
0x21 => unsafe { self.state.bc.as_u8s.right = self.sla(self.state.bc.as_u8s.right) },
|
||||
0x22 => unsafe { self.state.de.as_u8s.left = self.sla(self.state.de.as_u8s.left) },
|
||||
0x23 => unsafe { self.state.de.as_u8s.right = self.sla(self.state.de.as_u8s.right) },
|
||||
0x24 => unsafe { self.state.hl.as_u8s.left = self.sla(self.state.hl.as_u8s.left) },
|
||||
0x25 => unsafe { self.state.hl.as_u8s.right = self.sla(self.state.hl.as_u8s.right) },
|
||||
0x26 => unsafe {
|
||||
let rotated = self.sla(self.memory.get(self.state.hl.as_u16));
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0x27 => unsafe { self.state.af.as_u8s.left = self.sla(self.state.af.as_u8s.left) },
|
||||
0x28 => unsafe { self.state.bc.as_u8s.left = self.sra(self.state.bc.as_u8s.left) },
|
||||
0x29 => unsafe { self.state.bc.as_u8s.right = self.sra(self.state.bc.as_u8s.right) },
|
||||
0x2A => unsafe { self.state.de.as_u8s.left = self.sra(self.state.de.as_u8s.left) },
|
||||
0x2B => unsafe { self.state.de.as_u8s.right = self.sra(self.state.de.as_u8s.right) },
|
||||
0x2C => unsafe { self.state.hl.as_u8s.left = self.sra(self.state.hl.as_u8s.left) },
|
||||
0x2D => unsafe { self.state.hl.as_u8s.right = self.sra(self.state.hl.as_u8s.right) },
|
||||
0x2E => unsafe {
|
||||
let rotated = self.sra(self.memory.get(self.state.hl.as_u16));
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0x2F => unsafe { self.state.af.as_u8s.left = self.sra(self.state.af.as_u8s.left) },
|
||||
0x30 => unsafe { self.state.bc.as_u8s.left = swap_nibbles(self.state.bc.as_u8s.left) },
|
||||
0x31 => unsafe {
|
||||
self.state.bc.as_u8s.right = swap_nibbles(self.state.bc.as_u8s.right)
|
||||
},
|
||||
0x32 => unsafe { self.state.de.as_u8s.left = swap_nibbles(self.state.de.as_u8s.left) },
|
||||
0x33 => unsafe {
|
||||
self.state.de.as_u8s.right = swap_nibbles(self.state.de.as_u8s.right)
|
||||
},
|
||||
0x34 => unsafe { self.state.hl.as_u8s.left = swap_nibbles(self.state.hl.as_u8s.left) },
|
||||
0x35 => unsafe {
|
||||
self.state.hl.as_u8s.right = swap_nibbles(self.state.hl.as_u8s.right)
|
||||
},
|
||||
0x36 => unsafe {
|
||||
let rotated = swap_nibbles(self.memory.get(self.state.hl.as_u16));
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0x37 => unsafe { self.state.af.as_u8s.left = swap_nibbles(self.state.af.as_u8s.left) },
|
||||
0x38 => unsafe { self.state.bc.as_u8s.left = self.srl(self.state.bc.as_u8s.left) },
|
||||
0x39 => unsafe { self.state.bc.as_u8s.right = self.srl(self.state.bc.as_u8s.right) },
|
||||
0x3A => unsafe { self.state.de.as_u8s.left = self.srl(self.state.de.as_u8s.left) },
|
||||
0x3B => unsafe { self.state.de.as_u8s.right = self.srl(self.state.de.as_u8s.right) },
|
||||
0x3C => unsafe { self.state.hl.as_u8s.left = self.srl(self.state.hl.as_u8s.left) },
|
||||
0x3D => unsafe { self.state.hl.as_u8s.right = self.srl(self.state.hl.as_u8s.right) },
|
||||
0x3E => unsafe {
|
||||
let rotated = self.srl(self.memory.get(self.state.hl.as_u16));
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0x3F => unsafe { self.state.af.as_u8s.left = self.srl(self.state.af.as_u8s.left) },
|
||||
0x40 => unsafe { self.bit(self.state.bc.as_u8s.left, 0) },
|
||||
0x41 => unsafe { self.bit(self.state.bc.as_u8s.right, 0) },
|
||||
0x42 => unsafe { self.bit(self.state.de.as_u8s.left, 0) },
|
||||
0x43 => unsafe { self.bit(self.state.de.as_u8s.right, 0) },
|
||||
0x44 => unsafe { self.bit(self.state.hl.as_u8s.left, 0) },
|
||||
0x45 => unsafe { self.bit(self.state.hl.as_u8s.right, 0) },
|
||||
0x46 => unsafe {
|
||||
self.bit(self.memory.get(self.state.hl.as_u16), 0);
|
||||
},
|
||||
0x47 => unsafe { self.bit(self.state.af.as_u8s.left, 1) },
|
||||
0x48 => unsafe { self.bit(self.state.bc.as_u8s.left, 1) },
|
||||
0x49 => unsafe { self.bit(self.state.bc.as_u8s.right, 1) },
|
||||
0x4A => unsafe { self.bit(self.state.de.as_u8s.left, 1) },
|
||||
0x4B => unsafe { self.bit(self.state.de.as_u8s.right, 1) },
|
||||
0x4C => unsafe { self.bit(self.state.hl.as_u8s.left, 1) },
|
||||
0x4D => unsafe { self.bit(self.state.hl.as_u8s.right, 1) },
|
||||
0x4E => unsafe {
|
||||
self.bit(self.memory.get(self.state.hl.as_u16), 1);
|
||||
},
|
||||
0x4F => unsafe { self.bit(self.state.af.as_u8s.left, 1) },
|
||||
0x50 => unsafe { self.bit(self.state.bc.as_u8s.left, 2) },
|
||||
0x51 => unsafe { self.bit(self.state.bc.as_u8s.right, 2) },
|
||||
0x52 => unsafe { self.bit(self.state.de.as_u8s.left, 2) },
|
||||
0x53 => unsafe { self.bit(self.state.de.as_u8s.right, 2) },
|
||||
0x54 => unsafe { self.bit(self.state.hl.as_u8s.left, 2) },
|
||||
0x55 => unsafe { self.bit(self.state.hl.as_u8s.right, 2) },
|
||||
0x56 => unsafe {
|
||||
self.bit(self.memory.get(self.state.hl.as_u16), 2);
|
||||
},
|
||||
0x57 => unsafe { self.bit(self.state.af.as_u8s.left, 3) },
|
||||
0x58 => unsafe { self.bit(self.state.bc.as_u8s.left, 3) },
|
||||
0x59 => unsafe { self.bit(self.state.bc.as_u8s.right, 3) },
|
||||
0x5A => unsafe { self.bit(self.state.de.as_u8s.left, 3) },
|
||||
0x5B => unsafe { self.bit(self.state.de.as_u8s.right, 3) },
|
||||
0x5C => unsafe { self.bit(self.state.hl.as_u8s.left, 3) },
|
||||
0x5D => unsafe { self.bit(self.state.hl.as_u8s.right, 3) },
|
||||
0x5E => unsafe {
|
||||
self.bit(self.memory.get(self.state.hl.as_u16), 3);
|
||||
},
|
||||
0x5F => unsafe { self.bit(self.state.af.as_u8s.left, 3) },
|
||||
0x60 => unsafe { self.bit(self.state.bc.as_u8s.left, 4) },
|
||||
0x61 => unsafe { self.bit(self.state.bc.as_u8s.right, 4) },
|
||||
0x62 => unsafe { self.bit(self.state.de.as_u8s.left, 4) },
|
||||
0x63 => unsafe { self.bit(self.state.de.as_u8s.right, 4) },
|
||||
0x64 => unsafe { self.bit(self.state.hl.as_u8s.left, 4) },
|
||||
0x65 => unsafe { self.bit(self.state.hl.as_u8s.right, 4) },
|
||||
0x66 => unsafe {
|
||||
self.bit(self.memory.get(self.state.hl.as_u16), 4);
|
||||
},
|
||||
0x67 => unsafe { self.bit(self.state.af.as_u8s.left, 5) },
|
||||
0x68 => unsafe { self.bit(self.state.bc.as_u8s.left, 5) },
|
||||
0x69 => unsafe { self.bit(self.state.bc.as_u8s.right, 5) },
|
||||
0x6A => unsafe { self.bit(self.state.de.as_u8s.left, 5) },
|
||||
0x6B => unsafe { self.bit(self.state.de.as_u8s.right, 5) },
|
||||
0x6C => unsafe { self.bit(self.state.hl.as_u8s.left, 5) },
|
||||
0x6D => unsafe { self.bit(self.state.hl.as_u8s.right, 5) },
|
||||
0x6E => unsafe {
|
||||
self.bit(self.memory.get(self.state.hl.as_u16), 5);
|
||||
},
|
||||
0x6F => unsafe { self.bit(self.state.af.as_u8s.left, 5) },
|
||||
0x70 => unsafe { self.bit(self.state.bc.as_u8s.left, 6) },
|
||||
0x71 => unsafe { self.bit(self.state.bc.as_u8s.right, 6) },
|
||||
0x72 => unsafe { self.bit(self.state.de.as_u8s.left, 6) },
|
||||
0x73 => unsafe { self.bit(self.state.de.as_u8s.right, 6) },
|
||||
0x74 => unsafe { self.bit(self.state.hl.as_u8s.left, 6) },
|
||||
0x75 => unsafe { self.bit(self.state.hl.as_u8s.right, 6) },
|
||||
0x76 => unsafe {
|
||||
self.bit(self.memory.get(self.state.hl.as_u16), 6);
|
||||
},
|
||||
0x77 => unsafe { self.bit(self.state.af.as_u8s.left, 7) },
|
||||
0x78 => unsafe { self.bit(self.state.bc.as_u8s.left, 7) },
|
||||
0x79 => unsafe { self.bit(self.state.bc.as_u8s.right, 7) },
|
||||
0x7A => unsafe { self.bit(self.state.de.as_u8s.left, 7) },
|
||||
0x7B => unsafe { self.bit(self.state.de.as_u8s.right, 7) },
|
||||
0x7C => unsafe { self.bit(self.state.hl.as_u8s.left, 7) },
|
||||
0x7D => unsafe { self.bit(self.state.hl.as_u8s.right, 7) },
|
||||
0x7E => unsafe {
|
||||
self.bit(self.memory.get(self.state.hl.as_u16), 7);
|
||||
},
|
||||
0x7F => unsafe { self.bit(self.state.af.as_u8s.left, 7) },
|
||||
0x80 => unsafe { self.state.bc.as_u8s.left = res(self.state.bc.as_u8s.left, 0) },
|
||||
0x81 => unsafe { self.state.bc.as_u8s.right = res(self.state.bc.as_u8s.right, 0) },
|
||||
0x82 => unsafe { self.state.de.as_u8s.left = res(self.state.de.as_u8s.left, 0) },
|
||||
0x83 => unsafe { self.state.de.as_u8s.right = res(self.state.de.as_u8s.right, 0) },
|
||||
0x84 => unsafe { self.state.hl.as_u8s.left = res(self.state.hl.as_u8s.left, 0) },
|
||||
0x85 => unsafe { self.state.hl.as_u8s.right = res(self.state.hl.as_u8s.right, 0) },
|
||||
0x86 => unsafe {
|
||||
let rotated = res(self.memory.get(self.state.hl.as_u16), 0);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0x87 => unsafe { self.state.af.as_u8s.left = res(self.state.af.as_u8s.left, 0) },
|
||||
0x88 => unsafe { self.state.bc.as_u8s.left = res(self.state.bc.as_u8s.left, 1) },
|
||||
0x89 => unsafe { self.state.bc.as_u8s.right = res(self.state.bc.as_u8s.right, 1) },
|
||||
0x8A => unsafe { self.state.de.as_u8s.left = res(self.state.de.as_u8s.left, 1) },
|
||||
0x8B => unsafe { self.state.de.as_u8s.right = res(self.state.de.as_u8s.right, 1) },
|
||||
0x8C => unsafe { self.state.hl.as_u8s.left = res(self.state.hl.as_u8s.left, 1) },
|
||||
0x8D => unsafe { self.state.hl.as_u8s.right = res(self.state.hl.as_u8s.right, 1) },
|
||||
0x8E => unsafe {
|
||||
let rotated = res(self.memory.get(self.state.hl.as_u16), 1);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0x8F => unsafe { self.state.af.as_u8s.left = res(self.state.af.as_u8s.left, 1) },
|
||||
0x90 => unsafe { self.state.bc.as_u8s.left = res(self.state.bc.as_u8s.left, 2) },
|
||||
0x91 => unsafe { self.state.bc.as_u8s.right = res(self.state.bc.as_u8s.right, 2) },
|
||||
0x92 => unsafe { self.state.de.as_u8s.left = res(self.state.de.as_u8s.left, 2) },
|
||||
0x93 => unsafe { self.state.de.as_u8s.right = res(self.state.de.as_u8s.right, 2) },
|
||||
0x94 => unsafe { self.state.hl.as_u8s.left = res(self.state.hl.as_u8s.left, 2) },
|
||||
0x95 => unsafe { self.state.hl.as_u8s.right = res(self.state.hl.as_u8s.right, 2) },
|
||||
0x96 => unsafe {
|
||||
let rotated = res(self.memory.get(self.state.hl.as_u16), 2);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0x97 => unsafe { self.state.af.as_u8s.left = res(self.state.af.as_u8s.left, 2) },
|
||||
0x98 => unsafe { self.state.bc.as_u8s.left = res(self.state.bc.as_u8s.left, 3) },
|
||||
0x99 => unsafe { self.state.bc.as_u8s.right = res(self.state.bc.as_u8s.right, 3) },
|
||||
0x9A => unsafe { self.state.de.as_u8s.left = res(self.state.de.as_u8s.left, 3) },
|
||||
0x9B => unsafe { self.state.de.as_u8s.right = res(self.state.de.as_u8s.right, 3) },
|
||||
0x9C => unsafe { self.state.hl.as_u8s.left = res(self.state.hl.as_u8s.left, 3) },
|
||||
0x9D => unsafe { self.state.hl.as_u8s.right = res(self.state.hl.as_u8s.right, 3) },
|
||||
0x9E => unsafe {
|
||||
let rotated = res(self.memory.get(self.state.hl.as_u16), 3);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0x9F => unsafe { self.state.af.as_u8s.left = res(self.state.af.as_u8s.left, 3) },
|
||||
0xA0 => unsafe { self.state.bc.as_u8s.left = res(self.state.bc.as_u8s.left, 4) },
|
||||
0xA1 => unsafe { self.state.bc.as_u8s.right = res(self.state.bc.as_u8s.right, 4) },
|
||||
0xA2 => unsafe { self.state.de.as_u8s.left = res(self.state.de.as_u8s.left, 4) },
|
||||
0xA3 => unsafe { self.state.de.as_u8s.right = res(self.state.de.as_u8s.right, 4) },
|
||||
0xA4 => unsafe { self.state.hl.as_u8s.left = res(self.state.hl.as_u8s.left, 4) },
|
||||
0xA5 => unsafe { self.state.hl.as_u8s.right = res(self.state.hl.as_u8s.right, 4) },
|
||||
0xA6 => unsafe {
|
||||
let rotated = res(self.memory.get(self.state.hl.as_u16), 4);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0xA7 => unsafe { self.state.af.as_u8s.left = res(self.state.af.as_u8s.left, 4) },
|
||||
0xA8 => unsafe { self.state.bc.as_u8s.left = res(self.state.bc.as_u8s.left, 5) },
|
||||
0xA9 => unsafe { self.state.bc.as_u8s.right = res(self.state.bc.as_u8s.right, 5) },
|
||||
0xAA => unsafe { self.state.de.as_u8s.left = res(self.state.de.as_u8s.left, 5) },
|
||||
0xAB => unsafe { self.state.de.as_u8s.right = res(self.state.de.as_u8s.right, 5) },
|
||||
0xAC => unsafe { self.state.hl.as_u8s.left = res(self.state.hl.as_u8s.left, 5) },
|
||||
0xAD => unsafe { self.state.hl.as_u8s.right = res(self.state.hl.as_u8s.right, 5) },
|
||||
0xAE => unsafe {
|
||||
let rotated = res(self.memory.get(self.state.hl.as_u16), 5);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0xAF => unsafe { self.state.af.as_u8s.left = res(self.state.af.as_u8s.left, 5) },
|
||||
0xB0 => unsafe { self.state.bc.as_u8s.left = res(self.state.bc.as_u8s.left, 6) },
|
||||
0xB1 => unsafe { self.state.bc.as_u8s.right = res(self.state.bc.as_u8s.right, 6) },
|
||||
0xB2 => unsafe { self.state.de.as_u8s.left = res(self.state.de.as_u8s.left, 6) },
|
||||
0xB3 => unsafe { self.state.de.as_u8s.right = res(self.state.de.as_u8s.right, 6) },
|
||||
0xB4 => unsafe { self.state.hl.as_u8s.left = res(self.state.hl.as_u8s.left, 6) },
|
||||
0xB5 => unsafe { self.state.hl.as_u8s.right = res(self.state.hl.as_u8s.right, 6) },
|
||||
0xB6 => unsafe {
|
||||
let rotated = res(self.memory.get(self.state.hl.as_u16), 6);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0xB7 => unsafe { self.state.af.as_u8s.left = res(self.state.af.as_u8s.left, 6) },
|
||||
0xB8 => unsafe { self.state.bc.as_u8s.left = res(self.state.bc.as_u8s.left, 7) },
|
||||
0xB9 => unsafe { self.state.bc.as_u8s.right = res(self.state.bc.as_u8s.right, 7) },
|
||||
0xBA => unsafe { self.state.de.as_u8s.left = res(self.state.de.as_u8s.left, 7) },
|
||||
0xBB => unsafe { self.state.de.as_u8s.right = res(self.state.de.as_u8s.right, 7) },
|
||||
0xBC => unsafe { self.state.hl.as_u8s.left = res(self.state.hl.as_u8s.left, 7) },
|
||||
0xBD => unsafe { self.state.hl.as_u8s.right = res(self.state.hl.as_u8s.right, 7) },
|
||||
0xBE => unsafe {
|
||||
let rotated = res(self.memory.get(self.state.hl.as_u16), 7);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0xBF => unsafe { self.state.af.as_u8s.left = res(self.state.af.as_u8s.left, 7) },
|
||||
0xC0 => unsafe { self.state.bc.as_u8s.left = set(self.state.bc.as_u8s.left, 0) },
|
||||
0xC1 => unsafe { self.state.bc.as_u8s.right = set(self.state.bc.as_u8s.right, 0) },
|
||||
0xC2 => unsafe { self.state.de.as_u8s.left = set(self.state.de.as_u8s.left, 0) },
|
||||
0xC3 => unsafe { self.state.de.as_u8s.right = set(self.state.de.as_u8s.right, 0) },
|
||||
0xC4 => unsafe { self.state.hl.as_u8s.left = set(self.state.hl.as_u8s.left, 0) },
|
||||
0xC5 => unsafe { self.state.hl.as_u8s.right = set(self.state.hl.as_u8s.right, 0) },
|
||||
0xC6 => unsafe {
|
||||
let rotated = set(self.memory.get(self.state.hl.as_u16), 0);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0xC7 => unsafe { self.state.af.as_u8s.left = set(self.state.af.as_u8s.left, 0) },
|
||||
0xC8 => unsafe { self.state.bc.as_u8s.left = set(self.state.bc.as_u8s.left, 1) },
|
||||
0xC9 => unsafe { self.state.bc.as_u8s.right = set(self.state.bc.as_u8s.right, 1) },
|
||||
0xCA => unsafe { self.state.de.as_u8s.left = set(self.state.de.as_u8s.left, 1) },
|
||||
0xCB => unsafe { self.state.de.as_u8s.right = set(self.state.de.as_u8s.right, 1) },
|
||||
0xCC => unsafe { self.state.hl.as_u8s.left = set(self.state.hl.as_u8s.left, 1) },
|
||||
0xCD => unsafe { self.state.hl.as_u8s.right = set(self.state.hl.as_u8s.right, 1) },
|
||||
0xCE => unsafe {
|
||||
let rotated = set(self.memory.get(self.state.hl.as_u16), 1);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0xCF => unsafe { self.state.af.as_u8s.left = set(self.state.af.as_u8s.left, 1) },
|
||||
0xD0 => unsafe { self.state.bc.as_u8s.left = set(self.state.bc.as_u8s.left, 2) },
|
||||
0xD1 => unsafe { self.state.bc.as_u8s.right = set(self.state.bc.as_u8s.right, 2) },
|
||||
0xD2 => unsafe { self.state.de.as_u8s.left = set(self.state.de.as_u8s.left, 2) },
|
||||
0xD3 => unsafe { self.state.de.as_u8s.right = set(self.state.de.as_u8s.right, 2) },
|
||||
0xD4 => unsafe { self.state.hl.as_u8s.left = set(self.state.hl.as_u8s.left, 2) },
|
||||
0xD5 => unsafe { self.state.hl.as_u8s.right = set(self.state.hl.as_u8s.right, 2) },
|
||||
0xD6 => unsafe {
|
||||
let rotated = set(self.memory.get(self.state.hl.as_u16), 2);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0xD7 => unsafe { self.state.af.as_u8s.left = set(self.state.af.as_u8s.left, 2) },
|
||||
0xD8 => unsafe { self.state.bc.as_u8s.left = set(self.state.bc.as_u8s.left, 3) },
|
||||
0xD9 => unsafe { self.state.bc.as_u8s.right = set(self.state.bc.as_u8s.right, 3) },
|
||||
0xDA => unsafe { self.state.de.as_u8s.left = set(self.state.de.as_u8s.left, 3) },
|
||||
0xDB => unsafe { self.state.de.as_u8s.right = set(self.state.de.as_u8s.right, 3) },
|
||||
0xDC => unsafe { self.state.hl.as_u8s.left = set(self.state.hl.as_u8s.left, 3) },
|
||||
0xDD => unsafe { self.state.hl.as_u8s.right = set(self.state.hl.as_u8s.right, 3) },
|
||||
0xDE => unsafe {
|
||||
let rotated = set(self.memory.get(self.state.hl.as_u16), 3);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0xDF => unsafe { self.state.af.as_u8s.left = set(self.state.af.as_u8s.left, 3) },
|
||||
0xE0 => unsafe { self.state.bc.as_u8s.left = set(self.state.bc.as_u8s.left, 4) },
|
||||
0xE1 => unsafe { self.state.bc.as_u8s.right = set(self.state.bc.as_u8s.right, 4) },
|
||||
0xE2 => unsafe { self.state.de.as_u8s.left = set(self.state.de.as_u8s.left, 4) },
|
||||
0xE3 => unsafe { self.state.de.as_u8s.right = set(self.state.de.as_u8s.right, 4) },
|
||||
0xE4 => unsafe { self.state.hl.as_u8s.left = set(self.state.hl.as_u8s.left, 4) },
|
||||
0xE5 => unsafe { self.state.hl.as_u8s.right = set(self.state.hl.as_u8s.right, 4) },
|
||||
0xE6 => unsafe {
|
||||
let rotated = set(self.memory.get(self.state.hl.as_u16), 4);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0xE7 => unsafe { self.state.af.as_u8s.left = set(self.state.af.as_u8s.left, 4) },
|
||||
0xE8 => unsafe { self.state.bc.as_u8s.left = set(self.state.bc.as_u8s.left, 5) },
|
||||
0xE9 => unsafe { self.state.bc.as_u8s.right = set(self.state.bc.as_u8s.right, 5) },
|
||||
0xEA => unsafe { self.state.de.as_u8s.left = set(self.state.de.as_u8s.left, 5) },
|
||||
0xEB => unsafe { self.state.de.as_u8s.right = set(self.state.de.as_u8s.right, 5) },
|
||||
0xEC => unsafe { self.state.hl.as_u8s.left = set(self.state.hl.as_u8s.left, 5) },
|
||||
0xED => unsafe { self.state.hl.as_u8s.right = set(self.state.hl.as_u8s.right, 5) },
|
||||
0xEE => unsafe {
|
||||
let rotated = set(self.memory.get(self.state.hl.as_u16), 5);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0xEF => unsafe { self.state.af.as_u8s.left = set(self.state.af.as_u8s.left, 5) },
|
||||
0xF0 => unsafe { self.state.bc.as_u8s.left = set(self.state.bc.as_u8s.left, 6) },
|
||||
0xF1 => unsafe { self.state.bc.as_u8s.right = set(self.state.bc.as_u8s.right, 6) },
|
||||
0xF2 => unsafe { self.state.de.as_u8s.left = set(self.state.de.as_u8s.left, 6) },
|
||||
0xF3 => unsafe { self.state.de.as_u8s.right = set(self.state.de.as_u8s.right, 6) },
|
||||
0xF4 => unsafe { self.state.hl.as_u8s.left = set(self.state.hl.as_u8s.left, 6) },
|
||||
0xF5 => unsafe { self.state.hl.as_u8s.right = set(self.state.hl.as_u8s.right, 6) },
|
||||
0xF6 => unsafe {
|
||||
let rotated = set(self.memory.get(self.state.hl.as_u16), 6);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0xF7 => unsafe { self.state.af.as_u8s.left = set(self.state.af.as_u8s.left, 6) },
|
||||
0xF8 => unsafe { self.state.bc.as_u8s.left = set(self.state.bc.as_u8s.left, 7) },
|
||||
0xF9 => unsafe { self.state.bc.as_u8s.right = set(self.state.bc.as_u8s.right, 7) },
|
||||
0xFA => unsafe { self.state.de.as_u8s.left = set(self.state.de.as_u8s.left, 7) },
|
||||
0xFB => unsafe { self.state.de.as_u8s.right = set(self.state.de.as_u8s.right, 7) },
|
||||
0xFC => unsafe { self.state.hl.as_u8s.left = set(self.state.hl.as_u8s.left, 7) },
|
||||
0xFD => unsafe { self.state.hl.as_u8s.right = set(self.state.hl.as_u8s.right, 7) },
|
||||
0xFE => unsafe {
|
||||
let rotated = set(self.memory.get(self.state.hl.as_u16), 7);
|
||||
self.memory.set(self.state.hl.as_u16, rotated)
|
||||
},
|
||||
0xFF => unsafe { self.state.af.as_u8s.left = set(self.state.af.as_u8s.left, 7) },
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -877,7 +1218,23 @@ impl CPU {
|
|||
};
|
||||
}
|
||||
|
||||
fn rlc(&mut self, byte: u8, direction: Direction) -> u8 {
|
||||
fn rlc(&mut self, byte: u8) -> u8 {
|
||||
self.rotate_c(byte, Direction::Left)
|
||||
}
|
||||
|
||||
fn rrc(&mut self, byte: u8) -> u8 {
|
||||
self.rotate_c(byte, Direction::Right)
|
||||
}
|
||||
|
||||
fn rl(&mut self, byte: u8) -> u8 {
|
||||
self.rotate(byte, Direction::Left)
|
||||
}
|
||||
|
||||
fn rr(&mut self, byte: u8) -> u8 {
|
||||
self.rotate(byte, Direction::Right)
|
||||
}
|
||||
|
||||
fn rotate_c(&mut self, byte: u8, direction: Direction) -> u8 {
|
||||
let (mut rotated, carry) = rotate(byte, &direction);
|
||||
if carry {
|
||||
rotated += get_rotation_carry(&direction);
|
||||
|
@ -886,7 +1243,7 @@ impl CPU {
|
|||
return rotated;
|
||||
}
|
||||
|
||||
fn rl(&mut self, byte: u8, direction: Direction) -> u8 {
|
||||
fn rotate(&mut self, byte: u8, direction: Direction) -> u8 {
|
||||
let old_carry = self.get_flag(FLAGS::C);
|
||||
let (mut rotated, carry) = rotate(byte, &direction);
|
||||
if old_carry > 0 {
|
||||
|
@ -898,6 +1255,36 @@ impl CPU {
|
|||
return rotated;
|
||||
}
|
||||
|
||||
fn sla(&mut self, byte: u8) -> u8 {
|
||||
self.shift(byte, Direction::Left)
|
||||
}
|
||||
|
||||
fn sra(&mut self, byte: u8) -> u8 {
|
||||
let b = get_bit(byte, 7);
|
||||
let val = self.shift(byte, Direction::Right);
|
||||
if b {
|
||||
val + 0b10000000
|
||||
} else {
|
||||
val
|
||||
}
|
||||
}
|
||||
|
||||
fn srl(&mut self, byte: u8) -> u8 {
|
||||
self.shift(byte, Direction::Right)
|
||||
}
|
||||
|
||||
fn shift(&mut self, byte: u8, direction: Direction) -> u8 {
|
||||
let (rotated, carry) = rotate(byte, &direction);
|
||||
if carry {
|
||||
self.set_flag(FLAGS::C);
|
||||
}
|
||||
return rotated;
|
||||
}
|
||||
|
||||
fn bit(&mut self, byte: u8, bit: u8) {
|
||||
self.set_or_clear_flag(FLAGS::Z, !get_bit(byte, bit));
|
||||
}
|
||||
|
||||
fn rst(&mut self, address: u16) {
|
||||
self.push(self.state.pc);
|
||||
self.state.pc.as_u8s.left = 0x0;
|
||||
|
@ -907,9 +1294,11 @@ impl CPU {
|
|||
fn push(&mut self, register: Register) {
|
||||
unsafe {
|
||||
let address = self.state.sp.as_u16;
|
||||
self.memory.set(address - 1, register.as_u8s.right);
|
||||
self.memory.set(address - 2, register.as_u8s.left);
|
||||
self.state.sp.as_u16 = address - 2;
|
||||
self.memory
|
||||
.set(address.wrapping_sub(1), register.as_u8s.right);
|
||||
self.memory
|
||||
.set(address.wrapping_sub(2), register.as_u8s.left);
|
||||
self.state.sp.as_u16 = address.wrapping_sub(2);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1062,3 +1451,15 @@ fn get_rotation_carry(direction: &Direction) -> u8 {
|
|||
Direction::Right => 0b10000000,
|
||||
}
|
||||
}
|
||||
|
||||
fn swap_nibbles(byte: u8) -> u8 {
|
||||
(byte & 0x0F) << 4 | (byte & 0xF0) >> 4
|
||||
}
|
||||
|
||||
fn res(byte: u8, bit: u8) -> u8 {
|
||||
byte & !(1 << bit)
|
||||
}
|
||||
|
||||
fn set(byte: u8, bit: u8) -> u8 {
|
||||
byte | (1 << bit)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue