Skip to content

Commit 9ad7103

Browse files
committed
System task is now supported.
1 parent 07d26aa commit 9ad7103

File tree

13 files changed

+334
-146
lines changed

13 files changed

+334
-146
lines changed

sample/test/function/led.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def mkLed():
4242
If(count == 1023)(
4343
count(0)
4444
).Else(
45-
count( FunctionCall(inc.name, count, 1) )
46-
#count( inc.call(count, 1) )
45+
#count( FunctionCall(inc.name, count, 1) + 1 - 1 )
46+
count( inc.call(count, 1) + 1 - 1 )
4747
)
4848
))
4949

sample/test/function/test_led.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
if(count == 1023) begin
4040
count <= 0;
4141
end else begin
42-
count <= inc(count, 1);
42+
count <= inc(count, 1) + 1 - 1;
4343
end
4444
end
4545
end

sample/test/read_verilog/Makefile

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,7 @@
1-
TARGET=led.py
2-
TEST=test_led.py
3-
ARGS=
4-
5-
PYTHON=python3
6-
#PYTHON=python
7-
#OPT=-m pdb
8-
#OPT=-m cProfile -s time
9-
#OPT=-m cProfile -o profile.rslt
10-
11-
.PHONY: all
12-
all: test
13-
14-
.PHONY: run
15-
run:
16-
$(PYTHON) $(OPT) $(TARGET) $(ARGS)
1+
.PHONY: clean
2+
clean:
3+
find . -maxdepth 1 -type d |grep "./" | xargs -I {} make clean -C {}
174

185
.PHONY: test
196
test:
20-
$(PYTHON) -m pytest -vv $(TEST)
21-
22-
.PHONY: check
23-
check:
24-
$(PYTHON) $(OPT) $(TARGET) $(ARGS) > tmp.v
25-
iverilog -tnull -Wall tmp.v
26-
rm -f tmp.v
27-
28-
.PHONY: clean
29-
clean:
30-
rm -rf *.pyc __pycache__ parsetab.py *.out
7+
find . -maxdepth 1 -type d |grep "./" | xargs -I {} make test -C {}

sample/test/read_verilog/module/led.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ def mkTop():
1818

1919
params = ( width, )
2020
ports = ( clk, rst, led )
21-
22-
m.Instance(mkLed(), 'inst_blinkled', params, ports)
21+
22+
led = mkLed()
23+
m.Instance(led, 'inst_blinkled', params, ports)
2324

2425
return m
2526

sample/test/systemtask/Makefile

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

sample/test/systemtask/led.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
import os
3+
from veriloggen import *
4+
5+
def mkLed():
6+
m = Module('blinkled')
7+
width = m.Parameter('WIDTH', 8)
8+
clk = m.Input('CLK')
9+
rst = m.Input('RST')
10+
led = m.OutputReg('LED', width)
11+
count = m.Reg('count', 32)
12+
13+
m.Always(Posedge(clk))(
14+
If(rst)(
15+
count(0)
16+
).Else(
17+
If(count == 1023)(
18+
count(0)
19+
).Else(
20+
count(count + 1)
21+
)
22+
))
23+
24+
m.Always(Posedge(clk))(
25+
If(rst)(
26+
led(0)
27+
).Else(
28+
If(count == 1024 - 1)(
29+
led(led + 1),
30+
SystemTask('display', 'led:%x', led)
31+
)
32+
))
33+
34+
return m
35+
36+
if __name__ == '__main__':
37+
led = mkLed()
38+
# led.to_verilog(filename='tmp.v')
39+
verilog = led.to_verilog()
40+
print(verilog)

sample/test/systemtask/test_led.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import led
2+
3+
expected_verilog = """
4+
module blinkled #
5+
(
6+
parameter WIDTH = 8
7+
)
8+
(
9+
input CLK,
10+
input RST,
11+
output reg [WIDTH-1:0] LED
12+
);
13+
reg [32-1:0] count;
14+
always @(posedge CLK) begin
15+
if(RST) begin
16+
count <= 0;
17+
end else begin
18+
if(count == 1023) begin
19+
count <= 0;
20+
end else begin
21+
count <= count + 1;
22+
end
23+
end
24+
end
25+
always @(posedge CLK) begin
26+
if(RST) begin
27+
LED <= 0;
28+
end else begin
29+
if(count == 1023) begin
30+
LED <= LED + 1;
31+
$display("led:%x", LED);
32+
end
33+
end
34+
end
35+
endmodule
36+
"""
37+
38+
def test_led():
39+
led_module = led.mkLed()
40+
led_code = led_module.to_verilog()
41+
42+
from pyverilog.vparser.parser import VerilogParser
43+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
44+
parser = VerilogParser()
45+
expected_ast = parser.parse(expected_verilog)
46+
codegen = ASTCodeGenerator()
47+
expected_code = codegen.visit(expected_ast)
48+
49+
assert(led_code == expected_code)

sample/test/systemtask/veriloggen

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../veriloggen

veriloggen/function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def next(self, r):
4646
def call(self, *args):
4747
return FunctionCall(self.name, *args)
4848

49-
class FunctionCall(vtypes.VeriloggenNode):
49+
class FunctionCall(vtypes._Numeric):
5050
def __init__(self, name, *args):
5151
self.name = name
5252
self.args = tuple(args)

veriloggen/module.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,28 @@ def add_object(self, obj):
214214
self.initial.append(obj)
215215
return
216216

217+
if isinstance(obj, GenerateFor):
218+
if obj.scope is None:
219+
if None not in self.generate: self.generate[None] = []
220+
self.generate[None].append(obj)
221+
return
222+
self.generate[obj.scope] = obj
223+
return
224+
225+
if isinstance(obj, GenerateIf):
226+
if obj.true_scope is None:
227+
if None not in self.generate: self.generate[None] = []
228+
self.generate[None].append(obj)
229+
return
230+
self.generate[obj.true_scope] = obj
231+
return
232+
233+
if isinstance(obj, Instance):
234+
self.instance[obj.name] = obj
235+
if isinstance(obj.module, Module):
236+
self.submodule[obj.module.name] = obj.module
237+
return
238+
217239
raise TypeError("Object type '%s' is not supported." % str(type(obj)))
218240

219241
#---------------------------------------------------------------------------
@@ -278,7 +300,7 @@ def to_verilog(self, filename=None):
278300
return to_verilog.write_verilog(self, filename)
279301

280302
#-------------------------------------------------------------------------------
281-
class StubModule(Module):
303+
class StubModule(vtypes.VeriloggenNode):
282304
""" Verilog Module class """
283305
def __init__(self, name=None):
284306
self.name = name if name is not None else self.__class__.__name__
@@ -303,7 +325,7 @@ def __init__(self, module, instname, params, ports):
303325
self.ports = [ (None, p) for p in ports ]
304326

305327
def type_check_module(self, module):
306-
if not isinstance(module, Module):
328+
if not isinstance(module, (Module, StubModule)):
307329
raise TypeError("module of Instance must be Module or StubModule, not %s" %
308330
type(module))
309331

0 commit comments

Comments
 (0)