mirror of
https://github.com/myhdl/myhdl.git
synced 2025-01-24 21:52:56 +08:00
Removed old modules
This commit is contained in:
parent
81d6d60457
commit
3106e0c198
@ -1,220 +0,0 @@
|
||||
# This file is part of the myhdl library, a Python package for using
|
||||
# Python as a Hardware Description Language.
|
||||
#
|
||||
# Copyright (C) 2003 Jan Decaluwe
|
||||
#
|
||||
# The myhdl library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public License as
|
||||
# published by the Free Software Foundation; either version 2.1 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This library is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
""" myhdl trace_sigs module.
|
||||
|
||||
"""
|
||||
|
||||
__author__ = "Jan Decaluwe <jan@jandecaluwe.com>"
|
||||
__revision__ = "$Revision$"
|
||||
__date__ = "$Date$"
|
||||
|
||||
from __future__ import generators
|
||||
|
||||
import sys
|
||||
from inspect import currentframe, getframeinfo, getouterframes
|
||||
import compiler
|
||||
import re
|
||||
import string
|
||||
import time
|
||||
from types import FunctionType
|
||||
import os
|
||||
path = os.path
|
||||
import shutil
|
||||
|
||||
from myhdl import _simulator, Signal, __version__
|
||||
from myhdl.util import _isGenSeq, _isgeneratorfunction
|
||||
|
||||
_tracing = 0
|
||||
|
||||
class Error(Exception):
|
||||
""" trace_sigs Error"""
|
||||
def __init__(self, arg=""):
|
||||
self.arg = arg
|
||||
def __str__(self):
|
||||
msg = self.__doc__
|
||||
if self.arg:
|
||||
msg = msg + ": " + str(self.arg)
|
||||
return msg
|
||||
|
||||
class TopLevelNameError(Error):
|
||||
"""result of trace_sigs call should be assigned to a top level name"""
|
||||
|
||||
class ArgTypeError(Error):
|
||||
"""trace_sigs first argument should be a classic function"""
|
||||
|
||||
class NoInstancesError(Error):
|
||||
"""trace_sigs returned no instances"""
|
||||
|
||||
re_assname = re.compile(r"^\s*(?P<assname>\w[\w\d]*)\s*=")
|
||||
|
||||
def trace_sigs(dut, *args, **kwargs):
|
||||
global _tracing
|
||||
if _tracing:
|
||||
return dut(*args, **kwargs) # skip
|
||||
if not callable(dut):
|
||||
raise ArgTypeError("got %s" % type(dut))
|
||||
if _isgeneratorfunction(dut):
|
||||
raise ArgTypeError("got generator function")
|
||||
_tracing = 1
|
||||
try:
|
||||
o = getouterframes(currentframe())[1]
|
||||
s = o[4][0]
|
||||
m = re_assname.match(s)
|
||||
name = None
|
||||
if m:
|
||||
name = m.group('assname')
|
||||
else:
|
||||
raise TopLevelNameError
|
||||
h = HierExtr(name, dut, *args, **kwargs)
|
||||
vcdpath = name + ".vcd"
|
||||
if path.exists(vcdpath):
|
||||
backup = vcdpath + '.' + str(path.getmtime(vcdpath))
|
||||
shutil.copyfile(vcdpath, backup)
|
||||
os.remove(vcdpath)
|
||||
vcdfile = open(vcdpath, 'w')
|
||||
_simulator._tracing = 1
|
||||
_simulator._tf = vcdfile
|
||||
_writeVcdHeader(vcdfile)
|
||||
_writeVcdSigs(vcdfile, h.instances)
|
||||
finally:
|
||||
_tracing = 0
|
||||
return h.m
|
||||
|
||||
|
||||
class HierExtr(object):
|
||||
|
||||
def __init__(self, name, dut, *args, **kwargs):
|
||||
self.names = [name]
|
||||
self.instances = instances = []
|
||||
self.level = 0
|
||||
sys.setprofile(self.extractor)
|
||||
try:
|
||||
_top = dut(*args, **kwargs)
|
||||
finally:
|
||||
sys.setprofile(None)
|
||||
if not instances:
|
||||
raise NoInstancesError
|
||||
self.m = _top
|
||||
instances.reverse()
|
||||
# print instances
|
||||
instances[0][1] = name
|
||||
|
||||
def extractor(self, frame, event, arg):
|
||||
if event == "call":
|
||||
o = getouterframes(frame)[1]
|
||||
s = o[4][0]
|
||||
m = re_assname.match(s)
|
||||
name = None
|
||||
if m:
|
||||
name = m.group('assname')
|
||||
self.names.append(name)
|
||||
if name:
|
||||
self.level += 1
|
||||
elif event == "return":
|
||||
name = self.names.pop()
|
||||
if name:
|
||||
if _isGenSeq(arg):
|
||||
sigdict = {}
|
||||
for dict in (frame.f_locals, frame.f_globals):
|
||||
for n, v in dict.items():
|
||||
if isinstance(v, Signal):
|
||||
sigdict[n] = v
|
||||
i = [self.level, name, sigdict]
|
||||
self.instances.append(i)
|
||||
self.level -= 1
|
||||
|
||||
|
||||
_codechars = ""
|
||||
for i in range(33, 127):
|
||||
_codechars += chr(i)
|
||||
_mod = len(_codechars)
|
||||
|
||||
def _genNameCode():
|
||||
n = 0
|
||||
while 1:
|
||||
yield _namecode(n)
|
||||
n += 1
|
||||
|
||||
def _namecode(n):
|
||||
q, r = divmod(n, _mod)
|
||||
code = _codechars[r]
|
||||
while q > 0:
|
||||
q, r = divmod(q, _mod)
|
||||
code = _codechars[r] + code
|
||||
return code
|
||||
|
||||
def _writeVcdHeader(f):
|
||||
print >> f, "$date"
|
||||
print >> f, " %s" % time.asctime()
|
||||
print >> f, "$end"
|
||||
print >> f, "$version"
|
||||
print >> f, " MyHDL %s" % __version__
|
||||
print >> f, "$end"
|
||||
print >> f, "$timesscale"
|
||||
print >> f, " 1ns"
|
||||
print >> f, "$end"
|
||||
print >> f
|
||||
|
||||
def _writeVcdSigs(f, instances):
|
||||
curlevel = 0
|
||||
namegen = _genNameCode()
|
||||
siglist = []
|
||||
for level, name, sigdict in instances:
|
||||
delta = curlevel - level
|
||||
curlevel = level
|
||||
assert(delta >= -1)
|
||||
if delta >= 0:
|
||||
for i in range(delta + 1):
|
||||
print >> f, "$upscope $end"
|
||||
print >> f, "$scope module %s $end" % name
|
||||
for n, s in sigdict.items():
|
||||
if not s._tracing:
|
||||
s._tracing = 1
|
||||
s._code = namegen.next()
|
||||
siglist.append(s)
|
||||
w = s._nrbits
|
||||
if w:
|
||||
if w == 1:
|
||||
print >> f, "$var reg 1 %s %s $end" % (s._code, n)
|
||||
else:
|
||||
print >> f, "$var reg %s %s %s $end" % (w, s.code, n)
|
||||
else:
|
||||
print >> f, "$var real 1 %s %s $end" % (s._code, n)
|
||||
for i in range(curlevel):
|
||||
print >> f, "$upscope $end"
|
||||
print >> f
|
||||
print >> f, "$enddefinitions $end"
|
||||
print >> f, "$dumpvars"
|
||||
for s in siglist:
|
||||
s._printVcd() # initial value
|
||||
print >> f, "$end"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
130
myhdl/util.py
130
myhdl/util.py
@ -1,130 +0,0 @@
|
||||
# This file is part of the myhdl library, a Python package for using
|
||||
# Python as a Hardware Description Language.
|
||||
#
|
||||
# Copyright (C) 2003 Jan Decaluwe
|
||||
#
|
||||
# The myhdl library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public License as
|
||||
# published by the Free Software Foundation; either version 2.1 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This library is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
""" myhdl utilility objects.
|
||||
|
||||
This module provides the following myhdl objects:
|
||||
downrange -- function that returns a downward range
|
||||
Error -- myhdl Error exception
|
||||
bin -- returns a binary string representation.
|
||||
The optional width specifies the desired string
|
||||
width: padding of the sign-bit is used.
|
||||
|
||||
"""
|
||||
|
||||
__author__ = "Jan Decaluwe <jan@jandecaluwe.com>"
|
||||
__revision__ = "$Revision$"
|
||||
__date__ = "$Date$"
|
||||
|
||||
import exceptions
|
||||
import sys
|
||||
import inspect
|
||||
import re
|
||||
from types import FunctionType, GeneratorType, ListType, TupleType
|
||||
import compiler
|
||||
|
||||
from myhdl import Cosimulation
|
||||
|
||||
|
||||
def downrange(start, stop=0):
|
||||
""" Return a downward range. """
|
||||
return range(start-1, stop-1, -1)
|
||||
|
||||
def _int2bitstring(num):
|
||||
if num == 0:
|
||||
return '0'
|
||||
if abs(num) == 1:
|
||||
return '1'
|
||||
return _int2bitstring(num // 2) + _int2bitstring(num % 2)
|
||||
|
||||
def bin(num, width=0):
|
||||
"""Return a binary string representation.
|
||||
|
||||
num -- number to convert
|
||||
Optional parameter:
|
||||
width -- specifies the desired string (sign bit padding)
|
||||
"""
|
||||
num = long(num)
|
||||
s = _int2bitstring(num)
|
||||
pad = '0'
|
||||
if num < 0:
|
||||
pad = '1'
|
||||
return (width - len(s)) * pad + s
|
||||
|
||||
class Error(Exception):
|
||||
pass
|
||||
|
||||
class StopSimulation(exceptions.Exception):
|
||||
""" Basic exception to stop a Simulation """
|
||||
pass
|
||||
|
||||
class SuspendSimulation(exceptions.Exception):
|
||||
""" Basic exception to suspend a Simulation """
|
||||
pass
|
||||
|
||||
def printExcInfo():
|
||||
kind, value = sys.exc_info()[:2]
|
||||
msg = str(kind)
|
||||
msg = msg[msg.rindex('.')+1:]
|
||||
if str(value):
|
||||
msg += ": %s" % value
|
||||
print msg
|
||||
|
||||
|
||||
def _isGenSeq(obj):
|
||||
if type(obj) in (GeneratorType, Cosimulation):
|
||||
return 1
|
||||
if not isinstance(obj, (ListType, TupleType)):
|
||||
return 0
|
||||
for e in obj:
|
||||
if not _isGenSeq(e):
|
||||
return 0
|
||||
return 1
|
||||
|
||||
def _isgeneratorfunction(obj):
|
||||
if type(obj) is FunctionType:
|
||||
s = inspect.getsource(obj)
|
||||
s = s.lstrip()
|
||||
s = 'from __future__ import generators\n' + s # for 2.2 ...
|
||||
tree = compiler.parse(s)
|
||||
v = _YieldVisitor()
|
||||
compiler.walk(tree, v)
|
||||
return v.isgenfunc
|
||||
return 0
|
||||
|
||||
class _YieldVisitor(object):
|
||||
|
||||
def __init__(self):
|
||||
self.toplevel = 1
|
||||
self.isgenfunc = 0
|
||||
|
||||
def visitYield(self, node):
|
||||
self.isgenfunc = 1
|
||||
|
||||
def visitFunction(self, node):
|
||||
if self.toplevel:
|
||||
self.toplevel = 0 # skip embedded functions
|
||||
self.visit(node.code)
|
||||
|
||||
def visitClass(self, node):
|
||||
pass # skip
|
||||
|
||||
def visitExec(self, node):
|
||||
pass # skip
|
||||
|
Loading…
x
Reference in New Issue
Block a user