From bca56f1fc2dcf3e7badce3d433082300f339b745 Mon Sep 17 00:00:00 2001 From: Catherine Date: Thu, 28 Mar 2024 06:13:35 +0000 Subject: [PATCH] fmt,cxxrtl: add support for uppercase hex format. This is necessary for translating Python format strings in Amaranth. --- backends/cxxrtl/runtime/cxxrtl/cxxrtl.h | 3 ++- kernel/fmt.cc | 11 ++++++++--- kernel/fmt.h | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/backends/cxxrtl/runtime/cxxrtl/cxxrtl.h b/backends/cxxrtl/runtime/cxxrtl/cxxrtl.h index b834cd12019..5bd6a00681b 100644 --- a/backends/cxxrtl/runtime/cxxrtl/cxxrtl.h +++ b/backends/cxxrtl/runtime/cxxrtl/cxxrtl.h @@ -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; @@ -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) { diff --git a/kernel/fmt.cc b/kernel/fmt.cc index 18eb7cb7193..4d4aecf5a81 100644 --- a/kernel/fmt.cc +++ b/kernel/fmt.cc @@ -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') { @@ -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) @@ -507,7 +511,7 @@ std::vector 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; @@ -617,6 +621,7 @@ void Fmt::emit_cxxrtl(std::ostream &os, std::string indent, std::function