Skip to content

Commit

Permalink
Add output format for the game "Turing Complete"
Browse files Browse the repository at this point in the history
Players of the game "Turing Complete" often use
customasm to design a richer assembly language
than the game provides and then copy the resulting
binary into the game.

This change introduces an annotated format compatible
with the Turing Complete assembly editor, using '#'
for comments and '0x' or '0b' to prefix data groups.

This change also removes some trailing whitespace.
  • Loading branch information
thenorili committed Nov 6, 2023
1 parent 3913be5 commit 54a8523
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 140 deletions.
81 changes: 49 additions & 32 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ struct CommandOutput
pub enum OutputFormat
{
Binary,
TCGame {
base: usize,
group: usize,
},
Annotated {
base: usize,
group: usize,
Expand All @@ -44,7 +48,7 @@ pub enum OutputFormat
LogiSim8,
LogiSim16,
AddressSpan,

Symbols,
SymbolsMesenMlb,
}
Expand All @@ -56,20 +60,20 @@ pub fn drive_from_commandline(
-> Result<(), ()>
{
util::enable_windows_ansi_support();

let mut report = diagn::Report::new();

let maybe_command = parse_command(
&mut report,
args);

if let Ok(command) = maybe_command
{
{
let maybe_result = assemble_with_command(
&mut report,
fileserver,
&command);

report.print_all(
&mut std::io::stderr(),
fileserver,
Expand Down Expand Up @@ -98,7 +102,7 @@ pub fn drive(
let command = parse_command(
report,
args)?;

let maybe_result = assemble_with_command(
report,
fileserver,
Expand All @@ -119,7 +123,7 @@ fn assemble_with_command(
print_usage(command.use_colors);
return Ok(asm::AssemblyResult::new());
}

if command.show_version
{
print_version_full();
Expand Down Expand Up @@ -173,7 +177,7 @@ fn assemble_with_command(
{
println!("");
}

println!(
"{}",
String::from_utf8_lossy(&formatted));
Expand All @@ -193,15 +197,15 @@ fn assemble_with_command(
}
}
}

if !command.quiet
{
println!(
"resolved in {} iteration{}",
iterations_taken,
if iterations_taken == 1 { "" } else { "s" });
}

Ok(assembly)
}

Expand All @@ -211,7 +215,7 @@ fn make_opts() -> getopts::Options
let asm_opts = asm::AssemblyOptions::new();

let mut opts = getopts::Options::new();

opts.optopt(
"f", "format",
"The format of the output file.\n\
Expand All @@ -224,7 +228,7 @@ fn make_opts() -> getopts::Options
"FILE",
getopts::HasArg::Maybe,
getopts::Occur::Optional);

opts.opt(
"t", "iters",
&format!(
Expand All @@ -233,7 +237,7 @@ fn make_opts() -> getopts::Options
"NUM",
getopts::HasArg::Maybe,
getopts::Occur::Optional);

opts.optflag(
"p", "print",
"Print the output to the screen instead of writing to a file.");
Expand All @@ -248,14 +252,14 @@ fn make_opts() -> getopts::Options
"VALUE",
getopts::HasArg::Yes,
getopts::Occur::Multi);

opts.opt(
"", "color",
"Style the output with colors. [on/off]",
"VALUE",
getopts::HasArg::Maybe,
getopts::Occur::Optional);

opts.optflag(
"", "debug-iters",
"Print debug info for the resolution iterations.");
Expand All @@ -275,7 +279,7 @@ fn make_opts() -> getopts::Options
opts.optflag(
"h", "help",
"Display this information.");

opts
}

Expand Down Expand Up @@ -360,7 +364,7 @@ fn parse_command(

command.opts.optimize_instruction_matching &=
!parsed.opt_present("debug-no-optimize-matcher");

if parsed.opt_present("color")
{
command.use_colors = {
Expand All @@ -376,7 +380,7 @@ fn parse_command(
}
};
}

if let Some(t) = parsed.opt_str("t")
{
command.opts.max_iterations = {
Expand Down Expand Up @@ -456,18 +460,18 @@ fn derive_output_filename(

let mut output_filename = std::path::PathBuf::from(input_filename);
output_filename.set_extension(extension);

let output_filename = output_filename
.to_string_lossy()
.into_owned()
.replace("\\", "/");

if output_filename == input_filename
{
report.error("cannot derive safe output filename");
return Err(());
}

Ok(output_filename)
}

Expand All @@ -491,7 +495,7 @@ pub fn parse_output_format(
.collect::<Vec<_>>();

let param_id = param_split[0];

if param_split.len() == 1
{
params.insert(param_id.to_string(), "".to_string());
Expand Down Expand Up @@ -542,7 +546,7 @@ pub fn parse_output_format(
format_id,
param_id,
value));

Err(())
}
}
Expand All @@ -568,6 +572,16 @@ pub fn parse_output_format(
group: get_arg_usize("group", 2, check_nonzero)?,
},

"tcgame" => OutputFormat::TCGame {
base: get_arg_usize("base", 16, check_valid_base)?,
group: get_arg_usize("group", 2, check_nonzero)?,
},

"tcgamebin" => OutputFormat::TCGame {
base: 2,
group: 8,
},

"annotatedhex" => OutputFormat::Annotated {
base: 16,
group: 2,
Expand All @@ -583,7 +597,7 @@ pub fn parse_output_format(

"bindump" => OutputFormat::BinDump,
"hexdump" => OutputFormat::HexDump,

"mif" => OutputFormat::Mif,
"intelhex" => OutputFormat::IntelHex,

Expand All @@ -600,7 +614,7 @@ pub fn parse_output_format(
"logisim16" => OutputFormat::LogiSim16,

"addrspan" => OutputFormat::AddressSpan,

"symbols" => OutputFormat::Symbols,
"mesen-mlb" => OutputFormat::SymbolsMesenMlb,

Expand Down Expand Up @@ -682,16 +696,16 @@ fn parse_define_arg(
None,
diagn::Span::new_dummy(),
if has_negative_sign { split[1].get(1..).unwrap() } else { split[1] });


use std::ops::Neg;

match maybe_value
{
Ok(value) =>
expr::Value::make_integer(
if has_negative_sign { value.neg() } else { value }),

Err(()) =>
{
report.error(
Expand Down Expand Up @@ -726,10 +740,13 @@ pub fn format_output(
{
OutputFormat::Binary =>
return output.format_binary(),

OutputFormat::Annotated { base, group } =>
output.format_annotated(fileserver, base, group),


OutputFormat::TCGame { base, group } =>
output.format_tcgame(fileserver, base, group),

OutputFormat::BinStr => output.format_binstr(),
OutputFormat::HexStr => output.format_hexstr(),

Expand All @@ -750,9 +767,9 @@ pub fn format_output(

OutputFormat::LogiSim8 => output.format_logisim(8),
OutputFormat::LogiSim16 => output.format_logisim(16),

OutputFormat::AddressSpan => output.format_addrspan(fileserver),

OutputFormat::Symbols => decls.symbols.format_default(decls, defs),
OutputFormat::SymbolsMesenMlb => decls.symbols.format_mesen_mlb(decls, defs),
}
Expand Down
14 changes: 12 additions & 2 deletions src/usage_help.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,21 @@ Examples:

* `annotated,base:16,group:2`
Annotates the output data with snippets
of the source code.

of the source code. Supports base 2 and 16.
* `annotatedbin`
Same as: `annotated,base:2,group:8`


* `tcgame,base:16,group:2`
Annotates the output data with snippets
of the source code in a format compatible
with the assembly editor for the game
'Turing Complete'. Supports base 2 and 16.
Comments out annotations with '#' and prefixes
each group with 0x or 0b.
* `tcgamebin`
"Same as: `tcgame,base2,group8`"

* `binstr`
* `hexstr`
* `bindump`
Expand Down
Loading

0 comments on commit 54a8523

Please sign in to comment.