Skip to content

Commit

Permalink
Pass the bootrom into the interconnect
Browse files Browse the repository at this point in the history
We created a fixed length bootrom area, so we can stick
with a (fixed length) array. The type of an array is
the type of its elements AND its length - [u8; 16] != [u8; 32]
The compiler was complaining about not knowing the length, so I
stuck with [u8; gameboy::BOOTROM_SIZE] over just using Vec<u8>.
  • Loading branch information
Jonathan Dahan committed May 30, 2016
1 parent b0107e3 commit 12328eb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
26 changes: 21 additions & 5 deletions src/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
use std::fmt;

#[derive(Default, Debug)]
use interconnect;

pub struct Cpu {
pc: u16,
sp: u16,
reg_a: u8,
reg_f: u8,

reg_b: u8,
reg_c: u8,

reg_d: u8,
reg_e: u8,

reg_h: u8,
reg_l: u8
reg_l: u8,

interconnect: interconnect::Interconnect
}

impl Cpu {
pub fn new(interconnect: interconnect::Interconnect) -> Cpu {
Cpu {
pc: 0,
sp: 0,
reg_a: 0,
reg_f: 0,
reg_b: 0,
reg_c: 0,
reg_d: 0,
reg_e: 0,
reg_h: 0,
reg_l: 0,

interconnect: interconnect
}
}
pub fn run(&mut self) {
println!("I am running!");
}
Expand Down
12 changes: 9 additions & 3 deletions src/gameboy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ use std::fmt;
use cpu;
use interconnect;

#[derive(Default)]
pub const BOOTROM_SIZE: usize = 256;

pub struct GameBoy {
cpu: cpu::Cpu,
interconnect: interconnect::Interconnect
cpu: cpu::Cpu
}

impl GameBoy {
pub fn new(boot: [u8; BOOTROM_SIZE]) -> GameBoy {
let interconnect = interconnect::Interconnect::new(boot);
GameBoy {
cpu: cpu::Cpu::new(interconnect)
}
}
pub fn run(&mut self) {
self.cpu.run();
}
Expand Down
13 changes: 9 additions & 4 deletions src/interconnect.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use gameboy;
const RAM_SIZE: usize = 8 * 1024;

pub struct Interconnect {
mem: [u8; RAM_SIZE]
boot: [u8; gameboy::BOOTROM_SIZE],
wram: [u8; RAM_SIZE],
vram: [u8; RAM_SIZE]
}

impl Default for Interconnect {
fn default() -> Interconnect {
impl Interconnect {
pub fn new(boot: [u8; gameboy::BOOTROM_SIZE]) -> Interconnect {
Interconnect {
mem: [0; RAM_SIZE]
boot: boot,
vram: [0; RAM_SIZE],
wram: [0; RAM_SIZE]
}
}
}
18 changes: 12 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ use std::path::Path;

fn main() {
let boot_rom_file_name = env::args().nth(1).unwrap();
let boot = load_rom(Path::new(&boot_rom_file_name));
println!("boot: {:?}", boot);
let boot = load_bootrom(Path::new(&boot_rom_file_name));

let cart_rom_file_name = env::args().nth(2).unwrap();
let cart = load_cart(Path::new(&cart_rom_file_name));
println!("{}", cart);

let mut gb = gameboy::GameBoy::default();
gb.reset();
println!("{}", gb);
gb.run();
let mut gameboy: gameboy::GameBoy = gameboy::GameBoy::new(boot);
gameboy.reset();
println!("{}", gameboy);
gameboy.run();
}

fn load_bootrom<P: AsRef<Path>>(path: P) -> [u8; gameboy::BOOTROM_SIZE] {
let mut file = fs::File::open(path).unwrap();
let mut buffer = [0; gameboy::BOOTROM_SIZE];
file.read_exact(&mut buffer).unwrap();
buffer
}

fn load_rom<P: AsRef<Path>>(path: P) -> Vec<u8> {
Expand Down

0 comments on commit 12328eb

Please sign in to comment.