Skip to content

Commit

Permalink
add mif output format
Browse files Browse the repository at this point in the history
  • Loading branch information
hlorenzi committed Jun 23, 2019
1 parent 16f9d14 commit b6207d5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
22 changes: 10 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This can be useful if you'd like to test out a new virtual machine's bytecode,
or even if you're eager to write programs for that new processor architecture
you just implemented in FPGA!

[📱 Try it right now on your browser!](https://hlorenzi.github.io/customasm/web/)
[📱 Try it right now in your browser!](https://hlorenzi.github.io/customasm/web/)

[🎁 Check out the Releases section](https://github.com/hlorenzi/customasm/releases)
for pre-built binaries.
Expand All @@ -15,7 +15,7 @@ on how to use the main features!

[📋 Check out the documentation](/doc/index.md) for more in-depth instructions.

🕹 Also, [check out an example project](/examples/nes/) which targets the NES!
[🕹 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`.
Expand All @@ -27,9 +27,7 @@ Usage: customasm [options] <asm-file-1> ... <asm-file-N>
Options:
-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
given <asm-files>.
binary, binstr, hexstr, bindump, hexdump, mif
-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.
Expand Down Expand Up @@ -73,11 +71,11 @@ multiply3x4:
...the assembler would use the `#cpudef` rules to convert the instructions into binary code:

```
0x0100: 11 00
0x0102: 12 03
0x0104: 13 04
0x0106: 21
0x0107: 33 01
0x0109: 40 01 06
0x010c: 50
0x0100: 11 00 ; load r1, 0
0x0102: 12 03 ; load r2, 3
0x0104: 13 04 ; load r3, 4
0x0106: 21 ; add r1, r2
0x0107: 33 01 ; sub r3, 1
0x0109: 40 01 06 ; jnz .loop
0x010c: 50 ; ret
```
36 changes: 36 additions & 0 deletions src/asm/binary_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,40 @@ impl BinaryOutput

result
}


pub fn generate_mif(&self, start_bit: usize, end_bit: usize) -> String
{
let mut result = String::new();

let byte_num = (end_bit - start_bit) / 8 + if (end_bit - start_bit) % 8 != 0 { 1 } else { 0 };

result.push_str(&format!("DEPTH = {};\n", byte_num));
result.push_str("WIDTH = 8;\n");
result.push_str("ADDRESS_RADIX = HEX;\n");
result.push_str("DATA_RADIX = HEX;\n");
result.push_str("\n");
result.push_str("CONTENT\n");
result.push_str("BEGIN\n");

let addr_max_width = format!("{:x}", byte_num - 1).len();

let mut index = start_bit;
while index < end_bit
{
let mut byte: u8 = 0;
for _ in 0..8
{
byte <<= 1;
byte |= if self.read(index) { 1 } else { 0 };
index += 1;
}

result.push_str(&format!("{:1$X}: ", index / 8, addr_max_width));
result.push_str(&format!("{:02X};\n", byte));
}

result.push_str("END;");
result
}
}
11 changes: 7 additions & 4 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ enum OutputFormat
BinStr,
HexStr,
BinDump,
HexDump
HexDump,
Mif,
}


Expand Down Expand Up @@ -67,6 +68,7 @@ fn drive_inner(report: RcReport, opts: &getopts::Options, args: &Vec<String>, fi
Some("hexstr") => OutputFormat::HexStr,
Some("hexdump") => OutputFormat::HexDump,
Some("binary") => OutputFormat::Binary,
Some("mif") => OutputFormat::Mif,

None => if out_stdout
{ OutputFormat::HexDump }
Expand Down Expand Up @@ -106,11 +108,12 @@ fn drive_inner(report: RcReport, opts: &getopts::Options, args: &Vec<String>, fi

let output_data = match out_format
{
OutputFormat::Binary => assembled.generate_binary (0, assembled.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())
OutputFormat::Mif => assembled.generate_mif (0, assembled.len()).bytes().collect::<Vec<u8>>(),
};

if out_stdout
Expand Down Expand Up @@ -139,8 +142,8 @@ fn drive_inner(report: RcReport, opts: &getopts::Options, args: &Vec<String>, fi
fn make_opts() -> getopts::Options
{
let mut opts = getopts::Options::new();
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 given <asm-files>.", "FILE");
opts.optopt("f", "format", "The format of the output file. Possible formats: binary, binstr, hexstr, bindump, hexdump, mif", "FORMAT");
opts.optmulti("i", "include", "Specifies an additional file for processing before the given <asm-files>. [deprecated]", "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.");
Expand Down
5 changes: 3 additions & 2 deletions src/webasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ pub unsafe extern fn wasm_assemble(format: u32, src: *mut String) -> *mut String
{
0 => Ok(output.generate_hexdump(0, output.len())),
1 => Ok(output.generate_bindump(0, output.len())),
2 => Ok(output.generate_hexstr(0, output.len())),
3 => Ok(output.generate_binstr(0, output.len())),
2 => Ok(output.generate_hexstr (0, output.len())),
3 => Ok(output.generate_binstr (0, output.len())),
4 => Ok(output.generate_mif (0, output.len())),
_ => unreachable!()
}
};
Expand Down
1 change: 1 addition & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<option>Bin Dump</option>
<option>Hex String</option>
<option>Bin String</option>
<option>MIF</option>
</select>
<button id="buttonAssemble" onclick="assemble()" disabled>Assemble (Ctrl+Enter) &gt;&gt;</button>
<span id="spanVersion"></span>
Expand Down

0 comments on commit b6207d5

Please sign in to comment.