Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum OutputFormat
Annotated(util::FormatAnnotatedOptions),
BinStr,
HexStr,
ReadMemB(util::FormatReadMemOptions),
ReadMemH(util::FormatReadMemOptions),
BinDump,
HexDump,
Mif,
Expand Down Expand Up @@ -686,6 +688,13 @@ pub fn parse_output_format(
"binstr" => OutputFormat::BinStr,
"hexstr" => OutputFormat::HexStr,

"readmemb" => OutputFormat::ReadMemB(util::FormatReadMemOptions {
width: get_arg_usize(&mut params, report, "width", 8, check_nonzero)?
}),
"readmemh" => OutputFormat::ReadMemH(util::FormatReadMemOptions {
width: get_arg_usize(&mut params, report, "width", 8, check_nonzero)?
}),

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

Expand Down Expand Up @@ -866,6 +875,9 @@ pub fn format_output(
OutputFormat::BinStr => output.format_binstr(),
OutputFormat::HexStr => output.format_hexstr(),

OutputFormat::ReadMemB(opts) => output.format_readmemb(opts),
OutputFormat::ReadMemH(opts) => output.format_readmemh(opts),

OutputFormat::BinDump => output.format_bindump(),
OutputFormat::HexDump => output.format_hexdump(),

Expand Down
6 changes: 6 additions & 0 deletions src/usage_help.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ Examples:
Uninterrupted string of binary digits.
* `hexstr`
Uninterrupted string of hexadecimal digits.

* `readmemb,width:8`
Verilog readmemb format, width bits per line
* `readmemh,width:8`
Verilog readmemh format, width bits per line

* `bindump`
Memory-dump style encoded as binary digits.
* `hexdump`
Expand Down
79 changes: 79 additions & 0 deletions src/util/bitvec_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub struct FormatAnnotatedOptions
pub display_labels: bool,
}

pub struct FormatReadMemOptions{
pub width: usize
}


fn to_str_radix(mut value: usize, radix: usize) -> String
{
Expand Down Expand Up @@ -86,6 +90,13 @@ impl util::BitVec
self.format_str(4)
}

pub fn format_readmemb(&self, opts: &FormatReadMemOptions) -> String {
self.format_readmemb_with_width(opts.width)
}

pub fn format_readmemh(&self, opts: &FormatReadMemOptions) -> String {
self.format_readmemh_with_width(opts.width)
}

pub fn format_str(&self, bits_per_digit: usize) -> String
{
Expand Down Expand Up @@ -813,4 +824,72 @@ impl util::BitVec

result
}

fn format_readmemb_with_width(&self, width: usize) -> String {
// Format bits for `$readmemb` output: each line is `width` bits.
// Memory elements shorter than `width` will be left-padded by the simulator.
let mut result = String::new();
let mut index: usize = 0;

loop {
let bits_to_read = (self.len() - index).min(width);
if bits_to_read == 0 {
break;
}

for _ in 0..bits_to_read {
result.push(
if self.read_bit(index) {'1'} else{'0'}
);
index += 1;
}

result.push('\n');
}
result
}

fn format_readmemh_with_width(&self, width: usize) -> String {
// Outputs in Verilog readmemh format: MSB-first, grouped per `width` bits.
// Leading bits (<4) form a partial hex digit directly, with no padding or shift.
let mut result = String::new();
let mut index: usize = 0;
let read = |i| self.read_bit(i);

fn num_to_hex(d: u8) -> char {
const HEX: &[u8; 16] = b"0123456789abcdef";
HEX[d as usize] as char
}

fn read_bits<F>(read_bit: F, index: &mut usize, n: usize) -> u8
where
F: Fn(usize) -> bool,
{
let mut v = 0u8;
for _ in 0..n {
v = (v << 1) | read_bit(*index) as u8;
*index += 1;
}
v
}

while index < self.len() {
let bits = (self.len() - index).min(width);
let left = bits % 4;
let mut remaining = bits;

if left > 0 {
let v = read_bits(read, &mut index, left);
result.push(num_to_hex(v));
remaining -= left;
}

for _ in 0..(remaining / 4) {
result.push(num_to_hex(read_bits(read, &mut index, 4)));
}

result.push('\n');
}
result
}
}
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod bitvec_format;
pub use self::bitvec_format::{
FormatListOptions,
FormatAnnotatedOptions,
FormatReadMemOptions,
};

mod overlap_checker;
Expand Down