首页 > 其他分享 >优化数字前端工作流的小脚本

优化数字前端工作流的小脚本

时间:2023-04-24 11:23:54浏览次数:32  
标签:脚本 name 前端 module width verilog input 优化 port

使用python编写了两个优化工作流的小脚本。在前端设计工作时,我的工作流是:初步规划端口(markdown)-> 初步rtl代码设计(verilog)-> 完整rtl代码设计(verilog)-> 输出最终端口(markdown)

所以这里涉及两个可以自动化的步骤,一个是把我用markdown下画的表格使用脚本转化成verilog文件。另一个是把编写好的verilog文件转化回markdown表格。所以我又靠AI(还是new bing)写了这两个脚本:

将表格转换成verilog:

def generate_verilog(file_path):
    with open(file_path, 'r') as f:
        table = f.read()
    lines = table.split('\n')
    ports = []
    for line in lines[3:]:
        if line.strip() == '':
            continue
        port, width, direction, function = [x.strip() for x in line.split("|")[1:-1]]
        port = port.strip()
        width = width.strip()
        direction = direction.strip()
        function = function.strip()
        ports.append((port, width, direction,function))
    verilog = 'module my_module (\n'
    for port, width, direction, function in ports:
        if direction == 'input':
            if int(width) == 1:
                verilog += f'  input {port}, // {function}  \n' 
            else:
                verilog += f'  input [{int(width)-1}:0] {port}, // {function}  \n'
        elif direction == 'output':
            if int(width) == 1:
                verilog += f'  output {port}, // {function}  \n'
            else:
                verilog += f'  output [{int(width)-1}:0] {port}, // {function}  \n'
    verilog = verilog + '\n);\n'
    
    for port, width, direction,function in ports:
        if int(width) == 1:
            verilog += f'  wire {port};\n'
        else:
            verilog += f'  wire [{int(width)-1}:0] {port};\n'
    
    verilog += '\nendmodule'
    return verilog

file_path = input('Enter the file path: ')
verilog_code = generate_verilog(file_path)

output_file_path = input('Enter the output file path: ')
with open(output_file_path, 'w') as f:
    f.write(verilog_code)

例如输入一个表格:

Port Width Direction Function
clk 1 input 时钟
rst_n 1 input 复位
inst_dout 32 input 指令数据
inst_addr 5 output 指令地址

运行脚本后就会自动输出verilog代码:

module my_module (
  input clk, // 时钟  
  input rst_n, // 复位  
  input [31:0] inst_dout, // 指令数据  
  output [4:0] inst_addr, // 指令地址  

);
  wire clk;
  wire rst_n;
  wire [31:0] inst_dout;
  wire [4:0] inst_addr;

endmodule

注意一下最后一个信号把逗号删除就行。

反过来如果要把verilog转换回表格,使用下面这个脚本:

import re

def verilog_to_markdown(input_file, output_file):
    with open(input_file, 'r') as f:
        verilog_str = f.read()

    # Extract the module declaration
    module_declaration = re.search(r'module\s+\w+\s*\(([^)]*)\);', verilog_str, re.DOTALL)
    if not module_declaration:
        return 'Error: No valid module declaration found'
    port_declarations = module_declaration.group(1).strip().split('\n')

    # Create the markdown table
    markdown_table = '| Port | Width | Direction | Function |\n'
    markdown_table += '| :-----------------------: | :---: | :-------: | :----------------------------------: |\n'
    for port_declaration in port_declarations:
        port_declaration = port_declaration.strip()
        if not port_declaration:
            continue
        direction, port_name = port_declaration.split(' ',1)[:2]
        port_name = port_name.rstrip(',')
        width = 1
        if '[' in port_name:
            width_str = re.search(r'\[(\d+):(\d+)\]', port_name).group()
            msb, lsb = map(int, width_str[1:-1].split(':'))
            width = msb - lsb + 1
            port_name = re.sub(r'\[\d+:\d+\]', '', port_name)

        port_name = port_name.split('//')[0]
        port_name = port_name.replace(',', '')

        # Extract the comment after '//'
        comment = ''
        comment_match = re.search(r'//(.*)', port_declaration)
        if comment_match:
            comment = comment_match.group(1).strip()
        
        markdown_table += f'| {port_name:<23} | {width:^5} | {direction:^9} | {comment:<35} |\n'

    with open(output_file, 'w') as f:
        f.write(markdown_table)

input_file = input('Enter the input file name: ')
output_file = input('Enter the output file name: ')
verilog_to_markdown(input_file, output_file)

例如输入一个verilog的端口声明代码:

module my_module (
  input clk, // 时钟  
  input rst_n, // 复位  
  input [31:0] inst_dout, // 指令数据  
  output [4:0] inst_addr // 指令地址  
);

就能获得一个markdown描述的表格:

Port Width Direction Function
clk 1 input 时钟
rst_n 1 input 复位
inst_dout 32 input 指令数据
inst_addr 5 output 指令地址

当然这两个脚本也不是很完善,后面可以再增加一些新的功能。

标签:脚本,name,前端,module,width,verilog,input,优化,port
From: https://www.cnblogs.com/sasasatori/p/17348866.html

相关文章

  • 软件工程:阿姆达尔定律,性能设计和优化的指导原则
    hi,我是熵减,见字如面。在软件开发中,你是否做过性能的优化,譬如:有一个图片处理的程序,其中包含一个函数用于对图片进行滤镜处理。该函数中包含两个部分:一个可并行化的部分和一个串行部分。可并行化的部分用于对图片的每个像素进行计算,而串行部分用于对处理后的图片进行保存操作。......
  • 往maven远程仓库上传jar包脚本
    往maven仓库上传jar分两种情况,一种情况是只上传jar包,另一种情况是jar包和pom文件同时上传1.只上传jar包mvndeploy:deploy-file-Dmaven.test.skip=true-Dfile=[jar包的路径]这个不建议将jar包放在maven本地仓库的文件夹-DgroupId=[依赖的groupId]-DartifactId=[依赖的arti......
  • 【前端可视化】ECharts中国地图+散点图demo
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metahttp-equiv="X-UA-Compatible"content="IE=edge"/><metaname="viewport"content="w......
  • 【开发工具】idea2023.1社区版设置优化,媲美旗舰版
    相信大家很多时候都是用旗舰版来开发,各种序列号破解包各种搞,但说不定哪天又失效了那天又爆泄漏隐私安全问题。随着idea的版本升级破解也不好搞了,所以我就直接用了社区版,经过一番折腾发现社区版一点不比旗舰版差,可能是我用到的功能比较少吧。一、配置全局1.1、配置软件配置1.2......
  • java脚本读取finalshell密码
    在finalshell安装目录下找到coon文件夹,下面有许许多多的json文件,在这些文件中找到password{"forwarding_auto_reconnect":false,"custom_size":false,"delete_time":0,"secret_key_id":"","user_name":"root","remote_port_......
  • Java性能优化之序列化优化
    1、Java序列化及其缺陷Java提供了一种序列化机制,这种机制能够将一个对象序列化为二进制形式(字节数组),用于写入磁盘或输出到网络,同时也能从网络或磁盘中读取字节数组,反序列化成对象,在程序中使用。 JDK提供的两个输入、输出流对象ObjectInputStream和ObjectOutputStream,它......
  • 针对网络浮动版软件的许可证优化,解决许可证不够用的难题
    随着信息技术的发展和应用,越来越多的企业或组织需要使用各种专业或高效的软件来完成各种任务或项目。然而,购买软件许可证往往需要花费大量的资金和时间,并且还要考虑到软件使用者之间的协调和配合。为了解决这些问题,一种叫做网络浮动版软件(NetworkFloatingSoftware)的授权方式应运......
  • 原生ip代理如何帮助跨境网络营销优化 SEO 排名?
    随着全球化的加速和数字化时代的到来,跨境网络营销在过去几年中发展迅速,并成为企业扩大海外市场的重要手段之一。其中提高SEO排名是非常重要的,因为SEO排名可以提高网站在搜索引擎结果页面中的排名,从而吸引更多的访问者和潜在客户。 使用StormProxies提供的ip 代理可以帮......
  • Vue3 +element-plus+ wangEditor 富文本编辑器+前端七牛云上传
    我用的vue3,element-plus,没用ts搭建wangEditor 参考地址 https://www.cnblogs.com/xbxxf/p/16791084.html七牛云安装参考地址 https://blog.csdn.net/ldoit/article/details/121533204本来就是抄大佬的,就不复制粘贴了主要是整理关于七牛云部分的代码,秉着复制就能用的原则,我......
  • web前端三大主流框架对比
    关注我了解更多web前端技术知识,带你一路“狂飙”到底!上岸大厂不是梦!web前端开发框架是在前端工程师中经常会用到的内容,可以大大减少项目中的bug,节约开发成本,加快项目周期。在使用web前端开发框架之前,需要先了解web前端三三大主流框架有哪些。目前web前端三大框架Angular、R......