Skip to content

Commit

Permalink
add the -i command line option
Browse files Browse the repository at this point in the history
  • Loading branch information
hlorenzi committed Mar 31, 2018
1 parent 4a1ffa9 commit ccd0e07
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 32 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "customasm"
version = "0.5.0"
version = "0.6.0"
authors = ["Henrique Lorenzi <[email protected]>"]

[lib]
Expand All @@ -12,5 +12,5 @@ name = "customasm"
path = "src/main.rs"

[dependencies]
getopts = "0.2.14"
num = "0.1.40"
getopts = "0.2.17"
num = "0.1.42"
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,33 @@ Also, [check out an example project](/examples/nes/) which targets the NES!
You can compile from source by simply doing `cargo build`. There's also a
battery of tests available at `cargo test`.

Usage of the command line interface is as follows:
## Upgrading from `v0.4`

Starting from `v0.6`, if you don't want to `#include` a CPU file in the main assembly file,
you can specify separate files to process in the same assembly session
with the `-i` command line option. Just remember to enclose the old CPU definition in a
`#cpudef` directive.

## Command Line Usage

```
Usage: customasm [options] <asm-file>
Options:
-h, --help Display this information.
-v, --version Display version information.
-q, --quiet Suppress progress reports.
-f, --format FORMAT The format of the output file. Possible formats:
binary, binstr, hexstr, bindump, hexdump
-i, --include FILE Specifies an additional file for processing before the
main assembly.
-o, --output FILE The name of the output file.
-p, --print Print output to stdout instead of writing to a file.
-q, --quiet Suppress progress reports.
-v, --version Display version information.
-h, --help Display this information.
```

As an example, given the following file:
## Example

Given the following file:

```asm
#cpudef
Expand Down
13 changes: 12 additions & 1 deletion src/asm/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,21 @@ impl AssemblerState
}


pub fn assemble<S>(&mut self, report: RcReport, fileserver: &FileServer, filename: S) -> Result<(), ()>
pub fn process_file<S>(&mut self, report: RcReport, fileserver: &FileServer, filename: S) -> Result<(), ()>
where S: Into<String>
{
AssemblerParser::parse_file(report.clone(), self, fileserver, filename, None)?;

match report.has_errors()
{
true => Err(()),
false => Ok(())
}
}


pub fn wrapup(&mut self, report: RcReport) -> Result<(), ()>
{
self.resolve_instrs(report.clone())?;
self.resolve_exprs(report.clone())?;
self.check_bank_overlap(report.clone());
Expand Down
43 changes: 26 additions & 17 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,18 @@ fn drive_inner(report: RcReport, opts: &getopts::Options, args: &Vec<String>, fi
}
};

let compiled = compile(report.clone(), fileserver, asm_file, quiet).map_err(|_| false)?;
let mut filenames = matches.opt_strs("i");
filenames.push(asm_file);

let assembled = assemble(report.clone(), fileserver, &filenames, quiet).map_err(|_| false)?;

let output_data = match out_format
{
OutputFormat::BinStr => compiled.generate_binstr (0, compiled.len()).bytes().collect::<Vec<u8>>(),
OutputFormat::BinDump => compiled.generate_bindump(0, compiled.len()).bytes().collect::<Vec<u8>>(),
OutputFormat::HexStr => compiled.generate_hexstr (0, compiled.len()).bytes().collect::<Vec<u8>>(),
OutputFormat::HexDump => compiled.generate_hexdump(0, compiled.len()).bytes().collect::<Vec<u8>>(),
OutputFormat::Binary => compiled.generate_binary (0, compiled.len())
OutputFormat::BinStr => assembled.generate_binstr (0, assembled.len()).bytes().collect::<Vec<u8>>(),
OutputFormat::BinDump => assembled.generate_bindump(0, assembled.len()).bytes().collect::<Vec<u8>>(),
OutputFormat::HexStr => assembled.generate_hexstr (0, assembled.len()).bytes().collect::<Vec<u8>>(),
OutputFormat::HexDump => assembled.generate_hexdump(0, assembled.len()).bytes().collect::<Vec<u8>>(),
OutputFormat::Binary => assembled.generate_binary (0, assembled.len())
};

if out_stdout
Expand Down Expand Up @@ -132,12 +135,13 @@ fn drive_inner(report: RcReport, opts: &getopts::Options, args: &Vec<String>, fi
fn make_opts() -> getopts::Options
{
let mut opts = getopts::Options::new();
opts.optflag("h", "help", "Display this information.");
opts.optflag("v", "version", "Display version information.");
opts.optflag("q", "quiet", "Suppress progress reports.");
opts.optopt("f", "format", "The format of the output file. Possible formats: binary, binstr, hexstr, bindump, hexdump", "FORMAT");
opts.optmulti("i", "include", "Specifies an additional file for processing before the main assembly.", "FILE");
opts.optopt("o", "output", "The name of the output file.", "FILE");
opts.optflag("p", "print", "Print output to stdout instead of writing to a file.");
opts.optflag("q", "quiet", "Suppress progress reports.");
opts.optflag("v", "version", "Display version information.");
opts.optflag("h", "help", "Display this information.");

opts
}
Expand Down Expand Up @@ -187,18 +191,23 @@ fn get_default_output_filename(report: RcReport, input_filename: &str) -> Result
}


pub fn compile<S>(report: RcReport, fileserver: &FileServer, asm_file: S, quiet: bool) -> Result<BinaryOutput, ()>
where S: Into<String>
pub fn assemble(report: RcReport, fileserver: &FileServer, filenames: &[String], quiet: bool) -> Result<BinaryOutput, ()>
{
let asm_file_owned = asm_file.into();

if !quiet
{ print_header(); }

let mut asm = AssemblerState::new();

for filename in filenames
{
print_header();
println!("assembling `{}`...", &asm_file_owned);
let filename_owned = filename.clone();

if !quiet
{ println!("assembling `{}`...", &filename_owned); }

asm.process_file(report.clone(), fileserver, filename_owned)?;
}

let mut asm = AssemblerState::new();
asm.assemble(report, fileserver, asm_file_owned)?;
asm.wrapup(report)?;
Ok(asm.get_binary_output())
}
18 changes: 12 additions & 6 deletions src/test/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@ where S: Into<String>
else
{ 4 };

let assemble = |report: RcReport, fileserver: &FileServer, filename: S| -> Result<(usize, String), ()>
{
let mut asm = AssemblerState::new();
asm.process_file(report.clone(), fileserver, filename)?;
asm.wrapup(report.clone())?;

let output = asm.get_binary_output();
Ok((bits, output.generate_str(bits, 0, output.len())))
};

let report = RcReport::new();

let mut asm = AssemblerState::new();
let result = asm.assemble(report.clone(), fileserver, asm_filename).ok();
let result = result.map(|_| asm.get_binary_output());
let result = result.map(|r| (bits, r.generate_str(bits, 0, r.len())));
let result = result.as_ref().map(|r| (r.0, r.1.as_ref()));
let result = assemble(report.clone(), fileserver, asm_filename);

expect_result(report.clone(), fileserver, result, expected);
expect_result(report.clone(), fileserver, result.as_ref().map(|r| (r.0, r.1.as_ref())).ok(), expected);
}


Expand Down

0 comments on commit ccd0e07

Please sign in to comment.