1
0
mirror of https://github.com/myhdl/myhdl.git synced 2025-01-24 21:52:56 +08:00

processes, instances

This commit is contained in:
jand 2003-06-08 21:35:34 +00:00
parent 447f9785a3
commit a8323a6b03
4 changed files with 184 additions and 2 deletions

View File

@ -50,4 +50,5 @@ from _simulator import now
from delay import delay
from Cosimulation import Cosimulation
from util import downrange, bin, Error, StopSimulation
from misc import instances, processes

75
myhdl/misc.py Normal file
View File

@ -0,0 +1,75 @@
# 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 misc 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>"
__version__ = "$Revision$"
__date__ = "$Date$"
import re
import inspect
from types import GeneratorType, FunctionType, ListType, TupleType
from myhdl import Cosimulation
def _isGenSeq(seq):
if not isinstance(seq, (ListType, TupleType)):
return 0
for e in seq:
if type(e) in (GeneratorType, Cosimulation):
continue
if _isGenSeq(e):
continue
return 0
return 1
def instances():
f = inspect.currentframe()
d = inspect.getouterframes(f)[1][0].f_locals
l = []
for v in d.values():
if type(v) in (GeneratorType, Cosimulation):
l.append(v)
elif _isGenSeq(v):
l.append(v)
return l
def processes():
f = inspect.currentframe()
d = inspect.getouterframes(f)[1][0].f_locals
l = []
for v in d.values():
if type(v) is FunctionType:
s = inspect.getsource(v)
# check whether this is a generator function
if re.search(r"\byield\b", s):
l.append(v()) # call it
return l

View File

@ -23,9 +23,9 @@ __author__ = "Jan Decaluwe <jan@jandecaluwe.com>"
__version__ = "$Revision$"
__date__ = "$Date$"
import test_Simulation, test_Signal, test_intbv, test_Cosimulation
import test_Simulation, test_Signal, test_intbv, test_Cosimulation, test_misc
modules = (test_Simulation, test_Signal, test_intbv)
modules = (test_Simulation, test_Signal, test_intbv, test_misc)
import unittest

106
myhdl/test_misc.py Normal file
View File

@ -0,0 +1,106 @@
# 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
""" Run the unit tests for Signal """
__author__ = "Jan Decaluwe <jan@jandecaluwe.com>"
__version__ = "$Revision$"
__date__ = "$Date$"
from __future__ import generators
import random
from random import randrange
random.seed(1) # random, but deterministic
from types import GeneratorType
import unittest
from unittest import TestCase
from misc import instances, processes
def A(n):
yield None
def B(n):
yield None
def C(n):
A_1 = A(1)
A_2 = A(2)
B_1 = B(1)
return A_1, A_2, B_1
g = 3
class InstancesTest(TestCase):
def testInstances(self):
def D():
yield None
D_1 = D()
d = 1
A_1 = A(1)
a = [1, 2]
B_1 = B(1)
b = "string"
C_1 = C(1)
c = {}
i = instances()
# can't just construct an expected list;
# that would become part of the instances also!
self.assertEqual(len(i), 4)
for e in (D_1, A_1, B_1, C_1):
self.assert_(e in i)
class ProcessesTest(TestCase):
def testProcesses(self):
def D():
yield None
D_1 = D()
d = 1
def A():
yield None
a = [1, 2]
def E():
yield None
B_1 = B(1)
b = "string"
C_1 = C(1)
c = {}
p = processes()
self.assertEqual(len(p), 3)
for e in p:
self.assertEqual(type(e), GeneratorType)
n = [e.gi_frame.f_code.co_name for e in p]
for en in ['D', 'A', 'E']:
self.assert_(en in n)
if __name__ == "__main__":
unittest.main()