|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import print_function |
| 3 | +import os |
| 4 | +import sys |
| 5 | +from pyverilog.vparser.parser import VerilogCodeParser |
| 6 | + |
| 7 | +try: |
| 8 | + from StringIO import StringIO |
| 9 | +except: |
| 10 | + from io import StringIO |
| 11 | + |
| 12 | +codedir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + '/verilogcode/' |
| 13 | + |
| 14 | +expected = """\ |
| 15 | +Source: (at 1) |
| 16 | + Description: (at 1) |
| 17 | + ModuleDef: TOP (at 1) |
| 18 | + Paramlist: (at 0) |
| 19 | + Portlist: (at 2) |
| 20 | + Ioport: (at 3) |
| 21 | + Input: CLK, False (at 3) |
| 22 | + Ioport: (at 4) |
| 23 | + Input: RST, False (at 4) |
| 24 | + Ioport: (at 5) |
| 25 | + Output: LED, False (at 5) |
| 26 | + Width: (at 5) |
| 27 | + IntConst: 7 (at 5) |
| 28 | + IntConst: 0 (at 5) |
| 29 | + InstanceList: led (at 9) |
| 30 | + Instance: inst_led, led (at 9) |
| 31 | + PortArg: CLK (at 14) |
| 32 | + Identifier: CLK (at 14) |
| 33 | + PortArg: RST (at 15) |
| 34 | + Identifier: RST (at 15) |
| 35 | + PortArg: LED (at 16) |
| 36 | + Identifier: LED (at 16) |
| 37 | + ModuleDef: led (at 21) |
| 38 | + Paramlist: (at 21) |
| 39 | + Decl: (at 23) |
| 40 | + Parameter: STEP, False (at 23) |
| 41 | + Rvalue: (at 23) |
| 42 | + IntConst: 10 (at 23) |
| 43 | + Portlist: (at 25) |
| 44 | + Ioport: (at 26) |
| 45 | + Input: CLK, False (at 26) |
| 46 | + Ioport: (at 27) |
| 47 | + Input: RST, False (at 27) |
| 48 | + Ioport: (at 28) |
| 49 | + Output: LED, False (at 28) |
| 50 | + Width: (at 28) |
| 51 | + IntConst: 7 (at 28) |
| 52 | + IntConst: 0 (at 28) |
| 53 | + Reg: LED, False (at 28) |
| 54 | + Width: (at 28) |
| 55 | + IntConst: 7 (at 28) |
| 56 | + IntConst: 0 (at 28) |
| 57 | + Decl: (at 31) |
| 58 | + Reg: count, False (at 31) |
| 59 | + Width: (at 31) |
| 60 | + IntConst: 31 (at 31) |
| 61 | + IntConst: 0 (at 31) |
| 62 | + Always: (at 33) |
| 63 | + SensList: (at 33) |
| 64 | + Sens: posedge (at 33) |
| 65 | + Identifier: CLK (at 33) |
| 66 | + Block: None (at 33) |
| 67 | + IfStatement: (at 34) |
| 68 | + Identifier: RST (at 34) |
| 69 | + Block: None (at 34) |
| 70 | + NonblockingSubstitution: (at 35) |
| 71 | + Lvalue: (at 35) |
| 72 | + Identifier: count (at 35) |
| 73 | + Rvalue: (at 35) |
| 74 | + IntConst: 0 (at 35) |
| 75 | + NonblockingSubstitution: (at 36) |
| 76 | + Lvalue: (at 36) |
| 77 | + Identifier: LED (at 36) |
| 78 | + Rvalue: (at 36) |
| 79 | + IntConst: 0 (at 36) |
| 80 | + Block: None (at 37) |
| 81 | + IfStatement: (at 38) |
| 82 | + Eq: (at 38) |
| 83 | + Identifier: count (at 38) |
| 84 | + Minus: (at 38) |
| 85 | + Identifier: STEP (at 38) |
| 86 | + IntConst: 1 (at 38) |
| 87 | + Block: None (at 38) |
| 88 | + NonblockingSubstitution: (at 39) |
| 89 | + Lvalue: (at 39) |
| 90 | + Identifier: count (at 39) |
| 91 | + Rvalue: (at 39) |
| 92 | + IntConst: 0 (at 39) |
| 93 | + NonblockingSubstitution: (at 40) |
| 94 | + Lvalue: (at 40) |
| 95 | + Identifier: LED (at 40) |
| 96 | + Rvalue: (at 40) |
| 97 | + Plus: (at 40) |
| 98 | + Identifier: LED (at 40) |
| 99 | + IntConst: 1 (at 40) |
| 100 | + Block: None (at 41) |
| 101 | + NonblockingSubstitution: (at 42) |
| 102 | + Lvalue: (at 42) |
| 103 | + Identifier: count (at 42) |
| 104 | + Rvalue: (at 42) |
| 105 | + Plus: (at 42) |
| 106 | + Identifier: count (at 42) |
| 107 | + IntConst: 1 (at 42) |
| 108 | +""" |
| 109 | + |
| 110 | +def test(): |
| 111 | + filelist = [codedir + 'instance_empty_params.v'] |
| 112 | + output = 'preprocess.out' |
| 113 | + include = [codedir] |
| 114 | + define = [] |
| 115 | + |
| 116 | + parser = VerilogCodeParser(filelist, |
| 117 | + preprocess_include=include, |
| 118 | + preprocess_define=define) |
| 119 | + ast = parser.parse() |
| 120 | + directives = parser.get_directives() |
| 121 | + |
| 122 | + output = StringIO() |
| 123 | + ast.show(buf=output) |
| 124 | + |
| 125 | + for lineno, directive in directives: |
| 126 | + output.write('Line %d : %s' % (lineno, directive)) |
| 127 | + |
| 128 | + rslt = output.getvalue() |
| 129 | + |
| 130 | + print(rslt) |
| 131 | + assert(expected == rslt) |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + test() |
0 commit comments