For a change, we will use a traditional function as an example to illustrate slicing. The following function calculates the HEC byte of an ATM header.
from myhdl import intbv, concat
COSET = 0x55
def calculateHec(header):
""" Return hec for an ATM header, represented as an intbv.
The hec polynomial is 1 + x + x**2 + x**8.
"""
hec = intbv(0)
for bit in header[32:]:
hec[8:] = concat(hec[7:2],
bit ^ hec[1] ^ hec[7],
bit ^ hec[0] ^ hec[7],
bit ^ hec[7]
)
return hec ^ COSET
The code shows how slicing access and assignment is supported on the intbv data type. In accordance with the most common hardware convention, and unlike standard Python, slicing ranges are downward. The code also demonstrates concatenation of intbv objects.
As in standard Python, the slicing range is half-open: the highest
index bit is not included. Unlike standard Python however, this index
corresponds to the leftmost item. Both indices can be omitted
from the slice. If the leftmost index is omitted, the meaning is to
access ``all'' higher order bits. If the rightmost index is omitted,
it is 0 by default.
The half-openness of a slice may seem awkward at first, but it helps
to avoid one-off count issues in practice. For example, the slice
hex[8:] has exactly 8 bits. Likewise, the slice
hex[7:2] has 7-2=5 bits. You can think about it as
follows: for a slice [i:j], only bits below index i are
included, and the bit with index j is the last bit included.
When an intbv object is sliced, a new intbv object is returned. This new intbv object is always positive, even when the original object was negative.
About this document