├── .gitignore ├── FpgaTool.py ├── README.md ├── genHdlInst.py ├── gif ├── FpgaTool.gif └── genHdlInst.gif ├── mainwindow.py ├── mainwindow.ui ├── pic ├── fpga.png ├── gen_format.png ├── gen_inst.png ├── newfile.png ├── openfile.png ├── savefile.png └── text.png ├── syntax_highlight.py └── top.v /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/* 2 | /__pycache__/* 3 | /*/*/*.pyc -------------------------------------------------------------------------------- /FpgaTool.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | ------------------------------------------------- 4 | File Name: FpgaTool 5 | Description : 6 | Author : Rex 7 | date: 2019/9/20 8 | ------------------------------------------------- 9 | Change Activity: 10 | 2019/9/20: 11 | ------------------------------------------------- 12 | """ 13 | __author__ = 'Rex' 14 | 15 | import sys 16 | import os 17 | import time 18 | from PyQt5.QtCore import * 19 | from PyQt5 import QtGui 20 | from PyQt5.QtGui import * 21 | from PyQt5.QtWidgets import * 22 | from mainwindow import Ui_FpgaTool 23 | import syntax_highlight 24 | from genHdlInst import * 25 | 26 | 27 | class FpgaTool(Ui_FpgaTool, QMainWindow): 28 | def __init__(self): 29 | QMainWindow.__init__(self) 30 | Ui_FpgaTool.__init__(self) 31 | self.setupUi(self) 32 | 33 | 34 | 35 | class ToolApp(FpgaTool): 36 | def __init__(self): 37 | FpgaTool.__init__(self) 38 | self.setWindowTitle('FPGA Tool') 39 | self.actionNew_File.triggered.connect(lambda: self.on_newfile('')) 40 | self.actionOpen_File.triggered.connect(self.on_openfile) 41 | self.actionSave_File.triggered.connect(self.on_savefile) 42 | self.actionTiled.triggered.connect(self.on_tiled) 43 | self.actionCascade.triggered.connect(self.on_cascade) 44 | self.actionView_HDL_Inst.triggered.connect(self.on_viewInst) 45 | 46 | self.winCount = 0 47 | 48 | def on_newfile(self, text): 49 | self.winCount += 1 50 | sub = QMdiSubWindow() 51 | editor = QTextEdit() 52 | sub.setWidget(editor) 53 | highlight = syntax_highlight.PythonHighlighter(editor) 54 | editor.setPlainText(text) 55 | editor.setFont(QtGui.QFont("Timers", 14)) 56 | self.mdi.addSubWindow(sub) 57 | sub.setWindowIcon(QIcon('./pic/text.png')) 58 | sub.setWindowTitle('New' + str(self.winCount)) 59 | sub.showMaximized() 60 | 61 | def on_savefile(self): 62 | curSubWin = self.mdi.currentSubWindow() 63 | if curSubWin == None: 64 | return None 65 | 66 | if os.path.exists(curSubWin.windowTitle()): 67 | with open(curSubWin.windowTitle(),'w') as f: 68 | f.write(curSubWin.widget().toPlainText()) 69 | else: 70 | try: 71 | fname,_ = QFileDialog.getSaveFileName(self, 'Save file', '.', 'text file (*.v *.txt)') 72 | print(fname) 73 | with open(fname[0],'w') as f: 74 | f.write(curSubWin.widget().toPlainText()) 75 | curSubWin.setWindowTitle(fname) 76 | except: 77 | return None 78 | 79 | 80 | def on_openfile(self): 81 | try: 82 | fname,_ = QFileDialog.getOpenFileName(self, 'Open file', '.', 'text file (*.v *.txt)') 83 | with open(fname, 'r') as f: 84 | cont = f.read() 85 | sub = QMdiSubWindow() 86 | editor = QTextEdit() 87 | sub.setWidget(editor) 88 | highlight = syntax_highlight.PythonHighlighter(editor) 89 | editor.setPlainText(cont) 90 | editor.setFont(QtGui.QFont("Timers" , 14)) 91 | self.mdi.addSubWindow(sub) 92 | sub.setWindowIcon(QIcon('./pic/text.png')) 93 | sub.setWindowTitle(fname) 94 | sub.showMaximized() 95 | except: 96 | return None 97 | 98 | 99 | 100 | 101 | def on_cascade(self): 102 | # print('cascade') 103 | self.mdi.cascadeSubWindows() 104 | 105 | def on_tiled(self): 106 | # print('tiled') 107 | self.mdi.tileSubWindows() 108 | 109 | def on_viewInst(self): 110 | curSubWin = self.mdi.currentSubWindow() 111 | if curSubWin == None: 112 | return None 113 | 114 | text = curSubWin.widget().toPlainText() 115 | try: 116 | instTempl = genHdlInst(text) 117 | self.on_newfile(instTempl) 118 | self.on_tiled() 119 | except HdlError: 120 | print('error') 121 | self.critical_dlg() 122 | return None 123 | 124 | # print(rm_res) 125 | 126 | def critical_dlg(self): 127 | reply = QMessageBox.critical(self, "Error", "HDL Syntax error!!!") 128 | 129 | 130 | if __name__ == '__main__': 131 | app = QApplication(sys.argv) 132 | main = ToolApp() 133 | main.show() 134 | sys.exit(app.exec_()) 135 | 136 | 137 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FpgaTool 2 | 3 | Description: 4 |   This gui/script is used to generate verilog instance. There are two methods you can use. 5 | 6 | Method1: Use the gui. [Demo](https://raw.githubusercontent.com/Rex1168/FpgaTool/master/gif/FpgaTool.gif) 7 | 1. Run FpgaTool.py. You can run "python FpgaTool.py" on the command line. 8 | 2. Open a verilog file 9 | 3. Click button "View HDL Inst" 10 | 11 | 12 | Method2: Use script directly. [Demo](https://raw.githubusercontent.com/Rex1168/FpgaTool/master/gif/genHdlInst.gif) 13 | 1. Run genHdlInst.py. You can run "python genHdlInst.py top.v" on the command line. 14 | 2. It will generate its instance file named "inst_top.v" 15 | 16 | 17 | -------------------------------------------------------------------------------- /genHdlInst.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | ------------------------------------------------- 4 | File Name: genHdlInst 5 | Description : generate hdl instance 6 | Author : Rex 7 | date: 2019/9/20 8 | ------------------------------------------------- 9 | Change Activity: 10 | 2019/9/20: 11 | ------------------------------------------------- 12 | """ 13 | __author__ = 'Rex' 14 | 15 | import re 16 | 17 | 18 | class HdlError(Exception): 19 | def __init__(self,ErrorInfo): 20 | super().__init__(self) 21 | self.errorinfo=ErrorInfo 22 | def __str__(self): 23 | return self.errorinfo 24 | 25 | def rmComments(text): 26 | ''' 27 | remove comments 28 | :param text: hdl content (string) 29 | :return: hdl without comments (string) 30 | ''' 31 | singLineComments = re.compile(r'//(.*)', re.MULTILINE) 32 | multiLineComments = re.compile(r'/\*(.*)\*/', re.DOTALL) 33 | text = singLineComments.sub('', text) 34 | text = multiLineComments.sub('', text) 35 | return text 36 | 37 | def findModuleName(text): 38 | ''' 39 | find module name 40 | :param text: hdl context without comments (string) 41 | :return: module name 42 | ''' 43 | modulePos = text.find('module') 44 | if modulePos==-1: 45 | raise HdlError('Syntax error: Can not find module!') 46 | text = text[modulePos+7:] 47 | modNameRe = re.match(r'\w*\s*', text) 48 | modName = modNameRe.group(0).strip() 49 | return modName 50 | 51 | def findParams(text): 52 | ''' 53 | find module parameters 54 | :param text: hdl context without comments and keyword 'module' 55 | :return: all parameter list and value list 56 | ''' 57 | param = r'\sparameter\s[\w\W]*?[;,)]' 58 | paralst = re.findall(param, text) 59 | 60 | if paralst!=[]: 61 | paramStr = '\n'.join(paralst) 62 | pat = r'(\w*)\s*=\s*([\w\W]*?)\s*[;,)]' 63 | p = re.findall(pat, paramStr) 64 | paramNameLst = [i[0] for i in p] 65 | paramValLst = [i[1] for i in p] 66 | return paramNameLst,paramValLst 67 | else: 68 | return [], [] 69 | 70 | def paramInstTempl(paramNameLst, paramValLst): 71 | ''' 72 | generate parameters instance template 73 | :param paramNameLst: parameters Name list 74 | :param paramValLst: parameters value list 75 | :return: parameters instance template 76 | ''' 77 | if paramValLst==[]: 78 | return '' 79 | else: 80 | nameLen = max(len(i) for i in paramNameLst) 81 | valLen = max(len(i) for i in paramValLst) 82 | paraInst = '#(\n' + ',\n'.join(' .' + i.ljust(nameLen) 83 | + '(' + k.ljust(valLen) + ')' 84 | for (i,k) in zip(paramNameLst,paramValLst)) + '\n)' 85 | return paraInst 86 | 87 | def findIoPorts(text): 88 | ''' 89 | IO port declare syntax: 90 | (input | output | inout) (wire | reg) (signed) ([High:Low]) (port1,port2,port3) = (x'hxx) (,;'') 91 | example: 92 | output reg signed [MAX_WIDTH-1:0] out1, out2 = 10'd0, 93 | :param text: hdl context without comments and keyword 'module' 94 | :return: all io ports declare, include: port_name port_width port_type 95 | ''' 96 | portPair = {'input':'reg', 'output':'wire', 'inout':'inout'} 97 | portNameLst = [] 98 | portWidthLst = [] 99 | portTypeLst = [] 100 | 101 | portPat = r'(\s*(input|output|inout)\s+)((wire|reg)\s+)*((signed)\s+)*(\[.*?:.*?\]\s*)*' \ 102 | r'(.*\s*)(=?)(.*)(?=\binput\b|\boutput\b|\binout\b|\))' 103 | 104 | portLst = re.findall(portPat, text) 105 | if len(portLst)==0: 106 | raise HdlError('Syntax error: Can not find io port!') 107 | for port in portLst: 108 | portnames = re.findall(r'(\S*)',port[-3])[0].split(',') 109 | if portnames[-1] == '': 110 | portnames.pop() 111 | for item in portnames: 112 | portNameLst.append(item) 113 | portTypeLst.append(portPair[port[1]]) 114 | portWidthLst.append(port[-4]) 115 | return portNameLst, portWidthLst, portTypeLst 116 | 117 | def portInstTempl(portNameLst, portWidthLst, portTypeLst): 118 | nameLen = max(len(i) for i in portNameLst) 119 | typeLen = 6 # input output inout 120 | widthLen = max(len(i) for i in portWidthLst) 121 | portInst = '(\n' + ',\n'.join(' .' + port.ljust(nameLen) + '(' + port.ljust(nameLen) + ')' \ 122 | for port in portNameLst) + '\n);' 123 | portDeclare = '\n' + ';\n'.join(typ.ljust(typeLen) + ' ' + width.ljust(widthLen) + ' ' + name \ 124 | for (typ,width,name) in zip(portTypeLst,portWidthLst,portNameLst)) + ';\n\n' 125 | 126 | return portDeclare,portInst 127 | 128 | 129 | def genHdlInst(text): 130 | ''' 131 | generate hld instance 132 | :param s: hdl file context 133 | :return: the instance string 134 | ''' 135 | textRmComments = rmComments(text) 136 | moduleName = findModuleName(textRmComments) 137 | #if moduleName=='': 138 | 139 | paramNameLst, paramValLst = findParams(textRmComments) 140 | portNameLst, portWidthLst, portTypeLst = findIoPorts(textRmComments) 141 | portDeclare, portInst = portInstTempl(portNameLst, portWidthLst, portTypeLst) 142 | paraInst = paramInstTempl(paramNameLst, paramValLst) 143 | instTempl = portDeclare + moduleName + paraInst + ' inst_' + moduleName + portInst 144 | return instTempl 145 | 146 | if __name__=='__main__': 147 | import sys 148 | if len(sys.argv) < 2: 149 | raise RuntimeError("source HDL file is not selected") 150 | 151 | sourcefile = sys.argv[1] 152 | infile = open(sourcefile, encoding='UTF-8') 153 | fileCont = infile.read() 154 | instTempl = genHdlInst(fileCont) 155 | outfilename = 'inst_' + sourcefile 156 | outfile = open(outfilename, 'w', encoding='UTF-8') 157 | outfile.write(instTempl) 158 | infile.close() 159 | outfile.close() 160 | 161 | 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /gif/FpgaTool.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FouriersCat/Verilog-Template/537a43e7c33e19f8eb69f2a067cc53655e8cbc23/gif/FpgaTool.gif -------------------------------------------------------------------------------- /gif/genHdlInst.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FouriersCat/Verilog-Template/537a43e7c33e19f8eb69f2a067cc53655e8cbc23/gif/genHdlInst.gif -------------------------------------------------------------------------------- /mainwindow.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # Form implementation generated from reading ui file 'mainwindow.ui' 4 | # 5 | # Created by: PyQt5 UI code generator 5.11.2 6 | # 7 | # WARNING! All changes made in this file will be lost! 8 | 9 | from PyQt5 import QtCore, QtGui, QtWidgets 10 | 11 | class Ui_FpgaTool(object): 12 | def setupUi(self, FpgaTool): 13 | FpgaTool.setObjectName("FpgaTool") 14 | FpgaTool.resize(891, 795) 15 | icon = QtGui.QIcon() 16 | icon.addPixmap(QtGui.QPixmap("pic/fpga.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) 17 | FpgaTool.setWindowIcon(icon) 18 | self.centralWidget = QtWidgets.QWidget(FpgaTool) 19 | self.centralWidget.setObjectName("centralWidget") 20 | self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralWidget) 21 | self.horizontalLayout.setContentsMargins(11, 11, 11, 11) 22 | self.horizontalLayout.setSpacing(6) 23 | self.horizontalLayout.setObjectName("horizontalLayout") 24 | self.mdi = QtWidgets.QMdiArea(self.centralWidget) 25 | self.mdi.setObjectName("mdi") 26 | self.horizontalLayout.addWidget(self.mdi) 27 | FpgaTool.setCentralWidget(self.centralWidget) 28 | self.menuBar = QtWidgets.QMenuBar(FpgaTool) 29 | self.menuBar.setGeometry(QtCore.QRect(0, 0, 891, 23)) 30 | self.menuBar.setObjectName("menuBar") 31 | self.menuFile = QtWidgets.QMenu(self.menuBar) 32 | self.menuFile.setObjectName("menuFile") 33 | self.menuEdit = QtWidgets.QMenu(self.menuBar) 34 | self.menuEdit.setObjectName("menuEdit") 35 | self.menuTools = QtWidgets.QMenu(self.menuBar) 36 | self.menuTools.setObjectName("menuTools") 37 | self.menuHelp = QtWidgets.QMenu(self.menuBar) 38 | self.menuHelp.setObjectName("menuHelp") 39 | self.menuView = QtWidgets.QMenu(self.menuBar) 40 | self.menuView.setObjectName("menuView") 41 | FpgaTool.setMenuBar(self.menuBar) 42 | self.mainToolBar = QtWidgets.QToolBar(FpgaTool) 43 | self.mainToolBar.setObjectName("mainToolBar") 44 | FpgaTool.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar) 45 | self.statusBar = QtWidgets.QStatusBar(FpgaTool) 46 | self.statusBar.setObjectName("statusBar") 47 | FpgaTool.setStatusBar(self.statusBar) 48 | self.actionNew_File = QtWidgets.QAction(FpgaTool) 49 | icon1 = QtGui.QIcon() 50 | icon1.addPixmap(QtGui.QPixmap("pic/newfile.png"), QtGui.QIcon.Normal, QtGui.QIcon.On) 51 | self.actionNew_File.setIcon(icon1) 52 | self.actionNew_File.setObjectName("actionNew_File") 53 | self.actionOpen_File = QtWidgets.QAction(FpgaTool) 54 | icon2 = QtGui.QIcon() 55 | icon2.addPixmap(QtGui.QPixmap("pic/openfile.png"), QtGui.QIcon.Normal, QtGui.QIcon.On) 56 | self.actionOpen_File.setIcon(icon2) 57 | self.actionOpen_File.setObjectName("actionOpen_File") 58 | self.actionSave_File = QtWidgets.QAction(FpgaTool) 59 | icon3 = QtGui.QIcon() 60 | icon3.addPixmap(QtGui.QPixmap("pic/savefile.png"), QtGui.QIcon.Normal, QtGui.QIcon.On) 61 | self.actionSave_File.setIcon(icon3) 62 | self.actionSave_File.setObjectName("actionSave_File") 63 | self.actionCascade = QtWidgets.QAction(FpgaTool) 64 | self.actionCascade.setObjectName("actionCascade") 65 | self.actionTiled = QtWidgets.QAction(FpgaTool) 66 | self.actionTiled.setObjectName("actionTiled") 67 | self.actionView_HDL_Inst = QtWidgets.QAction(FpgaTool) 68 | icon4 = QtGui.QIcon() 69 | icon4.addPixmap(QtGui.QPixmap("pic/gen_inst.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) 70 | icon4.addPixmap(QtGui.QPixmap("pic/text.png"), QtGui.QIcon.Normal, QtGui.QIcon.On) 71 | self.actionView_HDL_Inst.setIcon(icon4) 72 | self.actionView_HDL_Inst.setObjectName("actionView_HDL_Inst") 73 | self.menuFile.addAction(self.actionNew_File) 74 | self.menuFile.addAction(self.actionOpen_File) 75 | self.menuFile.addAction(self.actionSave_File) 76 | self.menuTools.addAction(self.actionView_HDL_Inst) 77 | self.menuView.addAction(self.actionCascade) 78 | self.menuView.addAction(self.actionTiled) 79 | self.menuBar.addAction(self.menuFile.menuAction()) 80 | self.menuBar.addAction(self.menuEdit.menuAction()) 81 | self.menuBar.addAction(self.menuView.menuAction()) 82 | self.menuBar.addAction(self.menuTools.menuAction()) 83 | self.menuBar.addAction(self.menuHelp.menuAction()) 84 | self.mainToolBar.addAction(self.actionNew_File) 85 | self.mainToolBar.addSeparator() 86 | self.mainToolBar.addAction(self.actionOpen_File) 87 | self.mainToolBar.addSeparator() 88 | self.mainToolBar.addAction(self.actionSave_File) 89 | self.mainToolBar.addSeparator() 90 | self.mainToolBar.addAction(self.actionView_HDL_Inst) 91 | 92 | self.retranslateUi(FpgaTool) 93 | QtCore.QMetaObject.connectSlotsByName(FpgaTool) 94 | 95 | def retranslateUi(self, FpgaTool): 96 | _translate = QtCore.QCoreApplication.translate 97 | FpgaTool.setWindowTitle(_translate("FpgaTool", "MainWindow")) 98 | self.menuFile.setTitle(_translate("FpgaTool", "File")) 99 | self.menuEdit.setTitle(_translate("FpgaTool", "Edit")) 100 | self.menuTools.setTitle(_translate("FpgaTool", "Tools")) 101 | self.menuHelp.setTitle(_translate("FpgaTool", "Help")) 102 | self.menuView.setTitle(_translate("FpgaTool", "View")) 103 | self.actionNew_File.setText(_translate("FpgaTool", "New File")) 104 | self.actionNew_File.setShortcut(_translate("FpgaTool", "Ctrl+N")) 105 | self.actionOpen_File.setText(_translate("FpgaTool", "Open File")) 106 | self.actionOpen_File.setShortcut(_translate("FpgaTool", "Ctrl+O")) 107 | self.actionSave_File.setText(_translate("FpgaTool", "Save File")) 108 | self.actionSave_File.setShortcut(_translate("FpgaTool", "Ctrl+S")) 109 | self.actionCascade.setText(_translate("FpgaTool", "Cascade")) 110 | self.actionTiled.setText(_translate("FpgaTool", "Tiled")) 111 | self.actionView_HDL_Inst.setText(_translate("FpgaTool", "View HDL Inst")) 112 | 113 | -------------------------------------------------------------------------------- /mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | FpgaTool 4 | 5 | 6 | 7 | 0 8 | 0 9 | 891 10 | 795 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | pic/fpga.pngpic/fpga.png 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 0 31 | 0 32 | 891 33 | 23 34 | 35 | 36 | 37 | 38 | File 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | Edit 47 | 48 | 49 | 50 | 51 | Tools 52 | 53 | 54 | 55 | 56 | 57 | Help 58 | 59 | 60 | 61 | 62 | View 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | TopToolBarArea 76 | 77 | 78 | false 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | pic/newfile.png 93 | 94 | 95 | 96 | New File 97 | 98 | 99 | Ctrl+N 100 | 101 | 102 | 103 | 104 | 105 | pic/openfile.png 106 | 107 | 108 | 109 | Open File 110 | 111 | 112 | Ctrl+O 113 | 114 | 115 | 116 | 117 | 118 | pic/savefile.png 119 | 120 | 121 | 122 | Save File 123 | 124 | 125 | Ctrl+S 126 | 127 | 128 | 129 | 130 | Cascade 131 | 132 | 133 | 134 | 135 | Tiled 136 | 137 | 138 | 139 | 140 | 141 | pic/gen_inst.png 142 | pic/text.pngpic/gen_inst.png 143 | 144 | 145 | View HDL Inst 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /pic/fpga.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FouriersCat/Verilog-Template/537a43e7c33e19f8eb69f2a067cc53655e8cbc23/pic/fpga.png -------------------------------------------------------------------------------- /pic/gen_format.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FouriersCat/Verilog-Template/537a43e7c33e19f8eb69f2a067cc53655e8cbc23/pic/gen_format.png -------------------------------------------------------------------------------- /pic/gen_inst.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FouriersCat/Verilog-Template/537a43e7c33e19f8eb69f2a067cc53655e8cbc23/pic/gen_inst.png -------------------------------------------------------------------------------- /pic/newfile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FouriersCat/Verilog-Template/537a43e7c33e19f8eb69f2a067cc53655e8cbc23/pic/newfile.png -------------------------------------------------------------------------------- /pic/openfile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FouriersCat/Verilog-Template/537a43e7c33e19f8eb69f2a067cc53655e8cbc23/pic/openfile.png -------------------------------------------------------------------------------- /pic/savefile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FouriersCat/Verilog-Template/537a43e7c33e19f8eb69f2a067cc53655e8cbc23/pic/savefile.png -------------------------------------------------------------------------------- /pic/text.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FouriersCat/Verilog-Template/537a43e7c33e19f8eb69f2a067cc53655e8cbc23/pic/text.png -------------------------------------------------------------------------------- /syntax_highlight.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | ------------------------------------------------- 4 | File Name: syntax_hightlight 5 | Description : 6 | Author : don't know 7 | Reviser : Rex 8 | date: 2019/9/20 9 | ------------------------------------------------- 10 | Change Activity: 11 | 2019/9/20: 12 | ------------------------------------------------- 13 | """ 14 | 15 | import sys 16 | import time 17 | from PyQt5.QtCore import QRegExp 18 | from PyQt5.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter 19 | 20 | def format(color, style=''): 21 | """Return a QTextCharFormat with the given attributes. 22 | """ 23 | _color = QColor() 24 | _color.setNamedColor(color) 25 | 26 | _format = QTextCharFormat() 27 | _format.setForeground(_color) 28 | if 'bold' in style: 29 | _format.setFontWeight(QFont.Bold) 30 | if 'italic' in style: 31 | _format.setFontItalic(True) 32 | 33 | return _format 34 | 35 | 36 | # Syntax styles that can be shared by all languages 37 | STYLES = { 38 | 'keyword': format('blue'), 39 | 'operator': format('red'), 40 | 'brace': format('darkGray'), 41 | 'defclass': format('black', 'bold'), 42 | 'string': format('magenta'), 43 | 'string2': format('darkMagenta'), 44 | 'comment': format('darkGreen', 'italic'), 45 | 'self': format('black', 'italic'), 46 | 'numbers': format('brown'), 47 | } 48 | 49 | 50 | class PythonHighlighter (QSyntaxHighlighter): 51 | """Syntax highlighter for the Python language. 52 | """ 53 | # Python keywords 54 | keywords = [ 55 | # verilog 56 | 'module', 'begin','end', 'paramter','always', 'assign', 57 | 'case', 'endcase', 'endmodule', 'initial', 'wire','reg', 58 | 'and', 'if', 'else', 'for','input','output','inout' 59 | ] 60 | 61 | # Python operators 62 | operators = [ 63 | '=', 64 | # Comparison 65 | '==', '!=', '<', '<=', '>', '>=', 66 | # Arithmetic 67 | '\+', '-', '\*', '/', '//', '\%', '\*\*', 68 | # In-place 69 | '\+=', '-=', '\*=', '/=', '\%=', 70 | # Bitwise 71 | '\^', '\|', '\&', '\~', '>>', '<<', 72 | ] 73 | 74 | # Python braces 75 | braces = [ 76 | '\{', '\}', '\(', '\)', '\[', '\]', 77 | ] 78 | def __init__(self, document): 79 | QSyntaxHighlighter.__init__(self, document) 80 | 81 | # Multi-line strings (expression, flag, style) 82 | # FIXME: The triple-quotes in these two lines will mess up the 83 | # syntax highlighting from this point onward 84 | self.tri_single = (QRegExp("'''"), 1, STYLES['string2']) 85 | self.tri_double = (QRegExp('"""'), 2, STYLES['string2']) 86 | 87 | rules = [] 88 | 89 | # Keyword, operator, and brace rules 90 | rules += [(r'\b%s\b' % w, 0, STYLES['keyword']) 91 | for w in PythonHighlighter.keywords] 92 | rules += [(r'%s' % o, 0, STYLES['operator']) 93 | for o in PythonHighlighter.operators] 94 | rules += [(r'%s' % b, 0, STYLES['brace']) 95 | for b in PythonHighlighter.braces] 96 | 97 | # All other rules 98 | rules += [ 99 | # 'self' 100 | (r'\bself\b', 0, STYLES['self']), 101 | 102 | # Double-quoted string, possibly containing escape sequences 103 | (r'"[^"\\]*(\\.[^"\\]*)*"', 0, STYLES['string']), 104 | # Single-quoted string, possibly containing escape sequences 105 | (r"'[^'\\]*(\\.[^'\\]*)*'", 0, STYLES['string']), 106 | 107 | # 'def' followed by an identifier 108 | (r'\bdef\b\s*(\w+)', 1, STYLES['defclass']), 109 | # 'class' followed by an identifier 110 | (r'\bclass\b\s*(\w+)', 1, STYLES['defclass']), 111 | 112 | # From '#' until a newline 113 | (r'#[^\n]*', 0, STYLES['comment']), 114 | 115 | # Numeric literals 116 | (r'\b[+-]?[0-9]+[lL]?\b', 0, STYLES['numbers']), 117 | (r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b', 0, STYLES['numbers']), 118 | (r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, STYLES['numbers']), 119 | ] 120 | 121 | # Build a QRegExp for each pattern 122 | self.rules = [(QRegExp(pat), index, fmt) 123 | for (pat, index, fmt) in rules] 124 | 125 | 126 | def highlightBlock(self, text): 127 | """Apply syntax highlighting to the given block of text. 128 | """ 129 | # Do other syntax formatting 130 | for expression, nth, format in self.rules: 131 | index = expression.indexIn(text, 0) 132 | while index >= 0: 133 | # We actually want the index of the nth match 134 | index = expression.pos(nth) 135 | length = len(expression.cap(nth)) 136 | self.setFormat(index, length, format) 137 | index = expression.indexIn(text, index + length) 138 | 139 | self.setCurrentBlockState(0) 140 | 141 | # Do multi-line strings 142 | in_multiline = self.match_multiline(text, *self.tri_single) 143 | if not in_multiline: 144 | in_multiline = self.match_multiline(text, *self.tri_double) 145 | 146 | 147 | def match_multiline(self, text, delimiter, in_state, style): 148 | """Do highlighting of multi-line strings. ``delimiter`` should be a 149 | ``QRegExp`` for triple-single-quotes or triple-double-quotes, and 150 | ``in_state`` should be a unique integer to represent the corresponding 151 | state changes when inside those strings. Returns True if we're still 152 | inside a multi-line string when this function is finished. 153 | """ 154 | # If inside triple-single quotes, start at 0 155 | if self.previousBlockState() == in_state: 156 | start = 0 157 | add = 0 158 | # Otherwise, look for the delimiter on this line 159 | else: 160 | start = delimiter.indexIn(text) 161 | # Move past this match 162 | add = delimiter.matchedLength() 163 | 164 | # As long as there's a delimiter match on this line... 165 | while start >= 0: 166 | # Look for the ending delimiter 167 | end = delimiter.indexIn(text, start + add) 168 | # Ending delimiter on this line? 169 | if end >= add: 170 | length = end - start + add + delimiter.matchedLength() 171 | self.setCurrentBlockState(0) 172 | # No; multi-line string 173 | else: 174 | self.setCurrentBlockState(in_state) 175 | length = len(text) - start + add 176 | # Apply formatting 177 | self.setFormat(start, length, style) 178 | # Look for the next match 179 | start = delimiter.indexIn(text, start + length) 180 | 181 | # Return True if still inside a multi-line string, False otherwise 182 | if self.currentBlockState() == in_state: 183 | return True 184 | else: 185 | return False -------------------------------------------------------------------------------- /top.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | ////////////////////////////////////////////////////////////////////////////////// 3 | // Company: 4 | // Engineer: 5 | // 6 | // Create Date: 2019/09/03 15:04:00 7 | // Design Name: 8 | // Module Name: top 9 | // Project Name: 10 | // Target Devices: 11 | // Tool Versions: 12 | // Description: 13 | // 14 | // Dependencies: 15 | // 16 | // Revision: 17 | // Revision 0.01 - File Created 18 | // Additional Comments: 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | module top( 23 | input clk_n, 24 | input clk_p, 25 | output [14:0]DDR3_addr, 26 | output [2:0]DDR3_ba, 27 | output DDR3_cas_n, 28 | output [0:0]DDR3_ck_n, 29 | output [0:0]DDR3_ck_p, 30 | output [0:0]DDR3_cke, 31 | output [0:0]DDR3_cs_n, 32 | output [7:0]DDR3_dm, 33 | inout [63:0]DDR3_dq, 34 | inout [7:0]DDR3_dqs_n, 35 | inout [7:0]DDR3_dqs_p, 36 | output [0:0]DDR3_odt, 37 | output DDR3_ras_n, 38 | output DDR3_reset_n, 39 | output DDR3_we_n, 40 | input [0:0]MB_INTC, 41 | input ext_reset_in 42 | ); 43 | 44 | wire AXI_CLK; 45 | reg AXI_RSTN; 46 | reg [9:0] cnt; 47 | always @ ( posedge AXI_CLK ) 48 | begin 49 | if(cnt<=10'd1000) 50 | cnt <= cnt + 1'b1; 51 | end 52 | 53 | always @ ( posedge AXI_CLK ) 54 | begin 55 | if(cnt<=10'd500) 56 | AXI_RSTN <= 1'b0; 57 | else 58 | AXI_RSTN <= 1'b1; 59 | end 60 | 61 | ddr_test_wrapper inst_ddr( 62 | .AXI_CLK (AXI_CLK ), 63 | .AXI_RSTN (AXI_RSTN ), 64 | .CLK_IN1_D_clk_n (clk_n ), 65 | .CLK_IN1_D_clk_p (clk_p ), 66 | .DDR3_addr (DDR3_addr ), 67 | .DDR3_ba (DDR3_ba ), 68 | .DDR3_cas_n (DDR3_cas_n ), 69 | .DDR3_ck_n (DDR3_ck_n ), 70 | .DDR3_ck_p (DDR3_ck_p ), 71 | .DDR3_cke (DDR3_cke ), 72 | .DDR3_cs_n (DDR3_cs_n ), 73 | .DDR3_dm (DDR3_dm ), 74 | .DDR3_dq (DDR3_dq ), 75 | .DDR3_dqs_n (DDR3_dqs_n ), 76 | .DDR3_dqs_p (DDR3_dqs_p ), 77 | .DDR3_odt (DDR3_odt ), 78 | .DDR3_ras_n (DDR3_ras_n ), 79 | .DDR3_reset_n (DDR3_reset_n ), 80 | .DDR3_we_n (DDR3_we_n ), 81 | .MB_INTC (MB_INTC ), 82 | .ext_reset_in (ext_reset_in ), 83 | .init_calib_complete(init_calib_complete), 84 | .mmcm_locked (mmcm_locked), 85 | .sys_rst (~AXI_RSTN), 86 | .ui_clk_sync_rst (ui_clk_sync_rst) 87 | ); 88 | 89 | 90 | endmodule 91 | --------------------------------------------------------------------------------