Commit cb9f6277 authored by Arthur Carlsson's avatar Arthur Carlsson

Merge branch 'part-2' into 'master'

Part 2 - Basic stuff



See merge request !1
parents ed4123ef eb31bcca
Pipeline #4105891 passed with stage
in 2 minutes and 13 seconds
target
Cargo.lock
.DS_Store
......@@ -4,3 +4,5 @@ version = "0.1.0"
authors = ["Arthur Carlsson <arthur@kiron.net>"]
[dependencies]
clap = "2.10"
byteorder = "0.5"
......@@ -10,3 +10,5 @@ Nice-to-have Links
------------------
* [Sublime Text](https://www.sublimetext.com)
* [Hex Viewer Package for Sublime Text](https://github.com/facelessuser/HexViewer)
* [Online Disassembler](https://www.onlinedisassembler.com/odaweb/)
* [VR4300 Datasheet](http://datasheets.chipdb.org/NEC/Vr-Series/Vr43xx/U10504EJ7V0UMJ1.pdf)
extern crate byteorder;
mod types;
mod rom;
mod pif_rom;
pub mod n64;
pub use types::Word;
pub use pif_rom::PifRom;
extern crate rustn64;
extern crate clap;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use clap::{App, Arg};
use rustn64::PifRom;
use rustn64::n64::Cart;
fn main() {
println!("Hello World!");
let matches = App::new("RustN64")
.version(env!("CARGO_PKG_VERSION"))
.author("Arthur Carlsson <arthur@kiron.net>")
.about("A Nintendo 64 emulator written in Rust")
.arg(Arg::with_name("PIF")
.help("The PIF ROM file to use")
.short("p")
.long("pif")
.use_delimiter(false)
.takes_value(true)
.required(true))
.arg(Arg::with_name("ROM")
.help("The ROM file to use")
.use_delimiter(false)
.required(true)
.index(1))
.get_matches();
let pif = read_bin(matches.value_of("PIF").unwrap());
let rom = read_bin(matches.value_of("ROM").unwrap());
PifRom::new(pif);
Cart::new(rom);
}
fn read_bin<P: AsRef<Path>>(path: P) -> Vec<u8> {
let mut buf = vec![0];
match File::open(path.as_ref()).and_then(|mut f| f.read_to_end(&mut buf)) {
Ok(_) => buf,
Err(e) => {
println!("Failed to load \"{}\": {}", path.as_ref().display(), e);
std::process::exit(1);
}
}
}
use byteorder::BigEndian;
use rom::Rom;
pub struct Cart {
rom: Rom<BigEndian>
}
impl Cart {
pub fn new(buf: Vec<u8>) -> Cart {
Cart {
rom: Rom::new(buf)
}
}
}
mod cart;
pub use self::cart::Cart;
use byteorder::BigEndian;
use rom::Rom;
use types::Word;
pub struct PifRom {
rom: Rom<BigEndian>
}
impl PifRom {
pub fn new(rom: Vec<u8>) -> PifRom {
PifRom {
rom: Rom::new(rom)
}
}
pub fn read_word(&self, i: u32) -> Word {
self.rom.read_word(i as usize)
}
}
use std::marker::PhantomData;
use types::Word;
use byteorder::ByteOrder;
pub struct Rom<T: ByteOrder> {
buf: Vec<u8>,
byte_order: PhantomData<T>
}
impl<T: ByteOrder> Rom<T> {
pub fn new(buf: Vec<u8>) -> Rom<T> {
Rom {
buf: buf,
byte_order: PhantomData
}
}
pub fn read_word(&self, i: usize) -> Word {
T::read_u32(&self.buf[i..])
}
}
#[cfg(test)]
mod tests {
use byteorder::{BigEndian,LittleEndian};
use super::*;
#[test]
fn test_that_the_byte_order_type_is_honored() {
let be = Rom::<BigEndian>::new(vec![0,0,0,1]);
assert_eq!(1, be.read_word(0));
let le = Rom::<LittleEndian>::new(vec![1,0,0,0]);
assert_eq!(1, le.read_word(0));
}
}
pub type Word = u32;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment