diff --git a/doc/informal.tex b/doc/informal.tex index 531bab63..69ff1029 100644 --- a/doc/informal.tex +++ b/doc/informal.tex @@ -245,7 +245,7 @@ def bin2gray(width, B, G): width -- bit width B -- input intbv signal, binary encoded - G -- output intbv signal, gray encoded + G -- output intbv signal, Gray encoded """ while 1: yield B @@ -254,10 +254,10 @@ def bin2gray(width, B, G): \end{verbatim} -This code introduces a few new concepts. The string in triple -quotes at the start of the function is a \dfn{doc string}. This is -standard Python practice to document function and other -code. Also, we use a third form of the \keyword{yield} statement: +This code introduces a few new concepts. The string in triple quotes +at the start of the function is a \dfn{doc string}. This is standard +Python practice for the structured documentation of code. Moreover, we +use a third form of the \keyword{yield} statement: \samp{yield \var{signal}}. This specifies that the generator should resume whenever \var{signal} changes value. This is typically used to describe combinatorial logic. diff --git a/myhdl/intbv.py b/myhdl/intbv.py index 83651c4b..65040bed 100644 --- a/myhdl/intbv.py +++ b/myhdl/intbv.py @@ -26,7 +26,13 @@ __date__ = "$Date$" import sys maxint = sys.maxint from types import StringType - + +def _int2bitstring(num): + if num == 0: + return "0" + if num == 1: + return "1" + return _int2bitstring(num // 2) + _int2bitstring(num % 2) class intbv(object): __slots__ = ('_val', '_len') @@ -73,6 +79,11 @@ class intbv(object): res._len = basewidth + width return res + # conversion to binary string + def bin(self): + return _int2bitstring(self._val) + + # copy methods def __copy__(self): return intbv(self._val)