Skip to content

Commit 0c5c5eb

Browse files
committed
lib.pipeline is updated: Accumulator is supported.
1 parent ff1d013 commit 0c5c5eb

File tree

33 files changed

+1077
-8
lines changed

33 files changed

+1077
-8
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import sys
2+
import os
3+
4+
from veriloggen import *
5+
6+
def mkLed():
7+
m = Module('blinkled')
8+
clk = m.Input('CLK')
9+
rst = m.Input('RST')
10+
x = m.Input('x', 32)
11+
y = m.Output('y', 32)
12+
prst = m.Input('prst')
13+
14+
pipe = lib.Pipeline(m, 'pipe')
15+
16+
px = pipe.input(x)
17+
psum = pipe.acc_add(px, initval=0, resetcond=prst)
18+
psum.output(y)
19+
20+
pipe.make_always(clk, rst)
21+
22+
return m
23+
24+
def mkTest(numports=8):
25+
m = Module('test')
26+
27+
# target instance
28+
led = mkLed()
29+
30+
# copy paras and ports
31+
params = m.copy_params(led)
32+
ports = m.copy_sim_ports(led)
33+
34+
clk = ports['CLK']
35+
rst = ports['RST']
36+
x = ports['x']
37+
y = ports['y']
38+
prst = ports['prst']
39+
40+
uut = m.Instance(led, 'uut',
41+
params=m.connect_params(led),
42+
ports=m.connect_ports(led))
43+
44+
reset_done = m.Reg('reset_done', initval=0)
45+
46+
reset_stmt = []
47+
reset_stmt.append( reset_done(0) )
48+
reset_stmt.append( prst(0) )
49+
reset_stmt.append( x(0) )
50+
51+
lib.simulation.setup_waveform(m, uut)
52+
lib.simulation.setup_clock(m, clk, hperiod=5)
53+
init = lib.simulation.setup_reset(m, rst, reset_stmt, period=100)
54+
55+
nclk = lib.simulation.next_clock
56+
57+
init.add(
58+
Delay(1000),
59+
reset_done(1),
60+
nclk(clk),
61+
Delay(10000),
62+
Systask('finish'),
63+
)
64+
65+
x_count = m.TmpReg(32, initval=0)
66+
67+
xfsm = lib.FSM(m, 'xfsm')
68+
xfsm.goto_next(cond=reset_done)
69+
xfsm.add(x.inc())
70+
xfsm.add(x_count.inc())
71+
xfsm.goto_next(cond=x_count==10)
72+
xfsm.add(x(0))
73+
for i in range(5):
74+
xfsm.goto_next()
75+
xfsm.add(Systask('finish'))
76+
77+
xfsm.make_always(clk, rst)
78+
79+
80+
m.Always(Posedge(clk))(
81+
If(reset_done)(
82+
Systask('display', 'x=%d', x),
83+
Systask('display', 'y=%d', y)
84+
)
85+
)
86+
87+
return m
88+
89+
if __name__ == '__main__':
90+
test = mkTest()
91+
verilog = test.to_verilog('tmp.v')
92+
print(verilog)
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import led
2+
3+
expected_verilog = """
4+
module test;
5+
reg CLK;
6+
reg RST;
7+
reg [32-1:0] x;
8+
wire [32-1:0] y;
9+
reg prst;
10+
11+
blinkled
12+
uut
13+
(
14+
.CLK(CLK),
15+
.RST(RST),
16+
.x(x),
17+
.y(y),
18+
.prst(prst)
19+
);
20+
21+
reg reset_done;
22+
23+
initial begin
24+
$dumpfile("uut.vcd");
25+
$dumpvars(0, uut);
26+
end
27+
28+
29+
initial begin
30+
CLK = 0;
31+
forever begin
32+
#5 CLK = (!CLK);
33+
end
34+
end
35+
36+
37+
initial begin
38+
RST = 0;
39+
reset_done = 0;
40+
prst = 0;
41+
x = 0;
42+
#100;
43+
RST = 1;
44+
#100;
45+
RST = 0;
46+
#1000;
47+
reset_done = 1;
48+
@(posedge CLK);
49+
#1;
50+
#10000;
51+
$finish;
52+
end
53+
54+
reg [32-1:0] _tmp_0;
55+
reg [32-1:0] xfsm;
56+
localparam xfsm_init = 0;
57+
localparam xfsm_1 = 1;
58+
localparam xfsm_2 = 2;
59+
localparam xfsm_3 = 3;
60+
localparam xfsm_4 = 4;
61+
localparam xfsm_5 = 5;
62+
localparam xfsm_6 = 6;
63+
localparam xfsm_7 = 7;
64+
65+
always @(posedge CLK) begin
66+
if(RST) begin
67+
xfsm <= xfsm_init;
68+
_tmp_0 <= 0;
69+
end else begin
70+
case(xfsm)
71+
xfsm_init: begin
72+
if(reset_done) begin
73+
xfsm <= xfsm_1;
74+
end
75+
end
76+
xfsm_1: begin
77+
x <= (x + 1);
78+
_tmp_0 <= (_tmp_0 + 1);
79+
if((_tmp_0 == 10)) begin
80+
xfsm <= xfsm_2;
81+
end
82+
end
83+
xfsm_2: begin
84+
x <= 0;
85+
xfsm <= xfsm_3;
86+
end
87+
xfsm_3: begin
88+
xfsm <= xfsm_4;
89+
end
90+
xfsm_4: begin
91+
xfsm <= xfsm_5;
92+
end
93+
xfsm_5: begin
94+
xfsm <= xfsm_6;
95+
end
96+
xfsm_6: begin
97+
xfsm <= xfsm_7;
98+
end
99+
xfsm_7: begin
100+
$finish;
101+
end
102+
endcase
103+
end
104+
end
105+
106+
always @(posedge CLK) begin
107+
if(reset_done) begin
108+
$display("x=%d", x);
109+
$display("y=%d", y);
110+
end
111+
end
112+
113+
endmodule
114+
115+
116+
module blinkled
117+
(
118+
input CLK,
119+
input RST,
120+
input [32-1:0] x,
121+
output [32-1:0] y,
122+
input prst
123+
);
124+
125+
reg [32-1:0] _pipe_data_0;
126+
reg [32-1:0] _pipe_data_1;
127+
assign y = _pipe_data_1;
128+
129+
always @(posedge CLK) begin
130+
if(RST) begin
131+
_pipe_data_0 <= 0;
132+
_pipe_data_1 <= 0;
133+
end else begin
134+
_pipe_data_0 <= _pipe_data_0 + x;
135+
if(prst) begin
136+
_pipe_data_0 <= 0;
137+
end
138+
_pipe_data_1 <= _pipe_data_0;
139+
end
140+
end
141+
142+
endmodule
143+
"""
144+
145+
def test_led():
146+
test_module = led.mkTest()
147+
code = test_module.to_verilog()
148+
149+
from pyverilog.vparser.parser import VerilogParser
150+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
151+
parser = VerilogParser()
152+
expected_ast = parser.parse(expected_verilog)
153+
codegen = ASTCodeGenerator()
154+
expected_code = codegen.visit(expected_ast)
155+
156+
assert(expected_code == code)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import sys
2+
import os
3+
4+
from veriloggen import *
5+
6+
def mkLed():
7+
m = Module('blinkled')
8+
clk = m.Input('CLK')
9+
rst = m.Input('RST')
10+
x = m.Input('x', 32)
11+
vx = m.Input('vx')
12+
y = m.Output('y', 32)
13+
vy = m.Output('vy')
14+
prst = m.Input('prst')
15+
16+
pipe = lib.Pipeline(m, 'pipe')
17+
18+
px = pipe.input(x, valid=vx)
19+
psum = pipe.acc_add(px, initval=0, resetcond=prst)
20+
psum.output(y, valid=vy)
21+
22+
pipe.make_always(clk, rst)
23+
24+
return m
25+
26+
def mkTest(numports=8):
27+
m = Module('test')
28+
29+
# target instance
30+
led = mkLed()
31+
32+
# copy paras and ports
33+
params = m.copy_params(led)
34+
ports = m.copy_sim_ports(led)
35+
36+
clk = ports['CLK']
37+
rst = ports['RST']
38+
x = ports['x']
39+
vx = ports['vx']
40+
y = ports['y']
41+
vy = ports['vy']
42+
prst = ports['prst']
43+
44+
uut = m.Instance(led, 'uut',
45+
params=m.connect_params(led),
46+
ports=m.connect_ports(led))
47+
48+
reset_done = m.Reg('reset_done', initval=0)
49+
50+
reset_stmt = []
51+
reset_stmt.append( reset_done(0) )
52+
reset_stmt.append( prst(0) )
53+
reset_stmt.append( x(0) )
54+
reset_stmt.append( vx(0) )
55+
56+
lib.simulation.setup_waveform(m, uut)
57+
lib.simulation.setup_clock(m, clk, hperiod=5)
58+
init = lib.simulation.setup_reset(m, rst, reset_stmt, period=100)
59+
60+
nclk = lib.simulation.next_clock
61+
62+
init.add(
63+
Delay(1000),
64+
reset_done(1),
65+
nclk(clk),
66+
Delay(10000),
67+
Systask('finish'),
68+
)
69+
70+
x_count = m.TmpReg(32, initval=0)
71+
72+
xfsm = lib.FSM(m, 'xfsm')
73+
xfsm.add(vx(0))
74+
xfsm.goto_next(cond=reset_done)
75+
xfsm.add(vx(1))
76+
xfsm.add(x.inc())
77+
xfsm.add(x_count.inc())
78+
xfsm.goto_next(cond=x_count==10)
79+
xfsm.add(vx(0))
80+
81+
xfsm.make_always(clk, rst)
82+
83+
84+
m.Always(Posedge(clk))(
85+
If(reset_done)(
86+
If(vx)(
87+
Systask('display', 'x=%d', x)
88+
),
89+
If(vy)(
90+
Systask('display', 'y=%d', y)
91+
)
92+
)
93+
)
94+
95+
return m
96+
97+
if __name__ == '__main__':
98+
test = mkTest()
99+
verilog = test.to_verilog('tmp.v')
100+
print(verilog)

0 commit comments

Comments
 (0)