This commit is contained in:
2025-07-09 10:36:21 -04:00
parent a5042383c9
commit 258e4dc59b
3 changed files with 64 additions and 41 deletions
+13 -7
View File
@@ -1,11 +1,13 @@
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::path::PathBuf;
use std::thread::current;
use core::constants::constants_system::*;
use core::constants::constants_isa_op::*;
use clap::{Parser, Subcommand};
use core::instruction_table::INSTRUCTION_TABLE;
use std::io::Write;
#[derive(Parser)]
#[command(version, about, long_about = None)]
@@ -67,28 +69,29 @@ fn decompile(data: &[u8]) -> Vec<(u16, DecompiledLine)> {
let target_op = INSTRUCTION_TABLE[next_byte as usize].clone();
if let Some(top) = target_op {
println!("Instruction {:?}/{:?} needs {:?} bytes.", top.operation, top.mode, top.length);
// println!("Instruction {:?}/{:?} needs {:?} bytes.", top.operation, top.mode, top.length);
let num_bytes_to_load = top.length - 1;
println!("Need {num_bytes_to_load} more bytes.");
let mut formatted_asm = format!("{}{}", top.format_prefix, top.format_postfix);
let mut formatted_asm = String::from(top.format_prefix);
let mut param = None;
if num_bytes_to_load == 1 {
bytes.push(data[current_data_position as usize + 1]);
formatted_asm = format!("{}{:02x}", formatted_asm, bytes[1]);
formatted_asm = format!("{}{:02x}{}", formatted_asm, bytes[1], top.format_postfix);
};
if num_bytes_to_load == 2 {
bytes.push(data[current_data_position as usize + 1]);
bytes.push(data[current_data_position as usize + 2]);
// 16 bit parameter.
let param = ((bytes[2] as u16) << 8) | bytes[1] as u16;
formatted_asm = format!("{} ${:04x}", formatted_asm, param);
param = Some(((bytes[2] as u16) << 8) | bytes[1] as u16);
formatted_asm = format!("{}{:04x}{}", formatted_asm, param.unwrap(), top.format_postfix);
};
// figure out how long the instruction is and read that much more data
lines.insert(current_data_position, DecompiledLine {
offset: current_data_position,
text: formatted_asm,
text: String::from(formatted_asm),
label: None,
bytes,
});
@@ -140,9 +143,12 @@ fn main() {
println!("Loaded {}b", loaded_data.len());
// ...load the array to the WorkingProgram structure...
let result = decompile(&loaded_data);
let mut output_file = File::create(opts.output).unwrap();
// ...reap the decompiled code.
for (_, line) in &result {
println!("{:width$}; {:?}", line.text,line.bytes, width=30 );
let line =format!("{:width$}; {:?}", line.text,line.bytes, width=30 );
println!("{}", line);
writeln!(output_file, "{}", line).expect("cant write output file. you picked the red snapper.");
}
}