def randomize_offset(self): print("randomize_offset " + str(self.num)) addr_ = vsc.rand_int32_t() offset_ = vsc.rand_int32_t() self.offset = [0] * self.num self.addr = [0] * self.num for i in range(self.num): try: with vsc.randomize_with(addr_, offset_): if self.level == level_t.LOW: offset_.inside( vsc.rangelist(vsc.rng(-256, 256))) elif self.level == level_t.MEDIUM: offset_.inside( vsc.rangelist(vsc.rng(-512, 512))) elif self.level == level_t.HIGH: offset_.inside( vsc.rangelist(vsc.rng(-1024, 1024))) addr_ == self.num + offset_ addr_.inside(vsc.rangelist(vsc.rng(0, 32))) except SolveFailure as e: print("Cannot Randomize offset: " + e.diagnostics) self.offset[i] = offset_ self.addr[i] = addr_
def default_c(self): self.sub_program_instr_cnt.size == self.num_of_sub_program self.debug_sub_program_instr_cnt.size == self.num_debug_sub_program self.main_program_instr_cnt in vsc.rangelist( vsc.rng(10, self.instr_cnt)) with vsc.foreach(self.sub_program_instr_cnt, idx=True) as i: self.sub_program_instr_cnt[i].inside( vsc.rangelist(vsc.rng(10, self.instr_cnt)))
def post_randomize(self): order = [] RA = cfg.ra order = [0] * self.num_of_jump_instr self.jump = [0] * self.num_of_jump_instr for i in range(len(order)): order[i] = i random.shuffle(order) self.setup_allowed_instr(1, 1) jal = [riscv_instr_name_t.JAL] if not cfg.disable_compressed_instr: jal.append(riscv_instr_name_t.C_J) if rcs.XLEN == 32: jal.append(riscv_instr_name_t.C_JAL) self.jump_start = riscv_instr_ins.get_instr( riscv_instr_name_t.JAL.name) with self.jump_start.randomize_with() as it: self.jump_start.rd == RA self.jump_start.imm_str = "{}f".format(order[0]) self.jump_start.label = self.label # Last instruction self.jump_end = self.randomize_instr(self.jump_end) self.jump_end.label = "{}".format(self.num_of_jump_instr) for i in range(self.num_of_jump_instr): self.jump[i] = riscv_instr_ins.get_rand_instr( include_instr=[jal[0].name]) with self.jump[i].randomize_with() as it: if self.jump[i].has_rd: vsc.dist(self.jump[i].rd, [ vsc.weight(riscv_reg_t.RA, 5), vsc.weight(vsc.rng(riscv_reg_t.SP, riscv_reg_t.T0), 1), vsc.weight(vsc.rng(riscv_reg_t.T2, riscv_reg_t.T6), 2) ]) self.jump[i].rd.not_inside(cfg.reserved_regs) self.jump[i].label = "{}".format(i) for i in range(len(order)): if i == self.num_of_jump_instr - 1: self.jump[order[i]].imm_str = "{}f".format( self.num_of_jump_instr) else: if order[i + 1] > order[i]: self.jump[order[i]].imm_str = "{}f".format(order[i + 1]) else: self.jump[order[i]].imm_str = "{}b".format(order[i + 1]) self.instr_list.append(self.jump_start) self.instr_list.extend(self.jump) self.instr_list.append(self.jump_end) for i in range(len(self.instr_list)): self.instr_list[i].has_label = 1 self.instr_list[i].atomic = 1
def init_gpr(self): reg_val = vsc.rand_bit_t(pkg_ins.DATA_WIDTH) for i in range(rcs.NUM_GPR): if i in [cfg.sp.value, cfg.tp.value]: continue try: with vsc.randomize_with(reg_val): vsc.dist(reg_val, [vsc.weight(0, 1), vsc.weight(0x80000000, 1), vsc.weight(vsc.rng(0x1, 0xf), 1), vsc.weight(vsc.rng(0x10, 0xefffffff), 1), vsc.weight(vsc.rng(0xf0000000, 0xffffffff), 1)]) except Exception: logging.critical("Cannot Randomize reg_val") sys.exit(1) init_string = "{}li x{}, {}".format(pkg_ins.indent, i, hex(reg_val.get_val())) self.instr_stream.append(init_string)
def addr_c(self): vsc.solve_order(self.data_page_id, self.max_load_store_offset) vsc.solve_order(self.max_load_store_offset, self.base) self.data_page_id < self.max_data_page_id with vsc.foreach(self.data_page, idx = True) as i: with vsc.if_then(i == self.data_page_id): self.max_load_store_offset == self.data_page[i].size_in_bytes self.base in vsc.rangelist(vsc.rng(0, self.max_load_store_offset - 1))
def ab_c(self): self.a in vsc.rangelist(1, 2, 4, 8) self.c != 0 self.d != 0 self.c < self.d self.b in vsc.rangelist(vsc.rng(self.c, self.d))
def ab_c(self): # self.a in vsc.rangelist(self.b+1, [self.b+2,self.c], 8) self.a in vsc.rangelist(1, 2, 4, 8) self.c != 0 self.d != 0 self.c < self.d self.b in vsc.rangelist(vsc.rng(self.c, self.d))
def program_stack_level_c(self): # The stack level is assigned in ascending order to avoid call loop self.stack_level.size == self.program_cnt self.stack_level[0] == 0 with vsc.foreach(self.stack_level, idx=True) as i: with vsc.if_then(i > 0): self.stack_level[i] in vsc.rangelist( vsc.rng(1, self.program_cnt - 1)) self.stack_level[i] >= self.stack_level[i - 1] self.stack_level[i] <= self.stack_level[i - 1] + 1 self.stack_level[i] <= self.max_stack_level
def randomize_avail_regs(self): if self.avail_regs.size > 0: try: with vsc.randomize_with(self.avail_regs): vsc.unique(self.avail_regs) self.avail_regs[0].inside( vsc.rangelist(vsc.rng(riscv_reg_t.S0, riscv_reg_t.A5))) with vsc.foreach(self.avail_regs, idx=True) as i: self.avail_regs[i].not_inside( vsc.rangelist(cfg.reserved_regs, self.reserved_rd)) except Exception: logging.critical("Cannot randomize avail_regs") sys.exit(1)
def gen_stack_enter_instr(self): allow_branch = 0 if (self.illegal_instr_pct > 0 or self.hint_instr_pct > 0) else 1 allow_branch &= not cfg.no_branch_jump try: with vsc.randomize_with(self.program_stack_len): self.program_stack_len in vsc.rangelist(vsc.rng(cfg.min_stack_len_per_program, cfg.max_stack_len_per_program)) self.program_stack_len % (rcs.XLEN // 8) == 0 except Exception: logging.critical("Cannot randomize program_stack_len") sys.exit(1) self.instr_stack_enter.push_start_label = self.label_name + "_stack_p" self.instr_stack_enter.gen_push_stack_instr(self.program_stack_len, allow_branch = allow_branch) self.instr_stream.instr_list.extend((self.instr_stack_enter.instr_list))
def test_distweight(self): PCID = vsc.rand_uint16_t() hist1 = [0]*65536 hist2 = [0]*65536 items = 10000 for i in range(items): # vsc.randomize(PCID) with vsc.randomize_with(PCID, debug=0): # PCID.inside(vsc.rng(1,65534)) vsc.dist(PCID, [ vsc.weight(0, 1), vsc.weight(vsc.rng(1,65534), 900), vsc.weight(65535, 1) ]) # print("PCID: " + str(PCID.get_val())) hist1[PCID.get_val()] += 1 hist2[random.randint(1,65534)] += 1 hist1_hit = 0 hist2_hit = 0 for e in hist1[1:65534]: if e != 0: hist1_hit += 1 for e in hist2[1:65534]: if e != 0: hist2_hit += 1 print("Items: %d hist1=%f hist2=%f" % (items, hist1_hit/65534, hist2_hit/65534)) # Only check deeper if full random actually hit more than weighted if hist1_hit > hist2_hit: # Fail if plain randomization hit 15% more than weighted hist1_15p = int(hist1_hit * 0.15) self.assertLessEqual((hist1_hit-hist2_hit), hist1_15p)
def l_c(self): self.l.size in vsc.rangelist(vsc.rng(2, 10)) self.l[1] == (self.l[0] + 1)
def l_c(self): self.l.size in vsc.rangelist(vsc.rng(2, 10)) with vsc.foreach(self.l, it=False, idx=True) as idx: with vsc.if_then(idx > 0): self.l[idx] == self.l[idx - 1] + 1
def dist_a(self): # vsc.dist(self.a, [vsc.weight(vsc.rng(my_e.A, my_e.C),10), vsc.weight(my_e.D, 20)]) vsc.dist(self.a, [vsc.weight(vsc.rng(my_e.A, my_e.C),3), vsc.weight(my_e.D, 1)])
def init_val_c(self): # TO DO # solve init_val_type before init_val; self.init_val_type.size == self.num_of_avail_regs self.init_val.size == self.num_of_avail_regs self.num_of_instr in vsc.rangelist(vsc.rng(15, 30))
def legal_c(self): self.num_amo == 1 self.num_mixed_instr in vsc.rangelist(vsc.rng(0, 15))
def ab_c(self): self.a.inside(vsc.rangelist(vsc.rng(my_e.A, my_e.C), my_e.D)) self.a >= my_e.A self.a <= my_e.C
def reasonable_c(self): vsc.solve_order(self.num_amo, self.num_mixed_instr) self.num_amo in vsc.rangelist(vsc.rng(1, 10)) self.num_mixed_instr in vsc.rangelist(vsc.rng(0, self.num_amo))
def instr_c(self): self.num_of_jump_instr in vsc.rangelist(vsc.rng(10, 30))
def default_c(self): # TODO Add constraint related to sub_program self.main_program_instr_cnt in vsc.rangelist( vsc.rng(10, self.instr_cnt))
def num_of_rs1_reg_c(self): vsc.solve_order(self.num_amo, self.num_of_rs1_reg) self.num_of_rs1_reg in vsc.rangelist(vsc.rng(1, self.num_amo)) self.num_of_rs1_reg < 5
def init_val_c(self): # TODO vsc.solve_order(self.init_val_type, self.init_val) self.init_val_type.size == 10 # self.num_of_avail_regs self.init_val.size == 10 # self.num_of_avail_regs self.num_of_instr in vsc.rangelist(vsc.rng(15, 30))
def a_small(self): self.a in vsc.rangelist(vsc.rng(1, 10))
def offset_c(self): with vsc.foreach(self.offset, idx=True) as i: self.offset[i] in vsc.rangelist( vsc.rng(0, self.max_offset - 1))
async def run(self): tasks = [] cocotb.fork(self.irq_handler()) self.irq_bfm.add_callback(self.irq_cb) await self.init_dma() while self.n_complete < self.total_xfers: t = cocotb.fork(self.run_xfer()) tasks.append(t) # TODO: Delay a random amount before # starting the next transfer # If all channels are occupied, then clean # up at least enough tasks to allow us to # proceed if self.active_xfers == self.n_channels: desired_active = vsc.rand_uint8_t() with vsc.randomize_with(desired_active): desired_active.inside( vsc.rangelist(vsc.rng(0, self.n_channels - 2))) print("--> waiting for transfers to complete (" + str(desired_active) + ")") while self.active_xfers > int(desired_active): print("active: " + str(self.active_xfers) + " desired: " + str(desired_active)) print("--> Wait for first") await cocotb.triggers.First(*tasks) print("<-- Wait for first") # Now, find completed tasks while len(tasks) > 0: idx = -1 for i, t in enumerate(tasks): if t._finished: idx = i break if idx == -1: print("No more to remove " + str(idx)) break else: print("Remove " + str(idx)) tasks.pop(idx) print("<-- waiting for transfers to complete") # Wait for any pending transfers to complete # First, clean out already-completed transfers while len(tasks) > 0: idx = -1 for i, t in enumerate(tasks): if t._finished: idx = i break if idx == -1: print("No more to remove " + str(idx)) break else: print("Remove " + str(idx)) tasks.pop(idx) if len(tasks) > 0: await cocotb.triggers.Combine(*tasks)
def sz_c(self): self.chksz.inside(vsc.rangelist(vsc.rng(1, 64))) self.totsz.inside(vsc.rangelist(vsc.rng(16, 4095))) self.chksz <= self.totsz
def a_c(self): self.a <= 100 # Not skew self.b in vsc.rangelist(vsc.rng(1, 10))
def default_c(self): self.main_program_instr_cnt in vsc.rangelist( vsc.rng(10, self.instr_cnt))
def a_large(self): self.a in vsc.rangelist(vsc.rng(90, 100))
def ab_c(self): self.c.inside(vsc.rangelist(vsc.rng(0, 1999)))