Exemplo n.º 1
0
 def parse(cls, buf):
     (hdr, ) = struct.unpack_from(cls._hdr_fmt_str, buf, 0)
     rest = buf[struct.calcsize(cls._hdr_fmt_str):]
     if hdr == 0:
         return None, rest  # all-0 header is no-op for padding
     src_type = (hdr >> 13) & 0x1
     dst_type = (hdr >> 11) & 0x3
     n_bits = hdr & 0x3ff
     subcls = cls._subclasses[dst_type]
     if src_type == 0:  # subfield
         src = cls._parse_subfield(rest)
         rest = rest[6:]
     elif src_type == 1:  # immediate
         src_len = (n_bits + 15) // 16 * 2
         src_bin = rest[:src_len]
         src = type_desc.IntDescr(size=src_len).to_user(src_bin)
         rest = rest[src_len:]
     if dst_type == 0:  # match
         dst = cls._parse_subfield(rest)
         rest = rest[6:]
     elif dst_type == 1:  # load
         dst = cls._parse_subfield(rest)
         rest = rest[6:]
     elif dst_type == 2:  # output
         dst = ''  # empty
     return subcls(src=src, dst=dst, n_bits=n_bits), rest
Exemplo n.º 2
0
 def serialize(self):
     buf = bytearray()
     if isinstance(self.src, tuple):
         src_type = 0  # subfield
     else:
         src_type = 1  # immediate
     # header
     val = (src_type << 13) | (self._dst_type << 11) | self.n_bits
     msg_pack_into(self._hdr_fmt_str, buf, 0, val)
     # src
     if src_type == 0:  # subfield
         buf += self._serialize_subfield(self.src)
     elif src_type == 1:  # immediate
         src_len = (self.n_bits + 15) // 16 * 2
         buf += type_desc.IntDescr(size=src_len).from_user(self.src)
     # dst
     if self._dst_type == 0:  # match
         buf += self._serialize_subfield(self.dst)
     elif self._dst_type == 1:  # load
         buf += self._serialize_subfield(self.dst)
     elif self._dst_type == 2:  # output
         pass  # empty
     return buf