From 3939fecb763ff85502aa7ed8ce63e20570d3116d Mon Sep 17 00:00:00 2001 From: Aaron Linnell Date: Fri, 1 Apr 2022 11:37:56 -0500 Subject: [PATCH 1/2] Option to select address unit, only affects -f intelhex --- src/driver.rs | 18 +++++++++++++++++- src/util/bitvec_format.rs | 6 +++--- src/webasm/mod.rs | 2 +- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/driver.rs b/src/driver.rs index a0142e46..c8d5265c 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -128,6 +128,21 @@ fn drive_inner( } }; + let out_addrunit = match matches.opt_str("addr-unit").as_ref().map(|s| s.as_ref()) + { + None | + Some("byte") => 8, + Some("word") => 16, + Some("dword") => 32, + Some(_) => + { + report.error("invalid output address unit selection, must be byte, word or dword"); + return Err(true); + } + }; + + + if matches.free.len() < 1 { report.error("no input files"); @@ -219,7 +234,7 @@ fn drive_inner( OutputFormat::BinDump => binary.format_bindump () .bytes().collect(), OutputFormat::HexDump => binary.format_hexdump () .bytes().collect(), OutputFormat::Mif => binary.format_mif () .bytes().collect(), - OutputFormat::IntelHex => binary.format_intelhex() .bytes().collect(), + OutputFormat::IntelHex => binary.format_intelhex(out_addrunit).bytes().collect(), OutputFormat::DecComma => binary.format_comma (10).bytes().collect(), OutputFormat::HexComma => binary.format_comma (16).bytes().collect(), OutputFormat::DecC => binary.format_c_array (10).bytes().collect(), @@ -291,6 +306,7 @@ fn make_opts() -> getopts::Options { let mut opts = getopts::Options::new(); opts.optopt("f", "format", "The format of the output file. Possible formats: binary, annotated, annotatedbin, binstr, hexstr, bindump, hexdump, mif, intelhex, deccomma, hexcomma, decc, hexc, logisim8, logisim16, addrspan", "FORMAT"); + opts.optopt("","addr-unit","Address units when outputting intelhex. (byte, word, or dword)"," ADDRESS-UNIT"); opts.opt("o", "output", "The name of the output file.", "FILE", getopts::HasArg::Maybe, getopts::Occur::Optional); opts.optopt("", "symbol-format", "The format of the symbol file. Possible formats: default, mesen-mlb", "SYMBOL-FORMAT"); opts.opt("s", "symbol", "The name of the output symbol file.", "FILE", getopts::HasArg::Maybe, getopts::Occur::Optional); diff --git a/src/util/bitvec_format.rs b/src/util/bitvec_format.rs index 39fb68e5..bbc56ea9 100644 --- a/src/util/bitvec_format.rs +++ b/src/util/bitvec_format.rs @@ -207,8 +207,8 @@ impl util::BitVec } - pub fn format_intelhex(&self) -> String - { + pub fn format_intelhex(&self, addressunit: usize) -> String + { let mut result = String::new(); let mut bytes_left = self.len() / 8 + if self.len() % 8 != 0 { 1 } else { 0 }; @@ -220,7 +220,7 @@ impl util::BitVec result.push(':'); result.push_str(&format!("{:02X}", bytes_in_row)); - result.push_str(&format!("{:04X}", index / 8)); + result.push_str(&format!("{:04X}", index / addressunit)); result.push_str("00"); let mut checksum = 0_u8; diff --git a/src/webasm/mod.rs b/src/webasm/mod.rs index 744b1486..5ceb3888 100644 --- a/src/webasm/mod.rs +++ b/src/webasm/mod.rs @@ -28,7 +28,7 @@ pub unsafe extern fn wasm_assemble(format: u32, src: *mut String) -> *mut String 4 => Ok(binary.format_hexstr ()), 5 => Ok(binary.format_binstr ()), 6 => Ok(binary.format_mif ()), - 7 => Ok(binary.format_intelhex()), + 7 => Ok(binary.format_intelhex(8)), 8 => Ok(binary.format_comma (10)), 9 => Ok(binary.format_comma (16)), 10 => Ok(binary.format_c_array (10)), From ae72f739939e40b863faa0a504119e4a3a771099 Mon Sep 17 00:00:00 2001 From: Aaron Linnell Date: Fri, 1 Apr 2022 18:45:00 -0500 Subject: [PATCH 2/2] Checksum is now calculated correctly when using address units other than byte --- src/util/bitvec_format.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/bitvec_format.rs b/src/util/bitvec_format.rs index bbc56ea9..a6c5d93a 100644 --- a/src/util/bitvec_format.rs +++ b/src/util/bitvec_format.rs @@ -225,8 +225,8 @@ impl util::BitVec 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); + checksum = checksum.wrapping_add(((index / addressunit) >> 8) as u8); + checksum = checksum.wrapping_add((index / addressunit) as u8); for _ in 0..bytes_in_row {