-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
821f437
commit 586435d
Showing
27 changed files
with
1,178 additions
and
905 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[toolchain] | ||
channel = "1.77.0" | ||
channel = "nightly" | ||
components = [ "rustfmt", "clippy", "rust-analyzer", "rust-src" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use crate::gameboy; | ||
|
||
pub fn run(cartridge: &[u8; 0x200000]) { | ||
let mut gameboy = gameboy::Gameboy::default(); | ||
gameboy.run(cartridge); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use crate::cpu::{FlagsRegister, Register16bTarget, CPU}; | ||
use crate::instructions; | ||
use crate::memory::MemoryBus; | ||
use crate::opcode_info::OpcodeInfo; | ||
use serde_json; | ||
|
||
pub struct Gameboy<'a> { | ||
pub cpu: CPU, | ||
pub bus: MemoryBus, | ||
pub cartridge: &'a [u8; 0x200000], | ||
pub opcode_info: OpcodeInfo, | ||
} | ||
|
||
impl Default for Gameboy<'_> { | ||
fn default() -> Self { | ||
// load opcode info | ||
let json = include_bytes!("Opcodes.json"); | ||
let opcode_info: OpcodeInfo = serde_json::from_slice(json).unwrap(); | ||
|
||
let cpu = CPU::default(); | ||
|
||
Self { | ||
cpu, | ||
opcode_info, | ||
bus: MemoryBus::default(), | ||
cartridge: &[0; 0x200000], | ||
} | ||
} | ||
} | ||
|
||
// DMG initialization sequence | ||
pub fn initialize(gameboy: &mut Gameboy) { | ||
gameboy.cpu.registers.a = 0x01; | ||
gameboy.cpu.registers.f.zero = true; | ||
//TODO: set carry and half carry based on header checksum | ||
gameboy.cpu.registers.c = 0x13; | ||
gameboy.cpu.registers.e = 0xD8; | ||
gameboy.cpu.registers.h = 0x01; | ||
gameboy.cpu.registers.l = 0x4D; | ||
gameboy.cpu.registers.pc = 0x100; | ||
} | ||
|
||
impl<'a> Gameboy<'a> { | ||
pub fn run(self: &'a mut Self, cartridge: &'a [u8; 0x200000]) { | ||
self.cartridge = cartridge; | ||
|
||
// load first 0x8000 bytes of cartridge into memory | ||
self.bus.memory[..0x8000].copy_from_slice(&self.cartridge[..0x8000]); | ||
|
||
loop { | ||
self.step(); | ||
} | ||
} | ||
|
||
pub fn step(&mut self) { | ||
let instruction_byte = self.read_next_byte(); | ||
let opcode_info = self | ||
.opcode_info | ||
.unprefixed | ||
.get(&format!("0x{:02X}", instruction_byte)) | ||
.unwrap(); | ||
log::debug!( | ||
"instruction_byte: 0x{:02X}, program counter: 0x{:04X}", | ||
instruction_byte, | ||
self.cpu.registers.get_u16(Register16bTarget::PC) | ||
); | ||
let instruction = instructions::from_byte(instruction_byte); | ||
instruction(self); | ||
} | ||
|
||
pub fn read_next_byte(&mut self) -> u8 { | ||
let address = self.cpu.registers.get_u16(Register16bTarget::PC); | ||
let byte = self.bus.read_byte(address); | ||
self.cpu | ||
.registers | ||
.set_u16(Register16bTarget::PC, address.wrapping_add(1)); | ||
byte | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::cpu::{FlagsRegister, Register16bTarget, RegisterTarget, Registers}; | ||
|
||
#[test] | ||
fn test_add() { | ||
let mut gameboy = Gameboy { | ||
cpu: CPU { | ||
registers: Registers { | ||
a: 3, | ||
c: 4, | ||
f: FlagsRegister::from(0), | ||
pc: 1245, | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}, | ||
bus: MemoryBus { | ||
memory: [0x81; 0x10000], | ||
}, | ||
..Default::default() | ||
}; | ||
gameboy.step(); | ||
assert_eq!(gameboy.cpu.registers.get_u8(RegisterTarget::A), 7); | ||
assert_eq!(gameboy.cpu.registers.get_u16(Register16bTarget::PC), 1246); | ||
} | ||
} |
Oops, something went wrong.