Skip to content

Commit

Permalink
Consts - Also print presets of fields, add function to print presets
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanlippuner committed Jan 10, 2024
1 parent 52629a2 commit 4d2ac55
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions proto/cheby/print_consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 4d2ac55

Please sign in to comment.