首先导入需要用到的库
import openpyxl import re
然后将需要处理的excel表格以“读”的形式存在一个变量
file_name = r"test.xlsx"
然后创建一个文件名,这个文件是用来输出打印信息,生成需要的代码
file_o = open("test.sv", "w")
使用load_workbook将excel表格存入workbook变量
workbook = openpyxl.load_workbook(file_name)
使用get_sheet_by_name函数将需要处理的sheet存入sheet变量
sheet = workbook.get_sheet_by_name('Sheet1')
获取到行数rows和列数cols
rows = sheet.max_row cols = sheet.max_column
使用cell(row=i, column=j).value获取对应表格的内容
ball_name = sheet.cell(row=4, column=5).value
如果其中有需要替换内容中的某些字段,可以使用re库的sub函数,示例:将0x替换为32‘h,方便systemverilog识别。
ball_name = re.sub("0x", "32'h", ball_name)
先写这么多了,以后用到更多功能后再补充。谢谢阅读。
贴上全部代码:
file_o = open("test.sv", "w")
file_name = "test.xlsx"
workbook = openpyxl.load_workbook(file_name)
sheet = workbook.get_sheet_by_name('Sheet1')
rows = sheet.max_row
cols = sheet.max_column
print("bit [31:0] addr;", file=file_o)
print("bit [31:0] data;", file=file_o)
#begin second row in sheet
ball_name = sheet.cell(row=i, column=5).value
func1 = sheet.cell(row=i, column=16).value
func2 = sheet.cell(row=i, column=18).value
func3 = sheet.cell(row=i, column=20).value
func4 = sheet.cell(row=i, column=22).value
reg_offset = sheet.cell(row=i, column=31).value
#replace 0x to 32'h
reg_offset = re.sub("0x", "32'h", reg_offset)
#add postfix _sel behind ball_name
task_name = ball_name + "_sel"
print("task " + task_name + "(bit [1:0] func);", file=file_o)
print("tbegin", file=file_o)
print("ttgpio_addr = `GPIO_BASE_ADDR + "+reg_offset+";", file=file_o)
print("ttcase(func)", file=file_o)
print("ttt2'b00: data = ;", file=file_o)
print("ttt2'b01: data = ;", file=file_o)
print("ttt2'b10: data = ;", file=file_o)
print("ttt2'b11: data = ;", file=file_o)
print("tttdefault: data = 0", file=file_o)
print("ttendcase", file=file_o)
print("ttp_m0.write(addr, data);", file=file_o)
print("tt`uvm_info(get_type_name(), $sformatf("addr = 32'h%0h, data = 32'h%0h", addr, data), UVM_LOW)", file=file_o)
print("tend", file=file_o)
print("endtask:"+task_name, file=file_o)
print("", file=file_o)



