Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Dec 15, 2023
1 parent f6fba0e commit e478835
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
25 changes: 14 additions & 11 deletions src/rs/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* SPDX-License-Identifier: MIT */

use crate::cickinds::CICKind;
use crate::{utils, detect};
use crate::{detect, utils};

fn read_word_from_ram(rom_words: &[u32], entrypoint_ram: u32, ram_addr: u32) -> u32 {
rom_words[((ram_addr - entrypoint_ram + 0x1000) / 4) as usize]
Expand All @@ -26,14 +26,14 @@ pub fn calculate_checksum(rom_bytes: &[u8], kind: &CICKind) -> Option<(u32, u32)
return None;
}

let rom_words = utils::read_u32_vec(rom_bytes, 0, 0x101000/4);
let rom_words = utils::read_u32_vec(rom_bytes, 0, 0x101000 / 4);

let seed = kind.get_seed();
let magic = kind.get_magic();

let mut s6 = seed;

let mut a0 = rom_words[8/4];
let mut a0 = rom_words[8 / 4];
if *kind == CICKind::CIC_X103 {
a0 = a0.wrapping_sub(0x100000);
}
Expand Down Expand Up @@ -94,7 +94,6 @@ pub fn calculate_checksum(rom_bytes: &[u8], kind: &CICKind) -> Option<(u32, u32)
//t7 = utils.u32(t5 - v1)
let t7: u32 = t5.wrapping_sub(v1);


//let t8 = utils.u32(v0 >> t7)
//let t6 = utils.u32(v0 << v1)
let t8 = v0.wrapping_shr(t7);
Expand All @@ -116,12 +115,11 @@ pub fn calculate_checksum(rom_bytes: &[u8], kind: &CICKind) -> Option<(u32, u32)
a2 = t9 ^ a2;
// goto LA4000640;

// LA400063C:
// LA400063C:
} else {
a2 = a2 ^ a0;
}


// LA4000640:
if *kind == CICKind::CIC_X105 {
// ipl3 6105 copies 0x330 bytes from the ROM's offset 0x000554 (or offset 0x000514 into IPL3) to vram 0xA0000004
Expand Down Expand Up @@ -157,7 +155,6 @@ pub fn calculate_checksum(rom_bytes: &[u8], kind: &CICKind) -> Option<(u32, u32)
t4 = t7.wrapping_add(t4);
}


// if (t0 != ra) goto LA40005F0;
if t0 == ra {
loop_variable = false;
Expand Down Expand Up @@ -190,7 +187,7 @@ pub fn calculate_checksum(rom_bytes: &[u8], kind: &CICKind) -> Option<(u32, u32)
s0 = t8 ^ t4;
}

return Some((a3, s0))
Some((a3, s0))
}

pub fn calculate_checksum_autodetect(rom_bytes: &[u8]) -> Option<(u32, u32)> {
Expand Down Expand Up @@ -243,7 +240,7 @@ mod tests {
};
println!("CIC Kind: {:?}", kind);

for bin_path_result in fs::read_dir(ipl3_folder.path() ).unwrap() {
for bin_path_result in fs::read_dir(ipl3_folder.path()).unwrap() {
let bin_path = bin_path_result.unwrap();

println!("{:?}", bin_path);
Expand All @@ -255,12 +252,18 @@ mod tests {
println!(" Calculating checksum...");
let checksum = super::calculate_checksum(&bin_bytes, &kind).unwrap();

println!(" Calculated checksum is: 0x{:08X} 0x{:08X}", checksum.0, checksum.1);
println!(
" Calculated checksum is: 0x{:08X} 0x{:08X}",
checksum.0, checksum.1
);

println!(" Checking checksum...");
let bin_checksum = utils::read_u32_vec(&bin_bytes, 0x10, 2);

println!(" Expected checksum is: 0x{:08X} 0x{:08X}", bin_checksum[0], bin_checksum[1]);
println!(
" Expected checksum is: 0x{:08X} 0x{:08X}",
bin_checksum[0], bin_checksum[1]
);

assert_eq!(checksum.0, bin_checksum[0]);
assert_eq!(checksum.1, bin_checksum[1]);
Expand Down
2 changes: 1 addition & 1 deletion src/rs/cickinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl CICKind {
6103 | 7103 => Some(CICKind::CIC_X103),
6105 | 7105 => Some(CICKind::CIC_X105),
6106 | 7106 => Some(CICKind::CIC_X106),
_ => None
_ => None,
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/rs/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* SPDX-FileCopyrightText: © 2023 Decompollaborate */
/* SPDX-License-Identifier: MIT */

pub mod cickinds;
pub mod checksum;
pub mod cickinds;
pub mod detect;

mod utils;
Expand All @@ -14,13 +14,18 @@ use pyo3::prelude::*;
#[pymodule]
fn ipl3checksum(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<cickinds::CICKind>()?;
m.add_function(wrap_pyfunction!(checksum::python_bindings::calculateChecksum, m)?)?;
m.add_function(wrap_pyfunction!(checksum::python_bindings::calculateChecksumAutodetect, m)?)?;
m.add_function(wrap_pyfunction!(
checksum::python_bindings::calculateChecksum,
m
)?)?;
m.add_function(wrap_pyfunction!(
checksum::python_bindings::calculateChecksumAutodetect,
m
)?)?;
m.add_function(wrap_pyfunction!(detect::python_bindings::detectCICRaw, m)?)?;
m.add_function(wrap_pyfunction!(detect::python_bindings::detectCIC, m)?)?;
Ok(())
}

#[cfg(test)]
mod tests {
}
mod tests {}
4 changes: 2 additions & 2 deletions src/rs/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ pub(crate) fn read_u32(bytes: &[u8], offset: usize) -> u32 {
}

pub(crate) fn read_u32_vec(bytes: &[u8], offset: usize, len: usize) -> Vec<u32> {
let mut ret = vec![0;len];
let mut ret = vec![0; len];

for i in 0..len {
ret[i] = read_u32(bytes, offset + i*4);
ret[i] = read_u32(bytes, offset + i * 4);
}

ret
Expand Down

0 comments on commit e478835

Please sign in to comment.