@@ -213,19 +213,33 @@ def symbols( self ):
213213
214214 def columnWidth ( self ):
215215 """Return the width of the column in boxes."""
216- charWidth = .33
216+ offsetCharWidth = .26
217+ nameCharWidth = .33
217218 xlen_symbol = sympy .symbols ('XLEN' )
219+ lengthCharWidth = .22
218220 try :
219221 # Pretend XLEN=32. This makes 32-bit registers with just a single
220222 # field be wide, while registers with XLEN-32 width be narrow.
221223 length = int (self .length ().subs (xlen_symbol , 32 ))
222224 except TypeError :
223225 length = 20
226+ if self .length () == 1 :
227+ bitText = f"{ self .lowBit } "
228+ else :
229+ # There is no dash in what we're displaying, but we want some space
230+ # for separation between the two values.
231+ bitText = f"{ self .highBit } - { self .lowBit } "
224232 width = math .ceil (max (
233+ # Minimum length
225234 1 ,
235+ # Make fields with more bits take up more space visually
226236 1 + math .log (max (length , 0.1 ), 1.7 ),
227- len ( str ( self .length () )) * charWidth ,
228- len ( self .name ) * charWidth ))
237+ # Wide enough to accommodate the string showing the field length
238+ len ( str ( self .length () )) * lengthCharWidth ,
239+ # Wide enough to accommodate the high/low bit strings
240+ len ( bitText ) * offsetCharWidth ,
241+ # Wide enough to accommodate the name of the field
242+ len ( self .name ) * nameCharWidth ))
229243 # Make longer widths odd, so we can center the bit.
230244 if width > 1 and width % 2 == 0 :
231245 width += 1
@@ -879,14 +893,26 @@ def write_bytefield_row( fd, fields ):
879893 else :
880894 headers .append (f .lowBit )
881895 else :
882- assert columnWidth >= 2
883- headers .append (f .lowBit )
884- headers += ["" ] * (columnWidth - 2 )
885- headers .append (f .highBit )
896+ # If low/high bit need more than 2 characters, place them one in
897+ # from the end so they don't overflow into the neighboring field.
898+ # Really this should be done with right/left alignment, but
899+ # bytefield doesn't seem to support that.
900+ if len (f .lowBit ) > 2 :
901+ start = ["" , f .lowBit ]
902+ else :
903+ start = [f .lowBit ]
904+ if len (f .highBit ) > 2 :
905+ end = [f .highBit , "" ]
906+ else :
907+ end = [f .highBit ]
908+ assert columnWidth >= len (start ) + len (end )
909+ headers += start
910+ headers += ["" ] * (columnWidth - len (start ) - len (end ))
911+ headers += end
886912 columnOffset += columnWidth
887913 # remove whitespace to save space
888914 headers = [h .replace (" " , "" ) for h in headers ]
889- fd .write ('(draw-column-headers {:font-size 15 :height 16 :labels [%s]})\n ' % " " .join (f'"{ h } "' for h in reversed (headers )))
915+ fd .write ('(draw-column-headers {:font-size 15 :height 17 :labels [%s]})\n ' % " " .join (f'"{ h } "' for h in reversed (headers )))
890916
891917 for f in fields :
892918 fd .write ('(draw-box (text "%s" {:font-size 20}) {:span %s})\n ' % ( f .name , f .columnWidth () ))
0 commit comments