diff --git a/src/main.rs b/src/main.rs index c4ce230..1a23fb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,10 +4,38 @@ use std::fs; mod device; mod ui; +fn swap_rom(contents: Vec) -> Vec { + let test = u32::from_be_bytes(contents[0..4].try_into().unwrap()); + if test == 0x80371240 { + // z64 + return contents; + } else if test == 0x37804012 { + // v64 + let mut data: Vec = vec![0; contents.len()]; + for i in (0..contents.len()).step_by(2) { + let temp = u16::from_ne_bytes(contents[i..i + 2].try_into().unwrap()); + data[i..i + 2].copy_from_slice(&temp.to_be_bytes()); + } + return data; + } else if test == 0x40123780 { + // n64 + let mut data: Vec = vec![0; contents.len()]; + for i in (0..contents.len()).step_by(4) { + let temp = u32::from_ne_bytes(contents[i..i + 4].try_into().unwrap()); + data[i..i + 4].copy_from_slice(&temp.to_be_bytes()); + } + return data; + } else { + panic!("unknown rom format") + } +} + fn main() { let args: Vec = env::args().collect(); let file_path = &args[1]; - let contents = fs::read(file_path).expect("Should have been able to read the file"); + let mut contents = fs::read(file_path).expect("Should have been able to read the file"); + + contents = swap_rom(contents); let mut device = device::Device::new();