Skip to content

Commit

Permalink
Merge pull request #970 from riscv/diagram_spacing
Browse files Browse the repository at this point in the history
Fix text overlapping in offsets in register diagrams.
  • Loading branch information
wmat authored Feb 14, 2024
2 parents 66b857c + e6f4a36 commit b079fc1
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions registers.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,33 @@ def symbols( self ):

def columnWidth( self ):
"""Return the width of the column in boxes."""
charWidth = .33
offsetCharWidth = .26
nameCharWidth = .33
xlen_symbol = sympy.symbols('XLEN')
lengthCharWidth = .22
try:
# Pretend XLEN=32. This makes 32-bit registers with just a single
# field be wide, while registers with XLEN-32 width be narrow.
length = int(self.length().subs(xlen_symbol, 32))
except TypeError:
length = 20
if self.length() == 1:
bitText = f"{self.lowBit}"
else:
# There is no dash in what we're displaying, but we want some space
# for separation between the two values.
bitText = f"{self.highBit} - {self.lowBit}"
width = math.ceil(max(
# Minimum length
1,
# Make fields with more bits take up more space visually
1 + math.log(max(length, 0.1), 1.7),
len( str( self.length() )) * charWidth,
len( self.name ) * charWidth))
# Wide enough to accommodate the string showing the field length
len( str( self.length() )) * lengthCharWidth,
# Wide enough to accommodate the high/low bit strings
len( bitText ) * offsetCharWidth,
# Wide enough to accommodate the name of the field
len( self.name ) * nameCharWidth))
# Make longer widths odd, so we can center the bit.
if width > 1 and width % 2 == 0:
width += 1
Expand Down Expand Up @@ -879,14 +893,26 @@ def write_bytefield_row( fd, fields ):
else:
headers.append(f.lowBit)
else:
assert columnWidth >= 2
headers.append(f.lowBit)
headers += [""] * (columnWidth - 2)
headers.append(f.highBit)
# If low/high bit need more than 2 characters, place them one in
# from the end so they don't overflow into the neighboring field.
# Really this should be done with right/left alignment, but
# bytefield doesn't seem to support that.
if len(f.lowBit) > 2:
start = ["", f.lowBit]
else:
start = [f.lowBit]
if len(f.highBit) > 2:
end = [f.highBit, ""]
else:
end = [f.highBit]
assert columnWidth >= len(start) + len(end)
headers += start
headers += [""] * (columnWidth - len(start) - len(end))
headers += end
columnOffset += columnWidth
# remove whitespace to save space
headers = [h.replace(" ", "") for h in headers]
fd.write('(draw-column-headers {:font-size 15 :height 16 :labels [%s]})\n' % " ".join(f'"{h}"' for h in reversed(headers)))
fd.write('(draw-column-headers {:font-size 15 :height 17 :labels [%s]})\n' % " ".join(f'"{h}"' for h in reversed(headers)))

for f in fields:
fd.write('(draw-box (text "%s" {:font-size 20}) {:span %s})\n' % ( f.name, f.columnWidth() ))
Expand Down

0 comments on commit b079fc1

Please sign in to comment.