Skip to content

Instantly share code, notes, and snippets.

@GaryLee
Created August 31, 2024 10:07
Show Gist options
  • Save GaryLee/6f215712ee76393a435af951d3f0c0d4 to your computer and use it in GitHub Desktop.
Save GaryLee/6f215712ee76393a435af951d3f0c0d4 to your computer and use it in GitHub Desktop.
A example to generate register definition with drawsvg module.
#!python
# -*- coding: utf-8 -*-
import drawsvg as draw
# Reference: https://github.com/cduck/drawsvg/blob/master/docs/index.md
reg = {
'31': 'EN',
'30:29': 'TYPE',
'28:24': 'SCALE',
'23:16': 'OPER',
'15:8': 'SRC',
'7:0': 'DST',
}
def make_reg():
gap = 2
sep_height = 5
height = 50
cell_width = 25
total_width = 32 * cell_width
tick_min_y = 10
d = draw.Drawing(total_width, height, origin=(0, 0))
# Draw a rectangle
r = draw.Rectangle(gap, gap, 800-gap*2, height-gap*2, stroke='black', fill='white', stroke_width=2)
d.append(r)
for i in range(32):
d.append(draw.Line(i*cell_width, 0+gap, i*cell_width, sep_height+gap, stroke='black'))
d.append(draw.Line(i*cell_width, (height-sep_height)-gap, i*cell_width, height-gap, stroke='black'))
d.append(draw.Text(str(31-i), font_size=10, x=i * cell_width + cell_width/2, y=max(tick_min_y, sep_height+gap), fill='gray', center=True))
for field, name in reg.items():
if ':' not in field:
start, end = field, field
else:
start, end = field.split(':')
start = int(start)
end = int(end)
d.append(draw.Text(name, 14, x=(31 - (start + end - 1) / 2) * 25, y=(height/2)+gap, fill='black', center=True))
d.append(draw.Line((31-start)*25, 0+gap, (31-start)*25, height - gap, stroke='black'))
d.save_svg('example.svg')
d.save_png('example.png')
if __name__ == '__main__':
make_reg()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment