Exemplo n.º 1
0
    def eval_Integer_0OPI(vm, c):
        next_ip = None
        name = "ILLEGAL_0OPI"
        if c[RAW_XOP] == 0x00: # JUMP
            if DEBUG:
                name = "JUMP"
            #elif TRACE: # TODO
            #    record_trace("JUMP") # TODO
            next_ip = knightmodule.JUMP(vm, c)
        else:
            illegal_instruction(vm, c)

        if DEBUG:
            print_func( "# %s %d\n" % (name, c[RAW_IMMEDIATE]) )
        return next_ip
Exemplo n.º 2
0
def main(args):
    if len(args)<2:
        print_func("Usage: %s $FileName" % args[0], file=stderr)
        print_func("Where $FileName is the name of the paper tape of the"
                   "program being run", file=stderr)
        exit(EXIT_FAILURE)
    else: # len(args)>=2
        filename = args[1]

        if len(args)==2:
            do_minimal_vm(filename)
        else:
            assert len(args)>2
            romhex = args[2]=="--rom-hex" # check for rom-hex flag, set romhex
            do_minimal_vm(filename, romhex=romhex)
        exit(EXIT_SUCCESS)
Exemplo n.º 3
0
 def read_and_eval(vm, halt_print=COMPAT_TRUE):
     try:
         c = read_instruction(vm)
         vm = eval_instruction_specific_bit(vm, c, halt_print=halt_print)
         if vm==None or vm[IP]==None:
             raise InstructionNotImplemented(c)
         return vm
     except OutsideOfWorldException:
         e = exc_info()[1] # to remain backwards and forwards compatible
         print_func(
             "Invalid state reached after: %d instructions" % vm[PERF_COUNT],
             file=stderr)
         print_func(
             "%d: %s" % (e.args[1], e.args[0]),
             file=stderr )
         # if TRACE: TODO
         #    pass # TODO
         exit(EXIT_FAILURE)
Exemplo n.º 4
0
def eval_N_OP_int(vm, c, n, lookup_val, lookup_table,
                  immediate=COMPAT_FALSE, illegal_table=None):
    next_ip = None
    if immediate:
        name = "ILLEGAL_%dOPI" % n
    else:
        name = "ILLEGAL_%dOP" % n
    if lookup_val in lookup_table:
        instruction_func, instruction_str = lookup_table[lookup_val]
        if DEBUG:
            name = instruction_str
        #elif TRACE: # TODO
        #    record_trace(instruction_str) # TODO
        next_ip = instruction_func(vm, c)

    # not sure why zome XOP are matched explicitly for illegal whereas
    # others fall into default when the handling is the same
    # some explicitly illegal XOPs in eval_3OP_int probably just exist
    # to reserve them for future use
    # elif illegal_table!=None and raw_xop in illegal_table:
    #    illegal_instruction(vm, c)
    else:
        illegal_instruction(vm, c)

    if DEBUG:
        print_func(
            ("# %s" + " reg%d"*n) % ( (name,) + c[I_REGISTERS][0:n] ),
            end="",
            sep="",
        ) # print_func
        if immediate:
            print_func(" %d" % c[RAW_IMMEDIATE] )
        else:
            print_func()
    return next_ip
Exemplo n.º 5
0
    def eval_instruction(vm, current_instruction, halt_print=COMPAT_TRUE):
        vm = increment_vm_perf_count(vm)
        if DEBUG:
            print_func("Executing: %s" %
                       string_unpacked_instruction(current_instruction),
                       file=stderr)
            sleep(1)

        raw0 = current_instruction[RAW][0]

        if raw0 == 0: # Deal with NOPs
            if [0,0,0,0]==current_instruction[RAW].tolist():
                #if TRACE: # TODO
                #    record_trace("NOP") # TODO
                return vm_with_new_ip(vm, current_instruction[NEXTIP])
            illegal_instruction(vm, current_instruction)

        elif raw0 in DECODE_TABLE:
            assert raw0 in EVAL_TABLE
            current_instruction = DECODE_TABLE[raw0](vm, current_instruction)
            return vm_with_new_ip(vm,
                                  EVAL_TABLE[raw0](vm, current_instruction) )
        elif raw0 == HALT_OP:  # Deal with HALT
            vm = halt_vm(vm)
            if halt_print:
                print_func(
                    "Computer Program has Halted\nAfter Executing %d "
                    "instructions" % vm[PERF_COUNT],
                    file=stderr)
            # if TRACE: # TODO
            #    record_trace("HALT") # TODO
            #    print_traces() # TODO
            return vm
        else:
            illegal_instruction(vm, current_instruction)

        # we shouldn't make it this far, other branches call exit()
        assert COMPAT_FALSE
        return None
Exemplo n.º 6
0
def illegal_instruction(vm, current_instruction):
    print_func("Invalid instruction was recieved at address:%08X" %
               current_instruction[CURIP],
               file=stderr)
    print_func("After %d instructions" % vm[PERF_COUNT], file=stderr)
    print_func("Unable to execute the following instruction:\n\t%s" %
               string_unpacked_instruction(current_instruction),
               file=stderr)

    current_instruction = invalidate_instruction(current_instruction)
    vm = halt_vm(vm)

    if DEBUG:
        print_func("Computer Program has Halted", file=stderr)

    #if TRACE: # TODO
    #    record_trace("HALT") # TODO
    #    print_traces() # TODO

    exit(EXIT_FAILURE)
Exemplo n.º 7
0
def eval_HALCODE(vm, c):
    next_ip = None
    name = "ILLEGAL_HALCODE"

    # POSIX MODE instructions not implemented

    if c[HAL_CODE] in HAL_CODES_TABLE:
        instruction_func, instruction_str = HAL_CODES_TABLE[c[HAL_CODE]]
        if DEBUG:
            name = instruction_str
        #elif TRACE: # TODO
        #    record_trace(instruction_str) # TODO
        instruction_func(vm)
        next_ip = c[NEXTIP]
    else:
        print_func("Invalid HALCODE", file=stderr)
        print_func("Computer Program has Halted", file=stderr)
        illegal_instruction(vm, c)

    if DEBUG:
        print_func("# %s" % name)
    return next_ip
Exemplo n.º 8
0
                file=stderr )
            # if TRACE: TODO
            #    pass # TODO
            exit(EXIT_FAILURE)
    return read_and_eval

READ_AND_EVAL_TABLE = {}

def get_read_and_eval_for_register_size(regsize_bytes):
    global READ_AND_EVAL_TABLE
    if regsize_bytes not in READ_AND_EVAL_TABLE:
        READ_AND_EVAL_TABLE[regsize_bytes] = \
            make_read_and_eval_for_registersize(regsize_bytes*8)
    return READ_AND_EVAL_TABLE[regsize_bytes]

def read_and_eval(vm, optimize=COMPAT_TRUE, halt_print=COMPAT_TRUE):
    if optimize:
        return get_read_and_eval_for_register_size(vm[REG].itemsize)(
            vm, halt_print=halt_print)
    else:
        # this forces the generic version
        return get_read_and_eval_for_register_size(0)(
            vm, halt_print=halt_print)

if __name__ == "__main__":
    vm = create_vm(2**16) # (64*1024)
    print_func( "vm created %d bytes" %  len(vm[MEM]) )
    instruction = read_instruction(vm)
    print_func( "instruction opcode unpacked (0x0%s, 0x0%s)" % 
                instruction[OP] )