Skip to content

Commit c98c09e

Browse files
committed
vtypes.Int is updated: Int, IntX, IntZ are merged.
1 parent 078491c commit c98c09e

File tree

8 files changed

+367
-251
lines changed

8 files changed

+367
-251
lines changed

tests/_int/Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
TARGET=*.py
2+
ARGS=
3+
4+
PYTHON=python3
5+
#PYTHON=python
6+
#OPT=-m pdb
7+
#OPT=-m cProfile -s time
8+
#OPT=-m cProfile -o profile.rslt
9+
10+
.PHONY: all
11+
all: test
12+
13+
.PHONY: run
14+
run:
15+
$(PYTHON) $(OPT) $(TARGET) $(ARGS)
16+
17+
.PHONY: test
18+
test:
19+
$(PYTHON) -m pytest -vv
20+
21+
.PHONY: check
22+
check:
23+
$(PYTHON) $(OPT) $(TARGET) $(ARGS) > tmp.v
24+
iverilog -tnull -Wall tmp.v
25+
rm -f tmp.v
26+
27+
.PHONY: clean
28+
clean:
29+
rm -rf *.pyc __pycache__ parsetab.py *.out tmp.v uut.vcd

tests/_int/_int.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import sys
4+
import os
5+
6+
# the next line can be removed after installation
7+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
8+
9+
from veriloggen import *
10+
11+
def mkLed():
12+
m = Module('blinkled')
13+
width = m.Parameter('WIDTH', 8)
14+
clk = m.Input('CLK')
15+
rst = m.Input('RST')
16+
led = m.OutputReg('LED', width)
17+
count = m.Reg('count', 32)
18+
19+
m.Always(Posedge(clk))(
20+
If(rst)(
21+
count(0)
22+
).Else(
23+
If(count == 1023)(
24+
count(0)
25+
).Else(
26+
count(count + Int(1))
27+
)
28+
))
29+
30+
m.Always(Posedge(clk))(
31+
If(rst)(
32+
led(0)
33+
).Else(
34+
If(count == 1024 - 1)(
35+
led(Int("'h10"))
36+
)
37+
))
38+
39+
return m
40+
41+
def mkTest():
42+
m = Module('test')
43+
44+
# target instance
45+
led = mkLed()
46+
47+
# copy paras and ports
48+
params = m.copy_params(led)
49+
ports = m.copy_sim_ports(led)
50+
51+
clk = ports['CLK']
52+
rst = ports['RST']
53+
54+
uut = m.Instance(led, 'uut',
55+
params=m.connect_params(led),
56+
ports=m.connect_ports(led))
57+
58+
lib.simulation.setup_waveform(m, uut, m.get_vars())
59+
lib.simulation.setup_clock(m, clk, hperiod=5)
60+
init = lib.simulation.setup_reset(m, rst, m.make_reset(), period=100)
61+
62+
init.add(
63+
Delay(1000 * 100),
64+
Systask('finish'),
65+
)
66+
67+
return m
68+
69+
if __name__ == '__main__':
70+
test = mkTest()
71+
verilog = test.to_verilog('tmp.v')
72+
print(verilog)

tests/_int/test__int.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import _int
4+
5+
expected_verilog = """
6+
module test #
7+
(
8+
parameter WIDTH = 8
9+
)
10+
(
11+
);
12+
13+
reg CLK;
14+
reg RST;
15+
wire [(WIDTH - 1):0] LED;
16+
17+
blinkled
18+
#(
19+
.WIDTH(WIDTH)
20+
)
21+
uut
22+
(
23+
.CLK(CLK),
24+
.RST(RST),
25+
.LED(LED)
26+
);
27+
28+
initial begin
29+
$dumpfile("uut.vcd");
30+
$dumpvars(0, uut, CLK, RST, LED);
31+
end
32+
33+
initial begin
34+
CLK = 0;
35+
forever begin
36+
#5 CLK = (!CLK);
37+
end
38+
end
39+
40+
initial begin
41+
RST = 0;
42+
#100;
43+
RST = 1;
44+
#100;
45+
RST = 0;
46+
#100000;
47+
$finish;
48+
end
49+
endmodule
50+
51+
module blinkled #
52+
(
53+
parameter WIDTH = 8
54+
)
55+
(
56+
input CLK,
57+
input RST,
58+
output reg [WIDTH-1:0] LED
59+
);
60+
reg [32-1:0] count;
61+
always @(posedge CLK) begin
62+
if(RST) begin
63+
count <= 0;
64+
end else begin
65+
if(count == 1023) begin
66+
count <= 0;
67+
end else begin
68+
count <= count + 1;
69+
end
70+
end
71+
end
72+
always @(posedge CLK) begin
73+
if(RST) begin
74+
LED <= 0;
75+
end else begin
76+
if(count == 1023) begin
77+
LED <= 'h10;
78+
end
79+
end
80+
end
81+
endmodule
82+
"""
83+
84+
def test():
85+
test_module = _int.mkTest()
86+
code = test_module.to_verilog()
87+
88+
from pyverilog.vparser.parser import VerilogParser
89+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
90+
parser = VerilogParser()
91+
expected_ast = parser.parse(expected_verilog)
92+
codegen = ASTCodeGenerator()
93+
expected_code = codegen.visit(expected_ast)
94+
95+
assert(expected_code == code)

tests/intx/test_intx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
LED <= 0;
7575
end else begin
7676
if(count == 1023) begin
77-
LED <= 'dx;
77+
LED <= 'hx;
7878
end
7979
end
8080
end

tests/intz/test_intz.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
LED <= 0;
7575
end else begin
7676
if(count == 1023) begin
77-
LED <= 'dz;
77+
LED <= 'hz;
7878
end
7979
end
8080
end

veriloggen/from_verilog.py

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -81,45 +81,6 @@ def to_ast(*filelist, **opt):
8181
return ast
8282

8383
#-------------------------------------------------------------------------------
84-
def str_to_signed(s):
85-
targ = s.replace('_','')
86-
match = re.search(r's(.+)', targ)
87-
if match is not None:
88-
return True
89-
return False
90-
91-
def str_to_value(s):
92-
targ = s.replace('_','')
93-
match = re.search(r'h(.+)', targ)
94-
if match is not None:
95-
return int(match.group(1), 16), 16
96-
match = re.search(r'd(.+)', targ)
97-
if match is not None:
98-
return int(match.group(1), 10), 10
99-
match = re.search(r'o(.+)', targ)
100-
if match is not None:
101-
return int(match.group(1), 8), 8
102-
match = re.search(r'b(.+)', targ)
103-
if match is not None:
104-
return int(match.group(1), 2), 2
105-
return int(targ, 10), None
106-
107-
def str_to_width(s):
108-
targ = s.replace('_','')
109-
match = re.search(r'(.+)\'h.+', targ)
110-
if match is not None:
111-
return int(match.group(1), 10)
112-
match = re.search(r'(.+)\'d.+', targ)
113-
if match is not None:
114-
return int(match.group(1), 10)
115-
match = re.search(r'(.+)\'o.+', targ)
116-
if match is not None:
117-
return int(match.group(1), 10)
118-
match = re.search(r'(.+)\'b.+', targ)
119-
if match is not None:
120-
return int(match.group(1), 10)
121-
return None
122-
12384
def to_tuple(s):
12485
if not isinstance(s, (list, tuple)):
12586
return tuple([s])
@@ -205,9 +166,9 @@ def visit_Identifier(self, node):
205166
return self.m.find_identifier(node.name)
206167

207168
def visit_IntConst(self, node):
208-
value, base = str_to_value(node.value)
209-
width = str_to_width(node.value)
210-
signed = str_to_signed(node.value)
169+
value, base = vtypes.str_to_value(node.value)
170+
width = vtypes.str_to_width(node.value)
171+
signed = vtypes.str_to_signed(node.value)
211172
return vtypes.Int(value, width, base, signed)
212173

213174
def visit_FloatConst(self, node):

0 commit comments

Comments
 (0)