Example #1
0
    def from_cfg(cls, machine_cfg, use_labels=False):
        """extract machine parameters from the config

        return new Machine() or None on config error
        """
        cpu = machine_cfg.cpu
        cpu_type, cpu_name = cls.parse_cpu_type(cpu)
        if cpu_type is None:
            log_machine.error("invalid CPU type given: %s", cpu)
            return None
        ram_size = machine_cfg.ram_size
        cycles_per_run = machine_cfg.cycles_per_run
        max_cycles = machine_cfg.max_cycles
        log_machine.info(
            "cpu=%s(%d), ram_size=%d, labels=%s, "
            "cycles_per_run=%d, max_cycles=%d",
            cpu_name,
            cpu_type,
            ram_size,
            use_labels,
            cycles_per_run,
            max_cycles,
        )
        return cls(
            cpu_type,
            ram_size,
            raise_on_main_run=False,
            use_labels=use_labels,
            cycles_per_run=cycles_per_run,
            max_cycles=max_cycles,
            cpu_name=cpu_name,
        )
Example #2
0
 def instr_hook():
     state.get(self.cpu)
     res = state.dump()
     for r in res:
         log_machine.info(r)
     pc = self.cpu.r_pc()
     _, txt = self.cpu.disassemble(pc)
     log_machine.info("%06x: %s", pc, txt)
Example #3
0
 def instr_hook():
   state.get(self.cpu)
   res = state.dump()
   for r in res:
     log_machine.info(r)
   pc = self.cpu.r_pc()
   _, txt = self.cpu.disassemble(pc)
   log_machine.info("%06x: %s", pc, txt)
Example #4
0
  def from_cfg(cls, machine_cfg, use_labels=False):
    """extract machine parameters from the config

       return new Machine() or None on config error
    """
    cpu = machine_cfg.cpu
    cpu_type, cpu_name = cls.parse_cpu_type(cpu)
    if cpu_type is None:
      log_machine.error("invalid CPU type given: %s", cpu)
      return None
    ram_size = machine_cfg.ram_size
    cycles_per_run = machine_cfg.cycles_per_run
    max_cycles = machine_cfg.max_cycles
    log_machine.info("cpu=%s(%d), ram_size=%d, labels=%s, "
                     "cycles_per_run=%d, max_cycles=%d",
                     cpu_name, cpu_type, ram_size, use_labels,
                     cycles_per_run, max_cycles)
    return cls(cpu_type, ram_size,
               raise_on_main_run=False,
               use_labels=use_labels,
               cycles_per_run=cycles_per_run,
               max_cycles=max_cycles,
               cpu_name=cpu_name)
Example #5
0
    def run(self,
            pc,
            sp=None,
            set_regs=None,
            get_regs=None,
            max_cycles=0,
            cycles_per_run=0,
            name=None):
        mem = self.mem
        cpu = self.cpu

        if name is None:
            name = "default"

        # current run nesting level
        nesting = len(self.run_states)

        # return address of a run is always run_end_addr
        ret_addr = self.run_exit_addr

        # get cpu context
        if nesting > 0:
            cpu_ctx = cpu.get_cpu_context()
        else:
            cpu_ctx = None

        # share stack with last run if not specified
        if sp is None:
            if nesting == 0:
                raise ValueError("stack must be specified!")
            else:
                sp = cpu.r_reg(REG_A7)
                sp -= 4

        log_machine.info("run#%d(%s): begin pc=%06x, sp=%06x, ret_addr=%06x",
                         nesting, name, pc, sp, ret_addr)

        # store return address on stack
        mem.w32(sp, ret_addr)

        # setup pc, sp
        cpu.w_pc(pc)
        cpu.w_reg(REG_A7, sp)

        # create run state for this run and push it
        run_state = RunState(name, pc, sp, ret_addr)
        self.run_states.append(run_state)

        # setup regs
        if set_regs:
            set_regs_txt = self._print_regs(set_regs)
            log_machine.info("run#%d: set_regs=%s", nesting, set_regs_txt)
            for reg in set_regs:
                val = set_regs[reg]
                cpu.w_reg(reg, val)

        # get cycle params either from this call or from default
        if not cycles_per_run:
            cycles_per_run = self.cycles_per_run
        if not max_cycles:
            max_cycles = self.max_cycles

        # main execution loop of run
        total_cycles = 0
        start_time = time.clock()
        try:
            while not run_state.done:
                log_machine.debug("+ cpu.execute")
                total_cycles += cpu.execute(cycles_per_run)
                log_machine.debug("- cpu.execute")
                # end after enough cycles
                if max_cycles > 0 and total_cycles >= max_cycles:
                    break
        except Exception as e:
            self.error_reporter.report_error(e)
        end_time = time.clock()

        # retrieve regs
        if get_regs:
            regs = {}
            for reg in get_regs:
                val = cpu.r_reg(reg)
                regs[reg] = val
            regs_text = self._print_regs(regs)
            log_machine.info("run #%d: get_regs=%s", nesting, regs_text)
            run_state.regs = regs

        # restore cpu context
        if cpu_ctx:
            cpu.set_cpu_context(cpu_ctx)

        # update run state
        run_state.time_delta = end_time - start_time
        run_state.cycles = total_cycles
        # pop
        self.run_states.pop()

        log_machine.info("run #%d(%s): end. state=%s", nesting, name,
                         run_state)

        # if run_state has error and we are not a top-level raise an error
        # so the running trap code gets aborted and propagates the abort
        if run_state.error:
            if nesting > 0 or self.raise_on_main_run:
                pc = cpu.r_pc()
                raise NestedCPURunError(pc, run_state.error)

        return run_state
Example #6
0
 def instr_hook():
     pc = self.cpu.r_pc()
     _, txt = self.cpu.disassemble(pc)
     log_machine.info("%06x: %s", pc, txt)
Example #7
0
  def run(self, pc, sp=None, set_regs=None, get_regs=None,
          max_cycles=0, cycles_per_run=0, name=None):
    mem = self.mem
    cpu = self.cpu

    if name is None:
      name = "default"

    # current run nesting level
    nesting = len(self.run_states)

    # return address of a run is always run_end_addr
    ret_addr = self.run_exit_addr

    # get cpu context
    if nesting > 0:
      cpu_ctx = cpu.get_cpu_context()
    else:
      cpu_ctx = None

    # share stack with last run if not specified
    if sp is None:
      if nesting == 0:
        raise ValueError("stack must be specified!")
      else:
        sp = cpu.r_reg(REG_A7)
        sp -= 4

    log_machine.info("run#%d(%s): begin pc=%06x, sp=%06x, ret_addr=%06x",
                     nesting, name, pc, sp, ret_addr)

    # store return address on stack
    mem.w32(sp, ret_addr)

    # setup pc, sp
    cpu.w_pc(pc)
    cpu.w_reg(REG_A7, sp)

    # create run state for this run and push it
    run_state = RunState(name, pc, sp, ret_addr)
    self.run_states.append(run_state)

    # setup regs
    if set_regs:
      set_regs_txt = self._print_regs(set_regs)
      log_machine.info("run#%d: set_regs=%s", nesting, set_regs_txt)
      for reg in set_regs:
        val = set_regs[reg]
        cpu.w_reg(reg, val)

    # get cycle params either from this call or from default
    if not cycles_per_run:
      cycles_per_run = self.cycles_per_run
    if not max_cycles:
      max_cycles = self.max_cycles

    # main execution loop of run
    total_cycles = 0
    start_time = time.clock()
    try:
      while not run_state.done:
        log_machine.debug("+ cpu.execute")
        total_cycles += cpu.execute(cycles_per_run)
        log_machine.debug("- cpu.execute")
        # end after enough cycles
        if max_cycles > 0 and total_cycles >= max_cycles:
          break
    except Exception as e:
      self.error_reporter.report_error(e)
    end_time = time.clock()

    # retrieve regs
    if get_regs:
      regs = {}
      for reg in get_regs:
        val = cpu.r_reg(reg)
        regs[reg] = val
      regs_text = self._print_regs(regs)
      log_machine.info("run #%d: get_regs=%s", nesting, regs_text)
      run_state.regs = regs

    # restore cpu context
    if cpu_ctx:
      cpu.set_cpu_context(cpu_ctx)

    # update run state
    run_state.time_delta = end_time - start_time
    run_state.cycles = total_cycles
    # pop
    self.run_states.pop()

    log_machine.info("run #%d(%s): end. state=%s", nesting, name, run_state)

    # if run_state has error and we are not a top-level raise an error
    # so the running trap code gets aborted and propagates the abort
    if run_state.error:
      if nesting > 0 or self.raise_on_main_run:
        pc = cpu.r_pc()
        raise NestedCPURunError(pc, run_state.error)

    return run_state
Example #8
0
 def instr_hook():
   pc = self.cpu.r_pc()
   _, txt = self.cpu.disassemble(pc)
   log_machine.info("%06x: %s", pc, txt)