1
0
mirror of https://github.com/myhdl/myhdl.git synced 2024-12-14 07:44:38 +08:00

Support for string.Template with vhdl_code and verilog_code attributes

This commit is contained in:
Jan Decaluwe 2010-07-05 15:24:17 +02:00
parent f44fcfebc3
commit 9c2b13419b
3 changed files with 40 additions and 26 deletions

View File

@ -96,7 +96,7 @@ class _UserCode(object):
def __str__(self):
try:
code = self.code % self.namespace
code = self._interpolate()
except:
type, value, tb = sys.exc_info()
info = "in file %s, function %s starting on line %s:\n " % \
@ -105,6 +105,13 @@ class _UserCode(object):
self.raiseError(msg, info)
code = "\n%s\n" % code
return code
def _interpolate(self):
return string.Template(self.code).substitute(self.namespace)
class _UserCodeDepr(_UserCode):
def _interpolate(self):
return self.code % self.namespace
class _UserVerilogCode(_UserCode):
def raiseError(self, msg, info):
@ -114,6 +121,13 @@ class _UserVhdlCode(_UserCode):
def raiseError(self, msg, info):
raise ToVHDLError("Error in user defined VHDL code", msg, info)
class _UserVerilogCodeDepr(_UserVerilogCode, _UserCodeDepr):
pass
class _UserVhdlCodeDepr(_UserVhdlCode, _UserCodeDepr):
pass
class _UserVerilogInstance(_UserVerilogCode):
def __str__(self):
args = inspect.getargspec(self.func)[0]
@ -147,8 +161,8 @@ class _UserVhdlInstance(_UserVhdlCode):
def _addUserCode(specs, arg, funcname, func, frame):
classMap = {
'__verilog__' : _UserVerilogCode,
'__vhdl__' :_UserVhdlCode,
'__verilog__' : _UserVerilogCodeDepr,
'__vhdl__' :_UserVhdlCodeDepr,
'verilog_code' : _UserVerilogCode,
'vhdl_code' :_UserVhdlCode,
'verilog_instance' : _UserVerilogInstance,

View File

@ -72,12 +72,12 @@ def inc(count, enable, clock, reset, n):
inc.vhdl_code = \
"""
process (%(clock)s, %(reset)s) begin
process ($clock, $reset) begin
if (reset = '0') then
%(count)s <= (others => '0');
elsif rising_edge(%(clock)s) then
$count <= (others => '0');
elsif rising_edge($clock) then
if (enable = '1') then
%(count)s <= (%(count)s + 1) mod %(n)s;
$count <= ($count + 1) mod $n;
end if;
end if;
end process;
@ -102,13 +102,13 @@ def incErr(count, enable, clock, reset, n):
incErr.vhdl_code = \
"""
always @(posedge %(clock)s, negedge %(reset)s) begin
always @(posedge $clock, negedge $reset) begin
if (reset == 0) begin
%(count)s <= 0;
$count <= 0;
end
else begin
if (enable) begin
%(count)s <= (%(countq)s + 1) %% %(n)s;
$count <= ($countq + 1) %% $n;
end
end
end
@ -130,7 +130,7 @@ def inc_comb(nextCount, count, n):
inc_comb.vhdl_code =\
"""
%(nextCount)s <= (%(count)s + 1) mod %(n)s;
$nextCount <= ($count + 1) mod $n;
"""
return logic
@ -149,12 +149,12 @@ def inc_seq(count, nextCount, enable, clock, reset):
inc_seq.vhdl_code = \
"""
process (%(clock)s, %(reset)s) begin
process ($clock, $reset) begin
if (reset = '0') then
%(count)s <= (others => '0');
elsif rising_edge(%(clock)s) then
$count <= (others => '0');
elsif rising_edge($clock) then
if (enable = '1') then
%(count)s <= %(nextCount)s;
$count <= $nextCount;
end if;
end if;
end process;

View File

@ -41,7 +41,7 @@ def incGen(count, enable, clock, reset, n):
""" Generator with __verilog__ is not permitted """
@instance
def logic():
__verilog__ = "Template string"
incGen.verilog_code = "Template string"
while 1:
yield clock.posedge, reset.negedge
if reset == ACTIVE_LOW:
@ -75,13 +75,13 @@ def inc(count, enable, clock, reset, n):
inc.verilog_code = \
"""
always @(posedge %(clock)s, negedge %(reset)s) begin
always @(posedge $clock, negedge $reset) begin
if (reset == 0) begin
%(count)s <= 0;
$count <= 0;
end
else begin
if (enable) begin
%(count)s <= (%(count)s + 1) %% %(n)s;
$count <= ($count + 1) % $n;
end
end
end
@ -106,13 +106,13 @@ def incErr(count, enable, clock, reset, n):
incErr.verilog_code = \
"""
always @(posedge %(clock)s, negedge %(reset)s) begin
always @(posedge $clock, negedge $reset) begin
if (reset == 0) begin
%(count)s <= 0;
$count <= 0;
end
else begin
if (enable) begin
%(count)s <= (%(countq)s + 1) %% %(n)s;
$count <= ($countq + 1) % $n;
end
end
end
@ -134,7 +134,7 @@ def inc_comb(nextCount, count, n):
inc_comb.verilog_code =\
"""
assign %(nextCount)s = (%(count)s + 1) %% %(n)s;
assign $nextCount = ($count + 1) % $n;
"""
return logic
@ -153,13 +153,13 @@ def inc_seq(count, nextCount, enable, clock, reset):
inc_seq.verilog_code = \
"""
always @(posedge %(clock)s, negedge %(reset)s) begin
always @(posedge $clock, negedge $reset) begin
if (reset == 0) begin
%(count)s <= 0;
$count <= 0;
end
else begin
if (enable) begin
%(count)s <= %(nextCount)s;
$count <= $nextCount;
end
end
end