前言
小时候记得excel里有一个神秘的赛车游戏,发现excel原来并不简单啊;
之前我哥跟我讲excel可能是最牛逼的编程软件,但是当时我没信,现在我有点信了;
工作后发现很多代码原来并不需要写,通过excel、xml直接生成还是爽爽的;
最好的是,自带的编译器自动补全自动大小写转换自动联想比平时用的IDE舒服多了;
所以说啊,学一学VBA还是挺有意思的,你也不知道啥时候就用得上;
作为我写的第一个excel vba demo,必须好好记录下。
目标
通过excel添加信号,并生成Verilog module代码,每个sheet作为一个模块,最终的实现的效果如下:
1.点击添加信号;
2.输入信号名称;
3.输入信号位宽,默认为1;
4.输入信号类型,默认wires;
5.输入是否接口,默认no;
6.完成一个信号输入后,可以继续添加;单一信号输入途中选择取消,则推出输入;
7.点击“生成代码”,直接生成module rtl,除了对齐失败以外,可以说非常完美;
实现
列举一下用到的sub/function;
信号输入
Option Explicit
Dim sigName As String
Dim sigWidh As String
Dim sigType As String
Dim sigPort As String
Public Type sigInfo
name As String
widh As Integer
type As String
port As String
End Type
全局定义,由于开始没考虑清楚,有重复定义之嫌;全局变量用来输入和获取信号信息;
Private Function gainLastRow() As Integer
Dim lastRow As Integer
lastRow = ActiveSheet.UsedRange.Rows.Count
gainLastRow = ActiveSheet.UsedRange.Rows(lastRow).Row + 1
'Debug.Print gainLastRow
End Function
获取当前sheet最后一行位置,之后添加信号时默认向最后添加;
Private Function gainSigIn() As Boolean
sigName = InputBox(prompt:="信号名称")
If sigName = "" Then
gainSigIn = False
Exit Function
End If
sigWidh = InputBox(prompt:="信号位宽", Default:="1")
If sigWidh = "" Then
gainSigIn = False
Exit Function
End If
sigType = InputBox(prompt:="信号类型reg/wire??", Default:="wire")
If sigType = "" Then
gainSigIn = False
Exit Function
End If
sigPort = InputBox(prompt:="接口属性no/input/output/inout??", Default:="no")
If sigPort = "" Then
gainSigIn = False
Exit Function
End If
gainSigIn = True
End Function
通过inputbox函数获取键入的信号信息;
Public Sub mainAddSig()
Dim addRow As Integer
Do
If gainSigIn() = True Then
'Debug.Print "@@@@@@@"
addRow = gainLastRow()
With ActiveSheet
.Cells(addRow, 1).Value = sigName
.Cells(addRow, 2).Value = sigWidh
.Cells(addRow, 3).Value = sigType
.Cells(addRow, 4).Value = sigPort
End With
Else
Exit Sub
End If
Loop Until MsgBox("是否继续输入", vbOKCancel) <> vbOK
End Sub
“添加信号”按钮绑定sub,将获取到的信号信息写入单元格最后一行后面,写入完成后询问是否继续输入;
生成代码
Private Function gainOrgLoc() As Integer
Dim keyCell As Range
Cells(1, 1).Select
'Set keyCell = ActiveSheet.UsedRange.Find(What:="???????", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)
Set keyCell = Cells.Find(What:="???????", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)
If keyCell Is Nothing Then
gainOrgLoc = -1
Else
gainOrgLoc = keyCell.Row
'Debug.Print gainOrgLoc
End If
End Function
以左上角(1,1)坐标为起始,寻找标志“信号名称”,其下一行到最后一行为信号定义区间;
Private Function gainSigInfo(setRow As Integer) As sigInfo
With ActiveSheet
gainSigInfo.name = .Cells(setRow, 1)
gainSigInfo.widh = .Cells(setRow, 2)
gainSigInfo.type = .Cells(setRow, 3)
gainSigInfo.port = .Cells(setRow, 4)
End With
End Function
获取某一行信号信息;
Public Sub genRtlCode()
Dim inputArray As New Collection
Dim outputArray As New Collection
Dim inoutArray As New Collection
Dim wireArray As New Collection
Dim regArray As New Collection
Dim startRow As Integer
Dim sig As sigInfo
Dim ss As New Collection
Dim i As Integer
Dim s As String
startRow = gainOrgLoc() + 1
For i = startRow To gainLastRow
sig = gainSigInfo(i)
If sig.name <> "" Then
If sig.port = "input" Then
inputArray.Add (" input " & sig.type & " [" & sig.widh & " -1:0] " & sig.name & ";")
'Debug.Print " input " & sig.type & " [" & sig.widh & " -1:0] " & sig.name & ";"
ElseIf sig.port = "output" Then
outputArray.Add (" output " & sig.type & " [" & sig.widh & " -1:0] " & sig.name & ";")
'Debug.Print " output " & sig.type & " [" & sig.widh & " -1:0] " & sig.name & ";"
ElseIf sig.port = "inout" Then
inoutArray.Add (" inout " & sig.type & " [" & sig.widh & " -1:0] " & sig.name & ";")
ElseIf sig.port = "no" And sig.type = "wire" Then
wireArray.Add (" wire " & " [" & sig.widh & " -1:0] " & sig.name & ";")
'Debug.Print " wire " & " [" & sig.widh & " -1:0] " & sig.name & ";"
ElseIf sig.port = "no" And sig.type = "reg" Then
regArray.Add (" reg " & " [" & sig.widh & " -1:0] " & sig.name & ";")
'Debug.Print " reg " & " [" & sig.widh & " -1:0] " & sig.name & ";"
End If
End If
Next
Open "C:\Users\gaoji\Desktop\RISC_SPM\gen.sv" For Output As #1
Print #1, "module " & ActiveSheet.name & "("
For i = 1 To inputArray.Count
s = inputArray.Item(i)
Print #1, s
Next
For i = 1 To outputArray.Count
s = outputArray.Item(i)
Print #1, s
Next
For i = 1 To inoutArray.Count
s = inoutArray.Item(i)
Print #1, s
Next
Print #1, ");"
For i = 1 To regArray.Count
s = regArray.Item(i)
Print #1, s
Next
For i = 1 To wireArray.Count
s = wireArray.Item(i)
Print #1, s
Next
Print #1, "endmodule"
Close #1
End Sub
从起始行到最终行,获取每一行的信号信息并根据属性组织代码放入不同的collection中;
之后将组织好的代码写入文件中。