Skip to content

Commit 1db4d72

Browse files
committed
Naming rule is changed
1 parent a11f515 commit 1db4d72

File tree

12 files changed

+278
-480
lines changed

12 files changed

+278
-480
lines changed

sample/bram/bram.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
from veriloggen import *
66

77
class BramInterface(Interface):
8-
def __init__(self, m, prefix, postfix, addrwidth, datawidth, direction):
8+
def __init__(self, m, prefix='', postfix='', addrwidth=10, datawidth=32, direction='in'):
99
Interface.__init__(self, m, prefix, postfix)
1010

11+
if direction != 'in' and direction != 'out':
12+
raise ValueError("direction should be 'in or 'out''")
1113
self.direction = direction
14+
1215
self.addrwidth = self.Parameter('ADDR_WIDTH', addrwidth)
1316
self.datawidth = self.Parameter('DATA_WIDTH', datawidth)
1417

@@ -27,7 +30,7 @@ def mkBram(name):
2730
datawidth = m.Parameter('DATA_WIDTH', 32)
2831

2932
clk = m.Input('CLK')
30-
bramif = BramInterface(m, '', '', addrwidth, datawidth, direction='in')
33+
bramif = BramInterface(m, addrwidth=addrwidth, datawidth=datawidth, direction='in')
3134

3235
d_addr = m.Reg('d_' + bramif.addr.name, datawidth)
3336
mem = m.Reg('mem', datawidth, Int(2)**addrwidth)
@@ -47,29 +50,30 @@ def mkTop():
4750
datawidth = m.Parameter('DATA_WIDTH', 32)
4851
clk = m.Input('CLK')
4952
rst = m.Input('RST')
50-
bramif = BramInterface(m, 'bram_', '', addrwidth, datawidth, direction='out')
53+
bramif = BramInterface(m, prefix='bram_',
54+
addrwidth=addrwidth, datawidth=datawidth, direction='out')
5155

5256
params = collections.OrderedDict()
53-
params.update(bramif.connectAllParameters())
57+
params.update(bramif.connect_all_parameters())
5458

5559
ports = collections.OrderedDict()
5660
ports.update(clk.connect())
57-
ports.update(bramif.connectAllPorts())
61+
ports.update(bramif.connect_all_ports())
5862

5963
m.Instance(bram, 'inst_bram', params, ports)
6064

6165
fsm = lib.FSM(m, 'fsm')
6266
m.Always(Posedge(clk))(
6367
If(rst)(
6468
bramif.addr(0), bramif.datain(0), bramif.write(0), fsm.next(0)
65-
).els(
69+
).Else(
6670
fsm( bramif.addr(0), bramif.datain(0), bramif.write(0), fsm.next() ),
6771
fsm( bramif.datain(bramif.datain + 4), fsm.next() ),
6872
fsm( bramif.write(0), fsm.next() ),
6973
fsm(
7074
If(bramif.addr == 128)(
7175
bramif.addr(0), fsm.next(0)
72-
).els(
76+
).Else(
7377
bramif.addr(bramif.addr + 1), fsm.next(1)
7478
))
7579
))
@@ -79,5 +83,5 @@ def mkTop():
7983
#-------------------------------------------------------------------------------
8084
bram = mkBram('my_')
8185
top = mkTop()
82-
verilog = ''.join( (bram.toVerilog(), top.toVerilog()) )
86+
verilog = ''.join( (bram.to_verilog(), top.to_verilog()) )
8387
print(verilog)

sample/led-class/led.py

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,66 +14,20 @@ def __init__(self, name='blinkled'):
1414
self.Always(Posedge(self.clk))(
1515
If(self.rst)(
1616
self.count(0)
17-
).els(
17+
).Else(
1818
self.count(self.count + 1)
1919
))
2020

2121
self.Always(Posedge(self.clk))(
2222
If(self.rst)(
2323
self.led(0)
24-
).els(
24+
).Else(
2525
If(self.count == 1024 - 1)(
2626
self.led(self.led + 1)
2727
)
2828
))
2929

3030
#-------------------------------------------------------------------------------
3131
led = Led()
32-
verilog = led.toVerilog()
32+
verilog = led.to_verilog()
3333
print(verilog)
34-
35-
#-------------------------------------------------------------------------------
36-
expected_verilog = """
37-
module blinkled #
38-
(
39-
parameter WIDTH = 8
40-
)
41-
(
42-
input CLK,
43-
input RST,
44-
output reg [WIDTH-1:0] LED
45-
);
46-
reg [32-1:0] count;
47-
48-
always @(posedge CLK) begin
49-
if(RST) begin
50-
count <= 0;
51-
end else begin
52-
count <= count + 1;
53-
end
54-
end
55-
always @(posedge CLK) begin
56-
if(RST) begin
57-
LED <= 0;
58-
end else begin
59-
if(count == 1023) begin
60-
LED <= LED + 1;
61-
end
62-
end
63-
end
64-
endmodule
65-
"""
66-
67-
#from pyverilog.vparser.parser import VerilogParser
68-
#from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
69-
#parser = VerilogParser()
70-
#expected_verilog_ast = parser.parse(expected_verilog)
71-
#codegen = ASTCodeGenerator()
72-
#expected_verilog_code = codegen.visit(expected_verilog_ast)
73-
74-
#print('// Sample Verilog code -> AST -> Verilog code')
75-
#print(expected_verilog_code)
76-
77-
#import difflib
78-
#diff = difflib.unified_diff(verilog.splitlines(), expected_verilog_code.splitlines())
79-
#print('\n'.join(list(diff)))

sample/led-fsm/led.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ def mkLed():
1818
count(0),
1919
led(0),
2020
fsm.next(0)
21-
).els(
21+
).Else(
2222
fsm(count(count + 1), fsm.next()),
2323
fsm(count(count + 2), fsm.next()),
2424
fsm(count(count + 3), fsm.next()),
25-
fsm(If(count < 1024)( fsm.next(0) ).els( fsm.next() )),
25+
fsm(If(count < 1024)( fsm.next(0) ).Else( fsm.next() )),
2626
fsm(led(led + 1), fsm.next(0))
2727
))
2828

2929
return m
3030

3131
led = mkLed()
32-
verilog = led.toVerilog()
32+
verilog = led.to_verilog("tmp.v")
3333
print(verilog)

sample/led/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ check:
2222

2323
.PHONY: clean
2424
clean:
25-
rm -rf *.pyc __pycache__ parsetab.py *.out
25+
rm -rf *.pyc __pycache__ parsetab.py *.out tmp.v

sample/led/led.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ def mkLed():
1313
m.Always(Posedge(clk))(
1414
If(rst)(
1515
count(0)
16-
).els(
16+
).Else(
1717
count(count + 1)
1818
))
1919

2020
m.Always(Posedge(clk))(
2121
If(rst)(
2222
led(0)
23-
).els(
23+
).Else(
2424
If(count == 1024 - 1)(
2525
led(led + 1)
2626
)
@@ -30,7 +30,8 @@ def mkLed():
3030

3131
#-------------------------------------------------------------------------------
3232
led = mkLed()
33-
verilog = led.toVerilog()
33+
## if filename is not None: the generated source code is written to the file.
34+
verilog = led.to_verilog(filename='tmp.v')
3435
print(verilog)
3536

3637
#-------------------------------------------------------------------------------
@@ -65,16 +66,13 @@ def mkLed():
6566
endmodule
6667
"""
6768

68-
#from pyverilog.vparser.parser import VerilogParser
69-
#from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
70-
#parser = VerilogParser()
71-
#expected_verilog_ast = parser.parse(expected_verilog)
72-
#codegen = ASTCodeGenerator()
73-
#expected_verilog_code = codegen.visit(expected_verilog_ast)
69+
from pyverilog.vparser.parser import VerilogParser
70+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
71+
parser = VerilogParser()
72+
expected_verilog_ast = parser.parse(expected_verilog)
73+
codegen = ASTCodeGenerator()
74+
expected_verilog_code = codegen.visit(expected_verilog_ast)
7475

75-
#print('// Sample Verilog code -> AST -> Verilog code')
76-
#print(expected_verilog_code)
77-
78-
#import difflib
79-
#diff = difflib.unified_diff(verilog.splitlines(), expected_verilog_code.splitlines())
80-
#print('\n'.join(list(diff)))
76+
import difflib
77+
diff = difflib.unified_diff(verilog.splitlines(), expected_verilog_code.splitlines())
78+
print('\n'.join(list(diff)))

veriloggen/Makefile

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
PYTHON = python3
2-
#OPT=-m pdb
3-
4-
VERILOGGEN=veriloggen.py
5-
#SRCS=
6-
71
.PHONY: clean
82
clean:
93
make clean -C utils
104
rm -rf *.pyc __pycache__ parsetab.py *.out *.html
11-
12-
.PHONY: run
13-
run:
14-
$(PYTHON) $(OPT) $(VERILOGGEN) $(SRCS)

veriloggen/__init__.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
import sys
1+
#-------------------------------------------------------------------------------
2+
# veriloggen.py
3+
#
4+
# Veriloggen: A library for constructing a Verilog HDL source code in Python
5+
#
6+
# Copyright (C) 2015, Shinya Takamaeda-Yamazaki
7+
# License: Apache 2.0
8+
#-------------------------------------------------------------------------------
9+
210
import os
3-
if sys.version_info[0] < 3:
4-
import utils
5-
from veriloggen import *
6-
else:
7-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
8-
from veriloggen.veriloggen import *
11+
import sys
12+
13+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
14+
15+
import utils.version
16+
import vtypes
17+
18+
from module import Module
19+
from interface import Interface
20+
from vtypes import *
21+
22+
import lib

veriloggen/interface.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ def __init__(self, module, prefix='', postfix=''):
1313
self.prefix = prefix
1414
self.postfix = postfix
1515

16+
#---------------------------------------------------------------------------
1617
def get_name(self, name):
1718
return self.prefix + name + self.postfix
1819

1920
def get_basename(self, name):
2021
return re.sub(r'' + self.postfix + '$', '', name.replace(self.prefix, '', 1))
2122

22-
23+
#---------------------------------------------------------------------------
2324
def Input(self, name, width=1, length=None, signed=False, value=None):
2425
new_name = self.get_name(name)
2526
return self.module.Input(name, width, length, signed, value)
@@ -52,7 +53,8 @@ def Localparam(self, name, value, width=None, signed=False):
5253
new_name = self.get_name(name)
5354
return self.module.Localparam(name, value, width, signed)
5455

55-
def connectAllPorts(self, prefix='', postfix=''):
56+
#---------------------------------------------------------------------------
57+
def connect_all_ports(self, prefix='', postfix=''):
5658
inputs = [ s for s in self.__dir__() if isinstance(getattr(self, s), vtypes.Input) ]
5759
outputs = [ s for s in self.__dir__() if isinstance(getattr(self, s), vtypes.Output) ]
5860
inouts = [ s for s in self.__dir__() if isinstance(getattr(self, s), vtypes.Inout) ]
@@ -76,7 +78,8 @@ def connectAllPorts(self, prefix='', postfix=''):
7678
ret[name] = getattr(self, p)
7779
return ret
7880

79-
def connectAllParameters(self, prefix='', postfix=''):
81+
#---------------------------------------------------------------------------
82+
def connect_all_parameters(self, prefix='', postfix=''):
8083
parameters = [ s for s in self.__dir__() if isinstance(getattr(self, s), vtypes.Parameter) ]
8184
localparams = [ s for s in self.__dir__() if isinstance(getattr(self, s), vtypes.Localparam) ]
8285
ret = collections.OrderedDict()
@@ -88,13 +91,13 @@ def connectAllParameters(self, prefix='', postfix=''):
8891
ret[name] = getattr(self, p)
8992
return ret
9093

91-
def getPorts(self):
94+
def get_ports(self):
9295
return ([ getattr(self, s) for s in self.__dir__() if isinstance(getattr(self, s), vtypes.Input) ] +
9396
[ getattr(self, s) for s in self.__dir__() if isinstance(getattr(self, s), vtypes.Output) ] +
9497
[ getattr(self, s) for s in self.__dir__() if isinstance(getattr(self, s), vtypes.Inout) ] +
9598
[ getattr(self, s) for s in self.__dir__() if isinstance(getattr(self, s), vtypes.Reg) ] +
9699
[ getattr(self, s) for s in self.__dir__() if isinstance(getattr(self, s), vtypes.Wire) ])
97100

98-
def getParameters(self):
101+
def get_parameters(self):
99102
return ([ getattr(self, s) for s in self.__dir__() if isinstance(getattr(self, s), vtypes.Parameter) ] +
100103
[ getattr(self, s) for s in self.__dir__() if isinstance(getattr(self, s), vtypes.Localparam) ])

veriloggen/module.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
66

77
import vtypes
8-
import toverilog
8+
import to_verilog
99

1010
class Module(object):
1111
""" Verilog Module class """
12-
def __init__(self, name):
13-
self.name = name
12+
def __init__(self, name=None):
13+
self.name = name if name is not None else self.__class__.__name__
1414
self.variable = collections.OrderedDict()
1515
self.io_variable = collections.OrderedDict()
1616
self.constant = collections.OrderedDict()
@@ -79,31 +79,30 @@ def Instance(self, module, instname, params, ports):
7979
return t
8080

8181
#---------------------------------------------------------------------------
82-
def isReg(self, name):
82+
def is_reg(self, name):
8383
if name not in self.variable: return False
8484
if isinstance(self.variable[name], vtypes.Reg): return True
8585
return False
8686

87-
def isWire(self, name):
87+
def is_wire(self, name):
8888
if name not in self.variable and name not in self.io_variable: return False
8989
if name in self.variable and isinstance(self.variable[name], vtypes.Wire): return True
9090
if name in self.variable and isinstance(self.variable[name], vtypes.Reg): return False
9191
if name in self.io_variable: return True
9292
return False
9393

94-
def isOutput(self, name):
94+
def is_output(self, name):
9595
if name not in self.io_variable: return False
9696
if isinstance(self.io_variable[name], vtypes.Output): return True
9797
return False
9898

9999
#---------------------------------------------------------------------------
100-
def getIO(self):
100+
def get_io(self):
101101
return self.io_variable
102102

103-
def getIOname(self):
103+
def get_io_name(self):
104104
return tuple(self.io_variable.keys())
105105

106106
#---------------------------------------------------------------------------
107-
def toVerilog(self):
108-
return toverilog.toVerilog(self)
109-
107+
def to_verilog(self, filename=None):
108+
return to_verilog.to_verilog(self, filename)

0 commit comments

Comments
 (0)