def execute_amomax_w(s, inst): addr = s.rf[inst.rs1] value = s.mem.read(addr, 4) new = max(signed(value, 32), signed(s.rf[inst.rs2], 32)) s.mem.write(addr, 4, trim(new, 32)) s.rf[inst.rd] = sext_32(value) s.pc += 4
def execute_div(s, inst): x = signed(s.rf[inst.rs()]) y = signed(s.rf[inst.rt()]) sign = -1 if (x < 0) ^ (y < 0) else 1 s.rf[inst.rd()] = abs(x) / abs(y) * sign s.pc += 4
def execute_amomax_w( s, inst ): addr = s.rf[inst.rs1] value = s.mem.read( addr, 4 ) new = max( signed(value, 32), signed(s.rf[inst.rs2], 32) ) s.mem.write( addr, 4, trim(new, 32)) s.rf[inst.rd] = sext_32( value ) s.pc += 4
def execute_rem( s, inst ): a = signed( s.rf[inst.rs1], 64 ) b = signed( s.rf[inst.rs2], 64 ) if b == 0: s.rf[ inst.rd ] = a else: s.rf[ inst.rd ] = sext_xlen( abs(a) % abs(b) * (1 if (a > 0) else -1) ) s.pc += 4
def execute_divw( s, inst ): a = signed( s.rf[inst.rs1], 64 ) b = signed( s.rf[inst.rs2], 64 ) if b == 0: s.rf[ inst.rd ] = -1 else: sign = -1 if (a < 0) ^ (b < 0) else 1 s.rf[ inst.rd ] = sext_32( abs(a) / abs(b) * sign ) s.pc += 4
def execute_remw( s, inst ): a = signed( s.rf[inst.rs1], 64 ) b = signed( s.rf[inst.rs2], 64 ) if b == 0: s.rf[ inst.rd ] = a else: sign = 1 if (a > 0) else -1 s.rf[ inst.rd ] = sext_32( abs(a) % abs(b) * sign ) s.pc += 4
def execute_amomax_d( s, inst ): addr = s.rf[inst.rs1] value = (( s.mem.read( addr+4, 4 ) << 32 ) \ | s.mem.read( addr, 4 )) new = max( signed(value, 64), signed(s.rf[inst.rs2], 64) ) s.mem.write( addr, 4, trim_32( new ) ) s.mem.write( addr+4, 4, trim_32( new >> 32 ) ) s.rf[inst.rd] = value s.pc += 4
def execute_remw(s, inst): a = signed(s.rf[inst.rs1], 64) b = signed(s.rf[inst.rs2], 64) if b == 0: s.rf[inst.rd] = a else: sign = 1 if (a > 0) else -1 s.rf[inst.rd] = sext_32(abs(a) % abs(b) * sign) s.pc += 4
def execute_amomax_d(s, inst): addr = s.rf[inst.rs1] value = (( s.mem.read( addr+4, 4 ) << 32 ) \ | s.mem.read( addr, 4 )) new = max(signed(value, 64), signed(s.rf[inst.rs2], 64)) s.mem.write(addr, 4, trim_32(new)) s.mem.write(addr + 4, 4, trim_32(new >> 32)) s.rf[inst.rd] = value s.pc += 4
def execute_div( s, inst ): a = signed( s.rf[inst.rs1], 64 ) b = signed( s.rf[inst.rs2], 64 ) if b == 0: s.rf[ inst.rd ] = -1 else: s.rf[ inst.rd ] = sext_xlen( abs(a) / abs(b) * (-1 if (a < 0) ^ (b < 0) else 1) ) s.pc += 4
def execute_divw(s, inst): a = signed(s.rf[inst.rs1], 64) b = signed(s.rf[inst.rs2], 64) if b == 0: s.rf[inst.rd] = -1 else: sign = -1 if (a < 0) ^ (b < 0) else 1 s.rf[inst.rd] = sext_32(abs(a) / abs(b) * sign) s.pc += 4
def execute_bl(s, inst): if condition_passed(s, inst.cond()): s.rf[LR] = trim_32(s.fetch_pc() + 4) offset = signed(sign_extend_30(inst.imm_24()) << 2) s.rf[PC] = trim_32(s.rf[PC] + offset) return s.rf[PC] = s.fetch_pc() + 4
def execute_srai(s, inst): if s.xlen == 64: s.rf[inst.rd] = signed(s.rf[inst.rs1], 64) >> SHAMT(s, inst) elif SHAMT(s, inst) & 0x20: raise TRAP_ILLEGAL_INSTRUCTION() else: s.rf[inst.rd] = sext_32(s.rf[inst.rs1] >> SHAMT(s, inst)) s.pc += 4
def execute_srai( s, inst ): if s.xlen == 64: s.rf[ inst.rd ] = signed( s.rf[inst.rs1], 64 ) >> SHAMT( s, inst ) elif SHAMT( s, inst ) & 0x20: raise TRAP_ILLEGAL_INSTRUCTION() else: s.rf[ inst.rd ] = sext_32( s.rf[inst.rs1] >> SHAMT( s, inst ) ) s.pc += 4
def execute_mulh( s, inst ): a, b = s.rf[inst.rs1], s.rf[inst.rs2] a_s, b_s = signed(a, 64), signed(b, 64) a, b = abs(a_s), abs(b_s) multlo = trim_64( a * b ) multhi = multhi64( a, b ) # negate -- taken from # http://stackoverflow.com/questions/1541426/computing-high-64-bits-of-a-64x64-int-product-in-c # this requires us to do low multiplication as well, so it's probably # not very efficient if (a_s < 0) ^ (b_s < 0): multhi = ~multhi if multlo == 0: multhi += 1 s.rf[ inst.rd ] = sext_xlen( multhi ) s.pc += 4
def execute_smull(s, inst): if condition_passed(s, inst.cond()): if inst.rd() == 15: raise FatalError('UNPREDICTABLE') if inst.rm() == 15: raise FatalError('UNPREDICTABLE') if inst.rs() == 15: raise FatalError('UNPREDICTABLE') if inst.rn() == 15: raise FatalError('UNPREDICTABLE') RdHi, RdLo = inst.rn(), inst.rd() Rm, Rs = signed(s.rf[inst.rm()]), signed(s.rf[inst.rs()]) result = Rm * Rs if RdHi == RdLo: raise FatalError('UNPREDICTABLE') s.rf[RdHi] = trim_32(result >> 32) s.rf[RdLo] = trim_32(result) if inst.S(): s.N = (result >> 63) & 1 s.Z = result == 0 s.rf[PC] = s.fetch_pc() + 4
def execute_bge(s, inst): if signed(s.rf[inst.rs1], 64) >= signed(s.rf[inst.rs2], 64): s.pc = BRANCH_TARGET(s, inst) else: s.pc += 4
def execute_slt(s, inst): s.rf[inst.rd()] = signed(s.rf[inst.rs()]) < signed(s.rf[inst.rt()]) s.pc += 4
def branch(mac, flag, truth): print "got branch, %s, %s" % (flag, truth) if not (truth ^ mac.get_flag(flag)): # take branch mac.pc = (mac.pc + signed(mac.d)) & 0xffff
def execute_sraiw(s, inst): s.rf[inst.rd] = signed(s.rf[inst.rs1], 32) >> SHAMT(s, inst) s.pc += 4
def execute_cvt_s_w(s, inst): x = signed(s.rf[inst.fs()]) s.rf[inst.fd()] = float2bits(float(x)) s.pc += 4
def execute_bne(s, inst): if s.rf[inst.rs()] != s.rf[inst.rt()]: s.pc = s.pc + 4 + (signed(sext(inst.imm())) << 2) else: s.pc += 4
def execute_sra(s, inst): s.rf[inst.rd()] = trim(signed(s.rf[inst.rt()]) >> inst.shamt()) s.pc += 4
def execute_slti( s, inst ): s.rf[ inst.rd ] = signed( s.rf[inst.rs1], 64 ) < signed( inst.i_imm, 64 ) s.pc += 4
def execute_fcvt_s_w(s, inst): a = signed(s.rf[inst.rs1], 32) s.fp[inst.rd] = sfp.i32_to_f32(a) s.fcsr = sfp.get_flags() sfp.set_flags(0) s.pc += 4
def execute_slti(s, inst): s.rf[inst.rd] = signed(s.rf[inst.rs1], 64) < signed(inst.i_imm, 64) s.pc += 4
def execute_fcvt_s_w( s, inst ): a = signed( s.rf[inst.rs1], 32 ) s.fp[inst.rd] = sfp.i32_to_f32( a ) s.fcsr = sfp.get_flags() sfp.set_flags( 0 ) s.pc += 4
def execute_rem(s, inst): x = signed(s.rf[inst.rs()]) y = signed(s.rf[inst.rt()]) s.rf[inst.rd()] = abs(x) % abs(y) * (1 if x > 0 else -1) s.pc += 4
def execute_fcvt_d_l( s, inst ): a = signed( s.rf[inst.rs1], 64 ) s.fp[inst.rd] = sfp.i64_to_f64( a ) s.fcsr = sfp.get_flags() sfp.set_flags( 0 ) s.pc += 4
def execute_slti(s, inst): s.rf[inst.rt()] = signed(s.rf[inst.rs()]) < signed(sext(inst.imm())) s.pc += 4
def execute_sra(s, inst): s.rf[inst.rd] = sext_xlen( signed(s.rf[inst.rs1], 64) >> (s.rf[inst.rs2] & (s.xlen - 1))) s.pc += 4
def execute_srav(s, inst): # TODO: should it really be masked like this? s.rf[inst.rd()] = trim(signed(s.rf[inst.rt()]) >> trim_5(s.rf[inst.rs()])) s.pc += 4
def execute_sra( s, inst ): s.rf[ inst.rd ] = sext_xlen( signed( s.rf[inst.rs1], 64 ) >> (s.rf[inst.rs2] & (s.xlen-1)) ) s.pc += 4
def execute_bgez(s, inst): if signed(s.rf[inst.rs()]) >= 0: s.pc = s.pc + 4 + (signed(sext(inst.imm())) << 2) else: s.pc += 4
def execute_blt( s, inst ): if signed(s.rf[inst.rs1], 64) < signed(s.rf[inst.rs2], 64): s.pc = BRANCH_TARGET( s, inst ) else: s.pc += 4
def init(): print(request.form) msg = request.form['text'] username = request.form['user_name'] user_id = request.form['user_id'] team_id = request.form['team_id'] commands = msg.split(' ') character = get_character(username) contents = character.get_contents() contents['user_id'] = user_id if 'user_id' not in contents: name = character.name else: name = f'<@{contents["user_id"]}|{character.name}>' if len(commands[0]) == 0 or commands[0] == 'roll' or commands[0][0] in ( '+', '-'): if len(commands[0]) > 0 and commands[0][0] in ('+', '-'): init_mod = get_int(commands[0], 0) else: init_mod = int(contents.get('init_mod', 0)) title = '{} rolls 1d20 {}'.format(name, modifier(init_mod)) roll = random.randint(1, 20) last_init = roll + init_mod fields = [{ 'title': 'Rolls', 'value': f'{roll}', 'short': True }, { 'title': 'Result', 'value': f'{last_init}', 'short': True }] contents['last_init'] = last_init set_and_commit(character, contents) color = get_color(roll / 20) return make_response(fields, title, color=color) if len(commands) > 1 and commands[0] == 'mod': init_mod = get_int(commands[1], 0) contents['init_mod'] = init_mod set_and_commit(character, contents) fields = [{'value': signed(init_mod)}] return make_response(fields, f'{name}\'s Initiative modifier') if commands[0] == 'set': if len(commands) == 2: last_init = get_int(commands[1], 0) contents['last_init'] = last_init set_and_commit(character, contents) fields = [{'value': str(last_init)}] elif len(commands) > 2: team = db.session.query(Team).filter( Team.team_id == team_id).one_or_none() team = Team(team_id) if team is None else team team_contents = team.get_contents() tmp_inits = team_contents.get('tmp_inits', {}) last_init = get_int(commands[1], 0) target = ' '.join(commands[2:]) if target.startswith('@'): target = target[1:] name = character.name name = f'{target}({name})' tmp_inits[name] = last_init team_contents['tmp_inits'] = tmp_inits team.set_contents(team_contents) db.session.add(team) db.session.commit() fields = [{'value': str(last_init)}] return make_response(fields, f'{name}\'s Initiative value') if commands[0] in ('party', 'all'): characters = db.session.query(Character).all() team = db.session.query(Team).filter( Team.team_id == team_id).one_or_none() team = Team(team_id) if team is None else team team_contents = team.get_contents() tmp_inits = team_contents.get('tmp_inits', {}) log.info('tmp_inits: {}'.format(str(tmp_inits))) char_inits = [] for character in characters: contents = character.get_contents() if 'last_init' in contents: last_init = int(contents['last_init']) if 'user_id' not in contents: name = character.name else: name = f'<@{contents["user_id"]}|{character.name}>' char_inits.append((name, last_init)) for tmp_char in tmp_inits: char_inits.append((tmp_char, tmp_inits[tmp_char])) sorted_inits = sorted(char_inits, key=lambda tup: tup[1], reverse=True) merged_inits = [f'{name} ({init})' for name, init in sorted_inits] formatted_inits = ' > '.join(merged_inits) fields = [{'value': formatted_inits}] return make_response(fields, 'Round initiatives') if commands[0] in ('CLEAR', 'REMOVE'): if 'last_init' in contents: del (contents['last_init']) character.set_contents(contents) db.session.add(character) db.session.commit() return make_response('Your initiative is removed') if commands[0] in ('ALLCLEAR', 'ALLREMOVE', 'CLEARALL', 'REMOVEALL'): characters = db.session.query(Character).all() for character in characters: contents = character.get_contents() if 'last_init' in contents: del (contents['last_init']) character.set_contents(contents) db.session.add(character) db.session.commit() return make_response('All initiatives are removed') return process_unknown(username)
def execute_slt( s, inst ): s.rf[ inst.rd ] = signed( s.rf[inst.rs1], 64 ) < signed( s.rf[inst.rs2], 64) s.pc += 4
def execute_sraiw( s, inst ): s.rf[ inst.rd ] = signed( s.rf[inst.rs1], 32 ) >> SHAMT( s, inst ) s.pc += 4
def execute_fcvt_d_l(s, inst): a = signed(s.rf[inst.rs1], 64) s.fp[inst.rd] = sfp.i64_to_f64(a) s.fcsr = sfp.get_flags() sfp.set_flags(0) s.pc += 4
def execute_slt(s, inst): s.rf[inst.rd] = signed(s.rf[inst.rs1], 64) < signed(s.rf[inst.rs2], 64) s.pc += 4