Skip to content

ch5- Fix se and sne implementation for cpu-4 #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 1st-edition
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions ch5/ch5-cpu4/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ impl CPU {
}

fn se(&mut self, vx: u8, kk: u8) {
if vx == kk {
if self.registers[vx as usize] == kk {
self.position_in_memory += 2;
}
}

/// () SNE **S**tore if **n**ot **e**qual
fn sne(&mut self, vx: u8, kk: u8) {
if vx != kk {
if self.registers[vx as usize] != kk {
self.position_in_memory += 2;
}
}
Expand Down Expand Up @@ -102,9 +102,10 @@ impl CPU {

// (7xkk)
fn add_xy(&mut self, x: u8, y: u8) {
self.registers[x as usize] += self.registers[y as usize];

// TODO: SET CARRY FLAG!!!!
/// Set carry flag
let (wrapped, is_overflow) = self.registers[x as usize].overflowing_add(self.registers[y as usize]);
self.registers[0xF] = if is_overflow {1} else {0};
self.registers[x as usize] = wrapped;
}

fn and_xy(&mut self, x: u8, y: u8) {
Expand Down Expand Up @@ -154,4 +155,4 @@ fn main() {
assert_eq!(cpu.registers[0], 45);

println!("5 + (10 * 2) + (10 * 2) = {}", cpu.registers[0]);
}
}