From 4d2ac55ba53f300fef2a70222bd0f1859b663ab0 Mon Sep 17 00:00:00 2001 From: Stefan Lippuner <3071885+stefanlippuner@users.noreply.github.com> Date: Wed, 10 Jan 2024 14:21:53 +0100 Subject: [PATCH] Consts - Also print presets of fields, add function to print presets --- proto/cheby/print_consts.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/proto/cheby/print_consts.py b/proto/cheby/print_consts.py index a404a88f..cb703196 100644 --- a/proto/cheby/print_consts.py +++ b/proto/cheby/print_consts.py @@ -24,7 +24,7 @@ def pr_hex_const(self, name, val): "Unsized hex constant" pass - def pr_hex_data(self, name, val, reg): + def pr_hex_data(self, name, val, width): "Hex constant using the width of the data" self.pr_hex_const(name, val) @@ -63,7 +63,7 @@ def pr_reg(self, n): return f = n.children[0] if f.c_preset is not None: - self.pr_hex_data(self.pr_name(n) + '_PRESET', f.c_preset, n) + self.pr_preset(self.pr_name(n), f.c_preset, n.width) def pr_field_offset(self, f): self.pr_dec_const(self.pr_name(f) + "_OFFSET", f.lo) @@ -76,12 +76,19 @@ def compute_mask(self, f): return mask << f.lo def pr_field_mask(self, f): - self.pr_hex_data(self.pr_name(f), self.compute_mask(f), f._parent) + self.pr_hex_data(self.pr_name(f), self.compute_mask(f), f._parent.width) def pr_field(self, f): self.pr_field_offset(f) self.pr_field_mask(f) + if f.c_preset is not None: + self.pr_preset(self.pr_name(f), f.c_preset, f.c_rwidth) + + def pr_preset(self, name, preset, width): + name = name + '_PRESET' + self.pr_hex_data(name, preset, width) + def pr_enum(self, name, val, wd): self.pr_hex_const(name, val) @@ -114,8 +121,8 @@ def pr_const(self, name, val): def pr_hex_const(self, name, val): self.pr_const(name, "'h{:x}".format(val)) - def pr_hex_data(self, name, val, reg): - self.pr_const(name, "{}'h{:x}".format(reg.width, val)) + def pr_hex_data(self, name, val, width): + self.pr_const(name, "{}'h{:x}".format(width, val)) def pr_enum(self, name, val, wd): self.pr_const (name, "{}'h{:x}".format(wd, val)) @@ -146,11 +153,18 @@ def pr_const_width(self, name, val, width): def pr_hex_const(self, name, val): self.pr_const(name, "16#{:x}#".format(val)) - def pr_hex_data(self, name, val, reg): - hex_width = round(reg.width / 4) - assert(4*hex_width == reg.width) - hex_val = "{:x}".format(val).zfill(hex_width) - self.pr_const_width(name, "x\"{}\"".format(hex_val), reg.width) + def pr_bin_data(self, name, val, width): + bin_val = "{:b}".format(val).zfill(width) + self.pr_const_width(name, "\"{}\"".format(bin_val), width) + + def pr_hex_data(self, name, val, width): + if width % 4 == 0: + hex_width = round(width / 4) + hex_val = "{:x}".format(val).zfill(hex_width) + self.pr_const_width(name, "x\"{}\"".format(hex_val), width) + else: + # Fall back to a binary constant for improved tool compatibility + self.pr_bin_data(name, val, width) def pr_field_mask(self, f): # Not printed as a mask may overflow a natural. @@ -211,6 +225,9 @@ def pr_field(self, f): self.pr_hex_const(self.pr_name(f) + '_MASK', self.compute_mask(f)) self.pr_dec_const(self.pr_name(f) + "_SHIFT", f.lo) + if f.c_preset is not None: + self.pr_preset(self.pr_name(f), f.c_preset, f.c_rwidth) + class ConstsPrinterPython(ConstsPrinter): def pr_const(self, name, val):