Example #1
0
def number_inc_gt(typechar, loopvar, stop, step, sgn):
    """ Increase number and check if it exceeds a limit. """
    if sgn == 0:
        return False
    if typechar in ('#', '!'):
        fp_left = fp.from_bytes(loopvar).iadd(step)
        loopvar[:] = fp_left.to_bytes()
        return fp_left.gt(stop) if sgn > 0 else stop.gt(fp_left)
    else:
        int_left = vartypes.sint_to_value(loopvar) + step
        loopvar[:] = vartypes.value_to_sint(int_left)
        return int_left > stop if sgn > 0 else stop > int_left
Example #2
0
def randomize(val):        
    """ Reseed the random number generator. """
    # RANDOMIZE converts to int in a non-standard way - looking at the first two bytes in the internal representation
    # on a program line, if a number outside the signed int range (or -32768) is entered,
    # the number is stored as a MBF double or float. Randomize then:
    #   - ignores the first 4 bytes (if it's a double)
    #   - reads the next two
    #   - xors them with the final two (most signifant including sign bit, and exponent)
    # and interprets them as a signed int 
    # e.g. 1#    = /x00/x00/x00/x00 /x00/x00/x00/x81 gets read as /x00/x00 ^ /x00/x81 = /x00/x81 -> 0x10000-0x8100 = -32512 (sign bit set)
    #      0.25# = /x00/x00/x00/x00 /x00/x00/x00/x7f gets read as /x00/x00 ^ /x00/x7f = /x00/x7F -> 0x7F00 = 32512 (sign bit not set)
    #              /xDE/xAD/xBE/xEF /xFF/x80/x00/x80 gets read as /xFF/x80 ^ /x00/x80 = /xFF/x00 -> 0x00FF = 255   
    s = val[1]
    final_two = s[-2:]
    mask = bytearray('\x00\x00')
    if len(s) >= 4:
        mask = s[-4:-2]
    final_two = bytearray(chr(final_two[0]^mask[0]) + chr(final_two[1]^mask[1]))
    n = vartypes.sint_to_value(final_two)
    state.basic_state.rnd_seed &= 0xff
    get_random_int(1) # RND(1)
    state.basic_state.rnd_seed += n * rnd_step
    state.basic_state.rnd_seed %= rnd_period
Example #3
0
def randomize(val):
    """ Reseed the random number generator. """
    # RANDOMIZE converts to int in a non-standard way - looking at the first two bytes in the internal representation
    # on a program line, if a number outside the signed int range (or -32768) is entered,
    # the number is stored as a MBF double or float. Randomize then:
    #   - ignores the first 4 bytes (if it's a double)
    #   - reads the next two
    #   - xors them with the final two (most signifant including sign bit, and exponent)
    # and interprets them as a signed int
    # e.g. 1#    = /x00/x00/x00/x00 /x00/x00/x00/x81 gets read as /x00/x00 ^ /x00/x81 = /x00/x81 -> 0x10000-0x8100 = -32512 (sign bit set)
    #      0.25# = /x00/x00/x00/x00 /x00/x00/x00/x7f gets read as /x00/x00 ^ /x00/x7f = /x00/x7F -> 0x7F00 = 32512 (sign bit not set)
    #              /xDE/xAD/xBE/xEF /xFF/x80/x00/x80 gets read as /xFF/x80 ^ /x00/x80 = /xFF/x00 -> 0x00FF = 255
    s = val[1]
    final_two = s[-2:]
    mask = bytearray(2)
    if len(s) >= 4:
        mask = s[-4:-2]
    final_two = bytearray(
        chr(final_two[0] ^ mask[0]) + chr(final_two[1] ^ mask[1]))
    n = vartypes.sint_to_value(final_two)
    state.basic_state.rnd_seed &= 0xff
    get_random_int(1)  # RND(1)
    state.basic_state.rnd_seed += n * rnd_step
    state.basic_state.rnd_seed %= rnd_period
Example #4
0
def value_cvi(ins):
    """ CVI: return the int value of a byte representation. """
    cstr =  vartypes.pass_string_unpack(parse_bracket(ins))
    if len(cstr) < 2:
        raise error.RunError(error.IFC)
    return vartypes.pack_int(vartypes.sint_to_value(cstr[:2]))
Example #5
0
def sint_to_str(s):
    """ Convert signed int token to Python string. """
    return str(vartypes.sint_to_value(s))
Example #6
0
def sint_to_str(s):
    """ Convert signed int token to Python string. """
    return str(vartypes.sint_to_value(s))
Example #7
0
def value_cvi(ins):
    """ CVI: return the int value of a byte representation. """
    cstr = vartypes.pass_string_unpack(parse_bracket(ins))
    if len(cstr) < 2:
        raise error.RunError(error.IFC)
    return vartypes.pack_int(vartypes.sint_to_value(cstr[:2]))