Skip to content

Commit

Permalink
fmt,cxxrtl: add support for uppercase hex format.
Browse files Browse the repository at this point in the history
This is necessary for translating Python format strings in Amaranth.
  • Loading branch information
whitequark committed Mar 28, 2024
1 parent d73f71e commit bca56f1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion backends/cxxrtl/runtime/cxxrtl/cxxrtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,7 @@ struct fmt_part {
unsigned base; // = 10;
bool signed_; // = false;
bool plus; // = false;
bool hex_upper; // = false;

// VLOG_TIME type
bool realtime; // = false;
Expand Down Expand Up @@ -1085,7 +1086,7 @@ struct fmt_part {
uint8_t value = val.bit(index) | (val.bit(index + 1) << 1) | (val.bit(index + 2) << 2);
if (step == 4)
value |= val.bit(index + 3) << 3;
buf += "0123456789abcdef"[value];
buf += (hex_upper ? "0123456789ABCDEF" : "0123456789abcdef")[value];
}
std::reverse(buf.begin(), buf.end());
} else if (base == 10) {
Expand Down
11 changes: 8 additions & 3 deletions kernel/fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ void Fmt::parse_rtlil(const RTLIL::Cell *cell) {
} else if (fmt[i] == 'h') {
part.type = FmtPart::INTEGER;
part.base = 16;
} else if (fmt[i] == 'H') {
part.type = FmtPart::INTEGER;
part.base = 16;
part.hex_upper = true;
} else if (fmt[i] == 'c') {
part.type = FmtPart::CHARACTER;
} else if (fmt[i] == 't') {
Expand Down Expand Up @@ -197,7 +201,7 @@ void Fmt::emit_rtlil(RTLIL::Cell *cell) const {
case 2: fmt += 'b'; break;
case 8: fmt += 'o'; break;
case 10: fmt += 'd'; break;
case 16: fmt += 'h'; break;
case 16: fmt += part.hex_upper ? 'H' : 'h'; break;
default: log_abort();
}
if (part.plus)
Expand Down Expand Up @@ -507,7 +511,7 @@ std::vector<VerilogFmtArg> Fmt::emit_verilog() const
case 2: fmt.str += 'b'; break;
case 8: fmt.str += 'o'; break;
case 10: fmt.str += 'd'; break;
case 16: fmt.str += 'h'; break;
case 16: fmt.str += 'h'; break; // no uppercase hex
default: log_abort();
}
break;
Expand Down Expand Up @@ -617,6 +621,7 @@ void Fmt::emit_cxxrtl(std::ostream &os, std::string indent, std::function<void(c
os << part.base << ", ";
os << part.signed_ << ", ";
os << part.plus << ", ";
os << part.hex_upper << ", ";
os << part.realtime;
os << " }.render(";
emit_sig(part.sig);
Expand Down Expand Up @@ -676,7 +681,7 @@ std::string Fmt::render() const
else if (has_z)
buf += 'Z';
else
buf += "0123456789abcdef"[subvalue.as_int()];
buf += (part.hex_upper ? "0123456789ABCDEF" : "0123456789abcdef")[subvalue.as_int()];
}
std::reverse(buf.begin(), buf.end());
} else if (part.base == 10) {
Expand Down
1 change: 1 addition & 0 deletions kernel/fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct FmtPart {
unsigned base = 10;
bool signed_ = false;
bool plus = false;
bool hex_upper = false;

// VLOG_TIME type
bool realtime = false;
Expand Down

0 comments on commit bca56f1

Please sign in to comment.