Skip to content

Commit

Permalink
implement LD 16
Browse files Browse the repository at this point in the history
  • Loading branch information
pcasaretto committed Apr 7, 2024
1 parent 44e39d5 commit ed46459
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 59 deletions.
20 changes: 10 additions & 10 deletions src/instructions/adc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::super::*;

pub fn adc(target: ArithmeticTarget) -> impl Fn(&mut CPU) {
pub fn adc(target: RegisterTarget) -> impl Fn(&mut CPU) {
move |cpu: &mut CPU| {
let mut target_value = cpu.read_single_register(target);
let current_value = cpu.registers.a;
Expand Down Expand Up @@ -34,7 +34,7 @@ mod tests {
},
..Default::default()
};
adc(ArithmeticTarget::C)(&mut cpu);
adc(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.registers.a, 1);
}

Expand All @@ -52,7 +52,7 @@ mod tests {
},
..Default::default()
};
adc(ArithmeticTarget::C)(&mut cpu);
adc(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.registers.a, 2);
}

Expand All @@ -67,7 +67,7 @@ mod tests {
},
..Default::default()
};
adc(ArithmeticTarget::C)(&mut cpu);
adc(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.registers.a, 0);
}

Expand All @@ -82,7 +82,7 @@ mod tests {
},
..Default::default()
};
adc(ArithmeticTarget::C)(&mut cpu);
adc(RegisterTarget::C)(&mut cpu);
assert!(cpu.registers.f.carry);
}

Expand All @@ -97,7 +97,7 @@ mod tests {
},
..Default::default()
};
adc(ArithmeticTarget::C)(&mut cpu);
adc(RegisterTarget::C)(&mut cpu);
assert!(cpu.registers.f.zero);
}

Expand All @@ -115,7 +115,7 @@ mod tests {
},
..Default::default()
};
adc(ArithmeticTarget::C)(&mut cpu);
adc(RegisterTarget::C)(&mut cpu);
assert!(!cpu.registers.f.subtract);
}

Expand All @@ -130,7 +130,7 @@ mod tests {
},
..Default::default()
};
adc(ArithmeticTarget::C)(&mut cpu);
adc(RegisterTarget::C)(&mut cpu);
assert!(cpu.registers.f.half_carry);
}

Expand All @@ -140,7 +140,7 @@ mod tests {
pc: 123,
..Default::default()
};
adc(ArithmeticTarget::C)(&mut cpu);
adc(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.pc, 124);
}

Expand All @@ -150,7 +150,7 @@ mod tests {
pc: 0xFFFF,
..Default::default()
};
adc(ArithmeticTarget::C)(&mut cpu);
adc(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.pc, 0);
}
}
18 changes: 9 additions & 9 deletions src/instructions/add.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::super::*;

pub fn add(target: ArithmeticTarget) -> impl Fn(&mut CPU) {
pub fn add(target: RegisterTarget) -> impl Fn(&mut CPU) {
move |cpu: &mut CPU| {
let target_value = cpu.read_single_register(target);
let current_value = cpu.registers.a;
Expand Down Expand Up @@ -31,7 +31,7 @@ mod tests {
},
..Default::default()
};
add(ArithmeticTarget::C)(&mut cpu);
add(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.registers.a, 1);
}

Expand All @@ -46,7 +46,7 @@ mod tests {
},
..Default::default()
};
add(ArithmeticTarget::C)(&mut cpu);
add(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.registers.a, 0);
}

Expand All @@ -61,7 +61,7 @@ mod tests {
},
..Default::default()
};
add(ArithmeticTarget::C)(&mut cpu);
add(RegisterTarget::C)(&mut cpu);
assert!(cpu.registers.f.carry);
}

Expand All @@ -76,7 +76,7 @@ mod tests {
},
..Default::default()
};
add(ArithmeticTarget::C)(&mut cpu);
add(RegisterTarget::C)(&mut cpu);
assert!(cpu.registers.f.zero);
}

Expand All @@ -94,7 +94,7 @@ mod tests {
},
..Default::default()
};
add(ArithmeticTarget::C)(&mut cpu);
add(RegisterTarget::C)(&mut cpu);
assert!(!cpu.registers.f.subtract);
}

Expand All @@ -109,7 +109,7 @@ mod tests {
},
..Default::default()
};
add(ArithmeticTarget::C)(&mut cpu);
add(RegisterTarget::C)(&mut cpu);
assert!(cpu.registers.f.half_carry);
}

Expand All @@ -119,7 +119,7 @@ mod tests {
pc: 123,
..Default::default()
};
add(ArithmeticTarget::C)(&mut cpu);
add(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.pc, 124);
}

Expand All @@ -129,7 +129,7 @@ mod tests {
pc: 0xFFFF,
..Default::default()
};
add(ArithmeticTarget::C)(&mut cpu);
add(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.pc, 0);
}
}
25 changes: 25 additions & 0 deletions src/instructions/ld.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::{Register16bTarget, CPU};

pub fn ld_d16_u16(reg: Register16bTarget) -> impl Fn(&mut CPU) {
move |cpu: &mut CPU| {
let low = cpu.bus.memory[(cpu.pc + 1) as usize];
let high = cpu.bus.memory[(cpu.pc + 2) as usize];
cpu.registers.set_u16(reg, u16::from_le_bytes([low, high]));
cpu.pc += 3;
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_ld_d16_u16() {
let mut cpu = CPU::default();
cpu.bus.memory[1] = 0x01;
cpu.bus.memory[2] = 0x02;
ld_d16_u16(Register16bTarget::BC)(&mut cpu);
assert_eq!(cpu.registers.get_u16(Register16bTarget::BC), 0x0201);
assert_eq!(cpu.pc, 3);
}
}
44 changes: 23 additions & 21 deletions src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod adc;
mod add;
mod inc;
mod jmp;
mod ld;
mod nop;
mod sub;

Expand All @@ -10,28 +11,29 @@ use super::*;
pub fn from_byte(byte: u8) -> Box<dyn Fn(&mut CPU)> {
match byte {
0x00 => Box::new(nop::nop()),
0x21 => Box::new(ld::ld_d16_u16(Register16bTarget::HL)),
0x33 => Box::new(inc::inc_sp()),
0x87 => Box::new(add::add(ArithmeticTarget::A)),
0x80 => Box::new(add::add(ArithmeticTarget::B)),
0x81 => Box::new(add::add(ArithmeticTarget::C)),
0x82 => Box::new(add::add(ArithmeticTarget::D)),
0x83 => Box::new(add::add(ArithmeticTarget::E)),
0x84 => Box::new(add::add(ArithmeticTarget::H)),
0x85 => Box::new(add::add(ArithmeticTarget::L)),
0x8F => Box::new(adc::adc(ArithmeticTarget::A)),
0x88 => Box::new(adc::adc(ArithmeticTarget::B)),
0x89 => Box::new(adc::adc(ArithmeticTarget::C)),
0x8A => Box::new(adc::adc(ArithmeticTarget::D)),
0x8B => Box::new(adc::adc(ArithmeticTarget::E)),
0x8C => Box::new(adc::adc(ArithmeticTarget::H)),
0x8D => Box::new(adc::adc(ArithmeticTarget::L)),
0x97 => Box::new(sub::sub(ArithmeticTarget::A)),
0x90 => Box::new(sub::sub(ArithmeticTarget::B)),
0x91 => Box::new(sub::sub(ArithmeticTarget::C)),
0x92 => Box::new(sub::sub(ArithmeticTarget::D)),
0x93 => Box::new(sub::sub(ArithmeticTarget::E)),
0x94 => Box::new(sub::sub(ArithmeticTarget::H)),
0x95 => Box::new(sub::sub(ArithmeticTarget::L)),
0x87 => Box::new(add::add(RegisterTarget::A)),
0x80 => Box::new(add::add(RegisterTarget::B)),
0x81 => Box::new(add::add(RegisterTarget::C)),
0x82 => Box::new(add::add(RegisterTarget::D)),
0x83 => Box::new(add::add(RegisterTarget::E)),
0x84 => Box::new(add::add(RegisterTarget::H)),
0x85 => Box::new(add::add(RegisterTarget::L)),
0x8F => Box::new(adc::adc(RegisterTarget::A)),
0x88 => Box::new(adc::adc(RegisterTarget::B)),
0x89 => Box::new(adc::adc(RegisterTarget::C)),
0x8A => Box::new(adc::adc(RegisterTarget::D)),
0x8B => Box::new(adc::adc(RegisterTarget::E)),
0x8C => Box::new(adc::adc(RegisterTarget::H)),
0x8D => Box::new(adc::adc(RegisterTarget::L)),
0x97 => Box::new(sub::sub(RegisterTarget::A)),
0x90 => Box::new(sub::sub(RegisterTarget::B)),
0x91 => Box::new(sub::sub(RegisterTarget::C)),
0x92 => Box::new(sub::sub(RegisterTarget::D)),
0x93 => Box::new(sub::sub(RegisterTarget::E)),
0x94 => Box::new(sub::sub(RegisterTarget::H)),
0x95 => Box::new(sub::sub(RegisterTarget::L)),
0xC3 => Box::new(jmp::jmp_a16()),

other => {
Expand Down
20 changes: 10 additions & 10 deletions src/instructions/sub.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::super::*;

pub fn sub(target: ArithmeticTarget) -> impl Fn(&mut CPU) {
pub fn sub(target: RegisterTarget) -> impl Fn(&mut CPU) {
move |cpu: &mut CPU| {
let target_value = cpu.read_single_register(target);
let current_value = cpu.read_single_register(ArithmeticTarget::A);
let current_value = cpu.read_single_register(RegisterTarget::A);
let (new_value, did_overflow) = current_value.overflowing_sub(target_value);
cpu.registers.a = new_value;

Expand Down Expand Up @@ -31,7 +31,7 @@ mod tests {
},
..Default::default()
};
sub(ArithmeticTarget::C)(&mut cpu);
sub(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.registers.a, 3);
}

Expand All @@ -46,7 +46,7 @@ mod tests {
},
..Default::default()
};
sub(ArithmeticTarget::C)(&mut cpu);
sub(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.registers.a, 255);
}

Expand All @@ -61,7 +61,7 @@ mod tests {
},
..Default::default()
};
sub(ArithmeticTarget::C)(&mut cpu);
sub(RegisterTarget::C)(&mut cpu);
assert!(cpu.registers.f.carry);
}

Expand All @@ -76,7 +76,7 @@ mod tests {
},
..Default::default()
};
sub(ArithmeticTarget::C)(&mut cpu);
sub(RegisterTarget::C)(&mut cpu);
assert!(cpu.registers.f.zero);
}

Expand All @@ -94,7 +94,7 @@ mod tests {
},
..Default::default()
};
sub(ArithmeticTarget::C)(&mut cpu);
sub(RegisterTarget::C)(&mut cpu);
assert!(cpu.registers.f.subtract);
}

Expand All @@ -109,7 +109,7 @@ mod tests {
},
..Default::default()
};
sub(ArithmeticTarget::C)(&mut cpu);
sub(RegisterTarget::C)(&mut cpu);
assert!(cpu.registers.f.half_carry);
}

Expand All @@ -119,7 +119,7 @@ mod tests {
pc: 123,
..Default::default()
};
sub(ArithmeticTarget::C)(&mut cpu);
sub(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.pc, 124);
}

Expand All @@ -129,7 +129,7 @@ mod tests {
pc: 0xFFFF,
..Default::default()
};
sub(ArithmeticTarget::C)(&mut cpu);
sub(RegisterTarget::C)(&mut cpu);
assert_eq!(cpu.pc, 0);
}
}
Loading

0 comments on commit ed46459

Please sign in to comment.