def gen_random_test(): # Generate some random data data = [] for i in xrange(128): data.append( random.randint(0,0xffff) ) # Generate random accesses to this data asm_code = [] for i in xrange(100): a = random.randint(0,127) b = random.randint(0,127) base = Bits( 32, 0x2000 + (2*b) ) offset = Bits( 16, (2*(a - b)) ) result = Bits( 32, data[a] ) asm_code.append( gen_st_random_test( "sh", "lhu", result.uint(), offset.int(), base.uint() ) ) # Generate some random data to initialize memory initial_data = [] for i in xrange(128): initial_data.append( random.randint(0,0xffff) ) # Add the data to the end of the assembly code asm_code.append( gen_hword_data( initial_data ) ) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(100): src0 = Bits( 32, random.randint(0,0xffffffff) ) src1 = Bits( 32, random.randint(0,0xffffffff) ) # Python rounds to negative infinity even for negative values, so we # need to do something a little more complicated. This was inspired # from this post: # # http://stackoverflow.com/questions/19919387 # a = src0.int() b = src1.int() A = -a if (a < 0) else a B = -b if (b < 0) else b c = -(A // B) if (a < 0) ^ (b < 0) else (A // B) dest = Bits( 32, c ) asm_code.append( gen_rr_value_test( "div", src0.uint(), src1.uint(), dest.uint() ) ) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(100): src0 = Bits(32, random.randint(0, 0xffffffff)) src1 = Bits(32, random.randint(0, 0xffffffff)) # Python rounds to negative infinity even for negative values, so we # need to do something a little more complicated. This was inspired # from this post: # # http://stackoverflow.com/questions/19919387 # a = src0.int() b = src1.int() A = -a if (a < 0) else a B = -b if (b < 0) else b c = -(A // B) if (a < 0) ^ (b < 0) else (A // B) dest = Bits(32, c) asm_code.append( gen_rr_value_test("div", src0.uint(), src1.uint(), dest.uint())) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(100): src = Bits( 32, random.randint(0,0xffffffff) ) imm = Bits( 16, random.randint(0,0xffff) ) dest = Bits( 32, src.int() < sext(imm,32).int() ) asm_code.append( gen_rimm_value_test( "slti", src.uint(), imm.uint(), dest.uint() ) ) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(100): src = Bits( 32, random.randint(0,0xffffffff) ) imm = Bits( 5, random.randint(0,31) ) dest = src >> imm asm_code.append( gen_rimm_value_test( "srl", src.uint(), imm.uint(), dest.uint() ) ) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(100): src0 = Bits( 32, random.randint(0,0xffffffff) ) src1 = Bits( 5, random.randint(0,31) ) dest = Bits( 32, src0.int() >> src1.uint() ) asm_code.append( gen_rr_value_test( "srav", src0.uint(), src1.uint(), dest.uint() ) ) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(100): src0 = Bits(32, random.randint(0, 0xFFFFFFFF)) src1 = Bits(32, random.randint(0, 0xFFFFFFFF)) dest = Bits(32, src0 * src1, trunc=True) asm_code.append(gen_rr_value_test("mul", src0.uint(), src1.uint(), dest.uint())) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(100): src0 = Bits( 32, random.randint(0,0xffffffff) ) src1 = Bits( 32, random.randint(0,0xffffffff) ) dest = src0 - src1 asm_code.append( gen_rr_value_test( "subu", src0.uint(), src1.uint(), dest.uint() ) ) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(25): src0 = Bits( 32, random.randint(0,0xffffffff) ) src1 = Bits( 32, random.randint(0,0xffffffff) ) taken = ( src0 != src1 ) asm_code.append( gen_br2_value_test( "bne", src0.uint(), src1.uint(), taken ) ) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(100): src = Bits( 32, random.randint(0,0xffffffff) ) imm = Bits( 5, random.randint(0,31) ) dest = Bits( 32, src.int() >> imm.uint() ) asm_code.append( gen_rimm_value_test( "sra", src.uint(), imm.uint(), dest.uint() ) ) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(25): src0 = Bits(32, random.randint(0, 0xffffffff)) src1 = Bits(32, random.randint(0, 0xffffffff)) taken = (src0 != src1) asm_code.append( gen_br2_value_test("bne", src0.uint(), src1.uint(), taken)) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(100): src0 = Bits(32, random.randint(0, 0xffffffff)) src1 = Bits(32, random.randint(0, 0xffffffff)) dest = Bits(32, src0 * src1, trunc=True) asm_code.append( gen_rr_value_test("mul", src0.uint(), src1.uint(), dest.uint())) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(100): src0 = Bits(32, random.randint(0, 0xffffffff)) src1 = Bits(5, random.randint(0, 31)) dest = Bits(32, src0.int() >> src1.uint()) asm_code.append( gen_rr_value_test("srav", src0.uint(), src1.uint(), dest.uint())) return asm_code
def gen_random_test(): # Generate some random data data = [] for i in xrange(128): data.append(random.randint(0, 0xFFFF)) # Generate random accesses to this data asm_code = [] for i in xrange(100): a = random.randint(0, 127) b = random.randint(0, 127) base = Bits(32, 0x2000 + (2 * b)) offset = Bits(16, (2 * (a - b))) result = sext(Bits(16, data[a]), 32) asm_code.append(gen_ld_value_test("lh", offset.int(), base.uint(), result.uint())) # Add the data to the end of the assembly code asm_code.append(gen_hword_data(data)) return asm_code
def gen_random_test(): # Generate some random data data = [] for i in xrange(128): data.append( random.randint(0,0xffffffff) ) # Generate random accesses to this data asm_code = [] for i in xrange(100): a = random.randint(0,127) b = random.randint(0,127) base = Bits( 32, 0x2000 + (4*b) ) offset = Bits( 16, (4*(a - b)) ) result = data[a] asm_code.append( gen_st_value_test( "sw", result, offset.int(), base.uint(), result ) ) # Generate some random data to initialize memory initial_data = [] for i in xrange(128): initial_data.append( random.randint(0,0xffffffff) ) # Add the data to the end of the assembly code asm_code.append( gen_word_data( initial_data ) ) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(25): src = Bits(32, random.randint(0, 0xffffffff)) taken = (src.int() >= 0) asm_code.append(gen_br1_value_test("bgez", src.uint(), taken)) return asm_code
def gen_random_test(): # Generate some random data data = [] for i in xrange(128): data.append(random.randint(0, 0xffff)) # Generate random accesses to this data asm_code = [] for i in xrange(100): a = random.randint(0, 127) b = random.randint(0, 127) base = Bits(32, 0x2000 + (2 * b)) offset = Bits(16, (2 * (a - b))) result = sext(Bits(16, data[a]), 32) asm_code.append( gen_ld_value_test("lh", offset.int(), base.uint(), result.uint())) # Add the data to the end of the assembly code asm_code.append(gen_hword_data(data)) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(25): src = Bits( 32, random.randint(0,0xffffffff) ) taken = ( src.int() > 0 ) asm_code.append( gen_br1_value_test( "bgtz", src.uint(), taken ) ) return asm_code
def gen_random_test(): # Generate some random data data = [] for i in xrange(128): data.append( random.randint(0,0xff) ) # Generate random accesses to this data asm_code = [] for i in xrange(100): a = random.randint(0,127) b = random.randint(0,127) base = Bits( 32, 0x2000 + b ) offset = Bits( 16, a - b ) result = data[a] asm_code.append( gen_ld_value_test( "lbu", offset.int(), base.uint(), result ) ) # Add the data to the end of the assembly code asm_code.append( gen_byte_data( data ) ) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(100): imm = Bits(16, random.randint(0, 0xffff)) dest = zext(imm, 32) << 16 asm_code.append(gen_imm_value_test("lui", imm.uint(), dest.uint())) return asm_code
def gen_random_test(): asm_code = [] for i in xrange(100): imm = Bits( 16, random.randint(0,0xffff) ) dest = zext(imm,32) << 16 asm_code.append( gen_imm_value_test( "lui", imm.uint(), dest.uint() ) ) return asm_code
def disassemble_field_j_imm(bits): imm = Bits(21, 0) imm[1:11] = bits[tinyrv2_field_slice_j_imm0] imm[11:12] = bits[tinyrv2_field_slice_j_imm1] imm[12:20] = bits[tinyrv2_field_slice_j_imm2] imm[20:21] = bits[tinyrv2_field_slice_j_imm3] return "0x{:0>6x}".format(imm.uint())
def gen_random_test(): asm_code = [] for i in xrange(100): src0 = Bits( 32, random.randint(0,0xffffffff) ) src1 = Bits( 32, random.randint(0,0xffffffff) ) a = src0.int() b = src1.int() A = -a if (a < 0) else a B = -b if (b < 0) else b c = -(A % B) if (a < 0) else (A % B) dest = Bits( 32, c ) asm_code.append( gen_rr_value_test( "rem", src0.uint(), src1.uint(), dest.uint() ) ) return asm_code
def disassemble_field_b_imm(bits): imm = Bits(13, 0) imm[1:5] = bits[tinyrv2_field_slice_b_imm0] imm[5:11] = bits[tinyrv2_field_slice_b_imm1] imm[11:12] = bits[tinyrv2_field_slice_b_imm2] imm[12:13] = bits[tinyrv2_field_slice_b_imm3] return "0x{:0>4x}".format(imm.uint())
def gen_random_test(): asm_code = [] for i in xrange(100): src0 = Bits(32, random.randint(0, 0xffffffff)) src1 = Bits(32, random.randint(0, 0xffffffff)) a = src0.int() b = src1.int() A = -a if (a < 0) else a B = -b if (b < 0) else b c = -(A % B) if (a < 0) else (A % B) dest = Bits(32, c) asm_code.append( gen_rr_value_test("rem", src0.uint(), src1.uint(), dest.uint())) return asm_code
def disassemble_field_s_imm(bits): imm = Bits(12, 0) imm[0:5] = bits[tinyrv2_field_slice_s_imm0] imm[5:12] = bits[tinyrv2_field_slice_s_imm1] return "0x{:0>3x}".format(imm.uint())
def assemble(asm_code): # If asm_code is a single string, then put it in a list to simplify the # rest of the logic. asm_code_list = asm_code if isinstance(asm_code, str): asm_code_list = [asm_code] # Create a single list of lines asm_list = [] for asm_seq in asm_code_list: asm_list.extend(asm_seq.splitlines()) # First pass to create symbol table. This is obviously very simplistic. # We can maybe make it more robust in the future. addr = 0x00000400 sym = {} for line in asm_list: line = line.partition("#")[0] line = line.strip() if line == "": continue if line.startswith(".offset"): (cmd, sep, addr_str) = line.partition(" ") addr = int(addr_str, 0) elif line.startswith(".data"): pass else: (label, sep, rest) = line.partition(":") if sep != "": sym[label.strip()] = addr else: addr += 4 # Second pass to assemble text section asm_list_idx = 0 addr = 0x00000400 text_bytes = bytearray() mngr2proc_bytes = bytearray() proc2mngr_bytes = bytearray() for line in asm_list: asm_list_idx += 1 line = line.partition("#")[0] line = line.strip() if line == "": continue if line.startswith(".offset"): (cmd, sep, addr_str) = line.partition(" ") addr = int(addr_str, 0) elif line.startswith(".data"): break else: if ":" not in line: inst_str = line # First see if we have either a < or a > if "<" in line: (temp, sep, value) = line.partition("<") bits = Bits(32, int(value, 0)) mngr2proc_bytes.extend(struct.pack("<I", bits)) inst_str = temp elif ">" in line: (temp, sep, value) = line.partition(">") bits = Bits(32, int(value, 0)) proc2mngr_bytes.extend(struct.pack("<I", bits)) inst_str = temp bits = assemble_inst(sym, addr, inst_str) text_bytes.extend(struct.pack("<I", bits.uint())) addr += 4 # Assemble data section data_bytes = bytearray() for line in asm_list[asm_list_idx:]: line = line.partition("#")[0] line = line.strip() if line == "": continue if line.startswith(".offset"): (cmd, sep, addr_str) = line.partition(" ") addr = int(addr_str, 0) elif line.startswith(".word"): (cmd, sep, value) = line.partition(" ") data_bytes.extend(struct.pack("<I", int(value, 0))) addr += 4 elif line.startswith(".hword"): (cmd, sep, value) = line.partition(" ") data_bytes.extend(struct.pack("<H", int(value, 0))) addr += 2 elif line.startswith(".byte"): (cmd, sep, value) = line.partition(" ") data_bytes.extend(struct.pack("<B", int(value, 0))) addr += 1 # Construct the corresponding section objects text_section = SparseMemoryImage.Section(".text", 0x0400, text_bytes) data_section = SparseMemoryImage.Section(".data", 0x2000, data_bytes) mngr2proc_section = SparseMemoryImage.Section(".mngr2proc", 0x3000, mngr2proc_bytes) proc2mngr_section = SparseMemoryImage.Section(".proc2mngr", 0x4000, proc2mngr_bytes) # Build a sparse memory image mem_image = SparseMemoryImage() mem_image.add_section(text_section) if len(data_section.data) > 0: mem_image.add_section(data_section) if len(mngr2proc_section.data) > 0: mem_image.add_section(mngr2proc_section) if len(proc2mngr_section.data) > 0: mem_image.add_section(proc2mngr_section) return mem_image
def assemble(asm_code): # If asm_code is a single string, then put it in a list to simplify the # rest of the logic. asm_code_list = asm_code if isinstance(asm_code, str): asm_code_list = [asm_code] # Create a single list of lines asm_list = [] for asm_seq in asm_code_list: asm_list.extend(asm_seq.splitlines()) # First pass to create symbol table. This is obviously very simplistic. # We can maybe make it more robust in the future. addr = 0x00000200 sym = {} for line in asm_list: line = line.partition('#')[0] line = line.strip() if line == "": continue if line.startswith(".offset"): (cmd, sep, addr_str) = line.partition(' ') addr = int(addr_str, 0) elif line.startswith(".data"): pass else: (label, sep, rest) = line.partition(':') if sep != "": sym[label.strip()] = addr else: addr += 4 # Second pass to assemble text section asm_list_idx = 0 addr = 0x00000200 text_bytes = bytearray() mngr2proc_bytes = bytearray() proc2mngr_bytes = bytearray() # Shunning: the way I handle multiple manager is as follows. # # At the beginning the single_core sign is true and all "> 1" "< 2" # values are dumped into the above mngr2proc_bytes and mngr2proc_bytes. # So, for single core testing the assembler works as usual. # # For multicore testing, I assume that all lists wrapped by curly braces # have the same width, and I will use the first ever length as the number # of cores. For example, when I see "> {1,2,3,4}", it means there are 4 # cores. It will then set single_core=False and num_cores=4. # Later if I see "> {1,2,3}" I will throw out assertion error. # # Also, Upon the first occurence of the mentioned curly braces, I will # just duplicate mngr2proc_bytes for #core times, and put the duplicates # into mngrs2procs. Later, when I see a "> 1", I will check the # single_core flag. If it's False, it will dump the check message into # all the duplicated bytearrays. # # The problem of co-existence if we keep mngr2proc and mngrs2procs, is # that unless we record the exact order we receive the csr instructions, # we cannot arbitrarily interleave the values in mngr2proc and mngrs2procs. mngrs2procs_bytes = [] procs2mngrs_bytes = [] single_core = True num_cores = 1 def duplicate(): # duplicate the bytes and no more mngr2proc/proc2mngr for i in xrange(num_cores): mngrs2procs_bytes.append(bytearray()) mngrs2procs_bytes[i][:] = mngr2proc_bytes procs2mngrs_bytes.append(bytearray()) procs2mngrs_bytes[i][:] = proc2mngr_bytes for line in asm_list: asm_list_idx += 1 line = line.partition('#')[0] line = line.strip() if line == "": continue if line.startswith(".offset"): (cmd, sep, addr_str) = line.partition(' ') addr = int(addr_str, 0) elif line.startswith(".data"): break else: if ':' not in line: inst_str = line # First see if we have either a < or a > if '<' in line: (temp, sep, value) = line.partition('<') value = value.lstrip(' ') if value.startswith('{'): values = map(lambda x: int(x, 0), value[1:-1].split(',')) if not single_core and len(values) != num_cores: raise Exception( "Previous curly brace pair has {} elements in between, but this one \"{}\" has {}." .format(num_cores, line, len(values))) if single_core: single_core = False num_cores = len(values) duplicate() for i in xrange(num_cores): mngrs2procs_bytes[i].extend( struct.pack("<I", Bits(32, values[i]))) else: bits = Bits(32, int(value, 0)) if single_core: mngr2proc_bytes.extend(struct.pack("<I", bits)) else: for x in mngrs2procs_bytes: x.extend(struct.pack("<I", bits)) inst_str = temp elif '>' in line: (temp, sep, value) = line.partition('>') value = value.lstrip(' ') if value.startswith('{'): values = map(lambda x: int(x, 0), value[1:-1].split(',')) if not single_core and len(values) != num_cores: raise Exception( "Previous curly brace pair has {} elements in between, but this one \"{}\" has {}." .format(num_cores, line, len(values))) if single_core: single_core = False num_cores = len(values) duplicate() for i in xrange(num_cores): procs2mngrs_bytes[i].extend( struct.pack("<I", Bits(32, values[i]))) else: bits = Bits(32, int(value, 0)) if single_core: proc2mngr_bytes.extend(struct.pack("<I", bits)) else: for x in procs2mngrs_bytes: x.extend(struct.pack("<I", bits)) inst_str = temp bits = assemble_inst(sym, addr, inst_str) text_bytes.extend(struct.pack("<I", bits.uint())) addr += 4 # Assemble data section data_bytes = bytearray() for line in asm_list[asm_list_idx:]: line = line.partition('#')[0] line = line.strip() if line == "": continue if line.startswith(".offset"): (cmd, sep, addr_str) = line.partition(' ') addr = int(addr_str, 0) elif line.startswith(".word"): (cmd, sep, value) = line.partition(' ') data_bytes.extend(struct.pack("<I", int(value, 0))) addr += 4 elif line.startswith(".hword"): (cmd, sep, value) = line.partition(' ') data_bytes.extend(struct.pack("<H", int(value, 0))) addr += 2 elif line.startswith(".byte"): (cmd, sep, value) = line.partition(' ') data_bytes.extend(struct.pack("<B", int(value, 0))) addr += 1 # Construct the corresponding section objects text_section = \ SparseMemoryImage.Section( ".text", 0x0200, text_bytes ) data_section = SparseMemoryImage.Section(".data", 0x2000, data_bytes) # Build a sparse memory image mem_image = SparseMemoryImage() mem_image.add_section(text_section) if len(data_section.data) > 0: mem_image.add_section(data_section) if single_core: mngr2proc_section = \ SparseMemoryImage.Section( ".mngr2proc", 0x13000, mngr2proc_bytes ) if len(mngr2proc_section.data) > 0: mem_image.add_section(mngr2proc_section) proc2mngr_section = \ SparseMemoryImage.Section( ".proc2mngr", 0x14000, proc2mngr_bytes ) if len(proc2mngr_section.data) > 0: mem_image.add_section(proc2mngr_section) else: for i in xrange(len(mngrs2procs_bytes)): img = SparseMemoryImage.Section(".mngr{}_2proc".format(i), 0x15000 + 0x1000 * i, mngrs2procs_bytes[i]) if len(img.data) > 0: mem_image.add_section(img) for i in xrange(len(procs2mngrs_bytes)): img = SparseMemoryImage.Section(".proc{}_2mngr".format(i), 0x16000 + 0x2000 * i, procs2mngrs_bytes[i]) if len(img.data) > 0: mem_image.add_section(img) return mem_image
def assemble(asm_code): # If asm_code is a single string, then put it in a list to simplify the # rest of the logic. asm_code_list = asm_code if isinstance(asm_code, str): asm_code_list = [asm_code] # Create a single list of lines asm_list = [] for asm_seq in asm_code_list: asm_list.extend(asm_seq.splitlines()) # First pass to create symbol table. This is obviously very simplistic. # We can maybe make it more robust in the future. addr = 0x00000400 sym = {} for line in asm_list: line = line.partition('#')[0] line = line.strip() if line == "": continue if line.startswith(".offset"): (cmd, sep, addr_str) = line.partition(' ') addr = int(addr_str, 0) elif line.startswith(".data"): pass else: (label, sep, rest) = line.partition(':') if sep != "": sym[label.strip()] = addr else: addr += 4 # Second pass to assemble text section asm_list_idx = 0 addr = 0x00000400 text_bytes = bytearray() mngr2proc_bytes = bytearray() proc2mngr_bytes = bytearray() for line in asm_list: asm_list_idx += 1 line = line.partition('#')[0] line = line.strip() if line == "": continue if line.startswith(".offset"): (cmd, sep, addr_str) = line.partition(' ') addr = int(addr_str, 0) elif line.startswith(".data"): break else: if ':' not in line: inst_str = line # First see if we have either a < or a > if '<' in line: (temp, sep, value) = line.partition('<') bits = Bits(32, int(value, 0)) mngr2proc_bytes.extend(struct.pack("<I", bits)) inst_str = temp elif '>' in line: (temp, sep, value) = line.partition('>') bits = Bits(32, int(value, 0)) proc2mngr_bytes.extend(struct.pack("<I", bits)) inst_str = temp bits = assemble_inst(sym, addr, inst_str) text_bytes.extend(struct.pack("<I", bits.uint())) addr += 4 # Assemble data section data_bytes = bytearray() for line in asm_list[asm_list_idx:]: line = line.partition('#')[0] line = line.strip() if line == "": continue if line.startswith(".offset"): (cmd, sep, addr_str) = line.partition(' ') addr = int(addr_str, 0) elif line.startswith(".word"): (cmd, sep, value) = line.partition(' ') data_bytes.extend(struct.pack("<I", int(value, 0))) addr += 4 elif line.startswith(".hword"): (cmd, sep, value) = line.partition(' ') data_bytes.extend(struct.pack("<H", int(value, 0))) addr += 2 elif line.startswith(".byte"): (cmd, sep, value) = line.partition(' ') data_bytes.extend(struct.pack("<B", int(value, 0))) addr += 1 # Construct the corresponding section objects text_section = \ SparseMemoryImage.Section( ".text", 0x0400, text_bytes ) data_section = SparseMemoryImage.Section(".data", 0x2000, data_bytes) mngr2proc_section = \ SparseMemoryImage.Section( ".mngr2proc", 0x3000, mngr2proc_bytes ) proc2mngr_section = \ SparseMemoryImage.Section( ".proc2mngr", 0x4000, proc2mngr_bytes ) # Build a sparse memory image mem_image = SparseMemoryImage() mem_image.add_section(text_section) if len(data_section.data) > 0: mem_image.add_section(data_section) if len(mngr2proc_section.data) > 0: mem_image.add_section(mngr2proc_section) if len(proc2mngr_section.data) > 0: mem_image.add_section(proc2mngr_section) return mem_image