Skip to content

Commit

Permalink
jump: change jump implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmann committed May 18, 2024
1 parent 39d4cce commit a40e996
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 73 deletions.
14 changes: 10 additions & 4 deletions gameboy-bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
extern crate gameboy_lib;

use std::{fs::{self, File}, io::Write};

use gameboy_lib::Gameboy;

fn main() {
println!("{}", std::env::current_dir().unwrap().display());
let boot_rom = load_boot_rom();
let rom = load_rom("./roms/tetris.gb");
let mut gameboy: Gameboy = Gameboy::new(boot_rom, rom);
let _rom = load_rom("./roms/tetris.gb");
let mut gameboy: Gameboy = Gameboy::new(boot_rom, vec![]);
gameboy.start();

let mem_dump = gameboy.dump_memory();
let mut file = File::create("memory.bin").expect("Error while creating memory.bin");
file.write_all(&mem_dump.as_slice()).expect("Error while writing memory.bin");
}

fn load_boot_rom() -> Vec<u8> {
return std::fs::read("./boot_roms/dmg_boot.bin").expect("Error while reading boot rom");
return fs::read("./boot_roms/dmg_boot.bin").expect("Error while reading boot rom");
}

fn load_rom(file: &str) -> Vec<u8> {
return std::fs::read(file).expect("Error while reading rom");
return fs::read(file).expect("Error while reading rom");
}
9 changes: 5 additions & 4 deletions gameboy-lib/src/cpu/command/bit_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ impl<'a> BitCommand<'a> {
}

fn bit(&mut self, bit: &u8, from: &Register) -> u16 {
let value = self.cpu.registers.get(from);
let result = value & (1 << bit);
let (value, pc) = self.cpu.extract_operand(from);

self.cpu.registers.f.zero = result == 0;
self.cpu.registers.f.zero = (value >> bit) & 0xF == 0;
self.cpu.registers.f.subtract = false;
self.cpu.registers.f.half_carry = true;

self.cpu.pc.wrapping_add(2)
println!("[CPU] Bit {} from 0x{:02X} is {}", bit, value, self.cpu.registers.f.zero);

pc
}

fn res(&mut self, bit: &u8, from: &Register) -> u16 {
Expand Down
19 changes: 12 additions & 7 deletions gameboy-lib/src/cpu/command/jump_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ impl<'a> JumpCommand<'a> {
}

fn jp_cc(&mut self, condition: &FlagCondition) -> u16 {
let address = self.cpu.memory.read_16(self.cpu.pc + 1);
if self.cpu.resolve_flag_condition(condition) {
address
self.jp()
} else {
self.cpu.pc.wrapping_add(3)
}
Expand All @@ -34,16 +33,22 @@ impl<'a> JumpCommand<'a> {
}

fn jr(&mut self) -> u16 {
let offset = self.cpu.memory.read(self.cpu.pc + 1) as u16;
self.cpu.pc.wrapping_add(offset)
let offset = self.cpu.memory.read(self.cpu.pc + 1) as i8;
let new_pc = self
.cpu
.pc
.wrapping_add(2)
.wrapping_add(offset as i16 as u16);
println!("[CPU] Jump to address 0x{:x}", new_pc);
new_pc
}

fn jr_cc(&mut self, condition: &FlagCondition) -> u16 {
let offset = self.cpu.memory.read(self.cpu.pc + 1) as u16;
let next_step = self.cpu.pc.wrapping_add(2);
if self.cpu.resolve_flag_condition(condition) {
self.cpu.pc.wrapping_add(offset)
self.jr()
} else {
self.cpu.pc.wrapping_add(2)
return next_step;
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions gameboy-lib/src/cpu/command/load_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,22 @@ impl<'a> LoadCommand<'a> {
self.cpu.pc.wrapping_add(2)
}
LoadInstruction::LdHi => {
let address = self.cpu.registers.get_16(&Register::HL) + 1;
let address = self.cpu.registers.get_16(&Register::HL);
let value = self.cpu.registers.get(&Register::A);
self.cpu.memory.write(address, value);

let value = self.cpu.registers.get_16(&Register::HL).wrapping_add(1);
self.cpu.registers.set_16(&Register::HL, value);

self.cpu.pc.wrapping_add(1)
}
LoadInstruction::LdHd => {
let address = self.cpu.registers.get_16(&Register::HL) - 1;
let address = self.cpu.registers.get_16(&Register::HL);
let value = self.cpu.registers.get(&Register::A);
self.cpu.memory.write(address, value);

let value = self.cpu.registers.get_16(&Register::HL).wrapping_sub(1);
self.cpu.registers.set_16(&Register::HL, value);

self.cpu.pc.wrapping_add(1)
}
Expand Down
Loading

0 comments on commit a40e996

Please sign in to comment.