Skip to content

Commit

Permalink
Create initial model of the cpu
Browse files Browse the repository at this point in the history
The cpu has some registers, and we can print them out.
  • Loading branch information
Jonathan Dahan committed May 27, 2016
1 parent 7670db1 commit 356cea5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target*/
roms/
DMG_ROM.bin
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ It should only show 1 passing test - if you want to see that it is testing your

To run a game

cargo run -- roms/{DMG_ROM.bin,Super\ Mario\ Land\ \(World\).gb}
cargo run -- DMG_ROM.bin roms/Super\ Mario\ Land\ \(World\).gb

Note, this requires the game boy boot rom, which I cannot distribute. You can google for it though.
45 changes: 35 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,54 @@ use std::env;
use std::fs;
use std::io::Read;
use std::path::Path;
use std::fmt;

use lib::cart;

struct Cpu {
pc: u16,
sp: u16,
registers: [u8; 8],
wram: [u8; 1024],
vram: [u8; 1024]
}

impl Cpu {
fn new() -> Cpu {
Cpu {
pc: 0x0100,
sp: 0xFFFE,
// A, B, D, H, F, C, E, L?
registers: [0 as u8; 8],
wram: [0; 1024],
vram: [0; 1024]
}
}
}

impl fmt::Display for Cpu {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "{:?}", self.registers);
writeln!(f, "pc: {:0>4X}", self.pc);
writeln!(f, "sp: {:0>4X}", self.sp);
Ok(())
}
}

fn main() {
let boot_rom_file_name = env::args().nth(1).unwrap();
let cart_rom_file_name = env::args().nth(2).unwrap();
let boot = load_rom(boot_rom_file_name);
let cart = load_cart(cart_rom_file_name);
let registers = [0 as u8; 8]; // A, B, D, H, F, C, E, L?
let pc = 0x0100 as u16;
let sp = 0xFFFE as u16;

let wram = [0; 1024];
let vram = [0; 1024];

println!("{}", cart);
println!("{:?}", registers);
println!("pc: {:0>4X}", pc);
println!("sp: {:0>4X}", sp);

let cpu = Cpu::new();
println!("{}", cpu);
}

fn load_rom<P: AsRef<Path>>(path: P) -> Vec<u8> {
let mut file = fs::File::open(path.as_ref()).unwrap();
let mut file = fs::File::open(path).unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();
buffer
Expand Down

0 comments on commit 356cea5

Please sign in to comment.