verilog学习 (番外篇)
今天晚上吃饭的时候在想,Quartus好像有个功能是可以可视化生成模块,自己用的是Icarus Verilog小强大的东西却没有这个功能.于是乎我就下自己做个.使用工具Python + PIL.
PIL仅可以用来处理图片.也可以生成和绘制图片.
生成图片代码如下:
img = Image.new("RGB",(640,480))
绘制图片需要先导入包ImageDraw.然后代码如下
draw = ImageDraw.Draw(img)
#xxx 就可以绘制形状了具体的可以看官方文档 http://www.pythonware.com/library/pil/handbook/index.htm
del draw
有了这么多 够了直接贴代码了,中间大部分是用来计算宽度长度的.有好几块是硬编码的.
还有代码中没有使用类来封装设备(device)而是直接使用的 map组织的,还有分析代码的模块还没写,估计等我搞完编译原理再说,还有...... (额 太多了o(︶︿︶)o 我太懒了)
import Image
import ImageDraw
def generate_background(width,height,dot_width=12):
img = Image.new("RGB",(width,height),(255,255,255))
draw = ImageDraw.Draw(img)
draw.rectangle([(10,10),(img.size[0] - 10,img.size[1]-10)],outline=True)
for x in range(0,width-20,dot_width):
for y in range(0,height,dot_width):
draw.point((x+10,y+10),fill=(50,50,50))
del draw
return img
def parse_code(filename):
#simulate first
device = {}
device["pins_in"] = ["iA","iB"]
device["pins_out"] = ["oAnd","oOr","oNot"]
device["name"] = "count"
return device
def generate_device(device) :
pins_in = device["pins_in"]
pins_out = device["pins_out"]
pin_space = 20
head_space = 20
foot_space = 20
side_space = 10
line_len = 20
pins = max(len(pins_in),len(pins_out))
tmpimg = Image.new("RGB",(400,400))
tmpdraw = ImageDraw.Draw(tmpimg)
left_text_width = [tmpdraw.textsize(t)[0] for t in device["pins_in"]]
right_text_width = [tmpdraw.textsize(t)[0] for t in device["pins_out"]]
width = max(left_text_width)+max(right_text_width) + 30 + line_len*2
height = (pins-1)*pin_space+2*side_space+head_space+foot_space
img = Image.new("RGB",(width,height),(255,255,255))
draw = ImageDraw.Draw(img)
draw.rectangle([(line_len,head_space),(width - line_len,height - foot_space)],outline=True)
#left_pins
for i in range(len(pins_in)):
lines = [(0,i*pin_space + head_space + side_space),
(line_len,i*pin_space + head_space + side_space)]
draw.text((line_len+1,i*pin_space + head_space + side_space-2),pins_in[i],fill=(55,66,99))
draw.line(lines,(15,48,64))
#right_pins
for i in range(len(pins_out)):
lines = [(width - line_len , i*pin_space + head_space + side_space),
(width,i*pin_space + head_space + side_space)]
text_width = draw.textsize(pins_out[i])[0]
draw.text((width-line_len - text_width,i*pin_space + head_space + side_space - 4),
pins_out[i],fill=(94,64,66))
draw.line(lines,(15,48,64))
draw.text((1,1),device["name"],fill=(36,4,68))
del draw
return img
def build_all(filename):
device = parse_code(filename)
backimg = generate_background(640,480)
deviceimg = generate_device(device)
backimg.paste(deviceimg,(120,120,120+deviceimg.size[0],120+deviceimg.size[1]))
return backimg
if __name__ == "__main__":
img = build_all("count.v")
img.show()
效果如下
额 还算行,就是字体太丑了点,给出Quartus的效果.如下:
好像对比下来我生成的图片跟丑了::>_<::
好吧.能用就行.
改下输入输出:
嗯多个引脚,少个引脚都还行.
好了 睡觉Zzzzzz.