<commit_before>#
# A class which represents the Program Counter and decodes instructions
# to be executed by the ZPU.  Implements section 4 of Z-code specification.
#
# For the license of this file, please consult the LICENSE file in the
# root directory of this distribution.
#

from bitfield import BitField
from zmemory import ZMemory

class ZOperationError(Exception):
  "General exception for ZOperation class"
  pass


# Depending on the opcode, the list of operands that follow can be
# extremely varied, and are stored in weird and different ways.  This
# class parses both opcodes and operands, and manages the program
# counter.  The ZPU merely needs to call
# ZOpDecoder.get_next_instruction(), and a list of [opcode, [operand,
# operand, ...]] will be returned and the program counter
# automatically incremented.  Depending on the opcode, the ZPU may
# also need to fetch "extra" operands related to variable storage,
# branch offsets, and zstrings to print.  Those are available by
# calling the public functions at the end of the class.


class ZOpDecoder(object):

  def __init__(self, zmem):
    ""
    self._memory = zmem
    self._parse_map = {}
    self.program_counter = 0

    # Create a dictionary that maps initial instruction bytes
    # to functions that properly parse the opcode and operands.
    for i in range(0x0, 0x1f+1):
      self._parse_map[i] = self._long_2op_small_small
    for i in range(0x20, 0x3f+1):
      self._parse_map[i] = self._long_2op_small_var
    for i in range(0x40, 0x5f+1):
      self._parse_map[i] = self._long_2op_var_small
    for i in range(0x60, 0x7f+1):
      self._parse_map[i] = self._long_2op_var_var
    for i in range(0x80, 0x8f+1):
      self._parse_map[i] = self._short_1op_large
    for i in range(0x90, 0x9f+1):
      self._parse_map[i] = self._short_1op_small
    for i in range(0xa0, 0xaf+1):
      self._parse_map[i] = self._short_1op_var
    for i in range(0xb0, 0xbf+1):
      self._parse_map[i] = self._short_0op
    for i in range(0xc0, 0xdf+1):
      self._parse_map[i] = self._variable_2op
    for i in range(0xe0, 0xff+1):
      self._parse_map[i] = self._variable_var

    if self._memory.version == 5:  # for z5, opcode 0xbe is 'special'
      self._parse_map[0xbe] = self._extended_var<commit_msg>Start execution at the byte address given at word 0x6 of the header, and not at 0x0.<commit_after>#
# A class which represents the Program Counter and decodes instructions
# to be executed by the ZPU.  Implements section 4 of Z-code specification.
#
# For the license of this file, please consult the LICENSE file in the
# root directory of this distribution.
#

from bitfield import BitField
from zmemory import ZMemory

class ZOperationError(Exception):
  "General exception for ZOperation class"
  pass


# Depending on the opcode, the list of operands that follow can be
# extremely varied, and are stored in weird and different ways.  This
# class parses both opcodes and operands, and manages the program
# counter.  The ZPU merely needs to call
# ZOpDecoder.get_next_instruction(), and a list of [opcode, [operand,
# operand, ...]] will be returned and the program counter
# automatically incremented.  Depending on the opcode, the ZPU may
# also need to fetch "extra" operands related to variable storage,
# branch offsets, and zstrings to print.  Those are available by
# calling the public functions at the end of the class.


class ZOpDecoder(object):

  def __init__(self, zmem):
    ""
    self._memory = zmem
    self._parse_map = {}
    self.program_counter = self._memory.read_word(0x6)

    # Create a dictionary that maps initial instruction bytes
    # to functions that properly parse the opcode and operands.
    for i in range(0x0, 0x1f+1):
      self._parse_map[i] = self._long_2op_small_small
    for i in range(0x20, 0x3f+1):
      self._parse_map[i] = self._long_2op_small_var
    for i in range(0x40, 0x5f+1):
      self._parse_map[i] = self._long_2op_var_small
    for i in range(0x60, 0x7f+1):
      self._parse_map[i] = self._long_2op_var_var
    for i in range(0x80, 0x8f+1):
      self._parse_map[i] = self._short_1op_large
    for i in range(0x90, 0x9f+1):
      self._parse_map[i] = self._short_1op_small
    for i in range(0xa0, 0xaf+1):
      self._parse_map[i] = self._short_1op_var
    for i in range(0xb0, 0xbf+1):
      self._parse_map[i] = self._short_0op
    for i in range(0xc0, 0xdf+1):
      self._parse_map[i] = self._variable_2op
    for i in range(0xe0, 0xff+1):
      self._parse_map[i] = self._variable_var

    if self._memory.version == 5:  # for z5, opcode 0xbe is 'special'
      self._parse_map[0xbe] = self._extended_var
{'fraction_non_alphanumeric': 0.08335123523093448, 'fraction_numerical': 0.029430719656283567, 'mean_word_length': 3.6467065868263475, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '186290', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1544, 'n_words': 533}