Skip to content

Commit

Permalink
bin: Add help to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Granddave committed Jan 13, 2024
1 parent 9239fcb commit 7a504c1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ pub enum AssemblerError {

#[derive(Args, Debug)]
pub struct AssemblyArgs {
#[clap(help = "Input file")]
input: String,
#[clap(help = "Output file")]
#[clap(long_help = "Output file.")]
#[clap(short, value_name = "FILENAME", default_value = "a.bin")]
output: String,
}
Expand Down
10 changes: 10 additions & 0 deletions src/bin/mos6502.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,25 @@ use mos6502::{
#[command(propagate_version = true)]
struct Cli {
#[clap(long)]
#[clap(help = "Enable chrome tracing")]
#[clap(long_help = "Enable chrome tracing which on program exit will generate
a json file to be opened with a chrome tracing compatible
viewer.")]
trace: bool,
#[command(subcommand)]
command: Command,
}

#[derive(Subcommand)]
enum Command {
#[clap(about = "Assemble a program")]
#[clap(aliases = &["a", "asm"])]
Assemble(AssemblyArgs),
#[clap(about = "Disassemble a binary file")]
#[clap(aliases = &["d", "dis"])]
Disassemble(DisassemblyArgs),
#[clap(about = "Run a program in the emulator")]
#[clap(aliases = &["e", "emu"])]
Emulate(EmulationArgs),
}

Expand Down
1 change: 1 addition & 0 deletions src/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod listing;

#[derive(Args, Debug)]
pub struct DisassemblyArgs {
#[clap(help = "Input file to disassemble")]
input: String,
}

Expand Down
17 changes: 12 additions & 5 deletions src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ pub mod tui;

#[derive(Args, Debug)]
pub struct EmulationArgs {
#[clap(help = "Input file")]
#[clap(long_help = "Input file. Allowed file types: .bin, .asm")]
input: String,
#[clap(help = "Run the emulator without the terminal user interface")]
#[clap(long_help = "Run the emulator without the terminal user interface.
The program will run until it encounters a break instruction.")]
#[clap(long)]
tui: bool,
headless: bool,
}

/// Simple utility function to run the emulator without the terminal user interface.
/// Runs the emulator with the given program bytes and program start address.
#[tracing::instrument]
pub fn run(program_bytes: &[u8], program_start: u16) -> Result<()> {
pub fn run_headless(program_bytes: &[u8], program_start: u16) -> Result<()> {
let mut memory = Memory::new();
memory.write_word(cpu::RESET_VECTOR, program_start); // TODO: Include in the program
memory.load(0x0000, program_bytes);
Expand All @@ -38,6 +43,7 @@ pub fn run(program_bytes: &[u8], program_start: u16) -> Result<()> {
#[tracing::instrument]
pub fn emulate(args: &EmulationArgs) -> Result<()> {
const PROGRAM_START: u16 = 0x8000;

let program_bytes = if args.input.ends_with(".bin") {
// Read binary file
std::fs::read(&args.input).with_context(|| "Unable to read file")?
Expand All @@ -49,10 +55,11 @@ pub fn emulate(args: &EmulationArgs) -> Result<()> {
} else {
return Err(anyhow::anyhow!("Unknown file type. Allowed: .bin, .asm"));
};
if args.tui {
tui::exec(&program_bytes, PROGRAM_START)?;

if args.headless {
run_headless(&program_bytes, PROGRAM_START)?;
} else {
run(&program_bytes, PROGRAM_START)?;
tui::exec(&program_bytes, PROGRAM_START)?;
};
Ok(())
}

0 comments on commit 7a504c1

Please sign in to comment.