more work
This commit is contained in:
+30
-15
@@ -1,5 +1,9 @@
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::BufRead;
|
||||
use std::path::Path;
|
||||
use clap::Parser;
|
||||
use gemma::chip8::instructions::Chip8CpuInstructions;
|
||||
|
||||
/// Ch8Asm
|
||||
/// Converts well formed CH8ASM.
|
||||
@@ -10,19 +14,7 @@ use clap::Parser;
|
||||
pub struct Assembler {}
|
||||
|
||||
impl Assembler {
|
||||
pub fn instruction_to_string() -> String {
|
||||
// Format the output as
|
||||
// IST [P0] [P1] [P2] ; XXXX
|
||||
// IST = 3 letter instruction code
|
||||
// P0-2 = Parameters to Instruction
|
||||
// XXXX = Hex representation of data
|
||||
// ** OR **
|
||||
// DW XXXX ;
|
||||
// DW = DATAWORD
|
||||
// XXXX = HEX OF DATA
|
||||
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
@@ -30,8 +22,6 @@ impl Assembler {
|
||||
struct AssemblerApp {
|
||||
#[arg(short)]
|
||||
input_file: Box<Path>,
|
||||
#[arg(short)]
|
||||
output_file: Option<Path>
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -39,6 +29,31 @@ fn main() {
|
||||
|
||||
let result = AssemblerApp::parse();
|
||||
|
||||
println!("Preparing to assemble {}", result.input_file.file_name());
|
||||
let mut assembled_code: Vec<u8> = vec![];
|
||||
|
||||
// println!("Preparing to assemble {}", result.input_file.file_name());
|
||||
|
||||
let path = Path::new(result.input_file);
|
||||
|
||||
// Open the file in read-only mode
|
||||
let file = File::open(&path)?;
|
||||
|
||||
// Use a buffered reader for efficient reading
|
||||
let reader = io::BufReader::new(file);
|
||||
|
||||
// Read the file line by line
|
||||
for line in reader.lines() {
|
||||
// Get the line, handling possible errors
|
||||
let line = line?;
|
||||
|
||||
// Split the line by ';' and collect parts into a vector
|
||||
let parts: Vec<&str> = line.split(';').collect();
|
||||
|
||||
// Print each part or process it as needed
|
||||
for part in &parts {
|
||||
println!("{}", part);
|
||||
}
|
||||
}
|
||||
// read the line split by a semicolon
|
||||
|
||||
}
|
||||
@@ -6,14 +6,13 @@ 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>>
|
||||
output_file: Option<Box<Path>>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -27,26 +26,25 @@ fn main() {
|
||||
// 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 = (working_instruction << 8) | (*byte as u16);
|
||||
if offset % 2 != 0 {
|
||||
let decoded = Chip8CpuInstructions::decode(working_instruction);
|
||||
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")
|
||||
}
|
||||
}
|
||||
working_instruction = 0x0000;
|
||||
_ => {
|
||||
let fill_len = 25 - decoded.to_string().len();
|
||||
println!("{}{:<fill_len$}; Bytes [{:04x}] offset [{:04x}]", decoded, " ", working_instruction, offset - 1);
|
||||
}
|
||||
}
|
||||
working_instruction = 0x0000;
|
||||
}
|
||||
}
|
||||
|
||||
match result.output_file {
|
||||
@@ -57,4 +55,4 @@ fn main() {
|
||||
println!("Output to {:?}", target);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user