60 lines
2.1 KiB
Rust
60 lines
2.1 KiB
Rust
use std::path::Path;
|
|
use clap::{command, Parser};
|
|
use gemma::chip8::instructions::Chip8CpuInstructions;
|
|
use gemma::chip8::instructions::Chip8CpuInstructions::XXXXERRORINSTRUCTION;
|
|
|
|
/// ch8disasm
|
|
///
|
|
/// Provide disassembled version of the CH8 file provided.
|
|
|
|
#[derive(Parser)]
|
|
#[command(version, about, long_about = None)]
|
|
struct DisassemblerApp {
|
|
#[arg(short)]
|
|
input_file: Box<Path>,
|
|
#[arg(short)]
|
|
output_file: Option<Box<Path>>
|
|
}
|
|
|
|
fn main() {
|
|
println!("Taxation is Theft");
|
|
let result = DisassemblerApp::parse();
|
|
println!("PREPARING TO DISASSEMBLE {:?}", result.input_file.file_name());
|
|
let mut last_byte: u8 = 0x00;
|
|
let mut working_instruction: u16 = 0x0000;
|
|
let mut dump_as_data: bool = false;
|
|
|
|
// read the input file and loop through it byte by byte.
|
|
|
|
for (offset, byte) in std::fs::read(result.input_file).unwrap().iter().enumerate() {
|
|
working_instruction = (working_instruction << 8) | (*byte as u16);
|
|
if offset % 2 != 0 {
|
|
// println!("ODD BYTE -> {:02x} / WORKING: {working_instruction:04x}", *byte as u16);
|
|
let decoded = Chip8CpuInstructions::decode(working_instruction);
|
|
match decoded {
|
|
XXXXERRORINSTRUCTION => {
|
|
println!("\t\t; ERROR: BYTES: {} OFFSET: {}", working_instruction, offset);
|
|
println!("FOUND ERROR INSTRUCTION. LIKELY DATA.");
|
|
}
|
|
Chip8CpuInstructions::JpAddr(x) => {
|
|
if (offset + 0x200) == x as usize {
|
|
println!("INFINITE LOOP")
|
|
}
|
|
}
|
|
_ => {
|
|
println!("{}\t\t; Bytes [{:04x}] ending offset [{:04x}]", decoded, working_instruction, offset);
|
|
}
|
|
}
|
|
working_instruction = 0x0000;
|
|
}
|
|
}
|
|
|
|
match result.output_file {
|
|
None => {
|
|
println!("Output to console.");
|
|
}
|
|
Some(target) => {
|
|
println!("Output to {:?}", target);
|
|
}
|
|
}
|
|
} |