Skip to content

Commit a3a9fa7

Browse files
committed
Mux is an alias of Cond.
1 parent 2b34bcb commit a3a9fa7

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

tests/core/mux/Makefile

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 *.out tmp.v uut.vcd

tests/core/mux/mux.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.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+
count( Mux(count==1023, 0, count + 1) )
24+
))
25+
26+
m.Always(Posedge(clk))(
27+
If(rst)(
28+
led(0)
29+
).Else(
30+
led( Mux(count==1024-1, led+1, led) )
31+
))
32+
33+
return m
34+
35+
if __name__ == '__main__':
36+
led = mkLed()
37+
verilog = led.to_verilog('')
38+
print(verilog)

tests/core/mux/test_mux.py

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

veriloggen/core/vtypes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,9 @@ def bit_length(self):
726726

727727
def __str__(self):
728728
return ''.join(['(', str(self.condition), ')?', str(self.true_value), ' : ', str(self.false_value)])
729+
730+
# Alias of Cond
731+
Mux = Cond
729732

730733
#-------------------------------------------------------------------------------
731734
class Sensitive(VeriloggenNode):

0 commit comments

Comments
 (0)