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:
parent
19d63957d1
commit
a79822c570
@ -245,7 +245,7 @@ def bin2gray(width, B, G):
|
|||||||
|
|
||||||
width -- bit width
|
width -- bit width
|
||||||
B -- input intbv signal, binary encoded
|
B -- input intbv signal, binary encoded
|
||||||
G -- output intbv signal, gray encoded
|
G -- output intbv signal, Gray encoded
|
||||||
"""
|
"""
|
||||||
while 1:
|
while 1:
|
||||||
yield B
|
yield B
|
||||||
@ -254,10 +254,10 @@ def bin2gray(width, B, G):
|
|||||||
|
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
This code introduces a few new concepts. The string in triple
|
This code introduces a few new concepts. The string in triple quotes
|
||||||
quotes at the start of the function is a \dfn{doc string}. This is
|
at the start of the function is a \dfn{doc string}. This is standard
|
||||||
standard Python practice to document function and other
|
Python practice for the structured documentation of code. Moreover, we
|
||||||
code. Also, we use a third form of the \keyword{yield} statement:
|
use a third form of the \keyword{yield} statement:
|
||||||
\samp{yield \var{signal}}. This specifies that the generator should
|
\samp{yield \var{signal}}. This specifies that the generator should
|
||||||
resume whenever \var{signal} changes value. This is typically used to
|
resume whenever \var{signal} changes value. This is typically used to
|
||||||
describe combinatorial logic.
|
describe combinatorial logic.
|
||||||
|
@ -26,7 +26,13 @@ __date__ = "$Date$"
|
|||||||
import sys
|
import sys
|
||||||
maxint = sys.maxint
|
maxint = sys.maxint
|
||||||
from types import StringType
|
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):
|
class intbv(object):
|
||||||
__slots__ = ('_val', '_len')
|
__slots__ = ('_val', '_len')
|
||||||
@ -73,6 +79,11 @@ class intbv(object):
|
|||||||
res._len = basewidth + width
|
res._len = basewidth + width
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
# conversion to binary string
|
||||||
|
def bin(self):
|
||||||
|
return _int2bitstring(self._val)
|
||||||
|
|
||||||
|
|
||||||
# copy methods
|
# copy methods
|
||||||
def __copy__(self):
|
def __copy__(self):
|
||||||
return intbv(self._val)
|
return intbv(self._val)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user