disassembler disassembles

This commit is contained in:
Trevor Merritt 2024-10-21 13:18:46 -04:00
parent e927f6785f
commit 665309c2e4
6 changed files with 122 additions and 35 deletions

View File

@ -8,7 +8,7 @@ use crate::chip8::computer::{Chip8Computer};
use crate::chip8::cpu_states::Chip8CpuStates::WaitingForKey;
use crate::chip8::instructions::Chip8CpuInstructions::XXXXERRORINSTRUCTION;
use crate::chip8::util::InstructionUtil;
use crate::constants::{INST_ADD, INST_ADDI, INST_ADDR, INST_AND, INST_BCD, INST_CALL, INST_CLS, INST_DRW, INST_EXIT, INST_JPA, INST_JPI, INST_LDD, INST_LDF, INST_LDF2, INST_LDIA, INST_LDIX, INST_LDR, INST_LDRD, INST_LDRI, INST_LDRK, INST_LDRY, INST_LIDR, INST_LIDS};
use crate::constants::{*};
/*
nnn or addr - A 12-bit value, the lowest 12 bits of the instruction
@ -286,27 +286,27 @@ impl Chip8CpuInstructions {
Chip8CpuInstructions::LDRI(_) => INST_LDRI,
Chip8CpuInstructions::LDRK(_) => INST_LDRK,
Chip8CpuInstructions::LDR_Y(_, _) => INST_LDRY,
Chip8CpuInstructions::OR(_, _) => "OR",
Chip8CpuInstructions::RET => "RET",
Chip8CpuInstructions::RND(_, _) => "RND",
Chip8CpuInstructions::SDN(_) => "SDN",
Chip8CpuInstructions::SLF => "SLF",
Chip8CpuInstructions::SRT => "SRT",
Chip8CpuInstructions::SEX(_, _) => "SEX",
Chip8CpuInstructions::SEY(_, _) => "SEY",
Chip8CpuInstructions::SHL(_, _) => "SHL",
Chip8CpuInstructions::SHR(_, _) => "SHR",
Chip8CpuInstructions::SKP(_) => "SKP",
Chip8CpuInstructions::SNEB(_, _) => "SNEB",
Chip8CpuInstructions::SNEY(_, _) => "SNEY",
Chip8CpuInstructions::SKNP(_) => "SNKP",
Chip8CpuInstructions::STR(x) => "STR",
Chip8CpuInstructions::SUB(_, _) => "SUB",
Chip8CpuInstructions::SUBC(_, _) => "SUBC",
Chip8CpuInstructions::SYS(_) => "SYS",
Chip8CpuInstructions::DIS => "DIS",
Chip8CpuInstructions::ENA => "ENA",
Chip8CpuInstructions::ORY(_, _) => "ORY",
Chip8CpuInstructions::OR(_, _) => INST_OR,
Chip8CpuInstructions::RET => INST_RET,
Chip8CpuInstructions::RND(_, _) => INST_RND,
Chip8CpuInstructions::SDN(_) => INST_SDN,
Chip8CpuInstructions::SLF => INST_SLF,
Chip8CpuInstructions::SRT => INST_SRT,
Chip8CpuInstructions::SEX(_, _) => INST_SEX,
Chip8CpuInstructions::SEY(_, _) => INST_SEY,
Chip8CpuInstructions::SHL(_, _) => INST_SHL,
Chip8CpuInstructions::SHR(_, _) => INST_SHR,
Chip8CpuInstructions::SKP(_) => INST_SKP,
Chip8CpuInstructions::SNEB(_, _) => INST_SNEB,
Chip8CpuInstructions::SNEY(_, _) => INST_SNEY,
Chip8CpuInstructions::SKNP(_) => INST_SNKP,
Chip8CpuInstructions::STR(x) => INST_STR,
Chip8CpuInstructions::SUB(_, _) => INST_SUB,
Chip8CpuInstructions::SUBC(_, _) => INST_SUBC,
Chip8CpuInstructions::SYS(_) => INST_SYS,
Chip8CpuInstructions::DIS => INST_DIS,
Chip8CpuInstructions::ENA => INST_ENA,
Chip8CpuInstructions::ORY(_, _) => INST_ORY,
XXXXERRORINSTRUCTION => "XX ERROR XX",
}
}
@ -889,7 +889,6 @@ impl Chip8CpuInstructions {
println!("SCROLLING LEFT 4 LINES");
}
Chip8CpuInstructions::DIS => {
println!("DISABLE VIDEO MODE");
}
Chip8CpuInstructions::ENA => {

View File

@ -39,3 +39,27 @@ pub const INST_LDRD: &str = "LDRD";
pub const INST_LDRI: &str = "LDRI";
pub const INST_LDRK: &str = "LDRK";
pub const INST_LDRY: &str = "LDRY";
pub const INST_OR: &str = "OR";
pub const INST_RET: &str = "RET";
pub const INST_RND: &str = "RND";
pub const INST_SDN: &str = "SDN";
pub const INST_SRT: &str = "SRT";
pub const INST_SLF: &str = "SLF";
pub const INST_SEX: &str = "SEX";
pub const INST_SEY: &str = "SEY";
pub const INST_SHL: &str = "SHL";
pub const INST_SHR: &str = "SHR";
pub const INST_SKP: &str = "SKP";
pub const INST_SNEB: &str = "SNEB";
pub const INST_SNEY: &str = "SNEY";
pub const INST_SNKP: &str = "SNKP";
pub const INST_STR : &str = "STR";
pub const INST_SUB: &str = "SUB";
pub const INST_SUBC: &str = "SUBC";
pub const INST_SYS: &str = "SYS";
pub const INST_DIS: &str = "DIS";
pub const INST_ENA: &str = "ENA";
pub const INST_ORY: &str = "ORY";

View File

@ -15,34 +15,50 @@ struct DisassemblerApp {
output_file: Option<Box<Path>>,
}
struct Disassembler {}
impl Disassembler {
pub fn disassemble(from_data: Vec<u8>) -> String {
String::new()
}
}
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 target_file =
let source_file = result.input_file.to_str().unwrap();
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() {
let mut output_string = String::new();
for (offset, byte) in std::fs::read(source_file).unwrap().iter().enumerate() {
working_instruction = (working_instruction << 8) | (*byte as u16);
if offset % 2 != 0 {
let decoded = Chip8CpuInstructions::decode(working_instruction);
let decoded_string = decoded.to_string();
let mut current_parts = String::new();
match decoded {
XXXXERRORINSTRUCTION => {
println!("DW 0x{working_instruction:04x}{:>16}; {working_instruction:04x}", " ");
}
Chip8CpuInstructions::JPA(x) => {
if (offset + 0x200) == x as usize {
println!("INFINITE LOOP")
}
current_parts = format!("DW 0x{:04x}", working_instruction);
}
_ => {
let fill_len = 25 - decoded.to_string().len();
println!("{}{:<fill_len$}; Bytes [{:04x}] offset [{:04x}]", decoded, " ", working_instruction, offset - 1);
current_parts = format!("{}", decoded);
}
}
};
let target_length: i32 = 25;
let spacing_length = target_length.saturating_sub(current_parts.len() as i32);
// now add the rest after the string
let x = spacing_length as usize;
current_parts = format!("{}{:<x$}; Bytes [0x{:04x}] offset [0x{:04x}]\n", current_parts, " ", working_instruction, offset -1);
// println!("SHOULD OUTPUT: [{current_parts}]");
output_string = output_string.to_string() + &*current_parts.to_string();
working_instruction = 0x0000;
}
}
@ -50,9 +66,21 @@ fn main() {
match result.output_file {
None => {
println!("Output to console.");
println!("OS: \n\n{}", output_string);
}
Some(target) => {
println!("Output to {:?}", target);
std::fs::write(target, output_string).unwrap();
}
}
}
mod test {
#[test]
fn smoke() { assert!(true); }
#[test]
fn assemble_test_file() {
}
}

View File

@ -0,0 +1,36 @@
CLS ; Bytes [0x00e0] offset [0x0000]
RET ; Bytes [0x00ee] offset [0x0002]
SYS 0x0123 ; Bytes [0x0123] offset [0x0004]
JPA 0x0123 ; Bytes [0x1123] offset [0x0006]
CALL 0x0123 ; Bytes [0x2123] offset [0x0008]
SEX 0x01, 0x23 ; Bytes [0x3123] offset [0x000a]
SNEB 0x01, 0x23 ; Bytes [0x4123] offset [0x000c]
SEY 0x01, 0x02 ; Bytes [0x5120] offset [0x000e]
LDR 0x01, 0x23 ; Bytes [0x6123] offset [0x0010]
ADD 0x01, 0x23 ; Bytes [0x7123] offset [0x0012]
LDRY 0x01, 0x02 ; Bytes [0x8120] offset [0x0014]
OR 0x01, 0x02 ; Bytes [0x8121] offset [0x0016]
AND 0x01, 0x02 ; Bytes [0x8122] offset [0x0018]
ORY 0x01, 0x02 ; Bytes [0x8123] offset [0x001a]
ADDR 0x01, 0x02 ; Bytes [0x8124] offset [0x001c]
SUB 0x01, 0x02 ; Bytes [0x8125] offset [0x001e]
SHR 0x01, 0x02 ; Bytes [0x8126] offset [0x0020]
SUBC 0x01, 0x02 ; Bytes [0x8127] offset [0x0022]
SHL 0x01, 0x02 ; Bytes [0x812e] offset [0x0024]
SNEY 0x01, 0x02 ; Bytes [0x9120] offset [0x0026]
LDIA 0x0123 ; Bytes [0xa123] offset [0x0028]
JPI 0x0123 ; Bytes [0xb123] offset [0x002a]
RND 0x01, 0x23 ; Bytes [0xc123] offset [0x002c]
DRW 0x01, 0x02, 0x03 ; Bytes [0xd123] offset [0x002e]
SKP 0x01 ; Bytes [0xe19e] offset [0x0030]
SNKP ; Bytes [0xe1a1] offset [0x0032]
LDRD 0x01 ; Bytes [0xf107] offset [0x0034]
LDRK 0x01 ; Bytes [0xf10a] offset [0x0036]
LDD 0x01 ; Bytes [0xf115] offset [0x0038]
LIDS 0x01 ; Bytes [0xf118] offset [0x003a]
ADDI 0x01 ; Bytes [0xf11e] offset [0x003c]
LDF 0x01 ; Bytes [0xf129] offset [0x003e]
BCD 0x01 ; Bytes [0xf133] offset [0x0040]
LDIX 0x01 ; Bytes [0xf155] offset [0x0042]
LDRI 0x01 ; Bytes [0xf165] offset [0x0044]
DW 0xffff ; Bytes [0xffff] offset [0x0046]

Binary file not shown.