Skip to content

Commit

Permalink
fix #12: add intel hex output format
Browse files Browse the repository at this point in the history
  • Loading branch information
hlorenzi committed Jun 23, 2019
1 parent b6207d5 commit 58f4f37
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 19 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ 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, mif
binary, binstr, hexstr, bindump, hexdump,
mif, intelhex
-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
45 changes: 45 additions & 0 deletions src/asm/binary_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,49 @@ impl BinaryOutput
result.push_str("END;");
result
}


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

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

let mut index = start_bit;
while index < end_bit
{
let bytes_in_row = if bytes_left > 32 { 32 } else { bytes_left };

result.push(':');
result.push_str(&format!("{:02X}", bytes_in_row));
result.push_str(&format!("{:04X}", index / 8));
result.push_str("00");

let mut checksum = 0_u8;
checksum = checksum.wrapping_add(bytes_in_row as u8);
checksum = checksum.wrapping_add(((index / 8) >> 8) as u8);
checksum = checksum.wrapping_add((index / 8) as u8);

for _ in 0..bytes_in_row
{
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!("{:02X}", byte));
checksum = checksum.wrapping_add(byte);
}

bytes_left -= bytes_in_row;
result.push_str(&format!("{:02X}", (!checksum).wrapping_add(1)));
result.push('\n');
}

result.push_str(":00000001FF");
result
}
}
29 changes: 16 additions & 13 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum OutputFormat
BinDump,
HexDump,
Mif,
IntelHex,
}


Expand Down Expand Up @@ -63,12 +64,13 @@ fn drive_inner(report: RcReport, opts: &getopts::Options, args: &Vec<String>, fi

let out_format = match matches.opt_str("f").as_ref().map(|s| s.as_ref())
{
Some("binstr") => OutputFormat::BinStr,
Some("bindump") => OutputFormat::BinDump,
Some("hexstr") => OutputFormat::HexStr,
Some("hexdump") => OutputFormat::HexDump,
Some("binary") => OutputFormat::Binary,
Some("mif") => OutputFormat::Mif,
Some("binstr") => OutputFormat::BinStr,
Some("bindump") => OutputFormat::BinDump,
Some("hexstr") => OutputFormat::HexStr,
Some("hexdump") => OutputFormat::HexDump,
Some("binary") => OutputFormat::Binary,
Some("mif") => OutputFormat::Mif,
Some("intelhex") => OutputFormat::IntelHex,

None => if out_stdout
{ OutputFormat::HexDump }
Expand Down Expand Up @@ -108,12 +110,13 @@ 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::Mif => assembled.generate_mif (0, assembled.len()).bytes().collect::<Vec<u8>>(),
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::Mif => assembled.generate_mif (0, assembled.len()).bytes().collect::<Vec<u8>>(),
OutputFormat::IntelHex => assembled.generate_intelhex(0, assembled.len()).bytes().collect::<Vec<u8>>(),
};

if out_stdout
Expand Down Expand Up @@ -142,7 +145,7 @@ 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, mif", "FORMAT");
opts.optopt("f", "format", "The format of the output file. Possible formats: binary, binstr, hexstr, bindump, hexdump, mif, intelhex", "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.");
Expand Down
11 changes: 6 additions & 5 deletions src/webasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ pub unsafe extern fn wasm_assemble(format: u32, src: *mut String) -> *mut String
let output = asm.get_binary_output();
match format
{
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())),
4 => Ok(output.generate_mif (0, output.len())),
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())),
4 => Ok(output.generate_mif (0, output.len())),
5 => Ok(output.generate_intelhex(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 @@ -95,6 +95,7 @@
<option>Hex String</option>
<option>Bin String</option>
<option>MIF</option>
<option>Intel HEX</option>
</select>
<button id="buttonAssemble" onclick="assemble()" disabled>Assemble (Ctrl+Enter) &gt;&gt;</button>
<span id="spanVersion"></span>
Expand Down

0 comments on commit 58f4f37

Please sign in to comment.