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

bin conversion function

This commit is contained in:
jand 2003-02-05 22:38:06 +00:00
parent 19d63957d1
commit a79822c570
2 changed files with 17 additions and 6 deletions

View File

@ -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.

View File

@ -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)