Skip to content

Commit 8b96835

Browse files
committed
bug fix of Shift Right Arithmetic op in Thread
1 parent ff6ae06 commit 8b96835

File tree

4 files changed

+202
-2
lines changed

4 files changed

+202
-2
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: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import veriloggen
4+
import thread_sra
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+
CLK = 0;
24+
forever begin
25+
#5 CLK = !CLK;
26+
end
27+
end
28+
29+
30+
initial begin
31+
RST = 0;
32+
#100;
33+
RST = 1;
34+
#100;
35+
RST = 0;
36+
#10000;
37+
$finish;
38+
end
39+
40+
41+
endmodule
42+
43+
44+
45+
module blinkled
46+
(
47+
input CLK,
48+
input RST,
49+
output reg [8-1:0] LED
50+
);
51+
52+
reg [32-1:0] th_blink;
53+
localparam th_blink_init = 0;
54+
reg signed [32-1:0] _th_blink_a_0;
55+
reg signed [32-1:0] _th_blink_b_1;
56+
localparam th_blink_1 = 1;
57+
localparam th_blink_2 = 2;
58+
localparam th_blink_3 = 3;
59+
localparam th_blink_4 = 4;
60+
61+
always @(posedge CLK) begin
62+
if(RST) begin
63+
th_blink <= th_blink_init;
64+
_th_blink_a_0 <= 0;
65+
_th_blink_b_1 <= 0;
66+
end else begin
67+
case(th_blink)
68+
th_blink_init: begin
69+
th_blink <= th_blink_1;
70+
end
71+
th_blink_1: begin
72+
_th_blink_a_0 <= -'sd16;
73+
th_blink <= th_blink_2;
74+
end
75+
th_blink_2: begin
76+
_th_blink_b_1 <= _th_blink_a_0 >>> 2;
77+
th_blink <= th_blink_3;
78+
end
79+
th_blink_3: begin
80+
$display("%d %d", _th_blink_a_0, _th_blink_b_1);
81+
th_blink <= th_blink_4;
82+
end
83+
endcase
84+
end
85+
end
86+
87+
88+
endmodule
89+
"""
90+
91+
92+
def test():
93+
veriloggen.reset()
94+
test_module = thread_sra.mkTest()
95+
code = test_module.to_verilog()
96+
97+
from pyverilog.vparser.parser import VerilogParser
98+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
99+
parser = VerilogParser()
100+
expected_ast = parser.parse(expected_verilog)
101+
codegen = ASTCodeGenerator()
102+
expected_code = codegen.visit(expected_ast)
103+
104+
assert(expected_code == code)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
14+
def mkLed():
15+
m = Module('blinkled')
16+
clk = m.Input('CLK')
17+
rst = m.Input('RST')
18+
led = m.OutputReg('LED', 8, initval=0)
19+
20+
def blink():
21+
a = -16
22+
b = a >> 2
23+
print(a, b)
24+
25+
th = vthread.Thread(m, 'th_blink', clk, rst, blink)
26+
fsm = th.start()
27+
28+
return m
29+
30+
31+
def mkTest():
32+
m = Module('test')
33+
34+
# target instance
35+
led = mkLed()
36+
37+
# copy paras and ports
38+
params = m.copy_params(led)
39+
ports = m.copy_sim_ports(led)
40+
41+
clk = ports['CLK']
42+
rst = ports['RST']
43+
44+
uut = m.Instance(led, 'uut',
45+
params=m.connect_params(led),
46+
ports=m.connect_ports(led))
47+
48+
#simulation.setup_waveform(m, uut)
49+
simulation.setup_clock(m, clk, hperiod=5)
50+
init = simulation.setup_reset(m, rst, m.make_reset(), period=100)
51+
52+
init.add(
53+
Delay(10000),
54+
Systask('finish'),
55+
)
56+
57+
return m
58+
59+
60+
if __name__ == '__main__':
61+
test = mkTest()
62+
verilog = test.to_verilog('tmp.v')
63+
print(verilog)
64+
65+
sim = simulation.Simulator(test)
66+
rslt = sim.run()
67+
print(rslt)

veriloggen/thread/operator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
ast.Mod: vtypes.Mod,
1515
ast.Pow: vtypes.Power,
1616
ast.LShift: vtypes.Sll,
17-
ast.RShift: vtypes.Srl,
17+
ast.RShift: vtypes.Sra,
1818
ast.BitOr: vtypes.Or,
1919
ast.BitXor: vtypes.Xor,
2020
ast.BitAnd: vtypes.And,
@@ -51,7 +51,7 @@ def getVeriloggenOp(op):
5151
ast.Mod: '__mod__',
5252
ast.Pow: '__pow__',
5353
ast.LShift: '__lshift__',
54-
ast.RShift: '__rshift__',
54+
ast.RShift: None,
5555
ast.BitOr: '__or__',
5656
ast.BitXor: '__xor__',
5757
ast.BitAnd: '__and__',

0 commit comments

Comments
 (0)