Skip to content

Commit 011e9ca

Browse files
committed
global value access
1 parent 300340e commit 011e9ca

File tree

5 files changed

+265
-32
lines changed

5 files changed

+265
-32
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
TARGET=$(shell ls *.py | grep -v test | grep -v parsetab.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 .cache *.out *.png *.dot tmp.v uut.vcd
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import veriloggen
4+
import thread_global_value
5+
6+
expected_verilog = """
7+
module test;
8+
9+
reg CLK;
10+
reg RST;
11+
wire [8-1:0] LED;
12+
13+
blinkled
14+
uut
15+
(
16+
.CLK(CLK),
17+
.RST(RST),
18+
.LED(LED)
19+
);
20+
21+
22+
initial begin
23+
$dumpfile("uut.vcd");
24+
$dumpvars(0, uut);
25+
end
26+
27+
28+
initial begin
29+
CLK = 0;
30+
forever begin
31+
#5 CLK = !CLK;
32+
end
33+
end
34+
35+
36+
initial begin
37+
RST = 0;
38+
#100;
39+
RST = 1;
40+
#100;
41+
RST = 0;
42+
#10000;
43+
$finish;
44+
end
45+
46+
47+
endmodule
48+
49+
50+
51+
module blinkled
52+
(
53+
input CLK,
54+
input RST,
55+
output reg [8-1:0] LED
56+
);
57+
58+
reg [8-1:0] count;
59+
reg [32-1:0] th_blink;
60+
localparam th_blink_init = 0;
61+
reg signed [32-1:0] _th_blink_times_0;
62+
reg signed [32-1:0] _th_blink_i_1;
63+
64+
always @(posedge CLK) begin
65+
if(RST) begin
66+
count <= 0;
67+
end else begin
68+
count <= count + 1;
69+
end
70+
end
71+
72+
localparam th_blink_1 = 1;
73+
localparam th_blink_2 = 2;
74+
localparam th_blink_3 = 3;
75+
localparam th_blink_4 = 4;
76+
localparam th_blink_5 = 5;
77+
localparam th_blink_6 = 6;
78+
localparam th_blink_7 = 7;
79+
80+
always @(posedge CLK) begin
81+
if(RST) begin
82+
th_blink <= th_blink_init;
83+
_th_blink_times_0 <= 0;
84+
LED <= 0;
85+
_th_blink_i_1 <= 0;
86+
end else begin
87+
case(th_blink)
88+
th_blink_init: begin
89+
_th_blink_times_0 <= 10;
90+
th_blink <= th_blink_1;
91+
end
92+
th_blink_1: begin
93+
LED <= 0;
94+
th_blink <= th_blink_2;
95+
end
96+
th_blink_2: begin
97+
_th_blink_i_1 <= 0;
98+
th_blink <= th_blink_3;
99+
end
100+
th_blink_3: begin
101+
if(_th_blink_i_1 < _th_blink_times_0) begin
102+
th_blink <= th_blink_4;
103+
end else begin
104+
th_blink <= th_blink_7;
105+
end
106+
end
107+
th_blink_4: begin
108+
LED <= count + 100;
109+
th_blink <= th_blink_5;
110+
end
111+
th_blink_5: begin
112+
$display("led = %d", LED);
113+
th_blink <= th_blink_6;
114+
end
115+
th_blink_6: begin
116+
_th_blink_i_1 <= _th_blink_i_1 + 1;
117+
th_blink <= th_blink_3;
118+
end
119+
endcase
120+
end
121+
end
122+
123+
124+
endmodule
125+
"""
126+
127+
128+
def test():
129+
veriloggen.reset()
130+
test_module = thread_global_value.mkTest()
131+
code = test_module.to_verilog()
132+
133+
from pyverilog.vparser.parser import VerilogParser
134+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
135+
parser = VerilogParser()
136+
expected_ast = parser.parse(expected_verilog)
137+
codegen = ASTCodeGenerator()
138+
expected_code = codegen.visit(expected_ast)
139+
140+
assert(expected_code == code)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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(
8+
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))))
9+
10+
from veriloggen import *
11+
import veriloggen.thread as vthread
12+
13+
global_value = 100
14+
15+
def mkLed():
16+
m = Module('blinkled')
17+
clk = m.Input('CLK')
18+
rst = m.Input('RST')
19+
led = m.OutputReg('LED', 8, initval=0)
20+
21+
count = m.Reg('count', 8, initval=0)
22+
23+
seq = Seq(m, 'seq', clk, rst)
24+
seq(
25+
count.inc()
26+
)
27+
28+
def blink(times):
29+
led.value = 0
30+
for i in range(times):
31+
led.value = count + global_value
32+
print("led = ", led)
33+
34+
th = vthread.Thread(m, 'th_blink', clk, rst, blink)
35+
fsm = th.start(10)
36+
37+
return m
38+
39+
40+
def mkTest():
41+
m = Module('test')
42+
43+
# target instance
44+
led = mkLed()
45+
46+
# copy paras and ports
47+
params = m.copy_params(led)
48+
ports = m.copy_sim_ports(led)
49+
50+
clk = ports['CLK']
51+
rst = ports['RST']
52+
53+
uut = m.Instance(led, 'uut',
54+
params=m.connect_params(led),
55+
ports=m.connect_ports(led))
56+
57+
simulation.setup_waveform(m, uut)
58+
simulation.setup_clock(m, clk, hperiod=5)
59+
init = simulation.setup_reset(m, rst, m.make_reset(), period=100)
60+
61+
init.add(
62+
Delay(10000),
63+
Systask('finish'),
64+
)
65+
66+
return m
67+
68+
if __name__ == '__main__':
69+
test = mkTest()
70+
verilog = test.to_verilog('tmp.v')
71+
print(verilog)
72+
73+
sim = simulation.Simulator(test)
74+
rslt = sim.run()
75+
print(rslt)

veriloggen/thread/compiler.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ class CompileVisitor(ast.NodeVisitor):
5252

5353
def __init__(self, m, name, clk, rst, fsm,
5454
functions, intrinsic_functions,
55-
intrinsic_methods, local_objects,
55+
intrinsic_methods,
56+
start_frame,
5657
datawidth=32):
5758

5859
self.m = m
@@ -64,7 +65,8 @@ def __init__(self, m, name, clk, rst, fsm,
6465
self.functions = functions
6566
self.intrinsic_functions = intrinsic_functions
6667
self.intrinsic_methods = intrinsic_methods
67-
self.local_objects = local_objects
68+
69+
self.start_frame = start_frame
6870
self.datawidth = datawidth
6971

7072
fixed_intrinsics = {
@@ -116,7 +118,7 @@ def _variable_type(self, right):
116118
'point': right.point,
117119
'signed': right.signed}
118120
return ret
119-
if isinstance(right, vtypes._Numeric):
121+
if isinstance(right, numerical_types):
120122
return None
121123
raise TypeError('unsupported type')
122124

@@ -633,10 +635,10 @@ def _call_Attribute(self, node):
633635
from .thread import Thread
634636
from .pool import ThreadPool
635637
if isinstance(value, Thread):
636-
value.local_objects = self.local_objects
638+
value.start_frame = self.start_frame
637639
if isinstance(value, ThreadPool):
638640
for thread in value.threads:
639-
thread.local_objects = self.local_objects
641+
thread.start_frame = self.start_frame
640642

641643
return method(*args, **kwargs)
642644

@@ -988,8 +990,9 @@ def getVariable(self, name, store=False, _type=None):
988990
var = self.scope.searchVariable(name, store)
989991
if var is None:
990992
if not store:
991-
if name in self.local_objects:
992-
return self.local_objects[name]
993+
local_objects = self.start_frame.f_locals
994+
if name in local_objects:
995+
return local_objects[name]
993996
glb = self.getGlobalObject(name)
994997
if glb is not None:
995998
return glb
@@ -1005,8 +1008,7 @@ def getTmpVariable(self, _type=None):
10051008
return var
10061009

10071010
def getGlobalObject(self, name):
1008-
frame = inspect.currentframe()
1009-
global_objects = OrderedDict()
1011+
global_objects = self.start_frame.f_globals
10101012
if name in global_objects:
10111013
return global_objects[name]
10121014
return None
@@ -1023,8 +1025,9 @@ def getFunction(self, name):
10231025
return func
10241026

10251027
# implicit function definitions
1026-
if name in self.local_objects:
1027-
func = self.local_objects[name]
1028+
local_objects = self.start_frame.f_locals
1029+
if name in local_objects:
1030+
func = local_objects[name]
10281031
if inspect.isfunction(func):
10291032
text = textwrap.dedent(inspect.getsource(func))
10301033
tree = ast.parse(text).body[0]

veriloggen/thread/thread.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, m, name, clk, rst, targ, datawidth=32, tid=None):
3939
self.start_state = None
4040
self.end_state = None
4141
self.return_value = None
42-
self.local_objects = OrderedDict()
42+
self.start_frame = None
4343
self.args_dict = OrderedDict()
4444
self.vararg_regs = []
4545
self.vararg_set = False
@@ -55,11 +55,7 @@ def start(self, *args, **kwargs):
5555
raise ValueError('already started')
5656

5757
frame = inspect.currentframe()
58-
_locals = frame.f_back.f_locals
59-
60-
self.local_objects = OrderedDict()
61-
for key, value in _locals.items():
62-
self.local_objects[key] = value
58+
self.start_frame = frame.f_back
6359

6460
self.fsm = FSM(self.m, self.name, self.clk, self.rst)
6561

@@ -73,11 +69,7 @@ def extend(self, fsm, *args, **kwargs):
7369
""" extend a given thread FSM """
7470

7571
frame = inspect.currentframe()
76-
_locals = frame.f_back.f_locals
77-
78-
self.local_objects = OrderedDict()
79-
for key, value in _locals.items():
80-
self.local_objects[key] = value
72+
self.start_frame = frame.f_back
8173

8274
self._synthesize_start_fsm(args, kwargs, fsm)
8375

@@ -201,13 +193,10 @@ def _synthesize_start_fsm(self, args, kwargs, fsm=None):
201193

202194
functions = self._get_functions()
203195

204-
local_objects = {}
205-
for key, value in self.local_objects.items():
206-
local_objects[key] = value
207-
208196
cvisitor = compiler.CompileVisitor(self.m, self.name, self.clk, self.rst, fsm,
209197
functions, self.intrinsic_functions,
210-
self.intrinsic_methods, local_objects,
198+
self.intrinsic_methods,
199+
self.start_frame,
211200
datawidth=self.datawidth)
212201

213202
text = textwrap.dedent(inspect.getsource(self.targ))
@@ -268,13 +257,10 @@ def _synthesize_run_fsm(self, parent_fsm, args, kwargs, cond=None):
268257

269258
functions = self._get_functions()
270259

271-
local_objects = {}
272-
for key, value in self.local_objects.items():
273-
local_objects[key] = value
274-
275260
cvisitor = compiler.CompileVisitor(self.m, self.name, self.clk, self.rst, self.fsm,
276261
functions, self.intrinsic_functions,
277-
self.intrinsic_methods, local_objects,
262+
self.intrinsic_methods,
263+
self.start_frame,
278264
datawidth=self.datawidth)
279265

280266
text = textwrap.dedent(inspect.getsource(self.targ))

0 commit comments

Comments
 (0)